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

Side by Side 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 11 matching lines...) Expand all
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "platform/audio/AudioDestination.h" 29 #include "platform/audio/AudioDestination.h"
30 30
31 #include <memory> 31 #include <memory>
32 #include "platform/CrossThreadFunctional.h"
32 #include "platform/Histogram.h" 33 #include "platform/Histogram.h"
34 #include "platform/WebTaskRunner.h"
33 #include "platform/audio/AudioUtilities.h" 35 #include "platform/audio/AudioUtilities.h"
34 #include "platform/audio/PushPullFIFO.h" 36 #include "platform/audio/PushPullFIFO.h"
35 #include "platform/weborigin/SecurityOrigin.h" 37 #include "platform/weborigin/SecurityOrigin.h"
36 #include "platform/wtf/PtrUtil.h" 38 #include "platform/wtf/PtrUtil.h"
37 #include "public/platform/Platform.h" 39 #include "public/platform/Platform.h"
38 #include "public/platform/WebAudioLatencyHint.h" 40 #include "public/platform/WebAudioLatencyHint.h"
39 #include "public/platform/WebSecurityOrigin.h" 41 #include "public/platform/WebSecurityOrigin.h"
42 #include "public/platform/WebThread.h"
40 43
41 namespace blink { 44 namespace blink {
42 45
43 // FIFO Size. 46 // FIFO Size.
44 // 47 //
45 // TODO(hongchan): This was estimated based on the largest callback buffer size 48 // TODO(hongchan): This was estimated based on the largest callback buffer size
46 // that we would ever need. The current UMA stats indicates that this is, in 49 // that we would ever need. The current UMA stats indicates that this is, in
47 // fact, probably too small. There are Android devices out there with a size of 50 // fact, probably too small. There are Android devices out there with a size of
48 // 8000 or so. We might need to make this larger. See: crbug.com/670747 51 // 8000 or so. We might need to make this larger. See: crbug.com/670747
49 const size_t kFIFOSize = 8192; 52 const size_t kFIFOSize = 8192;
(...skipping 15 matching lines...) Expand all
65 : number_of_output_channels_(number_of_output_channels), 68 : number_of_output_channels_(number_of_output_channels),
66 is_playing_(false), 69 is_playing_(false),
67 callback_(callback), 70 callback_(callback),
68 output_bus_(AudioBus::Create(number_of_output_channels, 71 output_bus_(AudioBus::Create(number_of_output_channels,
69 AudioUtilities::kRenderQuantumFrames, 72 AudioUtilities::kRenderQuantumFrames,
70 false)), 73 false)),
71 render_bus_(AudioBus::Create(number_of_output_channels, 74 render_bus_(AudioBus::Create(number_of_output_channels,
72 AudioUtilities::kRenderQuantumFrames)), 75 AudioUtilities::kRenderQuantumFrames)),
73 fifo_(WTF::WrapUnique( 76 fifo_(WTF::WrapUnique(
74 new PushPullFIFO(number_of_output_channels, kFIFOSize))), 77 new PushPullFIFO(number_of_output_channels, kFIFOSize))),
78 rendering_thread_(WTF::WrapUnique(
79 Platform::Current()->CreateThread("WebAudio Rendering Thread"))),
75 frames_elapsed_(0) { 80 frames_elapsed_(0) {
76 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the 81 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the
77 // local input (e.g. loopback from OS audio system), but Chromium's media 82 // local input (e.g. loopback from OS audio system), but Chromium's media
78 // renderer does not support it currently. Thus, we use zero for the number 83 // renderer does not support it currently. Thus, we use zero for the number
79 // of input channels. 84 // of input channels.
80 web_audio_device_ = WTF::WrapUnique(Platform::Current()->CreateAudioDevice( 85 web_audio_device_ = WTF::WrapUnique(Platform::Current()->CreateAudioDevice(
81 0, number_of_output_channels, latency_hint, this, String(), 86 0, number_of_output_channels, latency_hint, this, String(),
82 std::move(security_origin))); 87 std::move(security_origin)));
83 DCHECK(web_audio_device_); 88 DCHECK(web_audio_device_);
84 89
85 callback_buffer_size_ = web_audio_device_->FramesPerBuffer(); 90 callback_buffer_size_ = web_audio_device_->FramesPerBuffer();
86 if (!CheckBufferSize()) { 91 if (!CheckBufferSize()) {
87 NOTREACHED(); 92 NOTREACHED();
88 } 93 }
89 } 94 }
90 95
91 AudioDestination::~AudioDestination() { 96 AudioDestination::~AudioDestination() {
92 Stop(); 97 Stop();
93 } 98 }
94 99
95 void AudioDestination::Render(const WebVector<float*>& destination_data, 100 void AudioDestination::Render(const WebVector<float*>& destination_data,
96 size_t number_of_frames, 101 size_t number_of_frames,
97 double delay, 102 double delay,
98 double delay_timestamp, 103 double delay_timestamp,
99 size_t prior_frames_skipped) { 104 size_t prior_frames_skipped) {
105 // This method is called by AudioDeviceThread.
106 DCHECK(!IsRenderingThread());
107
100 CHECK_EQ(destination_data.size(), number_of_output_channels_); 108 CHECK_EQ(destination_data.size(), number_of_output_channels_);
101 CHECK_EQ(number_of_frames, callback_buffer_size_); 109 CHECK_EQ(number_of_frames, callback_buffer_size_);
102 110
103 // Note that this method is called by AudioDeviceThread. If FIFO is not ready, 111 // Note that this method is called by AudioDeviceThread. If FIFO is not ready,
104 // or the requested render size is greater than FIFO size return here. 112 // or the requested render size is greater than FIFO size return here.
105 // (crbug.com/692423) 113 // (crbug.com/692423)
106 if (!fifo_ || fifo_->length() < number_of_frames) 114 if (!fifo_ || fifo_->length() < number_of_frames)
107 return; 115 return;
108 116
117 // Associate the destination data array with the output bus then fill the
118 // FIFO.
119 for (unsigned i = 0; i < number_of_output_channels_; ++i)
120 output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames);
121
122 size_t frames_to_render = fifo_->Pull(output_bus_.Get(), number_of_frames);
123
124 rendering_thread_->GetWebTaskRunner()->PostTask(
125 BLINK_FROM_HERE,
126 CrossThreadBind(&AudioDestination::RequestRenderOnWebThread,
127 CrossThreadUnretained(this),
128 number_of_frames, frames_to_render,
129 delay, delay_timestamp, prior_frames_skipped));
130 }
131
132 void AudioDestination::RequestRenderOnWebThread(size_t frames_requested,
133 size_t frames_to_render,
134 double delay,
135 double delay_timestamp,
136 size_t prior_frames_skipped) {
137 // This method is called by WebThread.
138 DCHECK(IsRenderingThread());
139
109 frames_elapsed_ -= std::min(frames_elapsed_, prior_frames_skipped); 140 frames_elapsed_ -= std::min(frames_elapsed_, prior_frames_skipped);
110 double output_position = 141 double output_position =
111 frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) - 142 frames_elapsed_ / static_cast<double>(web_audio_device_->SampleRate()) -
112 delay; 143 delay;
113 output_position_.position = output_position; 144 output_position_.position = output_position;
o1ka 2017/04/18 14:14:57 Shouldn't |output_position_| be just a local varia
hongchan 2017/04/18 18:16:08 Done.
114 output_position_.timestamp = delay_timestamp; 145 output_position_.timestamp = delay_timestamp;
115 output_position_received_timestamp_ = base::TimeTicks::Now(); 146 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.
116 147
117 // Associate the destination data array with the output bus then fill the
118 // FIFO.
119 for (unsigned i = 0; i < number_of_output_channels_; ++i)
120 output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames);
121
122 // Number of frames to render via WebAudio graph. |framesToRender > 0| means
123 // the frames in FIFO is not enough to fulfill the requested frames from the
124 // audio device.
125 size_t frames_to_render = number_of_frames > fifo_->FramesAvailable()
126 ? number_of_frames - fifo_->FramesAvailable()
127 : 0;
128
129 for (size_t pushed_frames = 0; pushed_frames < frames_to_render; 148 for (size_t pushed_frames = 0; pushed_frames < frames_to_render;
130 pushed_frames += AudioUtilities::kRenderQuantumFrames) { 149 pushed_frames += AudioUtilities::kRenderQuantumFrames) {
131 // If platform buffer is more than two times longer than |framesToProcess| 150 // If platform buffer is more than two times longer than |framesToProcess|
132 // we do not want output position to get stuck so we promote it 151 // we do not want output position to get stuck so we promote it
133 // using the elapsed time from the moment it was initially obtained. 152 // using the elapsed time from the moment it was initially obtained.
134 if (callback_buffer_size_ > AudioUtilities::kRenderQuantumFrames * 2) { 153 if (callback_buffer_size_ > AudioUtilities::kRenderQuantumFrames * 2) {
135 double delta = 154 double delta =
136 (base::TimeTicks::Now() - output_position_received_timestamp_) 155 (base::TimeTicks::Now() - output_position_received_timestamp_)
137 .InSecondsF(); 156 .InSecondsF();
138 output_position_.position += delta; 157 output_position_.position += delta;
139 output_position_.timestamp += delta; 158 output_position_.timestamp += delta;
140 } 159 }
141 160
142 // Some implementations give only rough estimation of |delay| so 161 // Some implementations give only rough estimation of |delay| so
143 // we might have negative estimation |outputPosition| value. 162 // we might have negative estimation |outputPosition| value.
144 if (output_position_.position < 0.0) 163 if (output_position_.position < 0.0)
145 output_position_.position = 0.0; 164 output_position_.position = 0.0;
146 165
147 // Process WebAudio graph and push the rendered output to FIFO. 166 // Process WebAudio graph and push the rendered output to FIFO.
148 callback_.Render(nullptr, render_bus_.Get(), 167 callback_.Render(nullptr, render_bus_.Get(),
149 AudioUtilities::kRenderQuantumFrames, output_position_); 168 AudioUtilities::kRenderQuantumFrames, output_position_);
150 fifo_->Push(render_bus_.Get()); 169 fifo_->Push(render_bus_.Get());
151 } 170 }
152 171
153 fifo_->Pull(output_bus_.Get(), number_of_frames); 172 frames_elapsed_ += frames_requested;
154
155 frames_elapsed_ += number_of_frames;
156 } 173 }
157 174
158 void AudioDestination::Start() { 175 void AudioDestination::Start() {
159 if (web_audio_device_ && !is_playing_) { 176 if (web_audio_device_ && !is_playing_) {
160 web_audio_device_->Start(); 177 web_audio_device_->Start();
161 is_playing_ = true; 178 is_playing_ = true;
162 } 179 }
163 } 180 }
164 181
165 void AudioDestination::Stop() { 182 void AudioDestination::Stop() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 hardware_buffer_size_histogram.Sample(HardwareBufferSize()); 214 hardware_buffer_size_histogram.Sample(HardwareBufferSize());
198 callback_buffer_size_histogram.Sample(callback_buffer_size_); 215 callback_buffer_size_histogram.Sample(callback_buffer_size_);
199 216
200 // Check if the requested buffer size is too large. 217 // Check if the requested buffer size is too large.
201 bool is_buffer_size_valid = 218 bool is_buffer_size_valid =
202 callback_buffer_size_ + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; 219 callback_buffer_size_ + AudioUtilities::kRenderQuantumFrames <= kFIFOSize;
203 DCHECK(is_buffer_size_valid); 220 DCHECK(is_buffer_size_valid);
204 return is_buffer_size_valid; 221 return is_buffer_size_valid;
205 } 222 }
206 223
224 bool AudioDestination::IsRenderingThread() {
225 return static_cast<ThreadIdentifier>(rendering_thread_->ThreadId()) ==
226 CurrentThread();
227 }
228
207 } // namespace blink 229 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698