OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "config.h" | 25 #include "config.h" |
26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
27 #include "modules/webaudio/OfflineAudioContext.h" | 27 #include "modules/webaudio/OfflineAudioContext.h" |
28 | 28 |
29 #include "bindings/core/v8/ExceptionMessages.h" | 29 #include "bindings/core/v8/ExceptionMessages.h" |
30 #include "bindings/core/v8/ExceptionState.h" | 30 #include "bindings/core/v8/ExceptionState.h" |
31 #include "bindings/core/v8/ScriptState.h" | 31 #include "bindings/core/v8/ScriptState.h" |
32 #include "core/dom/Document.h" | 32 #include "core/dom/Document.h" |
33 #include "core/dom/ExceptionCode.h" | 33 #include "core/dom/ExceptionCode.h" |
34 #include "core/dom/ExecutionContext.h" | 34 #include "core/dom/ExecutionContext.h" |
35 #include "modules/webaudio/OfflineAudioCompletionEvent.h" | |
36 #include "modules/webaudio/OfflineAudioDestinationNode.h" | |
35 #include "platform/audio/AudioUtilities.h" | 37 #include "platform/audio/AudioUtilities.h" |
36 | 38 |
37 namespace blink { | 39 namespace blink { |
38 | 40 |
39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 41 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
40 { | 42 { |
41 // FIXME: add support for workers. | 43 // FIXME: add support for workers. |
42 if (!context || !context->isDocument()) { | 44 if (!context || !context->isDocument()) { |
43 exceptionState.throwDOMException( | 45 exceptionState.throwDOMException( |
44 NotSupportedError, | 46 NotSupportedError, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 + ", " + String::number(sampleRate) | 88 + ", " + String::number(sampleRate) |
87 + ")"); | 89 + ")"); |
88 } | 90 } |
89 | 91 |
90 audioContext->suspendIfNeeded(); | 92 audioContext->suspendIfNeeded(); |
91 return audioContext; | 93 return audioContext; |
92 } | 94 } |
93 | 95 |
94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 96 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) | 97 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) |
98 , m_isRenderingStarted(false) | |
99 , m_totalRenderFrames(numberOfFrames) | |
96 { | 100 { |
101 // Create a new destination for offline rendering. | |
102 m_renderTarget = AudioBuffer::create(numberOfChannels, numberOfFrames, sampl eRate); | |
103 if (m_renderTarget.get()) | |
104 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get()); | |
105 | |
106 initialize(); | |
97 } | 107 } |
98 | 108 |
99 OfflineAudioContext::~OfflineAudioContext() | 109 OfflineAudioContext::~OfflineAudioContext() |
100 { | 110 { |
101 } | 111 } |
102 | 112 |
113 DEFINE_TRACE(OfflineAudioContext) | |
114 { | |
115 visitor->trace(m_renderTarget); | |
116 visitor->trace(m_completeResolver); | |
117 visitor->trace(m_scheduledSuspends); | |
118 AbstractAudioContext::trace(visitor); | |
119 } | |
120 | |
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 121 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
104 { | 122 { |
123 ASSERT(isMainThread()); | |
124 | |
105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 125 // Calling close() on an OfflineAudioContext is not supported/allowed, |
106 // but it might well have been stopped by its execution context. | 126 // but it might well have been closed by its execution context. |
107 if (isContextClosed()) { | 127 if (isContextClosed()) { |
108 return ScriptPromise::rejectWithDOMException( | 128 return ScriptPromise::rejectWithDOMException( |
109 scriptState, | 129 scriptState, |
110 DOMException::create( | 130 DOMException::create( |
111 InvalidStateError, | 131 InvalidStateError, |
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 132 "cannot call startRendering on an OfflineAudioContext in a close d state.")); |
113 } | 133 } |
114 | 134 |
115 if (m_offlineResolver) { | 135 if (m_completeResolver) { |
116 // Can't call startRendering more than once. Return a rejected promise now. | 136 // Can't call startRendering more than once. Return a rejected promise now. |
117 return ScriptPromise::rejectWithDOMException( | 137 return ScriptPromise::rejectWithDOMException( |
118 scriptState, | 138 scriptState, |
119 DOMException::create( | 139 DOMException::create( |
120 InvalidStateError, | 140 InvalidStateError, |
121 "cannot call startRendering more than once")); | 141 "cannot call startRendering more than once")); |
122 } | 142 } |
123 | 143 |
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 144 // If the context is not in the suspended state, reject the promise. |
125 startRendering(); | 145 if (contextState() != AudioContextState::Suspended) { |
126 return m_offlineResolver->promise(); | 146 return ScriptPromise::rejectWithDOMException( |
147 scriptState, | |
148 DOMException::create( | |
149 InvalidStateError, | |
150 "cannot startRendering when an OfflineAudioContext is not in a s uspended state")); | |
151 } | |
152 | |
153 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
154 | |
155 ASSERT(!m_isRenderingStarted); | |
156 | |
157 // Start rendering and return the promise. | |
158 m_isRenderingStarted = true; | |
159 setContextState(Running); | |
160 destinationHandler().startRendering(); | |
161 | |
162 return m_completeResolver->promise(); | |
127 } | 163 } |
128 | 164 |
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 165 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
130 { | 166 { |
131 return ScriptPromise::rejectWithDOMException( | 167 return ScriptPromise::rejectWithDOMException(scriptState, |
132 scriptState, | |
133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); | 168 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); |
134 } | 169 } |
135 | 170 |
171 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when) | |
172 { | |
173 ASSERT(isMainThread()); | |
174 | |
175 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
176 ScriptPromise promise = resolver->promise(); | |
177 | |
178 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
179 size_t frame = when * sampleRate(); | |
Raymond Toy
2015/10/16 23:32:36
No check for negative value of when? That should
hongchan
2015/10/19 20:08:12
Done.
| |
180 frame = frame - (frame % destinationHandler().renderQuantumLength()); | |
Raymond Toy
2015/10/16 23:32:36
Maybe "frame -= ..." is clearer than "frame = fram
hongchan
2015/10/19 20:08:12
Done.
| |
181 | |
182 // The render thread does not exist; reject the promise. | |
183 if (!destinationHandler().offlineRenderThread()) { | |
184 resolver->reject(DOMException::create(InvalidStateError, | |
185 "the rendering is already finished")); | |
186 return promise; | |
187 } | |
188 | |
189 // The specified suspend time is negative; reject the promise. | |
190 if (when < 0) { | |
191 resolver->reject(DOMException::create(InvalidStateError, | |
192 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
193 return promise; | |
194 } | |
195 | |
196 // The suspend time should be earlier than the total render frame. If the | |
197 // requested suspension time is equal to the total render frame, the promise | |
198 // will be rejected. | |
199 if (m_totalRenderFrames <= frame) { | |
200 resolver->reject(DOMException::create(InvalidStateError, | |
201 "cannot schedule a suspend at frame " + String::number(frame) + | |
202 " (" + String::number(when) + " seconds) " + | |
203 "because it is greater than or equal to the total render duration of " + | |
204 String::number(m_totalRenderFrames) + " frames")); | |
205 return promise; | |
206 } | |
207 | |
208 // The specified suspend time is in the past; reject the promise. | |
209 if (frame < currentSampleFrame()) { | |
210 resolver->reject(DOMException::create(InvalidStateError, | |
211 "cannot schedule a suspend at frame " + | |
212 String::number(frame) + " (" + String::number(when) + | |
213 " seconds) because it is earlier than the current frame of " + | |
214 String::number(currentSampleFrame()) + " (" + | |
215 String::number(currentTime()) + " seconds)")); | |
216 return promise; | |
217 } | |
218 | |
219 // If there is a duplicate suspension at the same quantized frame, | |
220 // reject the promise. | |
221 if (m_scheduledSuspends.contains(frame)) { | |
222 resolver->reject(DOMException::create(InvalidStateError, | |
223 "cannot schedule more than one suspend at frame " + | |
224 String::number(frame) + " (" + | |
225 String::number(when) + " seconds)")); | |
226 return promise; | |
227 } | |
228 | |
229 // Wait until the suspend map is available for the insertion. | |
230 AutoLocker lock(this); | |
231 | |
232 m_scheduledSuspends.add(frame, resolver); | |
233 | |
234 return promise; | |
235 } | |
236 | |
237 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | |
238 { | |
239 ASSERT(isMainThread()); | |
240 | |
241 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
242 ScriptPromise promise = resolver->promise(); | |
243 | |
244 // If the context is not in a suspended state, reject the promise. | |
245 if (contextState() != AudioContextState::Suspended) { | |
246 resolver->reject(DOMException::create(InvalidStateError, | |
247 "cannot resume a context that is not suspended")); | |
Raymond Toy
2015/10/16 23:32:36
Maybe say what state we're actually in. Just for
hongchan
2015/10/20 22:03:06
Okay, I will reverse the order of the checks. So i
| |
248 return promise; | |
249 } | |
250 | |
251 // If the rendering has not started, reject the promise. | |
252 if (!m_isRenderingStarted) { | |
253 resolver->reject(DOMException::create(InvalidStateError, | |
254 "cannot resume a context that has not started")); | |
255 return promise; | |
256 } | |
257 | |
258 // If the context is suspended, resume rendering by setting the state to | |
259 // "Running." and calling startRendering(). Note that resuming is possible | |
260 // only after the rendering started. | |
261 setContextState(Running); | |
262 destinationHandler().startRendering(); | |
263 | |
264 // Resolve the promise immediately. | |
265 resolver->resolve(); | |
266 | |
267 return promise; | |
268 } | |
269 | |
136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | 270 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) |
137 { | 271 { |
138 return ScriptPromise::rejectWithDOMException( | 272 // This CANNOT be called on OfflineAudioContext; this is only to implement |
139 scriptState, | 273 // the pure virtual interface from AbstractAudioContext. |
140 DOMException::create( | 274 RELEASE_ASSERT_NOT_REACHED(); |
141 InvalidAccessError, | 275 |
142 "cannot suspend an OfflineAudioContext")); | 276 return ScriptPromise(); |
Raymond Toy
2015/10/16 23:32:36
Although this isn't reachable, maybe we should ret
hongchan
2015/10/19 20:08:12
Done.
| |
143 } | 277 } |
144 | 278 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 279 void OfflineAudioContext::fireCompletionEvent() |
146 { | 280 { |
147 return ScriptPromise::rejectWithDOMException( | 281 ASSERT(isMainThread()); |
148 scriptState, | 282 |
149 DOMException::create( | 283 // We set the state to closed here so that the oncomplete event handler sees |
150 InvalidAccessError, | 284 // that the context has been closed. |
151 "cannot resume an OfflineAudioContext")); | 285 setContextState(Closed); |
286 | |
287 AudioBuffer* renderedBuffer = renderTarget(); | |
288 | |
289 ASSERT(renderedBuffer); | |
290 if (!renderedBuffer) | |
291 return; | |
292 | |
293 // Avoid firing the event if the document has already gone away. | |
294 if (executionContext()) { | |
295 // Call the offline rendering completion event listener and resolve the | |
296 // promise too. | |
297 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
298 m_completeResolver->resolve(renderedBuffer); | |
299 } else { | |
300 // The resolver should be rejected when the execution context is gone. | |
301 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
302 "the execution context does not exist")); | |
303 } | |
304 } | |
305 | |
306 bool OfflineAudioContext::handlePreOfflineRenderTasks() | |
307 { | |
308 ASSERT(isAudioThread()); | |
309 | |
310 // This is offline rendering, so it is safe to lock. | |
Raymond Toy
2015/10/16 23:32:36
I would expand on this to say why it's safe to cau
hongchan
2015/10/19 20:08:12
Done.
| |
311 deferredTaskHandler().offlineLock(); | |
312 | |
313 deferredTaskHandler().handleDeferredTasks(); | |
314 handleStoppableSourceNodes(); | |
315 deferredTaskHandler().unlock(); | |
316 | |
317 return shouldSuspend(); | |
318 } | |
319 | |
320 void OfflineAudioContext::handlePostOfflineRenderTasks() | |
321 { | |
322 ASSERT(isAudioThread()); | |
323 | |
324 // This is offline rendering, so it is safe to lock. | |
Raymond Toy
2015/10/16 23:32:36
Expand on this comment, like above.
hongchan
2015/10/19 20:08:12
Done.
| |
325 deferredTaskHandler().offlineLock(); | |
326 | |
327 deferredTaskHandler().breakConnections(); | |
328 releaseFinishedSourceNodes(); | |
329 deferredTaskHandler().handleDeferredTasks(); | |
330 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); | |
331 | |
332 deferredTaskHandler().unlock(); | |
333 } | |
334 | |
335 | |
336 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
337 { | |
338 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler()); | |
339 } | |
340 | |
341 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) | |
342 { | |
343 ASSERT(isMainThread()); | |
344 | |
345 // Suspend the context first. This will fire onstatechange event. | |
346 setContextState(Suspended); | |
347 | |
348 ASSERT(m_scheduledSuspends.contains(frame)); | |
349 | |
350 // Wait until the suspend map is available for the removal. | |
351 AutoLocker locker(this); | |
352 | |
353 SuspendMap::iterator it = m_scheduledSuspends.find(frame); | |
354 it->value->resolve(); | |
355 | |
356 m_scheduledSuspends.remove(it); | |
357 } | |
358 | |
359 bool OfflineAudioContext::shouldSuspend() | |
360 { | |
361 ASSERT(isAudioThread()); | |
362 | |
363 if (m_scheduledSuspends.contains(currentSampleFrame())) | |
364 return true; | |
365 | |
366 return false; | |
152 } | 367 } |
153 | 368 |
154 } // namespace blink | 369 } // namespace blink |
155 | 370 |
156 #endif // ENABLE(WEB_AUDIO) | 371 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |