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

Unified Diff: third_party/WebKit/Source/platform/audio/AudioDestination.cpp

Issue 2777903005: Add WebThread in AudioDestination to support AudioWorkletThread (Closed)
Patch Set: Remove callback between Destination and FIFO Created 3 years, 8 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/platform/audio/AudioDestination.cpp
diff --git a/third_party/WebKit/Source/platform/audio/AudioDestination.cpp b/third_party/WebKit/Source/platform/audio/AudioDestination.cpp
index ff053c2dd65a801f89c79d8f8588ba8f724af20e..324a2f32354398f08101567077ff905e4a16e1aa 100644
--- a/third_party/WebKit/Source/platform/audio/AudioDestination.cpp
+++ b/third_party/WebKit/Source/platform/audio/AudioDestination.cpp
@@ -29,7 +29,9 @@
#include "platform/audio/AudioDestination.h"
#include <memory>
+#include "platform/CrossThreadFunctional.h"
#include "platform/Histogram.h"
+#include "platform/WebTaskRunner.h"
#include "platform/audio/AudioUtilities.h"
#include "platform/audio/PushPullFIFO.h"
#include "platform/weborigin/SecurityOrigin.h"
@@ -37,6 +39,7 @@
#include "public/platform/Platform.h"
#include "public/platform/WebAudioLatencyHint.h"
#include "public/platform/WebSecurityOrigin.h"
+#include "public/platform/WebThread.h"
namespace blink {
@@ -72,6 +75,8 @@ AudioDestination::AudioDestination(AudioIOCallback& callback,
AudioUtilities::kRenderQuantumFrames)),
fifo_(WTF::WrapUnique(
new PushPullFIFO(number_of_output_channels, kFIFOSize))),
+ rendering_thread_(WTF::WrapUnique(
+ Platform::Current()->CreateThread("WebAudio Rendering Thread"))),
frames_elapsed_(0) {
// Create WebAudioDevice. blink::WebAudioDevice is designed to support the
// local input (e.g. loopback from OS audio system), but Chromium's media
@@ -97,6 +102,9 @@ void AudioDestination::Render(const WebVector<float*>& destination_data,
double delay,
double delay_timestamp,
size_t prior_frames_skipped) {
+ // This method is called by AudioDeviceThread.
+ DCHECK(!IsRenderingThread());
+
CHECK_EQ(destination_data.size(), number_of_output_channels_);
CHECK_EQ(number_of_frames, callback_buffer_size_);
@@ -106,6 +114,29 @@ void AudioDestination::Render(const WebVector<float*>& destination_data,
if (!fifo_ || fifo_->length() < number_of_frames)
return;
+ // Associate the destination data array with the output bus then fill the
+ // FIFO.
+ for (unsigned i = 0; i < number_of_output_channels_; ++i)
+ output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames);
+
+ size_t frames_to_render = fifo_->Pull(output_bus_.Get(), number_of_frames);
+
+ rendering_thread_->GetWebTaskRunner()->PostTask(
+ BLINK_FROM_HERE,
+ CrossThreadBind(&AudioDestination::RequestRenderOnWebThread,
+ CrossThreadUnretained(this),
+ number_of_frames, frames_to_render,
+ delay, delay_timestamp, prior_frames_skipped));
+}
+
+void AudioDestination::RequestRenderOnWebThread(size_t frames_requested,
+ size_t frames_to_render,
+ double delay,
+ double delay_timestamp,
+ size_t prior_frames_skipped) {
+ // This method is called by WebThread.
+ DCHECK(IsRenderingThread());
+
frames_elapsed_ -= std::min(frames_elapsed_, prior_frames_skipped);
double output_position =
frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) -
@@ -114,18 +145,6 @@ void AudioDestination::Render(const WebVector<float*>& destination_data,
output_position_.timestamp = delay_timestamp;
output_position_received_timestamp_ = base::TimeTicks::Now();
o1ka 2017/04/18 14:14:57 Same for |output_position_received_timestamp_|?
hongchan 2017/04/18 18:16:08 Done.
- // Associate the destination data array with the output bus then fill the
- // FIFO.
- for (unsigned i = 0; i < number_of_output_channels_; ++i)
- output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames);
-
- // Number of frames to render via WebAudio graph. |framesToRender > 0| means
- // the frames in FIFO is not enough to fulfill the requested frames from the
- // audio device.
- size_t frames_to_render = number_of_frames > fifo_->FramesAvailable()
- ? number_of_frames - fifo_->FramesAvailable()
- : 0;
-
for (size_t pushed_frames = 0; pushed_frames < frames_to_render;
pushed_frames += AudioUtilities::kRenderQuantumFrames) {
// If platform buffer is more than two times longer than |framesToProcess|
@@ -150,9 +169,7 @@ void AudioDestination::Render(const WebVector<float*>& destination_data,
fifo_->Push(render_bus_.Get());
}
- fifo_->Pull(output_bus_.Get(), number_of_frames);
-
- frames_elapsed_ += number_of_frames;
+ frames_elapsed_ += frames_requested;
}
void AudioDestination::Start() {
@@ -204,4 +221,9 @@ bool AudioDestination::CheckBufferSize() {
return is_buffer_size_valid;
}
+bool AudioDestination::IsRenderingThread() {
+ return static_cast<ThreadIdentifier>(rendering_thread_->ThreadId()) ==
+ CurrentThread();
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698