Chromium Code Reviews| Index: Source/modules/webaudio/OfflineAudioDestinationNode.cpp |
| diff --git a/Source/modules/webaudio/OfflineAudioDestinationNode.cpp b/Source/modules/webaudio/OfflineAudioDestinationNode.cpp |
| index 87374edd52f409f7dedee7624efbcd28db28c724..12da648c9c33b743a2fe5e145c1f7943c5a0efdf 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" |
| @@ -41,7 +42,8 @@ const size_t renderQuantumSize = 128; |
| OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node, AudioBuffer* renderTarget) |
| : AudioDestinationHandler(node, renderTarget->sampleRate()) |
| , m_renderTarget(renderTarget) |
| - , m_startedRendering(false) |
| + , m_framesProcessed(0) |
| + , m_framesToProcess(0) |
| { |
| m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuantumSize); |
| } |
| @@ -88,11 +90,16 @@ void OfflineAudioDestinationHandler::startRendering() |
| if (!m_renderTarget) |
| return; |
| - if (!m_startedRendering) { |
| - m_startedRendering = true; |
| + // Rendering was not started. Starting now. |
| + if (!m_renderThread) { |
| 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::startOfflineRendering, this))); |
| + return; |
| } |
| + |
| + // Rendering is already started, which implicitly means we resume the |
| + // rendering by calling |runOfflineRendering| on m_renderThread. |
| + m_renderThread->postTask(FROM_HERE, threadSafeBind(&OfflineAudioDestinationHandler::runOfflineRendering, this)); |
| } |
| void OfflineAudioDestinationHandler::stopRendering() |
| @@ -100,15 +107,18 @@ void OfflineAudioDestinationHandler::stopRendering() |
| ASSERT_NOT_REACHED(); |
| } |
| -void OfflineAudioDestinationHandler::offlineRender() |
| +size_t OfflineAudioDestinationHandler::quantizeTimeToRenderQuantum(double when) const |
| { |
| - offlineRenderInternal(); |
| - context()->handlePostRenderTasks(); |
| + ASSERT(when >= 0); |
| + |
| + size_t whenAsFrame = when * sampleRate(); |
| + return whenAsFrame - (whenAsFrame % renderQuantumSize); |
| } |
| -void OfflineAudioDestinationHandler::offlineRenderInternal() |
| +void OfflineAudioDestinationHandler::startOfflineRendering() |
| { |
| ASSERT(!isMainThread()); |
| + |
| ASSERT(m_renderBus); |
| if (!m_renderBus) |
| return; |
| @@ -128,36 +138,67 @@ void OfflineAudioDestinationHandler::offlineRenderInternal() |
| 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(); |
| + m_framesToProcess = m_renderTarget->length(); |
| + |
| + // Start rendering. |
| + runOfflineRendering(); |
| +} |
| + |
| +void OfflineAudioDestinationHandler::runOfflineRendering() |
| +{ |
| + ASSERT(!isMainThread()); |
| + |
| unsigned numberOfChannels = m_renderTarget->numberOfChannels(); |
| - unsigned n = 0; |
| - while (framesToProcess > 0) { |
| - // Render one render quantum. |
| + // If there is more to process and there is no suspension at the moment, |
| + // do continue to render quanta. If there is a suspend scheduled at the |
| + // current sample frame, stop the render loop and put the context into the |
| + // suspended state. Then calling OfflineAudioContext.resume() will pick up |
| + // the render loop again from where it suspended. |
|
Raymond Toy
2015/06/12 21:11:37
"it suspended" -> "it was suspended"
hongchan
2015/06/15 18:40:46
Done.
|
| + while (m_framesToProcess > 0 && !context()->shouldSuspendNow()) { |
| + |
| + // Render one render quantum. Note that this includes pre/post render |
| + // tasks from the online audio context. |
| render(0, m_renderBus.get(), renderQuantumSize); |
| - 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; |
| + } |
| + |
| + // Finish up the rendering loop if there is no more to process. |
| + if (m_framesToProcess <= 0) { |
|
Raymond Toy
2015/06/12 21:11:37
Isn't there a compiler warning here since m_frames
hongchan
2015/06/15 18:40:46
No. I haven't seen any warning. Also m_framesToPro
|
| + finishOfflineRendering(); |
| + return; |
| } |
| + // Otherwise resolve pending suspend promises. |
| + context()->resolvePendingSuspendPromises(); |
| +} |
| + |
| +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))); |
| + context()->executionContext()->postTask(FROM_HERE, createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplete, this)); |
| + |
| + // FIXME: this might not be necessary. |
| + // Perform post-render tasks once more. |
|
Raymond Toy
2015/06/12 21:11:37
Why do you think this is necessary? What processi
hongchan
2015/06/15 18:40:46
I just followed how it was structured before. I wi
Raymond Toy
2015/06/15 20:42:13
As I understand it, the offline render loop now ca
|
| + context()->handlePostRenderTasks(); |
| } |
| void OfflineAudioDestinationHandler::notifyComplete() |
| { |
| - // The AudioContext might be gone. |
| + // The OfflineAudioContext might be gone. |
| if (context()) |
| context()->fireCompletionEvent(); |
| } |