Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(920)

Unified Diff: media/audio/linux/pulse_output.cc

Issue 7473021: PulseAudio Sound Playback on Linux (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: "Volume adjustment works now" Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..c645f764928b9abdbb8ca8e379aeda19183535e2
--- /dev/null
+++ b/media/audio/linux/pulse_output.cc
@@ -0,0 +1,296 @@
+// 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/audio_util.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) {
+ // 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 LSB of 32-bit field, little endian),
+ // and PA_SAMPLE_24_32BE (in LSB 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::MainloopIterateTask() {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Also nit: method name should be more descriptive.
slock 2011/08/15 20:35:06 Done.
slock 2011/08/15 20:35:06 Done.
+ // Iterate the PulseAudio mainloop until the WriteCallback is called or the
+ // stream is stopped. The PulseAudio mainloop will call the WriteCallback to
+ // request more data when the server-side buffer needs more data to write to
+ // the audio sink. WriteCallback moves data from the |client_buffer_| to the
+ // server-side buffer. If the |client_buffer_| doesn't have enough data for
+ // the request, BufferPacketInClient is called to move data from the source
+ // into |client_buffer_|.
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Comment with a "WARNING WARNING this blocks on Pul
slock 2011/08/15 20:35:06 Done.
+ pa_write_has_calledback_ = false;
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 nit: Change field name to "write_callback_handled_
slock 2011/08/15 20:35:06 Done.
+ while (!pa_write_has_calledback_ && !stream_stopped_ && !source_exhausted_) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 nit: no {}
slock 2011/08/15 20:35:06 Done.
+ pa_mainloop_iterate(pa_mainloop_, 1, NULL);
+ }
+}
+
+void PulseAudioOutputStream::ContextStateCallback(pa_context* context,
+ void* userdata) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Ignored per offline with vrk: can't check the mess
+ pa_context_state_t* state = static_cast<pa_context_state_t*>(userdata);
+ *state = pa_context_get_state(context);
+}
+
+void PulseAudioOutputStream::WriteCallback(pa_stream* stream, size_t length,
+ void* userdata) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ PulseAudioOutputStream* stream_ptr =
+ static_cast<PulseAudioOutputStream*>(userdata);
+
+ stream_ptr->pa_write_has_calledback_ = true;
+
+ // 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);
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 OK, so there are two things here that aren't so id
slock 2011/08/15 20:35:06 Done.
+
+ // Write to stream.
+ pa_stream_write(stream, read_data.get(), length, NULL, 0LL, PA_SEEK_RELATIVE);
+
+ // Continue playback.
+ stream_ptr->message_loop_->PostTask(
+ FROM_HERE,
+ stream_ptr->method_factory_.NewRunnableMethod(
+ &PulseAudioOutputStream::MainloopIterateTask));
+}
+
+PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params,
+ AudioManagerLinux* manager,
+ MessageLoop* message_loop)
+ : 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),
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Shouldn't this value be true?
slock 2011/08/15 20:35:06 Done.
+ manager_(manager),
+ pa_context_(NULL),
+ pa_mainloop_(NULL),
+ pa_mainloop_api_(NULL),
+ playback_handle_(NULL),
+ pa_write_has_calledback_(false),
+ client_buffer_(NULL),
+ source_exhausted_(false),
+ volume_(1.0f),
+ source_callback_(NULL),
+ message_loop_(message_loop),
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ // TODO(slock): Sanity check input values.
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK(manager_)
slock 2011/08/15 20:35:06 Done.
+}
+
+PulseAudioOutputStream::~PulseAudioOutputStream() {
+ // All internal structures should already have been freed in Close(),
+ // which calls AudioManagerLinux::Release which deletes this object.
+ DCHECK(playback_handle_ == NULL);
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 nit: instead of == NULL, !<field> here and the res
slock 2011/08/15 20:35:06 Done.
+ DCHECK(pa_context_ == NULL);
+ DCHECK(pa_mainloop_ == NULL);
+ DCHECK(pa_mainloop_api_ == NULL);
+}
+
+bool PulseAudioOutputStream::Open() {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ // 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_);
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Actually, looks like pa_mainloop_api_ isn't used a
slock 2011/08/15 20:35:06 Done.
+ pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium");
+ pa_context_state_t pa_context_state = PA_CONTEXT_UNCONNECTED;
+ pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL);
+
+ // Wait until PulseAudio is ready.
+ pa_context_set_state_callback(pa_context_, &ContextStateCallback,
+ &pa_context_state);
+ while (pa_context_state != PA_CONTEXT_READY) {
+ pa_mainloop_iterate(pa_mainloop_, 1, NULL);
+ if (pa_context_state == PA_CONTEXT_FAILED ||
+ pa_context_state == PA_CONTEXT_TERMINATED) {
+ stream_stopped_ = false;
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Why are you setting stream_stopped_ to false? If a
slock 2011/08/15 20:35:06 Because I am dumb. That should definitely be fals
+ return false;
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 When you return false here and below, you are like
slock 2011/08/15 20:35:06 Done.
+ }
+ }
+
+ // Set sample specifications and open playback stream.
+ pa_sample_specs_.format = sample_format_;
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Any reason why pa_sample_specs_ is a field instead
slock 2011/08/15 20:35:06 Done, not a field anymore.
+ 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.
+ // (uint32_t)-1 is the default and recommended value from PulseAudio's
+ // documentation, found at:
+ // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html
+ pa_buffer_attributes_.maxlength = static_cast<uint32_t>(-1);
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Any reason why pa_buffer_attributes_ is a field in
slock 2011/08/15 20:35:06 Done, not a field anymore.
+ pa_buffer_attributes_.tlength = output_packet_size;
+ pa_buffer_attributes_.prebuf = static_cast<uint32_t>(-1);
+ pa_buffer_attributes_.minreq = static_cast<uint32_t>(-1);
+ pa_buffer_attributes_.fragsize = static_cast<uint32_t>(-1);
+
+ // 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),
+ NULL, NULL);
+
+ if (!playback_handle_) {
+ stream_stopped_ = true;
+ return false;
+ }
+
+ return true;
+}
+
+void PulseAudioOutputStream::Close() {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ // Close the stream.
+ if (playback_handle_) {
+ pa_stream_flush(playback_handle_, NULL, NULL);
+ pa_stream_disconnect(playback_handle_);
+
+ // Release PulseAudio structures.
+ pa_stream_unref(playback_handle_);
+ playback_handle_ = NULL;
+ }
+ if (pa_context_) {
+ pa_context_unref(pa_context_);
+ pa_context_ = NULL;
+ }
+ if (pa_mainloop_) {
+ pa_mainloop_free(pa_mainloop_);
+ pa_mainloop_ = NULL;
+ }
+ // |pa_mainloop_api| is freed with |pa_mainloop_|.
+ pa_mainloop_api_ = NULL;
+
+ // 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 and downmixing.
+ //volume_ = volume_ * 65536.0; //pa_sw_volume_from_linear(volume_);
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 ?
slock 2011/08/15 20:35:06 Done. Deleted, was old code from an experiment.
+ media::AdjustVolume(packet->GetWritableData(),
+ packet_size,
+ ChannelLayoutToChannelCount(channel_layout_),
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Save ChannelLayoutToChannelCount(channel_layout_)
slock 2011/08/15 20:35:06 Done.
+ bytes_per_frame_ / ChannelLayoutToChannelCount(
+ channel_layout_),
+ volume_);
+
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 Delete extra blank line.
slock 2011/08/15 20:35:06 Done.
+
+ if (packet_size > 0) {
+ packet->SetDataSize(packet_size);
+ // Add the packet to the buffer.
+ client_buffer_->Append(packet);
+ } else {
+ source_exhausted_ = true;
+ }
+ }
+}
+
+void PulseAudioOutputStream::Start(AudioSourceCallback* callback) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ CHECK(callback);
+ source_callback_ = callback;
+
+ // Clear buffer, it might still have data in it.
+ client_buffer_->Clear();
+ stream_stopped_ = false;
+ source_exhausted_ = false;
+
+ // Start playback.
+ message_loop_->PostTask(
+ FROM_HERE,
+ method_factory_.NewRunnableMethod(
+ &PulseAudioOutputStream::MainloopIterateTask));
+}
+
+void PulseAudioOutputStream::Stop() {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ // Effect will not be instantaneous as the PulseAudio server buffer drains.
+ // TODO(slock): Immediate stopping.
+ stream_stopped_ = true;
+}
+
+void PulseAudioOutputStream::SetVolume(double volume) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ volume_ = static_cast<float>(volume);
+}
+
+void PulseAudioOutputStream::GetVolume(double* volume) {
vrk (LEFT CHROMIUM) 2011/08/12 23:16:51 DCHECK_EQ(message_loop_, MessageLoop::current());
slock 2011/08/15 20:35:06 Done.
+ *volume = 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;
+}

Powered by Google App Engine
This is Rietveld 408576698