Index: media/audio/linux/pulse_output.cc |
diff --git a/media/audio/linux/pulse_output.cc b/media/audio/linux/pulse_output.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d717df6598caf5c7852dde3c2a655299d1c72a20 |
--- /dev/null |
+++ b/media/audio/linux/pulse_output.cc |
@@ -0,0 +1,271 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/audio/linux/pulse_output.h" |
+ |
+#include "media/audio/linux/audio_manager_linux.h" |
+#include "media/base/data_buffer.h" |
+#include "media/base/seekable_buffer.h" |
+ |
+static pa_sample_format_t BitsToFormat(int bits_per_sample) { |
+ switch(bits_per_sample) { |
scherkus (not reviewing)
2011/08/10 19:55:30
nit: space before (
slock
2011/08/10 22:41:04
Done.
|
+ // Unsupported sample formats shown for reference. I am assuming we want |
+ // signed and little endian because that is what we gave to ALSA. |
+ case 8: |
+ return PA_SAMPLE_U8; |
+ // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW |
+ case 16: |
+ return PA_SAMPLE_S16LE; |
+ // Also 16-bits: PA_SAMPLE_S16BE (big endian). |
+ case 24: |
+ return PA_SAMPLE_S24LE; |
+ // Also 24-bits: PA_SAMPLE_S24BE (big endian). |
+ // Other cases: PA_SAMPLE_24_32LE (in LSBs of 32-bit field, little endian), |
scherkus (not reviewing)
2011/08/10 19:55:30
align //
slock
2011/08/10 22:41:04
Done.
|
+ // and PA_SAMPLE_24_32BE (in LSBs of 32-bit field, big endian), |
+ case 32: |
+ return PA_SAMPLE_S32LE; |
+ // Also 32-bits: PA_SAMPLE_S32BE (big endian), |
+ // PA_SAMPLE_FLOAT32LE (floating point little endian), |
+ // and PA_SAMPLE_FLOAT32BE (floating point big endian). |
+ default: |
+ return PA_SAMPLE_INVALID; |
+ } |
+} |
+ |
+static size_t MicrosecondsToBytes( |
+ uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { |
+ return microseconds * sample_rate * bytes_per_frame / |
+ base::Time::kMicrosecondsPerSecond; |
+} |
+ |
+void PulseAudioOutputStream::ContextStateCallback(pa_context* context, |
+ void* userdata) { |
+ int* context_ready = static_cast<int*>(userdata); |
+ pa_context_state_t state = pa_context_get_state(context); |
+ switch(state) { |
Sergey Ulanov
2011/08/10 17:36:32
nit: space after switch
slock
2011/08/10 22:41:04
This code no longer exists.
|
+ default: |
Sergey Ulanov
2011/08/10 17:36:32
nit: default is usually at the end of switch state
slock
2011/08/10 22:41:04
This code no longer exists.
|
+ break; |
+ case PA_CONTEXT_FAILED: |
+ *context_ready = 3; |
Sergey Ulanov
2011/08/10 17:36:32
need break after that line.
Sergey Ulanov
2011/08/10 17:36:32
nit: this should be indented 2 spaces relative to
slock
2011/08/10 22:41:04
This code no longer exists.
slock
2011/08/10 22:41:04
This code no longer exists.
|
+ case PA_CONTEXT_TERMINATED: |
+ *context_ready = 2; |
+ case PA_CONTEXT_READY: |
+ *context_ready = 0; |
+ } |
+} |
+ |
+void PulseAudioOutputStream::WriteCallback(pa_stream* stream, size_t length, |
+ void* userdata) { |
+ PulseAudioOutputStream* stream_ptr = |
+ static_cast<PulseAudioOutputStream*>(userdata); |
+ |
+ // Request data from upstream if necessary. |
+ while (stream_ptr->client_buffer_->forward_bytes() < length && |
+ !stream_ptr->source_exhausted_) { |
+ stream_ptr->BufferPacketInClient(); |
+ } |
+ |
+ // Get data to write. |
+ scoped_array<uint8> read_data(new uint8[length]); |
+ stream_ptr->client_buffer_->Read(read_data.get(), length); |
+ // Write to stream. |
+ pa_stream_write(stream, read_data.get(), length, NULL, 0LL, PA_SEEK_RELATIVE); |
+} |
+ |
+PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
+ AudioManagerLinux* manager) |
+ : channel_layout_(params.channel_layout), |
+ sample_format_(BitsToFormat(params.bits_per_sample)), |
+ sample_rate_(params.sample_rate), |
+ bytes_per_frame_(params.channels * params.bits_per_sample / 8), |
+ packet_size_(params.GetPacketSize()), |
+ frames_per_packet_(packet_size_ / bytes_per_frame_), |
+ stream_stopped_(false), |
+ manager_(manager), |
+ pa_mainloop_(NULL), |
+ pa_mainloop_api_(NULL), |
+ pa_context_(NULL), |
+ playback_handle_(NULL), |
+ client_buffer_(NULL), |
+ source_exhausted_(false), |
+ source_callback_(NULL) { |
+ // TODO(slock): Sanity check input values. |
+} |
+ |
+PulseAudioOutputStream::~PulseAudioOutputStream() { |
+ // All internal structures are already freed in Close(), which calls |
+ // AudioManagerLinux::Release which deletes this object. |
+} |
+ |
+bool PulseAudioOutputStream::Open() { |
+ // TODO(slock): Possibly move most of this to a OpenPlaybackDevice function in |
+ // a new class 'pulse_util', like alsa_util. |
+ |
+ // Create a mainloop API and connect to the default server. |
+ pa_mainloop_ = pa_mainloop_new(); |
+ pa_mainloop_api_ = pa_mainloop_get_api(pa_mainloop_); |
+ pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium"); |
+ pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); |
+ |
+ // Wait until PulseAudio is ready. |
+ int pa_context_ready = 1; |
+ pa_context_set_state_callback(pa_context_, &ContextStateCallback, |
+ &pa_context_ready); |
+ while (pa_context_ready == 1) |
+ pa_mainloop_iterate(pa_mainloop_, 1, NULL); |
+ if (pa_context_ready != 0) { |
+ stream_stopped_ = false; |
+ return false; |
+ } |
+ |
+ // Set sample specifications and open playback stream. |
+ pa_sample_specs_.format = sample_format_; |
+ pa_sample_specs_.rate = sample_rate_; |
+ pa_sample_specs_.channels = ChannelLayoutToChannelCount(channel_layout_); |
+ playback_handle_ = pa_stream_new(pa_context_, "Playback", |
+ &pa_sample_specs_, NULL); |
+ |
+ // Initialize client buffer. |
+ uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; |
+ client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); |
+ |
+ // Set write callback. |
+ pa_stream_set_write_callback(playback_handle_, &WriteCallback, this); |
+ |
+ // Set server side buffer attributes. |
+ // TODO(slock): Figure out what these values should actually be, for now use |
+ // recommended values from PulseAudio's documentation: |
+ // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html |
+ pa_buffer_attributes_.maxlength = (uint32_t)-1; |
scherkus (not reviewing)
2011/08/10 19:55:30
static_cast<> instead of c-style cast
slock
2011/08/10 22:41:04
Done.
|
+ pa_buffer_attributes_.tlength = output_packet_size; |
+ pa_buffer_attributes_.prebuf = (uint32_t)-1; |
+ pa_buffer_attributes_.minreq = (uint32_t)-1; |
+ pa_buffer_attributes_.fragsize = (uint32_t)-1; |
+ |
+ // Set volume |
+ pa_volume_.channels = ChannelLayoutToChannelCount(channel_layout_); |
+ pa_cvolume_set(&pa_volume_, pa_volume_.channels, PA_VOLUME_NORM); |
+ |
+ // Connect playback stream. |
+ pa_stream_connect_playback(playback_handle_, NULL, |
+ &pa_buffer_attributes_, |
+ (pa_stream_flags_t) |
+ (PA_STREAM_INTERPOLATE_TIMING | |
+ PA_STREAM_ADJUST_LATENCY |
+ | PA_STREAM_AUTO_TIMING_UPDATE), |
Sergey Ulanov
2011/08/10 17:36:32
nit: operator must be on the previous line
slock
2011/08/10 22:41:04
Done.
|
+ &pa_volume_, NULL); |
+ |
+ if (!playback_handle_) { |
+ stream_stopped_ = true; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+void PulseAudioOutputStream::Close() { |
+ // Close the device. |
+ if (playback_handle_) { |
+ pa_stream_flush(playback_handle_, NULL, NULL); |
+ pa_stream_disconnect(playback_handle_); |
+ |
+ // Release PulseAudio structures. |
+ pa_stream_unref(playback_handle_); |
+ } |
+ if (pa_context_) |
+ pa_context_unref(pa_context_); |
+ if (pa_mainloop_) |
+ pa_mainloop_free(pa_mainloop_); |
+ |
+ // Release internal buffer. |
+ client_buffer_.reset(); |
+ |
+ // Signal to the manager that we're closed and can be removed. |
+ // This should be the last call in the function as it deletes "this". |
+ manager_->ReleaseOutputStream(this); |
+} |
+ |
+void PulseAudioOutputStream::BufferPacketInClient() { |
+ // Request more data if we have more capacity. |
+ if (client_buffer_->forward_capacity() > client_buffer_->forward_bytes()) { |
+ |
+ // Before making request to source for data we need to determine the delay |
+ // (in bytes) for the requested data to be played. |
+ uint32 buffer_delay = client_buffer_->forward_bytes(); |
+ pa_usec_t pa_latency_micros; |
+ int negative; |
+ pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); |
+ uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, sample_rate_, |
+ bytes_per_frame_); |
+ // TODO(slock): Deal with negative latency (negative == 1). This has yet to |
+ // happen in practice though. |
+ scoped_refptr<media::DataBuffer> packet = |
+ new media::DataBuffer(packet_size_); |
+ size_t packet_size = RunDataCallback(packet->GetWritableData(), |
+ packet->GetBufferSize(), |
+ AudioBuffersState(buffer_delay, |
+ hardware_delay)); |
+ CHECK(packet_size <= packet->GetBufferSize()) << |
+ "Data source overran buffer."; |
+ |
+ // TODO(slock): Swizzling, downmixing, and volume adjusting. |
+ |
+ if (packet_size > 0) { |
+ packet->SetDataSize(packet_size); |
+ // Add the packet to the buffer. |
+ client_buffer_->Append(packet); |
+ } else { |
+ source_exhausted_ = true; |
+ } |
+ } |
+} |
+ |
+void PulseAudioOutputStream::ClientBufferLoop() { |
+ while(!stream_stopped_ && !source_exhausted_) { |
scherkus (not reviewing)
2011/08/10 19:55:30
nit: space before (
slock
2011/08/10 22:41:04
Done.
|
+ // As long as the stream is active, we should be buffering packets if need |
+ // be and writing packets if need be. These are asynchronous processes. |
+ // This loop buffers packets and the PulseAudio mainloop writes them. |
+ // BufferPacket() only actually buffers under certain circumstances and |
+ // pa_mainloop_iterate() only calls WriteCallback under certain |
+ // circumstances, but the loop marches on in either case. |
+ pa_mainloop_iterate(pa_mainloop_, 1, NULL); |
+ } |
+} |
+ |
+void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
+ CHECK(callback); |
+ source_callback_ = callback; |
+ |
+ // Clear buffer, it might still have data in it. |
+ client_buffer_->Clear(); |
+ source_exhausted_ = false; |
+ |
+ // Start playing. |
+ ClientBufferLoop(); |
+} |
+ |
+void PulseAudioOutputStream::Stop() { |
+ // Effect will not be instantaneous as the PulseAudio server buffer drains. |
+ // TODO(slock): Immediate stopping. |
+ stream_stopped_ = true; |
+} |
+ |
+void PulseAudioOutputStream::SetVolume(double volume) { |
+ pa_volume_t new_volume = pa_sw_volume_from_linear(volume); |
+ pa_cvolume_set(&pa_volume_, pa_volume_.channels, new_volume); |
+} |
+ |
+void PulseAudioOutputStream::GetVolume(double* volume) { |
+ // We do not allow volume changes on a per-channel basis, so all channels will |
+ // always have the same volume and the average will reflect this. |
+ *volume = pa_cvolume_avg(&pa_volume_); |
+} |
+ |
+uint32 PulseAudioOutputStream::RunDataCallback( |
+ uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { |
+ if (source_callback_) |
+ return source_callback_->OnMoreData(this, dest, max_size, buffers_state); |
+ |
+ return 0; |
+} |