Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / mediasource / MediaSource.cpp
1 /*
2  * Copyright (C) 2013 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 "modules/mediasource/MediaSource.h"
33
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"
46
47 using blink::WebSourceBuffer;
48
49 namespace WebCore {
50
51 MediaSource* MediaSource::create(ExecutionContext* context)
52 {
53     MediaSource* mediaSource(adoptRefCountedGarbageCollected(new MediaSource(context)));
54     mediaSource->suspendIfNeeded();
55     return mediaSource;
56 }
57
58 MediaSource::MediaSource(ExecutionContext* context)
59     : MediaSourceBase(context)
60 {
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());
65 }
66
67 MediaSource::~MediaSource()
68 {
69     WTF_LOG(Media, "MediaSource::~MediaSource %p", this);
70     ASSERT(isClosed());
71 }
72
73 SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionState& exceptionState)
74 {
75     WTF_LOG(Media, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
76
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.
80     if (type.isEmpty()) {
81         exceptionState.throwDOMException(InvalidAccessError, "The type provided is empty.");
82         return 0;
83     }
84
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.");
89         return 0;
90     }
91
92     // 4. If the readyState attribute is not in the "open" state then throw an
93     // InvalidStateError exception and abort these steps.
94     if (!isOpen()) {
95         exceptionState.throwDOMException(InvalidStateError, "The MediaSource's readyState is not 'open'.");
96         return 0;
97     }
98
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);
103
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
108         return 0;
109     }
110
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.
116     return buffer;
117 }
118
119 void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionState& exceptionState)
120 {
121     WTF_LOG(Media, "MediaSource::removeSourceBuffer() %p", this);
122
123     // 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-removeSourceBuffer-void-SourceBuffer-sourceBuffer
124
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.");
129         return;
130     }
131
132     // 2. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
133     buffer->abortIfUpdating();
134
135     // Steps 3-8 are related to updating audioTracks, videoTracks, and textTracks which aren't implmented yet.
136     // FIXME(91649): support track selection
137
138     // 9. If sourceBuffer is in activeSourceBuffers, then remove sourceBuffer from activeSourceBuffers ...
139     m_activeSourceBuffers->remove(buffer);
140
141     // 10. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer event
142     // on that object.
143     m_sourceBuffers->remove(buffer);
144
145     // 11. Destroy all resources for sourceBuffer.
146     buffer->removedFromMediaSource();
147 }
148
149 void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicString& newState)
150 {
151     if (isOpen()) {
152         scheduleEvent(EventTypeNames::sourceopen);
153         return;
154     }
155
156     if (oldState == openKeyword() && newState == endedKeyword()) {
157         scheduleEvent(EventTypeNames::sourceended);
158         return;
159     }
160
161     ASSERT(isClosed());
162
163     m_activeSourceBuffers->clear();
164
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();
169
170     scheduleEvent(EventTypeNames::sourceclose);
171 }
172
173 Vector<RefPtr<TimeRanges> > MediaSource::activeRanges() const
174 {
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);
178
179     return activeRanges;
180 }
181
182 bool MediaSource::isUpdating() const
183 {
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())
187             return true;
188     }
189
190     return false;
191 }
192
193 bool MediaSource::isTypeSupported(const String& type)
194 {
195     WTF_LOG(Media, "MediaSource::isTypeSupported(%s)", type.ascii().data());
196
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())
201         return false;
202
203     ContentType contentType(type);
204     String codecs = contentType.parameter("codecs");
205
206     // 2. If type does not contain a valid MIME type string, then return false.
207     if (contentType.type().isEmpty())
208         return false;
209
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.
213     // 6. Return true.
214     return MIMETypeRegistry::isSupportedMediaSourceMIMEType(contentType.type(), codecs);
215 }
216
217 const AtomicString& MediaSource::interfaceName() const
218 {
219     return EventTargetNames::MediaSource;
220 }
221
222 void MediaSource::trace(Visitor* visitor)
223 {
224     visitor->trace(m_sourceBuffers);
225     visitor->trace(m_activeSourceBuffers);
226     MediaSourceBase::trace(visitor);
227 }
228
229 } // namespace WebCore