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

Side by Side Diff: third_party/WebKit/Source/platform/audio/AudioDestination.cpp

Issue 2853923002: Improve thread creation in plaform/audio/AudioDestination (Closed)
Patch Set: Addressing feedback Created 3 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/audio/AudioDestination.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 new AudioDestination(callback, number_of_output_channels, latency_hint, 60 new AudioDestination(callback, number_of_output_channels, latency_hint,
61 std::move(security_origin))); 61 std::move(security_origin)));
62 } 62 }
63 63
64 AudioDestination::AudioDestination(AudioIOCallback& callback, 64 AudioDestination::AudioDestination(AudioIOCallback& callback,
65 unsigned number_of_output_channels, 65 unsigned number_of_output_channels,
66 const WebAudioLatencyHint& latency_hint, 66 const WebAudioLatencyHint& latency_hint,
67 PassRefPtr<SecurityOrigin> security_origin) 67 PassRefPtr<SecurityOrigin> security_origin)
68 : number_of_output_channels_(number_of_output_channels), 68 : number_of_output_channels_(number_of_output_channels),
69 is_playing_(false), 69 is_playing_(false),
70 rendering_thread_(WTF::WrapUnique(
71 Platform::Current()->CreateThread("WebAudio Rendering Thread"))),
72 fifo_(WTF::WrapUnique( 70 fifo_(WTF::WrapUnique(
73 new PushPullFIFO(number_of_output_channels, kFIFOSize))), 71 new PushPullFIFO(number_of_output_channels, kFIFOSize))),
74 output_bus_(AudioBus::Create(number_of_output_channels, 72 output_bus_(AudioBus::Create(number_of_output_channels,
75 AudioUtilities::kRenderQuantumFrames, 73 AudioUtilities::kRenderQuantumFrames,
76 false)), 74 false)),
77 render_bus_(AudioBus::Create(number_of_output_channels, 75 render_bus_(AudioBus::Create(number_of_output_channels,
78 AudioUtilities::kRenderQuantumFrames)), 76 AudioUtilities::kRenderQuantumFrames)),
79 callback_(callback), 77 callback_(callback),
80 frames_elapsed_(0) { 78 frames_elapsed_(0) {
81 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the 79 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 if (!fifo_ || fifo_->length() < number_of_frames) 112 if (!fifo_ || fifo_->length() < number_of_frames)
115 return; 113 return;
116 114
117 // Associate the destination data array with the output bus then fill the 115 // Associate the destination data array with the output bus then fill the
118 // FIFO. 116 // FIFO.
119 for (unsigned i = 0; i < number_of_output_channels_; ++i) 117 for (unsigned i = 0; i < number_of_output_channels_; ++i)
120 output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames); 118 output_bus_->SetChannelMemory(i, destination_data[i], number_of_frames);
121 119
122 size_t frames_to_render = fifo_->Pull(output_bus_.Get(), number_of_frames); 120 size_t frames_to_render = fifo_->Pull(output_bus_.Get(), number_of_frames);
123 121
124 rendering_thread_->GetWebTaskRunner()->PostTask( 122 // TODO(hongchan): this check might be redundant, so consider removing later.
125 BLINK_FROM_HERE, 123 if (frames_to_render != 0 && rendering_thread_) {
126 CrossThreadBind(&AudioDestination::RequestRenderOnWebThread, 124 rendering_thread_->GetWebTaskRunner()->PostTask(
127 CrossThreadUnretained(this), 125 BLINK_FROM_HERE,
128 number_of_frames, frames_to_render, 126 CrossThreadBind(&AudioDestination::RequestRenderOnWebThread,
129 delay, delay_timestamp, prior_frames_skipped)); 127 CrossThreadUnretained(this), number_of_frames,
128 frames_to_render, delay, delay_timestamp,
129 prior_frames_skipped));
130 }
130 } 131 }
131 132
132 void AudioDestination::RequestRenderOnWebThread(size_t frames_requested, 133 void AudioDestination::RequestRenderOnWebThread(size_t frames_requested,
133 size_t frames_to_render, 134 size_t frames_to_render,
134 double delay, 135 double delay,
135 double delay_timestamp, 136 double delay_timestamp,
136 size_t prior_frames_skipped) { 137 size_t prior_frames_skipped) {
137 // This method is called by WebThread. 138 // This method is called by WebThread.
138 DCHECK(IsRenderingThread()); 139 DCHECK(IsRenderingThread());
139 140
(...skipping 24 matching lines...) Expand all
164 // Process WebAudio graph and push the rendered output to FIFO. 165 // Process WebAudio graph and push the rendered output to FIFO.
165 callback_.Render(nullptr, render_bus_.Get(), 166 callback_.Render(nullptr, render_bus_.Get(),
166 AudioUtilities::kRenderQuantumFrames, output_position); 167 AudioUtilities::kRenderQuantumFrames, output_position);
167 fifo_->Push(render_bus_.Get()); 168 fifo_->Push(render_bus_.Get());
168 } 169 }
169 170
170 frames_elapsed_ += frames_requested; 171 frames_elapsed_ += frames_requested;
171 } 172 }
172 173
173 void AudioDestination::Start() { 174 void AudioDestination::Start() {
175 DCHECK(IsMainThread());
176
177 // Start the "audio device" after the rendering thread is ready.
174 if (web_audio_device_ && !is_playing_) { 178 if (web_audio_device_ && !is_playing_) {
179 rendering_thread_ = WTF::WrapUnique(
180 Platform::Current()->CreateThread("WebAudio Rendering Thread"));
175 web_audio_device_->Start(); 181 web_audio_device_->Start();
176 is_playing_ = true; 182 is_playing_ = true;
177 } 183 }
178 } 184 }
179 185
180 void AudioDestination::Stop() { 186 void AudioDestination::Stop() {
187 DCHECK(IsMainThread());
188
189 // This assumes stopping the "audio device" is synchronous and dumping the
190 // rendering thread is safe after that.
181 if (web_audio_device_ && is_playing_) { 191 if (web_audio_device_ && is_playing_) {
182 web_audio_device_->Stop(); 192 web_audio_device_->Stop();
193 rendering_thread_.reset();
183 is_playing_ = false; 194 is_playing_ = false;
184 } 195 }
185 } 196 }
186 197
198 size_t AudioDestination::CallbackBufferSize() const {
199 DCHECK(IsMainThread());
200 return callback_buffer_size_;
201 }
202
203 bool AudioDestination::IsPlaying() {
204 DCHECK(IsMainThread());
205 return is_playing_;
206 }
207
208 int AudioDestination::FramesPerBuffer() const {
209 DCHECK(IsMainThread());
210 return web_audio_device_->FramesPerBuffer();
211 }
212
187 size_t AudioDestination::HardwareBufferSize() { 213 size_t AudioDestination::HardwareBufferSize() {
188 return Platform::Current()->AudioHardwareBufferSize(); 214 return Platform::Current()->AudioHardwareBufferSize();
189 } 215 }
190 216
191 float AudioDestination::HardwareSampleRate() { 217 float AudioDestination::HardwareSampleRate() {
192 return static_cast<float>(Platform::Current()->AudioHardwareSampleRate()); 218 return static_cast<float>(Platform::Current()->AudioHardwareSampleRate());
193 } 219 }
194 220
195 unsigned long AudioDestination::MaxChannelCount() { 221 unsigned long AudioDestination::MaxChannelCount() {
196 return static_cast<unsigned long>( 222 return static_cast<unsigned long>(
(...skipping 21 matching lines...) Expand all
218 DCHECK(is_buffer_size_valid); 244 DCHECK(is_buffer_size_valid);
219 return is_buffer_size_valid; 245 return is_buffer_size_valid;
220 } 246 }
221 247
222 bool AudioDestination::IsRenderingThread() { 248 bool AudioDestination::IsRenderingThread() {
223 return static_cast<ThreadIdentifier>(rendering_thread_->ThreadId()) == 249 return static_cast<ThreadIdentifier>(rendering_thread_->ThreadId()) ==
224 CurrentThread(); 250 CurrentThread();
225 } 251 }
226 252
227 } // namespace blink 253 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/audio/AudioDestination.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698