content/media/TextTrackList.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/TextTrackList.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/dom/TextTrackList.h"
    1.10 +#include "mozilla/dom/TextTrackListBinding.h"
    1.11 +#include "mozilla/dom/TrackEvent.h"
    1.12 +#include "nsThreadUtils.h"
    1.13 +#include "mozilla/dom/TextTrackCue.h"
    1.14 +#include "mozilla/dom/TextTrackManager.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +namespace dom {
    1.18 +
    1.19 +NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrackList,
    1.20 +                                   DOMEventTargetHelper,
    1.21 +                                   mTextTracks,
    1.22 +                                   mTextTrackManager)
    1.23 +
    1.24 +NS_IMPL_ADDREF_INHERITED(TextTrackList, DOMEventTargetHelper)
    1.25 +NS_IMPL_RELEASE_INHERITED(TextTrackList, DOMEventTargetHelper)
    1.26 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackList)
    1.27 +NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
    1.28 +
    1.29 +TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow)
    1.30 +  : DOMEventTargetHelper(aOwnerWindow)
    1.31 +{
    1.32 +}
    1.33 +
    1.34 +TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow,
    1.35 +                             TextTrackManager* aTextTrackManager)
    1.36 + : DOMEventTargetHelper(aOwnerWindow)
    1.37 + , mTextTrackManager(aTextTrackManager)
    1.38 +{
    1.39 +}
    1.40 +
    1.41 +void
    1.42 +TextTrackList::UpdateAndGetShowingCues(nsTArray<nsRefPtr<TextTrackCue> >& aCues)
    1.43 +{
    1.44 +  nsTArray< nsRefPtr<TextTrackCue> > cues;
    1.45 +  for (uint32_t i = 0; i < Length(); i++) {
    1.46 +    TextTrackMode mode = mTextTracks[i]->Mode();
    1.47 +    // If the mode is hidden then we just need to update the active cue list,
    1.48 +    // we don't need to show it on the video.
    1.49 +    if (mode == TextTrackMode::Hidden) {
    1.50 +      mTextTracks[i]->UpdateActiveCueList();
    1.51 +    } else if (mode == TextTrackMode::Showing) {
    1.52 +      // If the mode is showing then we need to update the cue list and show it
    1.53 +      // on the video. GetActiveCueArray() calls UpdateActiveCueList() so we
    1.54 +      // don't need to call it explicitly.
    1.55 +      mTextTracks[i]->GetActiveCueArray(cues);
    1.56 +      aCues.AppendElements(cues);
    1.57 +    }
    1.58 +  }
    1.59 +}
    1.60 +
    1.61 +JSObject*
    1.62 +TextTrackList::WrapObject(JSContext* aCx)
    1.63 +{
    1.64 +  return TextTrackListBinding::Wrap(aCx, this);
    1.65 +}
    1.66 +
    1.67 +TextTrack*
    1.68 +TextTrackList::IndexedGetter(uint32_t aIndex, bool& aFound)
    1.69 +{
    1.70 +  aFound = aIndex < mTextTracks.Length();
    1.71 +  return aFound ? mTextTracks[aIndex] : nullptr;
    1.72 +}
    1.73 +
    1.74 +TextTrack*
    1.75 +TextTrackList::operator[](uint32_t aIndex)
    1.76 +{
    1.77 +  return mTextTracks.SafeElementAt(aIndex, nullptr);
    1.78 +}
    1.79 +
    1.80 +already_AddRefed<TextTrack>
    1.81 +TextTrackList::AddTextTrack(TextTrackKind aKind,
    1.82 +                            const nsAString& aLabel,
    1.83 +                            const nsAString& aLanguage,
    1.84 +                            TextTrackMode aMode,
    1.85 +                            TextTrackReadyState aReadyState,
    1.86 +                            TextTrackSource aTextTrackSource,
    1.87 +                            const CompareTextTracks& aCompareTT)
    1.88 +{
    1.89 +  nsRefPtr<TextTrack> track = new TextTrack(GetOwner(), this, aKind, aLabel,
    1.90 +                                            aLanguage, aMode, aReadyState,
    1.91 +                                            aTextTrackSource);
    1.92 +  AddTextTrack(track, aCompareTT);
    1.93 +  return track.forget();
    1.94 +}
    1.95 +
    1.96 +void
    1.97 +TextTrackList::AddTextTrack(TextTrack* aTextTrack,
    1.98 +                            const CompareTextTracks& aCompareTT)
    1.99 +{
   1.100 +  if (mTextTracks.InsertElementSorted(aTextTrack, aCompareTT)) {
   1.101 +    aTextTrack->SetTextTrackList(this);
   1.102 +    CreateAndDispatchTrackEventRunner(aTextTrack, NS_LITERAL_STRING("addtrack"));
   1.103 +  }
   1.104 +}
   1.105 +
   1.106 +TextTrack*
   1.107 +TextTrackList::GetTrackById(const nsAString& aId)
   1.108 +{
   1.109 +  nsAutoString id;
   1.110 +  for (uint32_t i = 0; i < Length(); i++) {
   1.111 +    mTextTracks[i]->GetId(id);
   1.112 +    if (aId.Equals(id)) {
   1.113 +      return mTextTracks[i];
   1.114 +    }
   1.115 +  }
   1.116 +  return nullptr;
   1.117 +}
   1.118 +
   1.119 +void
   1.120 +TextTrackList::RemoveTextTrack(TextTrack* aTrack)
   1.121 +{
   1.122 +  if (mTextTracks.RemoveElement(aTrack)) {
   1.123 +    CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("removetrack"));
   1.124 +  }
   1.125 +}
   1.126 +
   1.127 +void
   1.128 +TextTrackList::DidSeek()
   1.129 +{
   1.130 +  for (uint32_t i = 0; i < mTextTracks.Length(); i++) {
   1.131 +    mTextTracks[i]->SetDirty();
   1.132 +  }
   1.133 +}
   1.134 +
   1.135 +class TrackEventRunner MOZ_FINAL: public nsRunnable
   1.136 +{
   1.137 +public:
   1.138 +  TrackEventRunner(TextTrackList* aList, nsIDOMEvent* aEvent)
   1.139 +    : mList(aList)
   1.140 +    , mEvent(aEvent)
   1.141 +  {}
   1.142 +
   1.143 +  NS_IMETHOD Run() MOZ_OVERRIDE
   1.144 +  {
   1.145 +    return mList->DispatchTrackEvent(mEvent);
   1.146 +  }
   1.147 +
   1.148 +private:
   1.149 +  nsRefPtr<TextTrackList> mList;
   1.150 +  nsRefPtr<nsIDOMEvent> mEvent;
   1.151 +};
   1.152 +
   1.153 +nsresult
   1.154 +TextTrackList::DispatchTrackEvent(nsIDOMEvent* aEvent)
   1.155 +{
   1.156 +  return DispatchTrustedEvent(aEvent);
   1.157 +}
   1.158 +
   1.159 +void
   1.160 +TextTrackList::CreateAndDispatchChangeEvent()
   1.161 +{
   1.162 +  nsCOMPtr<nsIDOMEvent> event;
   1.163 +  nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
   1.164 +  if (NS_FAILED(rv)) {
   1.165 +    NS_WARNING("Failed to create the error event!");
   1.166 +    return;
   1.167 +  }
   1.168 +
   1.169 +  rv = event->InitEvent(NS_LITERAL_STRING("change"), false, false);
   1.170 +  if (NS_FAILED(rv)) {
   1.171 +    NS_WARNING("Failed to init the change event!");
   1.172 +    return;
   1.173 +  }
   1.174 +
   1.175 +  event->SetTrusted(true);
   1.176 +
   1.177 +  nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
   1.178 +  NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
   1.179 +}
   1.180 +
   1.181 +void
   1.182 +TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack,
   1.183 +                                                 const nsAString& aEventName)
   1.184 +{
   1.185 +  TrackEventInit eventInit;
   1.186 +  eventInit.mBubbles = false;
   1.187 +  eventInit.mCancelable = false;
   1.188 +  eventInit.mTrack = aTrack;
   1.189 +  nsRefPtr<TrackEvent> event =
   1.190 +    TrackEvent::Constructor(this, aEventName, eventInit);
   1.191 +
   1.192 +  // Dispatch the TrackEvent asynchronously.
   1.193 +  nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
   1.194 +  NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
   1.195 +}
   1.196 +
   1.197 +HTMLMediaElement*
   1.198 +TextTrackList::GetMediaElement()
   1.199 +{
   1.200 +  if (mTextTrackManager) {
   1.201 +    return mTextTrackManager->mMediaElement;
   1.202 +  }
   1.203 +  return nullptr;
   1.204 +}
   1.205 +
   1.206 +void
   1.207 +TextTrackList::SetTextTrackManager(TextTrackManager* aTextTrackManager)
   1.208 +{
   1.209 +  mTextTrackManager = aTextTrackManager;
   1.210 +}
   1.211 +
   1.212 +} // namespace dom
   1.213 +} // namespace mozilla

mercurial