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/. */
7 #include "mozilla/dom/TextTrack.h"
8 #include "mozilla/dom/TextTrackBinding.h"
9 #include "mozilla/dom/TextTrackCue.h"
10 #include "mozilla/dom/TextTrackCueList.h"
11 #include "mozilla/dom/TextTrackRegion.h"
12 #include "mozilla/dom/TextTrackRegionList.h"
13 #include "mozilla/dom/HTMLMediaElement.h"
14 #include "mozilla/dom/HTMLTrackElement.h"
19 NS_IMPL_CYCLE_COLLECTION_INHERITED_5(TextTrack
,
20 nsDOMEventTargetHelper
,
27 NS_IMPL_ADDREF_INHERITED(TextTrack
, nsDOMEventTargetHelper
)
28 NS_IMPL_RELEASE_INHERITED(TextTrack
, nsDOMEventTargetHelper
)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrack
)
30 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper
)
32 TextTrack::TextTrack(nsISupports
* aParent
)
39 TextTrack::TextTrack(nsISupports
* aParent
, HTMLMediaElement
* aMediaElement
)
41 , mMediaElement(aMediaElement
)
47 TextTrack::TextTrack(nsISupports
* aParent
,
48 HTMLMediaElement
* aMediaElement
,
50 const nsAString
& aLabel
,
51 const nsAString
& aLanguage
)
53 , mMediaElement(aMediaElement
)
58 mLanguage
= aLanguage
;
63 TextTrack::SetDefaultSettings()
65 mKind
= TextTrackKind::Subtitles
;
66 mMode
= TextTrackMode::Hidden
;
67 mCueList
= new TextTrackCueList(mParent
);
68 mActiveCueList
= new TextTrackCueList(mParent
);
69 mRegionList
= new TextTrackRegionList(mParent
);
72 mReadyState
= HTMLTrackElement::NONE
;
76 TextTrack::Update(double aTime
)
79 mCueList
->Update(aTime
);
84 TextTrack::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aScope
)
86 return TextTrackBinding::Wrap(aCx
, aScope
, this);
90 TextTrack::SetMode(TextTrackMode aValue
)
96 TextTrack::AddCue(TextTrackCue
& aCue
)
98 mCueList
->AddCue(aCue
);
103 TextTrack::RemoveCue(TextTrackCue
& aCue
, ErrorResult
& aRv
)
105 mCueList
->RemoveCue(aCue
, aRv
);
110 TextTrack::CueChanged(TextTrackCue
& aCue
)
112 //XXX: Implement Cue changed. Bug 867823.
116 TextTrack::AddRegion(TextTrackRegion
& aRegion
)
118 TextTrackRegion
* region
= mRegionList
->GetRegionById(aRegion
.Id());
120 aRegion
.SetTextTrack(this);
121 mRegionList
->AddTextTrackRegion(&aRegion
);
125 region
->CopyValues(aRegion
);
129 TextTrack::RemoveRegion(const TextTrackRegion
& aRegion
, ErrorResult
& aRv
)
131 if (!mRegionList
->GetRegionById(aRegion
.Id())) {
132 aRv
.Throw(NS_ERROR_DOM_NOT_FOUND_ERR
);
136 mRegionList
->RemoveTextTrackRegion(aRegion
);
140 TextTrack::GetActiveCues()
142 if (mMode
== TextTrackMode::Disabled
|| !mMediaElement
) {
146 // If we are dirty, i.e. an event happened that may cause the sorted mCueList
147 // to have changed like a seek or an insert for a cue, than we need to rebuild
148 // the active cue list from scratch.
152 mActiveCueList
->RemoveAll();
155 double playbackTime
= mMediaElement
->CurrentTime();
156 // Remove all the cues from the active cue list whose end times now occur
157 // earlier then the current playback time. When we reach a cue whose end time
158 // is valid we can safely stop iterating as the list is sorted.
159 for (uint32_t i
= 0; i
< mActiveCueList
->Length() &&
160 (*mActiveCueList
)[i
]->EndTime() < playbackTime
; i
++) {
161 mActiveCueList
->RemoveCueAt(i
);
163 // Add all the cues, starting from the position of the last cue that was
164 // added, that have valid start and end times for the current playback time.
165 // We can stop iterating safely once we encounter a cue that does not have
166 // valid times for the current playback time as the cue list is sorted.
167 for (; mCuePos
< mCueList
->Length(); mCuePos
++) {
168 TextTrackCue
* cue
= (*mCueList
)[mCuePos
];
169 if (cue
->StartTime() > playbackTime
|| cue
->EndTime() < playbackTime
) {
172 mActiveCueList
->AddCue(*cue
);
174 return mActiveCueList
;
178 TextTrack::ReadyState() const
184 TextTrack::SetReadyState(uint16_t aState
)
186 mReadyState
= aState
;
187 if (mMediaElement
&& (mReadyState
== HTMLTrackElement::LOADED
||
188 mReadyState
== HTMLTrackElement::ERROR
)) {
189 mMediaElement
->RemoveTextTrack(this, true);
194 } // namespace mozilla