Bug 1281999 - Update the display when we remove an active cue by changing the texttrackmode to disabled. r=rillian
authorbechen <bechen@mozilla.com>
Mon, 18 Jul 2016 15:39:14 +0800
changeset 389627 c74b25c022b7318115a6e0c85d264d0b2808a4a2
parent 389626 61f5cb9b36df99327afc958e6a34b0c8ac876e48
child 389628 564fdbb916ba41e6186031ac633f733d5405ed92
push id23471
push userbmo:tchiovoloni@mozilla.com
push dateTue, 19 Jul 2016 18:52:45 +0000
reviewersrillian
bugs1281999
milestone50.0a1
Bug 1281999 - Update the display when we remove an active cue by changing the texttrackmode to disabled. r=rillian MozReview-Commit-ID: GeF5Icd9jLu
dom/html/TextTrackManager.cpp
dom/html/TextTrackManager.h
dom/media/TextTrack.cpp
dom/media/TextTrackList.cpp
dom/media/TextTrackList.h
--- a/dom/html/TextTrackManager.cpp
+++ b/dom/html/TextTrackManager.cpp
@@ -219,16 +219,18 @@ TextTrackManager::DidSeek()
     mLastTimeMarchesOnCalled = mMediaElement->CurrentTime();
   }
   mHasSeeked = true;
 }
 
 void
 TextTrackManager::UpdateCueDisplay()
 {
+  mUpdateCueDisplayDispatched = false;
+
   if (!mMediaElement || !mTextTracks) {
     return;
   }
 
   nsIFrame* frame = mMediaElement->GetPrimaryFrame();
   nsVideoFrame* videoFrame = do_QueryFrame(frame);
   if (!videoFrame) {
     return;
@@ -236,26 +238,25 @@ TextTrackManager::UpdateCueDisplay()
 
   nsCOMPtr<nsIContent> overlay = videoFrame->GetCaptionOverlay();
   nsCOMPtr<nsIContent> controls = videoFrame->GetVideoControls();
   if (!overlay) {
     return;
   }
 
   nsTArray<RefPtr<TextTrackCue> > activeCues;
-  mTextTracks->UpdateAndGetShowingCues(activeCues);
+  mTextTracks->GetShowingCues(activeCues);
 
   if (activeCues.Length() > 0) {
     RefPtr<nsVariantCC> jsCues = new nsVariantCC();
 
     jsCues->SetAsArray(nsIDataType::VTYPE_INTERFACE,
                        &NS_GET_IID(nsIDOMEventTarget),
                        activeCues.Length(),
                        static_cast<void*>(activeCues.Elements()));
-
     nsPIDOMWindowInner* window = mMediaElement->OwnerDoc()->GetInnerWindow();
     if (window) {
       sParserWrapper->ProcessCues(window, jsCues, overlay, controls);
     }
   } else if (overlay->Length() > 0) {
     nsContentUtils::SetNodeTextContent(overlay, EmptyString(), true);
   }
 }
@@ -272,16 +273,20 @@ TextTrackManager::NotifyCueAdded(TextTra
 
 void
 TextTrackManager::NotifyCueRemoved(TextTrackCue& aCue)
 {
   if (mNewCues) {
     mNewCues->RemoveCue(aCue);
   }
   DispatchTimeMarchesOn();
+  if (aCue.GetActive()) {
+    // We remove an active cue, need to update the display.
+    DispatchUpdateCueDisplay();
+  }
 }
 
 void
 TextTrackManager::PopulatePendingList()
 {
   if (!mTextTracks || !mPendingTextTracks || !mMediaElement) {
     return;
   }
@@ -519,16 +524,26 @@ public:
   {
     return mTextTracks.SafeElementAt(aIndex, nullptr);
   }
 private:
   nsTArray<RefPtr<TextTrack>> mTextTracks;
 };
 
 void
+TextTrackManager::DispatchUpdateCueDisplay()
+{
+  if (!mUpdateCueDisplayDispatched && !mShutdown &&
+      (mMediaElement->GetHasUserInteraction() || mMediaElement->IsCurrentlyPlaying())) {
+    NS_DispatchToMainThread(NewRunnableMethod(this, &TextTrackManager::UpdateCueDisplay));
+    mUpdateCueDisplayDispatched = true;
+  }
+}
+
+void
 TextTrackManager::DispatchTimeMarchesOn()
 {
   // Run the algorithm if no previous instance is still running, otherwise
   // enqueue the current playback position and whether only that changed
   // through its usual monotonic increase during normal playback; current
   // executing call upon completion will check queue for further 'work'.
   if (!mTimeMarchesOnDispatched && !mShutdown &&
       (mMediaElement->GetHasUserInteraction() || mMediaElement->IsCurrentlyPlaying())) {
--- a/dom/html/TextTrackManager.h
+++ b/dom/html/TextTrackManager.h
@@ -87,16 +87,17 @@ public:
 
   void AddListeners();
 
   // The HTMLMediaElement that this TextTrackManager manages the TextTracks of.
   RefPtr<HTMLMediaElement> mMediaElement;
 
   void DispatchTimeMarchesOn();
   void TimeMarchesOn();
+  void DispatchUpdateCueDisplay();
 
   void NotifyShutdown()
   {
     mShutdown = true;
   }
 
   void NotifyCueUpdated(TextTrackCue *aCue);
 
@@ -123,16 +124,17 @@ private:
 
   // True if the media player playback changed due to seeking prior to and
   // during running the "Time Marches On" algorithm.
   bool mHasSeeked;
   // Playback position at the time of last "Time Marches On" call
   double mLastTimeMarchesOnCalled;
 
   bool mTimeMarchesOnDispatched;
+  bool mUpdateCueDisplayDispatched;
 
   static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper;
 
   bool performedTrackSelection;
 
   // Runs the algorithm for performing automatic track selection.
   void HonorUserPreferencesForTrackSelection();
   // Performs track selection for a single TextTrackKind.
--- a/dom/media/TextTrack.cpp
+++ b/dom/media/TextTrack.cpp
@@ -88,26 +88,26 @@ TextTrack::WrapObject(JSContext* aCx, JS
 }
 
 void
 TextTrack::SetMode(TextTrackMode aValue)
 {
   if (mMode != aValue) {
     mMode = aValue;
     if (aValue == TextTrackMode::Disabled) {
-      SetCuesInactive();
       // Remove all the cues in MediaElement.
       if (mTextTrackList) {
         HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
         if (mediaElement) {
           for (size_t i = 0; i < mCueList->Length(); ++i) {
             mediaElement->NotifyCueRemoved(*(*mCueList)[i]);
           }
         }
       }
+      SetCuesInactive();
     } else {
       // Add all the cues into MediaElement.
       if (mTextTrackList) {
         HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
         if (mediaElement) {
           for (size_t i = 0; i < mCueList->Length(); ++i) {
             mediaElement->NotifyCueAdded(*(*mCueList)[i]);
           }
--- a/dom/media/TextTrackList.cpp
+++ b/dom/media/TextTrackList.cpp
@@ -35,29 +35,21 @@ TextTrackList::TextTrackList(nsPIDOMWind
 {
 }
 
 TextTrackList::~TextTrackList()
 {
 }
 
 void
-TextTrackList::UpdateAndGetShowingCues(nsTArray<RefPtr<TextTrackCue> >& aCues)
+TextTrackList::GetShowingCues(nsTArray<RefPtr<TextTrackCue> >& aCues)
 {
   nsTArray< RefPtr<TextTrackCue> > cues;
   for (uint32_t i = 0; i < Length(); i++) {
-    TextTrackMode mode = mTextTracks[i]->Mode();
-    // If the mode is hidden then we just need to update the active cue list,
-    // we don't need to show it on the video.
-    if (mode == TextTrackMode::Hidden) {
-      mTextTracks[i]->UpdateActiveCueList();
-    } else if (mode == TextTrackMode::Showing) {
-      // If the mode is showing then we need to update the cue list and show it
-      // on the video. GetActiveCueArray() calls UpdateActiveCueList() so we
-      // don't need to call it explicitly.
+    if (mTextTracks[i]->Mode() == TextTrackMode::Showing) {
       mTextTracks[i]->GetActiveCueArray(cues);
       aCues.AppendElements(cues);
     }
   }
 }
 
 JSObject*
 TextTrackList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
--- a/dom/media/TextTrackList.h
+++ b/dom/media/TextTrackList.h
@@ -32,17 +32,17 @@ public:
   JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   uint32_t Length() const
   {
     return mTextTracks.Length();
   }
 
   // Get all the current active cues.
-  void UpdateAndGetShowingCues(nsTArray<RefPtr<TextTrackCue> >& aCues);
+  void GetShowingCues(nsTArray<RefPtr<TextTrackCue> >& aCues);
 
   TextTrack* IndexedGetter(uint32_t aIndex, bool& aFound);
   TextTrack* operator[](uint32_t aIndex);
 
   already_AddRefed<TextTrack> AddTextTrack(TextTrackKind aKind,
                                            const nsAString& aLabel,
                                            const nsAString& aLanguage,
                                            TextTrackMode aMode,