1/* 2 * Copyright (C) 2012 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "MediaSource.h" 33 34#if ENABLE(MEDIA_SOURCE) 35 36#include "ContentType.h" 37#include "Event.h" 38#include "MIMETypeRegistry.h" 39#include "SourceBufferPrivate.h" 40#include "TimeRanges.h" 41#include <wtf/Uint8Array.h> 42 43namespace WebCore { 44 45PassRefPtr<MediaSource> MediaSource::create(ScriptExecutionContext* context) 46{ 47 RefPtr<MediaSource> mediaSource(adoptRef(new MediaSource(context))); 48 mediaSource->suspendIfNeeded(); 49 return mediaSource.release(); 50} 51 52MediaSource::MediaSource(ScriptExecutionContext* context) 53 : ActiveDOMObject(context) 54 , m_readyState(closedKeyword()) 55 , m_asyncEventQueue(GenericEventQueue::create(this)) 56{ 57 m_sourceBuffers = SourceBufferList::create(scriptExecutionContext(), m_asyncEventQueue.get()); 58 m_activeSourceBuffers = SourceBufferList::create(scriptExecutionContext(), m_asyncEventQueue.get()); 59} 60 61const String& MediaSource::openKeyword() 62{ 63 DEFINE_STATIC_LOCAL(const String, open, (ASCIILiteral("open"))); 64 return open; 65} 66 67const String& MediaSource::closedKeyword() 68{ 69 DEFINE_STATIC_LOCAL(const String, closed, (ASCIILiteral("closed"))); 70 return closed; 71} 72 73const String& MediaSource::endedKeyword() 74{ 75 DEFINE_STATIC_LOCAL(const String, ended, (ASCIILiteral("ended"))); 76 return ended; 77} 78 79SourceBufferList* MediaSource::sourceBuffers() 80{ 81 return m_sourceBuffers.get(); 82} 83 84SourceBufferList* MediaSource::activeSourceBuffers() 85{ 86 // FIXME(91649): support track selection 87 return m_activeSourceBuffers.get(); 88} 89 90double MediaSource::duration() const 91{ 92 return m_readyState == closedKeyword() ? std::numeric_limits<float>::quiet_NaN() : m_private->duration(); 93} 94 95void MediaSource::setDuration(double duration, ExceptionCode& ec) 96{ 97 if (duration < 0.0 || std::isnan(duration)) { 98 ec = INVALID_ACCESS_ERR; 99 return; 100 } 101 if (m_readyState != openKeyword()) { 102 ec = INVALID_STATE_ERR; 103 return; 104 } 105 m_private->setDuration(duration); 106} 107 108SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionCode& ec) 109{ 110 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#dom-addsourcebuffer 111 // 1. If type is null or an empty then throw an INVALID_ACCESS_ERR exception and 112 // abort these steps. 113 if (type.isNull() || type.isEmpty()) { 114 ec = INVALID_ACCESS_ERR; 115 return 0; 116 } 117 118 // 2. If type contains a MIME type that is not supported ..., then throw a 119 // NOT_SUPPORTED_ERR exception and abort these steps. 120 if (!isTypeSupported(type)) { 121 ec = NOT_SUPPORTED_ERR; 122 return 0; 123 } 124 125 // 4. If the readyState attribute is not in the "open" state then throw an 126 // INVALID_STATE_ERR exception and abort these steps. 127 if (!m_private || m_readyState != openKeyword()) { 128 ec = INVALID_STATE_ERR; 129 return 0; 130 } 131 132 // 5. Create a new SourceBuffer object and associated resources. 133 ContentType contentType(type); 134 Vector<String> codecs = contentType.codecs(); 135 OwnPtr<SourceBufferPrivate> sourceBufferPrivate; 136 switch (m_private->addSourceBuffer(contentType.type(), codecs, &sourceBufferPrivate)) { 137 case MediaSourcePrivate::Ok: { 138 ASSERT(sourceBufferPrivate); 139 RefPtr<SourceBuffer> buffer = SourceBuffer::create(sourceBufferPrivate.release(), this); 140 141 // 6. Add the new object to sourceBuffers and fire a addsourcebuffer on that object. 142 m_sourceBuffers->add(buffer); 143 m_activeSourceBuffers->add(buffer); 144 // 7. Return the new object to the caller. 145 return buffer.get(); 146 } 147 case MediaSourcePrivate::NotSupported: 148 // 2 (cont). If type contains a MIME type ... that is not supported with the types 149 // specified for the other SourceBuffer objects in sourceBuffers, then throw 150 // a NOT_SUPPORTED_ERR exception and abort these steps. 151 ec = NOT_SUPPORTED_ERR; 152 return 0; 153 case MediaSourcePrivate::ReachedIdLimit: 154 // 3 (cont). If the user agent can't handle any more SourceBuffer objects then throw 155 // a QUOTA_EXCEEDED_ERR exception and abort these steps. 156 ec = QUOTA_EXCEEDED_ERR; 157 return 0; 158 } 159 160 ASSERT_NOT_REACHED(); 161 return 0; 162} 163 164void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec) 165{ 166 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#dom-removesourcebuffer 167 // 1. If sourceBuffer is null then throw an INVALID_ACCESS_ERR exception and 168 // abort these steps. 169 if (!buffer) { 170 ec = INVALID_ACCESS_ERR; 171 return; 172 } 173 174 // 2. If sourceBuffers is empty then throw an INVALID_STATE_ERR exception and 175 // abort these steps. 176 if (!m_private || !m_sourceBuffers->length()) { 177 ec = INVALID_STATE_ERR; 178 return; 179 } 180 181 // 3. If sourceBuffer specifies an object that is not in sourceBuffers then 182 // throw a NOT_FOUND_ERR exception and abort these steps. 183 // 6. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer event 184 // on that object. 185 if (!m_sourceBuffers->remove(buffer)) { 186 ec = NOT_FOUND_ERR; 187 return; 188 } 189 190 // 7. Destroy all resources for sourceBuffer. 191 m_activeSourceBuffers->remove(buffer); 192 193 // 4. Remove track information from audioTracks, videoTracks, and textTracks for all tracks 194 // associated with sourceBuffer and fire a simple event named change on the modified lists. 195 // FIXME(91649): support track selection 196 197 // 5. If sourceBuffer is in activeSourceBuffers, then remove it from that list and fire a 198 // removesourcebuffer event on that object. 199 // FIXME(91649): support track selection 200} 201 202const String& MediaSource::readyState() const 203{ 204 return m_readyState; 205} 206 207void MediaSource::setReadyState(const String& state) 208{ 209 ASSERT(state == openKeyword() || state == closedKeyword() || state == endedKeyword()); 210 if (m_readyState == state) 211 return; 212 213 String oldState = m_readyState; 214 m_readyState = state; 215 216 if (m_readyState == closedKeyword()) { 217 m_sourceBuffers->clear(); 218 m_activeSourceBuffers->clear(); 219 m_private.clear(); 220 scheduleEvent(eventNames().webkitsourcecloseEvent); 221 return; 222 } 223 224 if (oldState == openKeyword() && m_readyState == endedKeyword()) { 225 scheduleEvent(eventNames().webkitsourceendedEvent); 226 return; 227 } 228 229 if (m_readyState == openKeyword()) { 230 scheduleEvent(eventNames().webkitsourceopenEvent); 231 return; 232 } 233} 234 235void MediaSource::endOfStream(const String& error, ExceptionCode& ec) 236{ 237 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#dom-endofstream 238 // 1. If the readyState attribute is not in the "open" state then throw an 239 // INVALID_STATE_ERR exception and abort these steps. 240 if (!m_private || m_readyState != openKeyword()) { 241 ec = INVALID_STATE_ERR; 242 return; 243 } 244 245 MediaSourcePrivate::EndOfStreamStatus eosStatus = MediaSourcePrivate::EosNoError; 246 247 if (error.isNull() || error.isEmpty()) 248 eosStatus = MediaSourcePrivate::EosNoError; 249 else if (error == "network") 250 eosStatus = MediaSourcePrivate::EosNetworkError; 251 else if (error == "decode") 252 eosStatus = MediaSourcePrivate::EosDecodeError; 253 else { 254 ec = INVALID_ACCESS_ERR; 255 return; 256 } 257 258 // 2. Change the readyState attribute value to "ended". 259 setReadyState(endedKeyword()); 260 m_private->endOfStream(eosStatus); 261} 262 263bool MediaSource::isTypeSupported(const String& type) 264{ 265 // Section 2.1 isTypeSupported() method steps. 266 // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#widl-MediaSource-isTypeSupported-boolean-DOMString-type 267 // 1. If type is an empty string, then return false. 268 if (type.isNull() || type.isEmpty()) 269 return false; 270 271 ContentType contentType(type); 272 String codecs = contentType.parameter("codecs"); 273 274 // 2. If type does not contain a valid MIME type string, then return false. 275 if (contentType.type().isEmpty() || codecs.isEmpty()) 276 return false; 277 278 // 3. If type contains a media type or media subtype that the MediaSource does not support, then return false. 279 // 4. If type contains at a codec that the MediaSource does not support, then return false. 280 // 5. If the MediaSource does not support the specified combination of media type, media subtype, and codecs then return false. 281 // 6. Return true. 282 return MIMETypeRegistry::isSupportedMediaSourceMIMEType(contentType.type(), codecs); 283} 284 285void MediaSource::setPrivateAndOpen(PassOwnPtr<MediaSourcePrivate> mediaSourcePrivate) 286{ 287 ASSERT(mediaSourcePrivate); 288 ASSERT(!m_private); 289 m_private = mediaSourcePrivate; 290 setReadyState(openKeyword()); 291} 292 293const AtomicString& MediaSource::interfaceName() const 294{ 295 return eventNames().interfaceForMediaSource; 296} 297 298ScriptExecutionContext* MediaSource::scriptExecutionContext() const 299{ 300 return ActiveDOMObject::scriptExecutionContext(); 301} 302 303bool MediaSource::hasPendingActivity() const 304{ 305 return m_private || m_asyncEventQueue->hasPendingEvents() 306 || ActiveDOMObject::hasPendingActivity(); 307} 308 309void MediaSource::stop() 310{ 311 m_private.clear(); 312 m_asyncEventQueue->cancelAllEvents(); 313} 314 315EventTargetData* MediaSource::eventTargetData() 316{ 317 return &m_eventTargetData; 318} 319 320EventTargetData* MediaSource::ensureEventTargetData() 321{ 322 return &m_eventTargetData; 323} 324 325void MediaSource::scheduleEvent(const AtomicString& eventName) 326{ 327 ASSERT(m_asyncEventQueue); 328 329 RefPtr<Event> event = Event::create(eventName, false, false); 330 event->setTarget(this); 331 332 m_asyncEventQueue->enqueueEvent(event.release()); 333} 334 335} // namespace WebCore 336 337#endif 338