Index: Source/modules/webaudio/OfflineAudioContext.cpp |
diff --git a/Source/modules/webaudio/OfflineAudioContext.cpp b/Source/modules/webaudio/OfflineAudioContext.cpp |
index d3488419e947e373d50f13cb105be0fb0604d178..249a2774a253a923170eea9550e4f84e0a610ea5 100644 |
--- a/Source/modules/webaudio/OfflineAudioContext.cpp |
+++ b/Source/modules/webaudio/OfflineAudioContext.cpp |
@@ -31,7 +31,13 @@ |
#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/Task.h" |
+#include "platform/ThreadSafeFunctional.h" |
#include "platform/audio/AudioUtilities.h" |
+#include "public/platform/Platform.h" |
+ |
namespace blink { |
@@ -92,6 +98,9 @@ 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_isResolvingSuspendPromise(false) |
+ , m_totalRenderFrames(numberOfFrames) |
{ |
} |
@@ -99,8 +108,75 @@ OfflineAudioContext::~OfflineAudioContext() |
{ |
} |
+DEFINE_TRACE(OfflineAudioContext) |
+{ |
+ visitor->trace(m_scheduledSuspends); |
hongchan
2015/06/24 21:14:12
This gives me a compile error:
./../third_party/W
haraken
2015/06/24 23:25:57
m_scheduledSuspends is not a HashMap of Oilpan. Yo
hongchan
2015/06/25 23:34:13
Done.
|
+ visitor->trace(m_completeResolver); |
+ AudioContext::trace(visitor); |
+} |
+ |
+bool OfflineAudioContext::shouldSuspendNow() |
+{ |
+ ASSERT(!isMainThread()); |
+ |
+ fprintf(stderr, "shouldSuspendNow\n"); |
+ |
+ // If there is any scheduled suspend at |currentSampleFrame|, the context |
+ // should be suspended. |
+ if (m_scheduledSuspends.contains(currentSampleFrame())) |
+ return true; |
+ |
+ return false; |
+} |
+ |
+void OfflineAudioContext::resolvePendingSuspendPromises() |
+{ |
+ ASSERT(!isMainThread()); |
+ |
+ fprintf(stderr, "resolvePendingSuspendPromises\n"); |
+ |
+ if (!m_isResolvingSuspendPromise || m_scheduledSuspends.isEmpty()) |
+ return; |
+ |
+ size_t nowFrame = currentSampleFrame(); |
+ |
+ SuspendContainerMap::iterator it = m_scheduledSuspends.find(nowFrame); |
+ if (it == m_scheduledSuspends.end()) |
+ return; |
+ |
+ RefPtrWillBeRawPtr<ScheduledSuspendContainer> suspendContainer = it->value; |
yhirano
2015/06/25 04:00:53
This should be just RefPtr.
hongchan
2015/06/25 23:34:13
Done.
|
+ Platform::current()->mainThread()->postTask(FROM_HERE, |
+ threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread, this, suspendContainer)); |
yhirano
2015/06/25 04:00:53
suspendContainer.release()
hongchan
2015/06/25 23:34:13
Done.
|
+ m_scheduledSuspends.remove(nowFrame); |
yhirano
2015/06/25 04:00:52
Please move this statement to L148.
yhirano
2015/06/25 04:00:53
[optional] remove(it) may be more straightforward?
hongchan
2015/06/25 23:34:13
Done and done.
|
+} |
+ |
+void OfflineAudioContext::fireCompletionEvent() |
+{ |
+ ASSERT(isMainThread()); |
+ |
+ // 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(); |
+ |
+ 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()); |
+ |
// Calling close() on an OfflineAudioContext is not supported/allowed, |
// but it might well have been stopped by its execution context. |
if (isContextClosed()) { |
@@ -111,7 +187,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 +196,187 @@ ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat |
"cannot call startRendering more than once")); |
} |
- m_offlineResolver = ScriptPromiseResolver::create(scriptState); |
- startRendering(); |
- return m_offlineResolver->promise(); |
+ // 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")); |
+ } |
+ |
+ m_completeResolver = ScriptPromiseResolver::create(scriptState); |
+ |
+ // 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()); |
+ ASSERT(destination()->audioDestinationHandler().offlineRenderThread()); |
+ |
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
+ ScriptPromise promise = resolver->promise(); |
+ |
+ // The specified suspend time is negative, reject the promise. |
+ if (when < 0) { |
+ resolver->reject(DOMException::create(InvalidStateError, |
+ "negative suspend time (" + String::number(when) + ") is not allowed")); |
+ return promise; |
+ } |
+ |
+ // 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()) { |
+ resolver->reject(DOMException::create(InvalidStateError, |
+ "cannot schedule a suspend at frame " + String::number(quantizedFrame) + |
+ " (" + String::number(when) + " seconds) because it is earlier than the current frame of " + |
+ String::number(currentSampleFrame()))); |
+ return promise; |
+ } |
+ |
+ // 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) { |
+ resolver->reject(DOMException::create(InvalidStateError, |
+ "cannot schedule a suspend at frame " + String::number(quantizedFrame) + |
+ " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " + |
+ String::number(m_totalRenderFrames) + " frames")); |
+ return promise; |
+ } |
+ |
+ ScheduledSuspendContainer* suspendContainer = ScheduledSuspendContainer::create(when, quantizedFrame, resolver); |
yhirano
2015/06/25 04:00:52
RefPtr<ScheduledSuspendContainer> suspendContainer
hongchan
2015/06/25 23:34:13
Done.
|
+ |
+ // Validate the suspend and append if necessary on the render thread. |
+ destination()->audioDestinationHandler().offlineRenderThread()->postTask(FROM_HERE, |
+ threadSafeBind(&OfflineAudioContext::checkDuplicateSuspend, this, suspendContainer)); |
+ |
+ return promise; |
+} |
+ |
+ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptState) |
+{ |
+ ASSERT(isMainThread()); |
+ |
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
+ ScriptPromise promise = resolver->promise(); |
+ |
+ // If the context is not in a suspended state, reject the promise. |
+ if (contextState() != AudioContextState::Suspended) { |
+ resolver->reject(DOMException::create(InvalidStateError, |
+ "cannot resume a context that is not suspended")); |
+ return promise; |
+ } |
+ |
+ // If the rendering has not started, reject the promise. |
+ if (!m_isRenderingStarted) { |
+ resolver->reject(DOMException::create(InvalidStateError, |
+ "cannot resume a context that has not started")); |
+ return promise; |
+ } |
+ |
+ // If the context is suspended, resume rendering by setting the state to |
+ // "Running." and calling startRendering(). Note that resuming is possible |
+ // only after the rendering started. |
+ setContextState(Running); |
+ destination()->audioDestinationHandler().startRendering(); |
+ |
+ // Resolve the promise immediately. |
+ resolver->resolve(); |
+ |
+ return promise; |
+} |
+ |
+void OfflineAudioContext::checkDuplicateSuspend(ScheduledSuspendContainer* suspendContainer) |
yhirano
2015/06/25 04:00:53
PassRefPtr<ScheduledSuspendContainer>
hongchan
2015/06/25 23:34:13
Done.
|
+{ |
+ ASSERT(!isMainThread()); |
+ |
+ fprintf(stderr, "checkDuplicateSuspend\n"); |
+ |
+ // If there is a duplicate suspension at the same quantize frame, reject the |
+ // promise. The rejection of promise should happen in the main thread, so we |
+ // post a task to it. |
+ if (m_scheduledSuspends.contains(suspendContainer->suspendFrame())) { |
+ Platform::current()->mainThread()->postTask(FROM_HERE, |
+ threadSafeBind(&OfflineAudioContext::rejectSuspendPromiseOnMainThread, this, suspendContainer)); |
+ } |
+ |
+ // If duplicate check passes, we can add the container safely here in the |
+ // render thread. |
+ RefPtr<ScheduledSuspendContainer> adoptedContainer = adoptRef(suspendContainer); |
yhirano
2015/06/25 04:00:53
You should not use adoptRef here.
hongchan
2015/06/25 23:34:13
Done.
|
+ m_scheduledSuspends.set(suspendContainer->suspendFrame(), adoptedContainer); |
hongchan
2015/06/24 21:14:12
This might be the problematic part. Since the |sus
|
+} |
+ |
+void OfflineAudioContext::rejectSuspendPromiseOnMainThread(ScheduledSuspendContainer* suspendContainer) |
yhirano
2015/06/25 04:00:52
PassRefPtr<ScheduledSuspendContainer>
hongchan
2015/06/25 23:34:13
Done.
|
+{ |
+ ASSERT(isMainThread()); |
+ |
+ suspendContainer->resolver()->reject(DOMException::create(InvalidStateError, |
+ "cannot schedule more than one suspend at frame " + |
+ String::number(suspendContainer->suspendFrame()) + " (" + |
+ String::number(suspendContainer->suspendTime()) + " seconds)")); |
+} |
+ |
+void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread(ScheduledSuspendContainer* suspendContainer) |
yhirano
2015/06/25 04:00:53
PassRefPtr<ScheduledSuspendContainer>
hongchan
2015/06/25 23:34:13
Done.
|
+{ |
+ ASSERT(isMainThread()); |
+ AutoLocker locker(this); |
+ |
+ // Suspend the context first. This will fire onstatechange event. |
+ setContextState(Suspended); |
+ |
+ suspendContainer->resolver()->resolve(); |
+ m_isResolvingSuspendPromise = false; |
+} |
+ |
+ScheduledSuspendContainer::ScheduledSuspendContainer( |
+ double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
+ : m_suspendTime(suspendTime) |
+ , m_suspendFrame(suspendFrame) |
+ , m_resolver(resolver) |
+ , m_isPending(false) |
+{ |
yhirano
2015/06/25 04:00:53
ASSERT(isMainThread);
hongchan
2015/06/25 23:34:13
Done.
|
+} |
+ |
+ScheduledSuspendContainer::~ScheduledSuspendContainer() |
+{ |
yhirano
2015/06/25 04:00:53
ASSERT(isMainThread);
hongchan
2015/06/25 23:34:13
Done.
|
+} |
+ |
+DEFINE_TRACE(ScheduledSuspendContainer) |
yhirano
2015/06/25 04:00:53
No trace is needed.
hongchan
2015/06/25 23:34:13
Done.
|
+{ |
+ visitor->trace(m_resolver); |
+} |
+ |
+ScheduledSuspendContainer* ScheduledSuspendContainer::create( |
+ double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
+{ |
+ return new ScheduledSuspendContainer(suspendTime, suspendFrame, resolver); |
+} |
+ |
+bool ScheduledSuspendContainer::shouldSuspendAtFrame(size_t whenFrame) const |
+{ |
+ if (m_suspendFrame != whenFrame) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+bool ScheduledSuspendContainer::isPending() const |
+{ |
+ return m_isPending; |
+} |
+ |
+void ScheduledSuspendContainer::markAsPending() |
+{ |
+ m_isPending = true; |
} |
} // namespace blink |