Home | History | Annotate | Download | only in track
      1 /*
      2  * Copyright (C) 2011 Google Inc.  All rights reserved.
      3  * Copyright (C) 2011, 2012, 2013 Apple Inc.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      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
     14  * distribution.
     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.
     18  *
     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.
     30  */
     31 
     32 #include "config.h"
     33 #include "core/html/track/TextTrack.h"
     34 
     35 #include "bindings/v8/ExceptionStatePlaceholder.h"
     36 #include "core/dom/ExceptionCode.h"
     37 #include "core/html/HTMLMediaElement.h"
     38 #include "core/html/track/TextTrackCueList.h"
     39 #include "core/html/track/TextTrackList.h"
     40 #include "core/html/track/TextTrackRegion.h"
     41 #include "core/html/track/TextTrackRegionList.h"
     42 
     43 namespace WebCore {
     44 
     45 static const int invalidTrackIndex = -1;
     46 
     47 const AtomicString& TextTrack::subtitlesKeyword()
     48 {
     49     DEFINE_STATIC_LOCAL(const AtomicString, subtitles, ("subtitles", AtomicString::ConstructFromLiteral));
     50     return subtitles;
     51 }
     52 
     53 const AtomicString& TextTrack::captionsKeyword()
     54 {
     55     DEFINE_STATIC_LOCAL(const AtomicString, captions, ("captions", AtomicString::ConstructFromLiteral));
     56     return captions;
     57 }
     58 
     59 const AtomicString& TextTrack::descriptionsKeyword()
     60 {
     61     DEFINE_STATIC_LOCAL(const AtomicString, descriptions, ("descriptions", AtomicString::ConstructFromLiteral));
     62     return descriptions;
     63 }
     64 
     65 const AtomicString& TextTrack::chaptersKeyword()
     66 {
     67     DEFINE_STATIC_LOCAL(const AtomicString, chapters, ("chapters", AtomicString::ConstructFromLiteral));
     68     return chapters;
     69 }
     70 
     71 const AtomicString& TextTrack::metadataKeyword()
     72 {
     73     DEFINE_STATIC_LOCAL(const AtomicString, metadata, ("metadata", AtomicString::ConstructFromLiteral));
     74     return metadata;
     75 }
     76 
     77 const AtomicString& TextTrack::disabledKeyword()
     78 {
     79     DEFINE_STATIC_LOCAL(const AtomicString, open, ("disabled", AtomicString::ConstructFromLiteral));
     80     return open;
     81 }
     82 
     83 const AtomicString& TextTrack::hiddenKeyword()
     84 {
     85     DEFINE_STATIC_LOCAL(const AtomicString, closed, ("hidden", AtomicString::ConstructFromLiteral));
     86     return closed;
     87 }
     88 
     89 const AtomicString& TextTrack::showingKeyword()
     90 {
     91     DEFINE_STATIC_LOCAL(const AtomicString, ended, ("showing", AtomicString::ConstructFromLiteral));
     92     return ended;
     93 }
     94 
     95 TextTrack::TextTrack(ScriptExecutionContext* context, TextTrackClient* client, const AtomicString& kind, const AtomicString& label, const AtomicString& language, TextTrackType type)
     96     : TrackBase(context, TrackBase::TextTrack)
     97     , m_cues(0)
     98 #if ENABLE(WEBVTT_REGIONS)
     99     , m_regions(0)
    100 #endif
    101     , m_mediaElement(0)
    102     , m_label(label)
    103     , m_language(language)
    104     , m_mode(disabledKeyword().string())
    105     , m_client(client)
    106     , m_trackType(type)
    107     , m_readinessState(NotLoaded)
    108     , m_trackIndex(invalidTrackIndex)
    109     , m_renderedTrackIndex(invalidTrackIndex)
    110     , m_hasBeenConfigured(false)
    111 {
    112     ScriptWrappable::init(this);
    113     setKind(kind);
    114 }
    115 
    116 TextTrack::~TextTrack()
    117 {
    118     if (m_cues) {
    119         if (m_client)
    120             m_client->textTrackRemoveCues(this, m_cues.get());
    121 
    122         for (size_t i = 0; i < m_cues->length(); ++i)
    123             m_cues->item(i)->setTrack(0);
    124     }
    125 
    126 #if ENABLE(WEBVTT_REGIONS)
    127     if (m_regions) {
    128         for (size_t i = 0; i < m_regions->length(); ++i)
    129             m_regions->item(i)->setTrack(0);
    130     }
    131 #endif
    132     clearClient();
    133 }
    134 
    135 bool TextTrack::isValidKindKeyword(const AtomicString& value)
    136 {
    137     if (value == subtitlesKeyword())
    138         return true;
    139     if (value == captionsKeyword())
    140         return true;
    141     if (value == descriptionsKeyword())
    142         return true;
    143     if (value == chaptersKeyword())
    144         return true;
    145     if (value == metadataKeyword())
    146         return true;
    147 
    148     return false;
    149 }
    150 
    151 void TextTrack::setKind(const AtomicString& kind)
    152 {
    153     String oldKind = m_kind;
    154 
    155     if (isValidKindKeyword(kind))
    156         m_kind = kind;
    157     else
    158         m_kind = subtitlesKeyword();
    159 
    160     if (m_client && oldKind != m_kind)
    161         m_client->textTrackKindChanged(this);
    162 }
    163 
    164 void TextTrack::setMode(const AtomicString& mode)
    165 {
    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 ...
    168     if (mode != disabledKeyword() && mode != hiddenKeyword() && mode != showingKeyword())
    169         return;
    170 
    171     if (m_mode == mode)
    172         return;
    173 
    174     // If mode changes to disabled, remove this track's cues from the client
    175     // because they will no longer be accessible from the cues() function.
    176     if (mode == disabledKeyword() && m_client && m_cues)
    177         m_client->textTrackRemoveCues(this, m_cues.get());
    178 
    179     if (mode != showingKeyword() && m_cues)
    180         for (size_t i = 0; i < m_cues->length(); ++i)
    181             m_cues->item(i)->removeDisplayTree();
    182 
    183     m_mode = mode;
    184 
    185     if (m_client)
    186         m_client->textTrackModeChanged(this);
    187 }
    188 
    189 TextTrackCueList* TextTrack::cues()
    190 {
    191     // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
    192     // then the cues attribute must return a live TextTrackCueList object ...
    193     // Otherwise, it must return null. When an object is returned, the
    194     // same object must be returned each time.
    195     // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
    196     if (m_mode != disabledKeyword())
    197         return ensureTextTrackCueList();
    198     return 0;
    199 }
    200 
    201 void TextTrack::removeAllCues()
    202 {
    203     if (!m_cues)
    204         return;
    205 
    206     if (m_client)
    207         m_client->textTrackRemoveCues(this, m_cues.get());
    208 
    209     for (size_t i = 0; i < m_cues->length(); ++i)
    210         m_cues->item(i)->setTrack(0);
    211 
    212     m_cues = 0;
    213 }
    214 
    215 TextTrackCueList* TextTrack::activeCues() const
    216 {
    217     // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
    218     // then the activeCues attribute must return a live TextTrackCueList object ...
    219     // ... whose active flag was set when the script started, in text track cue
    220     // order. Otherwise, it must return null. When an object is returned, the
    221     // same object must be returned each time.
    222     // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
    223     if (m_cues && m_mode != disabledKeyword())
    224         return m_cues->activeCues();
    225     return 0;
    226 }
    227 
    228 void TextTrack::addCue(PassRefPtr<TextTrackCue> prpCue)
    229 {
    230     if (!prpCue)
    231         return;
    232 
    233     RefPtr<TextTrackCue> cue = prpCue;
    234 
    235     // TODO(93143): Add spec-compliant behavior for negative time values.
    236     if (std::isnan(cue->startTime()) || std::isnan(cue->endTime()) || cue->startTime() < 0 || cue->endTime() < 0)
    237         return;
    238 
    239     // 4.8.10.12.5 Text track API
    240 
    241     // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
    242 
    243     // 1. If the given cue is in a text track list of cues, then remove cue from that text track
    244     // list of cues.
    245     TextTrack* cueTrack = cue->track();
    246     if (cueTrack && cueTrack != this)
    247         cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
    248 
    249     // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
    250     cue->setTrack(this);
    251     ensureTextTrackCueList()->add(cue);
    252 
    253     if (m_client)
    254         m_client->textTrackAddCue(this, cue.get());
    255 }
    256 
    257 void TextTrack::removeCue(TextTrackCue* cue, ExceptionState& es)
    258 {
    259     if (!cue)
    260         return;
    261 
    262     // 4.8.10.12.5 Text track API
    263 
    264     // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
    265 
    266     // 1. If the given cue is not currently listed in the method's TextTrack
    267     // object's text track's text track list of cues, then throw a NotFoundError exception.
    268     if (cue->track() != this) {
    269         es.throwDOMException(NotFoundError);
    270         return;
    271     }
    272 
    273     // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
    274     if (!m_cues || !m_cues->remove(cue)) {
    275         es.throwDOMException(InvalidStateError);
    276         return;
    277     }
    278 
    279     cue->setTrack(0);
    280     if (m_client)
    281         m_client->textTrackRemoveCue(this, cue);
    282 }
    283 
    284 #if ENABLE(WEBVTT_REGIONS)
    285 TextTrackRegionList* TextTrack::regionList()
    286 {
    287     return ensureTextTrackRegionList();
    288 }
    289 
    290 TextTrackRegionList* TextTrack::ensureTextTrackRegionList()
    291 {
    292     if (!m_regions)
    293         m_regions = TextTrackRegionList::create();
    294 
    295     return m_regions.get();
    296 }
    297 
    298 TextTrackRegionList* TextTrack::regions()
    299 {
    300     // If the text track mode of the text track that the TextTrack object
    301     // represents is not the text track disabled mode, then the regions
    302     // attribute must return a live TextTrackRegionList object that represents
    303     // the text track list of regions of the text track. Otherwise, it must
    304     // return null. When an object is returned, the same object must be returned
    305     // each time.
    306     if (m_mode != disabledKeyword())
    307         return ensureTextTrackRegionList();
    308 
    309     return 0;
    310 }
    311 
    312 void TextTrack::addRegion(PassRefPtr<TextTrackRegion> prpRegion)
    313 {
    314     if (!prpRegion)
    315         return;
    316 
    317     RefPtr<TextTrackRegion> region = prpRegion;
    318     TextTrackRegionList* regionList = ensureTextTrackRegionList();
    319 
    320     // 1. If the given region is in a text track list of regions, then remove
    321     // region from that text track list of regions.
    322     TextTrack* regionTrack = region->track();
    323     if (regionTrack && regionTrack != this)
    324         regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
    325 
    326     // 2. If the method's TextTrack object's text track list of regions contains
    327     // a region with the same identifier as region replace the values of that
    328     // region's width, height, anchor point, viewport anchor point and scroll
    329     // attributes with those of region.
    330     TextTrackRegion* existingRegion = regionList->getRegionById(region->id());
    331     if (existingRegion) {
    332         existingRegion->updateParametersFromRegion(region.get());
    333         return;
    334     }
    335 
    336     // Otherwise: add region to the method's TextTrack object's text track
    337     // list of regions.
    338     region->setTrack(this);
    339     regionList->add(region);
    340 }
    341 
    342 void TextTrack::removeRegion(TextTrackRegion* region, ExceptionState &es)
    343 {
    344     if (!region)
    345         return;
    346 
    347     // 1. If the given region is not currently listed in the method's TextTrack
    348     // object's text track list of regions, then throw a NotFoundError exception.
    349     if (region->track() != this) {
    350         es.throwDOMException(NotFoundError);
    351         return;
    352     }
    353 
    354     if (!m_regions || !m_regions->remove(region)) {
    355         es.throwDOMException(InvalidStateError);
    356         return;
    357     }
    358 
    359     region->setTrack(0);
    360 }
    361 #endif
    362 
    363 void TextTrack::cueWillChange(TextTrackCue* cue)
    364 {
    365     if (!m_client)
    366         return;
    367 
    368     // The cue may need to be repositioned in the media element's interval tree, may need to
    369     // be re-rendered, etc, so remove it before the modification...
    370     m_client->textTrackRemoveCue(this, cue);
    371 }
    372 
    373 void TextTrack::cueDidChange(TextTrackCue* cue)
    374 {
    375     if (!m_client)
    376         return;
    377 
    378     // Make sure the TextTrackCueList order is up-to-date.
    379     ensureTextTrackCueList()->updateCueIndex(cue);
    380 
    381     // ... and add it back again.
    382     m_client->textTrackAddCue(this, cue);
    383 }
    384 
    385 int TextTrack::trackIndex()
    386 {
    387     ASSERT(m_mediaElement);
    388 
    389     if (m_trackIndex == invalidTrackIndex)
    390         m_trackIndex = m_mediaElement->textTracks()->getTrackIndex(this);
    391 
    392     return m_trackIndex;
    393 }
    394 
    395 void TextTrack::invalidateTrackIndex()
    396 {
    397     m_trackIndex = invalidTrackIndex;
    398     m_renderedTrackIndex = invalidTrackIndex;
    399 }
    400 
    401 bool TextTrack::isRendered()
    402 {
    403     if (m_kind != captionsKeyword() && m_kind != subtitlesKeyword())
    404         return false;
    405 
    406     if (m_mode != showingKeyword())
    407         return false;
    408 
    409     return true;
    410 }
    411 
    412 TextTrackCueList* TextTrack::ensureTextTrackCueList()
    413 {
    414     if (!m_cues)
    415         m_cues = TextTrackCueList::create();
    416 
    417     return m_cues.get();
    418 }
    419 
    420 int TextTrack::trackIndexRelativeToRenderedTracks()
    421 {
    422     ASSERT(m_mediaElement);
    423 
    424     if (m_renderedTrackIndex == invalidTrackIndex)
    425         m_renderedTrackIndex = m_mediaElement->textTracks()->getTrackIndexRelativeToRenderedTracks(this);
    426 
    427     return m_renderedTrackIndex;
    428 }
    429 
    430 bool TextTrack::hasCue(TextTrackCue* cue)
    431 {
    432     if (cue->startTime() < 0 || cue->endTime() < 0)
    433         return false;
    434 
    435     if (!m_cues || !m_cues->length())
    436         return false;
    437 
    438     size_t searchStart = 0;
    439     size_t searchEnd = m_cues->length();
    440 
    441     while (1) {
    442         ASSERT(searchStart <= m_cues->length());
    443         ASSERT(searchEnd <= m_cues->length());
    444 
    445         TextTrackCue* existingCue;
    446 
    447         // Cues in the TextTrackCueList are maintained in start time order.
    448         if (searchStart == searchEnd) {
    449             if (!searchStart)
    450                 return false;
    451 
    452             // If there is more than one cue with the same start time, back up to first one so we
    453             // consider all of them.
    454             while (searchStart >= 2 && cue->startTime() == m_cues->item(searchStart - 2)->startTime())
    455                 --searchStart;
    456 
    457             bool firstCompare = true;
    458             while (1) {
    459                 if (!firstCompare)
    460                     ++searchStart;
    461                 firstCompare = false;
    462                 if (searchStart > m_cues->length())
    463                     return false;
    464 
    465                 existingCue = m_cues->item(searchStart - 1);
    466                 if (!existingCue || cue->startTime() > existingCue->startTime())
    467                     return false;
    468 
    469                 if (*existingCue != *cue)
    470                     continue;
    471 
    472                 return true;
    473             }
    474         }
    475 
    476         size_t index = (searchStart + searchEnd) / 2;
    477         existingCue = m_cues->item(index);
    478         if (cue->startTime() < existingCue->startTime() || (cue->startTime() == existingCue->startTime() && cue->endTime() > existingCue->endTime()))
    479             searchEnd = index;
    480         else
    481             searchStart = index + 1;
    482     }
    483 
    484     ASSERT_NOT_REACHED();
    485     return false;
    486 }
    487 
    488 bool TextTrack::isMainProgramContent() const
    489 {
    490     // "Main program" content is intrinsic to the presentation of the media file, regardless of locale. Content such as
    491     // directors commentary is not "main program" because it is not essential for the presentation. HTML5 doesn't have
    492     // a way to express this in a machine-reable form, it is typically done with the track label, so we assume that caption
    493     // tracks are main content and all other track types are not.
    494     return m_kind == captionsKeyword();
    495 }
    496 
    497 } // namespace WebCore
    498 
    499