Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1459)

Unified Diff: Source/modules/webaudio/OfflineAudioContext.cpp

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Bring ToT Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/webaudio/OfflineAudioContext.h ('k') | Source/modules/webaudio/OfflineAudioContext.idl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/webaudio/OfflineAudioContext.cpp
diff --git a/Source/modules/webaudio/OfflineAudioContext.cpp b/Source/modules/webaudio/OfflineAudioContext.cpp
index b95e4da61988f5074ebc95145d4682c63b41c476..67428bc8e9bb415fffacee6ad937d1dfb1e8ef1e 100644
--- a/Source/modules/webaudio/OfflineAudioContext.cpp
+++ b/Source/modules/webaudio/OfflineAudioContext.cpp
@@ -32,7 +32,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 {
@@ -93,26 +99,110 @@ OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi
OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate)
: AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRate)
+ , m_isRenderingStarted(false)
+ , m_totalRenderFrames(numberOfFrames)
{
}
OfflineAudioContext::~OfflineAudioContext()
{
+ // Reject all the remaining suspends and delete the raw pointer.
+ for (ScheduledSuspendContainer* suspendContainer : m_suspendContainers) {
+ suspendContainer->rejectPromise(InvalidStateError,
+ "Offline audio context is going away");
+
+ delete suspendContainer;
+ }
+
+ // TODO(hongchan): is this necessary?
+ m_suspendContainers.clear();
+}
+
+DEFINE_TRACE(OfflineAudioContext)
+{
+ visitor->trace(m_completeResolver);
+ AbstractAudioContext::trace(visitor);
+}
+
+bool OfflineAudioContext::shouldSuspendNow()
+{
+ ASSERT(!isMainThread());
+
+ // If there is any scheduled suspend at |currentSampleFrame|, the context
+ // should be suspended.
+ if (!m_scheduledSuspendContainers.contains(currentSampleFrame()))
+ return false;
+
+ return true;
+}
+
+void OfflineAudioContext::resolvePendingSuspendPromises()
+{
+ ASSERT(!isMainThread());
+
+ // Find a suspend scheduled at |currentSampleFrame| and resolve the
+ // associated promise on the main thread.
+ SuspendContainerMap::iterator it = m_scheduledSuspendContainers.find(currentSampleFrame());
+ if (it == m_scheduledSuspendContainers.end())
+ return;
+
+ ScheduledSuspendContainer* suspendContainer = it->value;
+ m_scheduledSuspendContainers.remove(it);
+
+ // Passing a raw pointer |suspendContainer| is safe here because it is
+ // created and destructed on the main thread. Also it is guaranteed to be
+ // alive until:
+ // - the raw pointer is passed back to the main thread and
+ // resolveSuspendContainerOnMainThread()/rejectSuspendContainerOnMainThread()
+ // is called on the main thread, or
+ // - OfflineAudioContext is destructed.
+ Platform::current()->mainThread()->postTask(FROM_HERE,
+ threadSafeBind(&OfflineAudioContext::resolveSuspendContainerOnMainThread, this,
+ AllowCrossThreadAccess(suspendContainer)));
+}
+
+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);
+ } else {
+ // The resolver should be rejected when the execution context is gone.
+ m_completeResolver->reject(DOMException::create(InvalidStateError,
+ "the execution context does not exist"));
+ }
}
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.
+ // but it might well have been closed by its execution context.
if (isContextClosed()) {
return ScriptPromise::rejectWithDOMException(
scriptState,
DOMException::create(
InvalidStateError,
- "cannot call startRendering on an OfflineAudioContext in a stopped state."));
+ "cannot call startRendering on an OfflineAudioContext in a closed state."));
}
- if (m_offlineResolver) {
+ if (m_completeResolver) {
// Can't call startRendering more than once. Return a rejected promise now.
return ScriptPromise::rejectWithDOMException(
scriptState,
@@ -121,9 +211,24 @@ 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.
+ ASSERT(!m_isRenderingStarted);
+ m_isRenderingStarted = true;
+ setContextState(Running);
+ destinationHandler().startRendering();
+
+ return m_completeResolver->promise();
}
ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState)
@@ -135,20 +240,203 @@ ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState)
ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState)
{
- return ScriptPromise::rejectWithDOMException(
- scriptState,
- DOMException::create(
- InvalidAccessError,
- "cannot suspend an OfflineAudioContext"));
+ // This CANNOT be called on OfflineAudioContext; it is to implement the pure
+ // virtual interface from AbstractAudioContext.
+ RELEASE_ASSERT_NOT_REACHED();
+
+ return ScriptPromise();
+}
+
+ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, double when)
+{
+ ASSERT(isMainThread());
+
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ // The render thread does not exist; reject the promise.
+ if (!destinationHandler().offlineRenderThread()) {
+ resolver->reject(DOMException::create(InvalidStateError,
+ "the rendering is already finished"));
+ return 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 render quantum boundary.
+ size_t quantizedFrame = destinationHandler().quantizeTimeToRenderQuantum(when);
+
+ // 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(resolver);
+
+ // Basic checks are passed; store the object to maintain the ownership.
+ m_suspendContainers.add(suspendContainer);
+
+ // Validate the suspend and add it if necessary on the render thread.
+ // Note that passing a raw pointer |suspendContainer| is safe here as well
+ // with the same reason in |resolvePendingSuspendPromises|.
+ destinationHandler().offlineRenderThread()->postTask(FROM_HERE,
+ threadSafeBind(&OfflineAudioContext::validateSuspendContainerOnRenderThread, this,
+ AllowCrossThreadAccess(suspendContainer),
+ when,
+ quantizedFrame));
+
+ return promise;
}
ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState)
{
- return ScriptPromise::rejectWithDOMException(
- scriptState,
- DOMException::create(
- InvalidAccessError,
- "cannot resume an OfflineAudioContext"));
+ 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);
+ destinationHandler().startRendering();
+
+ // Resolve the promise immediately.
+ resolver->resolve();
+
+ return promise;
+}
+
+OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler()
+{
+ return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDestinationHandler());
+}
+
+void OfflineAudioContext::validateSuspendContainerOnRenderThread(ScheduledSuspendContainer* suspendContainer, double suspendTime, size_t suspendFrame)
+{
+ ASSERT(!isMainThread());
+
+ bool shouldBeRejected = false;
+ String errorMessage;
+
+ // The specified suspend time is in the past; reject the promise. We cannot
+ // reject the promise in here, so set the error message before posting the
+ // rejection task to the main thread.
+ if (suspendFrame < currentSampleFrame()) {
+ shouldBeRejected = true;
+ errorMessage = "cannot schedule a suspend at frame " +
+ String::number(suspendFrame) + " (" + String::number(suspendTime) +
+ " seconds) because it is earlier than the current frame of " +
+ String::number(currentSampleFrame()) + " (" +
+ String::number(currentTime()) + " seconds)";
+ } else if (m_scheduledSuspendContainers.contains(suspendFrame)) {
+ // If there is a duplicate suspension at the same quantized frame,
+ // reject the promise. The rejection of promise should happen in the
+ // main thread, so we post a task to it. Here we simply set the error
+ // message and post a task to the main thread.
+ shouldBeRejected = true;
+ errorMessage = "cannot schedule more than one suspend at frame " +
+ String::number(suspendFrame) + " (" +
+ String::number(suspendTime) + " seconds)";
+ }
+
+ // Reject the promise if necessary on the main thread.
+ // Note that passing a raw pointer |suspendContainer| is safe here as well
+ // with the same reason in |resolvePendingSuspendPromises|.
+ if (shouldBeRejected) {
+ Platform::current()->mainThread()->postTask(FROM_HERE,
+ threadSafeBind(&OfflineAudioContext::rejectSuspendContainerOnMainThread, this,
+ AllowCrossThreadAccess(suspendContainer),
+ errorMessage));
+ return;
+ }
+
+ // If the validation check passes, we can add the container safely here in
+ // the render thread.
+ m_scheduledSuspendContainers.add(suspendFrame, suspendContainer);
+}
+
+void OfflineAudioContext::rejectSuspendContainerOnMainThread(ScheduledSuspendContainer* suspendContainer, const String& errorMessage)
+{
+ ASSERT(isMainThread());
+ RELEASE_ASSERT(suspendContainer);
+
+ suspendContainer->rejectPromise(InvalidStateError, errorMessage);
+
+ // Remove the container out of the set and delete the raw pointer.
+ m_suspendContainers.remove(suspendContainer);
+ delete suspendContainer;
+}
+
+void OfflineAudioContext::resolveSuspendContainerOnMainThread(ScheduledSuspendContainer* suspendContainer)
+{
+ ASSERT(isMainThread());
+ RELEASE_ASSERT(suspendContainer);
+
+ // Suspend the context first. This will fire onstatechange event.
+ setContextState(Suspended);
+
+ suspendContainer->resolvePromise();
+
+ // Remove the container out of the set and delete the raw pointer.
+ m_suspendContainers.remove(suspendContainer);
+ delete suspendContainer;
+}
+
+ScheduledSuspendContainer::ScheduledSuspendContainer(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
+ : m_resolver(resolver)
+{
+ ASSERT(isMainThread());
+}
+
+ScheduledSuspendContainer::~ScheduledSuspendContainer()
+{
+ ASSERT(isMainThread());
+}
+
+ScheduledSuspendContainer* ScheduledSuspendContainer::create(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
+{
+ return new ScheduledSuspendContainer(resolver);
+}
+
+void ScheduledSuspendContainer::resolvePromise()
+{
+ ASSERT(isMainThread());
+
+ m_resolver->resolve();
+}
+
+void ScheduledSuspendContainer::rejectPromise(ExceptionCode errorCode, const String& errorMessage)
+{
+ ASSERT(isMainThread());
+
+ m_resolver->reject(DOMException::create(errorCode, errorMessage));
}
} // namespace blink
« no previous file with comments | « Source/modules/webaudio/OfflineAudioContext.h ('k') | Source/modules/webaudio/OfflineAudioContext.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698