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)
109 ScriptWrappable::init(this);
113 TextTrack::~TextTrack()
116 ASSERT(!m_trackList);
119 for (size_t i = 0; i < m_cues->length(); ++i)
120 m_cues->item(i)->setTrack(0);
123 for (size_t i = 0; i < m_regions->length(); ++i)
124 m_regions->item(i)->setTrack(0);
129 bool TextTrack::isValidKindKeyword(const AtomicString& value)
131 if (value == subtitlesKeyword())
133 if (value == captionsKeyword())
135 if (value == descriptionsKeyword())
137 if (value == chaptersKeyword())
139 if (value == metadataKeyword())
145 void TextTrack::setTrackList(TextTrackList* trackList)
147 if (!trackList && mediaElement() && m_cues)
148 mediaElement()->textTrackRemoveCues(this, m_cues.get());
150 m_trackList = trackList;
151 invalidateTrackIndex();
154 void TextTrack::setKind(const AtomicString& newKind)
156 AtomicString oldKind = kind();
157 TrackBase::setKind(newKind);
159 if (mediaElement() && oldKind != kind())
160 mediaElement()->textTrackKindChanged(this);
163 void TextTrack::setMode(const AtomicString& mode)
165 ASSERT(mode == disabledKeyword() || mode == hiddenKeyword() || mode == showingKeyword());
167 // On setting, if the new value isn't equal to what the attribute would currently
168 // return, the new value must be processed as follows ...
172 // If mode changes to disabled, remove this track's cues from the client
173 // because they will no longer be accessible from the cues() function.
174 if (mode == disabledKeyword() && mediaElement() && m_cues)
175 mediaElement()->textTrackRemoveCues(this, m_cues.get());
177 if (mode != showingKeyword() && m_cues)
178 for (size_t i = 0; i < m_cues->length(); ++i)
179 m_cues->item(i)->removeDisplayTree();
184 mediaElement()->textTrackModeChanged(this);
187 TextTrackCueList* TextTrack::cues()
189 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
190 // then the cues attribute must return a live TextTrackCueList object ...
191 // Otherwise, it must return null. When an object is returned, the
192 // same object must be returned each time.
193 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
194 if (m_mode != disabledKeyword())
195 return ensureTextTrackCueList();
199 void TextTrack::removeAllCues()
205 mediaElement()->textTrackRemoveCues(this, m_cues.get());
207 for (size_t i = 0; i < m_cues->length(); ++i)
208 m_cues->item(i)->setTrack(0);
213 TextTrackCueList* TextTrack::activeCues() const
215 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
216 // then the activeCues attribute must return a live TextTrackCueList object ...
217 // ... whose active flag was set when the script started, in text track cue
218 // order. Otherwise, it must return null. When an object is returned, the
219 // same object must be returned each time.
220 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
221 if (m_cues && m_mode != disabledKeyword())
222 return m_cues->activeCues();
226 void TextTrack::addCue(PassRefPtrWillBeRawPtr<TextTrackCue> prpCue)
231 RefPtrWillBeRawPtr<TextTrackCue> cue = prpCue;
233 // TODO(93143): Add spec-compliant behavior for negative time values.
234 if (std::isnan(cue->startTime()) || std::isnan(cue->endTime()) || cue->startTime() < 0 || cue->endTime() < 0)
237 // 4.8.10.12.5 Text track API
239 // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
241 // 1. If the given cue is in a text track list of cues, then remove cue from that text track
243 TextTrack* cueTrack = cue->track();
244 if (cueTrack && cueTrack != this)
245 cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
247 // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
249 ensureTextTrackCueList()->add(cue);
252 mediaElement()->textTrackAddCue(this, cue.get());
255 void TextTrack::removeCue(TextTrackCue* cue, ExceptionState& exceptionState)
260 // 4.8.10.12.5 Text track API
262 // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
264 // 1. If the given cue is not currently listed in the method's TextTrack
265 // object's text track's text track list of cues, then throw a NotFoundError exception.
266 if (cue->track() != this) {
267 exceptionState.throwDOMException(NotFoundError, "The specified cue is not listed in the TextTrack's list of cues.");
271 // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
272 if (!m_cues || !m_cues->remove(cue)) {
273 exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified cue.");
279 mediaElement()->textTrackRemoveCue(this, cue);
282 VTTRegionList* TextTrack::ensureVTTRegionList()
285 m_regions = VTTRegionList::create();
287 return m_regions.get();
290 VTTRegionList* TextTrack::regions()
292 // If the text track mode of the text track that the TextTrack object
293 // represents is not the text track disabled mode, then the regions
294 // attribute must return a live VTTRegionList object that represents
295 // the text track list of regions of the text track. Otherwise, it must
296 // return null. When an object is returned, the same object must be returned
298 if (RuntimeEnabledFeatures::webVTTRegionsEnabled() && m_mode != disabledKeyword())
299 return ensureVTTRegionList();
303 void TextTrack::addRegion(PassRefPtrWillBeRawPtr<VTTRegion> prpRegion)
308 RefPtrWillBeRawPtr<VTTRegion> region = prpRegion;
309 VTTRegionList* regionList = ensureVTTRegionList();
311 // 1. If the given region is in a text track list of regions, then remove
312 // region from that text track list of regions.
313 TextTrack* regionTrack = region->track();
314 if (regionTrack && regionTrack != this)
315 regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
317 // 2. If the method's TextTrack object's text track list of regions contains
318 // a region with the same identifier as region replace the values of that
319 // region's width, height, anchor point, viewport anchor point and scroll
320 // attributes with those of region.
321 VTTRegion* existingRegion = regionList->getRegionById(region->id());
322 if (existingRegion) {
323 existingRegion->updateParametersFromRegion(region.get());
327 // Otherwise: add region to the method's TextTrack object's text track
329 region->setTrack(this);
330 regionList->add(region);
333 void TextTrack::removeRegion(VTTRegion* region, ExceptionState &exceptionState)
338 // 1. If the given region is not currently listed in the method's TextTrack
339 // object's text track list of regions, then throw a NotFoundError exception.
340 if (region->track() != this) {
341 exceptionState.throwDOMException(NotFoundError, "The specified region is not listed in the TextTrack's list of regions.");
345 if (!m_regions || !m_regions->remove(region)) {
346 exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified region.");
353 void TextTrack::cueWillChange(TextTrackCue* cue)
358 // The cue may need to be repositioned in the media element's interval tree, may need to
359 // be re-rendered, etc, so remove it before the modification...
360 mediaElement()->textTrackRemoveCue(this, cue);
363 void TextTrack::cueDidChange(TextTrackCue* cue)
368 // Make sure the TextTrackCueList order is up-to-date.
369 ensureTextTrackCueList()->updateCueIndex(cue);
371 // ... and add it back again.
372 mediaElement()->textTrackAddCue(this, cue);
375 int TextTrack::trackIndex()
379 if (m_trackIndex == invalidTrackIndex)
380 m_trackIndex = m_trackList->getTrackIndex(this);
385 void TextTrack::invalidateTrackIndex()
387 m_trackIndex = invalidTrackIndex;
388 m_renderedTrackIndex = invalidTrackIndex;
391 bool TextTrack::isRendered()
393 if (kind() != captionsKeyword() && kind() != subtitlesKeyword())
396 if (m_mode != showingKeyword())
402 TextTrackCueList* TextTrack::ensureTextTrackCueList()
405 m_cues = TextTrackCueList::create();
410 int TextTrack::trackIndexRelativeToRenderedTracks()
414 if (m_renderedTrackIndex == invalidTrackIndex)
415 m_renderedTrackIndex = m_trackList->getTrackIndexRelativeToRenderedTracks(this);
417 return m_renderedTrackIndex;
420 const AtomicString& TextTrack::interfaceName() const
422 return EventTargetNames::TextTrack;
425 ExecutionContext* TextTrack::executionContext() const
427 HTMLMediaElement* owner = mediaElement();
428 return owner ? owner->executionContext() : 0;
431 HTMLMediaElement* TextTrack::mediaElement() const
433 return m_trackList ? m_trackList->owner() : 0;
436 Node* TextTrack::owner() const
438 return mediaElement();
441 void TextTrack::trace(Visitor* visitor)
443 visitor->trace(m_cues);
444 visitor->trace(m_regions);
445 visitor->trace(m_trackList);
446 TrackBase::trace(visitor);
447 EventTargetWithInlineData::trace(visitor);