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()); | |
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, doub le when) | |
190 { | |
191 ASSERT(isMainThread()); | |
192 | |
193 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
194 ScriptPromise promise = resolver->promise(); | |
195 | |
196 // The render thread does not exist; reject the promise. | |
197 if (!destinationHandler().offlineRenderThread()) { | |
198 resolver->reject(DOMException::create(InvalidStateError, | |
199 "the rendering is already finished")); | |
200 return promise; | |
201 } | |
202 | |
203 // The specified suspend time is negative; reject the promise. | |
204 if (when < 0) { | |
205 resolver->reject(DOMException::create(InvalidStateError, | |
206 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
207 return promise; | |
208 } | |
209 | |
210 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
211 size_t frame = when * sampleRate(); | |
212 frame -= frame % destinationHandler().renderQuantumFrames(); | |
213 | |
214 // The suspend time should be earlier than the total render frame. If the | |
215 // requested suspension time is equal to the total render frame, the promise | |
216 // will be rejected. | |
217 if (m_totalRenderFrames <= frame) { | |
218 resolver->reject(DOMException::create(InvalidStateError, | |
219 "cannot schedule a suspend at frame " + String::number(frame) + | |
220 " (" + String::number(when) + " seconds) " + | |
221 "because it is greater than or equal to the total render duration of " + | |
222 String::number(m_totalRenderFrames) + " frames")); | |
223 return promise; | |
224 } | |
225 | |
226 // The specified suspend time is in the past; reject the promise. | |
227 if (frame < currentSampleFrame()) { | |
228 resolver->reject(DOMException::create(InvalidStateError, | |
229 "cannot schedule a suspend at frame " + | |
230 String::number(frame) + " (" + String::number(when) + | |
231 " seconds) because it is earlier than the current frame of " + | |
232 String::number(currentSampleFrame()) + " (" + | |
233 String::number(currentTime()) + " seconds)")); | |
234 return promise; | |
235 } | |
236 | |
237 // If there is a duplicate suspension at the same quantized frame, | |
238 // reject the promise. | |
239 if (m_scheduledSuspends.contains(frame)) { | |
240 resolver->reject(DOMException::create(InvalidStateError, | |
241 "cannot schedule more than one suspend at frame " + | |
242 String::number(frame) + " (" + | |
243 String::number(when) + " seconds)")); | |
244 return promise; | |
245 } | |
246 | |
247 // Wait until the suspend map is available for the insertion. Here we should | |
248 // use AutoLocker because it locks the graph from the main thread. | |
249 AutoLocker locker(this); | |
250 | |
251 m_scheduledSuspends.add(frame, resolver); | |
252 | |
253 return promise; | |
143 } | 254 } |
144 | 255 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 256 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 257 { |
258 ASSERT(isMainThread()); | |
259 | |
260 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
261 ScriptPromise promise = resolver->promise(); | |
262 | |
263 // If the rendering has not started, reject the promise. | |
264 if (!m_isRenderingStarted) { | |
265 resolver->reject(DOMException::create(InvalidStateError, | |
266 "cannot resume an offline context that has not started")); | |
267 return promise; | |
268 } | |
269 | |
270 // If the context is in a closed state, reject the promise. | |
271 if (contextState() == AudioContextState::Closed) { | |
272 resolver->reject(DOMException::create(InvalidStateError, | |
273 "cannot resume a closed offline context")); | |
274 return promise; | |
275 } | |
276 | |
277 // If the context is already running, resolve the promise without altering | |
278 // the current state or starting the rendering loop. | |
279 if (contextState() == AudioContextState::Running) { | |
280 resolver->resolve(); | |
281 return promise; | |
282 } | |
283 | |
284 // If the context is suspended, resume rendering by setting the state to | |
285 // "Running." and calling startRendering(). Note that resuming is possible | |
286 // only after the rendering started. | |
287 setContextState(Running); | |
288 destinationHandler().startRendering(); | |
289 | |
290 // Resolve the promise immediately. | |
291 resolver->resolve(); | |
292 | |
293 return promise; | |
294 } | |
295 | |
296 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
297 { | |
298 // This CANNOT be called on OfflineAudioContext; this is only to implement | |
299 // the pure virtual interface from AbstractAudioContext. | |
300 RELEASE_ASSERT_NOT_REACHED(); | |
301 | |
yhirano
2015/11/06 08:13:29
RELEASE_ASSERT always takes effect, so L139-L142 d
hongchan
2015/11/06 19:33:40
Done.
| |
147 return ScriptPromise::rejectWithDOMException( | 302 return ScriptPromise::rejectWithDOMException( |
148 scriptState, | 303 scriptState, |
149 DOMException::create( | 304 DOMException::create( |
150 InvalidAccessError, | 305 InvalidStateError, |
151 "cannot resume an OfflineAudioContext")); | 306 "cannot suspend offline audio context without the specified time.")) ; |
307 } | |
308 | |
309 void OfflineAudioContext::fireCompletionEvent() | |
310 { | |
311 ASSERT(isMainThread()); | |
312 | |
313 // We set the state to closed here so that the oncomplete event handler sees | |
314 // that the context has been closed. | |
315 setContextState(Closed); | |
316 | |
317 AudioBuffer* renderedBuffer = renderTarget(); | |
318 | |
319 ASSERT(renderedBuffer); | |
320 if (!renderedBuffer) | |
321 return; | |
322 | |
323 // Avoid firing the event if the document has already gone away. | |
324 if (executionContext()) { | |
325 // Call the offline rendering completion event listener and resolve the | |
326 // promise too. | |
327 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
328 m_completeResolver->resolve(renderedBuffer); | |
329 } else { | |
330 // The resolver should be rejected when the execution context is gone. | |
331 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
332 "the execution context does not exist")); | |
333 } | |
334 } | |
335 | |
336 bool OfflineAudioContext::handlePreOfflineRenderTasks() | |
337 { | |
338 ASSERT(isAudioThread()); | |
339 | |
340 // OfflineGraphAutoLocker here locks the audio graph for this scope. Note | |
341 // that this locker does not use tryLock() inside because the timing of | |
342 // suspension MUST NOT be delayed. | |
343 OfflineGraphAutoLocker locker(this); | |
344 | |
345 deferredTaskHandler().handleDeferredTasks(); | |
346 handleStoppableSourceNodes(); | |
347 | |
348 return shouldSuspend(); | |
349 } | |
350 | |
351 void OfflineAudioContext::handlePostOfflineRenderTasks() | |
352 { | |
353 ASSERT(isAudioThread()); | |
354 | |
355 // OfflineGraphAutoLocker here locks the audio graph for the same reason | |
356 // above in |handlePreOfflineRenderTasks|. | |
357 OfflineGraphAutoLocker locker(this); | |
358 | |
359 deferredTaskHandler().breakConnections(); | |
360 releaseFinishedSourceNodes(); | |
361 deferredTaskHandler().handleDeferredTasks(); | |
362 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); | |
363 } | |
364 | |
365 | |
366 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
367 { | |
368 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler()); | |
369 } | |
370 | |
371 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) | |
372 { | |
373 ASSERT(isMainThread()); | |
374 | |
375 // Suspend the context first. This will fire onstatechange event. | |
376 setContextState(Suspended); | |
377 | |
378 ASSERT(m_scheduledSuspends.contains(frame)); | |
379 | |
380 // Wait until the suspend map is available for the removal. | |
381 AutoLocker locker(this); | |
382 | |
383 SuspendMap::iterator it = m_scheduledSuspends.find(frame); | |
384 it->value->resolve(); | |
385 | |
386 m_scheduledSuspends.remove(it); | |
387 } | |
388 | |
389 bool OfflineAudioContext::shouldSuspend() | |
390 { | |
391 ASSERT(isAudioThread()); | |
392 | |
393 if (m_scheduledSuspends.contains(currentSampleFrame())) | |
394 return true; | |
395 | |
396 return false; | |
152 } | 397 } |
153 | 398 |
154 } // namespace blink | 399 } // namespace blink |
155 | 400 |
156 #endif // ENABLE(WEB_AUDIO) | 401 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |