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_input.h" | 5 #include "media/audio/pulse/pulse_input.h" |
| 6 | 6 |
| 7 #include <pulse/pulseaudio.h> | |
| 8 | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "media/audio/pulse/audio_manager_pulse.h" | 8 #include "media/audio/pulse/audio_manager_pulse.h" |
| 11 #include "media/audio/pulse/pulse_util.h" | 9 #include "media/audio/pulse/pulse_util.h" |
| 12 | 10 |
| 13 namespace media { | 11 namespace media { |
| 14 | 12 |
| 15 using pulse::AutoPulseLock; | 13 using pulse::AutoPulseLock; |
| 16 using pulse::WaitForOperationCompletion; | 14 using pulse::WaitForOperationCompletion; |
| 17 | 15 |
| 18 // Number of blocks of buffers used in the |fifo_|. | 16 // Number of blocks of buffers used in the |fifo_|. |
| 19 const int kNumberOfBlocksBufferInFifo = 2; | 17 const int kNumberOfBlocksBufferInFifo = 2; |
| 20 | 18 |
| 21 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, | 19 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, |
| 22 const std::string& device_name, | 20 const std::string& device_name, |
| 23 const AudioParameters& params, | 21 const AudioParameters& params, |
| 24 pa_threaded_mainloop* mainloop, | 22 pa_threaded_mainloop* mainloop, |
| 25 pa_context* context) | 23 pa_context* context) |
| 26 : audio_manager_(audio_manager), | 24 : audio_manager_(audio_manager), |
| 27 callback_(NULL), | 25 callback_(NULL), |
| 28 device_name_(device_name), | 26 device_name_(device_name), |
| 29 params_(params), | 27 params_(params), |
| 30 channels_(0), | 28 channels_(0), |
| 31 volume_(0.0), | 29 volume_(0.0), |
| 32 stream_started_(false), | 30 stream_started_(false), |
| 31 muted_(false), | |
| 33 fifo_(params.channels(), | 32 fifo_(params.channels(), |
| 34 params.frames_per_buffer(), | 33 params.frames_per_buffer(), |
| 35 kNumberOfBlocksBufferInFifo), | 34 kNumberOfBlocksBufferInFifo), |
| 36 pa_mainloop_(mainloop), | 35 pa_mainloop_(mainloop), |
| 37 pa_context_(context), | 36 pa_context_(context), |
| 38 handle_(NULL), | 37 handle_(NULL), |
| 39 context_state_changed_(false) { | 38 context_state_changed_(false) { |
| 40 DCHECK(mainloop); | 39 DCHECK(mainloop); |
| 41 DCHECK(context); | 40 DCHECK(context); |
| 42 CHECK(params_.IsValid()); | 41 CHECK(params_.IsValid()); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 | 177 |
| 179 size_t index = pa_stream_get_device_index(handle_); | 178 size_t index = pa_stream_get_device_index(handle_); |
| 180 pa_operation* operation = pa_context_get_source_info_by_index( | 179 pa_operation* operation = pa_context_get_source_info_by_index( |
| 181 pa_context_, index, &VolumeCallback, this); | 180 pa_context_, index, &VolumeCallback, this); |
| 182 // Do not wait for the operation since we can't block the pulse thread. | 181 // Do not wait for the operation since we can't block the pulse thread. |
| 183 pa_operation_unref(operation); | 182 pa_operation_unref(operation); |
| 184 | 183 |
| 185 // Return zero and the callback will asynchronously update the |volume_|. | 184 // Return zero and the callback will asynchronously update the |volume_|. |
| 186 return 0.0; | 185 return 0.0; |
| 187 } else { | 186 } else { |
| 188 // Called by other thread, put an AutoPulseLock and wait for the operation. | 187 GetSourceInformation(&VolumeCallback); |
| 189 AutoPulseLock auto_lock(pa_mainloop_); | |
| 190 if (!handle_) | |
| 191 return 0.0; | |
| 192 | |
| 193 size_t index = pa_stream_get_device_index(handle_); | |
| 194 pa_operation* operation = pa_context_get_source_info_by_index( | |
| 195 pa_context_, index, &VolumeCallback, this); | |
| 196 WaitForOperationCompletion(pa_mainloop_, operation); | |
| 197 | |
| 198 return volume_; | 188 return volume_; |
| 199 } | 189 } |
| 200 } | 190 } |
| 201 | 191 |
| 192 bool PulseAudioInputStream::IsMuted() { | |
| 193 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 194 GetSourceInformation(&MuteCallback); | |
| 195 return muted_; | |
| 196 } | |
| 197 | |
| 202 // static, used by pa_stream_set_read_callback. | 198 // static, used by pa_stream_set_read_callback. |
| 203 void PulseAudioInputStream::ReadCallback(pa_stream* handle, | 199 void PulseAudioInputStream::ReadCallback(pa_stream* handle, |
| 204 size_t length, | 200 size_t length, |
| 205 void* user_data) { | 201 void* user_data) { |
| 206 PulseAudioInputStream* stream = | 202 PulseAudioInputStream* stream = |
| 207 reinterpret_cast<PulseAudioInputStream*>(user_data); | 203 reinterpret_cast<PulseAudioInputStream*>(user_data); |
| 208 | 204 |
| 209 stream->ReadData(); | 205 stream->ReadData(); |
| 210 } | 206 } |
| 211 | 207 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 229 for (int i = 0; i < stream->channels_; ++i) { | 225 for (int i = 0; i < stream->channels_; ++i) { |
| 230 if (volume < info->volume.values[i]) | 226 if (volume < info->volume.values[i]) |
| 231 volume = info->volume.values[i]; | 227 volume = info->volume.values[i]; |
| 232 } | 228 } |
| 233 | 229 |
| 234 // It is safe to access |volume_| here since VolumeCallback() is running | 230 // It is safe to access |volume_| here since VolumeCallback() is running |
| 235 // under PulseLock. | 231 // under PulseLock. |
| 236 stream->volume_ = static_cast<double>(volume); | 232 stream->volume_ = static_cast<double>(volume); |
| 237 } | 233 } |
| 238 | 234 |
| 235 // static, used by pa_context_get_source_info_by_index. | |
| 236 void PulseAudioInputStream::MuteCallback(pa_context* context, | |
|
tommi (sloooow) - chröme
2014/10/15 17:00:48
on which thread does this callback run? Is it saf
henrika (OOO until Aug 14)
2014/10/16 09:58:00
Done.
henrika (OOO until Aug 14)
2014/10/16 09:58:00
Based on off-line discussion. Will add comment.
| |
| 237 const pa_source_info* info, | |
| 238 int error, | |
| 239 void* user_data) { | |
| 240 PulseAudioInputStream* stream = | |
| 241 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
| 242 | |
| 243 if (error) { | |
| 244 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
|
tommi (sloooow) - chröme
2014/10/15 17:00:48
Can you document why we need to call this here and
henrika (OOO until Aug 14)
2014/10/16 09:58:00
Discussed off-line.
| |
| 245 return; | |
| 246 } | |
| 247 | |
| 248 stream->muted_ = static_cast<bool>(info->mute); | |
|
tommi (sloooow) - chröme
2014/10/15 17:00:48
stream->muted_ = info->mute != 0;
henrika (OOO until Aug 14)
2014/10/16 09:58:00
Done.
| |
| 249 } | |
| 250 | |
| 239 // static, used by pa_stream_set_state_callback. | 251 // static, used by pa_stream_set_state_callback. |
| 240 void PulseAudioInputStream::StreamNotifyCallback(pa_stream* s, | 252 void PulseAudioInputStream::StreamNotifyCallback(pa_stream* s, |
| 241 void* user_data) { | 253 void* user_data) { |
| 242 PulseAudioInputStream* stream = | 254 PulseAudioInputStream* stream = |
| 243 reinterpret_cast<PulseAudioInputStream*>(user_data); | 255 reinterpret_cast<PulseAudioInputStream*>(user_data); |
| 244 if (s && stream->callback_ && | 256 if (s && stream->callback_ && |
| 245 pa_stream_get_state(s) == PA_STREAM_FAILED) { | 257 pa_stream_get_state(s) == PA_STREAM_FAILED) { |
| 246 stream->callback_->OnError(stream); | 258 stream->callback_->OnError(stream); |
| 247 } | 259 } |
| 248 | 260 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 | 306 |
| 295 // Sleep 5ms to wait until render consumes the data in order to avoid | 307 // Sleep 5ms to wait until render consumes the data in order to avoid |
| 296 // back to back OnData() method. | 308 // back to back OnData() method. |
| 297 if (fifo_.available_blocks()) | 309 if (fifo_.available_blocks()) |
| 298 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); | 310 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
| 299 } | 311 } |
| 300 | 312 |
| 301 pa_threaded_mainloop_signal(pa_mainloop_, 0); | 313 pa_threaded_mainloop_signal(pa_mainloop_, 0); |
| 302 } | 314 } |
| 303 | 315 |
| 316 bool PulseAudioInputStream::GetSourceInformation(pa_source_info_cb_t callback) { | |
| 317 AutoPulseLock auto_lock(pa_mainloop_); | |
| 318 if (!handle_) | |
| 319 return false; | |
| 320 | |
| 321 size_t index = pa_stream_get_device_index(handle_); | |
| 322 pa_operation* operation = | |
| 323 pa_context_get_source_info_by_index(pa_context_, index, callback, this); | |
| 324 WaitForOperationCompletion(pa_mainloop_, operation); | |
| 325 return true; | |
| 326 } | |
| 327 | |
| 304 } // namespace media | 328 } // namespace media |
| OLD | NEW |