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