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

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

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Promise Resolution Created 5 years, 7 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: Source/modules/webaudio/OfflineAudioDestinationNode.cpp
diff --git a/Source/modules/webaudio/OfflineAudioDestinationNode.cpp b/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
index 87374edd52f409f7dedee7624efbcd28db28c724..e8458511cda0b75e8caa0acdd0a73ac551168414 100644
--- a/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
+++ b/Source/modules/webaudio/OfflineAudioDestinationNode.cpp
@@ -28,6 +28,7 @@
#include "core/dom/CrossThreadTask.h"
#include "modules/webaudio/AudioContext.h"
+#include "modules/webaudio/OfflineAudioContext.h"
#include "platform/Task.h"
#include "platform/audio/AudioBus.h"
#include "platform/audio/HRTFDatabaseLoader.h"
@@ -36,14 +37,16 @@
namespace blink {
-const size_t renderQuantumSize = 128;
+const size_t kRenderQuantumSize = 128;
OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node, AudioBuffer* renderTarget)
: AudioDestinationHandler(node, renderTarget->sampleRate())
, m_renderTarget(renderTarget)
- , m_startedRendering(false)
+ , m_isRenderingStarted(false)
+ , m_framesProcessed(0)
+ , m_framesToProcess(0)
{
- m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuantumSize);
+ m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), kRenderQuantumSize);
}
PassRefPtr<OfflineAudioDestinationHandler> OfflineAudioDestinationHandler::create(AudioNode& node, AudioBuffer* renderTarget)
@@ -88,11 +91,23 @@ void OfflineAudioDestinationHandler::startRendering()
if (!m_renderTarget)
return;
- if (!m_startedRendering) {
- m_startedRendering = true;
+ // fprintf(stderr, "startRendering() called\n");
+
+ // Rendering was not started. Starting now.
+ if (!m_isRenderingStarted) {
+ m_isRenderingStarted = true;
m_renderThread = adoptPtr(Platform::current()->createThread("Offline Audio Renderer"));
- m_renderThread->postTask(FROM_HERE, new Task(threadSafeBind(&OfflineAudioDestinationHandler::offlineRender, PassRefPtr<OfflineAudioDestinationHandler>(this))));
+ m_renderThread->postTask(FROM_HERE,
+ new Task(threadSafeBind(&OfflineAudioDestinationHandler::initiateOfflineRendering, this)));
+ return;
}
+
+ // Rendering was already started, which implicitly means we resume the
+ // rendering by calling |renderNextQuantum| on m_renderThread.
+ // TO FIX: is there any corner case for this?
Raymond Toy 2015/05/19 22:04:46 I think blink style is FIXME.
hongchan 2015/05/20 22:09:41 Done.
+ m_renderThread->postTask(FROM_HERE,
+ threadSafeBind(&OfflineAudioDestinationHandler::renderNextQuantum, this));
+ return;
}
void OfflineAudioDestinationHandler::stopRendering()
@@ -100,15 +115,10 @@ void OfflineAudioDestinationHandler::stopRendering()
ASSERT_NOT_REACHED();
}
-void OfflineAudioDestinationHandler::offlineRender()
-{
- offlineRenderInternal();
- context()->handlePostRenderTasks();
-}
-
-void OfflineAudioDestinationHandler::offlineRenderInternal()
+void OfflineAudioDestinationHandler::initiateOfflineRendering()
{
ASSERT(!isMainThread());
+
ASSERT(m_renderBus);
if (!m_renderBus)
return;
@@ -123,36 +133,66 @@ void OfflineAudioDestinationHandler::offlineRenderInternal()
if (!channelsMatch)
return;
- bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize;
+ bool isRenderBusAllocated = m_renderBus->length() >= kRenderQuantumSize;
ASSERT(isRenderBusAllocated);
if (!isRenderBusAllocated)
return;
- // Break up the render target into smaller "render quantize" sized pieces.
- // Render until we're finished.
- size_t framesToProcess = m_renderTarget->length();
- unsigned numberOfChannels = m_renderTarget->numberOfChannels();
+ m_framesToProcess = m_renderTarget->length();
- unsigned n = 0;
- while (framesToProcess > 0) {
- // Render one render quantum.
- render(0, m_renderBus.get(), renderQuantumSize);
+ // Initiate rendering.
+ renderNextQuantum();
+}
- size_t framesAvailableToCopy = std::min(framesToProcess, renderQuantumSize);
+void OfflineAudioDestinationHandler::renderNextQuantum()
+{
+ ASSERT(!isMainThread());
- 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);
+ if (context()->suspendIfNecessary()) {
+ if (context()->executionContext()) {
+ context()->executionContext()->postTask(FROM_HERE,
+ createCrossThreadTask(&OfflineAudioDestinationHandler::notifySuspend, this));
}
+ return;
+ }
+
+ // Render one render quantum. Note that this includes pre/post render tasks.
+ render(0, m_renderBus.get(), kRenderQuantumSize);
- n += framesAvailableToCopy;
- framesToProcess -= framesAvailableToCopy;
+ unsigned numberOfChannels = m_renderTarget->numberOfChannels();
+ size_t framesAvailableToCopy = std::min(m_framesToProcess, kRenderQuantumSize);
+
+ for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
+ const float* source = m_renderBus->channel(channelIndex)->data();
+ float* destination = m_renderTarget->getChannelData(channelIndex)->data();
+ memcpy(destination + m_framesProcessed, source, sizeof(float) * framesAvailableToCopy);
}
+ m_framesProcessed += framesAvailableToCopy;
+ m_framesToProcess -= framesAvailableToCopy;
+
+ if (m_framesToProcess > 0)
+ renderNextQuantum();
+ else
+ finishOfflineRendering();
+}
+
+void OfflineAudioDestinationHandler::finishOfflineRendering()
+{
+ ASSERT(!isMainThread());
+
// Our work is done. Let the AudioContext know.
- if (context()->executionContext())
- context()->executionContext()->postTask(FROM_HERE, createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, PassRefPtr<OfflineAudioDestinationHandler>(this)));
+ if (context()->executionContext()) {
+ context()->executionContext()->postTask(FROM_HERE,
+ createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, this));
+ }
+}
+
+void OfflineAudioDestinationHandler::notifySuspend()
+{
+ // The AudioContext might be gone.
+ if (context())
+ context()->fireSuspendEvent();
}
void OfflineAudioDestinationHandler::notifyComplete()

Powered by Google App Engine
This is Rietveld 408576698