1 /* 2 * Copyright (C) 2012 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/html/track/InbandTextTrack.h" 28 29 #include "bindings/v8/ExceptionStatePlaceholder.h" 30 #include "core/html/track/TextTrackCueGeneric.h" 31 #include "core/platform/Logging.h" 32 #include "core/platform/graphics/InbandTextTrackPrivate.h" 33 #include "wtf/UnusedParam.h" 34 #include <math.h> 35 36 namespace WebCore { 37 38 PassRefPtr<InbandTextTrack> InbandTextTrack::create(ScriptExecutionContext* context, TextTrackClient* client, PassRefPtr<InbandTextTrackPrivate> playerPrivate) 39 { 40 return adoptRef(new InbandTextTrack(context, client, playerPrivate)); 41 } 42 43 InbandTextTrack::InbandTextTrack(ScriptExecutionContext* context, TextTrackClient* client, PassRefPtr<InbandTextTrackPrivate> tracksPrivate) 44 : TextTrack(context, client, emptyString(), tracksPrivate->label(), tracksPrivate->language(), InBand) 45 , m_private(tracksPrivate) 46 { 47 m_private->setClient(this); 48 49 switch (m_private->kind()) { 50 case InbandTextTrackPrivate::Subtitles: 51 setKind(TextTrack::subtitlesKeyword()); 52 break; 53 case InbandTextTrackPrivate::Captions: 54 setKind(TextTrack::captionsKeyword()); 55 break; 56 case InbandTextTrackPrivate::Descriptions: 57 setKind(TextTrack::descriptionsKeyword()); 58 break; 59 case InbandTextTrackPrivate::Chapters: 60 setKind(TextTrack::chaptersKeyword()); 61 break; 62 case InbandTextTrackPrivate::Metadata: 63 setKind(TextTrack::metadataKeyword()); 64 break; 65 case InbandTextTrackPrivate::None: 66 default: 67 ASSERT_NOT_REACHED(); 68 break; 69 } 70 } 71 72 InbandTextTrack::~InbandTextTrack() 73 { 74 // Make sure m_private was cleared by trackRemoved() before destruction. 75 ASSERT(!m_private); 76 } 77 78 void InbandTextTrack::setMode(const AtomicString& mode) 79 { 80 TextTrack::setMode(mode); 81 82 if (!m_private) 83 return; 84 85 if (mode == TextTrack::disabledKeyword()) 86 m_private->setMode(InbandTextTrackPrivate::Disabled); 87 else if (mode == TextTrack::hiddenKeyword()) 88 m_private->setMode(InbandTextTrackPrivate::Hidden); 89 else if (mode == TextTrack::showingKeyword()) 90 m_private->setMode(InbandTextTrackPrivate::Showing); 91 else 92 ASSERT_NOT_REACHED(); 93 } 94 95 bool InbandTextTrack::isClosedCaptions() const 96 { 97 if (!m_private) 98 return false; 99 100 return m_private->isClosedCaptions(); 101 } 102 103 bool InbandTextTrack::containsOnlyForcedSubtitles() const 104 { 105 if (!m_private) 106 return false; 107 108 return m_private->containsOnlyForcedSubtitles(); 109 } 110 111 bool InbandTextTrack::isMainProgramContent() const 112 { 113 if (!m_private) 114 return false; 115 116 return m_private->isMainProgramContent(); 117 } 118 119 bool InbandTextTrack::isEasyToRead() const 120 { 121 if (!m_private) 122 return false; 123 124 return m_private->isEasyToRead(); 125 } 126 127 size_t InbandTextTrack::inbandTrackIndex() 128 { 129 ASSERT(m_private); 130 return m_private->textTrackIndex(); 131 } 132 133 void InbandTextTrack::trackRemoved() 134 { 135 ASSERT(m_private); 136 m_private->setClient(0); 137 m_private = 0; 138 clearClient(); 139 } 140 141 void InbandTextTrack::addGenericCue(InbandTextTrackPrivate* trackPrivate, GenericCueData* cueData) 142 { 143 UNUSED_PARAM(trackPrivate); 144 ASSERT(trackPrivate == m_private); 145 146 RefPtr<TextTrackCueGeneric> cue = TextTrackCueGeneric::create(scriptExecutionContext(), cueData->startTime(), cueData->endTime(), cueData->content()); 147 148 cue->setId(cueData->id()); 149 cue->setBaseFontSizeRelativeToVideoHeight(cueData->baseFontSize()); 150 cue->setFontSizeMultiplier(cueData->relativeFontSize()); 151 cue->setFontName(cueData->fontName()); 152 153 if (cueData->position() > 0) 154 cue->setPosition(lround(cueData->position()), IGNORE_EXCEPTION); 155 if (cueData->line() > 0) 156 cue->setLine(lround(cueData->line()), IGNORE_EXCEPTION); 157 if (cueData->size() > 0) 158 cue->setSize(lround(cueData->size()), IGNORE_EXCEPTION); 159 if (cueData->backgroundColor().alpha()) 160 cue->setBackgroundColor(cueData->backgroundColor().rgb()); 161 if (cueData->foregroundColor().alpha()) 162 cue->setForegroundColor(cueData->foregroundColor().rgb()); 163 164 if (cueData->align() == GenericCueData::Start) 165 cue->setAlign("start", IGNORE_EXCEPTION); 166 else if (cueData->align() == GenericCueData::Middle) 167 cue->setAlign("middle", IGNORE_EXCEPTION); 168 else if (cueData->align() == GenericCueData::End) 169 cue->setAlign("end", IGNORE_EXCEPTION); 170 cue->setSnapToLines(false); 171 172 if (hasCue(cue.get())) { 173 LOG(Media, "InbandTextTrack::addGenericCue ignoring already added cue: start=%.2f, end=%.2f, content=\"%s\"\n", 174 cueData->startTime(), cueData->endTime(), cueData->content().utf8().data()); 175 return; 176 } 177 178 addCue(cue); 179 } 180 181 void InbandTextTrack::addWebVTTCue(InbandTextTrackPrivate* trackPrivate, double start, double end, const String& id, const String& content, const String& settings) 182 { 183 UNUSED_PARAM(trackPrivate); 184 ASSERT(trackPrivate == m_private); 185 186 RefPtr<TextTrackCue> cue = TextTrackCue::create(scriptExecutionContext(), start, end, content); 187 cue->setId(id); 188 cue->setCueSettings(settings); 189 190 if (hasCue(cue.get())) 191 return; 192 193 addCue(cue); 194 } 195 196 } // namespace WebCore 197