OLD | NEW |
| (Empty) |
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 "TimeRanges.h" | |
39 #include <wtf/Uint8Array.h> | |
40 | |
41 namespace WebCore { | |
42 | |
43 PassRefPtr<MediaSource> MediaSource::create(ScriptExecutionContext* context) | |
44 { | |
45 return adoptRef(new MediaSource(context)); | |
46 } | |
47 | |
48 MediaSource::MediaSource(ScriptExecutionContext* context) | |
49 : ContextDestructionObserver(context) | |
50 , m_readyState(closedKeyword()) | |
51 , m_player(0) | |
52 , m_nextSourceBufferId(0) | |
53 { | |
54 m_sourceBuffers = SourceBufferList::create(scriptExecutionContext()); | |
55 m_activeSourceBuffers = SourceBufferList::create(scriptExecutionContext()); | |
56 } | |
57 | |
58 SourceBufferList* MediaSource::sourceBuffers() | |
59 { | |
60 return m_sourceBuffers.get(); | |
61 } | |
62 | |
63 SourceBufferList* MediaSource::activeSourceBuffers() | |
64 { | |
65 // FIXME(91649): support track selection | |
66 return m_activeSourceBuffers.get(); | |
67 } | |
68 | |
69 SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionCode& ec
) | |
70 { | |
71 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-sour
ce.html#dom-addsourcebuffer | |
72 // 1. If type is null or an empty then throw an INVALID_ACCESS_ERR exception
and | |
73 // abort these steps. | |
74 if (type.isNull() || type.isEmpty()) { | |
75 ec = INVALID_ACCESS_ERR; | |
76 return 0; | |
77 } | |
78 | |
79 // 2. If type contains a MIME type that is not supported ..., then throw a | |
80 // NOT_SUPPORTED_ERR exception and abort these steps. | |
81 ContentType contentType(type); | |
82 Vector<String> codecs = contentType.codecs(); | |
83 | |
84 if (!codecs.size()) { | |
85 ec = NOT_SUPPORTED_ERR; | |
86 return 0; | |
87 } | |
88 | |
89 // 4. If the readyState attribute is not in the "open" state then throw an | |
90 // INVALID_STATE_ERR exception and abort these steps. | |
91 if (!m_player || m_readyState != openKeyword()) { | |
92 ec = INVALID_STATE_ERR; | |
93 return 0; | |
94 } | |
95 | |
96 // 5. Create a new SourceBuffer object and associated resources. | |
97 RefPtr<SourceBuffer> buffer = SourceBuffer::create(String::number(++m_nextSo
urceBufferId), this); | |
98 | |
99 switch (m_player->sourceAddId(buffer->id(), contentType.type(), codecs)) { | |
100 case MediaPlayer::Ok: | |
101 // 6. Add the new object to sourceBuffers and fire a addsourcebuffer on
that object. | |
102 m_sourceBuffers->add(buffer); | |
103 m_activeSourceBuffers->add(buffer); | |
104 // 7. Return the new object to the caller. | |
105 return buffer.get(); | |
106 case MediaPlayer::NotSupported: | |
107 // 2 (cont). If type contains a MIME type ... that is not supported with
the types | |
108 // specified for the other SourceBuffer objects in sourceBuffers, then t
hrow | |
109 // a NOT_SUPPORTED_ERR exception and abort these steps. | |
110 ec = NOT_SUPPORTED_ERR; | |
111 return 0; | |
112 case MediaPlayer::ReachedIdLimit: | |
113 // 3 (cont). If the user agent can't handle any more SourceBuffer object
s then throw | |
114 // a QUOTA_EXCEEDED_ERR exception and abort these steps. | |
115 ec = QUOTA_EXCEEDED_ERR; | |
116 return 0; | |
117 } | |
118 | |
119 ASSERT_NOT_REACHED(); | |
120 return 0; | |
121 } | |
122 | |
123 void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec) | |
124 { | |
125 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-sour
ce.html#dom-removesourcebuffer | |
126 // 1. If sourceBuffer is null then throw an INVALID_ACCESS_ERR exception and | |
127 // abort these steps. | |
128 if (!buffer) { | |
129 ec = INVALID_ACCESS_ERR; | |
130 return; | |
131 } | |
132 | |
133 // 2. If sourceBuffers is empty then throw an INVALID_STATE_ERR exception an
d | |
134 // abort these steps. | |
135 if (!m_player || !m_sourceBuffers->length()) { | |
136 ec = INVALID_STATE_ERR; | |
137 return; | |
138 } | |
139 | |
140 // 3. If sourceBuffer specifies an object that is not in sourceBuffers then | |
141 // throw a NOT_FOUND_ERR exception and abort these steps. | |
142 // 6. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer e
vent | |
143 // on that object. | |
144 if (!m_sourceBuffers->remove(buffer)) { | |
145 ec = NOT_FOUND_ERR; | |
146 return; | |
147 } | |
148 | |
149 // 7. Destroy all resources for sourceBuffer. | |
150 m_activeSourceBuffers->remove(buffer); | |
151 m_player->sourceRemoveId(buffer->id()); | |
152 | |
153 // 4. Remove track information from audioTracks, videoTracks, and textTracks
for all tracks | |
154 // associated with sourceBuffer and fire a simple event named change on the
modified lists. | |
155 // FIXME(91649): support track selection | |
156 | |
157 // 5. If sourceBuffer is in activeSourceBuffers, then remove it from that li
st and fire a | |
158 // removesourcebuffer event on that object. | |
159 // FIXME(91649): support track selection | |
160 } | |
161 | |
162 const String& MediaSource::readyState() const | |
163 { | |
164 return m_readyState; | |
165 } | |
166 | |
167 void MediaSource::setReadyState(const String& state) | |
168 { | |
169 ASSERT(state == openKeyword() || state == closedKeyword() || state == endedK
eyword()); | |
170 if (m_readyState == state) | |
171 return; | |
172 | |
173 String oldState = m_readyState; | |
174 m_readyState = state; | |
175 | |
176 if (m_readyState == closedKeyword()) { | |
177 m_sourceBuffers->clear(); | |
178 m_activeSourceBuffers->clear(); | |
179 m_player = 0; | |
180 dispatchEvent(Event::create(eventNames().webkitsourcecloseEvent, false,
false)); | |
181 return; | |
182 } | |
183 | |
184 if (oldState == openKeyword() && m_readyState == endedKeyword()) { | |
185 dispatchEvent(Event::create(eventNames().webkitsourceendedEvent, false,
false)); | |
186 return; | |
187 } | |
188 | |
189 if (m_readyState == openKeyword()) { | |
190 dispatchEvent(Event::create(eventNames().webkitsourceopenEvent, false, f
alse)); | |
191 return; | |
192 } | |
193 } | |
194 | |
195 void MediaSource::endOfStream(const String& error, ExceptionCode& ec) | |
196 { | |
197 // 3.1 http://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-sour
ce.html#dom-endofstream | |
198 // 1. If the readyState attribute is not in the "open" state then throw an | |
199 // INVALID_STATE_ERR exception and abort these steps. | |
200 if (!m_player || m_readyState != openKeyword()) { | |
201 ec = INVALID_STATE_ERR; | |
202 return; | |
203 } | |
204 | |
205 MediaPlayer::EndOfStreamStatus eosStatus = MediaPlayer::EosNoError; | |
206 | |
207 if (error.isNull() || error.isEmpty()) | |
208 eosStatus = MediaPlayer::EosNoError; | |
209 else if (error == "network") | |
210 eosStatus = MediaPlayer::EosNetworkError; | |
211 else if (error == "decode") | |
212 eosStatus = MediaPlayer::EosDecodeError; | |
213 else { | |
214 ec = INVALID_ACCESS_ERR; | |
215 return; | |
216 } | |
217 | |
218 // 2. Change the readyState attribute value to "ended". | |
219 setReadyState(endedKeyword()); | |
220 m_player->sourceEndOfStream(eosStatus); | |
221 } | |
222 | |
223 PassRefPtr<TimeRanges> MediaSource::buffered(const String& id, ExceptionCode& ec
) const | |
224 { | |
225 if (!m_player || m_readyState == closedKeyword()) { | |
226 ec = INVALID_STATE_ERR; | |
227 return 0; | |
228 } | |
229 | |
230 return m_player->sourceBuffered(id); | |
231 } | |
232 | |
233 void MediaSource::append(const String& id, PassRefPtr<Uint8Array> data, Exceptio
nCode& ec) | |
234 { | |
235 if (!data.get()) { | |
236 ec = INVALID_ACCESS_ERR; | |
237 return; | |
238 } | |
239 | |
240 if (!m_player || m_readyState != openKeyword()) { | |
241 ec = INVALID_STATE_ERR; | |
242 return; | |
243 } | |
244 | |
245 if (!data->length()) | |
246 return; | |
247 | |
248 if (!m_player->sourceAppend(id, data->data(), data->length())) { | |
249 ec = SYNTAX_ERR; | |
250 return; | |
251 } | |
252 } | |
253 | |
254 void MediaSource::abort(const String& id, ExceptionCode& ec) | |
255 { | |
256 if (!m_player || m_readyState != openKeyword()) { | |
257 ec = INVALID_STATE_ERR; | |
258 return; | |
259 } | |
260 | |
261 if (!m_player->sourceAbort(id)) | |
262 ASSERT_NOT_REACHED(); | |
263 } | |
264 | |
265 const AtomicString& MediaSource::interfaceName() const | |
266 { | |
267 return eventNames().interfaceForMediaSource; | |
268 } | |
269 | |
270 ScriptExecutionContext* MediaSource::scriptExecutionContext() const | |
271 { | |
272 return ContextDestructionObserver::scriptExecutionContext(); | |
273 } | |
274 | |
275 EventTargetData* MediaSource::eventTargetData() | |
276 { | |
277 return &m_eventTargetData; | |
278 } | |
279 | |
280 EventTargetData* MediaSource::ensureEventTargetData() | |
281 { | |
282 return &m_eventTargetData; | |
283 } | |
284 | |
285 } // namespace WebCore | |
286 | |
287 #endif | |
OLD | NEW |