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 "OfflineAudioContext", | |
110 "failed to create OfflineAudioContext(" + | |
111 String::number(numberOfChannels) + ", " + | |
112 String::number(numberOfFrames) + ", " + | |
113 String::number(sampleRate) + ")")); | |
114 } | |
115 | |
116 if (m_renderTarget.get()) | |
117 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get()); | |
haraken
2015/11/17 08:28:10
if (renderTarget) {
m_destinationNode = ...
} el
hongchan
2015/11/17 18:11:44
Done.
| |
118 | |
119 initialize(); | |
97 } | 120 } |
98 | 121 |
99 OfflineAudioContext::~OfflineAudioContext() | 122 OfflineAudioContext::~OfflineAudioContext() |
100 { | 123 { |
101 } | 124 } |
102 | 125 |
126 DEFINE_TRACE(OfflineAudioContext) | |
127 { | |
128 visitor->trace(m_renderTarget); | |
129 visitor->trace(m_completeResolver); | |
130 visitor->trace(m_scheduledSuspends); | |
131 AbstractAudioContext::trace(visitor); | |
132 } | |
133 | |
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 134 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
104 { | 135 { |
136 ASSERT(isMainThread()); | |
137 | |
105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 138 // Calling close() on an OfflineAudioContext is not supported/allowed, |
106 // but it might well have been stopped by its execution context. | 139 // but it might well have been stopped by its execution context. |
140 // | |
141 // See: crbug.com/435867 | |
107 if (isContextClosed()) { | 142 if (isContextClosed()) { |
108 return ScriptPromise::rejectWithDOMException( | 143 return ScriptPromise::rejectWithDOMException( |
109 scriptState, | 144 scriptState, |
110 DOMException::create( | 145 DOMException::create( |
111 InvalidStateError, | 146 InvalidStateError, |
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 147 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
113 } | 148 } |
114 | 149 |
115 if (m_offlineResolver) { | 150 // 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. | 151 if (contextState() != AudioContextState::Suspended) { |
117 return ScriptPromise::rejectWithDOMException( | 152 return ScriptPromise::rejectWithDOMException( |
118 scriptState, | 153 scriptState, |
119 DOMException::create( | 154 DOMException::create( |
155 InvalidStateError, | |
156 "cannot startRendering when an OfflineAudioContext is " + state( ))); | |
157 } | |
158 | |
159 // Can't call startRendering more than once. Return a rejected promise now. | |
160 if (m_isRenderingStarted) { | |
161 return ScriptPromise::rejectWithDOMException( | |
162 scriptState, | |
163 DOMException::create( | |
120 InvalidStateError, | 164 InvalidStateError, |
121 "cannot call startRendering more than once")); | 165 "cannot call startRendering more than once")); |
122 } | 166 } |
123 | 167 |
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 168 ASSERT(!m_isRenderingStarted); |
125 startRendering(); | 169 |
126 return m_offlineResolver->promise(); | 170 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
171 | |
172 // Start rendering and return the promise. | |
173 m_isRenderingStarted = true; | |
174 setContextState(Running); | |
175 destinationHandler().startRendering(); | |
176 | |
177 return m_completeResolver->promise(); | |
127 } | 178 } |
128 | 179 |
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 180 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
130 { | 181 { |
131 return ScriptPromise::rejectWithDOMException( | 182 return ScriptPromise::rejectWithDOMException( |
132 scriptState, | 183 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( | 184 DOMException::create( |
141 InvalidAccessError, | 185 InvalidAccessError, |
142 "cannot suspend an OfflineAudioContext")); | 186 "cannot close an OfflineAudioContext.")); |
187 } | |
188 | |
189 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
190 { | |
191 // This CANNOT be called on OfflineAudioContext; this is only to implement | |
192 // the pure virtual interface from AbstractAudioContext. | |
193 RELEASE_ASSERT_NOT_REACHED(); | |
194 | |
195 return ScriptPromise(); | |
196 } | |
197 | |
198 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when) | |
199 { | |
200 ASSERT(isMainThread()); | |
201 | |
202 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
203 ScriptPromise promise = resolver->promise(); | |
204 | |
205 // The render thread does not exist; reject the promise. | |
206 if (!destinationHandler().offlineRenderThread()) { | |
207 resolver->reject(DOMException::create(InvalidStateError, | |
208 "the rendering is already finished")); | |
209 return promise; | |
210 } | |
211 | |
212 // The specified suspend time is negative; reject the promise. | |
213 if (when < 0) { | |
214 resolver->reject(DOMException::create(InvalidStateError, | |
215 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
216 return promise; | |
217 } | |
218 | |
219 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
220 size_t frame = when * sampleRate(); | |
221 frame -= frame % destinationHandler().renderQuantumFrames(); | |
222 | |
223 // The suspend time should be earlier than the total render frame. If the | |
224 // requested suspension time is equal to the total render frame, the promise | |
225 // will be rejected. | |
226 if (m_totalRenderFrames <= frame) { | |
227 resolver->reject(DOMException::create(InvalidStateError, | |
228 "cannot schedule a suspend at frame " + String::number(frame) + | |
229 " (" + String::number(when) + " seconds) " + | |
230 "because it is greater than or equal to the total render duration of " + | |
231 String::number(m_totalRenderFrames) + " frames")); | |
232 return promise; | |
233 } | |
234 | |
235 // The specified suspend time is in the past; reject the promise. | |
236 if (frame < currentSampleFrame()) { | |
237 resolver->reject(DOMException::create(InvalidStateError, | |
238 "cannot schedule a suspend at frame " + | |
239 String::number(frame) + " (" + String::number(when) + | |
240 " seconds) because it is earlier than the current frame of " + | |
241 String::number(currentSampleFrame()) + " (" + | |
242 String::number(currentTime()) + " seconds)")); | |
243 return promise; | |
244 } | |
245 | |
246 // If there is a duplicate suspension at the same quantized frame, | |
247 // reject the promise. | |
248 if (m_scheduledSuspends.contains(frame)) { | |
249 resolver->reject(DOMException::create(InvalidStateError, | |
250 "cannot schedule more than one suspend at frame " + | |
251 String::number(frame) + " (" + | |
252 String::number(when) + " seconds)")); | |
253 return promise; | |
254 } | |
255 | |
256 // Wait until the suspend map is available for the insertion. Here we should | |
257 // use AutoLocker because it locks the graph from the main thread. | |
258 AutoLocker locker(this); | |
haraken
2015/11/17 08:28:10
Don't you need to acquire the lock before looking
hongchan
2015/11/17 18:11:44
Yes, you are correct. Done.
| |
259 | |
260 m_scheduledSuspends.add(frame, resolver); | |
261 | |
262 return promise; | |
143 } | 263 } |
144 | 264 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 265 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 266 { |
147 return ScriptPromise::rejectWithDOMException( | 267 ASSERT(isMainThread()); |
148 scriptState, | 268 |
149 DOMException::create( | 269 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
150 InvalidAccessError, | 270 ScriptPromise promise = resolver->promise(); |
151 "cannot resume an OfflineAudioContext")); | 271 |
272 // If the rendering has not started, reject the promise. | |
273 if (!m_isRenderingStarted) { | |
274 resolver->reject(DOMException::create(InvalidStateError, | |
275 "cannot resume an offline context that has not started")); | |
276 return promise; | |
277 } | |
278 | |
279 // If the context is in a closed state, reject the promise. | |
280 if (contextState() == AudioContextState::Closed) { | |
281 resolver->reject(DOMException::create(InvalidStateError, | |
282 "cannot resume a closed offline context")); | |
283 return promise; | |
284 } | |
285 | |
286 // If the context is already running, resolve the promise without altering | |
287 // the current state or starting the rendering loop. | |
288 if (contextState() == AudioContextState::Running) { | |
289 resolver->resolve(); | |
290 return promise; | |
291 } | |
292 | |
haraken
2015/11/17 08:28:10
Can we add ASSERT(contextState() == AudioContextSt
hongchan
2015/11/17 18:11:44
Done.
| |
293 // If the context is suspended, resume rendering by setting the state to | |
294 // "Running." and calling startRendering(). Note that resuming is possible | |
haraken
2015/11/17 08:28:10
"Running".
hongchan
2015/11/17 18:11:44
Done.
| |
295 // only after the rendering started. | |
296 setContextState(Running); | |
297 destinationHandler().startRendering(); | |
298 | |
299 // Resolve the promise immediately. | |
300 resolver->resolve(); | |
301 | |
302 return promise; | |
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); | |
haraken
2015/11/17 08:28:10
I guess we need to acquire the lock before calling
hongchan
2015/11/17 18:11:44
Done.
| |
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())) | |
haraken
2015/11/17 08:28:10
Don't we need to acquire OfflineGraphAutoLocker lo
hongchan
2015/11/17 18:11:44
This method only gets called when the lock is in p
| |
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 |