Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/pulse/pulse_output.h" | 5 #include "media/audio/pulse/pulse_output.h" |
| 6 | 6 |
| 7 #include <pulse/pulseaudio.h> | 7 #include <pulse/pulseaudio.h> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "media/audio/audio_manager_base.h" | 10 #include "media/audio/audio_manager_base.h" |
| 11 #include "media/audio/audio_parameters.h" | 11 #include "media/audio/audio_parameters.h" |
| 12 #include "media/audio/audio_util.h" | 12 #include "media/audio/audio_util.h" |
| 13 #include "media/audio/pulse/pulse_util.h" | |
| 14 #include "media/audio/pulse/pulse_wrapper.h" | |
| 13 | 15 |
| 14 namespace media { | 16 namespace media { |
| 15 | 17 |
| 16 // A helper class that acquires pa_threaded_mainloop_lock() while in scope. | |
| 17 class AutoPulseLock { | |
| 18 public: | |
| 19 explicit AutoPulseLock(pa_threaded_mainloop* pa_mainloop) | |
| 20 : pa_mainloop_(pa_mainloop) { | |
| 21 pa_threaded_mainloop_lock(pa_mainloop_); | |
| 22 } | |
| 23 | |
| 24 ~AutoPulseLock() { | |
| 25 pa_threaded_mainloop_unlock(pa_mainloop_); | |
| 26 } | |
| 27 | |
| 28 private: | |
| 29 pa_threaded_mainloop* pa_mainloop_; | |
| 30 | |
| 31 DISALLOW_COPY_AND_ASSIGN(AutoPulseLock); | |
| 32 }; | |
| 33 | |
| 34 static pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { | |
| 35 switch (bits_per_sample) { | |
| 36 case 8: | |
| 37 return PA_SAMPLE_U8; | |
| 38 case 16: | |
| 39 return PA_SAMPLE_S16LE; | |
| 40 case 24: | |
| 41 return PA_SAMPLE_S24LE; | |
| 42 case 32: | |
| 43 return PA_SAMPLE_S32LE; | |
| 44 default: | |
| 45 NOTREACHED() << "Invalid bits per sample: " << bits_per_sample; | |
| 46 return PA_SAMPLE_INVALID; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 static pa_channel_position ChromiumToPAChannelPosition(Channels channel) { | |
| 51 switch (channel) { | |
| 52 // PulseAudio does not differentiate between left/right and | |
| 53 // stereo-left/stereo-right, both translate to front-left/front-right. | |
| 54 case LEFT: | |
| 55 return PA_CHANNEL_POSITION_FRONT_LEFT; | |
| 56 case RIGHT: | |
| 57 return PA_CHANNEL_POSITION_FRONT_RIGHT; | |
| 58 case CENTER: | |
| 59 return PA_CHANNEL_POSITION_FRONT_CENTER; | |
| 60 case LFE: | |
| 61 return PA_CHANNEL_POSITION_LFE; | |
| 62 case BACK_LEFT: | |
| 63 return PA_CHANNEL_POSITION_REAR_LEFT; | |
| 64 case BACK_RIGHT: | |
| 65 return PA_CHANNEL_POSITION_REAR_RIGHT; | |
| 66 case LEFT_OF_CENTER: | |
| 67 return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; | |
| 68 case RIGHT_OF_CENTER: | |
| 69 return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; | |
| 70 case BACK_CENTER: | |
| 71 return PA_CHANNEL_POSITION_REAR_CENTER; | |
| 72 case SIDE_LEFT: | |
| 73 return PA_CHANNEL_POSITION_SIDE_LEFT; | |
| 74 case SIDE_RIGHT: | |
| 75 return PA_CHANNEL_POSITION_SIDE_RIGHT; | |
| 76 case CHANNELS_MAX: | |
| 77 return PA_CHANNEL_POSITION_INVALID; | |
| 78 default: | |
| 79 NOTREACHED() << "Invalid channel: " << channel; | |
| 80 return PA_CHANNEL_POSITION_INVALID; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 static pa_channel_map ChannelLayoutToPAChannelMap( | |
| 85 ChannelLayout channel_layout) { | |
| 86 pa_channel_map channel_map; | |
| 87 pa_channel_map_init(&channel_map); | |
| 88 | |
| 89 channel_map.channels = ChannelLayoutToChannelCount(channel_layout); | |
| 90 for (Channels ch = LEFT; ch < CHANNELS_MAX; | |
| 91 ch = static_cast<Channels>(ch + 1)) { | |
| 92 int channel_index = ChannelOrder(channel_layout, ch); | |
| 93 if (channel_index < 0) | |
| 94 continue; | |
| 95 | |
| 96 channel_map.map[channel_index] = ChromiumToPAChannelPosition(ch); | |
| 97 } | |
| 98 | |
| 99 return channel_map; | |
| 100 } | |
| 101 | |
| 102 // static, pa_context_notify_cb | 18 // static, pa_context_notify_cb |
| 103 void PulseAudioOutputStream::ContextNotifyCallback(pa_context* c, | 19 void PulseAudioOutputStream::ContextNotifyCallback(pa_context* c, |
| 104 void* p_this) { | 20 void* p_this) { |
| 105 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 21 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 106 | 22 |
| 107 // Forward unexpected failures to the AudioSourceCallback if available. All | 23 // Forward unexpected failures to the AudioSourceCallback if available. All |
| 108 // these variables are only modified under pa_threaded_mainloop_lock() so this | 24 // these variables are only modified under pa_threaded_mainloop_lock() so this |
| 109 // should be thread safe. | 25 // should be thread safe. |
| 110 if (c && stream->source_callback_ && | 26 if (c && stream->source_callback_ && |
| 111 pa_context_get_state(c) == PA_CONTEXT_FAILED) { | 27 stream->wrapper_->pa_context_get_state_(c) == PA_CONTEXT_FAILED) { |
| 112 stream->source_callback_->OnError(stream, pa_context_errno(c)); | 28 stream->source_callback_->OnError(stream, |
| 29 stream->wrapper_->pa_context_errno_(c)); | |
| 113 } | 30 } |
| 114 | 31 |
| 115 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | 32 stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| 116 } | 33 } |
| 117 | 34 |
| 118 // static, pa_stream_notify_cb | 35 // static, pa_stream_notify_cb |
| 119 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { | 36 void PulseAudioOutputStream::StreamNotifyCallback(pa_stream* s, void* p_this) { |
| 120 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 37 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 121 | 38 |
| 122 // Forward unexpected failures to the AudioSourceCallback if available. All | 39 // Forward unexpected failures to the AudioSourceCallback if available. All |
| 123 // these variables are only modified under pa_threaded_mainloop_lock() so this | 40 // these variables are only modified under pa_threaded_mainloop_lock() so this |
| 124 // should be thread safe. | 41 // should be thread safe. |
| 125 if (s && stream->source_callback_ && | 42 if (s && stream->source_callback_ && |
| 126 pa_stream_get_state(s) == PA_STREAM_FAILED) { | 43 stream->wrapper_->pa_stream_get_state_(s) == PA_STREAM_FAILED) { |
| 127 stream->source_callback_->OnError( | 44 stream->source_callback_->OnError( |
| 128 stream, pa_context_errno(stream->pa_context_)); | 45 stream, stream->wrapper_->pa_context_errno_(stream->pa_context_)); |
| 129 } | 46 } |
| 130 | 47 |
| 131 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | 48 stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| 132 } | 49 } |
| 133 | 50 |
| 134 // static, pa_stream_success_cb_t | 51 // static, pa_stream_success_cb_t |
| 135 void PulseAudioOutputStream::StreamSuccessCallback(pa_stream* s, int success, | 52 void PulseAudioOutputStream::StreamSuccessCallback(pa_stream* s, int error, |
| 136 void* p_this) { | 53 void* p_this) { |
| 137 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); | 54 PulseAudioOutputStream* stream = static_cast<PulseAudioOutputStream*>(p_this); |
| 138 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | 55 stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| 139 } | 56 } |
| 140 | 57 |
| 141 // static, pa_stream_request_cb_t | 58 // static, pa_stream_request_cb_t |
| 142 void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len, | 59 void PulseAudioOutputStream::StreamRequestCallback(pa_stream* s, size_t len, |
| 143 void* p_this) { | 60 void* p_this) { |
| 144 // Fulfill write request; must always result in a pa_stream_write() call. | 61 // Fulfill write request; must always result in a pa_stream_write() call. |
| 145 static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len); | 62 static_cast<PulseAudioOutputStream*>(p_this)->FulfillWriteRequest(len); |
| 146 } | 63 } |
| 147 | 64 |
| 148 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | 65 PulseAudioOutputStream::PulseAudioOutputStream(PulseWrapper* wrapper, |
| 66 const AudioParameters& params, | |
| 149 AudioManagerBase* manager) | 67 AudioManagerBase* manager) |
| 150 : params_(params), | 68 : wrapper_(wrapper), |
| 69 params_(params), | |
| 151 manager_(manager), | 70 manager_(manager), |
| 152 pa_context_(NULL), | 71 pa_context_(NULL), |
| 153 pa_mainloop_(NULL), | 72 pa_mainloop_(NULL), |
| 154 pa_stream_(NULL), | 73 pa_stream_(NULL), |
| 155 volume_(1.0f), | 74 volume_(1.0f), |
| 156 source_callback_(NULL) { | 75 source_callback_(NULL) { |
| 157 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 76 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 158 | 77 |
| 159 CHECK(params_.IsValid()); | 78 CHECK(params_.IsValid()); |
| 160 audio_bus_ = AudioBus::Create(params_); | 79 audio_bus_ = AudioBus::Create(params_); |
| 161 } | 80 } |
| 162 | 81 |
| 163 PulseAudioOutputStream::~PulseAudioOutputStream() { | 82 PulseAudioOutputStream::~PulseAudioOutputStream() { |
| 164 // All internal structures should already have been freed in Close(), which | 83 // All internal structures should already have been freed in Close(), which |
| 165 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. | 84 // calls AudioManagerBase::ReleaseOutputStream() which deletes this object. |
| 166 DCHECK(!pa_stream_); | 85 DCHECK(!pa_stream_); |
| 167 DCHECK(!pa_context_); | 86 DCHECK(!pa_context_); |
| 168 DCHECK(!pa_mainloop_); | 87 DCHECK(!pa_mainloop_); |
| 169 } | 88 } |
| 170 | 89 |
| 171 // Helper macro for Open() to avoid code spam and string bloat. | 90 // Helper macro for Open() to avoid code spam and string bloat. |
| 172 #define RETURN_ON_FAILURE(expression, message) do { \ | 91 #define RETURN_ON_FAILURE(expression, message) do { \ |
| 173 if (!(expression)) { \ | 92 if (!(expression)) { \ |
| 174 if (pa_context_) { \ | 93 if (pa_context_) { \ |
| 175 DLOG(ERROR) << message << " Error: " \ | 94 DLOG(ERROR) << message << " Error: " \ |
| 176 << pa_strerror(pa_context_errno(pa_context_)); \ | 95 << wrapper_->pa_strerror_( \ |
| 96 wrapper_->pa_context_errno_(pa_context_)); \ | |
| 177 } else { \ | 97 } else { \ |
| 178 DLOG(ERROR) << message; \ | 98 DLOG(ERROR) << message; \ |
| 179 } \ | 99 } \ |
| 180 return false; \ | 100 return false; \ |
| 181 } \ | 101 } \ |
| 182 } while(0) | 102 } while(0) |
| 183 | 103 |
| 184 bool PulseAudioOutputStream::Open() { | 104 bool PulseAudioOutputStream::Open() { |
| 185 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 105 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 186 | 106 |
| 187 pa_mainloop_ = pa_threaded_mainloop_new(); | 107 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
| |
| 188 RETURN_ON_FAILURE(pa_mainloop_, "Failed to create PulseAudio main loop."); | 108 RETURN_ON_FAILURE(pa_mainloop_, "Failed to create PulseAudio main loop."); |
| 189 | 109 |
| 190 pa_mainloop_api* pa_mainloop_api = pa_threaded_mainloop_get_api(pa_mainloop_); | 110 pa_mainloop_api* pa_mainloop_api = |
| 191 pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); | 111 wrapper_->pa_threaded_mainloop_get_api_(pa_mainloop_); |
| 112 pa_context_ = wrapper_->pa_context_new_(pa_mainloop_api, "Chromium"); | |
| 192 RETURN_ON_FAILURE(pa_context_, "Failed to create PulseAudio context."); | 113 RETURN_ON_FAILURE(pa_context_, "Failed to create PulseAudio context."); |
| 193 | 114 |
| 194 // A state callback must be set before calling pa_threaded_mainloop_lock() or | 115 // A state callback must be set before calling pa_threaded_mainloop_lock() or |
| 195 // pa_threaded_mainloop_wait() calls may lead to dead lock. | 116 // pa_threaded_mainloop_wait() calls may lead to dead lock. |
| 196 pa_context_set_state_callback(pa_context_, &ContextNotifyCallback, this); | 117 wrapper_->pa_context_set_state_callback_(pa_context_, &ContextNotifyCallback, |
| 118 this); | |
| 197 | 119 |
| 198 // Lock the main loop while setting up the context. Failure to do so may lead | 120 // Lock the main loop while setting up the context. Failure to do so may lead |
| 199 // to crashes as the PulseAudio thread tries to run before things are ready. | 121 // to crashes as the PulseAudio thread tries to run before things are ready. |
| 200 AutoPulseLock auto_lock(pa_mainloop_); | 122 AutoPulseLock auto_lock(wrapper_, pa_mainloop_); |
| 201 | 123 |
| 202 RETURN_ON_FAILURE( | 124 RETURN_ON_FAILURE( |
| 203 pa_threaded_mainloop_start(pa_mainloop_) == 0, | 125 wrapper_->pa_threaded_mainloop_start_(pa_mainloop_) == 0, |
| 204 "Failed to start PulseAudio main loop."); | 126 "Failed to start PulseAudio main loop."); |
| 205 RETURN_ON_FAILURE( | 127 RETURN_ON_FAILURE( |
| 206 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, | 128 wrapper_->pa_context_connect_( |
| 129 pa_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) == 0, | |
| 207 "Failed to connect PulseAudio context."); | 130 "Failed to connect PulseAudio context."); |
| 208 | 131 |
| 209 // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be | 132 // Wait until |pa_context_| is ready. pa_threaded_mainloop_wait() must be |
| 210 // called after pa_context_get_state() in case the context is already ready, | 133 // called after pa_context_get_state() in case the context is already ready, |
| 211 // otherwise pa_threaded_mainloop_wait() will hang indefinitely. | 134 // otherwise pa_threaded_mainloop_wait() will hang indefinitely. |
| 212 while (true) { | 135 while (true) { |
| 213 pa_context_state_t context_state = pa_context_get_state(pa_context_); | 136 pa_context_state_t context_state = |
| 137 wrapper_->pa_context_get_state_(pa_context_); | |
| 214 RETURN_ON_FAILURE( | 138 RETURN_ON_FAILURE( |
| 215 PA_CONTEXT_IS_GOOD(context_state), "Invalid PulseAudio context state."); | 139 PA_CONTEXT_IS_GOOD(context_state), "Invalid PulseAudio context state."); |
| 216 if (context_state == PA_CONTEXT_READY) | 140 if (context_state == PA_CONTEXT_READY) |
| 217 break; | 141 break; |
| 218 pa_threaded_mainloop_wait(pa_mainloop_); | 142 wrapper_->pa_threaded_mainloop_wait_(pa_mainloop_); |
| 219 } | 143 } |
| 220 | 144 |
| 221 // Set sample specifications. | 145 // Set sample specifications. |
| 222 pa_sample_spec pa_sample_specifications; | 146 pa_sample_spec pa_sample_specifications; |
| 223 pa_sample_specifications.format = BitsToPASampleFormat( | 147 pa_sample_specifications.format = BitsToPASampleFormat( |
| 224 params_.bits_per_sample()); | 148 wrapper_, params_.bits_per_sample()); |
| 225 pa_sample_specifications.rate = params_.sample_rate(); | 149 pa_sample_specifications.rate = params_.sample_rate(); |
| 226 pa_sample_specifications.channels = params_.channels(); | 150 pa_sample_specifications.channels = params_.channels(); |
| 227 | 151 |
| 228 // Get channel mapping and open playback stream. | 152 // Get channel mapping and open playback stream. |
| 229 pa_channel_map* map = NULL; | 153 pa_channel_map* map = NULL; |
| 230 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( | 154 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( |
| 231 params_.channel_layout()); | 155 wrapper_, params_.channel_layout()); |
| 232 if (source_channel_map.channels != 0) { | 156 if (source_channel_map.channels != 0) { |
| 233 // The source data uses a supported channel map so we will use it rather | 157 // The source data uses a supported channel map so we will use it rather |
| 234 // than the default channel map (NULL). | 158 // than the default channel map (NULL). |
| 235 map = &source_channel_map; | 159 map = &source_channel_map; |
| 236 } | 160 } |
| 237 pa_stream_ = pa_stream_new( | 161 pa_stream_ = wrapper_->pa_stream_new_( |
| 238 pa_context_, "Playback", &pa_sample_specifications, map); | 162 pa_context_, "Playback", &pa_sample_specifications, map); |
| 239 RETURN_ON_FAILURE(pa_stream_, "Failed to create PulseAudio stream."); | 163 RETURN_ON_FAILURE(pa_stream_, "Failed to create PulseAudio stream."); |
| 240 pa_stream_set_state_callback(pa_stream_, &StreamNotifyCallback, this); | 164 wrapper_->pa_stream_set_state_callback_(pa_stream_, &StreamNotifyCallback, |
| 165 this); | |
| 241 | 166 |
| 242 // Even though we start the stream corked below, PulseAudio will issue one | 167 // Even though we start the stream corked below, PulseAudio will issue one |
| 243 // stream request after setup. FulfillWriteRequest() must fulfill the write. | 168 // stream request after setup. FulfillWriteRequest() must fulfill the write. |
| 244 pa_stream_set_write_callback(pa_stream_, &StreamRequestCallback, this); | 169 wrapper_->pa_stream_set_write_callback_(pa_stream_, &StreamRequestCallback, |
| 170 this); | |
| 245 | 171 |
| 246 // Tell pulse audio we only want callbacks of a certain size. | 172 // Tell pulse audio we only want callbacks of a certain size. |
| 247 pa_buffer_attr pa_buffer_attributes; | 173 pa_buffer_attr pa_buffer_attributes; |
| 248 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); | 174 pa_buffer_attributes.maxlength = params_.GetBytesPerBuffer(); |
| 249 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); | 175 pa_buffer_attributes.minreq = params_.GetBytesPerBuffer(); |
| 250 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); | 176 pa_buffer_attributes.prebuf = params_.GetBytesPerBuffer(); |
| 251 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); | 177 pa_buffer_attributes.tlength = params_.GetBytesPerBuffer(); |
| 252 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | 178 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
| 253 | 179 |
| 254 // Connect playback stream. | 180 // Connect playback stream. |
| 255 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are | 181 // TODO(dalecurtis): Pulse tends to want really large buffer sizes if we are |
| 256 // not using the native sample rate. We should always open the stream with | 182 // not using the native sample rate. We should always open the stream with |
| 257 // PA_STREAM_FIX_RATE and ensure this is true. | 183 // PA_STREAM_FIX_RATE and ensure this is true. |
| 258 RETURN_ON_FAILURE( | 184 RETURN_ON_FAILURE( |
| 259 pa_stream_connect_playback( | 185 wrapper_->pa_stream_connect_playback_( |
| 260 pa_stream_, NULL, &pa_buffer_attributes, | 186 pa_stream_, NULL, &pa_buffer_attributes, |
| 261 static_cast<pa_stream_flags_t>( | 187 static_cast<pa_stream_flags_t>( |
| 262 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | | 188 PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | |
| 263 PA_STREAM_NOT_MONOTONIC | PA_STREAM_START_CORKED), | 189 PA_STREAM_NOT_MONOTONIC | PA_STREAM_START_CORKED), |
| 264 NULL, NULL) == 0, | 190 NULL, NULL) == 0, |
| 265 "Failed to connect PulseAudio stream."); | 191 "Failed to connect PulseAudio stream."); |
| 266 | 192 |
| 267 // Wait for the stream to be ready. | 193 // Wait for the stream to be ready. |
| 268 while (true) { | 194 while (true) { |
| 269 pa_stream_state_t stream_state = pa_stream_get_state(pa_stream_); | 195 pa_stream_state_t stream_state = wrapper_->pa_stream_get_state_(pa_stream_); |
| 270 RETURN_ON_FAILURE( | 196 RETURN_ON_FAILURE( |
| 271 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state."); | 197 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state."); |
| 272 if (stream_state == PA_STREAM_READY) | 198 if (stream_state == PA_STREAM_READY) |
| 273 break; | 199 break; |
| 274 pa_threaded_mainloop_wait(pa_mainloop_); | 200 wrapper_->pa_threaded_mainloop_wait_(pa_mainloop_); |
| 275 } | 201 } |
| 276 | 202 |
| 277 return true; | 203 return true; |
| 278 } | 204 } |
| 279 | 205 |
| 280 #undef RETURN_ON_FAILURE | 206 #undef RETURN_ON_FAILURE |
| 281 | 207 |
| 282 void PulseAudioOutputStream::Reset() { | 208 void PulseAudioOutputStream::Reset() { |
| 283 if (!pa_mainloop_) { | 209 if (!pa_mainloop_) { |
| 284 DCHECK(!pa_stream_); | 210 DCHECK(!pa_stream_); |
| 285 DCHECK(!pa_context_); | 211 DCHECK(!pa_context_); |
| 286 return; | 212 return; |
| 287 } | 213 } |
| 288 | 214 |
| 289 { | 215 { |
| 290 AutoPulseLock auto_lock(pa_mainloop_); | 216 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 291 | 217 |
| 292 // Close the stream. | 218 // Close the stream. |
| 293 if (pa_stream_) { | 219 if (pa_stream_) { |
| 294 // Ensure all samples are played out before shutdown. | 220 // Ensure all samples are played out before shutdown. |
| 295 WaitForPulseOperation(pa_stream_flush( | 221 pa_operation* operation = wrapper_->pa_stream_flush_( |
| 296 pa_stream_, &StreamSuccessCallback, this)); | 222 pa_stream_, &StreamSuccessCallback, this); |
| 223 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); | |
| 297 | 224 |
| 298 // Release PulseAudio structures. | 225 // Release PulseAudio structures. |
| 299 pa_stream_disconnect(pa_stream_); | 226 wrapper_->pa_stream_disconnect_(pa_stream_); |
| 300 pa_stream_set_write_callback(pa_stream_, NULL, NULL); | 227 wrapper_->pa_stream_set_write_callback_(pa_stream_, NULL, NULL); |
| 301 pa_stream_set_state_callback(pa_stream_, NULL, NULL); | 228 wrapper_->pa_stream_set_state_callback_(pa_stream_, NULL, NULL); |
| 302 pa_stream_unref(pa_stream_); | 229 wrapper_->pa_stream_unref_(pa_stream_); |
| 303 pa_stream_ = NULL; | 230 pa_stream_ = NULL; |
| 304 } | 231 } |
| 305 | 232 |
| 306 if (pa_context_) { | 233 if (pa_context_) { |
| 307 pa_context_disconnect(pa_context_); | 234 wrapper_->pa_context_disconnect_(pa_context_); |
| 308 pa_context_set_state_callback(pa_context_, NULL, NULL); | 235 wrapper_->pa_context_set_state_callback_(pa_context_, NULL, NULL); |
| 309 pa_context_unref(pa_context_); | 236 wrapper_->pa_context_unref_(pa_context_); |
| 310 pa_context_ = NULL; | 237 pa_context_ = NULL; |
| 311 } | 238 } |
| 312 } | 239 } |
| 313 | 240 |
| 314 pa_threaded_mainloop_stop(pa_mainloop_); | 241 wrapper_->pa_threaded_mainloop_stop_(pa_mainloop_); |
| 315 pa_threaded_mainloop_free(pa_mainloop_); | 242 wrapper_->pa_threaded_mainloop_free_(pa_mainloop_); |
| 316 pa_mainloop_ = NULL; | 243 pa_mainloop_ = NULL; |
| 317 } | 244 } |
| 318 | 245 |
| 319 void PulseAudioOutputStream::Close() { | 246 void PulseAudioOutputStream::Close() { |
| 320 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 247 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 321 | 248 |
| 322 Reset(); | 249 Reset(); |
| 323 | 250 |
| 324 // Signal to the manager that we're closed and can be removed. | 251 // Signal to the manager that we're closed and can be removed. |
| 325 // This should be the last call in the function as it deletes "this". | 252 // This should be the last call in the function as it deletes "this". |
| 326 manager_->ReleaseOutputStream(this); | 253 manager_->ReleaseOutputStream(this); |
| 327 } | 254 } |
| 328 | 255 |
| 329 int PulseAudioOutputStream::GetHardwareLatencyInBytes() { | |
| 330 int negative = 0; | |
| 331 pa_usec_t pa_latency_micros = 0; | |
| 332 if (pa_stream_get_latency(pa_stream_, &pa_latency_micros, &negative) != 0) | |
| 333 return 0; | |
| 334 | |
| 335 if (negative) | |
| 336 return 0; | |
| 337 | |
| 338 return (pa_latency_micros * params_.sample_rate() * | |
| 339 params_.GetBytesPerFrame()) / base::Time::kMicrosecondsPerSecond; | |
| 340 } | |
| 341 | |
| 342 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | 256 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { |
| 343 CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); | 257 CHECK_EQ(requested_bytes, static_cast<size_t>(params_.GetBytesPerBuffer())); |
| 344 | 258 |
| 345 int frames_filled = 0; | 259 int frames_filled = 0; |
| 346 if (source_callback_) { | 260 if (source_callback_) { |
| 261 uint32 hardware_delay = GetHardwareLatencyInBytes( | |
| 262 wrapper_, pa_stream_, params_.sample_rate(), | |
| 263 params_.GetBytesPerFrame()); | |
| 347 frames_filled = source_callback_->OnMoreData( | 264 frames_filled = source_callback_->OnMoreData( |
| 348 audio_bus_.get(), AudioBuffersState(0, GetHardwareLatencyInBytes())); | 265 audio_bus_.get(), AudioBuffersState(0, hardware_delay)); |
| 349 } | 266 } |
| 350 | 267 |
| 351 // Zero any unfilled data so it plays back as silence. | 268 // Zero any unfilled data so it plays back as silence. |
| 352 if (frames_filled < audio_bus_->frames()) { | 269 if (frames_filled < audio_bus_->frames()) { |
| 353 audio_bus_->ZeroFramesPartial( | 270 audio_bus_->ZeroFramesPartial( |
| 354 frames_filled, audio_bus_->frames() - frames_filled); | 271 frames_filled, audio_bus_->frames() - frames_filled); |
| 355 } | 272 } |
| 356 | 273 |
| 357 // PulseAudio won't always be able to provide a buffer large enough, so we may | 274 // PulseAudio won't always be able to provide a buffer large enough, so we may |
| 358 // need to request multiple buffers and fill them individually. | 275 // need to request multiple buffers and fill them individually. |
| 359 int current_frame = 0; | 276 int current_frame = 0; |
| 360 size_t bytes_remaining = requested_bytes; | 277 size_t bytes_remaining = requested_bytes; |
| 361 while (bytes_remaining > 0) { | 278 while (bytes_remaining > 0) { |
| 362 void* buffer = NULL; | 279 void* buffer = NULL; |
| 363 size_t bytes_to_fill = bytes_remaining; | 280 size_t bytes_to_fill = bytes_remaining; |
| 364 CHECK_GE(pa_stream_begin_write(pa_stream_, &buffer, &bytes_to_fill), 0); | 281 CHECK_GE(wrapper_->pa_stream_begin_write_(pa_stream_, &buffer, |
| 282 &bytes_to_fill), 0); | |
| 365 | 283 |
| 366 // In case PulseAudio gives us a bigger buffer than we want, cap our size. | 284 // In case PulseAudio gives us a bigger buffer than we want, cap our size. |
| 367 bytes_to_fill = std::min( | 285 bytes_to_fill = std::min( |
| 368 std::min(bytes_remaining, bytes_to_fill), | 286 std::min(bytes_remaining, bytes_to_fill), |
| 369 static_cast<size_t>(params_.GetBytesPerBuffer())); | 287 static_cast<size_t>(params_.GetBytesPerBuffer())); |
| 370 | 288 |
| 371 int frames_to_fill = bytes_to_fill / params_.GetBytesPerFrame();; | 289 int frames_to_fill = bytes_to_fill / params_.GetBytesPerFrame();; |
| 372 | 290 |
| 373 // Note: If this ever changes to output raw float the data must be clipped | 291 // Note: If this ever changes to output raw float the data must be clipped |
| 374 // and sanitized since it may come from an untrusted source such as NaCl. | 292 // and sanitized since it may come from an untrusted source such as NaCl. |
| 375 audio_bus_->ToInterleavedPartial( | 293 audio_bus_->ToInterleavedPartial( |
| 376 current_frame, frames_to_fill, params_.bits_per_sample() / 8, buffer); | 294 current_frame, frames_to_fill, params_.bits_per_sample() / 8, buffer); |
| 377 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), | 295 media::AdjustVolume(buffer, bytes_to_fill, params_.channels(), |
| 378 params_.bits_per_sample() / 8, volume_); | 296 params_.bits_per_sample() / 8, volume_); |
| 379 | 297 |
| 380 if (pa_stream_write(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, | 298 if (wrapper_->pa_stream_write_(pa_stream_, buffer, bytes_to_fill, NULL, 0LL, |
| 381 PA_SEEK_RELATIVE) < 0) { | 299 PA_SEEK_RELATIVE) < 0) { |
| 382 if (source_callback_) { | 300 if (source_callback_) { |
| 383 source_callback_->OnError(this, pa_context_errno(pa_context_)); | 301 source_callback_->OnError(this, |
| 302 wrapper_->pa_context_errno_(pa_context_)); | |
| 384 } | 303 } |
| 385 } | 304 } |
| 386 | 305 |
| 387 bytes_remaining -= bytes_to_fill; | 306 bytes_remaining -= bytes_to_fill; |
| 388 current_frame = frames_to_fill; | 307 current_frame = frames_to_fill; |
| 389 } | 308 } |
| 390 } | 309 } |
| 391 | 310 |
| 392 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | 311 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 393 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 312 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 394 CHECK(callback); | 313 CHECK(callback); |
| 395 CHECK(pa_stream_); | 314 CHECK(pa_stream_); |
| 396 | 315 |
| 397 AutoPulseLock auto_lock(pa_mainloop_); | 316 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 398 | 317 |
| 399 // Ensure the context and stream are ready. | 318 // Ensure the context and stream are ready. |
| 400 if (pa_context_get_state(pa_context_) != PA_CONTEXT_READY && | 319 if (wrapper_->pa_context_get_state_(pa_context_) != PA_CONTEXT_READY && |
| 401 pa_stream_get_state(pa_stream_) != PA_STREAM_READY) { | 320 wrapper_->pa_stream_get_state_(pa_stream_) != PA_STREAM_READY) { |
| 402 callback->OnError(this, pa_context_errno(pa_context_)); | 321 callback->OnError(this, wrapper_->pa_context_errno_(pa_context_)); |
| 403 return; | 322 return; |
| 404 } | 323 } |
| 405 | 324 |
| 406 source_callback_ = callback; | 325 source_callback_ = callback; |
| 407 | 326 |
| 408 // Uncork (resume) the stream. | 327 // Uncork (resume) the stream. |
| 409 WaitForPulseOperation(pa_stream_cork( | 328 pa_operation* operation = wrapper_->pa_stream_cork_( |
| 410 pa_stream_, 0, &StreamSuccessCallback, this)); | 329 pa_stream_, 0, &StreamSuccessCallback, this); |
| 330 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); | |
| 411 } | 331 } |
| 412 | 332 |
| 413 void PulseAudioOutputStream::Stop() { | 333 void PulseAudioOutputStream::Stop() { |
| 414 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 334 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 415 | 335 |
| 416 // Cork (pause) the stream. Waiting for the main loop lock will ensure | 336 // Cork (pause) the stream. Waiting for the main loop lock will ensure |
| 417 // outstanding callbacks have completed. | 337 // outstanding callbacks have completed. |
| 418 AutoPulseLock auto_lock(pa_mainloop_); | 338 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 419 | 339 |
| 420 // Flush the stream prior to cork, doing so after will cause hangs. Write | 340 // Flush the stream prior to cork, doing so after will cause hangs. Write |
| 421 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this | 341 // callbacks are suspended while inside pa_threaded_mainloop_lock() so this |
| 422 // is all thread safe. | 342 // is all thread safe. |
| 423 WaitForPulseOperation(pa_stream_flush( | 343 pa_operation* operation = wrapper_->pa_stream_flush_( |
| 424 pa_stream_, &StreamSuccessCallback, this)); | 344 pa_stream_, &StreamSuccessCallback, this); |
| 345 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); | |
| 425 | 346 |
| 426 WaitForPulseOperation(pa_stream_cork( | 347 operation = wrapper_->pa_stream_cork_( |
| 427 pa_stream_, 1, &StreamSuccessCallback, this)); | 348 pa_stream_, 1, &StreamSuccessCallback, this); |
| 349 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); | |
| 428 | 350 |
| 429 source_callback_ = NULL; | 351 source_callback_ = NULL; |
| 430 } | 352 } |
| 431 | 353 |
| 432 void PulseAudioOutputStream::SetVolume(double volume) { | 354 void PulseAudioOutputStream::SetVolume(double volume) { |
| 433 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 355 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 434 | 356 |
| 435 volume_ = static_cast<float>(volume); | 357 volume_ = static_cast<float>(volume); |
| 436 } | 358 } |
| 437 | 359 |
| 438 void PulseAudioOutputStream::GetVolume(double* volume) { | 360 void PulseAudioOutputStream::GetVolume(double* volume) { |
| 439 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); | 361 DCHECK(manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 440 | 362 |
| 441 *volume = volume_; | 363 *volume = volume_; |
| 442 } | 364 } |
| 443 | 365 |
| 444 void PulseAudioOutputStream::WaitForPulseOperation(pa_operation* op) { | |
| 445 CHECK(op); | |
| 446 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { | |
| 447 pa_threaded_mainloop_wait(pa_mainloop_); | |
| 448 } | |
| 449 pa_operation_unref(op); | |
| 450 } | |
| 451 | |
| 452 } // namespace media | 366 } // namespace media |
| OLD | NEW |