2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/mediasource/MediaSource.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "bindings/v8/ExceptionStatePlaceholder.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/events/GenericEventQueue.h"
38 #include "core/html/TimeRanges.h"
39 #include "modules/mediasource/MediaSourceRegistry.h"
40 #include "platform/ContentType.h"
41 #include "platform/Logging.h"
42 #include "platform/MIMETypeRegistry.h"
43 #include "public/platform/WebSourceBuffer.h"
44 #include "wtf/Uint8Array.h"
45 #include "wtf/text/CString.h"
47 using blink::WebSourceBuffer;
51 MediaSource* MediaSource::create(ExecutionContext* context)
53 MediaSource* mediaSource(adoptRefCountedGarbageCollected(new MediaSource(context)));
54 mediaSource->suspendIfNeeded();
58 MediaSource::MediaSource(ExecutionContext* context)
59 : MediaSourceBase(context)
61 WTF_LOG(Media, "MediaSource::MediaSource %p", this);
62 ScriptWrappable::init(this);
63 m_sourceBuffers = SourceBufferList::create(executionContext(), asyncEventQueue());
64 m_activeSourceBuffers = SourceBufferList::create(executionContext(), asyncEventQueue());
67 MediaSource::~MediaSource()
69 WTF_LOG(Media, "MediaSource::~MediaSource %p", this);
73 SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionState& exceptionState)
75 WTF_LOG(Media, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
77 // 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
78 // 1. If type is an empty string then throw an InvalidAccessError exception
79 // and abort these steps.
81 exceptionState.throwDOMException(InvalidAccessError, "The type provided is empty.");
85 // 2. If type contains a MIME type that is not supported ..., then throw a
86 // NotSupportedError exception and abort these steps.
87 if (!isTypeSupported(type)) {
88 exceptionState.throwDOMException(NotSupportedError, "The type provided ('" + type + "') is unsupported.");
92 // 4. If the readyState attribute is not in the "open" state then throw an
93 // InvalidStateError exception and abort these steps.
95 exceptionState.throwDOMException(InvalidStateError, "The MediaSource's readyState is not 'open'.");
99 // 5. Create a new SourceBuffer object and associated resources.
100 ContentType contentType(type);
101 Vector<String> codecs = contentType.codecs();
102 OwnPtr<WebSourceBuffer> webSourceBuffer = createWebSourceBuffer(contentType.type(), codecs, exceptionState);
104 if (!webSourceBuffer) {
105 ASSERT(exceptionState.code() == NotSupportedError || exceptionState.code() == QuotaExceededError);
106 // 2. If type contains a MIME type that is not supported ..., then throw a NotSupportedError exception and abort these steps.
107 // 3. If the user agent can't handle any more SourceBuffer objects then throw a QuotaExceededError exception and abort these steps
111 SourceBuffer* buffer = SourceBuffer::create(webSourceBuffer.release(), this, asyncEventQueue());
112 // 6. Add the new object to sourceBuffers and fire a addsourcebuffer on that object.
113 m_sourceBuffers->add(buffer);
114 m_activeSourceBuffers->add(buffer);
115 // 7. Return the new object to the caller.
119 void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionState& exceptionState)
121 WTF_LOG(Media, "MediaSource::removeSourceBuffer() %p", this);
123 // 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-removeSourceBuffer-void-SourceBuffer-sourceBuffer
125 // 1. If sourceBuffer specifies an object that is not in sourceBuffers then
126 // throw a NotFoundError exception and abort these steps.
127 if (!m_sourceBuffers->length() || !m_sourceBuffers->contains(buffer)) {
128 exceptionState.throwDOMException(NotFoundError, "The SourceBuffer provided is not contained in this MediaSource.");
132 // 2. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
133 buffer->abortIfUpdating();
135 // Steps 3-8 are related to updating audioTracks, videoTracks, and textTracks which aren't implmented yet.
136 // FIXME(91649): support track selection
138 // 9. If sourceBuffer is in activeSourceBuffers, then remove sourceBuffer from activeSourceBuffers ...
139 m_activeSourceBuffers->remove(buffer);
141 // 10. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer event
143 m_sourceBuffers->remove(buffer);
145 // 11. Destroy all resources for sourceBuffer.
146 buffer->removedFromMediaSource();
149 void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicString& newState)
152 scheduleEvent(EventTypeNames::sourceopen);
156 if (oldState == openKeyword() && newState == endedKeyword()) {
157 scheduleEvent(EventTypeNames::sourceended);
163 m_activeSourceBuffers->clear();
165 // Clear SourceBuffer references to this object.
166 for (unsigned long i = 0; i < m_sourceBuffers->length(); ++i)
167 m_sourceBuffers->item(i)->removedFromMediaSource();
168 m_sourceBuffers->clear();
170 scheduleEvent(EventTypeNames::sourceclose);
173 Vector<RefPtr<TimeRanges> > MediaSource::activeRanges() const
175 Vector<RefPtr<TimeRanges> > activeRanges(m_activeSourceBuffers->length());
176 for (size_t i = 0; i < m_activeSourceBuffers->length(); ++i)
177 activeRanges[i] = m_activeSourceBuffers->item(i)->buffered(ASSERT_NO_EXCEPTION);
182 bool MediaSource::isUpdating() const
184 // Return true if any member of |m_sourceBuffers| is updating.
185 for (unsigned long i = 0; i < m_sourceBuffers->length(); ++i) {
186 if (m_sourceBuffers->item(i)->updating())
193 bool MediaSource::isTypeSupported(const String& type)
195 WTF_LOG(Media, "MediaSource::isTypeSupported(%s)", type.ascii().data());
197 // Section 2.2 isTypeSupported() method steps.
198 // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#widl-MediaSource-isTypeSupported-boolean-DOMString-type
199 // 1. If type is an empty string, then return false.
200 if (type.isNull() || type.isEmpty())
203 ContentType contentType(type);
204 String codecs = contentType.parameter("codecs");
206 // 2. If type does not contain a valid MIME type string, then return false.
207 if (contentType.type().isEmpty())
210 // 3. If type contains a media type or media subtype that the MediaSource does not support, then return false.
211 // 4. If type contains at a codec that the MediaSource does not support, then return false.
212 // 5. If the MediaSource does not support the specified combination of media type, media subtype, and codecs then return false.
214 return MIMETypeRegistry::isSupportedMediaSourceMIMEType(contentType.type(), codecs);
217 const AtomicString& MediaSource::interfaceName() const
219 return EventTargetNames::MediaSource;
222 void MediaSource::trace(Visitor* visitor)
224 visitor->trace(m_sourceBuffers);
225 visitor->trace(m_activeSourceBuffers);
226 MediaSourceBase::trace(visitor);
229 } // namespace WebCore