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" | |
35 #include "platform/audio/AudioUtilities.h" | 39 #include "platform/audio/AudioUtilities.h" |
40 #include "public/platform/Platform.h" | |
41 | |
36 | 42 |
37 namespace blink { | 43 namespace blink { |
38 | 44 |
39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 45 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
40 { | 46 { |
41 // FIXME: add support for workers. | 47 // FIXME: add support for workers. |
42 if (!context || !context->isDocument()) { | 48 if (!context || !context->isDocument()) { |
43 exceptionState.throwDOMException( | 49 exceptionState.throwDOMException( |
44 NotSupportedError, | 50 NotSupportedError, |
45 "Workers are not supported."); | 51 "Workers are not supported."); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 + ", " + String::number(sampleRate) | 92 + ", " + String::number(sampleRate) |
87 + ")"); | 93 + ")"); |
88 } | 94 } |
89 | 95 |
90 audioContext->suspendIfNeeded(); | 96 audioContext->suspendIfNeeded(); |
91 return audioContext; | 97 return audioContext; |
92 } | 98 } |
93 | 99 |
94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 100 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) | 101 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) |
102 , m_isRenderingStarted(false) | |
103 , m_totalRenderFrames(numberOfFrames) | |
96 { | 104 { |
97 } | 105 } |
98 | 106 |
99 OfflineAudioContext::~OfflineAudioContext() | 107 OfflineAudioContext::~OfflineAudioContext() |
100 { | 108 { |
101 } | 109 } |
102 | 110 |
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 | |
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 176 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
104 { | 177 { |
178 ASSERT(isMainThread()); | |
179 | |
105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 180 // Calling close() on an OfflineAudioContext is not supported/allowed, |
106 // but it might well have been stopped by its execution context. | 181 // but it might well have been closed by its execution context. |
107 if (isContextClosed()) { | 182 if (isContextClosed()) { |
108 return ScriptPromise::rejectWithDOMException( | 183 return ScriptPromise::rejectWithDOMException( |
109 scriptState, | 184 scriptState, |
110 DOMException::create( | 185 DOMException::create( |
111 InvalidStateError, | 186 InvalidStateError, |
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 187 "cannot call startRendering on an OfflineAudioContext in a close d state.")); |
113 } | 188 } |
114 | 189 |
115 if (m_offlineResolver) { | 190 if (m_completeResolver) { |
116 // Can't call startRendering more than once. Return a rejected promise now. | 191 // Can't call startRendering more than once. Return a rejected promise now. |
117 return ScriptPromise::rejectWithDOMException( | 192 return ScriptPromise::rejectWithDOMException( |
118 scriptState, | 193 scriptState, |
119 DOMException::create( | 194 DOMException::create( |
120 InvalidStateError, | 195 InvalidStateError, |
121 "cannot call startRendering more than once")); | 196 "cannot call startRendering more than once")); |
122 } | 197 } |
123 | 198 |
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 199 // If the context is not in the suspended state, reject the promise. |
125 startRendering(); | 200 if (contextState() != AudioContextState::Suspended) { |
126 return m_offlineResolver->promise(); | 201 return ScriptPromise::rejectWithDOMException( |
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(); | |
127 } | 217 } |
128 | 218 |
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 219 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
130 { | 220 { |
131 return ScriptPromise::rejectWithDOMException( | 221 return ScriptPromise::rejectWithDOMException( |
132 scriptState, | 222 scriptState, |
133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); | 223 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); |
134 } | 224 } |
135 | 225 |
136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | 226 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) |
137 { | 227 { |
138 return ScriptPromise::rejectWithDOMException( | 228 // This CANNOT be called on OfflineAudioContext; it is to implement the pure |
139 scriptState, | 229 // virtual interface from AbstractAudioContext. |
140 DOMException::create( | 230 RELEASE_ASSERT_NOT_REACHED(); |
141 InvalidAccessError, | 231 |
142 "cannot suspend an OfflineAudioContext")); | 232 return ScriptPromise(); |
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))); | |
hiroshige
2015/07/21 10:50:48
My solution of using OwnPtr doesn't ensure Schedul
hongchan
2015/07/21 18:34:18
Done. I accepted your idea and created PS27.
| |
278 | |
279 return promise; | |
143 } | 280 } |
144 | 281 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 282 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 283 { |
147 return ScriptPromise::rejectWithDOMException( | 284 ASSERT(isMainThread()); |
148 scriptState, | 285 |
149 DOMException::create( | 286 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); |
150 InvalidAccessError, | 287 ScriptPromise promise = resolver->promise(); |
151 "cannot resume an OfflineAudioContext")); | 288 |
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)); | |
152 } | 424 } |
153 | 425 |
154 } // namespace blink | 426 } // namespace blink |
155 | 427 |
156 #endif // ENABLE(WEB_AUDIO) | 428 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |