Chromium Code Reviews| Index: media/audio/pulse/pulse_output.cc |
| diff --git a/media/audio/pulse/pulse_output.cc b/media/audio/pulse/pulse_output.cc |
| index 0687e6ef3b5c4984068208acf46a7fd45e47039d..a638340f83d8989992fe0df546cad2ee3a26e6f1 100644 |
| --- a/media/audio/pulse/pulse_output.cc |
| +++ b/media/audio/pulse/pulse_output.cc |
| @@ -10,95 +10,11 @@ |
| #include "media/audio/audio_manager_base.h" |
| #include "media/audio/audio_parameters.h" |
| #include "media/audio/audio_util.h" |
| +#include "media/audio/pulse/pulse_util.h" |
| +#include "media/audio/pulse/pulse_wrapper.h" |
| namespace media { |
| -// A helper class that acquires pa_threaded_mainloop_lock() while in scope. |
| -class AutoPulseLock { |
| - public: |
| - explicit AutoPulseLock(pa_threaded_mainloop* pa_mainloop) |
| - : pa_mainloop_(pa_mainloop) { |
| - pa_threaded_mainloop_lock(pa_mainloop_); |
| - } |
| - |
| - ~AutoPulseLock() { |
| - pa_threaded_mainloop_unlock(pa_mainloop_); |
| - } |
| - |
| - private: |
| - pa_threaded_mainloop* pa_mainloop_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(AutoPulseLock); |
| -}; |
| - |
| -static pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { |
| - switch (bits_per_sample) { |
| - case 8: |
| - return PA_SAMPLE_U8; |
| - case 16: |
| - return PA_SAMPLE_S16LE; |
| - case 24: |
| - return PA_SAMPLE_S24LE; |
| - case 32: |
| - return PA_SAMPLE_S32LE; |
| - default: |
| - NOTREACHED() << "Invalid bits per sample: " << bits_per_sample; |
| - return PA_SAMPLE_INVALID; |
| - } |
| -} |
| - |
| -static pa_channel_position ChromiumToPAChannelPosition(Channels channel) { |
| - switch (channel) { |
| - // PulseAudio does not differentiate between left/right and |
| - // stereo-left/stereo-right, both translate to front-left/front-right. |
| - case LEFT: |
| - return PA_CHANNEL_POSITION_FRONT_LEFT; |
| - case RIGHT: |
| - return PA_CHANNEL_POSITION_FRONT_RIGHT; |
| - case CENTER: |
| - return PA_CHANNEL_POSITION_FRONT_CENTER; |
| - case LFE: |
| - return PA_CHANNEL_POSITION_LFE; |
| - case BACK_LEFT: |
| - return PA_CHANNEL_POSITION_REAR_LEFT; |
| - case BACK_RIGHT: |
| - return PA_CHANNEL_POSITION_REAR_RIGHT; |
| - case LEFT_OF_CENTER: |
| - return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; |
| - case RIGHT_OF_CENTER: |
| - return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; |
| - case BACK_CENTER: |
| - return PA_CHANNEL_POSITION_REAR_CENTER; |
| - case SIDE_LEFT: |
| - return PA_CHANNEL_POSITION_SIDE_LEFT; |
| - case SIDE_RIGHT: |
| - return PA_CHANNEL_POSITION_SIDE_RIGHT; |
| - case CHANNELS_MAX: |
| - return PA_CHANNEL_POSITION_INVALID; |
| - default: |
| - NOTREACHED() << "Invalid channel: " << channel; |
| - return PA_CHANNEL_POSITION_INVALID; |
| - } |
| -} |
| - |
| -static pa_channel_map ChannelLayoutToPAChannelMap( |
| - ChannelLayout channel_layout) { |
| - pa_channel_map channel_map; |
| - pa_channel_map_init(&channel_map); |
| - |
| - channel_map.channels = ChannelLayoutToChannelCount(channel_layout); |
| - for (Channels ch = LEFT; ch < CHANNELS_MAX; |
| - ch = static_cast<Channels>(ch + 1)) { |
| - int channel_index = ChannelOrder(channel_layout, ch); |
| - if (channel_index < 0) |
| - continue; |
| - |
| - channel_map.map[channel_index] = ChromiumToPAChannelPosition(ch); |
| - } |
| - |
| - return channel_map; |
| -} |
| - |
| // static, pa_context_notify_cb |
| void PulseAudioOutputStream::ContextNotifyCallback(pa_context* c, |
| void* p_this) { |
| @@ -108,11 +24,12 @@ void PulseAudioOutputStream::ContextNotifyCallback(pa_context* c, |
| // these variables are only modified under pa_threaded_mainloop_lock() so this |
| // should be thread safe. |
| if (c && stream->source_callback_ && |
| - pa_context_get_state(c) == PA_CONTEXT_FAILED) { |
| - stream->source_callback_->OnError(stream, pa_context_errno(c)); |
| + stream->wrapper_->pa_context_get_state_(c) == PA_CONTEXT_FAILED) { |
| + stream->source_callback_->OnError(stream, |
| + stream->wrapper_->pa_context_errno_(c)); |
| } |
| - pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); |
| + stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| } |
| // static, pa_stream_notify_cb |
| @@ -123,19 +40,19 @@ void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { |
| // these variables are only modified under pa_threaded_mainloop_lock() so this |
| // should be thread safe. |
| if (s && stream->source_callback_ && |
| - pa_stream_get_state(s) == PA_STREAM_FAILED) { |
| + stream->wrapper_->pa_stream_get_state_(s) == PA_STREAM_FAILED) { |
| stream->source_callback_->OnError( |
| - stream, pa_context_errno(stream->pa_context_)); |
| + stream, stream->wrapper_->pa_context_errno_(stream->pa_context_)); |
| } |
| - pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); |
| + stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| } |
| // static, pa_stream_success_cb_t |
| -void PulseAudioOutputStream::StreamSuccessCallback(pa_stream* s, int success, |
| +void PulseAudioOutputStream::StreamSuccessCallback(pa_stream* s, int error, |
| void* p_this) { |
| PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| - pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); |
| + stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| } |
| // static, pa_stream_request_cb_t |
| @@ -145,9 +62,11 @@ void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len, |
| static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len); |
| } |
| -PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, |
| +PulseAudioOutputStream::PulseAudioOutputStream(PulseWrapper* wrapper, |
| + const AudioParameters& params, |
| AudioManagerBase* manager) |
| - : params_(params), |
| + : wrapper_(wrapper), |
| + params_(params), |
| manager_(manager), |
| pa_context_(NULL), |
| pa_mainloop_(NULL), |
| @@ -173,7 +92,8 @@ PulseAudioOutputStream::~PulseAudioOutputStream() { |
| if (!(expression)) { \ |
| if (pa_context_) { \ |
| DLOG(ERROR) << message << " Error: " \ |
| - << pa_strerror(pa_context_errno(pa_context_)); \ |
| + << wrapper_->pa_strerror_( \ |
| + wrapper_->pa_context_errno_(pa_context_)); \ |
| } else { \ |
| DLOG(ERROR) << message; \ |
| } \ |
| @@ -184,64 +104,70 @@ PulseAudioOutputStream::~PulseAudioOutputStream() { |
| bool PulseAudioOutputStream::Open() { |
| DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| - pa_mainloop_ = pa_threaded_mainloop_new(); |
| + pa_mainloop_ = wrapper_->pa_threaded_mainloop_new_(); |
|
DaleCurtis
2013/02/15 00:28:51
Should we be creating a new mainloop for every str
no longer working on chromium
2013/02/15 16:07:33
I do it this way to share the threads for all the
|
| RETURN_ON_FAILURE(pa_mainloop_, "Failed to create PulseAudio main loop."); |
| - pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_); |
| - pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); |
| + pa_mainloop_api* pa_mainloop_api = |
| + wrapper_->pa_threaded_mainloop_get_api_(pa_mainloop_); |
| + pa_context_ = wrapper_->pa_context_new_(pa_mainloop_api, "Chromium"); |
| RETURN_ON_FAILURE(pa_context_, "Failed to create PulseAudio context."); |
| // A state callback must be set before calling pa_threaded_mainloop_lock() or |
| // pa_threaded_mainloop_wait() calls may lead to dead lock. |
| - pa_context_set_state_callback(pa_context_, &ContextNotifyCallback, this); |
| + wrapper_->pa_context_set_state_callback_(pa_context_, &ContextNotifyCallback, |
| + this); |
| // Lock the main loop while setting up the context. Failure to do so may lead |
| // to crashes as the PulseAudio thread tries to run before things are ready. |
| - AutoPulseLock auto_lock(pa_mainloop_); |
| + AutoPulseLock auto_lock(wrapper_, pa_mainloop_); |
| RETURN_ON_FAILURE( |
| - pa_threaded_mainloop_start(pa_mainloop_) == 0, |
| + wrapper_->pa_threaded_mainloop_start_(pa_mainloop_) == 0, |
| "Failed to start PulseAudio main loop."); |
| RETURN_ON_FAILURE( |
| - pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, |
| + wrapper_->pa_context_connect_( |
| + pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, |
| "Failed to connect PulseAudio context."); |
| // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be |
| // called after pa_context_get_state() in case the context is already ready, |
| // otherwise pa_threaded_mainloop_wait() will hang indefinitely. |
| while (true) { |
| - pa_context_state_t context_state = pa_context_get_state(pa_context_); |
| + pa_context_state_t context_state = |
| + wrapper_->pa_context_get_state_(pa_context_); |
| RETURN_ON_FAILURE( |
| PA_CONTEXT_IS_GOOD(context_state), "Invalid PulseAudio context state."); |
| if (context_state == PA_CONTEXT_READY) |
| break; |
| - pa_threaded_mainloop_wait(pa_mainloop_); |
| + wrapper_->pa_threaded_mainloop_wait_(pa_mainloop_); |
| } |
| // Set sample specifications. |
| pa_sample_spec pa_sample_specifications; |
| pa_sample_specifications.format = BitsToPASampleFormat( |
| - params_.bits_per_sample()); |
| + wrapper_, params_.bits_per_sample()); |
| pa_sample_specifications.rate = params_.sample_rate(); |
| pa_sample_specifications.channels = params_.channels(); |
| // Get channel mapping and open playback stream. |
| pa_channel_map* map = NULL; |
| pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( |
| - params_.channel_layout()); |
| + wrapper_, params_.channel_layout()); |
| if (source_channel_map.channels != 0) { |
| // The source data uses a supported channel map so we will use it rather |
| // than the default channel map (NULL). |
| map = &source_channel_map; |
| } |
| - pa_stream_ = pa_stream_new( |
| + pa_stream_ = wrapper_->pa_stream_new_( |
| pa_context_, "Playback", &pa_sample_specifications, map); |
| RETURN_ON_FAILURE(pa_stream_, "Failed to create PulseAudio stream."); |
| - pa_stream_set_state_callback(pa_stream_, &StreamNotifyCallback, this); |
| + wrapper_->pa_stream_set_state_callback_(pa_stream_, &StreamNotifyCallback, |
| + this); |
| // Even though we start the stream corked below, PulseAudio will issue one |
| // stream request after setup. FulfillWriteRequest() must fulfill the write. |
| - pa_stream_set_write_callback(pa_stream_, &StreamRequestCallback, this); |
| + wrapper_->pa_stream_set_write_callback_(pa_stream_, &StreamRequestCallback, |
| + this); |
| // Tell pulse audio we only want callbacks of a certain size. |
| pa_buffer_attr pa_buffer_attributes; |
| @@ -256,7 +182,7 @@ bool PulseAudioOutputStream::Open() { |
| // not using the native sample rate. We should always open the stream with |
| // PA_STREAM_FIX_RATE and ensure this is true. |
| RETURN_ON_FAILURE( |
| - pa_stream_connect_playback( |
| + wrapper_->pa_stream_connect_playback_( |
| pa_stream_, NULL, &pa_buffer_attributes, |
| static_cast<pa_stream_flags_t>( |
| PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | |
| @@ -266,12 +192,12 @@ bool PulseAudioOutputStream::Open() { |
| // Wait for the stream to be ready. |
| while (true) { |
| - pa_stream_state_t stream_state = pa_stream_get_state(pa_stream_); |
| + pa_stream_state_t stream_state = wrapper_->pa_stream_get_state_(pa_stream_); |
| RETURN_ON_FAILURE( |
| PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state."); |
| if (stream_state == PA_STREAM_READY) |
| break; |
| - pa_threaded_mainloop_wait(pa_mainloop_); |
| + wrapper_->pa_threaded_mainloop_wait_(pa_mainloop_); |
| } |
| return true; |
| @@ -287,32 +213,33 @@ void PulseAudioOutputStream::Reset() { |
| } |
| { |
| - AutoPulseLock auto_lock(pa_mainloop_); |
| + AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| // Close the stream. |
| if (pa_stream_) { |
| // Ensure all samples are played out before shutdown. |
| - WaitForPulseOperation(pa_stream_flush( |
| - pa_stream_, &StreamSuccessCallback, this)); |
| + pa_operation* operation = wrapper_->pa_stream_flush_( |
| + pa_stream_, &StreamSuccessCallback, this); |
| + WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| // Release PulseAudio structures. |
| - 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_); |
| + wrapper_->pa_stream_disconnect_(pa_stream_); |
| + wrapper_->pa_stream_set_write_callback_(pa_stream_, NULL, NULL); |
| + wrapper_->pa_stream_set_state_callback_(pa_stream_, NULL, NULL); |
| + wrapper_->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_); |
| + wrapper_->pa_context_disconnect_(pa_context_); |
| + wrapper_->pa_context_set_state_callback_(pa_context_, NULL, NULL); |
| + wrapper_->pa_context_unref_(pa_context_); |
| pa_context_ = NULL; |
| } |
| } |
| - pa_threaded_mainloop_stop(pa_mainloop_); |
| - pa_threaded_mainloop_free(pa_mainloop_); |
| + wrapper_->pa_threaded_mainloop_stop_(pa_mainloop_); |
| + wrapper_->pa_threaded_mainloop_free_(pa_mainloop_); |
| pa_mainloop_ = NULL; |
| } |
| @@ -326,26 +253,16 @@ void PulseAudioOutputStream::Close() { |
| manager_->ReleaseOutputStream(this); |
| } |
| -int PulseAudioOutputStream::GetHardwareLatencyInBytes() { |
| - int negative = 0; |
| - pa_usec_t pa_latency_micros = 0; |
| - if (pa_stream_get_latency(pa_stream_, &pa_latency_micros, &negative) != 0) |
| - return 0; |
| - |
| - if (negative) |
| - return 0; |
| - |
| - return (pa_latency_micros * params_.sample_rate() * |
| - params_.GetBytesPerFrame()) / base::Time::kMicrosecondsPerSecond; |
| -} |
| - |
| void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); |
| int frames_filled = 0; |
| if (source_callback_) { |
| + uint32 hardware_delay = GetHardwareLatencyInBytes( |
| + wrapper_, pa_stream_, params_.sample_rate(), |
| + params_.GetBytesPerFrame()); |
| frames_filled = source_callback_->OnMoreData( |
| - audio_bus_.get(), AudioBuffersState(0, GetHardwareLatencyInBytes())); |
| + audio_bus_.get(), AudioBuffersState(0, hardware_delay)); |
| } |
| // Zero any unfilled data so it plays back as silence. |
| @@ -361,7 +278,8 @@ void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| while (bytes_remaining > 0) { |
| void* buffer = NULL; |
| size_t bytes_to_fill = bytes_remaining; |
| - CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); |
| + CHECK_GE(wrapper_->pa_stream_begin_write_(pa_stream_, &buffer, |
| + &bytes_to_fill), 0); |
| // In case PulseAudio gives us a bigger buffer than we want, cap our size. |
| bytes_to_fill = std::min( |
| @@ -377,10 +295,11 @@ void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), |
| params_.bits_per_sample() / 8, volume_); |
| - if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| - PA_SEEK_RELATIVE) < 0) { |
| + if (wrapper_->pa_stream_write_(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| + PA_SEEK_RELATIVE) < 0) { |
| if (source_callback_) { |
| - source_callback_->OnError(this, pa_context_errno(pa_context_)); |
| + source_callback_->OnError(this, |
| + wrapper_->pa_context_errno_(pa_context_)); |
| } |
| } |
| @@ -394,20 +313,21 @@ void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| CHECK(callback); |
| CHECK(pa_stream_); |
| - AutoPulseLock auto_lock(pa_mainloop_); |
| + AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| // Ensure the context and stream are ready. |
| - if (pa_context_get_state(pa_context_) != PA_CONTEXT_READY && |
| - pa_stream_get_state(pa_stream_) != PA_STREAM_READY) { |
| - callback->OnError(this, pa_context_errno(pa_context_)); |
| + if (wrapper_->pa_context_get_state_(pa_context_) != PA_CONTEXT_READY && |
| + wrapper_->pa_stream_get_state_(pa_stream_) != PA_STREAM_READY) { |
| + callback->OnError(this, wrapper_->pa_context_errno_(pa_context_)); |
| return; |
| } |
| source_callback_ = callback; |
| // Uncork (resume) the stream. |
| - WaitForPulseOperation(pa_stream_cork( |
| - pa_stream_, 0, &StreamSuccessCallback, this)); |
| + pa_operation* operation = wrapper_->pa_stream_cork_( |
| + pa_stream_, 0, &StreamSuccessCallback, this); |
| + WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| } |
| void PulseAudioOutputStream::Stop() { |
| @@ -415,16 +335,18 @@ void PulseAudioOutputStream::Stop() { |
| // Cork (pause) the stream. Waiting for the main loop lock will ensure |
| // outstanding callbacks have completed. |
| - AutoPulseLock auto_lock(pa_mainloop_); |
| + AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| // Flush the stream prior to cork, doing so after will cause hangs. Write |
| // callbacks are suspended while inside pa_threaded_mainloop_lock() so this |
| // is all thread safe. |
| - WaitForPulseOperation(pa_stream_flush( |
| - pa_stream_, &StreamSuccessCallback, this)); |
| + pa_operation* operation = wrapper_->pa_stream_flush_( |
| + pa_stream_, &StreamSuccessCallback, this); |
| + WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| - WaitForPulseOperation(pa_stream_cork( |
| - pa_stream_, 1, &StreamSuccessCallback, this)); |
| + operation = wrapper_->pa_stream_cork_( |
| + pa_stream_, 1, &StreamSuccessCallback, this); |
| + WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| source_callback_ = NULL; |
| } |
| @@ -441,12 +363,4 @@ void PulseAudioOutputStream::GetVolume(double* volume) { |
| *volume = volume_; |
| } |
| -void PulseAudioOutputStream::WaitForPulseOperation(pa_operation* op) { |
| - CHECK(op); |
| - while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { |
| - pa_threaded_mainloop_wait(pa_mainloop_); |
| - } |
| - pa_operation_unref(op); |
| -} |
| - |
| } // namespace media |