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 21be406a871f9da078d91bac36b03b5ea7712ec5..026f1ce85538d821980107528572158673b5e238 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); |
Raymond Toy
2015/10/21 18:22:46
What if AudioBus::create fails to allocate space?
hongchan
2015/10/22 18:23:49
That would be an exception.
Also without a valid
|
} |
@@ -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); |
Raymond Toy
2015/10/21 18:22:46
I would reverse these so that ASSERT(m_renderTarge
hongchan
2015/10/22 18:23:49
Done.
|
+ |
if (!m_renderTarget) |
return; |
- if (!m_startedRendering) { |
- m_startedRendering = true; |
- m_renderThread = adoptPtr(Platform::current()->createThread("Offline Audio Renderer")); |
- m_renderThread->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&OfflineAudioDestinationHandler::offlineRender, PassRefPtr<OfflineAudioDestinationHandler>(this)))); |
+ // Rendering was not started. Starting now. |
+ if (!m_isRenderingStarted) { |
+ m_renderThread->taskRunner()->postTask(BLINK_FROM_HERE, |
+ new Task(threadSafeBind(&OfflineAudioDestinationHandler::startOfflineRendering, this))); |
+ m_isRenderingStarted = true; |
Raymond Toy
2015/10/21 18:22:46
Does it matter if m_isRenderingStarted is set to t
hongchan
2015/10/22 18:23:49
m_isRenderingStart can only be read/modified by st
|
+ return; |
} |
+ |
+ // Rendering is already started, which implicitly means we resume the |
+ // rendering by calling |doOfflineRendering| on the render thread. |
+ m_renderThread->taskRunner()->postTask(BLINK_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::renderQuantumFrames() const |
Raymond Toy
2015/10/21 18:22:45
Should this be inline? It's so small. Or maybe m
hongchan
2015/10/22 18:23:49
Done.
|
{ |
- offlineRenderInternal(); |
- context()->handlePostRenderTasks(); |
+ return renderQuantumSize; |
} |
-void OfflineAudioDestinationHandler::offlineRenderInternal() |
+void OfflineAudioDestinationHandler::startOfflineRendering() |
{ |
ASSERT(!isMainThread()); |
+ |
ASSERT(m_renderBus); |
if (!m_renderBus) |
return; |
@@ -130,38 +160,146 @@ 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(); |
Raymond Toy
2015/10/21 18:22:46
Shouldn't this be set when the offline context is
hongchan
2015/10/22 18:23:49
Done, and I mistakenly kept the old comment. I wil
|
+ |
+ // 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 |
Raymond Toy
2015/10/21 18:22:46
This comment about changing m_shouldSuspend intern
hongchan
2015/10/22 18:23:49
Oops. Done.
|
+ // suspends. |
+ m_shouldSuspend = checkSuspendsAndRender(0, m_renderBus.get(), renderQuantumSize); |
+ |
+ if (m_shouldSuspend) { |
+ 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; |
+ |
+ ASSERT(m_framesToProcess >= framesAvailableToCopy); |
+ m_framesToProcess -= framesAvailableToCopy; |
+ } |
+ |
+ // Finish up the rendering loop if there is no more to process. |
+ if (m_framesToProcess == 0) { |
Raymond Toy
2015/10/21 18:22:46
I hate the style, but you're not supposed to compa
hongchan
2015/10/22 18:23:49
Done.
|
+ ASSERT(m_framesToProcess == 0); |
Raymond Toy
2015/10/21 18:22:46
How can this assert ever fail? Delete it.
hongchan
2015/10/22 18:23:49
This was suggested by haraken@ before, but I think
|
+ finishOfflineRendering(); |
+ } |
+} |
+ |
+void OfflineAudioDestinationHandler::suspendOfflineRendering() |
+{ |
+ ASSERT(!isMainThread()); |
+ |
+ // The actual rendering has been suspended. Notify the context. |
+ if (context()->executionContext()) { |
+ context()->executionContext()->postTask(BLINK_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(BLINK_FROM_HERE, |
+ createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, this)); |
} |
+} |
- // Our work is done. Let the AbstractAudioContext know. |
- if (context()->executionContext()) |
- context()->executionContext()->postTask(BLINK_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(); |
} |
+bool OfflineAudioDestinationHandler::checkSuspendsAndRender(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()) { |
Raymond Toy
2015/10/21 18:22:45
For an offline context, is it actually possible fo
hongchan
2015/10/22 18:23:49
This method is just a ported version of render() m
|
+ destinationBus->zero(); |
+ return false; |
+ } |
+ |
+ // 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 |
Raymond Toy
2015/10/21 18:22:46
Comment is wrong? m_shouldSuspend isn't modified.
hongchan
2015/10/22 18:23:49
Oops. Done.
|
+ // current frame. |
+ if (context()->handlePreOfflineRenderTasks()) |
+ return true; |
+ |
+ // 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 false; |
+ } |
+ // 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); |
+ |
+ return false; |
+} |
+ |
// ---------------------------------------------------------------- |
OfflineAudioDestinationNode::OfflineAudioDestinationNode(AbstractAudioContext& context, AudioBuffer* renderTarget) |