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 "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "media/audio/audio_parameters.h" | 7 #include "media/audio/audio_parameters.h" |
| 10 #include "media/audio/audio_util.h" | 8 #include "media/audio/audio_util.h" |
| 11 #if defined(OS_LINUX) | 9 #if defined(OS_LINUX) |
| 12 #include "media/audio/linux/audio_manager_linux.h" | 10 #include "media/audio/linux/audio_manager_linux.h" |
| 13 #elif defined(OS_OPENBSD) | 11 #elif defined(OS_OPENBSD) |
| 14 #include "media/audio/openbsd/audio_manager_openbsd.h" | 12 #include "media/audio/openbsd/audio_manager_openbsd.h" |
| 15 #endif | 13 #endif |
| 16 #include "media/base/data_buffer.h" | |
| 17 #include "media/base/seekable_buffer.h" | |
| 18 | 14 |
| 19 namespace media { | 15 namespace media { |
| 20 | 16 |
| 21 static pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { | 17 static pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { |
| 22 switch (bits_per_sample) { | 18 switch (bits_per_sample) { |
| 23 // Unsupported sample formats shown for reference. I am assuming we want | 19 // Unsupported sample formats shown for reference. I am assuming we want |
| 24 // signed and little endian because that is what we gave to ALSA. | 20 // signed and little endian because that is what we gave to ALSA. |
| 25 case 8: | 21 case 8: |
| 26 return PA_SAMPLE_U8; | 22 return PA_SAMPLE_U8; |
| 27 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW | 23 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 } | 105 } |
| 110 | 106 |
| 111 static size_t MicrosecondsToBytes( | 107 static size_t MicrosecondsToBytes( |
| 112 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { | 108 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { |
| 113 return microseconds * sample_rate * bytes_per_frame / | 109 return microseconds * sample_rate * bytes_per_frame / |
| 114 base::Time::kMicrosecondsPerSecond; | 110 base::Time::kMicrosecondsPerSecond; |
| 115 } | 111 } |
| 116 | 112 |
| 117 // static | 113 // static |
| 118 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, | 114 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, |
| 119 void* state_addr) { | 115 void* p_this) { |
| 120 pa_context_state_t* state = static_cast<pa_context_state_t*>(state_addr); | 116 // is pulse giving us callbacks for all contexts? |
| 121 *state = pa_context_get_state(context); | 117 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 118 stream->context_state_ = pa_context_get_state(stream->pa_context_); | |
| 119 } | |
| 120 | |
| 121 // static | |
| 122 void PulseAudioOutputStream::StreamStateCallback(pa_stream* stream, | |
| 123 void* p_this) { | |
| 124 // is pulse giving us callbacks for all streams? | |
| 125 PulseAudioOutputStream* stream_ptr = | |
| 126 static_cast<PulseAudioOutputStream*>(p_this); | |
| 127 stream_ptr->stream_state_ = pa_stream_get_state(stream_ptr->playback_handle_); | |
| 122 } | 128 } |
| 123 | 129 |
| 124 // static | 130 // static |
| 125 void PulseAudioOutputStream::WriteRequestCallback(pa_stream* playback_handle, | 131 void PulseAudioOutputStream::WriteRequestCallback(pa_stream* playback_handle, |
| 126 size_t length, | 132 size_t length, void* p_this) { |
| 127 void* stream_addr) { | 133 // Fulfill write request; must always result in a pa_stream_write() call. |
| 128 PulseAudioOutputStream* stream = | 134 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 129 reinterpret_cast<PulseAudioOutputStream*>(stream_addr); | |
| 130 | |
| 131 DCHECK(stream->manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 132 | |
| 133 stream->write_callback_handled_ = true; | |
| 134 | |
| 135 // Fulfill write request. | |
| 136 stream->FulfillWriteRequest(length); | 135 stream->FulfillWriteRequest(length); |
| 137 } | 136 } |
| 138 | 137 |
| 139 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | 138 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
| 140 AudioManagerPulse* manager) | 139 AudioManagerPulse* manager) |
| 141 : channel_layout_(params.channel_layout()), | 140 : params_(params), |
| 142 channel_count_(ChannelLayoutToChannelCount(channel_layout_)), | |
| 143 sample_format_(BitsToPASampleFormat(params.bits_per_sample())), | |
| 144 sample_rate_(params.sample_rate()), | |
| 145 bytes_per_frame_(params.GetBytesPerFrame()), | |
| 146 manager_(manager), | 141 manager_(manager), |
| 147 pa_context_(NULL), | 142 pa_context_(NULL), |
| 148 pa_mainloop_(NULL), | 143 pa_mainloop_(NULL), |
| 149 playback_handle_(NULL), | 144 playback_handle_(NULL), |
| 150 packet_size_(params.GetBytesPerBuffer()), | |
| 151 frames_per_packet_(packet_size_ / bytes_per_frame_), | |
| 152 client_buffer_(NULL), | |
| 153 volume_(1.0f), | 145 volume_(1.0f), |
| 154 stream_stopped_(true), | 146 stream_stopped_(true), |
| 155 write_callback_handled_(false), | |
| 156 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 157 source_callback_(NULL) { | 147 source_callback_(NULL) { |
| 158 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 148 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 159 | 149 |
| 160 // TODO(slock): Sanity check input values. | 150 CHECK(params_.IsValid()); |
| 151 audio_bus_ = AudioBus::Create(params_); | |
| 152 interleaved_audio_data_.reset(new uint8[params_.GetBytesPerBuffer()]); | |
| 161 } | 153 } |
| 162 | 154 |
| 163 PulseAudioOutputStream::~PulseAudioOutputStream() { | 155 PulseAudioOutputStream::~PulseAudioOutputStream() { |
| 164 // All internal structures should already have been freed in Close(), | 156 // All internal structures should already have been freed in Close(), |
| 165 // which calls AudioManagerPulse::Release which deletes this object. | 157 // which calls AudioManagerPulse::Release which deletes this object. |
| 166 DCHECK(!playback_handle_); | 158 DCHECK(!playback_handle_); |
| 167 DCHECK(!pa_context_); | 159 DCHECK(!pa_context_); |
| 168 DCHECK(!pa_mainloop_); | 160 DCHECK(!pa_mainloop_); |
| 169 } | 161 } |
| 170 | 162 |
| 171 bool PulseAudioOutputStream::Open() { | 163 bool PulseAudioOutputStream::Open() { |
| 172 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 164 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 173 | 165 |
| 174 // TODO(slock): Possibly move most of this to an OpenPlaybackDevice function | 166 // Create a mainloop API and connect to the default server. |
| 175 // in a new class 'pulse_util', like alsa_util. | 167 pa_mainloop_ = pa_threaded_mainloop_new(); |
| 168 CHECK(pa_mainloop_); | |
| 176 | 169 |
| 177 // Create a mainloop API and connect to the default server. | 170 pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_); |
| 178 pa_mainloop_ = pa_mainloop_new(); | |
| 179 pa_mainloop_api* pa_mainloop_api = pa_mainloop_get_api(pa_mainloop_); | |
| 180 pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); | 171 pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); |
| 181 pa_context_state_t pa_context_state = PA_CONTEXT_UNCONNECTED; | 172 CHECK(pa_context_); |
| 182 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); | 173 |
| 174 context_state_ = PA_CONTEXT_UNCONNECTED; | |
| 175 pa_context_set_state_callback(pa_context_, &ContextStateCallback, this); | |
| 176 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); | |
| 177 | |
| 178 pa_threaded_mainloop_start(pa_mainloop_); | |
| 183 | 179 |
| 184 // Wait until PulseAudio is ready. | 180 // Wait until PulseAudio is ready. |
| 185 pa_context_set_state_callback(pa_context_, &ContextStateCallback, | 181 while (context_state_ != PA_CONTEXT_READY) { |
|
scherkus (not reviewing)
2012/10/10 17:56:29
instead of the volatile funny business and state c
DaleCurtis
2012/10/10 18:19:05
Nice, didn't see that. Will convert. Even more cod
| |
| 186 &pa_context_state); | 182 if (context_state_ == PA_CONTEXT_FAILED || |
| 187 while (pa_context_state != PA_CONTEXT_READY) { | 183 context_state_ == PA_CONTEXT_TERMINATED) { |
| 188 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 189 if (pa_context_state == PA_CONTEXT_FAILED || | |
| 190 pa_context_state == PA_CONTEXT_TERMINATED) { | |
| 191 Reset(); | 184 Reset(); |
| 192 return false; | 185 return false; |
| 193 } | 186 } |
| 187 // Yukka yuk, context_state_ will be updated in the background. | |
| 188 // TODO(dalecurtis): Change this to a waitable event. | |
| 189 base::PlatformThread::YieldCurrentThread(); | |
| 194 } | 190 } |
| 195 | 191 |
| 196 // Set sample specifications. | 192 // Set sample specifications. |
| 197 pa_sample_spec pa_sample_specifications; | 193 pa_sample_spec pa_sample_specifications; |
| 198 pa_sample_specifications.format = sample_format_; | 194 pa_sample_specifications.format = BitsToPASampleFormat( |
| 199 pa_sample_specifications.rate = sample_rate_; | 195 params_.bits_per_sample()); |
| 200 pa_sample_specifications.channels = channel_count_; | 196 pa_sample_specifications.rate = params_.sample_rate(); |
| 197 pa_sample_specifications.channels = params_.channels(); | |
| 201 | 198 |
| 202 // Get channel mapping and open playback stream. | 199 // Get channel mapping and open playback stream. |
| 200 // TODO(dalecurtis): Is this section correct? | |
| 203 pa_channel_map* map = NULL; | 201 pa_channel_map* map = NULL; |
| 204 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( | 202 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( |
| 205 channel_layout_); | 203 params_.channel_layout()); |
| 206 if (source_channel_map.channels != 0) { | 204 if (source_channel_map.channels != 0) { |
| 207 // The source data uses a supported channel map so we will use it rather | 205 // The source data uses a supported channel map so we will use it rather |
| 208 // than the default channel map (NULL). | 206 // than the default channel map (NULL). |
| 209 map = &source_channel_map; | 207 map = &source_channel_map; |
| 210 } | 208 } |
| 211 playback_handle_ = pa_stream_new(pa_context_, "Playback", | 209 playback_handle_ = pa_stream_new(pa_context_, "Playback", |
| 212 &pa_sample_specifications, map); | 210 &pa_sample_specifications, map); |
| 211 if (!playback_handle_) { | |
| 212 Reset(); | |
| 213 return false; | |
| 214 } | |
| 213 | 215 |
| 214 // Initialize client buffer. | 216 // Setup callbacks. |
| 215 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; | 217 stream_state_ = PA_STREAM_READY; |
| 216 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); | 218 pa_stream_set_state_callback(playback_handle_, &StreamStateCallback, this); |
| 217 | |
| 218 // Set write callback. | |
| 219 pa_stream_set_write_callback(playback_handle_, &WriteRequestCallback, this); | 219 pa_stream_set_write_callback(playback_handle_, &WriteRequestCallback, this); |
| 220 | 220 |
| 221 // Set server-side buffer attributes. | 221 // Tell pulse audio we only want callbacks of a certain size. |
| 222 // (uint32_t)-1 is the default and recommended value from PulseAudio's | |
| 223 // documentation, found at: | |
| 224 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h tml. | |
| 225 pa_buffer_attr pa_buffer_attributes; | 222 pa_buffer_attr pa_buffer_attributes; |
| 226 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1); | 223 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); |
| 227 pa_buffer_attributes.tlength = output_packet_size; | 224 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); |
| 228 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); | 225 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); |
| 229 pa_buffer_attributes.minreq = static_cast<uint32_t>(-1); | 226 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); |
| 230 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | 227 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
| 231 | 228 |
| 232 // Connect playback stream. | 229 // Connect playback stream. |
| 233 pa_stream_connect_playback(playback_handle_, NULL, | 230 pa_stream_connect_playback(playback_handle_, NULL, |
| 234 &pa_buffer_attributes, | 231 &pa_buffer_attributes, |
| 235 (pa_stream_flags_t) | 232 (pa_stream_flags_t) |
| 236 (PA_STREAM_INTERPOLATE_TIMING | | 233 (PA_STREAM_INTERPOLATE_TIMING | |
| 237 PA_STREAM_ADJUST_LATENCY | | 234 PA_STREAM_ADJUST_LATENCY | |
| 238 PA_STREAM_AUTO_TIMING_UPDATE), | 235 PA_STREAM_AUTO_TIMING_UPDATE), |
| 239 NULL, NULL); | 236 NULL, NULL); |
| 240 | 237 |
| 241 if (!playback_handle_) { | 238 if (!playback_handle_) { |
| 242 Reset(); | 239 Reset(); |
| 243 return false; | 240 return false; |
| 244 } | 241 } |
| 245 | 242 |
| 243 while (stream_state_ != PA_STREAM_READY) { | |
|
scherkus (not reviewing)
2012/10/10 17:56:29
ditto
| |
| 244 if (stream_state_ == PA_STREAM_FAILED) { | |
| 245 Reset(); | |
| 246 return false; | |
| 247 } | |
| 248 // Yukka yuk, stream_state_ will be updated in the background. | |
| 249 // TODO(dalecurtis): Change this to a waitable event. | |
| 250 base::PlatformThread::YieldCurrentThread(); | |
| 251 } | |
| 252 | |
| 246 return true; | 253 return true; |
| 247 } | 254 } |
| 248 | 255 |
| 249 void PulseAudioOutputStream::Reset() { | 256 void PulseAudioOutputStream::Reset() { |
| 250 stream_stopped_ = true; | 257 stream_stopped_ = true; |
| 251 | 258 |
| 259 if (pa_mainloop_) | |
| 260 pa_threaded_mainloop_lock(pa_mainloop_); | |
| 261 | |
| 252 // Close the stream. | 262 // Close the stream. |
| 253 if (playback_handle_) { | 263 if (playback_handle_) { |
|
scherkus (not reviewing)
2012/10/10 17:56:29
can we make stronger guarantees over which objects
DaleCurtis
2012/10/10 18:19:05
I can add a if (!pa_mainloop) return early. Is tha
| |
| 264 pa_stream_set_state_callback(playback_handle_, NULL, NULL); | |
| 254 pa_stream_flush(playback_handle_, NULL, NULL); | 265 pa_stream_flush(playback_handle_, NULL, NULL); |
| 255 pa_stream_disconnect(playback_handle_); | |
| 256 | 266 |
| 257 // Release PulseAudio structures. | 267 // Release PulseAudio structures. |
| 268 pa_stream_disconnect(playback_handle_); | |
| 258 pa_stream_unref(playback_handle_); | 269 pa_stream_unref(playback_handle_); |
| 259 playback_handle_ = NULL; | 270 playback_handle_ = NULL; |
| 260 } | 271 } |
| 272 | |
| 261 if (pa_context_) { | 273 if (pa_context_) { |
| 274 pa_context_disconnect(pa_context_); | |
| 262 pa_context_unref(pa_context_); | 275 pa_context_unref(pa_context_); |
| 263 pa_context_ = NULL; | 276 pa_context_ = NULL; |
| 264 } | 277 } |
| 278 | |
| 279 if (pa_mainloop_) | |
| 280 pa_threaded_mainloop_unlock(pa_mainloop_); | |
| 281 | |
| 265 if (pa_mainloop_) { | 282 if (pa_mainloop_) { |
| 266 pa_mainloop_free(pa_mainloop_); | 283 pa_threaded_mainloop_stop(pa_mainloop_); |
| 284 pa_threaded_mainloop_free(pa_mainloop_); | |
| 267 pa_mainloop_ = NULL; | 285 pa_mainloop_ = NULL; |
| 268 } | 286 } |
| 269 | |
| 270 // Release internal buffer. | |
| 271 client_buffer_.reset(); | |
| 272 } | 287 } |
| 273 | 288 |
| 274 void PulseAudioOutputStream::Close() { | 289 void PulseAudioOutputStream::Close() { |
| 275 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 290 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 276 | 291 |
| 277 Reset(); | 292 Reset(); |
| 278 | 293 |
| 279 // Signal to the manager that we're closed and can be removed. | 294 // Signal to the manager that we're closed and can be removed. |
| 280 // This should be the last call in the function as it deletes "this". | 295 // This should be the last call in the function as it deletes "this". |
| 281 manager_->ReleaseOutputStream(this); | 296 manager_->ReleaseOutputStream(this); |
| 282 } | 297 } |
| 283 | 298 |
| 284 void PulseAudioOutputStream::WaitForWriteRequest() { | 299 int PulseAudioOutputStream::FillBuffer() { |
| 285 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 300 int negative = 0; |
| 301 pa_usec_t pa_latency_micros = 0; | |
| 302 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
| 303 uint32 hardware_delay = MicrosecondsToBytes( | |
| 304 pa_latency_micros, params_.sample_rate(), params_.GetBytesPerFrame()); | |
| 286 | 305 |
| 287 if (stream_stopped_) | 306 // TODO(dalecurtis): Deal with negative latency (negative == 1). This has yet |
| 288 return; | 307 // to happen in practice though. |
| 308 DCHECK(!negative); | |
| 289 | 309 |
| 290 // Iterate the PulseAudio mainloop. If PulseAudio doesn't request a write, | 310 int frames_filled = RunDataCallback( |
| 291 // post a task to iterate the mainloop again. | 311 audio_bus_.get(), AudioBuffersState(0, hardware_delay)); |
| 292 write_callback_handled_ = false; | |
| 293 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 294 if (!write_callback_handled_) { | |
| 295 manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | |
| 296 &PulseAudioOutputStream::WaitForWriteRequest, | |
| 297 weak_factory_.GetWeakPtr())); | |
| 298 } | |
| 299 } | |
| 300 | 312 |
| 301 bool PulseAudioOutputStream::BufferPacketFromSource() { | 313 int packet_size = frames_filled * params_.GetBytesPerFrame(); |
| 302 uint32 buffer_delay = client_buffer_->forward_bytes(); | 314 DCHECK_LE(packet_size, params_.GetBytesPerBuffer()); |
| 303 pa_usec_t pa_latency_micros; | |
| 304 int negative; | |
| 305 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
| 306 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, | |
| 307 sample_rate_, | |
| 308 bytes_per_frame_); | |
| 309 // TODO(slock): Deal with negative latency (negative == 1). This has yet | |
| 310 // to happen in practice though. | |
| 311 scoped_refptr<media::DataBuffer> packet = | |
| 312 new media::DataBuffer(packet_size_); | |
| 313 int frames_filled = RunDataCallback( | |
| 314 audio_bus_.get(), AudioBuffersState(buffer_delay, hardware_delay)); | |
| 315 size_t packet_size = frames_filled * bytes_per_frame_; | |
| 316 | 315 |
| 317 DCHECK_LE(packet_size, packet_size_); | 316 if (packet_size == 0) |
| 317 return 0; | |
| 318 | |
| 318 // Note: If this ever changes to output raw float the data must be clipped and | 319 // Note: If this ever changes to output raw float the data must be clipped and |
| 319 // sanitized since it may come from an untrusted source such as NaCl. | 320 // sanitized since it may come from an untrusted source such as NaCl. |
| 320 audio_bus_->ToInterleaved( | 321 audio_bus_->ToInterleaved( |
| 321 frames_filled, bytes_per_frame_ / channel_count_, | 322 frames_filled, params_.GetBytesPerFrame() / params_.channels(), |
| 322 packet->GetWritableData()); | 323 interleaved_audio_data_.get()); |
| 323 | 324 |
| 324 if (packet_size == 0) | 325 media::AdjustVolume(interleaved_audio_data_.get(), |
| 325 return false; | |
| 326 | |
| 327 media::AdjustVolume(packet->GetWritableData(), | |
| 328 packet_size, | 326 packet_size, |
| 329 channel_count_, | 327 params_.channels(), |
| 330 bytes_per_frame_ / channel_count_, | 328 params_.GetBytesPerFrame() / params_.channels(), |
| 331 volume_); | 329 volume_); |
| 332 packet->SetDataSize(packet_size); | 330 return packet_size; |
| 333 // Add the packet to the buffer. | |
| 334 client_buffer_->Append(packet); | |
| 335 return true; | |
| 336 } | 331 } |
| 337 | 332 |
| 338 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 333 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 334 int bytes_available = params_.GetBytesPerBuffer(); | |
| 335 | |
| 339 // If we have enough data to fulfill the request, we can finish the write. | 336 // If we have enough data to fulfill the request, we can finish the write. |
| 340 if (stream_stopped_) | 337 if (stream_stopped_ || !source_callback_) { |
| 341 return; | 338 memset(interleaved_audio_data_.get(), 0, params_.GetBytesPerBuffer()); |
| 339 } else { | |
| 340 CHECK_EQ(requested_bytes, static_cast<size_t>( | |
| 341 audio_bus_->frames() * params_.GetBytesPerFrame())); | |
| 342 | 342 |
| 343 // Request more data from the source until we can fulfill the request or | 343 int bytes_available = FillBuffer(); |
| 344 // fail to receive anymore data. | 344 if (bytes_available <= 0) { |
| 345 bool buffering_successful = true; | 345 memset(interleaved_audio_data_.get(), 0, params_.GetBytesPerBuffer()); |
| 346 size_t forward_bytes = static_cast<size_t>(client_buffer_->forward_bytes()); | 346 bytes_available = params_.GetBytesPerBuffer(); |
| 347 while (forward_bytes < requested_bytes && buffering_successful) { | 347 } |
| 348 buffering_successful = BufferPacketFromSource(); | |
| 349 } | 348 } |
| 350 | 349 |
| 351 size_t bytes_written = 0; | 350 pa_stream_write(playback_handle_, interleaved_audio_data_.get(), |
| 352 if (client_buffer_->forward_bytes() > 0) { | 351 bytes_available, NULL, 0LL, PA_SEEK_RELATIVE); |
| 353 // Try to fulfill the request by writing as many of the requested bytes to | |
| 354 // the stream as we can. | |
| 355 WriteToStream(requested_bytes, &bytes_written); | |
| 356 } | |
| 357 | |
| 358 if (bytes_written < requested_bytes) { | |
| 359 // We weren't able to buffer enough data to fulfill the request. Try to | |
| 360 // fulfill the rest of the request later. | |
| 361 manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | |
| 362 &PulseAudioOutputStream::FulfillWriteRequest, | |
| 363 weak_factory_.GetWeakPtr(), | |
| 364 requested_bytes - bytes_written)); | |
| 365 } else { | |
| 366 // Continue playback. | |
| 367 manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | |
| 368 &PulseAudioOutputStream::WaitForWriteRequest, | |
| 369 weak_factory_.GetWeakPtr())); | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 void PulseAudioOutputStream::WriteToStream(size_t bytes_to_write, | |
| 374 size_t* bytes_written) { | |
| 375 *bytes_written = 0; | |
| 376 while (*bytes_written < bytes_to_write) { | |
| 377 const uint8* chunk; | |
| 378 int chunk_size; | |
| 379 | |
| 380 // Stop writing if there is no more data available. | |
| 381 if (!client_buffer_->GetCurrentChunk(&chunk, &chunk_size)) | |
| 382 break; | |
| 383 | |
| 384 // Write data to stream. | |
| 385 pa_stream_write(playback_handle_, chunk, chunk_size, | |
| 386 NULL, 0LL, PA_SEEK_RELATIVE); | |
| 387 client_buffer_->Seek(chunk_size); | |
| 388 *bytes_written += chunk_size; | |
| 389 } | |
| 390 } | 352 } |
| 391 | 353 |
| 392 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | 354 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 393 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 355 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 394 CHECK(callback); | 356 CHECK(callback); |
| 395 DLOG_IF(ERROR, !playback_handle_) | 357 DLOG_IF(ERROR, !playback_handle_) |
| 396 << "Open() has not been called successfully"; | 358 << "Open() has not been called successfully"; |
| 397 if (!playback_handle_) | 359 if (!playback_handle_) |
| 398 return; | 360 return; |
| 399 | 361 |
| 400 source_callback_ = callback; | 362 source_callback_ = callback; |
| 401 | |
| 402 // Clear buffer, it might still have data in it. | |
| 403 client_buffer_->Clear(); | |
| 404 stream_stopped_ = false; | 363 stream_stopped_ = false; |
| 405 | |
| 406 // Start playback. | |
| 407 manager_->GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | |
| 408 &PulseAudioOutputStream::WaitForWriteRequest, | |
| 409 weak_factory_.GetWeakPtr())); | |
| 410 } | 364 } |
| 411 | 365 |
| 412 void PulseAudioOutputStream::Stop() { | 366 void PulseAudioOutputStream::Stop() { |
| 413 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 367 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 414 | 368 |
| 415 stream_stopped_ = true; | 369 stream_stopped_ = true; |
| 416 } | 370 } |
| 417 | 371 |
| 418 void PulseAudioOutputStream::SetVolume(double volume) { | 372 void PulseAudioOutputStream::SetVolume(double volume) { |
| 419 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 373 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 420 | 374 |
| 421 volume_ = static_cast<float>(volume); | 375 volume_ = static_cast<float>(volume); |
| 422 } | 376 } |
| 423 | 377 |
| 424 void PulseAudioOutputStream::GetVolume(double* volume) { | 378 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 425 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 379 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 426 | 380 |
| 427 *volume = volume_; | 381 *volume = volume_; |
| 428 } | 382 } |
| 429 | 383 |
| 430 int PulseAudioOutputStream::RunDataCallback( | 384 int PulseAudioOutputStream::RunDataCallback( |
| 431 AudioBus* audio_bus, AudioBuffersState buffers_state) { | 385 AudioBus* audio_bus, AudioBuffersState buffers_state) { |
| 432 if (source_callback_) | 386 if (source_callback_) |
| 433 return source_callback_->OnMoreData(audio_bus, buffers_state); | 387 return source_callback_->OnMoreData(audio_bus, buffers_state); |
| 434 | 388 |
| 435 return 0; | 389 return 0; |
| 436 } | 390 } |
| 437 | 391 |
| 438 } // namespace media | 392 } // namespace media |
| OLD | NEW |