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.throwRangeError(ExceptionMessages::failedToConstruct( | |
109 "AudioBuffer", | |
110 "failed to create a target buffer for offline rendering. (" + | |
111 String::number(numberOfChannels) + ", " + String::number(numberOfFra mes) + | |
112 ")")); | |
Raymond Toy
2015/10/22 20:27:17
Nit: If you're going to do this, I would propose t
hongchan
2015/10/22 21:05:03
Done. I took your first solution.
| |
113 } | |
114 | |
115 if (m_renderTarget.get()) | |
116 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get()); | |
117 | |
118 initialize(); | |
97 } | 119 } |
98 | 120 |
99 OfflineAudioContext::~OfflineAudioContext() | 121 OfflineAudioContext::~OfflineAudioContext() |
100 { | 122 { |
101 } | 123 } |
102 | 124 |
125 DEFINE_TRACE(OfflineAudioContext) | |
126 { | |
127 visitor->trace(m_renderTarget); | |
128 visitor->trace(m_completeResolver); | |
129 visitor->trace(m_scheduledSuspends); | |
130 AbstractAudioContext::trace(visitor); | |
131 } | |
132 | |
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 133 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
104 { | 134 { |
135 ASSERT(isMainThread()); | |
136 | |
105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 137 // Calling close() on an OfflineAudioContext is not supported/allowed, |
106 // but it might well have been stopped by its execution context. | 138 // but it might well have been stopped by its execution context. |
139 // | |
140 // See: crbug.com/435867 | |
107 if (isContextClosed()) { | 141 if (isContextClosed()) { |
108 return ScriptPromise::rejectWithDOMException( | 142 return ScriptPromise::rejectWithDOMException( |
109 scriptState, | 143 scriptState, |
110 DOMException::create( | 144 DOMException::create( |
111 InvalidStateError, | 145 InvalidStateError, |
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 146 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
113 } | 147 } |
114 | 148 |
115 if (m_offlineResolver) { | 149 // 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. | 150 if (contextState() != AudioContextState::Suspended) { |
117 return ScriptPromise::rejectWithDOMException( | 151 return ScriptPromise::rejectWithDOMException( |
118 scriptState, | 152 scriptState, |
119 DOMException::create( | 153 DOMException::create( |
154 InvalidStateError, | |
155 "cannot startRendering when an OfflineAudioContext is " + state( ))); | |
156 } | |
157 | |
158 // Can't call startRendering more than once. Return a rejected promise now. | |
159 if (m_isRenderingStarted) { | |
160 return ScriptPromise::rejectWithDOMException( | |
161 scriptState, | |
162 DOMException::create( | |
120 InvalidStateError, | 163 InvalidStateError, |
121 "cannot call startRendering more than once")); | 164 "cannot call startRendering more than once")); |
122 } | 165 } |
123 | 166 |
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 167 ASSERT(!m_isRenderingStarted); |
125 startRendering(); | 168 |
126 return m_offlineResolver->promise(); | 169 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
170 | |
171 // Start rendering and return the promise. | |
172 m_isRenderingStarted = true; | |
173 setContextState(Running); | |
174 destinationHandler().startRendering(); | |
175 | |
176 return m_completeResolver->promise(); | |
127 } | 177 } |
128 | 178 |
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 179 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
130 { | 180 { |
131 return ScriptPromise::rejectWithDOMException( | 181 return ScriptPromise::rejectWithDOMException( |
132 scriptState, | 182 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( | 183 DOMException::create( |
141 InvalidAccessError, | 184 InvalidAccessError, |
142 "cannot suspend an OfflineAudioContext")); | 185 "cannot close an OfflineAudioContext.")); |
186 } | |
187 | |
188 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when) | |
189 { | |
190 ASSERT(isMainThread()); | |
191 | |
192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
193 ScriptPromise promise = resolver->promise(); | |
194 | |
195 // The render thread does not exist; reject the promise. | |
196 if (!destinationHandler().offlineRenderThread()) { | |
197 resolver->reject(DOMException::create(InvalidStateError, | |
198 "the rendering is already finished")); | |
199 return promise; | |
200 } | |
201 | |
202 // The specified suspend time is negative; reject the promise. | |
203 if (when < 0) { | |
204 resolver->reject(DOMException::create(InvalidStateError, | |
205 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
206 return promise; | |
207 } | |
208 | |
209 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
210 size_t frame = when * sampleRate(); | |
211 frame -= frame % destinationHandler().renderQuantumFrames(); | |
212 | |
213 // The suspend time should be earlier than the total render frame. If the | |
214 // requested suspension time is equal to the total render frame, the promise | |
215 // will be rejected. | |
216 if (m_totalRenderFrames <= frame) { | |
217 resolver->reject(DOMException::create(InvalidStateError, | |
218 "cannot schedule a suspend at frame " + String::number(frame) + | |
219 " (" + String::number(when) + " seconds) " + | |
220 "because it is greater than or equal to the total render duration of " + | |
221 String::number(m_totalRenderFrames) + " frames")); | |
222 return promise; | |
223 } | |
224 | |
225 // The specified suspend time is in the past; reject the promise. | |
226 if (frame < currentSampleFrame()) { | |
227 resolver->reject(DOMException::create(InvalidStateError, | |
228 "cannot schedule a suspend at frame " + | |
229 String::number(frame) + " (" + String::number(when) + | |
230 " seconds) because it is earlier than the current frame of " + | |
231 String::number(currentSampleFrame()) + " (" + | |
232 String::number(currentTime()) + " seconds)")); | |
233 return promise; | |
234 } | |
235 | |
236 // If there is a duplicate suspension at the same quantized frame, | |
237 // reject the promise. | |
238 if (m_scheduledSuspends.contains(frame)) { | |
239 resolver->reject(DOMException::create(InvalidStateError, | |
240 "cannot schedule more than one suspend at frame " + | |
241 String::number(frame) + " (" + | |
242 String::number(when) + " seconds)")); | |
243 return promise; | |
244 } | |
245 | |
246 // Wait until the suspend map is available for the insertion. Here we should | |
247 // use AutoLocker because it locks the graph from the main thread. | |
248 AutoLocker locker(this); | |
249 | |
250 m_scheduledSuspends.add(frame, resolver); | |
251 | |
252 return promise; | |
143 } | 253 } |
144 | 254 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 255 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 256 { |
257 ASSERT(isMainThread()); | |
258 | |
259 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
260 ScriptPromise promise = resolver->promise(); | |
261 | |
262 // If the rendering has not started, reject the promise. | |
263 if (!m_isRenderingStarted) { | |
264 resolver->reject(DOMException::create(InvalidStateError, | |
265 "cannot resume an offline context that has not started")); | |
266 return promise; | |
267 } | |
268 | |
269 // If the context is not in a suspended or closed state, reject the promise. | |
270 // | |
271 // TODO(hongchan): there is a conflict in the spec regarding resuming a | |
272 // running context. Per the current spec, the implementation here rejects | |
273 // the promise for resuming a running context. See crbug.com/545686 | |
274 if (contextState() != AudioContextState::Suspended) { | |
275 resolver->reject(DOMException::create(InvalidStateError, | |
276 "cannot resume an offline context that is " + state())); | |
277 return promise; | |
278 } | |
279 | |
280 // If the context is suspended, resume rendering by setting the state to | |
281 // "Running." and calling startRendering(). Note that resuming is possible | |
282 // only after the rendering started. | |
283 setContextState(Running); | |
284 destinationHandler().startRendering(); | |
285 | |
286 // Resolve the promise immediately. | |
287 resolver->resolve(); | |
288 | |
289 return promise; | |
290 } | |
291 | |
292 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
293 { | |
294 // This CANNOT be called on OfflineAudioContext; this is only to implement | |
295 // the pure virtual interface from AbstractAudioContext. | |
296 RELEASE_ASSERT_NOT_REACHED(); | |
297 | |
147 return ScriptPromise::rejectWithDOMException( | 298 return ScriptPromise::rejectWithDOMException( |
148 scriptState, | 299 scriptState, |
149 DOMException::create( | 300 DOMException::create( |
150 InvalidAccessError, | 301 InvalidStateError, |
151 "cannot resume an OfflineAudioContext")); | 302 "cannot suspend offline audio context without the specified time.")) ; |
303 } | |
304 | |
305 void OfflineAudioContext::fireCompletionEvent() | |
306 { | |
307 ASSERT(isMainThread()); | |
308 | |
309 // We set the state to closed here so that the oncomplete event handler sees | |
310 // that the context has been closed. | |
311 setContextState(Closed); | |
312 | |
313 AudioBuffer* renderedBuffer = renderTarget(); | |
314 | |
315 ASSERT(renderedBuffer); | |
316 if (!renderedBuffer) | |
317 return; | |
318 | |
319 // Avoid firing the event if the document has already gone away. | |
320 if (executionContext()) { | |
321 // Call the offline rendering completion event listener and resolve the | |
322 // promise too. | |
323 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
324 m_completeResolver->resolve(renderedBuffer); | |
325 } else { | |
326 // The resolver should be rejected when the execution context is gone. | |
327 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
328 "the execution context does not exist")); | |
329 } | |
330 } | |
331 | |
332 bool OfflineAudioContext::handlePreOfflineRenderTasks() | |
333 { | |
334 ASSERT(isAudioThread()); | |
335 | |
336 // OfflineGraphAutoLocker here locks the audio graph for this scope. Note | |
337 // that this locker does not use tryLock() inside because the timing of | |
338 // suspension MUST NOT be delayed. | |
339 OfflineGraphAutoLocker locker(this); | |
340 | |
341 deferredTaskHandler().handleDeferredTasks(); | |
342 handleStoppableSourceNodes(); | |
343 | |
344 return shouldSuspend(); | |
345 } | |
346 | |
347 void OfflineAudioContext::handlePostOfflineRenderTasks() | |
348 { | |
349 ASSERT(isAudioThread()); | |
350 | |
351 // OfflineGraphAutoLocker here locks the audio graph for the same reason | |
352 // above in |handlePreOfflineRenderTasks|. | |
353 OfflineGraphAutoLocker locker(this); | |
354 | |
355 deferredTaskHandler().breakConnections(); | |
356 releaseFinishedSourceNodes(); | |
357 deferredTaskHandler().handleDeferredTasks(); | |
358 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); | |
359 } | |
360 | |
361 | |
362 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
363 { | |
364 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler()); | |
365 } | |
366 | |
367 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) | |
368 { | |
369 ASSERT(isMainThread()); | |
370 | |
371 // Suspend the context first. This will fire onstatechange event. | |
372 setContextState(Suspended); | |
373 | |
374 ASSERT(m_scheduledSuspends.contains(frame)); | |
375 | |
376 // Wait until the suspend map is available for the removal. | |
377 AutoLocker locker(this); | |
378 | |
379 SuspendMap::iterator it = m_scheduledSuspends.find(frame); | |
380 it->value->resolve(); | |
381 | |
382 m_scheduledSuspends.remove(it); | |
383 } | |
384 | |
385 bool OfflineAudioContext::shouldSuspend() | |
386 { | |
387 ASSERT(isAudioThread()); | |
388 | |
389 if (m_scheduledSuspends.contains(currentSampleFrame())) | |
390 return true; | |
391 | |
392 return false; | |
152 } | 393 } |
153 | 394 |
154 } // namespace blink | 395 } // namespace blink |
155 | 396 |
156 #endif // ENABLE(WEB_AUDIO) | 397 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |