OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/pulse/pulse_input.h" |
| 6 |
| 7 #include <pulse/pulseaudio.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "media/audio/pulse/audio_manager_pulse.h" |
| 12 #include "media/audio/pulse/pulse_util.h" |
| 13 #include "media/audio/pulse/pulse_wrapper.h" |
| 14 #include "media/base/seekable_buffer.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 PulseAudioInputStream::PulseAudioInputStream(PulseWrapper* wrapper, |
| 19 AudioManagerPulse* audio_manager, |
| 20 const std::string& device_name, |
| 21 const AudioParameters& params, |
| 22 pa_threaded_mainloop* mainloop, |
| 23 pa_context* context) |
| 24 : wrapper_(wrapper), |
| 25 audio_manager_(audio_manager), |
| 26 callback_(NULL), |
| 27 device_name_(device_name), |
| 28 params_(params), |
| 29 channels_(0), |
| 30 volume_(0.0), |
| 31 stream_started_(false), |
| 32 pa_mainloop_(mainloop), |
| 33 pa_context_(context), |
| 34 handle_(NULL), |
| 35 context_state_changed_(false) { |
| 36 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 37 DCHECK(mainloop); |
| 38 DCHECK(context); |
| 39 } |
| 40 |
| 41 PulseAudioInputStream::~PulseAudioInputStream() { |
| 42 // All internal structures should already have been freed in Close(), |
| 43 // which calls AudioManagerPulse::Release which deletes this object. |
| 44 DCHECK(!handle_); |
| 45 } |
| 46 |
| 47 bool PulseAudioInputStream::Open() { |
| 48 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 49 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 50 |
| 51 // Set sample specifications. |
| 52 pa_sample_spec pa_sample_specifications; |
| 53 pa_sample_specifications.format = BitsToPASampleFormat( |
| 54 wrapper_, params_.bits_per_sample()); |
| 55 pa_sample_specifications.rate = params_.sample_rate(); |
| 56 pa_sample_specifications.channels = params_.channels(); |
| 57 |
| 58 // Get channel mapping and open recording stream. |
| 59 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( |
| 60 wrapper_, params_.channel_layout()); |
| 61 pa_channel_map* map = (source_channel_map.channels != 0)? |
| 62 &source_channel_map : NULL; |
| 63 |
| 64 // Create a new recording stream. |
| 65 handle_ = wrapper_->pa_stream_new_(pa_context_, "RecordStream", |
| 66 &pa_sample_specifications, map); |
| 67 if (!handle_) { |
| 68 DLOG(ERROR) << "Open: failed to create PA stream"; |
| 69 return false; |
| 70 } |
| 71 |
| 72 wrapper_->pa_stream_set_state_callback_(handle_, &StreamNotifyCallback, this); |
| 73 |
| 74 // Set server-side capture buffer metrics. Detailed documentation on what |
| 75 // values should be chosen can be found at |
| 76 // freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html. |
| 77 pa_buffer_attr buffer_attributes; |
| 78 const unsigned int buffer_size = params_.GetBytesPerBuffer(); |
| 79 buffer_attributes.maxlength = static_cast<uint32_t>(-1); |
| 80 buffer_attributes.tlength = buffer_size; |
| 81 buffer_attributes.minreq = buffer_size; |
| 82 buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 83 buffer_attributes.fragsize = buffer_size; |
| 84 int flags = PA_STREAM_AUTO_TIMING_UPDATE | |
| 85 PA_STREAM_INTERPOLATE_TIMING | |
| 86 PA_STREAM_ADJUST_LATENCY | |
| 87 PA_STREAM_START_CORKED; |
| 88 int err = wrapper_->pa_stream_connect_record_( |
| 89 handle_, |
| 90 device_name_ == AudioManagerBase::kDefaultDeviceId ? |
| 91 NULL : device_name_.c_str(), |
| 92 &buffer_attributes, |
| 93 static_cast<pa_stream_flags_t>(flags)); |
| 94 if (err) { |
| 95 DLOG(ERROR) << "pa_stream_connect_playback FAILED " << err; |
| 96 return false; |
| 97 } |
| 98 |
| 99 // Wait for the stream to be ready. |
| 100 while (true) { |
| 101 pa_stream_state_t stream_state = wrapper_->pa_stream_get_state_(handle_); |
| 102 if(!PA_STREAM_IS_GOOD(stream_state)) { |
| 103 DLOG(ERROR) << "Invalid PulseAudio stream state"; |
| 104 return false; |
| 105 } |
| 106 |
| 107 if (stream_state == PA_STREAM_READY) |
| 108 break; |
| 109 wrapper_->pa_threaded_mainloop_wait_(pa_mainloop_); |
| 110 } |
| 111 |
| 112 wrapper_->pa_stream_set_read_callback_(handle_, &ReadCallback, this); |
| 113 wrapper_->pa_stream_readable_size_(handle_); |
| 114 |
| 115 buffer_.reset(new media::SeekableBuffer(0, 2 * params_.GetBytesPerBuffer())); |
| 116 audio_data_buffer_.reset(new uint8[params_.GetBytesPerBuffer()]); |
| 117 return true; |
| 118 } |
| 119 |
| 120 void PulseAudioInputStream::Start(AudioInputCallback* callback) { |
| 121 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 122 DCHECK(callback); |
| 123 DCHECK(handle_); |
| 124 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 125 |
| 126 if (stream_started_) |
| 127 return; |
| 128 |
| 129 // Clean up the old buffer. |
| 130 wrapper_->pa_stream_drop_(handle_); |
| 131 |
| 132 // Start the streaming. |
| 133 stream_started_ = true; |
| 134 callback_ = callback; |
| 135 |
| 136 pa_operation* operation = wrapper_->pa_stream_cork_(handle_, 0, NULL, NULL); |
| 137 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| 138 } |
| 139 |
| 140 void PulseAudioInputStream::Stop() { |
| 141 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 142 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 143 if (!stream_started_) |
| 144 return; |
| 145 |
| 146 // Set the flag to false to stop filling new data to soundcard. |
| 147 stream_started_ = false; |
| 148 |
| 149 pa_operation* operation = wrapper_->pa_stream_flush_( |
| 150 handle_, &StreamSuccessCallback, this); |
| 151 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| 152 |
| 153 // Stop the stream. |
| 154 wrapper_->pa_stream_set_read_callback_(handle_, NULL, NULL); |
| 155 operation = wrapper_->pa_stream_cork_(handle_, 1, &StreamSuccessCallback, |
| 156 this); |
| 157 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| 158 } |
| 159 |
| 160 void PulseAudioInputStream::Close() { |
| 161 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| 162 { |
| 163 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 164 if (handle_) { |
| 165 // Disable all the callbacks before disconnecting. |
| 166 wrapper_->pa_stream_set_state_callback_(handle_, NULL, NULL); |
| 167 wrapper_->pa_stream_flush_(handle_, NULL, NULL); |
| 168 |
| 169 if (wrapper_->pa_stream_get_state_(handle_) != PA_STREAM_UNCONNECTED) |
| 170 wrapper_->pa_stream_disconnect_(handle_); |
| 171 |
| 172 // Release PulseAudio structures. |
| 173 wrapper_->pa_stream_unref_(handle_); |
| 174 handle_ = NULL; |
| 175 } |
| 176 } |
| 177 |
| 178 if (callback_) |
| 179 callback_->OnClose(this); |
| 180 |
| 181 // Signal to the manager that we're closed and can be removed. |
| 182 // This should be the last call in the function as it deletes "this". |
| 183 audio_manager_->ReleaseInputStream(this); |
| 184 } |
| 185 |
| 186 double PulseAudioInputStream::GetMaxVolume() { |
| 187 return static_cast<double>(PA_VOLUME_NORM); |
| 188 } |
| 189 |
| 190 void PulseAudioInputStream::SetVolume(double volume) { |
| 191 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 192 if (!handle_) |
| 193 return; |
| 194 |
| 195 size_t index = wrapper_->pa_stream_get_device_index_(handle_); |
| 196 pa_operation* operation = NULL; |
| 197 if (!channels_) { |
| 198 // Get the number of channels for the source only when the |channels_| is 0. |
| 199 // We are assuming the stream source is not changed on the fly here. |
| 200 operation = wrapper_->pa_context_get_source_info_by_index_( |
| 201 pa_context_, index, &VolumeCallback, this); |
| 202 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| 203 if (!channels_) { |
| 204 DLOG(WARNING) << "Failed to get the number of channels for the source"; |
| 205 return; |
| 206 } |
| 207 } |
| 208 |
| 209 pa_cvolume pa_volume; |
| 210 wrapper_->pa_cvolume_set_(&pa_volume, channels_, volume); |
| 211 operation = wrapper_->pa_context_set_source_volume_by_index_( |
| 212 pa_context_, index, &pa_volume, NULL, NULL); |
| 213 |
| 214 // Don't need to wait for this task to complete. |
| 215 wrapper_->pa_operation_unref_(operation); |
| 216 } |
| 217 |
| 218 double PulseAudioInputStream::GetVolume() { |
| 219 AutoPulseLock auto_lock(wrapper_,pa_mainloop_); |
| 220 if (!handle_) |
| 221 return 0.0; |
| 222 |
| 223 size_t index = wrapper_->pa_stream_get_device_index_(handle_); |
| 224 pa_operation* operation = wrapper_->pa_context_get_source_info_by_index_( |
| 225 pa_context_, index, &VolumeCallback, this); |
| 226 WaitForOperationCompletion(wrapper_, pa_mainloop_, operation); |
| 227 |
| 228 return volume_; |
| 229 } |
| 230 |
| 231 // static, used by pa_stream_set_read_callback. |
| 232 void PulseAudioInputStream::ReadCallback(pa_stream* handle, |
| 233 size_t length, |
| 234 void* user_data) { |
| 235 PulseAudioInputStream* stream = |
| 236 reinterpret_cast<PulseAudioInputStream*>(user_data); |
| 237 |
| 238 stream->ReadData(); |
| 239 } |
| 240 |
| 241 // static, used by pa_context_get_source_info_by_index. |
| 242 void PulseAudioInputStream::VolumeCallback(pa_context* context, |
| 243 const pa_source_info* info, |
| 244 int error, void* user_data) { |
| 245 PulseAudioInputStream* stream = |
| 246 reinterpret_cast<PulseAudioInputStream*>(user_data); |
| 247 |
| 248 if (error) { |
| 249 stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| 250 return; |
| 251 } |
| 252 |
| 253 if (stream->channels_ != info->channel_map.channels) |
| 254 stream->channels_ = info->channel_map.channels; |
| 255 |
| 256 pa_volume_t volume = PA_VOLUME_MUTED; // Minimum possible value. |
| 257 // Use the max volume of any channel as the volume. |
| 258 for (int i = 0; i < stream->channels_; ++i) { |
| 259 if (volume < info->volume.values[i]) |
| 260 volume = info->volume.values[i]; |
| 261 } |
| 262 |
| 263 stream->volume_ = static_cast<double>(volume); |
| 264 } |
| 265 |
| 266 // static, pa_stream_success_cb_t |
| 267 void PulseAudioInputStream::StreamSuccessCallback(pa_stream* s, int error, |
| 268 void* user_data) { |
| 269 PulseAudioInputStream* stream = |
| 270 static_cast<PulseAudioInputStream*>(user_data); |
| 271 //stream->wrapper_-> |
| 272 stream->wrapper_->pa_threaded_mainloop_signal_(stream->pa_mainloop_, 0); |
| 273 } |
| 274 |
| 275 // static, used by pa_stream_set_state_callback. |
| 276 void PulseAudioInputStream::StreamNotifyCallback(pa_stream* s, |
| 277 void* user_data) { |
| 278 PulseAudioInputStream* stream = |
| 279 reinterpret_cast<PulseAudioInputStream*>(user_data); |
| 280 if (s && stream->callback_ && |
| 281 stream->wrapper_->pa_stream_get_state_(s) == PA_STREAM_FAILED) { |
| 282 stream->callback_->OnError( |
| 283 stream, |
| 284 stream->wrapper_->pa_context_errno_(stream->pa_context_)); |
| 285 } |
| 286 |
| 287 stream->wrapper_->pa_threaded_mainloop_signal_( |
| 288 stream->pa_mainloop_, 0); |
| 289 } |
| 290 |
| 291 void PulseAudioInputStream::ReadData() { |
| 292 uint32 hardware_delay = GetHardwareLatencyInBytes( |
| 293 wrapper_, handle_, params_.sample_rate(), params_.GetBytesPerFrame()); |
| 294 |
| 295 // Update the AGC volume level once every second. Note that, |
| 296 // |volume| is also updated each time SetVolume() is called |
| 297 // through IPC by the render-side AGC. |
| 298 double normalized_volume = 0.0; |
| 299 QueryAgcVolume(&normalized_volume); |
| 300 |
| 301 while (true) { |
| 302 size_t length = 0; |
| 303 const void* data = NULL; |
| 304 wrapper_->pa_stream_peek_(handle_, &data, &length); |
| 305 if (!data || length == 0) |
| 306 break; |
| 307 |
| 308 buffer_->Append(reinterpret_cast<const uint8*>(data), length); |
| 309 |
| 310 // Checks if we still have data. |
| 311 wrapper_->pa_stream_drop_(handle_); |
| 312 if (wrapper_->pa_stream_readable_size_(handle_) <= 0) |
| 313 break; |
| 314 } |
| 315 |
| 316 int packet_size = params_.GetBytesPerBuffer(); |
| 317 while (buffer_->forward_bytes() >= packet_size) { |
| 318 buffer_->Read(audio_data_buffer_.get(), packet_size); |
| 319 callback_->OnData(this, audio_data_buffer_.get(), packet_size, |
| 320 hardware_delay, normalized_volume); |
| 321 |
| 322 if (buffer_->forward_bytes() < packet_size) |
| 323 break; |
| 324 |
| 325 // TODO(xians): improve the code by implementing a WaitTillDataReady on the |
| 326 // input side. |
| 327 DLOG(WARNING) << "OnData is being called consecutively, sleep 2ms to " |
| 328 << "wait until render consumes the data"; |
| 329 base::PlatformThread::Sleep( |
| 330 base::TimeDelta::FromMilliseconds(2)); |
| 331 } |
| 332 |
| 333 wrapper_->pa_threaded_mainloop_signal_(pa_mainloop_, 0); |
| 334 } |
| 335 |
| 336 } // namespace media |
OLD | NEW |