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..c2bd7fdea8980494957fb345ead142caab9b4016 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 { |
@@ -58,21 +61,27 @@ std::unique_ptr<AudioDestination> AudioDestination::Create( |
std::move(security_origin))); |
} |
-AudioDestination::AudioDestination(AudioIOCallback& callback, |
+AudioDestination::AudioDestination(AudioIOCallback& web_audio_render_callback, |
unsigned number_of_output_channels, |
const WebAudioLatencyHint& latency_hint, |
PassRefPtr<SecurityOrigin> security_origin) |
: number_of_output_channels_(number_of_output_channels), |
is_playing_(false), |
- callback_(callback), |
+ // callback_(callback), |
output_bus_(AudioBus::Create(number_of_output_channels, |
AudioUtilities::kRenderQuantumFrames, |
false)), |
- render_bus_(AudioBus::Create(number_of_output_channels, |
- AudioUtilities::kRenderQuantumFrames)), |
- fifo_(WTF::WrapUnique( |
- new PushPullFIFO(number_of_output_channels, kFIFOSize))), |
+ // render_bus_(AudioBus::Create(number_of_output_channels, |
+ // AudioUtilities::kRenderQuantumFrames)), |
frames_elapsed_(0) { |
+ // Create a thread |WebThread| for WebAudio graph rendering. |
+ rendering_thread_ = WTF::WrapUnique( |
+ Platform::Current()->CreateThread("WebAudio Rendering Thread")); |
+ |
+ fifo_ = WTF::WrapUnique(new PushPullFIFO(number_of_output_channels, kFIFOSize, |
+ rendering_thread_->ThreadId(), |
+ web_audio_render_callback)); |
+ |
// Create WebAudioDevice. blink::WebAudioDevice is designed to support the |
// local input (e.g. loopback from OS audio system), but Chromium's media |
// renderer does not support it currently. Thus, we use zero for the number |
@@ -97,6 +106,9 @@ void AudioDestination::Render(const WebVector<float*>& destination_data, |
double delay, |
double delay_timestamp, |
size_t prior_frames_skipped) { |
+ // This 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,51 +118,73 @@ void AudioDestination::Render(const WebVector<float*>& destination_data, |
if (!fifo_ || fifo_->length() < number_of_frames) |
o1ka
2017/04/13 08:36:53
Are you keeping silent returns?
hongchan
2017/04/14 16:31:48
This inexplicable case happens only on Android. If
|
return; |
- frames_elapsed_ -= std::min(frames_elapsed_, prior_frames_skipped); |
- double output_position = |
- frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) - |
- delay; |
- output_position_.position = output_position; |
- output_position_.timestamp = delay_timestamp; |
- output_position_received_timestamp_ = base::TimeTicks::Now(); |
- |
// 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| |
- // we do not want output position to get stuck so we promote it |
- // using the elapsed time from the moment it was initially obtained. |
- if (callback_buffer_size_ > AudioUtilities::kRenderQuantumFrames * 2) { |
- double delta = |
- (base::TimeTicks::Now() - output_position_received_timestamp_) |
- .InSecondsF(); |
- output_position_.position += delta; |
- output_position_.timestamp += delta; |
- } |
- |
- // Some implementations give only rough estimation of |delay| so |
- // we might have negative estimation |outputPosition| value. |
- if (output_position_.position < 0.0) |
- output_position_.position = 0.0; |
- |
- // Process WebAudio graph and push the rendered output to FIFO. |
- callback_.Render(nullptr, render_bus_.Get(), |
- AudioUtilities::kRenderQuantumFrames, output_position_); |
- fifo_->Push(render_bus_.Get()); |
- } |
+ rendering_thread_->GetWebTaskRunner()->PostTask( |
+ BLINK_FROM_HERE, |
+ CrossThreadBind(&AudioDestination::RequestRenderOnWebThread, |
+ CrossThreadUnretained(this), number_of_frames, delay, |
+ delay_timestamp, prior_frames_skipped)); |
fifo_->Pull(output_bus_.Get(), number_of_frames); |
+} |
+ |
+void AudioDestination::RequestRenderOnWebThread(size_t number_of_frames, |
+ double delay, |
+ double delay_timestamp, |
+ size_t prior_frames_skipped) { |
+ // This this method is called by WebAudio rendering thread. |
+ DCHECK(IsRenderingThread()); |
+ |
+ frames_elapsed_ -= std::min(frames_elapsed_, prior_frames_skipped); |
+ |
+ AudioIOPosition output_position; |
+ output_position.position = |
+ frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) - |
+ delay; |
+ output_position.timestamp = delay_timestamp; |
+ |
+ fifo_->FillRequestedFrames(number_of_frames, callback_buffer_size_, |
+ output_position); |
+ |
+ // double output_position = |
+ // frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) - |
+ // delay; |
o1ka
2017/04/13 08:36:53
It's really inconvenient to review patches with co
hongchan
2017/04/14 16:31:48
I am terribly sorry about this mistake. :(
|
+ // output_position_.position = output_position; |
+ // output_position_.timestamp = delay_timestamp; |
+ // output_position_received_timestamp_ = base::TimeTicks::Now(); |
+ |
+ // Number of frames to render via WebAudio graph. |frames_to_render > 0| means |
+ // the frames in FIFO is not enough to fulfill the requested frames from the |
+ // audio device. |
+ // size_t frames_to_render = fifo_->GetFramesToRender(number_of_frames); |
+ |
+ // 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| |
+ // // we do not want output position to get stuck so we promote it |
+ // // using the elapsed time from the moment it was initially obtained. |
+ // if (callback_buffer_size_ > AudioUtilities::kRenderQuantumFrames * 2) { |
+ // double delta = |
+ // (base::TimeTicks::Now() - output_position_received_timestamp_) |
+ // .InSecondsF(); |
+ // output_position_.position += delta; |
+ // output_position_.timestamp += delta; |
+ // } |
+ |
+ // // Some implementations give only rough estimation of |delay| so |
+ // // we might have negative estimation |outputPosition| value. |
+ // if (output_position_.position < 0.0) |
+ // output_position_.position = 0.0; |
+ |
+ // // Process WebAudio graph and push the rendered output to FIFO. |
+ // callback_.Render(nullptr, render_bus_.Get(), |
+ // AudioUtilities::kRenderQuantumFrames, output_position_); |
+ // fifo_->Push(render_bus_.Get()); |
+ // } |
frames_elapsed_ += number_of_frames; |
} |
@@ -204,4 +238,9 @@ bool AudioDestination::CheckBufferSize() { |
return is_buffer_size_valid; |
} |
+bool AudioDestination::IsRenderingThread() { |
+ return static_cast<ThreadIdentifier>(rendering_thread_->ThreadId()) == |
+ CurrentThread(); |
+} |
+ |
} // namespace blink |