TextTrack.h (5738B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 et tw=78: */ 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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_TextTrack_h 8 #define mozilla_dom_TextTrack_h 9 10 #include "TimeUnits.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/DefineEnum.h" 13 #include "mozilla/dom/TextTrackBinding.h" 14 #include "nsCOMPtr.h" 15 #include "nsCycleCollectionParticipant.h" 16 #include "nsString.h" 17 18 namespace mozilla::dom { 19 20 class TextTrackList; 21 class TextTrackCue; 22 class TextTrackCueList; 23 class HTMLTrackElement; 24 class HTMLMediaElement; 25 26 enum class TextTrackSource : uint8_t { 27 Track, 28 AddTextTrack, 29 MediaResourceSpecific, 30 }; 31 32 // Constants for numeric readyState property values. 33 MOZ_DEFINE_ENUM_CLASS_WITH_BASE_AND_TOSTRING(TextTrackReadyState, uint8_t, 34 (NotLoaded, Loading, Loaded, 35 FailedToLoad)); 36 37 class TextTrack final : public DOMEventTargetHelper { 38 public: 39 NS_DECL_ISUPPORTS_INHERITED 40 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextTrack, DOMEventTargetHelper) 41 42 TextTrack(nsPIDOMWindowInner* aOwnerWindow, TextTrackKind aKind, 43 const nsAString& aLabel, const nsAString& aLanguage, 44 TextTrackMode aMode, TextTrackReadyState aReadyState, 45 TextTrackSource aTextTrackSource); 46 TextTrack(nsPIDOMWindowInner* aOwnerWindow, TextTrackList* aTextTrackList, 47 TextTrackKind aKind, const nsAString& aLabel, 48 const nsAString& aLanguage, TextTrackMode aMode, 49 TextTrackReadyState aReadyState, TextTrackSource aTextTrackSource); 50 51 void SetDefaultSettings(); 52 53 JSObject* WrapObject(JSContext* aCx, 54 JS::Handle<JSObject*> aGivenProto) override; 55 56 TextTrackKind Kind() const { return mKind; } 57 void GetLabel(nsAString& aLabel) const; 58 void GetLanguage(nsAString& aLanguage) const; 59 void GetInBandMetadataTrackDispatchType(nsAString& aType) const { 60 aType = mType; 61 } 62 void GetId(nsAString& aId) const; 63 64 TextTrackMode Mode() const { return mMode; } 65 void SetMode(TextTrackMode aValue); 66 67 TextTrackCueList* GetCues() const { 68 if (mMode == TextTrackMode::Disabled) { 69 return nullptr; 70 } 71 return mCueList; 72 } 73 74 TextTrackCueList* GetActiveCues(); 75 void GetActiveCueArray(nsTArray<RefPtr<TextTrackCue>>& aCues); 76 77 TextTrackReadyState ReadyState() const; 78 void SetReadyState(TextTrackReadyState aState); 79 80 void AddCue(TextTrackCue& aCue); 81 void RemoveCue(TextTrackCue& aCue, ErrorResult& aRv); 82 void SetDirty() { mDirty = true; } 83 void SetCuesDirty(); 84 85 TextTrackList* GetTextTrackList(); 86 void SetTextTrackList(TextTrackList* aTextTrackList); 87 88 IMPL_EVENT_HANDLER(cuechange) 89 90 HTMLTrackElement* GetTrackElement(); 91 void SetTrackElement(HTMLTrackElement* aTrackElement); 92 93 TextTrackSource GetTextTrackSource() { return mTextTrackSource; } 94 95 void SetCuesInactive(); 96 97 void NotifyCueUpdated(TextTrackCue* aCue); 98 99 void DispatchAsyncTrustedEvent(const nsString& aEventName); 100 101 bool IsLoaded(); 102 103 // Called when associated cue's active flag has been changed, and then we 104 // would add or remove the cue to the active cue list. 105 void NotifyCueActiveStateChanged(TextTrackCue* aCue); 106 107 enum class CueActivityState : uint8_t { Inactive = 0, Active, All, Count }; 108 class CueBuckets final { 109 public: 110 void AddCue(TextTrackCue* aCue); 111 112 nsTArray<RefPtr<TextTrackCue>>& ActiveCues() { 113 return mCues[static_cast<size_t>(CueActivityState::Active)]; 114 } 115 116 nsTArray<RefPtr<TextTrackCue>>& InactiveCues() { 117 return mCues[static_cast<size_t>(CueActivityState::Inactive)]; 118 } 119 120 nsTArray<RefPtr<TextTrackCue>>& AllCues() { 121 return mCues[static_cast<size_t>(CueActivityState::All)]; 122 } 123 124 bool HasPauseOnExit(CueActivityState aState) const { 125 MOZ_DIAGNOSTIC_ASSERT(aState != CueActivityState::Count); 126 return mHasPauseOnExist[static_cast<uint8_t>(aState)]; 127 } 128 129 private: 130 nsTArray<RefPtr<TextTrackCue>> 131 mCues[static_cast<size_t>(CueActivityState::Count)]; 132 133 // Returns true if any cue has in the given stage has the PauseOnExit flag 134 // set to true. 135 bool mHasPauseOnExist[static_cast<size_t>(CueActivityState::Count)] = { 136 false}; 137 }; 138 139 // Use this function to get `current cues`, `other cues` and `miss cues` 140 // which are overlapping with the given interval. 141 // The `current cues` have start time are less than or equal to the current 142 // playback position and whose end times are greater than the current playback 143 // position. the `other cues` are not in the current cues. 144 // `aLastTime` is the last time defined in the time marches on step3, it will 145 // only exists when miss cues calculation is needed. 146 void GetOverlappingCurrentOtherAndMissCues( 147 CueBuckets* aCurrentCues, CueBuckets* aOtherCues, CueBuckets* aMissCues, 148 const media::TimeInterval& aInterval, 149 const Maybe<double>& aLastTime) const; 150 151 void ClearAllCues(); 152 153 private: 154 ~TextTrack(); 155 156 HTMLMediaElement* GetMediaElement() const; 157 158 RefPtr<TextTrackList> mTextTrackList; 159 160 TextTrackKind mKind; 161 nsString mLabel; 162 nsString mLanguage; 163 nsString mType; 164 TextTrackMode mMode; 165 166 RefPtr<TextTrackCueList> mCueList; 167 RefPtr<TextTrackCueList> mActiveCueList; 168 RefPtr<HTMLTrackElement> mTrackElement; 169 170 uint32_t mCuePos; 171 TextTrackReadyState mReadyState; 172 bool mDirty; 173 174 // An enum that represents where the track was sourced from. 175 TextTrackSource mTextTrackSource; 176 }; 177 178 } // namespace mozilla::dom 179 180 #endif // mozilla_dom_TextTrack_h