2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "core/html/track/TextTrack.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/html/HTMLMediaElement.h"
39 #include "core/html/track/TextTrackCueList.h"
40 #include "core/html/track/TextTrackList.h"
41 #include "core/html/track/vtt/VTTRegion.h"
42 #include "core/html/track/vtt/VTTRegionList.h"
43 #include "platform/RuntimeEnabledFeatures.h"
47 static const int invalidTrackIndex = -1;
49 const AtomicString& TextTrack::subtitlesKeyword()
51 DEFINE_STATIC_LOCAL(const AtomicString, subtitles, ("subtitles", AtomicString::ConstructFromLiteral));
55 const AtomicString& TextTrack::captionsKeyword()
57 DEFINE_STATIC_LOCAL(const AtomicString, captions, ("captions", AtomicString::ConstructFromLiteral));
61 const AtomicString& TextTrack::descriptionsKeyword()
63 DEFINE_STATIC_LOCAL(const AtomicString, descriptions, ("descriptions", AtomicString::ConstructFromLiteral));
67 const AtomicString& TextTrack::chaptersKeyword()
69 DEFINE_STATIC_LOCAL(const AtomicString, chapters, ("chapters", AtomicString::ConstructFromLiteral));
73 const AtomicString& TextTrack::metadataKeyword()
75 DEFINE_STATIC_LOCAL(const AtomicString, metadata, ("metadata", AtomicString::ConstructFromLiteral));
79 const AtomicString& TextTrack::disabledKeyword()
81 DEFINE_STATIC_LOCAL(const AtomicString, open, ("disabled", AtomicString::ConstructFromLiteral));
85 const AtomicString& TextTrack::hiddenKeyword()
87 DEFINE_STATIC_LOCAL(const AtomicString, closed, ("hidden", AtomicString::ConstructFromLiteral));
91 const AtomicString& TextTrack::showingKeyword()
93 DEFINE_STATIC_LOCAL(const AtomicString, ended, ("showing", AtomicString::ConstructFromLiteral));
97 TextTrack::TextTrack(const AtomicString& kind, const AtomicString& label, const AtomicString& language, const AtomicString& id, TextTrackType type)
98 : TrackBase(TrackBase::TextTrack, label, language, id)
101 , m_trackList(nullptr)
102 , m_mode(disabledKeyword())
104 , m_readinessState(NotLoaded)
105 , m_trackIndex(invalidTrackIndex)
106 , m_renderedTrackIndex(invalidTrackIndex)
107 , m_hasBeenConfigured(false)
112 TextTrack::~TextTrack()
115 ASSERT(!m_trackList);
118 for (size_t i = 0; i < m_cues->length(); ++i)
119 m_cues->item(i)->setTrack(0);
122 for (size_t i = 0; i < m_regions->length(); ++i)
123 m_regions->item(i)->setTrack(0);
128 bool TextTrack::isValidKindKeyword(const AtomicString& value)
130 if (value == subtitlesKeyword())
132 if (value == captionsKeyword())
134 if (value == descriptionsKeyword())
136 if (value == chaptersKeyword())
138 if (value == metadataKeyword())
144 void TextTrack::setTrackList(TextTrackList* trackList)
146 if (!trackList && mediaElement() && m_cues)
147 mediaElement()->textTrackRemoveCues(this, m_cues.get());
149 m_trackList = trackList;
150 invalidateTrackIndex();
153 void TextTrack::setKind(const AtomicString& newKind)
155 AtomicString oldKind = kind();
156 TrackBase::setKind(newKind);
158 if (mediaElement() && oldKind != kind())
159 mediaElement()->textTrackKindChanged(this);
162 void TextTrack::setMode(const AtomicString& mode)
164 ASSERT(mode == disabledKeyword() || mode == hiddenKeyword() || mode == showingKeyword());
166 // On setting, if the new value isn't equal to what the attribute would currently
167 // return, the new value must be processed as follows ...
171 // If mode changes to disabled, remove this track's cues from the client
172 // because they will no longer be accessible from the cues() function.
173 if (mode == disabledKeyword() && mediaElement() && m_cues)
174 mediaElement()->textTrackRemoveCues(this, m_cues.get());
176 if (mode != showingKeyword() && m_cues)
177 for (size_t i = 0; i < m_cues->length(); ++i)
178 m_cues->item(i)->removeDisplayTree();
183 mediaElement()->textTrackModeChanged(this);
186 TextTrackCueList* TextTrack::cues()
188 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
189 // then the cues attribute must return a live TextTrackCueList object ...
190 // Otherwise, it must return null. When an object is returned, the
191 // same object must be returned each time.
192 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
193 if (m_mode != disabledKeyword())
194 return ensureTextTrackCueList();
198 void TextTrack::removeAllCues()
204 mediaElement()->textTrackRemoveCues(this, m_cues.get());
206 for (size_t i = 0; i < m_cues->length(); ++i)
207 m_cues->item(i)->setTrack(0);
212 TextTrackCueList* TextTrack::activeCues() const
214 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
215 // then the activeCues attribute must return a live TextTrackCueList object ...
216 // ... whose active flag was set when the script started, in text track cue
217 // order. Otherwise, it must return null. When an object is returned, the
218 // same object must be returned each time.
219 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
220 if (m_cues && m_mode != disabledKeyword())
221 return m_cues->activeCues();
225 void TextTrack::addCue(PassRefPtrWillBeRawPtr<TextTrackCue> prpCue)
230 RefPtrWillBeRawPtr<TextTrackCue> cue = prpCue;
232 // TODO(93143): Add spec-compliant behavior for negative time values.
233 if (std::isnan(cue->startTime()) || std::isnan(cue->endTime()) || cue->startTime() < 0 || cue->endTime() < 0)
236 // 4.8.10.12.5 Text track API
238 // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
240 // 1. If the given cue is in a text track list of cues, then remove cue from that text track
242 TextTrack* cueTrack = cue->track();
243 if (cueTrack && cueTrack != this)
244 cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
246 // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
248 ensureTextTrackCueList()->add(cue);
251 mediaElement()->textTrackAddCue(this, cue.get());
254 void TextTrack::removeCue(TextTrackCue* cue, ExceptionState& exceptionState)
259 // 4.8.10.12.5 Text track API
261 // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
263 // 1. If the given cue is not currently listed in the method's TextTrack
264 // object's text track's text track list of cues, then throw a NotFoundError exception.
265 if (cue->track() != this) {
266 exceptionState.throwDOMException(NotFoundError, "The specified cue is not listed in the TextTrack's list of cues.");
270 // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
271 if (!m_cues || !m_cues->remove(cue)) {
272 exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified cue.");
278 mediaElement()->textTrackRemoveCue(this, cue);
281 VTTRegionList* TextTrack::ensureVTTRegionList()
284 m_regions = VTTRegionList::create();
286 return m_regions.get();
289 VTTRegionList* TextTrack::regions()
291 // If the text track mode of the text track that the TextTrack object
292 // represents is not the text track disabled mode, then the regions
293 // attribute must return a live VTTRegionList object that represents
294 // the text track list of regions of the text track. Otherwise, it must
295 // return null. When an object is returned, the same object must be returned
297 if (RuntimeEnabledFeatures::webVTTRegionsEnabled() && m_mode != disabledKeyword())
298 return ensureVTTRegionList();
302 void TextTrack::addRegion(PassRefPtrWillBeRawPtr<VTTRegion> prpRegion)
307 RefPtrWillBeRawPtr<VTTRegion> region = prpRegion;
308 VTTRegionList* regionList = ensureVTTRegionList();
310 // 1. If the given region is in a text track list of regions, then remove
311 // region from that text track list of regions.
312 TextTrack* regionTrack = region->track();
313 if (regionTrack && regionTrack != this)
314 regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
316 // 2. If the method's TextTrack object's text track list of regions contains
317 // a region with the same identifier as region replace the values of that
318 // region's width, height, anchor point, viewport anchor point and scroll
319 // attributes with those of region.
320 VTTRegion* existingRegion = regionList->getRegionById(region->id());
321 if (existingRegion) {
322 existingRegion->updateParametersFromRegion(region.get());
326 // Otherwise: add region to the method's TextTrack object's text track
328 region->setTrack(this);
329 regionList->add(region);
332 void TextTrack::removeRegion(VTTRegion* region, ExceptionState &exceptionState)
337 // 1. If the given region is not currently listed in the method's TextTrack
338 // object's text track list of regions, then throw a NotFoundError exception.
339 if (region->track() != this) {
340 exceptionState.throwDOMException(NotFoundError, "The specified region is not listed in the TextTrack's list of regions.");
344 if (!m_regions || !m_regions->remove(region)) {
345 exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified region.");
352 void TextTrack::cueWillChange(TextTrackCue* cue)
357 // The cue may need to be repositioned in the media element's interval tree, may need to
358 // be re-rendered, etc, so remove it before the modification...
359 mediaElement()->textTrackRemoveCue(this, cue);
362 void TextTrack::cueDidChange(TextTrackCue* cue)
367 // Make sure the TextTrackCueList order is up-to-date.
368 ensureTextTrackCueList()->updateCueIndex(cue);
370 // ... and add it back again.
371 mediaElement()->textTrackAddCue(this, cue);
374 int TextTrack::trackIndex()
378 if (m_trackIndex == invalidTrackIndex)
379 m_trackIndex = m_trackList->getTrackIndex(this);
384 void TextTrack::invalidateTrackIndex()
386 m_trackIndex = invalidTrackIndex;
387 m_renderedTrackIndex = invalidTrackIndex;
390 bool TextTrack::isRendered()
392 if (kind() != captionsKeyword() && kind() != subtitlesKeyword())
395 if (m_mode != showingKeyword())
401 TextTrackCueList* TextTrack::ensureTextTrackCueList()
404 m_cues = TextTrackCueList::create();
409 int TextTrack::trackIndexRelativeToRenderedTracks()
413 if (m_renderedTrackIndex == invalidTrackIndex)
414 m_renderedTrackIndex = m_trackList->getTrackIndexRelativeToRenderedTracks(this);
416 return m_renderedTrackIndex;
419 const AtomicString& TextTrack::interfaceName() const
421 return EventTargetNames::TextTrack;
424 ExecutionContext* TextTrack::executionContext() const
426 HTMLMediaElement* owner = mediaElement();
427 return owner ? owner->executionContext() : 0;
430 HTMLMediaElement* TextTrack::mediaElement() const
432 return m_trackList ? m_trackList->owner() : 0;
435 Node* TextTrack::owner() const
437 return mediaElement();
440 void TextTrack::trace(Visitor* visitor)
442 visitor->trace(m_cues);
443 visitor->trace(m_regions);
444 visitor->trace(m_trackList);
445 TrackBase::trace(visitor);
446 EventTargetWithInlineData::trace(visitor);