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

Unified Diff: third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp

Issue 1405413004: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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
Index: third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
diff --git a/third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp b/third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
index a0e085f13dbf05d5fd251e4c00baf0ab5e818e22..68ed81ff88adede5ecfc7f7d89abfa1fed9bb23a 100644
--- a/third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
+++ b/third_party/WebKit/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
@@ -28,8 +28,12 @@
#include "core/dom/CrossThreadTask.h"
#include "modules/webaudio/AbstractAudioContext.h"
+#include "modules/webaudio/AudioNodeInput.h"
+#include "modules/webaudio/AudioNodeOutput.h"
+#include "modules/webaudio/OfflineAudioContext.h"
#include "platform/Task.h"
#include "platform/audio/AudioBus.h"
+#include "platform/audio/DenormalDisabler.h"
#include "platform/audio/HRTFDatabaseLoader.h"
#include "public/platform/Platform.h"
#include <algorithm>
@@ -41,7 +45,11 @@ const size_t renderQuantumSize = 128;
OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node, AudioBuffer* renderTarget)
: AudioDestinationHandler(node, renderTarget->sampleRate())
, m_renderTarget(renderTarget)
- , m_startedRendering(false)
+ , m_renderThread(adoptPtr(Platform::current()->createThread("offline audio renderer")))
+ , m_framesProcessed(0)
+ , m_framesToProcess(0)
+ , m_isRenderingStarted(false)
+ , m_shouldSuspend(false)
{
m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuantumSize);
}
@@ -81,34 +89,56 @@ void OfflineAudioDestinationHandler::uninitialize()
AudioHandler::uninitialize();
}
+OfflineAudioContext* OfflineAudioDestinationHandler::context() const
+{
+ return static_cast<OfflineAudioContext*>(m_context);
+}
+
void OfflineAudioDestinationHandler::startRendering()
{
ASSERT(isMainThread());
ASSERT(m_renderTarget);
+ ASSERT(m_renderThread);
+
if (!m_renderTarget)
return;
- if (!m_startedRendering) {
- m_startedRendering = true;
- m_renderThread = adoptPtr(Platform::current()->createThread("Offline Audio Renderer"));
- m_renderThread->taskRunner()->postTask(FROM_HERE, new Task(threadSafeBind(&OfflineAudioDestinationHandler::offlineRender, PassRefPtr<OfflineAudioDestinationHandler>(this))));
+ // Rendering was not started. Starting now.
+ if (!m_isRenderingStarted) {
+ m_renderThread->taskRunner()->postTask(FROM_HERE,
+ new Task(threadSafeBind(&OfflineAudioDestinationHandler::startOfflineRendering, this)));
+ m_isRenderingStarted = true;
+ return;
}
+
+ // Rendering is already started, which implicitly means we resume the
+ // rendering by calling |doOfflineRendering| on the render thread.
+ m_renderThread->taskRunner()->postTask(FROM_HERE,
+ threadSafeBind(&OfflineAudioDestinationHandler::doOfflineRendering, this));
}
void OfflineAudioDestinationHandler::stopRendering()
{
+ // OfflineAudioContext CANNOT BE stopped by JavaScript.
ASSERT_NOT_REACHED();
}
-void OfflineAudioDestinationHandler::offlineRender()
+WebThread* OfflineAudioDestinationHandler::offlineRenderThread()
+{
+ ASSERT(m_renderThread);
+
+ return m_renderThread.get();
+}
+
+size_t OfflineAudioDestinationHandler::renderQuantumLength() const
Raymond Toy 2015/10/16 23:32:36 Maybe renderQuantumFrames is a better name to indi
hongchan 2015/10/19 20:08:12 Done.
{
- offlineRenderInternal();
- context()->handlePostRenderTasks();
+ return renderQuantumSize;
}
-void OfflineAudioDestinationHandler::offlineRenderInternal()
+void OfflineAudioDestinationHandler::startOfflineRendering()
{
ASSERT(!isMainThread());
+
ASSERT(m_renderBus);
if (!m_renderBus)
return;
@@ -130,38 +160,144 @@ void OfflineAudioDestinationHandler::offlineRenderInternal()
// Break up the render target into smaller "render quantize" sized pieces.
// Render until we're finished.
- size_t framesToProcess = m_renderTarget->length();
+ m_framesToProcess = m_renderTarget->length();
+
+ // Start rendering.
+ doOfflineRendering();
+}
+
+void OfflineAudioDestinationHandler::doOfflineRendering()
+{
+ ASSERT(!isMainThread());
+
unsigned numberOfChannels = m_renderTarget->numberOfChannels();
- unsigned n = 0;
- while (framesToProcess > 0) {
- // Render one render quantum.
- render(0, m_renderBus.get(), renderQuantumSize);
+ // Reset the suspend flag.
+ m_shouldSuspend = false;
+
+ // If there is more to process and there is no suspension at the moment,
+ // do continue to render quanta. Then calling OfflineAudioContext.resume() will pick up
+ // the render loop again from where it was suspended.
+ while (m_framesToProcess > 0 && !m_shouldSuspend) {
+
+ // Render one render quantum. Note that this includes pre/post render
+ // tasks from the online audio context. Note that this method will
+ // change |m_shouldSuspend| internally according to the scheduled
+ // suspends.
+ offlineRender(0, m_renderBus.get(), renderQuantumSize);
+
+ if (m_shouldSuspend) {
Raymond Toy 2015/10/16 23:32:36 I think it would be clearer if offlineRender retur
hongchan 2015/10/19 20:08:12 I can certainly do that and I have thought about i
hongchan 2015/10/20 22:03:06 Done per the first comment on this.
+ suspendOfflineRendering();
+ return;
+ }
- size_t framesAvailableToCopy = std::min(framesToProcess, renderQuantumSize);
+ size_t framesAvailableToCopy = std::min(m_framesToProcess, renderQuantumSize);
for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
const float* source = m_renderBus->channel(channelIndex)->data();
float* destination = m_renderTarget->getChannelData(channelIndex)->data();
- memcpy(destination + n, source, sizeof(float) * framesAvailableToCopy);
+ memcpy(destination + m_framesProcessed, source, sizeof(float) * framesAvailableToCopy);
}
- n += framesAvailableToCopy;
- framesToProcess -= framesAvailableToCopy;
+ m_framesProcessed += framesAvailableToCopy;
+ m_framesToProcess -= framesAvailableToCopy;
Raymond Toy 2015/10/16 23:32:36 Is it possible for wrap around to happen? Maybe a
hongchan 2015/10/19 20:08:12 Done.
+ }
+
+ // Finish up the rendering loop if there is no more to process.
+ if (m_framesToProcess <= 0) {
Raymond Toy 2015/10/16 23:32:36 Since m_framesToProcess is size_t, less than 0 isn
hongchan 2015/10/19 20:08:12 Done.
+ ASSERT(m_framesToProcess == 0);
+ finishOfflineRendering();
+ return;
Raymond Toy 2015/10/16 23:32:36 return not really necessary here.
hongchan 2015/10/19 20:08:12 Done.
+ }
+}
+
+void OfflineAudioDestinationHandler::suspendOfflineRendering()
+{
+ ASSERT(!isMainThread());
+
+ // The actual rendering has been suspended. Notify the context.
+ if (context()->executionContext()) {
+ context()->executionContext()->postTask(FROM_HERE,
+ createCrossThreadTask(&OfflineAudioDestinationHandler::notifySuspend, this));
+ }
+}
+
+void OfflineAudioDestinationHandler::finishOfflineRendering()
+{
+ ASSERT(!isMainThread());
+
+ // The actual rendering has been completed. Notify the context.
+ if (context()->executionContext()) {
+ context()->executionContext()->postTask(FROM_HERE,
+ createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, this));
}
+}
- // Our work is done. Let the AbstractAudioContext know.
- if (context()->executionContext())
- context()->executionContext()->postTask(FROM_HERE, createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, PassRefPtr<OfflineAudioDestinationHandler>(this)));
+void OfflineAudioDestinationHandler::notifySuspend()
+{
+ if (context())
+ context()->resolveSuspendOnMainThread(context()->currentSampleFrame());
}
void OfflineAudioDestinationHandler::notifyComplete()
{
- // The AbstractAudioContext might be gone.
+ // The OfflineAudioContext might be gone.
if (context())
context()->fireCompletionEvent();
}
+void OfflineAudioDestinationHandler::offlineRender(AudioBus* sourceBus, AudioBus* destinationBus, size_t numberOfFrames)
+{
+ // We don't want denormals slowing down any of the audio processing
+ // since they can very seriously hurt performance.
+ // This will take care of all AudioNodes because they all process within this scope.
+ DenormalDisabler denormalDisabler;
+
+ context()->deferredTaskHandler().setAudioThread(currentThread());
+
+ if (!context()->isDestinationInitialized()) {
+ destinationBus->zero();
+ return;
+ }
+
+ // Take care pre-render tasks at the beginning of each render quantum. This
+ // will change |m_shouldSuspend| flag if there is a suspend scheduled at the
+ // current frame.
+ m_shouldSuspend = context()->handlePreOfflineRenderTasks();
+ if (m_shouldSuspend)
+ return;
+
+ // Prepare the local audio input provider for this render quantum.
+ if (sourceBus)
+ m_localAudioInputProvider.set(sourceBus);
+
+ ASSERT(numberOfInputs() >= 1);
+ if (numberOfInputs() < 1) {
+ destinationBus->zero();
+ return;
+ }
+ // This will cause the node(s) connected to us to process, which in turn will pull on their input(s),
+ // all the way backwards through the rendering graph.
+ AudioBus* renderedBus = input(0).pull(destinationBus, numberOfFrames);
+
+ if (!renderedBus) {
+ destinationBus->zero();
+ } else if (renderedBus != destinationBus) {
+ // in-place processing was not possible - so copy
+ destinationBus->copyFrom(*renderedBus);
+ }
+
+ // Process nodes which need a little extra help because they are not connected to anything, but still need to process.
+ context()->deferredTaskHandler().processAutomaticPullNodes(numberOfFrames);
+
+ // Let the context take care of any business at the end of each render quantum.
+ context()->handlePostOfflineRenderTasks();
+
+ // Advance current sample-frame.
+ size_t newSampleFrame = m_currentSampleFrame + numberOfFrames;
+ releaseStore(&m_currentSampleFrame, newSampleFrame);
Raymond Toy 2015/10/16 23:32:36 Not for this CL, but we should probably add a sett
hongchan 2015/10/19 20:08:12 You mean having the override = operator with the a
Raymond Toy 2015/10/19 20:27:27 I think the chromium/blink style is currentSampleF
hongchan 2015/10/20 22:03:06 Acknowledged.
+}
+
// ----------------------------------------------------------------
OfflineAudioDestinationNode::OfflineAudioDestinationNode(AbstractAudioContext& context, AudioBuffer* renderTarget)

Powered by Google App Engine
This is Rietveld 408576698