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/DeferredTaskHandler.h" | |
36 #include "modules/webaudio/OfflineAudioCompletionEvent.h" | |
37 #include "modules/webaudio/OfflineAudioDestinationNode.h" | |
38 | |
35 #include "platform/audio/AudioUtilities.h" | 39 #include "platform/audio/AudioUtilities.h" |
36 | 40 |
37 namespace blink { | 41 namespace blink { |
38 | 42 |
39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 43 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
40 { | 44 { |
41 // FIXME: add support for workers. | 45 // FIXME: add support for workers. |
42 if (!context || !context->isDocument()) { | 46 if (!context || !context->isDocument()) { |
43 exceptionState.throwDOMException( | 47 exceptionState.throwDOMException( |
44 NotSupportedError, | 48 NotSupportedError, |
(...skipping 24 matching lines...) Expand all Loading... | |
69 if (!AudioUtilities::isValidAudioBufferSampleRate(sampleRate)) { | 73 if (!AudioUtilities::isValidAudioBufferSampleRate(sampleRate)) { |
70 exceptionState.throwDOMException( | 74 exceptionState.throwDOMException( |
71 IndexSizeError, | 75 IndexSizeError, |
72 ExceptionMessages::indexOutsideRange( | 76 ExceptionMessages::indexOutsideRange( |
73 "sampleRate", sampleRate, | 77 "sampleRate", sampleRate, |
74 AudioUtilities::minAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound, | 78 AudioUtilities::minAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound, |
75 AudioUtilities::maxAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound)); | 79 AudioUtilities::maxAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound)); |
76 return nullptr; | 80 return nullptr; |
77 } | 81 } |
78 | 82 |
79 OfflineAudioContext* audioContext = new OfflineAudioContext(document, number OfChannels, numberOfFrames, sampleRate); | 83 OfflineAudioContext* audioContext = new OfflineAudioContext(document, number OfChannels, numberOfFrames, sampleRate, exceptionState); |
80 | 84 |
81 if (!audioContext->destination()) { | 85 if (!audioContext->destination()) { |
82 exceptionState.throwDOMException( | 86 exceptionState.throwDOMException( |
83 NotSupportedError, | 87 NotSupportedError, |
84 "OfflineAudioContext(" + String::number(numberOfChannels) | 88 "OfflineAudioContext(" + String::number(numberOfChannels) |
85 + ", " + String::number(numberOfFrames) | 89 + ", " + String::number(numberOfFrames) |
86 + ", " + String::number(sampleRate) | 90 + ", " + String::number(sampleRate) |
87 + ")"); | 91 + ")"); |
88 } | 92 } |
89 | 93 |
90 audioContext->suspendIfNeeded(); | 94 audioContext->suspendIfNeeded(); |
91 return audioContext; | 95 return audioContext; |
92 } | 96 } |
93 | 97 |
94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 98 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) | 99 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) |
100 , m_isRenderingStarted(false) | |
101 , m_totalRenderFrames(numberOfFrames) | |
96 { | 102 { |
103 // Create a new destination for offline rendering. | |
104 m_renderTarget = AudioBuffer::create(numberOfChannels, numberOfFrames, sampl eRate); | |
105 | |
106 // Throw an exception if the render target is not ready. | |
107 if (!m_renderTarget) { | |
108 exceptionState.throwDOMException(InvalidAccessError, | |
Raymond Toy
2015/10/21 23:14:18
I think the CL was using RangeError for arrays tha
hongchan
2015/10/22 18:23:49
Done.
| |
109 "failed to create a target buffer for offline audio rendering."); | |
Raymond Toy
2015/10/21 23:14:18
Include the the requested number of frames and num
hongchan
2015/10/22 18:23:49
Done.
| |
110 } | |
111 | |
112 // TODO(hongchan): throw an exception when the buffer creation fails. | |
113 if (m_renderTarget.get()) | |
114 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get()); | |
115 | |
116 initialize(); | |
97 } | 117 } |
98 | 118 |
99 OfflineAudioContext::~OfflineAudioContext() | 119 OfflineAudioContext::~OfflineAudioContext() |
100 { | 120 { |
101 } | 121 } |
102 | 122 |
123 DEFINE_TRACE(OfflineAudioContext) | |
124 { | |
125 visitor->trace(m_renderTarget); | |
126 visitor->trace(m_completeResolver); | |
127 visitor->trace(m_scheduledSuspends); | |
128 AbstractAudioContext::trace(visitor); | |
129 } | |
130 | |
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 131 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
104 { | 132 { |
133 ASSERT(isMainThread()); | |
134 | |
105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 135 // Calling close() on an OfflineAudioContext is not supported/allowed, |
106 // but it might well have been stopped by its execution context. | 136 // but it might well have been stopped by its execution context. |
137 // | |
138 // See: crbug.com/435867 | |
107 if (isContextClosed()) { | 139 if (isContextClosed()) { |
108 return ScriptPromise::rejectWithDOMException( | 140 return ScriptPromise::rejectWithDOMException( |
109 scriptState, | 141 scriptState, |
110 DOMException::create( | 142 DOMException::create( |
111 InvalidStateError, | 143 InvalidStateError, |
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 144 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
113 } | 145 } |
114 | 146 |
115 if (m_offlineResolver) { | 147 // If the context is not in the suspended state (i.e. running), reject the p romise. |
116 // Can't call startRendering more than once. Return a rejected promise now. | 148 if (contextState() != AudioContextState::Suspended) { |
117 return ScriptPromise::rejectWithDOMException( | 149 return ScriptPromise::rejectWithDOMException( |
118 scriptState, | 150 scriptState, |
119 DOMException::create( | 151 DOMException::create( |
152 InvalidStateError, | |
153 "cannot startRendering when an OfflineAudioContext is " + state( ))); | |
154 } | |
155 | |
156 // Can't call startRendering more than once. Return a rejected promise now. | |
157 if (m_isRenderingStarted) { | |
158 return ScriptPromise::rejectWithDOMException( | |
159 scriptState, | |
160 DOMException::create( | |
120 InvalidStateError, | 161 InvalidStateError, |
121 "cannot call startRendering more than once")); | 162 "cannot call startRendering more than once")); |
122 } | 163 } |
123 | 164 |
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 165 ASSERT(!m_isRenderingStarted); |
125 startRendering(); | 166 |
126 return m_offlineResolver->promise(); | 167 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
168 | |
169 // Start rendering and return the promise. | |
170 m_isRenderingStarted = true; | |
171 setContextState(Running); | |
172 destinationHandler().startRendering(); | |
173 | |
174 return m_completeResolver->promise(); | |
127 } | 175 } |
128 | 176 |
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 177 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
130 { | 178 { |
131 return ScriptPromise::rejectWithDOMException( | 179 return ScriptPromise::rejectWithDOMException( |
132 scriptState, | 180 scriptState, |
133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); | |
134 } | |
135 | |
136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
137 { | |
138 return ScriptPromise::rejectWithDOMException( | |
139 scriptState, | |
140 DOMException::create( | 181 DOMException::create( |
141 InvalidAccessError, | 182 InvalidAccessError, |
142 "cannot suspend an OfflineAudioContext")); | 183 "cannot close an OfflineAudioContext.")); |
184 } | |
185 | |
186 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when) | |
187 { | |
188 ASSERT(isMainThread()); | |
189 | |
190 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
191 ScriptPromise promise = resolver->promise(); | |
192 | |
193 // The render thread does not exist; reject the promise. | |
194 if (!destinationHandler().offlineRenderThread()) { | |
195 resolver->reject(DOMException::create(InvalidStateError, | |
196 "the rendering is already finished")); | |
197 return promise; | |
198 } | |
199 | |
200 // The specified suspend time is negative; reject the promise. | |
201 if (when < 0) { | |
202 resolver->reject(DOMException::create(InvalidStateError, | |
203 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
204 return promise; | |
205 } | |
206 | |
207 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
208 size_t frame = when * sampleRate(); | |
209 frame -= frame % destinationHandler().renderQuantumFrames(); | |
210 | |
211 // The suspend time should be earlier than the total render frame. If the | |
212 // requested suspension time is equal to the total render frame, the promise | |
213 // will be rejected. | |
214 if (m_totalRenderFrames <= frame) { | |
215 resolver->reject(DOMException::create(InvalidStateError, | |
216 "cannot schedule a suspend at frame " + String::number(frame) + | |
217 " (" + String::number(when) + " seconds) " + | |
218 "because it is greater than or equal to the total render duration of " + | |
219 String::number(m_totalRenderFrames) + " frames")); | |
220 return promise; | |
221 } | |
222 | |
223 // The specified suspend time is in the past; reject the promise. | |
224 if (frame < currentSampleFrame()) { | |
225 resolver->reject(DOMException::create(InvalidStateError, | |
226 "cannot schedule a suspend at frame " + | |
227 String::number(frame) + " (" + String::number(when) + | |
228 " seconds) because it is earlier than the current frame of " + | |
229 String::number(currentSampleFrame()) + " (" + | |
230 String::number(currentTime()) + " seconds)")); | |
231 return promise; | |
232 } | |
233 | |
234 // If there is a duplicate suspension at the same quantized frame, | |
235 // reject the promise. | |
236 if (m_scheduledSuspends.contains(frame)) { | |
237 resolver->reject(DOMException::create(InvalidStateError, | |
238 "cannot schedule more than one suspend at frame " + | |
239 String::number(frame) + " (" + | |
240 String::number(when) + " seconds)")); | |
241 return promise; | |
242 } | |
243 | |
244 // Wait until the suspend map is available for the insertion. | |
245 AutoLocker locker(this); | |
Raymond Toy
2015/10/21 23:14:18
AutoLocker or OfflineAutoLocker? Add a comment to
hongchan
2015/10/22 18:23:49
This gets called in the main thread. It needs to b
| |
246 | |
247 m_scheduledSuspends.add(frame, resolver); | |
248 | |
249 return promise; | |
143 } | 250 } |
144 | 251 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 252 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 253 { |
254 ASSERT(isMainThread()); | |
255 | |
256 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
257 ScriptPromise promise = resolver->promise(); | |
258 | |
259 // If the rendering has not started, reject the promise. | |
260 if (!m_isRenderingStarted) { | |
261 resolver->reject(DOMException::create(InvalidStateError, | |
262 "cannot resume an offline context that has not started")); | |
263 return promise; | |
264 } | |
265 | |
266 // If the context is not in a suspended or closed state, reject the promise. | |
267 // TODO(hongchan): there is a conflict in the spec regarding resuming a | |
Raymond Toy
2015/10/21 23:14:18
If you have the crbug number, add that too.
| |
268 // running context. Per the current spec, the implementation here rejects | |
269 // the promise for resuming a running context. | |
270 if (contextState() != AudioContextState::Suspended) { | |
271 resolver->reject(DOMException::create(InvalidStateError, | |
272 "cannot resume an offline context that is " + state())); | |
273 return promise; | |
274 } | |
275 | |
276 // If the context is suspended, resume rendering by setting the state to | |
277 // "Running." and calling startRendering(). Note that resuming is possible | |
278 // only after the rendering started. | |
279 setContextState(Running); | |
280 destinationHandler().startRendering(); | |
281 | |
282 // Resolve the promise immediately. | |
283 resolver->resolve(); | |
284 | |
285 return promise; | |
286 } | |
287 | |
288 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
289 { | |
290 // This CANNOT be called on OfflineAudioContext; this is only to implement | |
291 // the pure virtual interface from AbstractAudioContext. | |
292 RELEASE_ASSERT_NOT_REACHED(); | |
293 | |
147 return ScriptPromise::rejectWithDOMException( | 294 return ScriptPromise::rejectWithDOMException( |
148 scriptState, | 295 scriptState, |
149 DOMException::create( | 296 DOMException::create( |
150 InvalidAccessError, | 297 InvalidStateError, |
151 "cannot resume an OfflineAudioContext")); | 298 "cannot suspend offline audio context without the specified time.")) ; |
299 } | |
300 | |
301 void OfflineAudioContext::fireCompletionEvent() | |
302 { | |
303 ASSERT(isMainThread()); | |
304 | |
305 // We set the state to closed here so that the oncomplete event handler sees | |
306 // that the context has been closed. | |
307 setContextState(Closed); | |
308 | |
309 AudioBuffer* renderedBuffer = renderTarget(); | |
310 | |
311 ASSERT(renderedBuffer); | |
312 if (!renderedBuffer) | |
313 return; | |
314 | |
315 // Avoid firing the event if the document has already gone away. | |
316 if (executionContext()) { | |
317 // Call the offline rendering completion event listener and resolve the | |
318 // promise too. | |
319 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
320 m_completeResolver->resolve(renderedBuffer); | |
321 } else { | |
322 // The resolver should be rejected when the execution context is gone. | |
323 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
324 "the execution context does not exist")); | |
325 } | |
326 } | |
327 | |
328 bool OfflineAudioContext::handlePreOfflineRenderTasks() | |
329 { | |
330 ASSERT(isAudioThread()); | |
331 | |
332 // OfflineAutoLocker here locks the render thread for this scope and it is | |
333 // safe to lock the offline rendering because it does not have to be careful | |
334 // about glitches as opposed to the real-time rendering. It is also | |
335 // necessary to avoid tryLock() inside of this auto locker because the | |
336 // timing of suspension MUST NOT be delayed. | |
337 OfflineAutoLocker locker(this); | |
Raymond Toy
2015/10/21 23:14:18
I only seem to be able to see OfflineAudioLocker b
hongchan
2015/10/22 18:23:49
OfflineAutoLocker can be called from the AudioThre
| |
338 | |
339 deferredTaskHandler().handleDeferredTasks(); | |
340 handleStoppableSourceNodes(); | |
341 | |
342 return shouldSuspend(); | |
343 } | |
344 | |
345 void OfflineAudioContext::handlePostOfflineRenderTasks() | |
346 { | |
347 ASSERT(isAudioThread()); | |
348 | |
349 // OfflineAutoLocker here locks the render thread for this scope and it is | |
350 // safe to lock the offline rendering because of the same reason described | |
351 // in |handlePreOfflineRenderTasks|. | |
352 OfflineAutoLocker locker(this); | |
353 | |
354 deferredTaskHandler().breakConnections(); | |
355 releaseFinishedSourceNodes(); | |
356 deferredTaskHandler().handleDeferredTasks(); | |
357 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); | |
358 } | |
359 | |
360 | |
361 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
362 { | |
363 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler()); | |
364 } | |
365 | |
366 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) | |
367 { | |
368 ASSERT(isMainThread()); | |
369 | |
370 // Suspend the context first. This will fire onstatechange event. | |
371 setContextState(Suspended); | |
372 | |
373 ASSERT(m_scheduledSuspends.contains(frame)); | |
374 | |
375 // Wait until the suspend map is available for the removal. | |
376 AutoLocker locker(this); | |
377 | |
378 SuspendMap::iterator it = m_scheduledSuspends.find(frame); | |
379 it->value->resolve(); | |
380 | |
381 m_scheduledSuspends.remove(it); | |
382 } | |
383 | |
384 bool OfflineAudioContext::shouldSuspend() | |
385 { | |
386 ASSERT(isAudioThread()); | |
387 | |
388 if (m_scheduledSuspends.contains(currentSampleFrame())) | |
389 return true; | |
390 | |
391 return false; | |
152 } | 392 } |
153 | 393 |
154 } // namespace blink | 394 } // namespace blink |
155 | 395 |
156 #endif // ENABLE(WEB_AUDIO) | 396 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |