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" | |
37 #include "platform/Task.h" | |
38 #include "platform/ThreadSafeFunctional.h" | |
39 #include "platform/audio/AudioUtilities.h" | 35 #include "platform/audio/AudioUtilities.h" |
40 #include "public/platform/Platform.h" | |
41 | |
42 | 36 |
43 namespace blink { | 37 namespace blink { |
44 | 38 |
45 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi
gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&
exceptionState) | 39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi
gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&
exceptionState) |
46 { | 40 { |
47 // FIXME: add support for workers. | 41 // FIXME: add support for workers. |
48 if (!context || !context->isDocument()) { | 42 if (!context || !context->isDocument()) { |
49 exceptionState.throwDOMException( | 43 exceptionState.throwDOMException( |
50 NotSupportedError, | 44 NotSupportedError, |
51 "Workers are not supported."); | 45 "Workers are not supported."); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 + ", " + String::number(sampleRate) | 86 + ", " + String::number(sampleRate) |
93 + ")"); | 87 + ")"); |
94 } | 88 } |
95 | 89 |
96 audioContext->suspendIfNeeded(); | 90 audioContext->suspendIfNeeded(); |
97 return audioContext; | 91 return audioContext; |
98 } | 92 } |
99 | 93 |
100 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh
annels, size_t numberOfFrames, float sampleRate) | 94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh
annels, size_t numberOfFrames, float sampleRate) |
101 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat
e) | 95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat
e) |
102 , m_isRenderingStarted(false) | |
103 , m_totalRenderFrames(numberOfFrames) | |
104 { | 96 { |
105 } | 97 } |
106 | 98 |
107 OfflineAudioContext::~OfflineAudioContext() | 99 OfflineAudioContext::~OfflineAudioContext() |
108 { | 100 { |
109 } | 101 } |
110 | 102 |
111 DEFINE_TRACE(OfflineAudioContext) | |
112 { | |
113 visitor->trace(m_completeResolver); | |
114 AbstractAudioContext::trace(visitor); | |
115 } | |
116 | |
117 bool OfflineAudioContext::shouldSuspendNow() | |
118 { | |
119 ASSERT(!isMainThread()); | |
120 | |
121 // If there is any scheduled suspend at |currentSampleFrame|, the context | |
122 // should be suspended. | |
123 if (!m_scheduledSuspends.contains(currentSampleFrame())) | |
124 return false; | |
125 | |
126 return true; | |
127 } | |
128 | |
129 void OfflineAudioContext::resolvePendingSuspendPromises() | |
130 { | |
131 ASSERT(!isMainThread()); | |
132 | |
133 // Find a suspend scheduled at |currentSampleFrame| and resolve the | |
134 // associated promise on the main thread. | |
135 SuspendContainerMap::iterator it = m_scheduledSuspends.find(currentSampleFra
me()); | |
136 if (it == m_scheduledSuspends.end()) | |
137 return; | |
138 | |
139 ScheduledSuspendContainer* suspendContainer = it->value; | |
140 m_scheduledSuspends.remove(it); | |
141 | |
142 // Passing a raw pointer |suspendContainer| is safe here because it is | |
143 // created/managed/destructed on the main thread. Also it is guaranteed to | |
144 // be alive until the task is finished somewhere else. | |
145 Platform::current()->mainThread()->postTask(FROM_HERE, | |
146 threadSafeBind(&OfflineAudioContext::resolveSuspendContainerOnMainThread
, this, AllowCrossThreadAccess(suspendContainer))); | |
147 } | |
148 | |
149 void OfflineAudioContext::fireCompletionEvent() | |
150 { | |
151 ASSERT(isMainThread()); | |
152 | |
153 // We set the state to closed here so that the oncomplete event handler sees | |
154 // that the context has been closed. | |
155 setContextState(Closed); | |
156 | |
157 AudioBuffer* renderedBuffer = renderTarget(); | |
158 | |
159 ASSERT(renderedBuffer); | |
160 if (!renderedBuffer) | |
161 return; | |
162 | |
163 // Avoid firing the event if the document has already gone away. | |
164 if (executionContext()) { | |
165 // Call the offline rendering completion event listener and resolve the | |
166 // promise too. | |
167 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
168 m_completeResolver->resolve(renderedBuffer); | |
169 } else { | |
170 // The resolver should be rejected when the execution context is gone. | |
171 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
172 "the execution context does not exist")); | |
173 } | |
174 } | |
175 | |
176 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat
e) | 103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat
e) |
177 { | 104 { |
178 ASSERT(isMainThread()); | |
179 | |
180 // Calling close() on an OfflineAudioContext is not supported/allowed, | 105 // Calling close() on an OfflineAudioContext is not supported/allowed, |
181 // but it might well have been closed by its execution context. | 106 // but it might well have been stopped by its execution context. |
182 if (isContextClosed()) { | 107 if (isContextClosed()) { |
183 return ScriptPromise::rejectWithDOMException( | 108 return ScriptPromise::rejectWithDOMException( |
184 scriptState, | 109 scriptState, |
185 DOMException::create( | 110 DOMException::create( |
186 InvalidStateError, | 111 InvalidStateError, |
187 "cannot call startRendering on an OfflineAudioContext in a close
d state.")); | 112 "cannot call startRendering on an OfflineAudioContext in a stopp
ed state.")); |
188 } | 113 } |
189 | 114 |
190 if (m_completeResolver) { | 115 if (m_offlineResolver) { |
191 // Can't call startRendering more than once. Return a rejected promise
now. | 116 // Can't call startRendering more than once. Return a rejected promise
now. |
192 return ScriptPromise::rejectWithDOMException( | 117 return ScriptPromise::rejectWithDOMException( |
193 scriptState, | 118 scriptState, |
194 DOMException::create( | 119 DOMException::create( |
195 InvalidStateError, | 120 InvalidStateError, |
196 "cannot call startRendering more than once")); | 121 "cannot call startRendering more than once")); |
197 } | 122 } |
198 | 123 |
199 // If the context is not in the suspended state, reject the promise. | 124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); |
200 if (contextState() != AudioContextState::Suspended) { | 125 startRendering(); |
201 return ScriptPromise::rejectWithDOMException( | 126 return m_offlineResolver->promise(); |
202 scriptState, | |
203 DOMException::create( | |
204 InvalidStateError, | |
205 "cannot startRendering when an OfflineAudioContext is not in a s
uspended state")); | |
206 } | |
207 | |
208 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
209 | |
210 // Start rendering and return the promise. | |
211 ASSERT(!m_isRenderingStarted); | |
212 m_isRenderingStarted = true; | |
213 setContextState(Running); | |
214 destinationHandler().startRendering(); | |
215 | |
216 return m_completeResolver->promise(); | |
217 } | 127 } |
218 | 128 |
219 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
220 { | 130 { |
221 return ScriptPromise::rejectWithDOMException( | 131 return ScriptPromise::rejectWithDOMException( |
222 scriptState, | 132 scriptState, |
223 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo
ntext.")); | 133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo
ntext.")); |
224 } | 134 } |
225 | 135 |
226 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | 136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) |
227 { | 137 { |
228 // This CANNOT be called on OfflineAudioContext; it is to implement the pure | 138 return ScriptPromise::rejectWithDOMException( |
229 // virtual interface from AbstractAudioContext. | 139 scriptState, |
230 RELEASE_ASSERT_NOT_REACHED(); | 140 DOMException::create( |
231 | 141 InvalidAccessError, |
232 return ScriptPromise(); | 142 "cannot suspend an OfflineAudioContext")); |
233 } | |
234 | |
235 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub
le when) | |
236 { | |
237 ASSERT(isMainThread()); | |
238 | |
239 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | |
240 ScriptPromise promise = resolver->promise(); | |
241 | |
242 // The render thread does not exist; reject the promise. | |
243 if (!destinationHandler().offlineRenderThread()) { | |
244 resolver->reject(DOMException::create(InvalidStateError, | |
245 "the rendering is already finished")); | |
246 return promise; | |
247 } | |
248 | |
249 // The specified suspend time is negative; reject the promise. | |
250 if (when < 0) { | |
251 resolver->reject(DOMException::create(InvalidStateError, | |
252 "negative suspend time (" + String::number(when) + ") is not allowed
")); | |
253 return promise; | |
254 } | |
255 | |
256 // Quantize the suspend time to the render quantum boundary. | |
257 size_t quantizedFrame = destinationHandler().quantizeTimeToRenderQuantum(whe
n); | |
258 | |
259 // The suspend time should be earlier than the total render frame. If the | |
260 // requested suspension time is equal to the total render frame, the promise | |
261 // will be rejected. | |
262 if (m_totalRenderFrames <= quantizedFrame) { | |
263 resolver->reject(DOMException::create(InvalidStateError, | |
264 "cannot schedule a suspend at frame " + String::number(quantizedFram
e) + | |
265 " (" + String::number(when) + " seconds) " + | |
266 "because it is greater than or equal to the total render duration of
" + | |
267 String::number(m_totalRenderFrames) + " frames")); | |
268 return promise; | |
269 } | |
270 | |
271 ScheduledSuspendContainer* suspendContainer = ScheduledSuspendContainer::cre
ate(when, quantizedFrame, resolver); | |
272 | |
273 // Validate the suspend and append if necessary on the render thread. | |
274 // Note that passing a raw pointer |suspendContainer| is safe here as well | |
275 // with the same reason in |resolvePendingSuspendPromises|. | |
276 destinationHandler().offlineRenderThread()->postTask(FROM_HERE, | |
277 threadSafeBind(&OfflineAudioContext::validateSuspendContainerOnRenderThr
ead, this, AllowCrossThreadAccess(suspendContainer))); | |
278 | |
279 return promise; | |
280 } | 143 } |
281 | 144 |
282 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
283 { | 146 { |
284 ASSERT(isMainThread()); | 147 return ScriptPromise::rejectWithDOMException( |
285 | 148 scriptState, |
286 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 149 DOMException::create( |
287 ScriptPromise promise = resolver->promise(); | 150 InvalidAccessError, |
288 | 151 "cannot resume an OfflineAudioContext")); |
289 // If the context is not in a suspended state, reject the promise. | |
290 if (contextState() != AudioContextState::Suspended) { | |
291 resolver->reject(DOMException::create(InvalidStateError, | |
292 "cannot resume a context that is not suspended")); | |
293 return promise; | |
294 } | |
295 | |
296 // If the rendering has not started, reject the promise. | |
297 if (!m_isRenderingStarted) { | |
298 resolver->reject(DOMException::create(InvalidStateError, | |
299 "cannot resume a context that has not started")); | |
300 return promise; | |
301 } | |
302 | |
303 // If the context is suspended, resume rendering by setting the state to | |
304 // "Running." and calling startRendering(). Note that resuming is possible | |
305 // only after the rendering started. | |
306 setContextState(Running); | |
307 destinationHandler().startRendering(); | |
308 | |
309 // Resolve the promise immediately. | |
310 resolver->resolve(); | |
311 | |
312 return promise; | |
313 } | |
314 | |
315 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
316 { | |
317 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest
inationHandler()); | |
318 } | |
319 | |
320 void OfflineAudioContext::validateSuspendContainerOnRenderThread(ScheduledSuspen
dContainer* suspendContainer) | |
321 { | |
322 ASSERT(!isMainThread()); | |
323 | |
324 bool shouldBeRejected = false; | |
325 | |
326 // The specified suspend time is in the past; reject the promise. We cannot | |
327 // reject the promise in here, so set the error message before posting the | |
328 // rejection task to the main thread. | |
329 if (suspendContainer->suspendFrame() < currentSampleFrame()) { | |
330 shouldBeRejected = true; | |
331 suspendContainer->setErrorMessageForRejection(InvalidStateError, | |
332 "cannot schedule a suspend at frame " + | |
333 String::number(suspendContainer->suspendFrame()) + " (" + | |
334 String::number(suspendContainer->suspendTime()) + | |
335 " seconds) because it is earlier than the current frame of " + | |
336 String::number(currentSampleFrame()) + " (" + | |
337 String::number(currentTime()) + " seconds)"); | |
338 } else if (m_scheduledSuspends.contains(suspendContainer->suspendFrame())) { | |
339 // If there is a duplicate suspension at the same quantized frame, | |
340 // reject the promise. The rejection of promise should happen in the | |
341 // main thread, so we post a task to it. Here we simply set the error | |
342 // message and post a task to the main thread. | |
343 shouldBeRejected = true; | |
344 suspendContainer->setErrorMessageForRejection(InvalidStateError, | |
345 "cannot schedule more than one suspend at frame " + | |
346 String::number(suspendContainer->suspendFrame()) + " (" + | |
347 String::number(suspendContainer->suspendTime()) + " seconds)"); | |
348 } | |
349 | |
350 // Reject the promise if necessary on the main thread. | |
351 // Note that passing a raw pointer |suspendContainer| is safe here as well | |
352 // with the same reason in |resolvePendingSuspendPromises|. | |
353 if (shouldBeRejected) { | |
354 Platform::current()->mainThread()->postTask(FROM_HERE, | |
355 threadSafeBind(&OfflineAudioContext::rejectSuspendContainerOnMainThr
ead, this, AllowCrossThreadAccess(suspendContainer))); | |
356 return; | |
357 } | |
358 | |
359 // If the validation check passes, we can add the container safely here in | |
360 // the render thread. | |
361 m_scheduledSuspends.add(suspendContainer->suspendFrame(), suspendContainer); | |
362 } | |
363 | |
364 void OfflineAudioContext::rejectSuspendContainerOnMainThread(ScheduledSuspendCon
tainer* suspendContainer) | |
365 { | |
366 ASSERT(isMainThread()); | |
367 | |
368 suspendContainer->rejectPromise(); | |
369 } | |
370 | |
371 void OfflineAudioContext::resolveSuspendContainerOnMainThread(ScheduledSuspendCo
ntainer* suspendContainer) | |
372 { | |
373 ASSERT(isMainThread()); | |
374 | |
375 // Suspend the context first. This will fire onstatechange event. | |
376 setContextState(Suspended); | |
377 | |
378 suspendContainer->resolvePromise(); | |
379 } | |
380 | |
381 ScheduledSuspendContainer::ScheduledSuspendContainer(double suspendTime, size_t
suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
382 : m_suspendTime(suspendTime) | |
383 , m_suspendFrame(suspendFrame) | |
384 , m_resolver(resolver) | |
385 { | |
386 ASSERT(isMainThread()); | |
387 } | |
388 | |
389 ScheduledSuspendContainer::~ScheduledSuspendContainer() | |
390 { | |
391 ASSERT(isMainThread()); | |
392 } | |
393 | |
394 ScheduledSuspendContainer* ScheduledSuspendContainer::create(double suspendTime,
size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
395 { | |
396 return new ScheduledSuspendContainer(suspendTime, suspendFrame, resolver); | |
397 } | |
398 | |
399 void ScheduledSuspendContainer::setErrorMessageForRejection(ExceptionCode errorC
ode, const String& errorMessage) | |
400 { | |
401 ASSERT(!isMainThread()); | |
402 | |
403 m_errorCode = errorCode; | |
404 | |
405 // |errorMessage| is created on the render thread, but its actual usage is | |
406 // in the main thread. So we need to be thread-safe on this. | |
407 m_errorMessage = errorMessage.isolatedCopy(); | |
408 } | |
409 | |
410 void ScheduledSuspendContainer::resolvePromise() | |
411 { | |
412 ASSERT(isMainThread()); | |
413 | |
414 m_resolver->resolve(); | |
415 } | |
416 | |
417 void ScheduledSuspendContainer::rejectPromise() | |
418 { | |
419 ASSERT(isMainThread()); | |
420 ASSERT(m_errorCode); | |
421 ASSERT(m_errorMessage); | |
422 | |
423 m_resolver->reject(DOMException::create(m_errorCode, m_errorMessage)); | |
424 } | 152 } |
425 | 153 |
426 } // namespace blink | 154 } // namespace blink |
427 | 155 |
428 #endif // ENABLE(WEB_AUDIO) | 156 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |