OLD | NEW |
---|---|
(Empty) | |
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/SourceBuffer.h" | |
33 | |
34 #include "core/dom/Event.h" | |
35 #include "core/dom/GenericEventQueue.h" | |
36 #include "core/html/TimeRanges.h" | |
37 #include "core/platform/Logging.h" | |
38 #include "core/platform/graphics/SourceBufferPrivate.h" | |
39 #include "modules/mediasource/MediaSource.h" | |
40 #include "wtf/ArrayBuffer.h" | |
41 #include "wtf/ArrayBufferView.h" | |
42 | |
43 namespace WebCore { | |
44 | |
45 PassRefPtr<SourceBuffer> SourceBuffer::create(PassOwnPtr<SourceBufferPrivate> so urceBufferPrivate, MediaSource* source, GenericEventQueue* asyncEventQueue) | |
46 { | |
47 RefPtr<SourceBuffer> sourceBuffer(adoptRef(new SourceBuffer(sourceBufferPriv ate, source, asyncEventQueue))); | |
48 sourceBuffer->suspendIfNeeded(); | |
49 return sourceBuffer.release(); | |
50 } | |
51 | |
52 SourceBuffer::SourceBuffer(PassOwnPtr<SourceBufferPrivate> sourceBufferPrivate, MediaSource* source, GenericEventQueue* asyncEventQueue) | |
53 : ActiveDOMObject(source->scriptExecutionContext()) | |
54 , m_private(sourceBufferPrivate) | |
55 , m_source(source) | |
56 , m_asyncEventQueue(asyncEventQueue) | |
57 , m_updating(false) | |
58 , m_timestampOffset(0) | |
59 , m_appendBufferTimer(this, &SourceBuffer::appendBufferTimerFired) | |
60 { | |
61 ASSERT(m_private); | |
62 ASSERT(m_source); | |
63 ScriptWrappable::init(this); | |
64 } | |
65 | |
66 SourceBuffer::~SourceBuffer() | |
adamk
2013/06/11 18:19:26
Would it be useful to ASSERT(isRemoved()) here?
acolwell GONE FROM CHROMIUM
2013/06/12 01:14:04
Done.
| |
67 { | |
68 } | |
69 | |
70 PassRefPtr<TimeRanges> SourceBuffer::buffered(ExceptionCode& ec) const | |
71 { | |
72 // Section 3.1 buffered attribute steps. | |
73 // 1. If this object has been removed from the sourceBuffers attribute of th e parent media source then throw an | |
74 // INVALID_STATE_ERR exception and abort these steps. | |
75 if (isRemoved()) { | |
76 ec = INVALID_STATE_ERR; | |
77 return 0; | |
78 } | |
79 | |
80 // 2. Return a new static normalized TimeRanges object for the media segment s buffered. | |
81 return m_private->buffered(); | |
82 } | |
83 | |
84 double SourceBuffer::timestampOffset() const | |
85 { | |
86 return m_timestampOffset; | |
87 } | |
88 | |
89 void SourceBuffer::setTimestampOffset(double offset, ExceptionCode& ec) | |
90 { | |
91 // Section 3.1 timestampOffset attribute setter steps. | |
92 // 1. If this object has been removed from the sourceBuffers attribute of th e parent media source then throw an | |
93 // INVALID_STATE_ERR exception and abort these steps. | |
94 if (isRemoved()) { | |
95 ec = INVALID_STATE_ERR; | |
96 return; | |
97 } | |
98 | |
99 // 4. If the readyState attribute of the parent media source is in the "ende d" state then run the following steps: | |
100 // 4.1 Set the readyState attribute of the parent media source to "open" | |
101 // 4.2 Queue a task to fire a simple event named sourceopen at the parent me dia source. | |
102 m_source->openIfInEndedState(); | |
103 | |
104 // 5. If this object is waiting for the end of a media segment to be appende d, then throw an INVALID_STATE_ERR | |
105 // and abort these steps. | |
106 if (!m_private->setTimestampOffset(offset)) { | |
107 ec = INVALID_STATE_ERR; | |
108 return; | |
109 } | |
110 | |
111 // 6. Update the attribute to the new value. | |
112 m_timestampOffset = offset; | |
113 } | |
114 | |
115 void SourceBuffer::appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionCode& ec) | |
116 { | |
117 // Section 3.2 appendBuffer() | |
118 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data | |
119 // 1. If data is null then throw an INVALID_ACCESS_ERR exception and abort t hese steps. | |
120 if (!data) { | |
121 ec = INVALID_ACCESS_ERR; | |
122 return; | |
123 } | |
124 | |
125 appendBufferInternal(reinterpret_cast<unsigned char*>(data->data()), data->b yteLength(), ec); | |
adamk
2013/06/11 18:19:26
This can just be a static_cast, can't it (since da
acolwell GONE FROM CHROMIUM
2013/06/12 01:14:04
Done.
| |
126 } | |
127 | |
128 void SourceBuffer::appendBuffer(PassRefPtr<ArrayBufferView> data, ExceptionCode& ec) | |
129 { | |
130 // Section 3.2 appendBuffer() | |
131 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data | |
132 // 1. If data is null then throw an INVALID_ACCESS_ERR exception and abort t hese steps. | |
133 if (!data) { | |
134 ec = INVALID_ACCESS_ERR; | |
135 return; | |
136 } | |
137 | |
138 appendBufferInternal(reinterpret_cast<unsigned char*>(data->baseAddress()), data->byteLength(), ec); | |
adamk
2013/06/11 18:19:26
Same here, prefer static_cast when possible.
acolwell GONE FROM CHROMIUM
2013/06/12 01:14:04
Done.
| |
139 } | |
140 | |
141 void SourceBuffer::abort(ExceptionCode& ec) | |
142 { | |
143 // Section 3.2 abort() method steps. | |
144 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#widl-SourceBuffer-abort-void | |
145 // 1. If this object has been removed from the sourceBuffers attribute of th e parent media source | |
146 // then throw an INVALID_STATE_ERR exception and abort these steps. | |
147 // 2. If the readyState attribute of the parent media source is not in the " open" state | |
148 // then throw an INVALID_STATE_ERR exception and abort these steps. | |
149 if (isRemoved() || !m_source->isOpen()) { | |
150 ec = INVALID_STATE_ERR; | |
151 return; | |
152 } | |
153 | |
154 // 3. If the sourceBuffer.updating attribute equals true, then run the follo wing steps: ... | |
155 abortIfUpdating(); | |
156 | |
157 // 4. Run the reset parser state algorithm. | |
158 m_private->abort(); | |
159 | |
160 // FIXME(229408) Add steps 5-6 update appendWindowStart & appendWindowEnd. | |
161 } | |
162 | |
163 | |
164 void SourceBuffer::abortIfUpdating() | |
165 { | |
166 // Section 3.2 abort() method step 3 substeps. | |
167 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#widl-SourceBuffer-abort-void | |
168 | |
169 if (!m_updating) | |
170 return; | |
171 | |
172 // 3.1. Abort the buffer append and stream append loop algorithms if they ar e running. | |
173 m_appendBufferTimer.stop(); | |
174 m_pendingAppendData.clear(); | |
175 | |
176 // 3.2. Set the updating attribute to false. | |
177 m_updating = false; | |
178 | |
179 // 3.3. Queue a task to fire a simple event named abort at this SourceBuffer object. | |
180 scheduleEvent(eventNames().abortEvent); | |
181 | |
182 // 3.4. Queue a task to fire a simple event named updateend at this SourceBu ffer object. | |
183 scheduleEvent(eventNames().updateendEvent); | |
184 } | |
185 | |
186 void SourceBuffer::removedFromMediaSource() | |
187 { | |
188 if (isRemoved()) | |
189 return; | |
190 | |
191 m_private->removedFromMediaSource(); | |
192 m_source = 0; | |
193 m_asyncEventQueue = 0; | |
194 } | |
195 | |
196 bool SourceBuffer::hasPendingActivity() const | |
197 { | |
198 return m_source; | |
199 } | |
200 | |
201 void SourceBuffer::stop() | |
202 { | |
203 m_appendBufferTimer.stop(); | |
204 } | |
205 | |
206 ScriptExecutionContext* SourceBuffer::scriptExecutionContext() const | |
207 { | |
208 return ActiveDOMObject::scriptExecutionContext(); | |
209 } | |
210 | |
211 const AtomicString& SourceBuffer::interfaceName() const | |
212 { | |
213 return eventNames().interfaceForSourceBuffer; | |
214 } | |
215 | |
216 | |
adamk
2013/06/11 18:19:26
Nit: extra blank line.
acolwell GONE FROM CHROMIUM
2013/06/12 01:14:04
Done.
| |
217 EventTargetData* SourceBuffer::eventTargetData() | |
218 { | |
219 return &m_eventTargetData; | |
220 } | |
221 | |
222 EventTargetData* SourceBuffer::ensureEventTargetData() | |
223 { | |
224 return &m_eventTargetData; | |
225 } | |
226 | |
227 bool SourceBuffer::isRemoved() const | |
228 { | |
229 return !m_source; | |
230 } | |
231 | |
232 void SourceBuffer::scheduleEvent(const AtomicString& eventName) | |
233 { | |
234 ASSERT(m_asyncEventQueue); | |
235 | |
236 RefPtr<Event> event = Event::create(eventName, false, false); | |
237 event->setTarget(this); | |
238 | |
239 m_asyncEventQueue->enqueueEvent(event.release()); | |
240 } | |
241 | |
242 void SourceBuffer::appendBufferInternal(unsigned char* data, unsigned size, Exce ptionCode& ec) | |
243 { | |
244 // Section 3.2 appendBuffer() | |
245 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data | |
246 | |
247 // Step 1 is enforced by the caller. | |
248 // 2. If this object has been removed from the sourceBuffers attribute of th e parent media source then throw an INVALID_STATE_ERR exception and abort these steps. | |
249 // 3. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps. | |
250 if (isRemoved() || m_updating) { | |
251 ec = INVALID_STATE_ERR; | |
252 return; | |
253 } | |
254 | |
255 // 4. If the readyState attribute of the parent media source is in the "ende d" state then run the following steps: ... | |
256 m_source->openIfInEndedState(); | |
257 | |
258 // Steps 5-6 | |
259 | |
260 // 7. Add data to the end of the input buffer. | |
261 m_pendingAppendData.append(data, size); | |
262 | |
263 // 8. Set the updating attribute to true. | |
264 m_updating = true; | |
265 | |
266 // 9. Queue a task to fire a simple event named updatestart at this SourceBu ffer object. | |
267 scheduleEvent(eventNames().updatestartEvent); | |
268 | |
269 // 10. Asynchronously run the buffer append algorithm. | |
270 m_appendBufferTimer.startOneShot(0); | |
271 } | |
272 | |
273 void SourceBuffer::appendBufferTimerFired(Timer<SourceBuffer>*) | |
274 { | |
275 ASSERT(m_updating); | |
276 | |
277 // Section 3.5.4 Buffer Append Algorithm | |
278 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou rce.html#sourcebuffer-buffer-append | |
279 | |
280 // 1. Run the segment parser loop algorithm. | |
281 // Step 2 doesn't apply since we run Step 1 synchronously here. | |
282 m_private->append(&m_pendingAppendData[0], m_pendingAppendData.size()); | |
283 | |
284 | |
285 // 3. Set the updating attribute to false. | |
286 m_updating = false; | |
287 m_pendingAppendData.clear(); | |
288 | |
289 // 4. Queue a task to fire a simple event named update at this SourceBuffer object. | |
290 scheduleEvent(eventNames().updateEvent); | |
291 | |
292 // 5. Queue a task to fire a simple event named updateend at this SourceBuff er object. | |
293 scheduleEvent(eventNames().updateendEvent); | |
294 } | |
295 | |
296 } // namespace WebCore | |
OLD | NEW |