| 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/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 void LogCaptureStartupResult(CaptureStartupResult result) { | 110 void LogCaptureStartupResult(CaptureStartupResult result) { |
| 111 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerCaptureStartupSuccess", | 111 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerCaptureStartupSuccess", |
| 112 result, | 112 result, |
| 113 CAPTURE_STARTUP_RESULT_MAX + 1); | 113 CAPTURE_STARTUP_RESULT_MAX + 1); |
| 114 | 114 |
| 115 } | 115 } |
| 116 | 116 |
| 117 namespace media { | 117 namespace media { |
| 118 | 118 |
| 119 // static | 119 // static |
| 120 AudioInputController::Factory* AudioInputController::factory_ = NULL; | 120 AudioInputController::Factory* AudioInputController::factory_ = nullptr; |
| 121 | 121 |
| 122 AudioInputController::AudioInputController(EventHandler* handler, | 122 AudioInputController::AudioInputController(EventHandler* handler, |
| 123 SyncWriter* sync_writer, | 123 SyncWriter* sync_writer, |
| 124 UserInputMonitor* user_input_monitor, | 124 UserInputMonitor* user_input_monitor, |
| 125 const bool agc_is_enabled) | 125 const bool agc_is_enabled) |
| 126 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 126 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 127 handler_(handler), | 127 handler_(handler), |
| 128 stream_(NULL), | 128 stream_(nullptr), |
| 129 data_is_active_(false), | 129 data_is_active_(false), |
| 130 state_(CLOSED), | 130 state_(CLOSED), |
| 131 sync_writer_(sync_writer), | 131 sync_writer_(sync_writer), |
| 132 max_volume_(0.0), | 132 max_volume_(0.0), |
| 133 user_input_monitor_(user_input_monitor), | 133 user_input_monitor_(user_input_monitor), |
| 134 agc_is_enabled_(agc_is_enabled), | 134 agc_is_enabled_(agc_is_enabled), |
| 135 #if defined(AUDIO_POWER_MONITORING) | 135 #if defined(AUDIO_POWER_MONITORING) |
| 136 power_measurement_is_enabled_(false), | 136 power_measurement_is_enabled_(false), |
| 137 log_silence_state_(false), | 137 log_silence_state_(false), |
| 138 silence_state_(SILENCE_STATE_NO_MEASUREMENT), | 138 silence_state_(SILENCE_STATE_NO_MEASUREMENT), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 149 // static | 149 // static |
| 150 scoped_refptr<AudioInputController> AudioInputController::Create( | 150 scoped_refptr<AudioInputController> AudioInputController::Create( |
| 151 AudioManager* audio_manager, | 151 AudioManager* audio_manager, |
| 152 EventHandler* event_handler, | 152 EventHandler* event_handler, |
| 153 const AudioParameters& params, | 153 const AudioParameters& params, |
| 154 const std::string& device_id, | 154 const std::string& device_id, |
| 155 UserInputMonitor* user_input_monitor) { | 155 UserInputMonitor* user_input_monitor) { |
| 156 DCHECK(audio_manager); | 156 DCHECK(audio_manager); |
| 157 | 157 |
| 158 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) | 158 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) |
| 159 return NULL; | 159 return nullptr; |
| 160 | 160 |
| 161 if (factory_) { | 161 if (factory_) { |
| 162 return factory_->Create( | 162 return factory_->Create( |
| 163 audio_manager, event_handler, params, user_input_monitor); | 163 audio_manager, event_handler, params, user_input_monitor); |
| 164 } | 164 } |
| 165 scoped_refptr<AudioInputController> controller( | 165 scoped_refptr<AudioInputController> controller(new AudioInputController( |
| 166 new AudioInputController(event_handler, NULL, user_input_monitor, false)); | 166 event_handler, nullptr, user_input_monitor, false)); |
| 167 | 167 |
| 168 controller->task_runner_ = audio_manager->GetTaskRunner(); | 168 controller->task_runner_ = audio_manager->GetTaskRunner(); |
| 169 | 169 |
| 170 // Create and open a new audio input stream from the existing | 170 // Create and open a new audio input stream from the existing |
| 171 // audio-device thread. | 171 // audio-device thread. |
| 172 if (!controller->task_runner_->PostTask( | 172 if (!controller->task_runner_->PostTask( |
| 173 FROM_HERE, | 173 FROM_HERE, |
| 174 base::Bind(&AudioInputController::DoCreate, | 174 base::Bind(&AudioInputController::DoCreate, |
| 175 controller, | 175 controller, |
| 176 base::Unretained(audio_manager), | 176 base::Unretained(audio_manager), |
| 177 params, | 177 params, |
| 178 device_id))) { | 178 device_id))) { |
| 179 controller = NULL; | 179 controller = nullptr; |
| 180 } | 180 } |
| 181 | 181 |
| 182 return controller; | 182 return controller; |
| 183 } | 183 } |
| 184 | 184 |
| 185 // static | 185 // static |
| 186 scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency( | 186 scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency( |
| 187 AudioManager* audio_manager, | 187 AudioManager* audio_manager, |
| 188 EventHandler* event_handler, | 188 EventHandler* event_handler, |
| 189 const AudioParameters& params, | 189 const AudioParameters& params, |
| 190 const std::string& device_id, | 190 const std::string& device_id, |
| 191 SyncWriter* sync_writer, | 191 SyncWriter* sync_writer, |
| 192 UserInputMonitor* user_input_monitor, | 192 UserInputMonitor* user_input_monitor, |
| 193 const bool agc_is_enabled) { | 193 const bool agc_is_enabled) { |
| 194 DCHECK(audio_manager); | 194 DCHECK(audio_manager); |
| 195 DCHECK(sync_writer); | 195 DCHECK(sync_writer); |
| 196 | 196 |
| 197 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) | 197 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) |
| 198 return NULL; | 198 return nullptr; |
| 199 | 199 |
| 200 // Create the AudioInputController object and ensure that it runs on | 200 // Create the AudioInputController object and ensure that it runs on |
| 201 // the audio-manager thread. | 201 // the audio-manager thread. |
| 202 scoped_refptr<AudioInputController> controller(new AudioInputController( | 202 scoped_refptr<AudioInputController> controller(new AudioInputController( |
| 203 event_handler, sync_writer, user_input_monitor, agc_is_enabled)); | 203 event_handler, sync_writer, user_input_monitor, agc_is_enabled)); |
| 204 controller->task_runner_ = audio_manager->GetTaskRunner(); | 204 controller->task_runner_ = audio_manager->GetTaskRunner(); |
| 205 | 205 |
| 206 // Create and open a new audio input stream from the existing | 206 // Create and open a new audio input stream from the existing |
| 207 // audio-device thread. Use the provided audio-input device. | 207 // audio-device thread. Use the provided audio-input device. |
| 208 if (!controller->task_runner_->PostTask( | 208 if (!controller->task_runner_->PostTask( |
| 209 FROM_HERE, | 209 FROM_HERE, |
| 210 base::Bind(&AudioInputController::DoCreateForLowLatency, | 210 base::Bind(&AudioInputController::DoCreateForLowLatency, |
| 211 controller, | 211 controller, |
| 212 base::Unretained(audio_manager), | 212 base::Unretained(audio_manager), |
| 213 params, | 213 params, |
| 214 device_id))) { | 214 device_id))) { |
| 215 controller = NULL; | 215 controller = nullptr; |
| 216 } | 216 } |
| 217 | 217 |
| 218 return controller; | 218 return controller; |
| 219 } | 219 } |
| 220 | 220 |
| 221 // static | 221 // static |
| 222 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( | 222 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( |
| 223 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 223 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 224 EventHandler* event_handler, | 224 EventHandler* event_handler, |
| 225 AudioInputStream* stream, | 225 AudioInputStream* stream, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 237 // TODO(miu): See TODO at top of file. Until that's resolved, we need to | 237 // TODO(miu): See TODO at top of file. Until that's resolved, we need to |
| 238 // disable the error auto-detection here (since the audio mirroring | 238 // disable the error auto-detection here (since the audio mirroring |
| 239 // implementation will reliably report error and close events). Note, of | 239 // implementation will reliably report error and close events). Note, of |
| 240 // course, that we're assuming CreateForStream() has been called for the audio | 240 // course, that we're assuming CreateForStream() has been called for the audio |
| 241 // mirroring use case only. | 241 // mirroring use case only. |
| 242 if (!controller->task_runner_->PostTask( | 242 if (!controller->task_runner_->PostTask( |
| 243 FROM_HERE, | 243 FROM_HERE, |
| 244 base::Bind(&AudioInputController::DoCreateForStream, | 244 base::Bind(&AudioInputController::DoCreateForStream, |
| 245 controller, | 245 controller, |
| 246 stream))) { | 246 stream))) { |
| 247 controller = NULL; | 247 controller = nullptr; |
| 248 } | 248 } |
| 249 | 249 |
| 250 return controller; | 250 return controller; |
| 251 } | 251 } |
| 252 | 252 |
| 253 void AudioInputController::Record() { | 253 void AudioInputController::Record() { |
| 254 task_runner_->PostTask(FROM_HERE, base::Bind( | 254 task_runner_->PostTask(FROM_HERE, base::Bind( |
| 255 &AudioInputController::DoRecord, this)); | 255 &AudioInputController::DoRecord, this)); |
| 256 } | 256 } |
| 257 | 257 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 281 // avoid adding logs and UMA for non-WebRTC clients. | 281 // avoid adding logs and UMA for non-WebRTC clients. |
| 282 power_measurement_is_enabled_ = agc_is_enabled_; | 282 power_measurement_is_enabled_ = agc_is_enabled_; |
| 283 last_audio_level_log_time_ = base::TimeTicks::Now(); | 283 last_audio_level_log_time_ = base::TimeTicks::Now(); |
| 284 silence_state_ = SILENCE_STATE_NO_MEASUREMENT; | 284 silence_state_ = SILENCE_STATE_NO_MEASUREMENT; |
| 285 #endif | 285 #endif |
| 286 | 286 |
| 287 // TODO(miu): See TODO at top of file. Until that's resolved, assume all | 287 // TODO(miu): See TODO at top of file. Until that's resolved, assume all |
| 288 // platform audio input requires the |no_data_timer_| be used to auto-detect | 288 // platform audio input requires the |no_data_timer_| be used to auto-detect |
| 289 // errors. In reality, probably only Windows needs to be treated as | 289 // errors. In reality, probably only Windows needs to be treated as |
| 290 // unreliable here. | 290 // unreliable here. |
| 291 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id)); | 291 DoCreateForStream(audio_manager->MakeAudioInputStream( |
| 292 params, device_id, base::Bind(&AudioInputController::LogMessage, this))); |
| 292 } | 293 } |
| 293 | 294 |
| 294 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, | 295 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, |
| 295 const AudioParameters& params, | 296 const AudioParameters& params, |
| 296 const std::string& device_id) { | 297 const std::string& device_id) { |
| 297 DCHECK(task_runner_->BelongsToCurrentThread()); | 298 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 298 | 299 |
| 299 #if defined(AUDIO_POWER_MONITORING) | 300 #if defined(AUDIO_POWER_MONITORING) |
| 300 // We only log silence state UMA stats for low latency mode and if we use a | 301 // We only log silence state UMA stats for low latency mode and if we use a |
| 301 // real device. | 302 // real device. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 316 | 317 |
| 317 if (!stream_) { | 318 if (!stream_) { |
| 318 if (handler_) | 319 if (handler_) |
| 319 handler_->OnError(this, STREAM_CREATE_ERROR); | 320 handler_->OnError(this, STREAM_CREATE_ERROR); |
| 320 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); | 321 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); |
| 321 return; | 322 return; |
| 322 } | 323 } |
| 323 | 324 |
| 324 if (stream_ && !stream_->Open()) { | 325 if (stream_ && !stream_->Open()) { |
| 325 stream_->Close(); | 326 stream_->Close(); |
| 326 stream_ = NULL; | 327 stream_ = nullptr; |
| 327 if (handler_) | 328 if (handler_) |
| 328 handler_->OnError(this, STREAM_OPEN_ERROR); | 329 handler_->OnError(this, STREAM_OPEN_ERROR); |
| 329 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); | 330 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); |
| 330 return; | 331 return; |
| 331 } | 332 } |
| 332 | 333 |
| 333 DCHECK(!no_data_timer_.get()); | 334 DCHECK(!no_data_timer_.get()); |
| 334 | 335 |
| 335 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in | 336 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in |
| 336 // CreateLowLatency(). | 337 // CreateLowLatency(). |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 FROM_HERE, | 662 FROM_HERE, |
| 662 base::Bind(&AudioInputController::DoDisableDebugRecording, | 663 base::Bind(&AudioInputController::DoDisableDebugRecording, |
| 663 this), | 664 this), |
| 664 callback); | 665 callback); |
| 665 } | 666 } |
| 666 | 667 |
| 667 void AudioInputController::DoStopCloseAndClearStream() { | 668 void AudioInputController::DoStopCloseAndClearStream() { |
| 668 DCHECK(task_runner_->BelongsToCurrentThread()); | 669 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 669 | 670 |
| 670 // Allow calling unconditionally and bail if we don't have a stream to close. | 671 // Allow calling unconditionally and bail if we don't have a stream to close. |
| 671 if (stream_ != NULL) { | 672 if (stream_ != nullptr) { |
| 672 stream_->Stop(); | 673 stream_->Stop(); |
| 673 stream_->Close(); | 674 stream_->Close(); |
| 674 stream_ = NULL; | 675 stream_ = nullptr; |
| 675 } | 676 } |
| 676 | 677 |
| 677 // The event handler should not be touched after the stream has been closed. | 678 // The event handler should not be touched after the stream has been closed. |
| 678 handler_ = NULL; | 679 handler_ = nullptr; |
| 679 } | 680 } |
| 680 | 681 |
| 681 void AudioInputController::SetDataIsActive(bool enabled) { | 682 void AudioInputController::SetDataIsActive(bool enabled) { |
| 682 base::subtle::Release_Store(&data_is_active_, enabled); | 683 base::subtle::Release_Store(&data_is_active_, enabled); |
| 683 } | 684 } |
| 684 | 685 |
| 685 bool AudioInputController::GetDataIsActive() { | 686 bool AudioInputController::GetDataIsActive() { |
| 686 return (base::subtle::Acquire_Load(&data_is_active_) != false); | 687 return (base::subtle::Acquire_Load(&data_is_active_) != false); |
| 687 } | 688 } |
| 688 | 689 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 input_writer_ = nullptr; | 729 input_writer_ = nullptr; |
| 729 } | 730 } |
| 730 | 731 |
| 731 void AudioInputController::WriteInputDataForDebugging( | 732 void AudioInputController::WriteInputDataForDebugging( |
| 732 std::unique_ptr<AudioBus> data) { | 733 std::unique_ptr<AudioBus> data) { |
| 733 DCHECK(task_runner_->BelongsToCurrentThread()); | 734 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 734 if (input_writer_) | 735 if (input_writer_) |
| 735 input_writer_->Write(std::move(data)); | 736 input_writer_->Write(std::move(data)); |
| 736 } | 737 } |
| 737 | 738 |
| 739 void AudioInputController::LogMessage(const std::string& message) { |
| 740 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 741 handler_->OnLog(this, message); |
| 742 } |
| 743 |
| 738 } // namespace media | 744 } // namespace media |
| OLD | NEW |