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
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef TextTrack_h
     28 #define TextTrack_h
     29 
     30 #include "bindings/v8/ScriptWrappable.h"
     31 #include "core/html/track/TrackBase.h"
     32 #include "wtf/text/WTFString.h"
     33 
     34 namespace WebCore {
     35 
     36 class ExceptionState;
     37 class HTMLMediaElement;
     38 class TextTrack;
     39 class TextTrackCue;
     40 class TextTrackCueList;
     41 #if ENABLE(WEBVTT_REGIONS)
     42 class TextTrackRegion;
     43 class TextTrackRegionList;
     44 #endif
     45 
     46 class TextTrackClient {
     47 public:
     48     virtual ~TextTrackClient() { }
     49     virtual void textTrackKindChanged(TextTrack*) = 0;
     50     virtual void textTrackModeChanged(TextTrack*) = 0;
     51     virtual void textTrackAddCues(TextTrack*, const TextTrackCueList*) = 0;
     52     virtual void textTrackRemoveCues(TextTrack*, const TextTrackCueList*) = 0;
     53     virtual void textTrackAddCue(TextTrack*, PassRefPtr<TextTrackCue>) = 0;
     54     virtual void textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue>) = 0;
     55 };
     56 
     57 class TextTrack : public TrackBase, public ScriptWrappable {
     58 public:
     59     static PassRefPtr<TextTrack> create(ScriptExecutionContext* context, TextTrackClient* client, const AtomicString& kind, const AtomicString& label, const AtomicString& language)
     60     {
     61         return adoptRef(new TextTrack(context, client, kind, label, language, AddTrack));
     62     }
     63     virtual ~TextTrack();
     64 
     65     void setMediaElement(HTMLMediaElement* element) { m_mediaElement = element; }
     66     HTMLMediaElement* mediaElement() { return m_mediaElement; }
     67 
     68     AtomicString kind() const { return m_kind; }
     69     void setKind(const AtomicString&);
     70 
     71     static const AtomicString& subtitlesKeyword();
     72     static const AtomicString& captionsKeyword();
     73     static const AtomicString& descriptionsKeyword();
     74     static const AtomicString& chaptersKeyword();
     75     static const AtomicString& metadataKeyword();
     76     static bool isValidKindKeyword(const AtomicString&);
     77 
     78     AtomicString label() const { return m_label; }
     79     void setLabel(const AtomicString& label) { m_label = label; }
     80 
     81     AtomicString language() const { return m_language; }
     82     void setLanguage(const AtomicString& language) { m_language = language; }
     83 
     84     static const AtomicString& disabledKeyword();
     85     static const AtomicString& hiddenKeyword();
     86     static const AtomicString& showingKeyword();
     87 
     88     AtomicString mode() const { return m_mode; }
     89     virtual void setMode(const AtomicString&);
     90 
     91     enum ReadinessState { NotLoaded = 0, Loading = 1, Loaded = 2, FailedToLoad = 3 };
     92     ReadinessState readinessState() const { return m_readinessState; }
     93     void setReadinessState(ReadinessState state) { m_readinessState = state; }
     94 
     95     TextTrackCueList* cues();
     96     TextTrackCueList* activeCues() const;
     97 
     98     void clearClient() { m_client = 0; }
     99     TextTrackClient* client() { return m_client; }
    100 
    101     void addCue(PassRefPtr<TextTrackCue>);
    102     void removeCue(TextTrackCue*, ExceptionState&);
    103     bool hasCue(TextTrackCue*);
    104 
    105 #if ENABLE(WEBVTT_REGIONS)
    106     TextTrackRegionList* regions();
    107     void addRegion(PassRefPtr<TextTrackRegion>);
    108     void removeRegion(TextTrackRegion*, ExceptionState&);
    109 #endif
    110 
    111     void cueWillChange(TextTrackCue*);
    112     void cueDidChange(TextTrackCue*);
    113 
    114     DEFINE_ATTRIBUTE_EVENT_LISTENER(cuechange);
    115 
    116     enum TextTrackType { TrackElement, AddTrack, InBand };
    117     TextTrackType trackType() const { return m_trackType; }
    118 
    119     virtual bool isClosedCaptions() const { return false; }
    120 
    121     virtual bool containsOnlyForcedSubtitles() const { return false; }
    122     virtual bool isMainProgramContent() const;
    123     virtual bool isEasyToRead() const { return false; }
    124 
    125     int trackIndex();
    126     void invalidateTrackIndex();
    127 
    128     bool isRendered();
    129     int trackIndexRelativeToRenderedTracks();
    130 
    131     bool hasBeenConfigured() const { return m_hasBeenConfigured; }
    132     void setHasBeenConfigured(bool flag) { m_hasBeenConfigured = flag; }
    133 
    134     virtual bool isDefault() const { return false; }
    135     virtual void setIsDefault(bool) { }
    136 
    137     void removeAllCues();
    138 
    139 protected:
    140     TextTrack(ScriptExecutionContext*, TextTrackClient*, const AtomicString& kind, const AtomicString& label, const AtomicString& language, TextTrackType);
    141 #if ENABLE(WEBVTT_REGIONS)
    142     TextTrackRegionList* regionList();
    143 #endif
    144 
    145     RefPtr<TextTrackCueList> m_cues;
    146 
    147 private:
    148 
    149 #if ENABLE(WEBVTT_REGIONS)
    150     TextTrackRegionList* ensureTextTrackRegionList();
    151     RefPtr<TextTrackRegionList> m_regions;
    152 #endif
    153 
    154     TextTrackCueList* ensureTextTrackCueList();
    155 
    156     HTMLMediaElement* m_mediaElement;
    157     AtomicString m_kind;
    158     AtomicString m_label;
    159     AtomicString m_language;
    160     AtomicString m_mode;
    161     TextTrackClient* m_client;
    162     TextTrackType m_trackType;
    163     ReadinessState m_readinessState;
    164     int m_trackIndex;
    165     int m_renderedTrackIndex;
    166     bool m_hasBeenConfigured;
    167 };
    168 
    169 } // namespace WebCore
    170 
    171 #endif
    172