| 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_ = nullptr; | 120 AudioInputController::Factory* AudioInputController::factory_ = NULL; |
| 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_(nullptr), | 128 stream_(NULL), |
| 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 nullptr; | 159 return NULL; |
| 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(new AudioInputController( | 165 scoped_refptr<AudioInputController> controller( |
| 166 event_handler, nullptr, user_input_monitor, false)); | 166 new AudioInputController(event_handler, NULL, 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 = nullptr; | 179 controller = NULL; |
| 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 nullptr; | 198 return NULL; |
| 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 = nullptr; | 215 controller = NULL; |
| 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 = nullptr; | 247 controller = NULL; |
| 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( | 291 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id)); |
| 292 params, device_id, base::Bind(&AudioInputController::LogMessage, this))); | |
| 293 } | 292 } |
| 294 | 293 |
| 295 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, | 294 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, |
| 296 const AudioParameters& params, | 295 const AudioParameters& params, |
| 297 const std::string& device_id) { | 296 const std::string& device_id) { |
| 298 DCHECK(task_runner_->BelongsToCurrentThread()); | 297 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 299 | 298 |
| 300 #if defined(AUDIO_POWER_MONITORING) | 299 #if defined(AUDIO_POWER_MONITORING) |
| 301 // We only log silence state UMA stats for low latency mode and if we use a | 300 // We only log silence state UMA stats for low latency mode and if we use a |
| 302 // real device. | 301 // real device. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 317 | 316 |
| 318 if (!stream_) { | 317 if (!stream_) { |
| 319 if (handler_) | 318 if (handler_) |
| 320 handler_->OnError(this, STREAM_CREATE_ERROR); | 319 handler_->OnError(this, STREAM_CREATE_ERROR); |
| 321 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); | 320 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); |
| 322 return; | 321 return; |
| 323 } | 322 } |
| 324 | 323 |
| 325 if (stream_ && !stream_->Open()) { | 324 if (stream_ && !stream_->Open()) { |
| 326 stream_->Close(); | 325 stream_->Close(); |
| 327 stream_ = nullptr; | 326 stream_ = NULL; |
| 328 if (handler_) | 327 if (handler_) |
| 329 handler_->OnError(this, STREAM_OPEN_ERROR); | 328 handler_->OnError(this, STREAM_OPEN_ERROR); |
| 330 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); | 329 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); |
| 331 return; | 330 return; |
| 332 } | 331 } |
| 333 | 332 |
| 334 DCHECK(!no_data_timer_.get()); | 333 DCHECK(!no_data_timer_.get()); |
| 335 | 334 |
| 336 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in | 335 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in |
| 337 // CreateLowLatency(). | 336 // CreateLowLatency(). |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 FROM_HERE, | 661 FROM_HERE, |
| 663 base::Bind(&AudioInputController::DoDisableDebugRecording, | 662 base::Bind(&AudioInputController::DoDisableDebugRecording, |
| 664 this), | 663 this), |
| 665 callback); | 664 callback); |
| 666 } | 665 } |
| 667 | 666 |
| 668 void AudioInputController::DoStopCloseAndClearStream() { | 667 void AudioInputController::DoStopCloseAndClearStream() { |
| 669 DCHECK(task_runner_->BelongsToCurrentThread()); | 668 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 670 | 669 |
| 671 // Allow calling unconditionally and bail if we don't have a stream to close. | 670 // Allow calling unconditionally and bail if we don't have a stream to close. |
| 672 if (stream_ != nullptr) { | 671 if (stream_ != NULL) { |
| 673 stream_->Stop(); | 672 stream_->Stop(); |
| 674 stream_->Close(); | 673 stream_->Close(); |
| 675 stream_ = nullptr; | 674 stream_ = NULL; |
| 676 } | 675 } |
| 677 | 676 |
| 678 // The event handler should not be touched after the stream has been closed. | 677 // The event handler should not be touched after the stream has been closed. |
| 679 handler_ = nullptr; | 678 handler_ = NULL; |
| 680 } | 679 } |
| 681 | 680 |
| 682 void AudioInputController::SetDataIsActive(bool enabled) { | 681 void AudioInputController::SetDataIsActive(bool enabled) { |
| 683 base::subtle::Release_Store(&data_is_active_, enabled); | 682 base::subtle::Release_Store(&data_is_active_, enabled); |
| 684 } | 683 } |
| 685 | 684 |
| 686 bool AudioInputController::GetDataIsActive() { | 685 bool AudioInputController::GetDataIsActive() { |
| 687 return (base::subtle::Acquire_Load(&data_is_active_) != false); | 686 return (base::subtle::Acquire_Load(&data_is_active_) != false); |
| 688 } | 687 } |
| 689 | 688 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 input_writer_ = nullptr; | 728 input_writer_ = nullptr; |
| 730 } | 729 } |
| 731 | 730 |
| 732 void AudioInputController::WriteInputDataForDebugging( | 731 void AudioInputController::WriteInputDataForDebugging( |
| 733 std::unique_ptr<AudioBus> data) { | 732 std::unique_ptr<AudioBus> data) { |
| 734 DCHECK(task_runner_->BelongsToCurrentThread()); | 733 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 735 if (input_writer_) | 734 if (input_writer_) |
| 736 input_writer_->Write(std::move(data)); | 735 input_writer_->Write(std::move(data)); |
| 737 } | 736 } |
| 738 | 737 |
| 739 void AudioInputController::LogMessage(const std::string& message) { | |
| 740 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 741 handler_->OnLog(this, message); | |
| 742 } | |
| 743 | |
| 744 } // namespace media | 738 } // namespace media |
| OLD | NEW |