Chromium Code Reviews| Index: Source/modules/webaudio/OfflineAudioContext.cpp |
| diff --git a/Source/modules/webaudio/OfflineAudioContext.cpp b/Source/modules/webaudio/OfflineAudioContext.cpp |
| index d3488419e947e373d50f13cb105be0fb0604d178..24b48b384c1ab87310c8af533fc7653ef6b52c84 100644 |
| --- a/Source/modules/webaudio/OfflineAudioContext.cpp |
| +++ b/Source/modules/webaudio/OfflineAudioContext.cpp |
| @@ -31,7 +31,11 @@ |
| #include "core/dom/Document.h" |
| #include "core/dom/ExceptionCode.h" |
| #include "core/dom/ExecutionContext.h" |
| +#include "modules/webaudio/OfflineAudioCompletionEvent.h" |
| +#include "modules/webaudio/OfflineAudioDestinationNode.h" |
| +#include "platform/ThreadSafeFunctional.h" |
| #include "platform/audio/AudioUtilities.h" |
| +#include "public/platform/Platform.h" |
| namespace blink { |
| @@ -92,6 +96,8 @@ OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi |
| OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate) |
| : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) |
| + , m_isRenderingStarted(false) |
| + , m_totalRenderFrames(numberOfFrames) |
| { |
| } |
| @@ -99,8 +105,118 @@ OfflineAudioContext::~OfflineAudioContext() |
| { |
| } |
| +// FIXME: What should be done to trace members in OfflineAudioContext? |
|
hongchan
2015/06/12 17:51:44
Oilpan team's opinion is needed here.
|
| +// DEFINE_TRACE(OfflineAudioContext) |
| +// { |
| +// visitor->trace(m_scheduledSuspends); |
| +// visitor->trace(m_completeResolver); |
| +// } |
| + |
| +OfflineAudioContext::ScheduledSuspendContainer::ScheduledSuspendContainer( |
| + size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
| + : m_suspendFrame(suspendFrame) |
| + , m_resolver(resolver) |
| + , m_isPending(false) |
| +{ |
| +} |
| + |
| +OfflineAudioContext::ScheduledSuspendContainer::~ScheduledSuspendContainer() |
| +{ |
| +} |
| + |
| +DEFINE_TRACE(OfflineAudioContext::ScheduledSuspendContainer) |
| +{ |
| + visitor->trace(m_resolver); |
| +} |
| + |
| +PassOwnPtr<OfflineAudioContext::ScheduledSuspendContainer> |
| +OfflineAudioContext::ScheduledSuspendContainer::create( |
| + size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
| +{ |
| + return adoptPtr(new ScheduledSuspendContainer(suspendFrame, resolver)); |
| +} |
| + |
| +bool OfflineAudioContext::ScheduledSuspendContainer::shouldSuspendAt(size_t whenFrame) const |
| +{ |
| + if (m_suspendFrame != whenFrame) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +bool OfflineAudioContext::ScheduledSuspendContainer::isPending() const |
| +{ |
| + return m_isPending; |
| +} |
| + |
| +void OfflineAudioContext::ScheduledSuspendContainer::markAsPending() |
| +{ |
| + m_isPending = true; |
| +} |
| + |
| +bool OfflineAudioContext::shouldSuspendNow() |
| +{ |
| + ASSERT(!isMainThread()); |
| + |
| + // Suspend if necessary and mark the associated promise as pending. Note |
| + // that duplicate entries in the suspend list are prohibited so it returns |
|
Raymond Toy
2015/06/12 21:11:37
Comment on who prohibits duplicate entries.
hongchan
2015/06/15 18:40:46
Done.
|
| + // immediately when a valid suspend is found. |
| + size_t nowFrame = currentSampleFrame(); |
| + for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { |
| + if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) { |
| + m_scheduledSuspends.at(index)->markAsPending(); |
| + return true; |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void OfflineAudioContext::resolvePendingSuspendPromises() |
| +{ |
| + ASSERT(!isMainThread()); |
| + |
| + // Resolve promises marked as 'pending'. |
| + if (m_scheduledSuspends.size() > 0) { |
| + Platform::current()->mainThread()->postTask(FROM_HERE, |
| + threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread, this)); |
| + } |
| +} |
| + |
| +void OfflineAudioContext::fireCompletionEvent() |
| +{ |
| + ASSERT(isMainThread()); |
| + if (!isMainThread()) |
| + return; |
| + |
| + // We set the state to closed here so that the oncomplete event handler sees |
| + // that the context has been closed. |
| + setContextState(Closed); |
| + |
| + AudioBuffer* renderedBuffer = renderTarget().get(); |
| + |
| + ASSERT(renderedBuffer); |
| + if (!renderedBuffer) |
| + return; |
| + |
| + // Avoid firing the event if the document has already gone away. |
| + if (executionContext()) { |
| + // Call the offline rendering completion event listener and resolve the |
| + // promise too. |
| + dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); |
| + m_completeResolver->resolve(renderedBuffer); |
| + } |
| +} |
| + |
| ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptState) |
| { |
| + ASSERT(isMainThread()); |
| + |
| + // FIXME: This check causes crash on oac-detached-no-crash.html |
| + // ASSERT(m_destinationNode); |
| + |
| + // AutoLocker locker(this); |
|
Raymond Toy
2015/06/12 21:11:37
Fix these. Does the crash still happen?
hongchan
2015/06/15 18:40:46
Done.
|
| + |
| // Calling close() on an OfflineAudioContext is not supported/allowed, |
| // but it might well have been stopped by its execution context. |
| if (isContextClosed()) { |
| @@ -111,7 +227,7 @@ ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat |
| "cannot call startRendering on an OfflineAudioContext in a stopped state.")); |
| } |
| - if (m_offlineResolver) { |
| + if (m_completeResolver) { |
| // Can't call startRendering more than once. Return a rejected promise now. |
| return ScriptPromise::rejectWithDOMException( |
| scriptState, |
| @@ -120,9 +236,138 @@ ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat |
| "cannot call startRendering more than once")); |
| } |
| - m_offlineResolver = ScriptPromiseResolver::create(scriptState); |
| - startRendering(); |
| - return m_offlineResolver->promise(); |
| + m_completeResolver = ScriptPromiseResolver::create(scriptState); |
| + |
| + // If the context is not in the suspended state, reject the promise. |
| + if (contextState() != AudioContextState::Suspended) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot startRendering when an OfflineAudioContext is not in a suspended state")); |
| + } |
| + |
| + // Start rendering and return the promise. |
| + m_isRenderingStarted = true; |
| + setContextState(Running); |
| + destination()->audioDestinationHandler().startRendering(); |
| + return m_completeResolver->promise(); |
| +} |
| + |
| +ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptState, double when) |
| +{ |
| + ASSERT(isMainThread()); |
| + |
| + // AutoLocker locker(this); |
|
Raymond Toy
2015/06/12 21:11:37
Remove this.
hongchan
2015/06/15 18:40:46
Done.
|
| + |
| + // The specified suspend time is negative, reject the promise. |
| + if (when < 0) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "negative suspend time is not allowed")); |
| + } |
| + |
| + // Quantize the suspend time to the rendering block boundary. |
| + size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTimeToRenderQuantum(when); |
| + |
| + // The specified suspend time is in the past, reject the promise. |
| + if (quantizedFrame < currentSampleFrame()) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot schedule a suspend in the past")); |
|
Raymond Toy
2015/06/12 21:11:37
Is there a test for this?
hongchan
2015/06/15 18:40:46
I will add one in |oac-suspend-resume-basic.html|
|
| + } |
| + |
| + // The suspend time should be earlier than the total render frame. If the |
| + // requested suspension time is equal to the total render frame, the promise |
| + // will be rejected. |
| + if (m_totalRenderFrames <= quantizedFrame) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot schedule a suspend beyond the total render duration")); |
| + } |
| + |
| + // If there is a duplicate suspension at the same quantize frame, reject the |
| + // promise. |
| + for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { |
| + if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot schedule a suspend at the duplicate time")); |
|
Raymond Toy
2015/06/12 21:11:37
Include the time in the message.
hongchan
2015/06/15 18:40:46
Done.
|
| + } |
| + } |
| + |
| + RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver)); |
| + |
| + return promise; |
| +} |
| + |
| +ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptState) |
| +{ |
| + ASSERT(isMainThread()); |
| + |
| + AutoLocker locker(this); |
| + |
| + // If the context is not in a suspended state, reject the promise. |
| + if (contextState() != AudioContextState::Suspended) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot resume the context that is not suspended")); |
|
Raymond Toy
2015/06/12 21:11:36
the -> a
hongchan
2015/06/15 18:40:46
Done.
|
| + } |
| + |
| + // If the rendering has not started, reject the promise. |
| + if (!m_isRenderingStarted) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create( |
| + InvalidStateError, |
| + "cannot resume the context that has not started")); |
| + } |
| + |
| + // If the context is suspended, resume rendering by calling startRendering() |
| + // and set the state to "Running." Note that resuming is possible only after |
| + // the rendering started. |
| + destination()->audioDestinationHandler().startRendering(); |
| + setContextState(Running); |
|
Raymond Toy
2015/06/12 21:11:37
Should the state be set for calling startRendering
hongchan
2015/06/15 18:40:46
Can you elaborate? I can swap the order of the sta
Raymond Toy
2015/06/15 20:42:13
Pretty much. I think setting the Running state bef
|
| + |
| + RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + |
| + // Resolve the promise immediately. |
| + resolver->resolve(); |
| + |
| + return promise; |
| +} |
| + |
| +void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread() |
| +{ |
| + ASSERT(isMainThread()); |
| + AutoLocker locker(this); |
| + |
| + // Suspend the context first. This will fire onstatechange event. |
| + setContextState(Suspended); |
| + |
| + // FIXME: is removing elements efficient? What if there are 10K suspends? |
| + // Resolve any pending suspend and remove it from the list. |
| + for (unsigned index = 0; index < m_scheduledSuspends.size();) { |
| + if (m_scheduledSuspends.at(index)->isPending()) { |
| + m_scheduledSuspends.at(index)->resolver()->resolve(); |
| + m_scheduledSuspends.remove(index); |
| + } else { |
| + ++index; |
| + } |
| + } |
| } |
| } // namespace blink |