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