Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 // Tell pulse audio we generally want callbacks of a certain size. Pulse |
| 157 // typically wants larger buffers up front but will honor our maxlength and | |
| 158 // minreq settings once it reaches steady state. | |
| 157 pa_buffer_attr pa_buffer_attributes; | 159 pa_buffer_attr pa_buffer_attributes; |
| 158 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); | 160 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); |
| 159 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); | 161 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); |
| 160 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); | 162 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 161 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); | 163 pa_buffer_attributes.tlength = static_cast<uint32_t>(-1); |
| 162 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | 164 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
|
no longer working on chromium
2013/02/28 08:56:09
by looking at http://freedesktop.org/software/puls
DaleCurtis
2013/03/01 00:08:17
Hmm I retested this with multiple streams and Puls
| |
| 163 | 165 |
| 164 // Connect playback stream. | 166 // Connect playback stream. |
| 165 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are | 167 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are |
| 166 // not using the native sample rate. We should always open the stream with | 168 // not using the native sample rate. We should always open the stream with |
| 167 // PA_STREAM_FIX_RATE and ensure this is true. | 169 // PA_STREAM_FIX_RATE and ensure this is true. |
| 168 RETURN_ON_FAILURE( | 170 RETURN_ON_FAILURE( |
| 169 pa_stream_connect_playback( | 171 pa_stream_connect_playback( |
| 170 pa_stream_, NULL, &pa_buffer_attributes, | 172 pa_stream_, NULL, &pa_buffer_attributes, |
| 171 static_cast<pa_stream_flags_t>( | 173 static_cast<pa_stream_flags_t>( |
| 172 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | | 174 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 233 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 232 | 234 |
| 233 Reset(); | 235 Reset(); |
| 234 | 236 |
| 235 // Signal to the manager that we're closed and can be removed. | 237 // 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". | 238 // This should be the last call in the function as it deletes "this". |
| 237 manager_->ReleaseOutputStream(this); | 239 manager_->ReleaseOutputStream(this); |
| 238 } | 240 } |
| 239 | 241 |
| 240 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 242 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 241 CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); | 243 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) { | 244 while (bytes_remaining > 0) { |
| 263 void* buffer = NULL; | 245 void* buffer = NULL; |
| 264 size_t bytes_to_fill = bytes_remaining; | 246 size_t bytes_to_fill = params_.GetBytesPerBuffer(); |
| 265 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); | 247 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); |
| 248 CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer())); | |
|
no longer working on chromium
2013/02/28 08:56:09
I agree these checks might be dangerous if we supp
DaleCurtis
2013/03/01 00:08:17
Mostly because I don't think we'll need to since w
| |
| 266 | 249 |
| 267 // In case PulseAudio gives us a bigger buffer than we want, cap our size. | 250 int frames_filled = 0; |
| 268 bytes_to_fill = std::min( | 251 if (source_callback_) { |
| 269 std::min(bytes_remaining, bytes_to_fill), | 252 uint32 hardware_delay = pulse::GetHardwareLatencyInBytes( |
| 270 static_cast<size_t>(params_.GetBytesPerBuffer())); | 253 pa_stream_, params_.sample_rate(), |
| 254 params_.GetBytesPerFrame()); | |
| 255 source_callback_->WaitTillDataReady(); | |
| 256 frames_filled = source_callback_->OnMoreData( | |
| 257 audio_bus_.get(), AudioBuffersState(0, hardware_delay)); | |
| 258 } | |
| 271 | 259 |
| 272 int frames_to_fill = bytes_to_fill / params_.GetBytesPerFrame();; | 260 // Zero any unfilled data so it plays back as silence. |
| 261 if (frames_filled < audio_bus_->frames()) { | |
| 262 audio_bus_->ZeroFramesPartial( | |
| 263 frames_filled, audio_bus_->frames() - frames_filled); | |
| 264 } | |
| 273 | 265 |
| 274 // Note: If this ever changes to output raw float the data must be clipped | 266 // 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. | 267 // and sanitized since it may come from an untrusted source such as NaCl. |
| 276 audio_bus_->ToInterleavedPartial( | 268 audio_bus_->ToInterleaved( |
| 277 current_frame, frames_to_fill, params_.bits_per_sample() / 8, buffer); | 269 audio_bus_->frames(), params_.bits_per_sample() / 8, buffer); |
| 278 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), | 270 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), |
| 279 params_.bits_per_sample() / 8, volume_); | 271 params_.bits_per_sample() / 8, volume_); |
| 280 | 272 |
| 281 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, | 273 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| 282 PA_SEEK_RELATIVE) < 0) { | 274 PA_SEEK_RELATIVE) < 0) { |
| 283 if (source_callback_) { | 275 if (source_callback_) { |
| 284 source_callback_->OnError(this, pa_context_errno(pa_context_)); | 276 source_callback_->OnError(this, pa_context_errno(pa_context_)); |
| 285 } | 277 } |
| 286 } | 278 } |
| 287 | 279 |
| 288 bytes_remaining -= bytes_to_fill; | 280 bytes_remaining -= bytes_to_fill; |
| 289 current_frame = frames_to_fill; | |
| 290 } | 281 } |
| 291 } | 282 } |
| 292 | 283 |
| 293 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | 284 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 294 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 285 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 295 CHECK(callback); | 286 CHECK(callback); |
| 296 CHECK(pa_stream_); | 287 CHECK(pa_stream_); |
| 297 | 288 |
| 298 AutoPulseLock auto_lock(pa_mainloop_); | 289 AutoPulseLock auto_lock(pa_mainloop_); |
| 299 | 290 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 volume_ = static_cast<float>(volume); | 330 volume_ = static_cast<float>(volume); |
| 340 } | 331 } |
| 341 | 332 |
| 342 void PulseAudioOutputStream::GetVolume(double* volume) { | 333 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 343 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 334 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 344 | 335 |
| 345 *volume = volume_; | 336 *volume = volume_; |
| 346 } | 337 } |
| 347 | 338 |
| 348 } // namespace media | 339 } // namespace media |
| OLD | NEW |