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

Side by Side Diff: media/audio/pulse/pulse_output.cc

Issue 12328097: Always fully fill PulseAudio's requested buffer. Allow larger initial requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes. Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | 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 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/pulse/pulse_output.h" 5 #include "media/audio/pulse/pulse_output.h"
6 6
7 #include <pulse/pulseaudio.h> 7 #include <pulse/pulseaudio.h>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "media/audio/audio_manager_base.h" 10 #include "media/audio/audio_manager_base.h"
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 } 146 }
147 pa_stream_ = pa_stream_new( 147 pa_stream_ = pa_stream_new(
148 pa_context_, "Playback", &pa_sample_specifications, map); 148 pa_context_, "Playback", &pa_sample_specifications, map);
149 RETURN_ON_FAILURE(pa_stream_, "Failed to create PulseAudio stream."); 149 RETURN_ON_FAILURE(pa_stream_, "Failed to create PulseAudio stream.");
150 pa_stream_set_state_callback(pa_stream_, &StreamNotifyCallback, this); 150 pa_stream_set_state_callback(pa_stream_, &StreamNotifyCallback, this);
151 151
152 // Even though we start the stream corked below, PulseAudio will issue one 152 // Even though we start the stream corked below, PulseAudio will issue one
153 // stream request after setup. FulfillWriteRequest() must fulfill the write. 153 // stream request after setup. FulfillWriteRequest() must fulfill the write.
154 pa_stream_set_write_callback(pa_stream_, &StreamRequestCallback, this); 154 pa_stream_set_write_callback(pa_stream_, &StreamRequestCallback, this);
155 155
156 // Tell pulse audio we only want callbacks of a certain size. 156 // Pulse is very finicky with the small buffer sizes used by Chrome. The
157 // settings below are mostly found through trial and error. Essentially we
158 // want Pulse to auto size its internal buffers, but call us back nearly every
159 // |minreq| bytes. |tlength| should be a multiple of |minreq|; too low and
160 // Pulse will issue callbacks way too fast, too high and we don't get
161 // callbacks frequently enough.
157 pa_buffer_attr pa_buffer_attributes; 162 pa_buffer_attr pa_buffer_attributes;
158 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); 163 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1);
159 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); 164 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer();
160 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); 165 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1);
161 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); 166 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer() * 3;
162 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); 167 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1);
163 168
164 // Connect playback stream. 169 // Connect playback stream. Like pa_buffer_attr, the pa_stream_flags have a
165 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are 170 // huge impact on the performance of the stream and were chosen through trial
166 // not using the native sample rate. We should always open the stream with 171 // and error.
167 // PA_STREAM_FIX_RATE and ensure this is true.
168 RETURN_ON_FAILURE( 172 RETURN_ON_FAILURE(
169 pa_stream_connect_playback( 173 pa_stream_connect_playback(
170 pa_stream_, NULL, &pa_buffer_attributes, 174 pa_stream_, NULL, &pa_buffer_attributes,
171 static_cast<pa_stream_flags_t>( 175 static_cast<pa_stream_flags_t>(
176 PA_STREAM_FIX_RATE | PA_STREAM_INTERPOLATE_TIMING |
Sam Edwards 2013/05/22 23:25:03 All, I realize I'm a bit late to the party here (
172 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | 177 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE |
173 PA_STREAM_NOT_MONOTONIC | PA_STREAM_START_CORKED), 178 PA_STREAM_NOT_MONOTONIC | PA_STREAM_START_CORKED),
174 NULL, NULL) == 0, 179 NULL, NULL) == 0,
175 "Failed to connect PulseAudio stream."); 180 "Failed to connect PulseAudio stream.");
176 181
177 // Wait for the stream to be ready. 182 // Wait for the stream to be ready.
178 while (true) { 183 while (true) {
179 pa_stream_state_t stream_state = pa_stream_get_state(pa_stream_); 184 pa_stream_state_t stream_state = pa_stream_get_state(pa_stream_);
180 RETURN_ON_FAILURE( 185 RETURN_ON_FAILURE(
181 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state."); 186 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state.");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); 236 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread());
232 237
233 Reset(); 238 Reset();
234 239
235 // Signal to the manager that we're closed and can be removed. 240 // Signal to the manager that we're closed and can be removed.
236 // This should be the last call in the function as it deletes "this". 241 // This should be the last call in the function as it deletes "this".
237 manager_->ReleaseOutputStream(this); 242 manager_->ReleaseOutputStream(this);
238 } 243 }
239 244
240 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { 245 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) {
241 CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); 246 int bytes_remaining = requested_bytes;
242
243 int frames_filled = 0;
244 if (source_callback_) {
245 uint32 hardware_delay = pulse::GetHardwareLatencyInBytes(
246 pa_stream_, params_.sample_rate(),
247 params_.GetBytesPerFrame());
248 frames_filled = source_callback_->OnMoreData(
249 audio_bus_.get(), AudioBuffersState(0, hardware_delay));
250 }
251
252 // Zero any unfilled data so it plays back as silence.
253 if (frames_filled < audio_bus_->frames()) {
254 audio_bus_->ZeroFramesPartial(
255 frames_filled, audio_bus_->frames() - frames_filled);
256 }
257
258 // PulseAudio won't always be able to provide a buffer large enough, so we may
259 // need to request multiple buffers and fill them individually.
260 int current_frame = 0;
261 size_t bytes_remaining = requested_bytes;
262 while (bytes_remaining > 0) { 247 while (bytes_remaining > 0) {
263 void* buffer = NULL; 248 void* buffer = NULL;
264 size_t bytes_to_fill = bytes_remaining; 249 size_t bytes_to_fill = params_.GetBytesPerBuffer();
265 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); 250 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0);
251 CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer()));
266 252
267 // In case PulseAudio gives us a bigger buffer than we want, cap our size. 253 int frames_filled = 0;
268 bytes_to_fill = std::min( 254 if (source_callback_) {
269 std::min(bytes_remaining, bytes_to_fill), 255 uint32 hardware_delay = pulse::GetHardwareLatencyInBytes(
270 static_cast<size_t>(params_.GetBytesPerBuffer())); 256 pa_stream_, params_.sample_rate(),
257 params_.GetBytesPerFrame());
258 source_callback_->WaitTillDataReady();
259 frames_filled = source_callback_->OnMoreData(
260 audio_bus_.get(), AudioBuffersState(0, hardware_delay));
261 }
271 262
272 int frames_to_fill = bytes_to_fill / params_.GetBytesPerFrame();; 263 // Zero any unfilled data so it plays back as silence.
264 if (frames_filled < audio_bus_->frames()) {
265 audio_bus_->ZeroFramesPartial(
266 frames_filled, audio_bus_->frames() - frames_filled);
267 }
273 268
274 // Note: If this ever changes to output raw float the data must be clipped 269 // Note: If this ever changes to output raw float the data must be clipped
275 // and sanitized since it may come from an untrusted source such as NaCl. 270 // and sanitized since it may come from an untrusted source such as NaCl.
276 audio_bus_->ToInterleavedPartial( 271 audio_bus_->ToInterleaved(
277 current_frame, frames_to_fill, params_.bits_per_sample() / 8, buffer); 272 audio_bus_->frames(), params_.bits_per_sample() / 8, buffer);
278 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), 273 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(),
279 params_.bits_per_sample() / 8, volume_); 274 params_.bits_per_sample() / 8, volume_);
280 275
281 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, 276 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL,
282 PA_SEEK_RELATIVE) < 0) { 277 PA_SEEK_RELATIVE) < 0) {
283 if (source_callback_) { 278 if (source_callback_) {
284 source_callback_->OnError(this, pa_context_errno(pa_context_)); 279 source_callback_->OnError(this, pa_context_errno(pa_context_));
285 } 280 }
286 } 281 }
287 282
288 bytes_remaining -= bytes_to_fill; 283 bytes_remaining -= bytes_to_fill;
289 current_frame = frames_to_fill;
290 } 284 }
291 } 285 }
292 286
293 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { 287 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) {
294 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); 288 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread());
295 CHECK(callback); 289 CHECK(callback);
296 CHECK(pa_stream_); 290 CHECK(pa_stream_);
297 291
298 AutoPulseLock auto_lock(pa_mainloop_); 292 AutoPulseLock auto_lock(pa_mainloop_);
299 293
(...skipping 12 matching lines...) Expand all
312 WaitForOperationCompletion(pa_mainloop_, operation); 306 WaitForOperationCompletion(pa_mainloop_, operation);
313 } 307 }
314 308
315 void PulseAudioOutputStream::Stop() { 309 void PulseAudioOutputStream::Stop() {
316 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); 310 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread());
317 311
318 // Cork (pause) the stream. Waiting for the main loop lock will ensure 312 // Cork (pause) the stream. Waiting for the main loop lock will ensure
319 // outstanding callbacks have completed. 313 // outstanding callbacks have completed.
320 AutoPulseLock auto_lock(pa_mainloop_); 314 AutoPulseLock auto_lock(pa_mainloop_);
321 315
316 // Set |source_callback_| to NULL so all FulfillWriteRequest() calls which may
DaleCurtis 2013/03/01 00:08:17 Also discovered some shutdown hangs here due to Wa
no longer working on chromium 2013/03/01 20:44:13 makes sense.
317 // occur while waiting on the flush and cork exit immediately.
318 source_callback_ = NULL;
319
322 // Flush the stream prior to cork, doing so after will cause hangs. Write 320 // Flush the stream prior to cork, doing so after will cause hangs. Write
323 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this 321 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this
324 // is all thread safe. 322 // is all thread safe.
325 pa_operation* operation = pa_stream_flush( 323 pa_operation* operation = pa_stream_flush(
326 pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_); 324 pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_);
327 WaitForOperationCompletion(pa_mainloop_, operation); 325 WaitForOperationCompletion(pa_mainloop_, operation);
328 326
329 operation = pa_stream_cork(pa_stream_, 1, &pulse::StreamSuccessCallback, 327 operation = pa_stream_cork(pa_stream_, 1, &pulse::StreamSuccessCallback,
330 pa_mainloop_); 328 pa_mainloop_);
331 WaitForOperationCompletion(pa_mainloop_, operation); 329 WaitForOperationCompletion(pa_mainloop_, operation);
332
333 source_callback_ = NULL;
334 } 330 }
335 331
336 void PulseAudioOutputStream::SetVolume(double volume) { 332 void PulseAudioOutputStream::SetVolume(double volume) {
337 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); 333 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread());
338 334
339 volume_ = static_cast<float>(volume); 335 volume_ = static_cast<float>(volume);
340 } 336 }
341 337
342 void PulseAudioOutputStream::GetVolume(double* volume) { 338 void PulseAudioOutputStream::GetVolume(double* volume) {
343 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); 339 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread());
344 340
345 *volume = volume_; 341 *volume = volume_;
346 } 342 }
347 343
348 } // namespace media 344 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698