2 * Copyright (C) 2011 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 "TextTrackRegionList.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::forcedKeyword()
81 DEFINE_STATIC_LOCAL(const AtomicString, forced, ("forced", AtomicString::ConstructFromLiteral));
85 const AtomicString& TextTrack::disabledKeyword()
87 DEFINE_STATIC_LOCAL(const AtomicString, open, ("disabled", AtomicString::ConstructFromLiteral));
91 const AtomicString& TextTrack::hiddenKeyword()
93 DEFINE_STATIC_LOCAL(const AtomicString, closed, ("hidden", AtomicString::ConstructFromLiteral));
97 const AtomicString& TextTrack::showingKeyword()
99 DEFINE_STATIC_LOCAL(const AtomicString, ended, ("showing", AtomicString::ConstructFromLiteral));
103 TextTrack* TextTrack::captionMenuOffItem()
105 static TextTrack* off = TextTrack::create(0, 0, "off menu item", "", "", "").leakRef();
109 TextTrack* TextTrack::captionMenuAutomaticItem()
111 static TextTrack* automatic = TextTrack::create(0, 0, "automatic menu item", "", "", "").leakRef();
115 TextTrack::TextTrack(ScriptExecutionContext* context, TextTrackClient* client, const AtomicString& kind, const AtomicString& id, const AtomicString& label, const AtomicString& language, TextTrackType type)
116 : TrackBase(TrackBase::TextTrack, id, label, language)
118 #if ENABLE(WEBVTT_REGIONS)
121 , m_scriptExecutionContext(context)
122 , m_mode(disabledKeyword().string())
125 , m_readinessState(NotLoaded)
126 , m_trackIndex(invalidTrackIndex)
127 , m_renderedTrackIndex(invalidTrackIndex)
128 , m_hasBeenConfigured(false)
130 setKindInternal(kind);
133 TextTrack::~TextTrack()
137 m_client->textTrackRemoveCues(this, m_cues.get());
139 for (size_t i = 0; i < m_cues->length(); ++i)
140 m_cues->item(i)->setTrack(0);
141 #if ENABLE(WEBVTT_REGIONS)
142 for (size_t i = 0; i < m_regions->length(); ++i)
143 m_regions->item(i)->setTrack(0);
149 bool TextTrack::isValidKind(const AtomicString& value) const
151 return TextTrack::isValidKindKeyword(value);
154 bool TextTrack::enabled() const
156 return m_mode != disabledKeyword();
159 bool TextTrack::isValidKindKeyword(const AtomicString& value)
161 if (value == subtitlesKeyword())
163 if (value == captionsKeyword())
165 if (value == descriptionsKeyword())
167 if (value == chaptersKeyword())
169 if (value == metadataKeyword())
171 if (value == forcedKeyword())
177 void TextTrack::setKind(const AtomicString& newKind)
179 String oldKind = kind();
181 #if ENABLE(MEDIA_SOURCE)
182 // 10.1 kind, on setting:
183 // 1. If the value being assigned to this attribute does not match one of the text track kinds,
184 // then abort these steps.
185 if (!isValidKindKeyword(newKind))
188 // 2. Update this attribute to the new value.
189 setKindInternal(newKind);
191 // 3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple
192 // event named change at sourceBuffer.textTracks.
194 m_sourceBuffer->textTracks()->scheduleChangeEvent();
196 // 4. Queue a task to fire a simple event named change at the TextTrackList object referenced by
197 // the textTracks attribute on the HTMLMediaElement.
199 mediaElement()->textTracks()->scheduleChangeEvent();
201 TrackBase::setKind(newKind);
204 if (m_client && oldKind != kind())
205 m_client->textTrackKindChanged(this);
208 void TextTrack::setMode(const AtomicString& mode)
210 // On setting, if the new value isn't equal to what the attribute would currently
211 // return, the new value must be processed as follows ...
212 if (mode != disabledKeyword() && mode != hiddenKeyword() && mode != showingKeyword())
218 // If mode changes to disabled, remove this track's cues from the client
219 // because they will no longer be accessible from the cues() function.
220 if (mode == disabledKeyword() && m_client && m_cues)
221 m_client->textTrackRemoveCues(this, m_cues.get());
223 if (mode != showingKeyword() && m_cues) {
224 for (size_t i = 0; i < m_cues->length(); ++i) {
225 TextTrackCue* cue = m_cues->item(i);
226 if (cue->isRenderable())
227 toVTTCue(cue)->removeDisplayTree();
234 m_client->textTrackModeChanged(this);
237 TextTrackCueList* TextTrack::cues()
239 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
240 // then the cues attribute must return a live TextTrackCueList object ...
241 // Otherwise, it must return null. When an object is returned, the
242 // same object must be returned each time.
243 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
244 if (m_mode != disabledKeyword())
245 return ensureTextTrackCueList();
249 void TextTrack::removeAllCues()
255 m_client->textTrackRemoveCues(this, m_cues.get());
257 for (size_t i = 0; i < m_cues->length(); ++i)
258 m_cues->item(i)->setTrack(0);
263 TextTrackCueList* TextTrack::activeCues() const
265 // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
266 // then the activeCues attribute must return a live TextTrackCueList object ...
267 // ... whose active flag was set when the script started, in text track cue
268 // order. Otherwise, it must return null. When an object is returned, the
269 // same object must be returned each time.
270 // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
271 if (m_cues && m_mode != disabledKeyword())
272 return m_cues->activeCues();
276 void TextTrack::addCue(PassRefPtr<TextTrackCue> prpCue, ExceptionCode& ec)
281 RefPtr<TextTrackCue> cue = prpCue;
283 // 4.7.10.12.6 Text tracks exposing in-band metadata
284 // The UA will use DataCue to expose only text track cue objects that belong to a text track that has a text
285 // track kind of metadata.
286 // If a DataCue is added to a TextTrack via the addCue() method but the text track does not have its text
287 // track kind set to metadata, throw a InvalidNodeTypeError exception and don't add the cue to the TextTrackList
289 if (cue->cueType() == TextTrackCue::Data && kind() != metadataKeyword()) {
290 ec = INVALID_NODE_TYPE_ERR;
294 // TODO(93143): Add spec-compliant behavior for negative time values.
295 if (std::isnan(cue->startTime()) || std::isnan(cue->endTime()) || cue->startTime() < 0 || cue->endTime() < 0)
298 // 4.8.10.12.5 Text track API
300 // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
302 // 1. If the given cue is in a text track list of cues, then remove cue from that text track
304 TextTrack* cueTrack = cue->track();
305 if (cueTrack && cueTrack != this)
306 cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
308 // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
310 ensureTextTrackCueList()->add(cue);
313 m_client->textTrackAddCue(this, cue.get());
316 void TextTrack::removeCue(TextTrackCue* cue, ExceptionCode& ec)
321 // 4.8.10.12.5 Text track API
323 // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
325 // 1. If the given cue is not currently listed in the method's TextTrack
326 // object's text track's text track list of cues, then throw a NotFoundError exception.
327 if (cue->track() != this) {
332 // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
333 if (!m_cues || !m_cues->remove(cue)) {
334 ec = INVALID_STATE_ERR;
340 m_client->textTrackRemoveCue(this, cue);
343 #if ENABLE(VIDEO_TRACK) && ENABLE(WEBVTT_REGIONS)
344 TextTrackRegionList* TextTrack::regionList()
346 return ensureTextTrackRegionList();
349 TextTrackRegionList* TextTrack::ensureTextTrackRegionList()
352 m_regions = TextTrackRegionList::create();
354 return m_regions.get();
357 TextTrackRegionList* TextTrack::regions()
359 // If the text track mode of the text track that the TextTrack object
360 // represents is not the text track disabled mode, then the regions
361 // attribute must return a live TextTrackRegionList object that represents
362 // the text track list of regions of the text track. Otherwise, it must
363 // return null. When an object is returned, the same object must be returned
365 if (m_mode != disabledKeyword())
366 return ensureTextTrackRegionList();
371 void TextTrack::addRegion(PassRefPtr<TextTrackRegion> prpRegion)
376 RefPtr<TextTrackRegion> region = prpRegion;
377 TextTrackRegionList* regionList = ensureTextTrackRegionList();
379 // 1. If the given region is in a text track list of regions, then remove
380 // region from that text track list of regions.
381 TextTrack* regionTrack = region->track();
382 if (regionTrack && regionTrack != this)
383 regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
385 // 2. If the method's TextTrack object's text track list of regions contains
386 // a region with the same identifier as region replace the values of that
387 // region's width, height, anchor point, viewport anchor point and scroll
388 // attributes with those of region.
389 TextTrackRegion* existingRegion = regionList->getRegionById(region->id());
390 if (existingRegion) {
391 existingRegion->updateParametersFromRegion(region.get());
395 // Otherwise: add region to the method's TextTrack object's text track
397 region->setTrack(this);
398 regionList->add(region);
401 void TextTrack::removeRegion(TextTrackRegion* region, ExceptionCode &ec)
406 // 1. If the given region is not currently listed in the method's TextTrack
407 // object's text track list of regions, then throw a NotFoundError exception.
408 if (region->track() != this) {
413 if (!m_regions || !m_regions->remove(region)) {
414 ec = INVALID_STATE_ERR;
422 void TextTrack::cueWillChange(TextTrackCue* cue)
427 // The cue may need to be repositioned in the media element's interval tree, may need to
428 // be re-rendered, etc, so remove it before the modification...
429 m_client->textTrackRemoveCue(this, cue);
432 void TextTrack::cueDidChange(TextTrackCue* cue)
437 // Make sure the TextTrackCueList order is up-to-date.
438 ensureTextTrackCueList()->updateCueIndex(cue);
440 // ... and add it back again.
441 m_client->textTrackAddCue(this, cue);
444 int TextTrack::trackIndex()
446 ASSERT(m_mediaElement);
448 if (m_trackIndex == invalidTrackIndex)
449 m_trackIndex = m_mediaElement->textTracks()->getTrackIndex(this);
454 void TextTrack::invalidateTrackIndex()
456 m_trackIndex = invalidTrackIndex;
457 m_renderedTrackIndex = invalidTrackIndex;
460 bool TextTrack::isRendered()
462 if (kind() != captionsKeyword() && kind() != subtitlesKeyword() && kind() != forcedKeyword())
465 if (m_mode != showingKeyword())
471 TextTrackCueList* TextTrack::ensureTextTrackCueList()
474 m_cues = TextTrackCueList::create();
479 int TextTrack::trackIndexRelativeToRenderedTracks()
481 ASSERT(m_mediaElement);
483 if (m_renderedTrackIndex == invalidTrackIndex)
484 m_renderedTrackIndex = m_mediaElement->textTracks()->getTrackIndexRelativeToRenderedTracks(this);
486 return m_renderedTrackIndex;
489 bool TextTrack::hasCue(VTTCue* cue, VTTCue::CueMatchRules match)
491 if (cue->startTime() < 0 || cue->endTime() < 0)
494 if (!m_cues || !m_cues->length())
497 size_t searchStart = 0;
498 size_t searchEnd = m_cues->length();
501 ASSERT(searchStart <= m_cues->length());
502 ASSERT(searchEnd <= m_cues->length());
504 TextTrackCue* existingCue;
506 // Cues in the TextTrackCueList are maintained in start time order.
507 if (searchStart == searchEnd) {
511 // If there is more than one cue with the same start time, back up to first one so we
512 // consider all of them.
513 while (searchStart >= 2 && cue->startTime() == m_cues->item(searchStart - 2)->startTime())
516 bool firstCompare = true;
520 firstCompare = false;
521 if (searchStart > m_cues->length())
524 existingCue = m_cues->item(searchStart - 1);
525 if (!cue->isRenderable())
528 if (!existingCue || cue->startTime() > existingCue->startTime())
531 if (!toVTTCue(existingCue)->isEqual(*cue, match))
538 size_t index = (searchStart + searchEnd) / 2;
539 existingCue = m_cues->item(index);
540 if (cue->startTime() < existingCue->startTime() || (match != VTTCue::IgnoreDuration && cue->startTime() == existingCue->startTime() && cue->endTime() > existingCue->endTime()))
543 searchStart = index + 1;
546 ASSERT_NOT_REACHED();
550 #if USE(PLATFORM_TEXT_TRACK_MENU)
551 PassRefPtr<PlatformTextTrack> TextTrack::platformTextTrack()
553 if (m_platformTextTrack)
554 return m_platformTextTrack;
556 PlatformTextTrack::TrackKind platformKind = PlatformTextTrack::Caption;
557 if (kind() == subtitlesKeyword())
558 platformKind = PlatformTextTrack::Subtitle;
559 else if (kind() == captionsKeyword())
560 platformKind = PlatformTextTrack::Caption;
561 else if (kind() == descriptionsKeyword())
562 platformKind = PlatformTextTrack::Description;
563 else if (kind() == chaptersKeyword())
564 platformKind = PlatformTextTrack::Chapter;
565 else if (kind() == metadataKeyword())
566 platformKind = PlatformTextTrack::MetaData;
567 else if (kind() == forcedKeyword())
568 platformKind = PlatformTextTrack::Forced;
570 PlatformTextTrack::TrackType type = PlatformTextTrack::OutOfBand;
571 if (m_trackType == TrackElement)
572 type = PlatformTextTrack::OutOfBand;
573 else if (m_trackType == AddTrack)
574 type = PlatformTextTrack::Script;
575 else if (m_trackType == InBand)
576 type = PlatformTextTrack::InBand;
578 PlatformTextTrack::TrackMode platformMode = PlatformTextTrack::Disabled;
579 if (TextTrack::hiddenKeyword() == mode())
580 platformMode = PlatformTextTrack::Hidden;
581 else if (TextTrack::disabledKeyword() == mode())
582 platformMode = PlatformTextTrack::Disabled;
583 else if (TextTrack::showingKeyword() == mode())
584 platformMode = PlatformTextTrack::Showing;
586 m_platformTextTrack = PlatformTextTrack::create(this, label(), language(), platformMode, platformKind, type, uniqueId());
588 return m_platformTextTrack;
592 bool TextTrack::isMainProgramContent() const
594 // "Main program" content is intrinsic to the presentation of the media file, regardless of locale. Content such as
595 // directors commentary is not "main program" because it is not essential for the presentation. HTML5 doesn't have
596 // a way to express this in a machine-reable form, it is typically done with the track label, so we assume that caption
597 // tracks are main content and all other track types are not.
598 return kind() == captionsKeyword();
601 #if ENABLE(MEDIA_SOURCE)
602 void TextTrack::setLanguage(const AtomicString& language)
604 // 11.1 language, on setting:
605 // 1. If the value being assigned to this attribute is not an empty string or a BCP 47 language
606 // tag[BCP47], then abort these steps.
607 // FIXME(123926): Validate the BCP47-ness of langague.
609 // 2. Update this attribute to the new value.
610 TrackBase::setLanguage(language);
612 // 3. If the sourceBuffer attribute on this track is not null, then queue a task to fire a simple
613 // event named change at sourceBuffer.textTracks.
615 m_sourceBuffer->textTracks()->scheduleChangeEvent();
617 // 4. Queue a task to fire a simple event named change at the TextTrackList object referenced by
618 // the textTracks attribute on the HTMLMediaElement.
620 mediaElement()->textTracks()->scheduleChangeEvent();
624 } // namespace WebCore