This source file includes following definitions.
- StreamNotifyCallback
- StreamRequestCallback
- source_callback_
- Open
- Reset
- Close
- FulfillWriteRequest
- Start
- Stop
- SetVolume
- GetVolume
#include "media/audio/pulse/pulse_output.h"
#include <pulse/pulseaudio.h>
#include "base/single_thread_task_runner.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/audio_parameters.h"
#include "media/audio/pulse/pulse_util.h"
namespace media {
using pulse::AutoPulseLock;
using pulse::WaitForOperationCompletion;
void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) {
PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this);
if (s && stream->source_callback_ &&
pa_stream_get_state(s) == PA_STREAM_FAILED) {
stream->source_callback_->OnError(stream);
}
pa_threaded_mainloop_signal(stream->pa_mainloop_, 0);
}
void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len,
void* p_this) {
static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len);
}
PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params,
const std::string& device_id,
AudioManagerBase* manager)
: params_(params),
device_id_(device_id),
manager_(manager),
pa_context_(NULL),
pa_mainloop_(NULL),
pa_stream_(NULL),
volume_(1.0f),
source_callback_(NULL) {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
CHECK(params_.IsValid());
audio_bus_ = AudioBus::Create(params_);
}
PulseAudioOutputStream::~PulseAudioOutputStream() {
DCHECK(!pa_stream_);
DCHECK(!pa_context_);
DCHECK(!pa_mainloop_);
}
bool PulseAudioOutputStream::Open() {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
return pulse::CreateOutputStream(&pa_mainloop_, &pa_context_, &pa_stream_,
params_, device_id_, &StreamNotifyCallback,
&StreamRequestCallback, this);
}
void PulseAudioOutputStream::Reset() {
if (!pa_mainloop_) {
DCHECK(!pa_stream_);
DCHECK(!pa_context_);
return;
}
{
AutoPulseLock auto_lock(pa_mainloop_);
if (pa_stream_) {
pa_operation* operation = pa_stream_flush(
pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_);
WaitForOperationCompletion(pa_mainloop_, operation);
pa_stream_disconnect(pa_stream_);
pa_stream_set_write_callback(pa_stream_, NULL, NULL);
pa_stream_set_state_callback(pa_stream_, NULL, NULL);
pa_stream_unref(pa_stream_);
pa_stream_ = NULL;
}
if (pa_context_) {
pa_context_disconnect(pa_context_);
pa_context_set_state_callback(pa_context_, NULL, NULL);
pa_context_unref(pa_context_);
pa_context_ = NULL;
}
}
pa_threaded_mainloop_stop(pa_mainloop_);
pa_threaded_mainloop_free(pa_mainloop_);
pa_mainloop_ = NULL;
}
void PulseAudioOutputStream::Close() {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
Reset();
manager_->ReleaseOutputStream(this);
}
void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) {
int bytes_remaining = requested_bytes;
while (bytes_remaining > 0) {
void* buffer = NULL;
size_t bytes_to_fill = params_.GetBytesPerBuffer();
CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0);
CHECK_EQ(bytes_to_fill, static_cast<size_t>(params_.GetBytesPerBuffer()));
int frames_filled = 0;
if (source_callback_) {
uint32 hardware_delay = pulse::GetHardwareLatencyInBytes(
pa_stream_, params_.sample_rate(),
params_.GetBytesPerFrame());
frames_filled = source_callback_->OnMoreData(
audio_bus_.get(), AudioBuffersState(0, hardware_delay));
}
if (frames_filled < audio_bus_->frames()) {
audio_bus_->ZeroFramesPartial(
frames_filled, audio_bus_->frames() - frames_filled);
}
audio_bus_->Scale(volume_);
audio_bus_->ToInterleaved(
audio_bus_->frames(), params_.bits_per_sample() / 8, buffer);
if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL,
PA_SEEK_RELATIVE) < 0) {
if (source_callback_) {
source_callback_->OnError(this);
}
}
bytes_remaining -= bytes_to_fill;
}
}
void PulseAudioOutputStream::Start(AudioSourceCallback* callback) {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
CHECK(callback);
CHECK(pa_stream_);
AutoPulseLock auto_lock(pa_mainloop_);
if (pa_context_get_state(pa_context_) != PA_CONTEXT_READY &&
pa_stream_get_state(pa_stream_) != PA_STREAM_READY) {
callback->OnError(this);
return;
}
source_callback_ = callback;
pa_operation* operation = pa_stream_cork(
pa_stream_, 0, &pulse::StreamSuccessCallback, pa_mainloop_);
WaitForOperationCompletion(pa_mainloop_, operation);
}
void PulseAudioOutputStream::Stop() {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
AutoPulseLock auto_lock(pa_mainloop_);
source_callback_ = NULL;
pa_operation* operation = pa_stream_flush(
pa_stream_, &pulse::StreamSuccessCallback, pa_mainloop_);
WaitForOperationCompletion(pa_mainloop_, operation);
operation = pa_stream_cork(pa_stream_, 1, &pulse::StreamSuccessCallback,
pa_mainloop_);
WaitForOperationCompletion(pa_mainloop_, operation);
}
void PulseAudioOutputStream::SetVolume(double volume) {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
volume_ = static_cast<float>(volume);
}
void PulseAudioOutputStream::GetVolume(double* volume) {
DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
*volume = volume_;
}
}