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 only want callbacks of a certain size. |
|
scherkus (not reviewing)
2013/02/28 01:20:13
is this comment still true?
DaleCurtis
2013/02/28 01:41:15
Updated.
| |
| 157 pa_buffer_attr pa_buffer_attributes; | 157 pa_buffer_attr pa_buffer_attributes; |
| 158 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); | 158 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); |
| 159 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); | 159 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); |
| 160 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); | 160 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 161 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); | 161 pa_buffer_attributes.tlength = static_cast<uint32_t>(-1); |
| 162 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | 162 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
| 163 | 163 |
| 164 // Connect playback stream. | 164 // Connect playback stream. |
| 165 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are | 165 // 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 | 166 // not using the native sample rate. We should always open the stream with |
| 167 // PA_STREAM_FIX_RATE and ensure this is true. | 167 // PA_STREAM_FIX_RATE and ensure this is true. |
| 168 RETURN_ON_FAILURE( | 168 RETURN_ON_FAILURE( |
| 169 pa_stream_connect_playback( | 169 pa_stream_connect_playback( |
| 170 pa_stream_, NULL, &pa_buffer_attributes, | 170 pa_stream_, NULL, &pa_buffer_attributes, |
| 171 static_cast<pa_stream_flags_t>( | 171 static_cast<pa_stream_flags_t>( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 231 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 232 | 232 |
| 233 Reset(); | 233 Reset(); |
| 234 | 234 |
| 235 // Signal to the manager that we're closed and can be removed. | 235 // 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". | 236 // This should be the last call in the function as it deletes "this". |
| 237 manager_->ReleaseOutputStream(this); | 237 manager_->ReleaseOutputStream(this); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 240 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 241 CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); | 241 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) { | 242 while (bytes_remaining > 0) { |
| 263 void* buffer = NULL; | 243 void* buffer = NULL; |
| 264 size_t bytes_to_fill = bytes_remaining; | 244 size_t bytes_to_fill = params_.GetBytesPerBuffer(); |
| 265 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); | 245 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); |
| 246 CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer())); | |
| 266 | 247 |
| 267 // In case PulseAudio gives us a bigger buffer than we want, cap our size. | 248 int frames_filled = 0; |
| 268 bytes_to_fill = std::min( | 249 if (source_callback_) { |
| 269 std::min(bytes_remaining, bytes_to_fill), | 250 uint32 hardware_delay = pulse::GetHardwareLatencyInBytes( |
| 270 static_cast<size_t>(params_.GetBytesPerBuffer())); | 251 pa_stream_, params_.sample_rate(), |
| 252 params_.GetBytesPerFrame()); | |
| 253 source_callback_->WaitTillDataReady(); | |
| 254 frames_filled = source_callback_->OnMoreData( | |
| 255 audio_bus_.get(), AudioBuffersState(0, hardware_delay)); | |
| 256 } | |
| 271 | 257 |
| 272 int frames_to_fill = bytes_to_fill / params_.GetBytesPerFrame();; | 258 // Zero any unfilled data so it plays back as silence. |
| 259 if (frames_filled < audio_bus_->frames()) { | |
| 260 audio_bus_->ZeroFramesPartial( | |
| 261 frames_filled, audio_bus_->frames() - frames_filled); | |
| 262 } | |
| 273 | 263 |
| 274 // Note: If this ever changes to output raw float the data must be clipped | 264 // 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. | 265 // and sanitized since it may come from an untrusted source such as NaCl. |
| 276 audio_bus_->ToInterleavedPartial( | 266 audio_bus_->ToInterleaved( |
| 277 current_frame, frames_to_fill, params_.bits_per_sample() / 8, buffer); | 267 audio_bus_->frames(), params_.bits_per_sample() / 8, buffer); |
| 278 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), | 268 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), |
| 279 params_.bits_per_sample() / 8, volume_); | 269 params_.bits_per_sample() / 8, volume_); |
| 280 | 270 |
| 281 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, | 271 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| 282 PA_SEEK_RELATIVE) < 0) { | 272 PA_SEEK_RELATIVE) < 0) { |
| 283 if (source_callback_) { | 273 if (source_callback_) { |
| 284 source_callback_->OnError(this, pa_context_errno(pa_context_)); | 274 source_callback_->OnError(this, pa_context_errno(pa_context_)); |
| 285 } | 275 } |
| 286 } | 276 } |
| 287 | 277 |
| 288 bytes_remaining -= bytes_to_fill; | 278 bytes_remaining -= bytes_to_fill; |
| 289 current_frame = frames_to_fill; | |
| 290 } | 279 } |
| 291 } | 280 } |
| 292 | 281 |
| 293 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | 282 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 294 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 283 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 295 CHECK(callback); | 284 CHECK(callback); |
| 296 CHECK(pa_stream_); | 285 CHECK(pa_stream_); |
| 297 | 286 |
| 298 AutoPulseLock auto_lock(pa_mainloop_); | 287 AutoPulseLock auto_lock(pa_mainloop_); |
| 299 | 288 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 volume_ = static_cast<float>(volume); | 328 volume_ = static_cast<float>(volume); |
| 340 } | 329 } |
| 341 | 330 |
| 342 void PulseAudioOutputStream::GetVolume(double* volume) { | 331 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 343 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 332 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 344 | 333 |
| 345 *volume = volume_; | 334 *volume = volume_; |
| 346 } | 335 } |
| 347 | 336 |
| 348 } // namespace media | 337 } // namespace media |
| OLD | NEW |