TextTrackManager.h (6525B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_TextTrackManager_h 8 #define mozilla_dom_TextTrackManager_h 9 10 #include "TimeUnits.h" 11 #include "mozilla/StaticPtr.h" 12 #include "mozilla/dom/TextTrack.h" 13 #include "mozilla/dom/TextTrackCueList.h" 14 #include "mozilla/dom/TextTrackList.h" 15 #include "nsContentUtils.h" 16 #include "nsIDOMEventListener.h" 17 18 class nsIWebVTTParserWrapper; 19 20 namespace mozilla { 21 template <typename T> 22 class Maybe; 23 namespace dom { 24 25 class HTMLMediaElement; 26 27 class CompareTextTracks { 28 private: 29 HTMLMediaElement* mMediaElement; 30 Maybe<uint32_t> TrackChildPosition(TextTrack* aTrack) const; 31 32 public: 33 explicit CompareTextTracks(HTMLMediaElement* aMediaElement); 34 bool Equals(TextTrack* aOne, TextTrack* aTwo) const; 35 bool LessThan(TextTrack* aOne, TextTrack* aTwo) const; 36 }; 37 38 class TextTrack; 39 class TextTrackCue; 40 41 class TextTrackManager final : public nsIDOMEventListener { 42 ~TextTrackManager(); 43 44 public: 45 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 46 NS_DECL_CYCLE_COLLECTION_CLASS(TextTrackManager) 47 48 NS_DECL_NSIDOMEVENTLISTENER 49 50 explicit TextTrackManager(HTMLMediaElement* aMediaElement); 51 52 TextTrackList* GetTextTracks() const; 53 already_AddRefed<TextTrack> AddTextTrack(TextTrackKind aKind, 54 const nsAString& aLabel, 55 const nsAString& aLanguage, 56 TextTrackMode aMode, 57 TextTrackReadyState aReadyState, 58 TextTrackSource aTextTrackSource); 59 void AddTextTrack(TextTrack* aTextTrack); 60 void RemoveTextTrack(TextTrack* aTextTrack, bool aPendingListOnly); 61 void DidSeek(); 62 63 void NotifyCueAdded(TextTrackCue& aCue); 64 void AddCues(TextTrack* aTextTrack); 65 void NotifyCueRemoved(TextTrackCue& aCue); 66 /** 67 * Overview of WebVTT cuetext and anonymous content setup. 68 * 69 * WebVTT nodes are the parsed version of WebVTT cuetext. WebVTT cuetext is 70 * the portion of a WebVTT cue that specifies what the caption will actually 71 * show up as on screen. 72 * 73 * WebVTT cuetext can contain markup that loosely relates to HTML markup. It 74 * can contain tags like <b>, <u>, <i>, <c>, <v>, <ruby>, <rt>, <lang>, 75 * including timestamp tags. 76 * 77 * When the caption is ready to be displayed the WebVTT nodes are converted 78 * over to anonymous DOM content. <i>, <u>, <b>, <ruby>, and <rt> all become 79 * HTMLElements of their corresponding HTML markup tags. <c> and <v> are 80 * converted to <span> tags. Timestamp tags are converted to XML processing 81 * instructions. Additionally, all cuetext tags support specifying of classes. 82 * This takes the form of <foo.class.subclass>. These classes are then parsed 83 * and set as the anonymous content's class attribute. 84 * 85 * Rules on constructing DOM objects from WebVTT nodes can be found here 86 * http://dev.w3.org/html5/webvtt/#webvtt-cue-text-dom-construction-rules. 87 * Current rules are taken from revision on April 15, 2013. 88 */ 89 90 void PopulatePendingList(); 91 92 void AddListeners(); 93 94 // The HTMLMediaElement that this TextTrackManager manages the TextTracks of. 95 RefPtr<HTMLMediaElement> mMediaElement; 96 97 void DispatchTimeMarchesOn(); 98 void TimeMarchesOn(); 99 void DispatchUpdateCueDisplay(); 100 101 void NotifyShutdown() { mShutdown = true; } 102 103 void NotifyCueUpdated(TextTrackCue* aCue); 104 105 void NotifyReset(); 106 107 bool IsLoaded(); 108 109 private: 110 /** 111 * Converts the TextTrackCue's cuetext into a tree of DOM objects 112 * and attaches it to a div on its owning TrackElement's 113 * MediaElement's caption overlay. 114 */ 115 void UpdateCueDisplay(); 116 117 // List of the TextTrackManager's owning HTMLMediaElement's TextTracks. 118 RefPtr<TextTrackList> mTextTracks; 119 // List of text track objects awaiting loading. 120 RefPtr<TextTrackList> mPendingTextTracks; 121 // List of newly introduced Text Track cues. 122 123 // Contain all cues for a MediaElement. Not sorted. 124 RefPtr<TextTrackCueList> mNewCues; 125 126 // True if the media player playback changed due to seeking prior to and 127 // during running the "Time Marches On" algorithm. 128 bool mHasSeeked; 129 // Playback position at the time of last "Time Marches On" call 130 media::TimeUnit mLastTimeMarchesOnCalled; 131 132 bool mTimeMarchesOnDispatched; 133 bool mUpdateCueDisplayDispatched; 134 135 static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper; 136 137 bool performedTrackSelection; 138 139 // Runs the algorithm for performing automatic track selection. 140 void HonorUserPreferencesForTrackSelection(); 141 // Performs track selection for a single TextTrackKind. 142 void PerformTrackSelection(TextTrackKind aTextTrackKind); 143 // Performs track selection for a set of TextTrackKinds, for example, 144 // 'subtitles' and 'captions' should be selected together. 145 void PerformTrackSelection(TextTrackKind aTextTrackKinds[], uint32_t size); 146 void GetTextTracksOfKinds(TextTrackKind aTextTrackKinds[], uint32_t size, 147 nsTArray<TextTrack*>& aTextTracks); 148 void GetTextTracksOfKind(TextTrackKind aTextTrackKind, 149 nsTArray<TextTrack*>& aTextTracks); 150 bool TrackIsDefault(TextTrack* aTextTrack); 151 152 bool IsShutdown() const; 153 154 // This function will check media element's show poster flag to decide whether 155 // we need to run `TimeMarchesOn`. 156 void MaybeRunTimeMarchesOn(); 157 158 class ShutdownObserverProxy final : public nsIObserver { 159 NS_DECL_ISUPPORTS 160 161 public: 162 explicit ShutdownObserverProxy(TextTrackManager* aManager) 163 : mManager(aManager) { 164 nsContentUtils::RegisterShutdownObserver(this); 165 } 166 167 NS_IMETHODIMP Observe(nsISupports* aSubject, const char* aTopic, 168 const char16_t* aData) override { 169 MOZ_ASSERT(NS_IsMainThread()); 170 if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) { 171 if (mManager) { 172 mManager->NotifyShutdown(); 173 } 174 Unregister(); 175 } 176 return NS_OK; 177 } 178 179 void Unregister(); 180 181 private: 182 ~ShutdownObserverProxy() = default; 183 184 TextTrackManager* mManager; 185 }; 186 187 RefPtr<ShutdownObserverProxy> mShutdownProxy; 188 bool mShutdown; 189 }; 190 191 } // namespace dom 192 } // namespace mozilla 193 194 #endif // mozilla_dom_TextTrackManager_h