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/linux/audio_manager_linux.h" | |
| 8 #include "media/base/data_buffer.h" | |
| 9 #include "media/base/seekable_buffer.h" | |
| 10 | |
| 11 void PulseAudioStateCallback(pa_context* c, void* userdata) { | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
All these callback methods should be static (to av
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: more descriptive variable name than "c"?
slock
2011/08/08 20:30:15
Done.
slock
2011/08/08 20:30:15
Done.
| |
| 12 // TODO(slock): Cover the rest of the states and integrate this state with the | |
| 13 // InternalState system. | |
| 14 pa_context_state_t state; | |
| 15 int* pa_context_ready = (int*)userdata; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
static_cast instead of C-style cast
slock
2011/08/08 20:30:15
Done.
| |
| 16 state = pa_context_get_state(c); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
pa_context_state_t state = ...
and remove line 14
slock
2011/08/08 20:30:15
Done.
| |
| 17 switch(state) { | |
| 18 default: | |
| 19 break; | |
| 20 case PA_CONTEXT_FAILED: | |
| 21 *pa_context_ready = 3; | |
| 22 break; | |
| 23 case PA_CONTEXT_TERMINATED: | |
| 24 *pa_context_ready = 2; | |
| 25 break; | |
| 26 case PA_CONTEXT_READY: | |
| 27 *pa_context_ready = 1; | |
| 28 break; | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 void WriteCallback(pa_stream* s, size_t length, void* userdata) { | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
So actually, I think you can make this a static pr
slock
2011/08/08 20:30:15
Done. That DID work, awesome. That's huge. Shou
| |
| 33 PulseAudioOutputStream* stream_ptr = | |
| 34 static_cast<PulseAudioOutputStream*>(userdata); | |
| 35 | |
| 36 // Request data from upstream if necessary. | |
| 37 while (stream_ptr->client_buffer_->forward_bytes() < length && | |
| 38 !stream_ptr->source_exhausted_) | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: {} around body of loop (multi-line condition)
slock
2011/08/08 20:30:15
Done.
| |
| 39 stream_ptr->BufferPacketInClient(); | |
| 40 | |
| 41 // Get data to write. | |
| 42 uint8 read_data[length]; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
variable-length arrays are no good! scoped_array i
slock
2011/08/08 20:30:15
Done.
| |
| 43 stream_ptr->client_buffer_->Read(read_data, length); | |
| 44 // Write to stream. | |
| 45 pa_stream_write(s, read_data, length, NULL, 0LL, PA_SEEK_RELATIVE); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: more descriptive variable name than "s"?
slock
2011/08/08 20:30:15
Done.
| |
| 46 } | |
| 47 | |
| 48 pa_sample_format_t BitsToFormat(int bits_per_sample) { | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
static
slock
2011/08/08 20:30:15
Done.
| |
| 49 switch(bits_per_sample) { | |
| 50 // Unsupported sample formats shown for reference. I am assuming we want | |
| 51 // signed and little endian because that is what we gave to ALSA. | |
| 52 case 8: | |
| 53 return PA_SAMPLE_U8; | |
| 54 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW | |
| 55 case 16: | |
| 56 return PA_SAMPLE_S16LE; | |
| 57 // Also 16-bits: PA_SAMPLE_S16BE (big endian). | |
| 58 case 24: | |
| 59 return PA_SAMPLE_S24LE; | |
| 60 // Also 24-bits: PA_SAMPLE_S24BE (big endian). | |
| 61 // Other cases: PA_SAMPLE_24_32LE (in LSBs of 32-bit field, little endian), | |
| 62 // and PA_SAMPLE_24_32BE (in LSBs of 32-bit field, big endian), | |
| 63 case 32: | |
| 64 return PA_SAMPLE_S32LE; | |
| 65 // Also 32-bits: PA_SAMPLE_S32BE (big endian), | |
| 66 // PA_SAMPLE_FLOAT32LE (floating point little endian), | |
| 67 // and PA_SAMPLE_FLOAT32BE (floating point big endian). | |
| 68 default: | |
| 69 return PA_SAMPLE_INVALID; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | |
| 74 AudioManagerLinux* manager) | |
| 75 : client_buffer_(NULL), | |
| 76 source_exhausted_(false), | |
| 77 channel_layout_(params.channel_layout), | |
| 78 sample_format_(BitsToFormat(params.bits_per_sample)), | |
| 79 sample_rate_(params.sample_rate), | |
| 80 bytes_per_sample_(params.bits_per_sample / 8), | |
| 81 bytes_per_frame_(params.channels * params.bits_per_sample / 8), | |
| 82 should_downmix_(false), | |
| 83 should_swizzle_(false), | |
| 84 packet_size_(params.GetPacketSize()), | |
| 85 stop_stream_(false), | |
| 86 manager_(manager), | |
| 87 pa_mainloop_(NULL), | |
| 88 pa_mainloop_api_(NULL), | |
| 89 pa_context_(NULL), | |
| 90 playback_handle_(NULL), | |
| 91 frames_per_packet_(packet_size_ / bytes_per_frame_), | |
| 92 state_(kCreated), | |
| 93 volume_(1.0f), | |
| 94 source_callback_(NULL) { | |
| 95 // TODO(slock): Sanity check input values. | |
| 96 } | |
| 97 | |
| 98 PulseAudioOutputStream::~PulseAudioOutputStream() { | |
| 99 // TODO(slock): Nothing to be done but state work. | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Delete comment
Also, here you *do* want to free y
slock
2011/08/08 20:30:15
Done. Note that AudioManagerLinux requires the ru
| |
| 100 } | |
| 101 | |
| 102 bool PulseAudioOutputStream::Open() { | |
| 103 // TODO(slock): Possibly move most of this to a OpenPlaybackDevice function in | |
| 104 // a new class 'pulse_util', like alsa_util. | |
| 105 | |
| 106 if (state() == kInError) | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
delete; state transitions are not implemented
slock
2011/08/08 20:30:15
Done.
| |
| 107 return false; | |
| 108 | |
| 109 // TODO(slock): Implement state transitions. | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
delete TODO
slock
2011/08/08 20:30:15
Done.
| |
| 110 | |
| 111 // Create a mainloop API and connect to the default server. | |
| 112 pa_mainloop_ = pa_mainloop_new(); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Never deleted; needs a call to pa_mainloop_free
slock
2011/08/08 20:30:15
Done.
| |
| 113 pa_mainloop_api_ = pa_mainloop_get_api(pa_mainloop_); | |
| 114 pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium"); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Never deleted; I believe you need to call pa_conte
slock
2011/08/08 20:30:15
Done.
| |
| 115 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); | |
| 116 | |
| 117 // Wait until PulseAudio is ready. | |
| 118 int pa_context_ready = 0; | |
| 119 pa_context_set_state_callback(pa_context_, &PulseAudioStateCallback, | |
| 120 &pa_context_ready); | |
| 121 while (pa_context_ready == 0 ){ | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: while (!pa_context_ready) and no {}
slock
2011/08/08 20:30:15
Done.
| |
| 122 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 123 } | |
| 124 | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Handle error condition (pa_context_ready != PA_CON
slock
2011/08/08 20:30:15
Done.
| |
| 125 // Set sample specifications and open playback stream. | |
| 126 pa_sample_specs_ = new pa_sample_spec; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
probably better to stack-allocate instead of creat
slock
2011/08/08 20:30:15
Done.
| |
| 127 pa_sample_specs_->format = sample_format_; | |
| 128 pa_sample_specs_->rate = sample_rate_; | |
| 129 pa_sample_specs_->channels = ChannelLayoutToChannelCount(channel_layout_); | |
| 130 playback_handle_ = pa_stream_new(pa_context_, "Playback", | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Need to delete (I believe pa_stream_unref)
slock
2011/08/08 20:30:15
Done.
| |
| 131 pa_sample_specs_, NULL); | |
| 132 | |
| 133 // Initialize client buffer. | |
| 134 bytes_per_output_frame_ = bytes_per_frame_; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
bytes_per_output_frame_ is an unnecessary field; r
slock
2011/08/08 20:30:15
Done.
| |
| 135 uint32 output_packet_size = frames_per_packet_ * bytes_per_output_frame_; | |
| 136 client_buffer_ = new media::SeekableBuffer(0, output_packet_size); | |
| 137 | |
| 138 // Set write callback. | |
| 139 pa_stream_set_write_callback(playback_handle_, WriteCallback, | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: put & before "WriteCallback" for clarity/cons
slock
2011/08/08 20:30:15
Done.
| |
| 140 this); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: move "this" to line above
slock
2011/08/08 20:30:15
Done.
| |
| 141 | |
| 142 // Set server side buffer attributes and connect playback stream. | |
| 143 // TODO(slock): Figure out what these values should actually be, recommended | |
| 144 // values from PulseAudio's documentation for now. | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
link to documentation where it gives these recomme
slock
2011/08/08 20:30:15
Done, but the url plus the "//" and indentation is
| |
| 145 pa_buffer_attributes_ = new pa_buffer_attr; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
stack-allocate instead of creating new
slock
2011/08/08 20:30:15
Done.
| |
| 146 pa_buffer_attributes_->maxlength = (uint32_t)-1; | |
| 147 pa_buffer_attributes_->tlength = output_packet_size; | |
| 148 pa_buffer_attributes_->prebuf = (uint32_t)-1; | |
| 149 pa_buffer_attributes_->minreq = (uint32_t)-1; | |
| 150 pa_buffer_attributes_->fragsize = (uint32_t)-1; | |
| 151 pa_stream_connect_playback(playback_handle_, NULL, | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
indentation
slock
2011/08/08 20:30:15
Done.
| |
| 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 // Finish initializing the stream if the device was opened successfully. | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Comment doesn't seem to match the block below?
slock
2011/08/08 20:30:15
Done.
| |
| 160 if (playback_handle_ == NULL) { | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: if (!playback_handle_) and no {}
slock
2011/08/08 20:30:15
Done.
| |
| 161 stop_stream_ = true; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
What does this accomplish? Also, wouldn't you want
slock
2011/08/08 20:30:15
Done.
| |
| 162 } | |
| 163 | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 void PulseAudioOutputStream::Close() { | |
| 168 // Close the device. | |
| 169 pa_stream_disconnect(playback_handle_); | |
| 170 | |
| 171 // Release stuff. | |
| 172 delete pa_sample_specs_; | |
| 173 delete pa_buffer_attributes_; | |
| 174 delete client_buffer_; | |
| 175 | |
| 176 // Stop everything. | |
| 177 stop_stream_ = true; | |
| 178 | |
| 179 // Signal to the manager that we're closed and can be removed. | |
| 180 // This should be the last call in the function as it deletes "this". | |
| 181 manager_->ReleaseOutputStream(this); | |
| 182 } | |
| 183 | |
| 184 void PulseAudioOutputStream::BufferPacketInClient() { | |
| 185 // If stopped, simulate a 0-length packet. | |
| 186 if (stop_stream_) { | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Is this ever reached?
BufferPacketInClient() is o
slock
2011/08/08 20:30:15
I'll remove this for now, but this is part of the
| |
| 187 client_buffer_->Clear(); | |
| 188 source_exhausted_ = true; | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 source_exhausted_ = false; | |
| 193 | |
| 194 // Request more data if we have more capacity. | |
| 195 if (client_buffer_->forward_capacity() > client_buffer_->forward_bytes()) { | |
| 196 | |
| 197 // Before making request to source for data we need to determine the delay | |
| 198 // (in bytes) for the requested data to be played. | |
| 199 uint32 buffer_delay = client_buffer_->forward_bytes(); | |
| 200 pa_usec_t pa_latency_micros; | |
| 201 int negative; | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Is it okay for negative to be unused in hardware_d
slock
2011/08/08 20:30:15
No, but its never been negative that I know of. I
| |
| 202 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
| 203 uint32 hardware_delay = MicrosToBytes(pa_latency_micros, sample_rate_, | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: Change "Micros" to "Microseconds"
slock
2011/08/08 20:30:15
Done.
| |
| 204 bytes_per_frame_); | |
| 205 scoped_refptr<media::DataBuffer> packet = | |
| 206 new media::DataBuffer(packet_size_); | |
| 207 size_t packet_size = RunDataCallback(packet->GetWritableData(), | |
| 208 packet->GetBufferSize(), | |
| 209 AudioBuffersState(buffer_delay, | |
| 210 hardware_delay)); | |
| 211 CHECK(packet_size <= packet->GetBufferSize()) << | |
| 212 "Data source overran buffer."; | |
| 213 | |
| 214 // This should not happen, but in case it does, drop any trailing bytes | |
| 215 // that aren't large enough to make a frame. Without this, packet writing | |
| 216 // may stall because the last few bytes in the packet may never get used by | |
| 217 // WritePacket. TODO(slocK): Ensure that this is relevant here, it might | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
"WritePacket" doesn't exist
slock
2011/08/08 20:30:15
Done. This wasn't relevant anyway because I don't
| |
| 218 // not be. | |
| 219 DCHECK(packet_size % bytes_per_frame_ == 0); | |
| 220 packet_size = (packet_size / bytes_per_frame_) * bytes_per_frame_; | |
| 221 | |
| 222 // TODO(slock): Swizzling, downmixing, and volume adjusting. | |
| 223 | |
| 224 if (packet_size > 0) { | |
| 225 packet->SetDataSize(packet_size); | |
| 226 // Add the packet to the buffer. | |
| 227 client_buffer_->Append(packet); | |
| 228 } else | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
nit: This time you *should* have {} around the els
slock
2011/08/08 20:30:15
Done.
| |
| 229 source_exhausted_ = true; | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void PulseAudioOutputStream::ClientBufferLoop() { | |
| 234 while(!stop_stream_ && !source_exhausted_) { | |
| 235 // As long as the stream is active, we should be buffering packets if need | |
| 236 // be and writing packets if need be. These are asynchronous processes. | |
| 237 // This loop buffers packets and the PulseAudio mainloop writes them. | |
| 238 // BufferPacket() only actually buffers under certain circumstances and | |
| 239 // pa_mainloop_iterate() only calls WriteCallback under certain | |
| 240 // circumstances, but the loop marches on in either case. | |
| 241 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | |
| 246 CHECK(callback); | |
| 247 set_source_callback(callback); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
inline this method
slock
2011/08/08 20:30:15
Done.
| |
| 248 | |
| 249 // Clear buffer, it might still have data in it. | |
| 250 client_buffer_->Clear(); | |
| 251 source_exhausted_ = false; | |
| 252 | |
| 253 // Start playing. | |
| 254 ClientBufferLoop(); | |
| 255 } | |
| 256 | |
| 257 void PulseAudioOutputStream::Stop() { | |
| 258 // Nothing to be done because InternalState not implemented. | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
Instead of trying to "port" the Alsa output stream
slock
2011/08/08 20:30:15
Done.
| |
| 259 // TODO(slock): Implement state transitions. | |
| 260 } | |
| 261 | |
| 262 void PulseAudioOutputStream::SetVolume(double volume) { | |
| 263 volume_ = static_cast<float>(volume); | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
TODO make necessary calls to PulseAudio to actuall
slock
2011/08/08 20:30:15
Done. I actually implemented this, but its not te
| |
| 264 } | |
| 265 | |
| 266 void PulseAudioOutputStream::GetVolume(double* volume) { | |
| 267 *volume = volume_; | |
| 268 } | |
| 269 | |
| 270 bool PulseAudioOutputStream::CanTransitionTo(InternalState to) { | |
| 271 // TODO(slock): Not implemented. | |
| 272 return false; | |
| 273 } | |
| 274 | |
| 275 PulseAudioOutputStream::InternalState | |
| 276 PulseAudioOutputStream::TransitionTo(InternalState to) { | |
| 277 // TODO(slock): Not implemented. | |
| 278 return state_; | |
| 279 } | |
| 280 | |
| 281 PulseAudioOutputStream::InternalState PulseAudioOutputStream::state() { | |
| 282 return state_; | |
| 283 } | |
| 284 | |
| 285 uint32 PulseAudioOutputStream::RunDataCallback( | |
| 286 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { | |
| 287 if (source_callback_) | |
| 288 return source_callback_->OnMoreData(this, dest, max_size, buffers_state); | |
| 289 | |
| 290 return 0; | |
| 291 } | |
| 292 | |
| 293 void PulseAudioOutputStream::RunErrorCallback(int code) { | |
| 294 NOTIMPLEMENTED(); | |
| 295 } | |
| 296 | |
| 297 size_t PulseAudioOutputStream::MicrosToBytes(uint32 micros, uint32 sample_rate, | |
|
vrk (LEFT CHROMIUM)
2011/08/05 15:02:26
file-static function?
slock
2011/08/08 20:30:15
Done.
| |
| 298 size_t bytes_per_frame) { | |
| 299 return micros * sample_rate * bytes_per_frame / | |
| 300 base::Time::kMicrosecondsPerSecond; | |
| 301 } | |
| 302 | |
| 303 void PulseAudioOutputStream::set_source_callback( | |
| 304 AudioSourceCallback* callback) { | |
| 305 source_callback_= callback; | |
| 306 } | |
| OLD | NEW |