OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/linux/pulse_output.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "media/audio/audio_parameters.h" |
| 9 #include "media/audio/audio_util.h" |
| 10 #include "media/audio/linux/audio_manager_linux.h" |
| 11 #include "media/base/data_buffer.h" |
| 12 #include "media/base/seekable_buffer.h" |
| 13 |
| 14 static pa_sample_format_t BitsToFormat(int bits_per_sample) { |
| 15 switch (bits_per_sample) { |
| 16 // Unsupported sample formats shown for reference. I am assuming we want |
| 17 // signed and little endian because that is what we gave to ALSA. |
| 18 case 8: |
| 19 return PA_SAMPLE_U8; |
| 20 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW |
| 21 case 16: |
| 22 return PA_SAMPLE_S16LE; |
| 23 // Also 16-bits: PA_SAMPLE_S16BE (big endian). |
| 24 case 24: |
| 25 return PA_SAMPLE_S24LE; |
| 26 // Also 24-bits: PA_SAMPLE_S24BE (big endian). |
| 27 // Other cases: PA_SAMPLE_24_32LE (in LSB of 32-bit field, little endian), |
| 28 // and PA_SAMPLE_24_32BE (in LSB of 32-bit field, big endian), |
| 29 case 32: |
| 30 return PA_SAMPLE_S32LE; |
| 31 // Also 32-bits: PA_SAMPLE_S32BE (big endian), |
| 32 // PA_SAMPLE_FLOAT32LE (floating point little endian), |
| 33 // and PA_SAMPLE_FLOAT32BE (floating point big endian). |
| 34 default: |
| 35 return PA_SAMPLE_INVALID; |
| 36 } |
| 37 } |
| 38 |
| 39 static size_t MicrosecondsToBytes( |
| 40 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { |
| 41 return microseconds * sample_rate * bytes_per_frame / |
| 42 base::Time::kMicrosecondsPerSecond; |
| 43 } |
| 44 |
| 45 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, |
| 46 void* state_addr) { |
| 47 pa_context_state_t* state = static_cast<pa_context_state_t*>(state_addr); |
| 48 *state = pa_context_get_state(context); |
| 49 } |
| 50 |
| 51 void PulseAudioOutputStream::WriteRequestCallback( |
| 52 pa_stream* playback_handle, size_t length, void* stream_addr) { |
| 53 PulseAudioOutputStream* stream = |
| 54 static_cast<PulseAudioOutputStream*>(stream_addr); |
| 55 |
| 56 DCHECK_EQ(stream->message_loop_, MessageLoop::current()); |
| 57 |
| 58 stream->write_callback_handled_ = true; |
| 59 |
| 60 // Fulfill write request. |
| 61 stream->FulfillWriteRequest(length); |
| 62 } |
| 63 |
| 64 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
| 65 AudioManagerLinux* manager, |
| 66 MessageLoop* message_loop) |
| 67 : channel_layout_(params.channel_layout), |
| 68 channel_count_(ChannelLayoutToChannelCount(channel_layout_)), |
| 69 sample_format_(BitsToFormat(params.bits_per_sample)), |
| 70 sample_rate_(params.sample_rate), |
| 71 bytes_per_frame_(params.channels * params.bits_per_sample / 8), |
| 72 manager_(manager), |
| 73 pa_context_(NULL), |
| 74 pa_mainloop_(NULL), |
| 75 playback_handle_(NULL), |
| 76 packet_size_(params.GetPacketSize()), |
| 77 frames_per_packet_(packet_size_ / bytes_per_frame_), |
| 78 client_buffer_(NULL), |
| 79 volume_(1.0f), |
| 80 stream_stopped_(true), |
| 81 write_callback_handled_(false), |
| 82 message_loop_(message_loop), |
| 83 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 84 source_callback_(NULL) { |
| 85 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 86 DCHECK(manager_); |
| 87 |
| 88 // TODO(slock): Sanity check input values. |
| 89 } |
| 90 |
| 91 PulseAudioOutputStream::~PulseAudioOutputStream() { |
| 92 // All internal structures should already have been freed in Close(), |
| 93 // which calls AudioManagerLinux::Release which deletes this object. |
| 94 DCHECK(!playback_handle_); |
| 95 DCHECK(!pa_context_); |
| 96 DCHECK(!pa_mainloop_); |
| 97 } |
| 98 |
| 99 bool PulseAudioOutputStream::Open() { |
| 100 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 101 |
| 102 // TODO(slock): Possibly move most of this to an OpenPlaybackDevice function |
| 103 // in a new class 'pulse_util', like alsa_util. |
| 104 |
| 105 // Create a mainloop API and connect to the default server. |
| 106 pa_mainloop_ = pa_mainloop_new(); |
| 107 pa_mainloop_api* pa_mainloop_api = pa_mainloop_get_api(pa_mainloop_); |
| 108 pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); |
| 109 pa_context_state_t pa_context_state = PA_CONTEXT_UNCONNECTED; |
| 110 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); |
| 111 |
| 112 // Wait until PulseAudio is ready. |
| 113 pa_context_set_state_callback(pa_context_, &ContextStateCallback, |
| 114 &pa_context_state); |
| 115 while (pa_context_state != PA_CONTEXT_READY) { |
| 116 pa_mainloop_iterate(pa_mainloop_, 1, NULL); |
| 117 if (pa_context_state == PA_CONTEXT_FAILED || |
| 118 pa_context_state == PA_CONTEXT_TERMINATED) { |
| 119 Reset(); |
| 120 return false; |
| 121 } |
| 122 } |
| 123 |
| 124 // Set sample specifications and open playback stream. |
| 125 pa_sample_spec pa_sample_specifications; |
| 126 pa_sample_specifications.format = sample_format_; |
| 127 pa_sample_specifications.rate = sample_rate_; |
| 128 pa_sample_specifications.channels = channel_count_; |
| 129 playback_handle_ = pa_stream_new(pa_context_, "Playback", |
| 130 &pa_sample_specifications, NULL); |
| 131 |
| 132 // Initialize client buffer. |
| 133 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; |
| 134 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); |
| 135 |
| 136 // Set write callback. |
| 137 pa_stream_set_write_callback(playback_handle_, &WriteRequestCallback, this); |
| 138 |
| 139 // Set server-side buffer attributes. |
| 140 // (uint32_t)-1 is the default and recommended value from PulseAudio's |
| 141 // documentation, found at: |
| 142 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h
tml. |
| 143 pa_buffer_attr pa_buffer_attributes; |
| 144 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1); |
| 145 pa_buffer_attributes.tlength = output_packet_size; |
| 146 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 147 pa_buffer_attributes.minreq = static_cast<uint32_t>(-1); |
| 148 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
| 149 |
| 150 // Connect playback stream. |
| 151 pa_stream_connect_playback(playback_handle_, NULL, |
| 152 &pa_buffer_attributes, |
| 153 (pa_stream_flags_t) |
| 154 (PA_STREAM_INTERPOLATE_TIMING | |
| 155 PA_STREAM_ADJUST_LATENCY | |
| 156 PA_STREAM_AUTO_TIMING_UPDATE), |
| 157 NULL, NULL); |
| 158 |
| 159 if (!playback_handle_) { |
| 160 Reset(); |
| 161 return false; |
| 162 } |
| 163 |
| 164 return true; |
| 165 } |
| 166 |
| 167 void PulseAudioOutputStream::Reset() { |
| 168 stream_stopped_ = true; |
| 169 |
| 170 // Close the stream. |
| 171 if (playback_handle_) { |
| 172 pa_stream_flush(playback_handle_, NULL, NULL); |
| 173 pa_stream_disconnect(playback_handle_); |
| 174 |
| 175 // Release PulseAudio structures. |
| 176 pa_stream_unref(playback_handle_); |
| 177 playback_handle_ = NULL; |
| 178 } |
| 179 if (pa_context_) { |
| 180 pa_context_unref(pa_context_); |
| 181 pa_context_ = NULL; |
| 182 } |
| 183 if (pa_mainloop_) { |
| 184 pa_mainloop_free(pa_mainloop_); |
| 185 pa_mainloop_ = NULL; |
| 186 } |
| 187 |
| 188 // Release internal buffer. |
| 189 client_buffer_.reset(); |
| 190 } |
| 191 |
| 192 void PulseAudioOutputStream::Close() { |
| 193 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 194 |
| 195 Reset(); |
| 196 |
| 197 // Signal to the manager that we're closed and can be removed. |
| 198 // This should be the last call in the function as it deletes "this". |
| 199 manager_->ReleaseOutputStream(this); |
| 200 } |
| 201 |
| 202 void PulseAudioOutputStream::WaitForWriteRequest() { |
| 203 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 204 |
| 205 if (stream_stopped_) |
| 206 return; |
| 207 |
| 208 // Iterate the PulseAudio mainloop. If PulseAudio doesn't request a write, |
| 209 // post a task to iterate the mainloop again. |
| 210 write_callback_handled_ = false; |
| 211 pa_mainloop_iterate(pa_mainloop_, 1, NULL); |
| 212 if (!write_callback_handled_) { |
| 213 message_loop_->PostTask( |
| 214 FROM_HERE, |
| 215 method_factory_.NewRunnableMethod( |
| 216 &PulseAudioOutputStream::WaitForWriteRequest)); |
| 217 } |
| 218 } |
| 219 |
| 220 bool PulseAudioOutputStream::BufferPacketFromSource() { |
| 221 uint32 buffer_delay = client_buffer_->forward_bytes(); |
| 222 pa_usec_t pa_latency_micros; |
| 223 int negative; |
| 224 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); |
| 225 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, |
| 226 sample_rate_, |
| 227 bytes_per_frame_); |
| 228 // TODO(slock): Deal with negative latency (negative == 1). This has yet |
| 229 // to happen in practice though. |
| 230 scoped_refptr<media::DataBuffer> packet = |
| 231 new media::DataBuffer(packet_size_); |
| 232 size_t packet_size = RunDataCallback(packet->GetWritableData(), |
| 233 packet->GetBufferSize(), |
| 234 AudioBuffersState(buffer_delay, |
| 235 hardware_delay)); |
| 236 |
| 237 if (packet_size == 0) |
| 238 return false; |
| 239 |
| 240 // TODO(slock): Swizzling and downmixing. |
| 241 media::AdjustVolume(packet->GetWritableData(), |
| 242 packet_size, |
| 243 channel_count_, |
| 244 bytes_per_frame_ / channel_count_, |
| 245 volume_); |
| 246 packet->SetDataSize(packet_size); |
| 247 // Add the packet to the buffer. |
| 248 client_buffer_->Append(packet); |
| 249 return true; |
| 250 } |
| 251 |
| 252 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 253 // If we have enough data to fulfill the request, we can finish the write. |
| 254 if (stream_stopped_) |
| 255 return; |
| 256 |
| 257 // Request more data from the source until we can fulfill the request or |
| 258 // fail to receive anymore data. |
| 259 bool buffering_successful = true; |
| 260 while (client_buffer_->forward_bytes() < requested_bytes && |
| 261 buffering_successful) { |
| 262 buffering_successful = BufferPacketFromSource(); |
| 263 } |
| 264 |
| 265 size_t bytes_written = 0; |
| 266 if (client_buffer_->forward_bytes() > 0) { |
| 267 // Try to fulfill the request by writing as many of the requested bytes to |
| 268 // the stream as we can. |
| 269 WriteToStream(requested_bytes, &bytes_written); |
| 270 } |
| 271 |
| 272 if (bytes_written < requested_bytes) { |
| 273 // We weren't able to buffer enough data to fulfill the request. Try to |
| 274 // fulfill the rest of the request later. |
| 275 message_loop_->PostTask( |
| 276 FROM_HERE, |
| 277 method_factory_.NewRunnableMethod( |
| 278 &PulseAudioOutputStream::FulfillWriteRequest, |
| 279 requested_bytes - bytes_written)); |
| 280 } else { |
| 281 // Continue playback. |
| 282 message_loop_->PostTask( |
| 283 FROM_HERE, |
| 284 method_factory_.NewRunnableMethod( |
| 285 &PulseAudioOutputStream::WaitForWriteRequest)); |
| 286 } |
| 287 } |
| 288 |
| 289 void PulseAudioOutputStream::WriteToStream(size_t bytes_to_write, |
| 290 size_t* bytes_written) { |
| 291 *bytes_written = 0; |
| 292 while (*bytes_written < bytes_to_write) { |
| 293 const uint8* chunk; |
| 294 size_t chunk_size; |
| 295 |
| 296 // Stop writing if there is no more data available. |
| 297 if (!client_buffer_->GetCurrentChunk(&chunk, &chunk_size)) |
| 298 break; |
| 299 |
| 300 // Write data to stream. |
| 301 pa_stream_write(playback_handle_, chunk, chunk_size, |
| 302 NULL, 0LL, PA_SEEK_RELATIVE); |
| 303 client_buffer_->Seek(chunk_size); |
| 304 *bytes_written += chunk_size; |
| 305 } |
| 306 } |
| 307 |
| 308 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 309 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 310 |
| 311 CHECK(callback); |
| 312 source_callback_ = callback; |
| 313 |
| 314 // Clear buffer, it might still have data in it. |
| 315 client_buffer_->Clear(); |
| 316 stream_stopped_ = false; |
| 317 |
| 318 // Start playback. |
| 319 message_loop_->PostTask( |
| 320 FROM_HERE, |
| 321 method_factory_.NewRunnableMethod( |
| 322 &PulseAudioOutputStream::WaitForWriteRequest)); |
| 323 } |
| 324 |
| 325 void PulseAudioOutputStream::Stop() { |
| 326 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 327 |
| 328 stream_stopped_ = true; |
| 329 } |
| 330 |
| 331 void PulseAudioOutputStream::SetVolume(double volume) { |
| 332 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 333 |
| 334 volume_ = static_cast<float>(volume); |
| 335 } |
| 336 |
| 337 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 338 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 339 |
| 340 *volume = volume_; |
| 341 } |
| 342 |
| 343 uint32 PulseAudioOutputStream::RunDataCallback( |
| 344 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { |
| 345 if (source_callback_) |
| 346 return source_callback_->OnMoreData(this, dest, max_size, buffers_state); |
| 347 |
| 348 return 0; |
| 349 } |
OLD | NEW |