| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/audio/audio_device_description.h" | 10 #include "media/audio/audio_device_description.h" |
| 11 #include "media/audio/pulse/audio_manager_pulse.h" | 11 #include "media/audio/pulse/audio_manager_pulse.h" |
| 12 #include "media/audio/pulse/pulse_util.h" | 12 #include "media/audio/pulse/pulse_util.h" |
| 13 #include "media/base/audio_timestamp_helper.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 using pulse::AutoPulseLock; | 17 using pulse::AutoPulseLock; |
| 17 using pulse::WaitForOperationCompletion; | 18 using pulse::WaitForOperationCompletion; |
| 18 | 19 |
| 19 // Number of blocks of buffers used in the |fifo_|. | 20 // Number of blocks of buffers used in the |fifo_|. |
| 20 const int kNumberOfBlocksBufferInFifo = 2; | 21 const int kNumberOfBlocksBufferInFifo = 2; |
| 21 | 22 |
| 22 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, | 23 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 264 |
| 264 if (s && stream->callback_ && | 265 if (s && stream->callback_ && |
| 265 pa_stream_get_state(s) == PA_STREAM_FAILED) { | 266 pa_stream_get_state(s) == PA_STREAM_FAILED) { |
| 266 stream->callback_->OnError(stream); | 267 stream->callback_->OnError(stream); |
| 267 } | 268 } |
| 268 | 269 |
| 269 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | 270 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); |
| 270 } | 271 } |
| 271 | 272 |
| 272 void PulseAudioInputStream::ReadData() { | 273 void PulseAudioInputStream::ReadData() { |
| 273 uint32_t hardware_delay = pulse::GetHardwareLatencyInBytes( | |
| 274 handle_, params_.sample_rate(), params_.GetBytesPerFrame()); | |
| 275 | |
| 276 // Update the AGC volume level once every second. Note that, | 274 // Update the AGC volume level once every second. Note that, |
| 277 // |volume| is also updated each time SetVolume() is called | 275 // |volume| is also updated each time SetVolume() is called |
| 278 // through IPC by the render-side AGC. | 276 // through IPC by the render-side AGC. |
| 279 // We disregard the |normalized_volume| from GetAgcVolume() | 277 // We disregard the |normalized_volume| from GetAgcVolume() |
| 280 // and use the value calculated by |volume_|. | 278 // and use the value calculated by |volume_|. |
| 281 double normalized_volume = 0.0; | 279 double normalized_volume = 0.0; |
| 282 GetAgcVolume(&normalized_volume); | 280 GetAgcVolume(&normalized_volume); |
| 283 normalized_volume = volume_ / GetMaxVolume(); | 281 normalized_volume = volume_ / GetMaxVolume(); |
| 284 | 282 |
| 285 do { | 283 do { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 302 fifo_.Push(data, number_of_frames, params_.bits_per_sample() / 8); | 300 fifo_.Push(data, number_of_frames, params_.bits_per_sample() / 8); |
| 303 | 301 |
| 304 // Checks if we still have data. | 302 // Checks if we still have data. |
| 305 pa_stream_drop(handle_); | 303 pa_stream_drop(handle_); |
| 306 } while (pa_stream_readable_size(handle_) > 0); | 304 } while (pa_stream_readable_size(handle_) > 0); |
| 307 | 305 |
| 308 while (fifo_.available_blocks()) { | 306 while (fifo_.available_blocks()) { |
| 309 const AudioBus* audio_bus = fifo_.Consume(); | 307 const AudioBus* audio_bus = fifo_.Consume(); |
| 310 | 308 |
| 311 // Compensate the audio delay caused by the FIFO. | 309 // Compensate the audio delay caused by the FIFO. |
| 312 hardware_delay += fifo_.GetAvailableFrames() * params_.GetBytesPerFrame(); | 310 base::TimeDelta delay = |
| 313 callback_->OnData(this, audio_bus, hardware_delay, normalized_volume); | 311 pulse::GetHardwareLatency(handle_) + |
| 312 AudioTimestampHelper::FramesToTime(fifo_.GetAvailableFrames(), |
| 313 params_.sample_rate()); |
| 314 base::TimeTicks delay_timestamp = base::TimeTicks::Now(); |
| 315 callback_->OnData(this, audio_bus, delay, delay_timestamp, |
| 316 normalized_volume); |
| 314 | 317 |
| 315 // Sleep 5ms to wait until render consumes the data in order to avoid | 318 // Sleep 5ms to wait until render consumes the data in order to avoid |
| 316 // back to back OnData() method. | 319 // back to back OnData() method. |
| 317 if (fifo_.available_blocks()) | 320 if (fifo_.available_blocks()) |
| 318 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); | 321 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
| 319 } | 322 } |
| 320 | 323 |
| 321 pa_threaded_mainloop_signal(pa_mainloop_, 0); | 324 pa_threaded_mainloop_signal(pa_mainloop_, 0); |
| 322 } | 325 } |
| 323 | 326 |
| 324 bool PulseAudioInputStream::GetSourceInformation(pa_source_info_cb_t callback) { | 327 bool PulseAudioInputStream::GetSourceInformation(pa_source_info_cb_t callback) { |
| 325 AutoPulseLock auto_lock(pa_mainloop_); | 328 AutoPulseLock auto_lock(pa_mainloop_); |
| 326 if (!handle_) | 329 if (!handle_) |
| 327 return false; | 330 return false; |
| 328 | 331 |
| 329 size_t index = pa_stream_get_device_index(handle_); | 332 size_t index = pa_stream_get_device_index(handle_); |
| 330 pa_operation* operation = | 333 pa_operation* operation = |
| 331 pa_context_get_source_info_by_index(pa_context_, index, callback, this); | 334 pa_context_get_source_info_by_index(pa_context_, index, callback, this); |
| 332 WaitForOperationCompletion(pa_mainloop_, operation); | 335 WaitForOperationCompletion(pa_mainloop_, operation); |
| 333 return true; | 336 return true; |
| 334 } | 337 } |
| 335 | 338 |
| 336 } // namespace media | 339 } // namespace media |
| OLD | NEW |