| 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> | 7 #include <pulse/pulseaudio.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" |
| 10 #include "media/audio/pulse/audio_manager_pulse.h" | 11 #include "media/audio/pulse/audio_manager_pulse.h" |
| 11 #include "media/audio/pulse/pulse_util.h" | 12 #include "media/audio/pulse/pulse_util.h" |
| 12 #include "media/base/seekable_buffer.h" | 13 #include "media/base/seekable_buffer.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 17 using base::TimeDelta; |
| 16 using pulse::AutoPulseLock; | 18 using pulse::AutoPulseLock; |
| 17 using pulse::WaitForOperationCompletion; | 19 using pulse::WaitForOperationCompletion; |
| 18 | 20 |
| 21 static const int kPowerMeasurementTimeConstantMillis = 100; |
| 22 |
| 19 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, | 23 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, |
| 20 const std::string& device_name, | 24 const std::string& device_name, |
| 21 const AudioParameters& params, | 25 const AudioParameters& params, |
| 22 pa_threaded_mainloop* mainloop, | 26 pa_threaded_mainloop* mainloop, |
| 23 pa_context* context) | 27 pa_context* context) |
| 24 : audio_manager_(audio_manager), | 28 : audio_manager_(audio_manager), |
| 25 callback_(NULL), | 29 callback_(NULL), |
| 26 device_name_(device_name), | 30 device_name_(device_name), |
| 27 params_(params), | 31 params_(params), |
| 28 channels_(0), | 32 channels_(0), |
| 29 volume_(0.0), | 33 volume_(0.0), |
| 30 stream_started_(false), | 34 stream_started_(false), |
| 31 pa_mainloop_(mainloop), | 35 pa_mainloop_(mainloop), |
| 32 pa_context_(context), | 36 pa_context_(context), |
| 33 handle_(NULL), | 37 handle_(NULL), |
| 34 context_state_changed_(false) { | 38 context_state_changed_(false), |
| 39 audio_level_monitor_(params) { |
| 35 DCHECK(mainloop); | 40 DCHECK(mainloop); |
| 36 DCHECK(context); | 41 DCHECK(context); |
| 37 } | 42 } |
| 38 | 43 |
| 39 PulseAudioInputStream::~PulseAudioInputStream() { | 44 PulseAudioInputStream::~PulseAudioInputStream() { |
| 40 // All internal structures should already have been freed in Close(), | 45 // All internal structures should already have been freed in Close(), |
| 41 // which calls AudioManagerPulse::Release which deletes this object. | 46 // which calls AudioManagerPulse::Release which deletes this object. |
| 42 DCHECK(!handle_); | 47 DCHECK(!handle_); |
| 43 } | 48 } |
| 44 | 49 |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 270 |
| 266 buffer_->Append(reinterpret_cast<const uint8*>(data), length); | 271 buffer_->Append(reinterpret_cast<const uint8*>(data), length); |
| 267 | 272 |
| 268 // Checks if we still have data. | 273 // Checks if we still have data. |
| 269 pa_stream_drop(handle_); | 274 pa_stream_drop(handle_); |
| 270 } while (pa_stream_readable_size(handle_) > 0); | 275 } while (pa_stream_readable_size(handle_) > 0); |
| 271 | 276 |
| 272 int packet_size = params_.GetBytesPerBuffer(); | 277 int packet_size = params_.GetBytesPerBuffer(); |
| 273 while (buffer_->forward_bytes() >= packet_size) { | 278 while (buffer_->forward_bytes() >= packet_size) { |
| 274 buffer_->Read(audio_data_buffer_.get(), packet_size); | 279 buffer_->Read(audio_data_buffer_.get(), packet_size); |
| 280 |
| 281 audio_level_monitor_.Scan(audio_data_buffer_.get(), packet_size); |
| 282 std::pair<float, bool> result = audio_level_monitor_.ReadCurrentPowerAndClip
(); |
| 283 float level_dbfs = result.first; |
| 284 float level = 0; |
| 285 static const float kSilenceThresholdDBFS = -72.24719896f; |
| 286 if (level_dbfs < kSilenceThresholdDBFS) |
| 287 level = 0.0f; |
| 288 else if (level_dbfs > 0.0f) |
| 289 level = 1.0f; |
| 290 else |
| 291 level = 1.0f - level_dbfs / kSilenceThresholdDBFS; |
| 292 DVLOG(1) << "PA audio_level: " << level; |
| 293 audio_level_monitor_.Reset(); |
| 294 |
| 275 callback_->OnData(this, audio_data_buffer_.get(), packet_size, | 295 callback_->OnData(this, audio_data_buffer_.get(), packet_size, |
| 276 hardware_delay, normalized_volume); | 296 hardware_delay, normalized_volume); |
| 277 | 297 |
| 278 if (buffer_->forward_bytes() < packet_size) | 298 if (buffer_->forward_bytes() < packet_size) |
| 279 break; | 299 break; |
| 280 | 300 |
| 281 // TODO(xians): Remove once PPAPI is using circular buffers. | 301 // TODO(xians): Remove once PPAPI is using circular buffers. |
| 282 DVLOG(1) << "OnData is being called consecutively, sleep 5ms to " | 302 DVLOG(1) << "OnData is being called consecutively, sleep 5ms to " |
| 283 << "wait until render consumes the data"; | 303 << "wait until render consumes the data"; |
| 284 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); | 304 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(5)); |
| 285 } | 305 } |
| 286 | 306 |
| 287 pa_threaded_mainloop_signal(pa_mainloop_, 0); | 307 pa_threaded_mainloop_signal(pa_mainloop_, 0); |
| 288 } | 308 } |
| 289 | 309 |
| 290 } // namespace media | 310 } // namespace media |
| OLD | NEW |