2 * Copyright (C) 2011, 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2011-2014 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.
34 #if ENABLE(VIDEO_TRACK)
36 #include "TextTrack.h"
39 #include "HTMLMediaElement.h"
40 #include "SourceBuffer.h"
41 #include "TextTrackCueList.h"
42 #include "TextTrackList.h"
43 #include "VTTRegion.h"
44 #include "VTTRegionList.h"
48 static const int invalidTrackIndex = -1;
50 const AtomicString& TextTrack::subtitlesKeyword()
52 static NeverDestroyed<const AtomicString> subtitles("subtitles", AtomicString::ConstructFromLiteral);
56 static const AtomicString& captionsKeyword()
58 static NeverDestroyed<const AtomicString> captions("captions", AtomicString::ConstructFromLiteral);
62 static const AtomicString& descriptionsKeyword()
64 static NeverDestroyed<const AtomicString> descriptions("descriptions", AtomicString::ConstructFromLiteral);
68 static const AtomicString& chaptersKeyword()
70 static NeverDestroyed<const AtomicString> chapters("chapters", AtomicString::ConstructFromLiteral);
74 static const AtomicString& metadataKeyword()
76 static NeverDestroyed<const AtomicString> metadata("metadata", AtomicString::ConstructFromLiteral);
80 static const AtomicString& forcedKeyword()
82 static NeverDestroyed<const AtomicString> forced("forced", AtomicString::ConstructFromLiteral);
86 TextTrack* TextTrack::captionMenuOffItem()
88 static TextTrack& off = TextTrack::create(0, 0, "off menu item", emptyAtom, emptyAtom, emptyAtom).leakRef();
92 TextTrack* TextTrack::captionMenuAutomaticItem()
94 static TextTrack& automatic = TextTrack::create(0, 0, "automatic menu item", emptyAtom, emptyAtom, emptyAtom).leakRef();
98 TextTrack::TextTrack(ScriptExecutionContext* context, TextTrackClient* client, const AtomicString& kind, const AtomicString& id, const AtomicString& label, const AtomicString& language, TextTrackType type)
99 : TrackBase(TrackBase::TextTrack, id, label, language)
100 , m_scriptExecutionContext(context)
103 , m_trackIndex(invalidTrackIndex)
104 , m_renderedTrackIndex(invalidTrackIndex)
106 if (kind == captionsKeyword())
107 m_kind = Kind::Captions;
108 else if (kind == chaptersKeyword())
109 m_kind = Kind::Chapters;
110 else if (kind == descriptionsKeyword())
111 m_kind = Kind::Descriptions;
112 else if (kind == forcedKeyword())
113 m_kind = Kind::Forced;
114 else if (kind == metadataKeyword())
115 m_kind = Kind::Metadata;
118 TextTrack::~TextTrack()
122 m_client->textTrackRemoveCues(this, m_cues.get());
124 for (size_t i = 0; i < m_cues->length(); ++i)
125 m_cues->item(i)->setTrack(nullptr);
128 for (size_t i = 0; i < m_regions->length(); ++i)
129 m_regions->item(i)->setTrack(nullptr);
133 bool TextTrack::enabled() const
135 return m_mode != Mode::Disabled;
138 bool TextTrack::isValidKindKeyword(const AtomicString& value)
140 if (value == subtitlesKeyword())
142 if (value == captionsKeyword())
144 if (value == descriptionsKeyword())
146 if (value == chaptersKeyword())
148 if (value == metadataKeyword())
150 if (value == forcedKeyword())
156 const AtomicString& TextTrack::kindKeyword() const
160 return captionsKeyword();
162 return chaptersKeyword();
163 case Kind::Descriptions:
164 return descriptionsKeyword();
166 return forcedKeyword();
168 return metadataKeyword();
169 case Kind::Subtitles:
170 return subtitlesKeyword();
172 ASSERT_NOT_REACHED();
173 return subtitlesKeyword();
176 void TextTrack::setKind(Kind newKind)
178 auto oldKind = m_kind;
180 // 10.1 kind, on setting:
181 // 1. If the value being assigned to this attribute does not match one of the text track kinds,
182 // then abort these steps.
184 // 2. Update this attribute to the new value.
187 #if ENABLE(MEDIA_SOURCE)
188 // 3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple
189 // event named change at sourceBuffer.textTracks.
191 m_sourceBuffer->textTracks()->scheduleChangeEvent();
193 // 4. Queue a task to fire a simple event named change at the TextTrackList object referenced by
194 // the textTracks attribute on the HTMLMediaElement.
196 mediaElement()->textTracks().scheduleChangeEvent();
199 if (m_client && oldKind != m_kind)
200 m_client->textTrackKindChanged(this);
203 void TextTrack::setKindKeywordIgnoringASCIICase(StringView keyword)
205 if (keyword.isNull()) {
206 // The missing value default is the subtitles state.
207 setKind(Kind::Subtitles);
210 if (equalLettersIgnoringASCIICase(keyword, "captions"))
211 setKind(Kind::Captions);
212 else if (equalLettersIgnoringASCIICase(keyword, "chapters"))
213 setKind(Kind::Chapters);
214 else if (equalLettersIgnoringASCIICase(keyword, "descriptions"))
215 setKind(Kind::Descriptions);
216 else if (equalLettersIgnoringASCIICase(keyword, "forced"))
217 setKind(Kind::Forced);
218 else if (equalLettersIgnoringASCIICase(keyword, "metadata"))
219 setKind(Kind::Metadata);
220 else if (equalLettersIgnoringASCIICase(keyword, "subtitles"))
221 setKind(Kind::Subtitles);
223 // The invalid value default is the metadata state.
224 setKind(Kind::Metadata);
228 void TextTrack::setMode(Mode mode)
230 // On setting, if the new value isn't equal to what the attribute would currently
231 // return, the new value must be processed as follows ...
235 // If mode changes to disabled, remove this track's cues from the client
236 // because they will no longer be accessible from the cues() function.
237 if (mode == Mode::Disabled && m_client && m_cues)
238 m_client->textTrackRemoveCues(this, m_cues.get());
240 if (mode != Mode::Showing && m_cues) {
241 for (size_t i = 0; i < m_cues->length(); ++i) {
242 TextTrackCue* cue = m_cues->item(i);
243 if (cue->isRenderable())
244 toVTTCue(cue)->removeDisplayTree();
251 m_client->textTrackModeChanged(this);
254 TextTrackCueList* TextTrack::cues()
256 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
257 // then the cues attribute must return a live TextTrackCueList object ...
258 // Otherwise, it must return null. When an object is returned, the
259 // same object must be returned each time.
260 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
261 if (m_mode == Mode::Disabled)
263 return &ensureTextTrackCueList();
266 void TextTrack::removeAllCues()
272 m_client->textTrackRemoveCues(this, m_cues.get());
274 for (size_t i = 0; i < m_cues->length(); ++i)
275 m_cues->item(i)->setTrack(nullptr);
280 TextTrackCueList* TextTrack::activeCues() const
282 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
283 // then the activeCues attribute must return a live TextTrackCueList object ...
284 // ... whose active flag was set when the script started, in text track cue
285 // order. Otherwise, it must return null. When an object is returned, the
286 // same object must be returned each time.
287 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
288 if (!m_cues || m_mode == Mode::Disabled)
290 return m_cues->activeCues();
293 void TextTrack::addCue(PassRefPtr<TextTrackCue> prpCue, ExceptionCode& ec)
298 RefPtr<TextTrackCue> cue = prpCue;
300 // 4.7.10.12.6 Text tracks exposing in-band metadata
301 // The UA will use DataCue to expose only text track cue objects that belong to a text track that has a text
302 // track kind of metadata.
303 // If a DataCue is added to a TextTrack via the addCue() method but the text track does not have its text
304 // track kind set to metadata, throw a InvalidNodeTypeError exception and don't add the cue to the TextTrackList
306 if (cue->cueType() == TextTrackCue::Data && m_kind != Kind::Metadata) {
307 ec = INVALID_NODE_TYPE_ERR;
311 // TODO(93143): Add spec-compliant behavior for negative time values.
312 if (!cue->startMediaTime().isValid() || !cue->endMediaTime().isValid() || cue->startMediaTime() < MediaTime::zeroTime() || cue->endMediaTime() < MediaTime::zeroTime())
315 // 4.8.10.12.5 Text track API
317 // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
319 // 1. If the given cue is in a text track list of cues, then remove cue from that text track
321 TextTrack* cueTrack = cue->track();
322 if (cueTrack && cueTrack != this)
323 cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
325 // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
327 ensureTextTrackCueList().add(cue);
330 m_client->textTrackAddCue(this, *cue);
333 void TextTrack::removeCue(TextTrackCue* cue, ExceptionCode& ec)
338 // 4.8.10.12.5 Text track API
340 // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
342 // 1. If the given cue is not currently listed in the method's TextTrack
343 // object's text track's text track list of cues, then throw a NotFoundError exception.
344 if (cue->track() != this) {
349 // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
350 if (!m_cues || !m_cues->remove(cue)) {
351 ec = INVALID_STATE_ERR;
357 m_client->textTrackRemoveCue(this, *cue);
360 VTTRegionList& TextTrack::ensureVTTRegionList()
363 m_regions = VTTRegionList::create();
368 VTTRegionList* TextTrack::regions()
370 // If the text track mode of the text track that the TextTrack object
371 // represents is not the text track disabled mode, then the regions
372 // attribute must return a live VTTRegionList object that represents
373 // the text track list of regions of the text track. Otherwise, it must
374 // return null. When an object is returned, the same object must be returned
376 if (m_mode == Mode::Disabled)
378 return &ensureVTTRegionList();
381 void TextTrack::addRegion(PassRefPtr<VTTRegion> prpRegion)
386 RefPtr<VTTRegion> region = prpRegion;
387 VTTRegionList& regionList = ensureVTTRegionList();
389 // 1. If the given region is in a text track list of regions, then remove
390 // region from that text track list of regions.
391 TextTrack* regionTrack = region->track();
392 if (regionTrack && regionTrack != this)
393 regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
395 // 2. If the method's TextTrack object's text track list of regions contains
396 // a region with the same identifier as region replace the values of that
397 // region's width, height, anchor point, viewport anchor point and scroll
398 // attributes with those of region.
399 VTTRegion* existingRegion = regionList.getRegionById(region->id());
400 if (existingRegion) {
401 existingRegion->updateParametersFromRegion(region.get());
405 // Otherwise: add region to the method's TextTrack object's text track
407 region->setTrack(this);
408 regionList.add(region);
411 void TextTrack::removeRegion(VTTRegion* region, ExceptionCode &ec)
416 // 1. If the given region is not currently listed in the method's TextTrack
417 // object's text track list of regions, then throw a NotFoundError exception.
418 if (region->track() != this) {
423 if (!m_regions || !m_regions->remove(region)) {
424 ec = INVALID_STATE_ERR;
431 void TextTrack::cueWillChange(TextTrackCue* cue)
436 // The cue may need to be repositioned in the media element's interval tree, may need to
437 // be re-rendered, etc, so remove it before the modification...
438 m_client->textTrackRemoveCue(this, *cue);
441 void TextTrack::cueDidChange(TextTrackCue* cue)
446 // Make sure the TextTrackCueList order is up-to-date.
447 ensureTextTrackCueList().updateCueIndex(cue);
449 // ... and add it back again.
450 m_client->textTrackAddCue(this, *cue);
453 int TextTrack::trackIndex()
455 ASSERT(m_mediaElement);
457 if (m_trackIndex == invalidTrackIndex)
458 m_trackIndex = m_mediaElement->textTracks().getTrackIndex(*this);
463 void TextTrack::invalidateTrackIndex()
465 m_trackIndex = invalidTrackIndex;
466 m_renderedTrackIndex = invalidTrackIndex;
469 bool TextTrack::isRendered()
471 return (m_kind == Kind::Captions || m_kind == Kind::Subtitles || m_kind == Kind::Forced)
472 && m_mode == Mode::Showing;
475 TextTrackCueList& TextTrack::ensureTextTrackCueList()
478 m_cues = TextTrackCueList::create();
482 int TextTrack::trackIndexRelativeToRenderedTracks()
484 ASSERT(m_mediaElement);
486 if (m_renderedTrackIndex == invalidTrackIndex)
487 m_renderedTrackIndex = m_mediaElement->textTracks().getTrackIndexRelativeToRenderedTracks(*this);
489 return m_renderedTrackIndex;
492 bool TextTrack::hasCue(TextTrackCue* cue, TextTrackCue::CueMatchRules match)
494 if (cue->startMediaTime() < MediaTime::zeroTime() || cue->endMediaTime() < MediaTime::zeroTime())
497 if (!m_cues || !m_cues->length())
500 size_t searchStart = 0;
501 size_t searchEnd = m_cues->length();
504 ASSERT(searchStart <= m_cues->length());
505 ASSERT(searchEnd <= m_cues->length());
507 TextTrackCue* existingCue;
509 // Cues in the TextTrackCueList are maintained in start time order.
510 if (searchStart == searchEnd) {
514 // If there is more than one cue with the same start time, back up to first one so we
515 // consider all of them.
516 while (searchStart >= 2 && cue->hasEquivalentStartTime(*m_cues->item(searchStart - 2)))
519 bool firstCompare = true;
523 firstCompare = false;
524 if (searchStart > m_cues->length())
527 existingCue = m_cues->item(searchStart - 1);
531 if (cue->startMediaTime() > (existingCue->startMediaTime() + startTimeVariance()))
534 if (existingCue->isEqual(*cue, match))
539 size_t index = (searchStart + searchEnd) / 2;
540 existingCue = m_cues->item(index);
541 if ((cue->startMediaTime() + startTimeVariance()) < existingCue->startMediaTime() || (match != TextTrackCue::IgnoreDuration && cue->hasEquivalentStartTime(*existingCue) && cue->endMediaTime() > existingCue->endMediaTime()))
544 searchStart = index + 1;
547 ASSERT_NOT_REACHED();
551 bool TextTrack::isMainProgramContent() const
553 // "Main program" content is intrinsic to the presentation of the media file, regardless of locale. Content such as
554 // directors commentary is not "main program" because it is not essential for the presentation. HTML5 doesn't have
555 // a way to express this in a machine-reable form, it is typically done with the track label, so we assume that caption
556 // tracks are main content and all other track types are not.
557 return m_kind == Kind::Captions;
560 bool TextTrack::containsOnlyForcedSubtitles() const
562 return m_kind == Kind::Forced;
565 #if ENABLE(MEDIA_SOURCE)
567 void TextTrack::setLanguage(const AtomicString& language)
569 // 11.1 language, on setting:
570 // 1. If the value being assigned to this attribute is not an empty string or a BCP 47 language
571 // tag[BCP47], then abort these steps.
572 // FIXME(123926): Validate the BCP47-ness of langague.
574 // 2. Update this attribute to the new value.
575 TrackBase::setLanguage(language);
577 // 3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple
578 // event named change at sourceBuffer.textTracks.
580 m_sourceBuffer->textTracks()->scheduleChangeEvent();
582 // 4. Queue a task to fire a simple event named change at the TextTrackList object referenced by
583 // the textTracks attribute on the HTMLMediaElement.
585 mediaElement()->textTracks().scheduleChangeEvent();
590 } // namespace WebCore