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 static pa_sample_format_t BitsToFormat(int bits_per_sample) { | |
| 12 switch(bits_per_sample) { | |
| 13 // Unsupported sample formats shown for reference. I am assuming we want | |
| 14 // signed and little endian because that is what we gave to ALSA. | |
| 15 case 8: | |
| 16 return PA_SAMPLE_U8; | |
| 17 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW | |
| 18 case 16: | |
| 19 return PA_SAMPLE_S16LE; | |
| 20 // Also 16-bits: PA_SAMPLE_S16BE (big endian). | |
| 21 case 24: | |
| 22 return PA_SAMPLE_S24LE; | |
| 23 // Also 24-bits: PA_SAMPLE_S24BE (big endian). | |
| 24 // Other cases: PA_SAMPLE_24_32LE (in LSBs of 32-bit field, little endian), | |
| 25 // and PA_SAMPLE_24_32BE (in LSBs of 32-bit field, big endian), | |
| 26 case 32: | |
| 27 return PA_SAMPLE_S32LE; | |
| 28 // Also 32-bits: PA_SAMPLE_S32BE (big endian), | |
| 29 // PA_SAMPLE_FLOAT32LE (floating point little endian), | |
| 30 // and PA_SAMPLE_FLOAT32BE (floating point big endian). | |
| 31 default: | |
| 32 return PA_SAMPLE_INVALID; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 static size_t MicrosecondsToBytes( | |
| 37 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { | |
| 38 return microseconds * sample_rate * bytes_per_frame / | |
| 39 base::Time::kMicrosecondsPerSecond; | |
| 40 } | |
| 41 | |
| 42 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, | |
| 43 void* userdata) { | |
| 44 int* context_ready = static_cast<int*>(userdata); | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
Change int* to pa_context_state_t*, then just do
slock
2011/08/10 22:41:04
Done.
| |
| 45 pa_context_state_t state = pa_context_get_state(context); | |
| 46 switch(state) { | |
| 47 default: | |
| 48 break; | |
| 49 case PA_CONTEXT_FAILED: | |
| 50 *context_ready = 3; | |
| 51 case PA_CONTEXT_TERMINATED: | |
| 52 *context_ready = 2; | |
| 53 case PA_CONTEXT_READY: | |
| 54 *context_ready = 0; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 void PulseAudioOutputStream::WriteCallback(pa_stream* stream, size_t length, | |
| 59 void* userdata) { | |
| 60 PulseAudioOutputStream* stream_ptr = | |
| 61 static_cast<PulseAudioOutputStream*>(userdata); | |
| 62 | |
| 63 // Request data from upstream if necessary. | |
| 64 while (stream_ptr->client_buffer_->forward_bytes() < length && | |
| 65 !stream_ptr->source_exhausted_) { | |
| 66 stream_ptr->BufferPacketInClient(); | |
| 67 } | |
| 68 | |
| 69 // Get data to write. | |
| 70 scoped_array<uint8> read_data(new uint8[length]); | |
| 71 stream_ptr->client_buffer_->Read(read_data.get(), length); | |
| 72 // Write to stream. | |
| 73 pa_stream_write(stream, read_data.get(), length, NULL, 0LL, PA_SEEK_RELATIVE); | |
| 74 } | |
| 75 | |
| 76 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | |
| 77 AudioManagerLinux* manager) | |
| 78 : channel_layout_(params.channel_layout), | |
| 79 sample_format_(BitsToFormat(params.bits_per_sample)), | |
| 80 sample_rate_(params.sample_rate), | |
| 81 bytes_per_frame_(params.channels * params.bits_per_sample / 8), | |
| 82 packet_size_(params.GetPacketSize()), | |
| 83 frames_per_packet_(packet_size_ / bytes_per_frame_), | |
| 84 stream_stopped_(false), | |
| 85 manager_(manager), | |
| 86 pa_mainloop_(NULL), | |
| 87 pa_mainloop_api_(NULL), | |
| 88 pa_context_(NULL), | |
| 89 playback_handle_(NULL), | |
| 90 client_buffer_(NULL), | |
| 91 source_exhausted_(false), | |
| 92 source_callback_(NULL) { | |
| 93 // TODO(slock): Sanity check input values. | |
| 94 } | |
| 95 | |
| 96 PulseAudioOutputStream::~PulseAudioOutputStream() { | |
| 97 // All internal structures are already freed in Close(), which calls | |
| 98 // AudioManagerLinux::Release which deletes this object. | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
DCHECK playback_handle_, pa_context_, pa_mainloop_
slock
2011/08/10 22:41:04
Done.
| |
| 99 } | |
| 100 | |
| 101 bool PulseAudioOutputStream::Open() { | |
| 102 // TODO(slock): Possibly move most of this to a OpenPlaybackDevice function in | |
| 103 // 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_get_api(pa_mainloop_); | |
| 108 pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium"); | |
| 109 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); | |
| 110 | |
| 111 // Wait until PulseAudio is ready. | |
| 112 int pa_context_ready = 1; | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
(See comment in ContextStateCallback)
Change int
slock
2011/08/10 22:41:04
Done.
| |
| 113 pa_context_set_state_callback(pa_context_, &ContextStateCallback, | |
| 114 &pa_context_ready); | |
| 115 while (pa_context_ready == 1) | |
| 116 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 117 if (pa_context_ready != 0) { | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
if (pa_context_ready != PA_CONTEXT_READY)
slock
2011/08/10 22:41:04
Done.
| |
| 118 stream_stopped_ = false; | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
remove line
slock
2011/08/10 22:41:04
Done.
| |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 // Set sample specifications and open playback stream. | |
| 123 pa_sample_specs_.format = sample_format_; | |
| 124 pa_sample_specs_.rate = sample_rate_; | |
| 125 pa_sample_specs_.channels = ChannelLayoutToChannelCount(channel_layout_); | |
| 126 playback_handle_ = pa_stream_new(pa_context_, "Playback", | |
| 127 &pa_sample_specs_, NULL); | |
| 128 | |
| 129 // Initialize client buffer. | |
| 130 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; | |
| 131 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); | |
| 132 | |
| 133 // Set write callback. | |
| 134 pa_stream_set_write_callback(playback_handle_, &WriteCallback, this); | |
| 135 | |
| 136 // Set server side buffer attributes. | |
| 137 // TODO(slock): Figure out what these values should actually be, for now use | |
| 138 // recommended values from PulseAudio's documentation: | |
| 139 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h tml | |
| 140 pa_buffer_attributes_.maxlength = (uint32_t)-1; | |
| 141 pa_buffer_attributes_.tlength = output_packet_size; | |
| 142 pa_buffer_attributes_.prebuf = (uint32_t)-1; | |
| 143 pa_buffer_attributes_.minreq = (uint32_t)-1; | |
| 144 pa_buffer_attributes_.fragsize = (uint32_t)-1; | |
| 145 | |
| 146 // Set volume | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
nit: complete sentence
slock
2011/08/10 22:41:04
Done.
| |
| 147 pa_volume_.channels = ChannelLayoutToChannelCount(channel_layout_); | |
| 148 pa_cvolume_set(&pa_volume_, pa_volume_.channels, PA_VOLUME_NORM); | |
| 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 &pa_volume_, NULL); | |
| 158 | |
| 159 if (!playback_handle_) { | |
| 160 stream_stopped_ = true; | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 void PulseAudioOutputStream::Close() { | |
| 168 /* | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
commented out?
slock
2011/08/10 22:41:04
Done.
| |
| 169 // Close the device. | |
| 170 if (playback_handle_) { | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
Set playback_handle_, pa_context_, pa_mainloop_ to
slock
2011/08/10 22:41:04
Done.
| |
| 171 pa_stream_flush(playback_handle_, NULL, NULL); | |
| 172 pa_stream_disconnect(playback_handle_); | |
| 173 | |
| 174 // Release PulseAudio structures. | |
| 175 pa_stream_unref(playback_handle_); | |
| 176 } | |
| 177 if (pa_context_) | |
| 178 pa_context_unref(pa_context_); | |
| 179 if (pa_mainloop_) | |
| 180 pa_mainloop_free(pa_mainloop_); | |
| 181 | |
| 182 // Release internal buffer. | |
| 183 client_buffer_.reset(); | |
| 184 */ | |
| 185 // Signal to the manager that we're closed and can be removed. | |
| 186 // This should be the last call in the function as it deletes "this". | |
| 187 manager_->ReleaseOutputStream(this); | |
| 188 } | |
| 189 | |
| 190 void PulseAudioOutputStream::BufferPacketInClient() { | |
| 191 // Request more data if we have more capacity. | |
| 192 if (client_buffer_->forward_capacity() > client_buffer_->forward_bytes()) { | |
| 193 | |
| 194 // Before making request to source for data we need to determine the delay | |
| 195 // (in bytes) for the requested data to be played. | |
| 196 uint32 buffer_delay = client_buffer_->forward_bytes(); | |
| 197 pa_usec_t pa_latency_micros; | |
| 198 int negative; | |
| 199 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
| 200 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, sample_rate_, | |
| 201 bytes_per_frame_); | |
| 202 // TODO(slock): Deal with negative latency (negative == 1). This has yet to | |
| 203 // happen in practice though. | |
| 204 scoped_refptr<media::DataBuffer> packet = | |
| 205 new media::DataBuffer(packet_size_); | |
| 206 size_t packet_size = RunDataCallback(packet->GetWritableData(), | |
| 207 packet->GetBufferSize(), | |
| 208 AudioBuffersState(buffer_delay, | |
| 209 hardware_delay)); | |
| 210 CHECK(packet_size <= packet->GetBufferSize()) << | |
| 211 "Data source overran buffer."; | |
| 212 | |
| 213 // TODO(slock): Swizzling, downmixing, and volume adjusting. | |
| 214 | |
| 215 if (packet_size > 0) { | |
| 216 packet->SetDataSize(packet_size); | |
| 217 // Add the packet to the buffer. | |
| 218 client_buffer_->Append(packet); | |
| 219 } else { | |
| 220 source_exhausted_ = true; | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void PulseAudioOutputStream::ClientBufferLoop() { | |
| 226 while(!stream_stopped_ && !source_exhausted_) { | |
| 227 // As long as the stream is active, we should be buffering packets if need | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
Write comment outside of the while loop and reword
slock
2011/08/10 22:41:04
Done.
| |
| 228 // be and writing packets if need be. These are asynchronous processes. | |
| 229 // This loop buffers packets and the PulseAudio mainloop writes them. | |
| 230 // BufferPacket() only actually buffers under certain circumstances and | |
| 231 // pa_mainloop_iterate() only calls WriteCallback under certain | |
| 232 // circumstances, but the loop marches on in either case. | |
| 233 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | |
| 238 CHECK(callback); | |
| 239 source_callback_ = callback; | |
| 240 | |
| 241 // Clear buffer, it might still have data in it. | |
| 242 client_buffer_->Clear(); | |
| 243 source_exhausted_ = false; | |
| 244 | |
| 245 // Start playing. | |
| 246 ClientBufferLoop(); | |
|
vrk (LEFT CHROMIUM)
2011/08/10 16:34:51
inline function
slock
2011/08/10 22:41:04
Done.
| |
| 247 } | |
| 248 | |
| 249 void PulseAudioOutputStream::Stop() { | |
| 250 // Effect will not be instantaneous as the PulseAudio server buffer drains. | |
| 251 // TODO(slock): Immediate stopping. | |
| 252 stream_stopped_ = true; | |
| 253 } | |
| 254 | |
| 255 void PulseAudioOutputStream::SetVolume(double volume) { | |
| 256 pa_volume_t new_volume = pa_sw_volume_from_linear(volume); | |
| 257 pa_cvolume_set(&pa_volume_, pa_volume_.channels, new_volume); | |
| 258 } | |
| 259 | |
| 260 void PulseAudioOutputStream::GetVolume(double* volume) { | |
| 261 // We do not allow volume changes on a per-channel basis, so all channels will | |
| 262 // always have the same volume and the average will reflect this. | |
| 263 *volume = pa_cvolume_avg(&pa_volume_); | |
| 264 } | |
| 265 | |
| 266 uint32 PulseAudioOutputStream::RunDataCallback( | |
| 267 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { | |
| 268 if (source_callback_) | |
| 269 return source_callback_->OnMoreData(this, dest, max_size, buffers_state); | |
| 270 | |
| 271 return 0; | |
| 272 } | |
| OLD | NEW |