Chromium Code Reviews| 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 <inttypes.h> | |
| 8 | |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <limits> | 10 #include <limits> |
| 9 #include <utility> | 11 #include <utility> |
| 10 | 12 |
| 11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 19 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 20 #include "media/audio/audio_file_writer.h" | 22 #include "media/audio/audio_file_writer.h" |
| 21 #include "media/base/user_input_monitor.h" | 23 #include "media/base/user_input_monitor.h" |
| 22 | 24 |
| 25 namespace media { | |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 const int kMaxInputChannels = 3; | 28 const int kMaxInputChannels = 3; |
| 26 | 29 |
| 27 #if defined(AUDIO_POWER_MONITORING) | 30 #if defined(AUDIO_POWER_MONITORING) |
| 28 // Time in seconds between two successive measurements of audio power levels. | 31 // Time in seconds between two successive measurements of audio power levels. |
| 29 const int kPowerMonitorLogIntervalSeconds = 15; | 32 const int kPowerMonitorLogIntervalSeconds = 15; |
| 30 | 33 |
| 31 // A warning will be logged when the microphone audio volume is below this | 34 // A warning will be logged when the microphone audio volume is below this |
| 32 // threshold. | 35 // threshold. |
| 33 const int kLowLevelMicrophoneLevelPercent = 10; | 36 const int kLowLevelMicrophoneLevelPercent = 10; |
| 34 | 37 |
| 35 // Logs if the user has enabled the microphone mute or not. This is normally | 38 // Logs if the user has enabled the microphone mute or not. This is normally |
| 36 // done by marking a checkbox in an audio-settings UI which is unique for each | 39 // done by marking a checkbox in an audio-settings UI which is unique for each |
| 37 // platform. Elements in this enum should not be added, deleted or rearranged. | 40 // platform. Elements in this enum should not be added, deleted or rearranged. |
| 38 enum MicrophoneMuteResult { | 41 enum MicrophoneMuteResult { |
| 39 MICROPHONE_IS_MUTED = 0, | 42 MICROPHONE_IS_MUTED = 0, |
| 40 MICROPHONE_IS_NOT_MUTED = 1, | 43 MICROPHONE_IS_NOT_MUTED = 1, |
| 41 MICROPHONE_MUTE_MAX = MICROPHONE_IS_NOT_MUTED | 44 MICROPHONE_MUTE_MAX = MICROPHONE_IS_NOT_MUTED |
| 42 }; | 45 }; |
| 43 | 46 |
| 44 void LogMicrophoneMuteResult(MicrophoneMuteResult result) { | 47 void LogMicrophoneMuteResult(MicrophoneMuteResult result) { |
| 45 UMA_HISTOGRAM_ENUMERATION("Media.MicrophoneMuted", | 48 UMA_HISTOGRAM_ENUMERATION("Media.MicrophoneMuted", |
| 46 result, | 49 result, |
| 47 MICROPHONE_MUTE_MAX + 1); | 50 MICROPHONE_MUTE_MAX + 1); |
| 48 } | 51 } |
| 49 | 52 |
| 50 // Helper method which calculates the average power of an audio bus. Unit is in | 53 // Helper method which calculates the average power of an audio bus. Unit is in |
| 51 // dBFS, where 0 dBFS corresponds to all channels and samples equal to 1.0. | 54 // dBFS, where 0 dBFS corresponds to all channels and samples equal to 1.0. |
| 52 float AveragePower(const media::AudioBus& buffer) { | 55 float AveragePower(const AudioBus& buffer) { |
| 53 const int frames = buffer.frames(); | 56 const int frames = buffer.frames(); |
| 54 const int channels = buffer.channels(); | 57 const int channels = buffer.channels(); |
| 55 if (frames <= 0 || channels <= 0) | 58 if (frames <= 0 || channels <= 0) |
| 56 return 0.0f; | 59 return 0.0f; |
| 57 | 60 |
| 58 // Scan all channels and accumulate the sum of squares for all samples. | 61 // Scan all channels and accumulate the sum of squares for all samples. |
| 59 float sum_power = 0.0f; | 62 float sum_power = 0.0f; |
| 60 for (int ch = 0; ch < channels; ++ch) { | 63 for (int ch = 0; ch < channels; ++ch) { |
| 61 const float* channel_data = buffer.channel(ch); | 64 const float* channel_data = buffer.channel(ch); |
| 62 for (int i = 0; i < frames; i++) { | 65 for (int i = 0; i < frames; i++) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 74 const float kInsignificantPower = 1.0e-10f; // -100 dBFS | 77 const float kInsignificantPower = 1.0e-10f; // -100 dBFS |
| 75 const float power_dbfs = average_power < kInsignificantPower ? | 78 const float power_dbfs = average_power < kInsignificantPower ? |
| 76 -std::numeric_limits<float>::infinity() : 10.0f * log10f(average_power); | 79 -std::numeric_limits<float>::infinity() : 10.0f * log10f(average_power); |
| 77 | 80 |
| 78 return power_dbfs; | 81 return power_dbfs; |
| 79 } | 82 } |
| 80 #endif // AUDIO_POWER_MONITORING | 83 #endif // AUDIO_POWER_MONITORING |
| 81 | 84 |
| 82 } // namespace | 85 } // namespace |
| 83 | 86 |
| 84 namespace media { | 87 class AudioInputController::AudioCallback |
|
o1ka
2017/01/16 15:04:52
Could you add a comment explaining the reasons why
tommi (sloooow) - chröme
2017/01/16 17:02:32
Done.
| |
| 88 : public AudioInputStream::AudioInputCallback { | |
| 89 public: | |
| 90 explicit AudioCallback(AudioInputController* controller) | |
| 91 : controller_(controller), | |
| 92 weak_controller_(controller->weak_ptr_factory_.GetWeakPtr()) {} | |
| 93 ~AudioCallback() override {} | |
| 94 | |
| 95 bool received_callback() const { return received_callback_; } | |
| 96 bool error_during_callback() const { return error_during_callback_; } | |
| 97 | |
| 98 private: | |
| 99 void OnData(AudioInputStream* stream, | |
| 100 const AudioBus* source, | |
| 101 uint32_t hardware_delay_bytes, | |
| 102 double volume) override { | |
| 103 TRACE_EVENT0("audio", "AC::OnData"); | |
| 104 | |
| 105 received_callback_ = true; | |
| 106 | |
| 107 DeliverDataToSyncWriter(source, hardware_delay_bytes, volume); | |
| 108 PerformOptionalDebugRecording(source); | |
| 109 } | |
| 110 | |
| 111 void OnError(AudioInputStream* stream) override { | |
| 112 error_during_callback_ = true; | |
| 113 controller_->task_runner_->PostTask( | |
| 114 FROM_HERE, | |
| 115 base::Bind(&AudioInputController::DoReportError, weak_controller_)); | |
| 116 } | |
| 117 | |
| 118 void PerformOptionalDebugRecording(const AudioBus* source) { | |
| 119 // Called on the hw callback thread while recording is enabled. | |
| 120 if (!controller_->debug_writer_ || !controller_->debug_writer_->WillWrite()) | |
| 121 return; | |
| 122 | |
| 123 // TODO(tommi): This is costly. AudioBus heap allocs and we create a new | |
| 124 // one for every callback. We could instead have a pool of bus objects | |
| 125 // that get returned to us somehow. | |
| 126 // We should also avoid calling PostTask here since the implementation | |
| 127 // of the debug writer will basically do a PostTask straight away anyway. | |
| 128 // Might require some modifications to AudioInputDebugWriter though since | |
| 129 // there are some threading concerns there and AudioInputDebugWriter's | |
| 130 // lifetime guarantees need to be longer than that of associated active | |
| 131 // audio streams. | |
| 132 std::unique_ptr<AudioBus> source_copy = | |
| 133 AudioBus::Create(source->channels(), source->frames()); | |
| 134 source->CopyTo(source_copy.get()); | |
| 135 controller_->task_runner_->PostTask( | |
| 136 FROM_HERE, base::Bind(&AudioInputController::WriteInputDataForDebugging, | |
| 137 weak_controller_, base::Passed(&source_copy))); | |
| 138 } | |
| 139 | |
| 140 void DeliverDataToSyncWriter(const AudioBus* source, | |
| 141 uint32_t hardware_delay_bytes, | |
| 142 double volume) { | |
| 143 bool key_pressed = controller_->CheckForKeyboardInput(); | |
|
o1ka
2017/01/16 10:23:31
maxmorin@: here is another dependency on browser p
| |
| 144 controller_->sync_writer_->Write(source, volume, key_pressed, | |
|
o1ka
2017/01/16 10:23:31
I feel like relationships between AIC and AIC::Cal
tommi (sloooow) - chröme
2017/01/16 12:34:30
Before, they were all in the same class. With this
o1ka
2017/01/16 15:04:52
Agree that moving ownership does not make sense. A
tommi (sloooow) - chröme
2017/01/16 17:02:32
I agree that it's not exactly elegant. I added a
| |
| 145 hardware_delay_bytes); | |
| 146 float average_power_dbfs; | |
| 147 int mic_volume_percent; | |
| 148 if (controller_->CheckAudioPower(source, volume, &average_power_dbfs, | |
| 149 &mic_volume_percent)) { | |
| 150 // Use event handler on the audio thread to relay a message to the ARIH | |
| 151 // in content which does the actual logging on the IO thread. | |
| 152 controller_->task_runner_->PostTask( | |
| 153 FROM_HERE, | |
| 154 base::Bind(&AudioInputController::DoLogAudioLevels, weak_controller_, | |
| 155 average_power_dbfs, mic_volume_percent)); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 AudioInputController* const controller_; | |
| 160 // We do not want any pending posted tasks generated from the callback class | |
| 161 // to keep the controller object alive longer than it should. So we use | |
| 162 // a weak ptr whenever we post, we use this weak pointer. | |
| 163 base::WeakPtr<AudioInputController> weak_controller_; | |
| 164 bool received_callback_ = false; | |
| 165 bool error_during_callback_ = false; | |
| 166 }; | |
| 85 | 167 |
| 86 // static | 168 // static |
| 87 AudioInputController::Factory* AudioInputController::factory_ = nullptr; | 169 AudioInputController::Factory* AudioInputController::factory_ = nullptr; |
| 88 | 170 |
| 89 AudioInputController::AudioInputController( | 171 AudioInputController::AudioInputController( |
| 172 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 90 EventHandler* handler, | 173 EventHandler* handler, |
| 91 SyncWriter* sync_writer, | 174 SyncWriter* sync_writer, |
| 92 std::unique_ptr<AudioFileWriter> debug_writer, | 175 std::unique_ptr<AudioFileWriter> debug_writer, |
| 93 UserInputMonitor* user_input_monitor, | 176 UserInputMonitor* user_input_monitor, |
| 94 const bool agc_is_enabled) | 177 const bool agc_is_enabled) |
| 95 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 178 : creator_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 179 task_runner_(std::move(task_runner)), | |
| 96 handler_(handler), | 180 handler_(handler), |
| 97 stream_(nullptr), | 181 stream_(nullptr), |
| 98 should_report_stats(0), | |
| 99 state_(CLOSED), | |
| 100 sync_writer_(sync_writer), | 182 sync_writer_(sync_writer), |
| 101 max_volume_(0.0), | 183 max_volume_(0.0), |
| 102 user_input_monitor_(user_input_monitor), | 184 user_input_monitor_(user_input_monitor), |
| 103 agc_is_enabled_(agc_is_enabled), | 185 agc_is_enabled_(agc_is_enabled), |
| 104 #if defined(AUDIO_POWER_MONITORING) | 186 #if defined(AUDIO_POWER_MONITORING) |
| 105 power_measurement_is_enabled_(false), | 187 power_measurement_is_enabled_(false), |
| 106 log_silence_state_(false), | 188 log_silence_state_(false), |
| 107 silence_state_(SILENCE_STATE_NO_MEASUREMENT), | 189 silence_state_(SILENCE_STATE_NO_MEASUREMENT), |
| 108 #endif | 190 #endif |
| 109 prev_key_down_count_(0), | 191 prev_key_down_count_(0), |
| 110 debug_writer_(std::move(debug_writer)) { | 192 debug_writer_(std::move(debug_writer)), |
| 193 weak_ptr_factory_(this) { | |
| 111 DCHECK(creator_task_runner_.get()); | 194 DCHECK(creator_task_runner_.get()); |
| 195 DCHECK(handler_); | |
| 196 DCHECK(sync_writer_); | |
| 112 } | 197 } |
| 113 | 198 |
| 114 AudioInputController::~AudioInputController() { | 199 AudioInputController::~AudioInputController() { |
| 115 DCHECK_EQ(state_, CLOSED); | 200 DCHECK(!audio_callback_); |
| 201 DCHECK(!stream_); | |
| 116 } | 202 } |
| 117 | 203 |
| 118 // static | 204 // static |
| 119 scoped_refptr<AudioInputController> AudioInputController::Create( | 205 scoped_refptr<AudioInputController> AudioInputController::Create( |
| 120 AudioManager* audio_manager, | 206 AudioManager* audio_manager, |
| 121 EventHandler* event_handler, | 207 EventHandler* event_handler, |
| 208 SyncWriter* sync_writer, | |
| 122 const AudioParameters& params, | 209 const AudioParameters& params, |
| 123 const std::string& device_id, | 210 const std::string& device_id, |
| 124 UserInputMonitor* user_input_monitor) { | 211 UserInputMonitor* user_input_monitor) { |
| 125 DCHECK(audio_manager); | 212 DCHECK(audio_manager); |
| 213 DCHECK(event_handler); | |
| 214 DCHECK(sync_writer); | |
| 126 | 215 |
| 127 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) | 216 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) |
| 128 return nullptr; | 217 return nullptr; |
| 129 | 218 |
| 130 if (factory_) { | 219 if (factory_) { |
| 131 return factory_->Create(audio_manager->GetTaskRunner(), | 220 return factory_->Create(audio_manager->GetTaskRunner(), sync_writer, |
| 132 /*sync_writer*/ nullptr, audio_manager, | 221 audio_manager, event_handler, params, |
| 133 event_handler, params, user_input_monitor); | 222 user_input_monitor); |
| 134 } | 223 } |
| 135 | 224 |
| 136 scoped_refptr<AudioInputController> controller(new AudioInputController( | 225 scoped_refptr<AudioInputController> controller(new AudioInputController( |
| 137 event_handler, nullptr, nullptr, user_input_monitor, false)); | 226 audio_manager->GetTaskRunner(), event_handler, sync_writer, nullptr, |
| 138 | 227 user_input_monitor, false)); |
| 139 controller->task_runner_ = audio_manager->GetTaskRunner(); | |
| 140 | 228 |
| 141 // Create and open a new audio input stream from the existing | 229 // Create and open a new audio input stream from the existing |
| 142 // audio-device thread. | 230 // audio-device thread. |
| 143 if (!controller->task_runner_->PostTask( | 231 if (!controller->task_runner_->PostTask( |
| 144 FROM_HERE, | 232 FROM_HERE, |
| 145 base::Bind(&AudioInputController::DoCreate, | 233 base::Bind(&AudioInputController::DoCreate, |
| 146 controller, | 234 controller, |
| 147 base::Unretained(audio_manager), | 235 base::Unretained(audio_manager), |
| 148 params, | 236 params, |
| 149 device_id))) { | 237 device_id))) { |
| 150 controller = nullptr; | 238 controller = nullptr; |
| 151 } | 239 } |
| 152 | 240 |
| 153 return controller; | 241 return controller; |
| 154 } | 242 } |
| 155 | 243 |
| 156 // static | 244 // static |
| 157 scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency( | 245 scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency( |
| 158 AudioManager* audio_manager, | 246 AudioManager* audio_manager, |
| 159 EventHandler* event_handler, | 247 EventHandler* event_handler, |
| 160 const AudioParameters& params, | 248 const AudioParameters& params, |
| 161 const std::string& device_id, | 249 const std::string& device_id, |
| 162 SyncWriter* sync_writer, | 250 SyncWriter* sync_writer, |
| 163 std::unique_ptr<AudioFileWriter> debug_writer, | 251 std::unique_ptr<AudioFileWriter> debug_writer, |
| 164 UserInputMonitor* user_input_monitor, | 252 UserInputMonitor* user_input_monitor, |
| 165 const bool agc_is_enabled) { | 253 const bool agc_is_enabled) { |
| 166 DCHECK(audio_manager); | 254 DCHECK(audio_manager); |
| 167 DCHECK(sync_writer); | 255 DCHECK(sync_writer); |
| 256 DCHECK(event_handler); | |
| 168 | 257 |
| 169 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) | 258 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) |
| 170 return nullptr; | 259 return nullptr; |
| 171 | 260 |
| 172 if (factory_) { | 261 if (factory_) { |
| 173 return factory_->Create(audio_manager->GetTaskRunner(), sync_writer, | 262 return factory_->Create(audio_manager->GetTaskRunner(), sync_writer, |
| 174 audio_manager, event_handler, params, | 263 audio_manager, event_handler, params, |
| 175 user_input_monitor); | 264 user_input_monitor); |
| 176 } | 265 } |
| 177 | 266 |
| 178 // Create the AudioInputController object and ensure that it runs on | 267 // Create the AudioInputController object and ensure that it runs on |
| 179 // the audio-manager thread. | 268 // the audio-manager thread. |
| 180 scoped_refptr<AudioInputController> controller(new AudioInputController( | 269 scoped_refptr<AudioInputController> controller(new AudioInputController( |
| 181 event_handler, sync_writer, std::move(debug_writer), user_input_monitor, | 270 audio_manager->GetTaskRunner(), event_handler, sync_writer, |
| 182 agc_is_enabled)); | 271 std::move(debug_writer), user_input_monitor, agc_is_enabled)); |
| 183 controller->task_runner_ = audio_manager->GetTaskRunner(); | |
| 184 | 272 |
| 185 // Create and open a new audio input stream from the existing | 273 // Create and open a new audio input stream from the existing |
| 186 // audio-device thread. Use the provided audio-input device. | 274 // audio-device thread. Use the provided audio-input device. |
| 187 if (!controller->task_runner_->PostTask( | 275 if (!controller->task_runner_->PostTask( |
| 188 FROM_HERE, | 276 FROM_HERE, |
| 189 base::Bind(&AudioInputController::DoCreateForLowLatency, | 277 base::Bind(&AudioInputController::DoCreateForLowLatency, |
| 190 controller, | 278 controller, |
| 191 base::Unretained(audio_manager), | 279 base::Unretained(audio_manager), |
| 192 params, | 280 params, |
| 193 device_id))) { | 281 device_id))) { |
| 194 controller = nullptr; | 282 controller = nullptr; |
| 195 } | 283 } |
| 196 | 284 |
| 197 return controller; | 285 return controller; |
| 198 } | 286 } |
| 199 | 287 |
| 200 // static | 288 // static |
| 201 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( | 289 scoped_refptr<AudioInputController> AudioInputController::CreateForStream( |
| 202 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 290 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 203 EventHandler* event_handler, | 291 EventHandler* event_handler, |
| 204 AudioInputStream* stream, | 292 AudioInputStream* stream, |
| 205 SyncWriter* sync_writer, | 293 SyncWriter* sync_writer, |
| 206 std::unique_ptr<AudioFileWriter> debug_writer, | 294 std::unique_ptr<AudioFileWriter> debug_writer, |
| 207 UserInputMonitor* user_input_monitor) { | 295 UserInputMonitor* user_input_monitor) { |
| 208 DCHECK(sync_writer); | 296 DCHECK(sync_writer); |
| 209 DCHECK(stream); | 297 DCHECK(stream); |
| 298 DCHECK(event_handler); | |
| 210 | 299 |
| 211 if (factory_) { | 300 if (factory_) { |
| 212 return factory_->Create( | 301 return factory_->Create( |
| 213 task_runner, sync_writer, AudioManager::Get(), event_handler, | 302 task_runner, sync_writer, AudioManager::Get(), event_handler, |
| 214 media::AudioParameters::UnavailableDeviceParams(), user_input_monitor); | 303 AudioParameters::UnavailableDeviceParams(), user_input_monitor); |
| 215 } | 304 } |
| 216 | 305 |
| 217 // Create the AudioInputController object and ensure that it runs on | 306 // Create the AudioInputController object and ensure that it runs on |
| 218 // the audio-manager thread. | 307 // the audio-manager thread. |
| 219 scoped_refptr<AudioInputController> controller(new AudioInputController( | 308 scoped_refptr<AudioInputController> controller(new AudioInputController( |
| 220 event_handler, sync_writer, std::move(debug_writer), user_input_monitor, | 309 task_runner, event_handler, sync_writer, std::move(debug_writer), |
| 221 false)); | 310 user_input_monitor, false)); |
| 222 controller->task_runner_ = task_runner; | |
| 223 | 311 |
| 224 if (!controller->task_runner_->PostTask( | 312 if (!controller->task_runner_->PostTask( |
| 225 FROM_HERE, | 313 FROM_HERE, |
| 226 base::Bind(&AudioInputController::DoCreateForStream, | 314 base::Bind(&AudioInputController::DoCreateForStream, |
| 227 controller, | 315 controller, |
| 228 stream))) { | 316 stream))) { |
| 229 controller = nullptr; | 317 controller = nullptr; |
| 230 } | 318 } |
| 231 | 319 |
| 232 return controller; | 320 return controller; |
| 233 } | 321 } |
| 234 | 322 |
| 235 void AudioInputController::Record() { | 323 void AudioInputController::Record() { |
| 324 DCHECK(creator_task_runner_->BelongsToCurrentThread()); | |
| 236 task_runner_->PostTask(FROM_HERE, base::Bind( | 325 task_runner_->PostTask(FROM_HERE, base::Bind( |
| 237 &AudioInputController::DoRecord, this)); | 326 &AudioInputController::DoRecord, this)); |
| 238 } | 327 } |
| 239 | 328 |
| 240 void AudioInputController::Close(const base::Closure& closed_task) { | 329 void AudioInputController::Close(const base::Closure& closed_task) { |
| 241 DCHECK(!closed_task.is_null()); | 330 DCHECK(!closed_task.is_null()); |
| 242 DCHECK(creator_task_runner_->BelongsToCurrentThread()); | 331 DCHECK(creator_task_runner_->BelongsToCurrentThread()); |
| 243 | 332 |
| 244 task_runner_->PostTaskAndReply( | 333 task_runner_->PostTaskAndReply( |
| 245 FROM_HERE, base::Bind(&AudioInputController::DoClose, this), closed_task); | 334 FROM_HERE, base::Bind(&AudioInputController::DoClose, this), closed_task); |
| 246 } | 335 } |
| 247 | 336 |
| 248 void AudioInputController::SetVolume(double volume) { | 337 void AudioInputController::SetVolume(double volume) { |
| 338 DCHECK(creator_task_runner_->BelongsToCurrentThread()); | |
| 249 task_runner_->PostTask(FROM_HERE, base::Bind( | 339 task_runner_->PostTask(FROM_HERE, base::Bind( |
| 250 &AudioInputController::DoSetVolume, this, volume)); | 340 &AudioInputController::DoSetVolume, this, volume)); |
| 251 } | 341 } |
| 252 | 342 |
| 253 void AudioInputController::DoCreate(AudioManager* audio_manager, | 343 void AudioInputController::DoCreate(AudioManager* audio_manager, |
| 254 const AudioParameters& params, | 344 const AudioParameters& params, |
| 255 const std::string& device_id) { | 345 const std::string& device_id) { |
| 256 DCHECK(task_runner_->BelongsToCurrentThread()); | 346 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 347 DCHECK(!stream_); | |
| 257 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CreateTime"); | 348 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CreateTime"); |
| 258 if (handler_) | 349 handler_->OnLog(this, "AIC::DoCreate"); |
| 259 handler_->OnLog(this, "AIC::DoCreate"); | |
| 260 | 350 |
| 261 #if defined(AUDIO_POWER_MONITORING) | 351 #if defined(AUDIO_POWER_MONITORING) |
| 262 // Disable power monitoring for streams that run without AGC enabled to | 352 // Disable power monitoring for streams that run without AGC enabled to |
| 263 // avoid adding logs and UMA for non-WebRTC clients. | 353 // avoid adding logs and UMA for non-WebRTC clients. |
| 264 power_measurement_is_enabled_ = agc_is_enabled_; | 354 power_measurement_is_enabled_ = agc_is_enabled_; |
| 265 last_audio_level_log_time_ = base::TimeTicks::Now(); | 355 last_audio_level_log_time_ = base::TimeTicks::Now(); |
| 266 silence_state_ = SILENCE_STATE_NO_MEASUREMENT; | 356 silence_state_ = SILENCE_STATE_NO_MEASUREMENT; |
| 267 #endif | 357 #endif |
| 268 | 358 |
| 359 // MakeAudioInputStream might fail and return nullptr. If so, | |
| 360 // DoCreateForStream will handle and report it. | |
| 269 DoCreateForStream(audio_manager->MakeAudioInputStream( | 361 DoCreateForStream(audio_manager->MakeAudioInputStream( |
| 270 params, device_id, base::Bind(&AudioInputController::LogMessage, this))); | 362 params, device_id, base::Bind(&AudioInputController::LogMessage, this))); |
| 271 } | 363 } |
| 272 | 364 |
| 273 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, | 365 void AudioInputController::DoCreateForLowLatency(AudioManager* audio_manager, |
| 274 const AudioParameters& params, | 366 const AudioParameters& params, |
| 275 const std::string& device_id) { | 367 const std::string& device_id) { |
| 276 DCHECK(task_runner_->BelongsToCurrentThread()); | 368 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 277 | 369 |
| 278 #if defined(AUDIO_POWER_MONITORING) | 370 #if defined(AUDIO_POWER_MONITORING) |
| 279 // We only log silence state UMA stats for low latency mode and if we use a | 371 // We only log silence state UMA stats for low latency mode and if we use a |
| 280 // real device. | 372 // real device. |
| 281 if (params.format() != AudioParameters::AUDIO_FAKE) | 373 if (params.format() != AudioParameters::AUDIO_FAKE) |
| 282 log_silence_state_ = true; | 374 log_silence_state_ = true; |
| 283 #endif | 375 #endif |
| 284 | 376 |
| 285 low_latency_create_time_ = base::TimeTicks::Now(); | 377 low_latency_create_time_ = base::TimeTicks::Now(); |
| 286 DoCreate(audio_manager, params, device_id); | 378 DoCreate(audio_manager, params, device_id); |
| 287 } | 379 } |
| 288 | 380 |
| 289 void AudioInputController::DoCreateForStream( | 381 void AudioInputController::DoCreateForStream( |
| 290 AudioInputStream* stream_to_control) { | 382 AudioInputStream* stream_to_control) { |
| 291 DCHECK(task_runner_->BelongsToCurrentThread()); | 383 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 384 DCHECK(!stream_); | |
| 292 | 385 |
| 293 DCHECK(!stream_); | 386 if (!stream_to_control) { |
| 294 stream_ = stream_to_control; | |
| 295 should_report_stats = 1; | |
| 296 | |
| 297 if (!stream_) { | |
| 298 if (handler_) | |
| 299 handler_->OnError(this, STREAM_CREATE_ERROR); | |
| 300 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); | 387 LogCaptureStartupResult(CAPTURE_STARTUP_CREATE_STREAM_FAILED); |
| 388 handler_->OnError(this, STREAM_CREATE_ERROR); | |
| 301 return; | 389 return; |
| 302 } | 390 } |
| 303 | 391 |
| 304 if (stream_ && !stream_->Open()) { | 392 if (!stream_to_control->Open()) { |
| 305 stream_->Close(); | 393 stream_to_control->Close(); |
| 306 stream_ = nullptr; | |
| 307 if (handler_) | |
| 308 handler_->OnError(this, STREAM_OPEN_ERROR); | |
| 309 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); | 394 LogCaptureStartupResult(CAPTURE_STARTUP_OPEN_STREAM_FAILED); |
| 395 handler_->OnError(this, STREAM_OPEN_ERROR); | |
| 310 return; | 396 return; |
| 311 } | 397 } |
| 312 | 398 |
| 313 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in | 399 // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in |
| 314 // CreateLowLatency(). | 400 // CreateLowLatency(). |
| 315 #if defined(AUDIO_POWER_MONITORING) | 401 #if defined(AUDIO_POWER_MONITORING) |
| 316 bool agc_is_supported = false; | 402 bool agc_is_supported = |
| 317 agc_is_supported = stream_->SetAutomaticGainControl(agc_is_enabled_); | 403 stream_to_control->SetAutomaticGainControl(agc_is_enabled_); |
| 318 // Disable power measurements on platforms that does not support AGC at a | 404 // Disable power measurements on platforms that does not support AGC at a |
| 319 // lower level. AGC can fail on platforms where we don't support the | 405 // lower level. AGC can fail on platforms where we don't support the |
| 320 // functionality to modify the input volume slider. One such example is | 406 // functionality to modify the input volume slider. One such example is |
| 321 // Windows XP. | 407 // Windows XP. |
| 322 power_measurement_is_enabled_ &= agc_is_supported; | 408 power_measurement_is_enabled_ &= agc_is_supported; |
| 323 #else | 409 #else |
| 324 stream_->SetAutomaticGainControl(agc_is_enabled_); | 410 stream_to_control->SetAutomaticGainControl(agc_is_enabled_); |
| 325 #endif | 411 #endif |
| 326 | 412 |
| 327 | |
| 328 state_ = CREATED; | |
| 329 if (handler_) | |
| 330 handler_->OnCreated(this); | |
| 331 | |
| 332 if (user_input_monitor_) { | 413 if (user_input_monitor_) { |
| 333 user_input_monitor_->EnableKeyPressMonitoring(); | 414 user_input_monitor_->EnableKeyPressMonitoring(); |
| 334 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); | 415 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); |
|
o1ka
2017/01/16 15:04:52
Shouldn't we init it right before starting the str
tommi (sloooow) - chröme
2017/01/16 17:02:32
Good point, moved. In general I think this isn't
| |
| 335 } | 416 } |
| 417 | |
| 418 // Finally, keep the stream pointer around, update the state and notify. | |
| 419 stream_ = stream_to_control; | |
| 420 handler_->OnCreated(this); | |
| 336 } | 421 } |
| 337 | 422 |
| 338 void AudioInputController::DoRecord() { | 423 void AudioInputController::DoRecord() { |
| 339 DCHECK(task_runner_->BelongsToCurrentThread()); | 424 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 340 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); | 425 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); |
| 341 | 426 |
| 342 if (state_ != CREATED) | 427 if (!stream_ || audio_callback_) |
| 343 return; | 428 return; |
| 344 | 429 |
| 345 { | 430 audio_callback_.reset(new AudioCallback(this)); |
| 346 base::AutoLock auto_lock(lock_); | |
| 347 state_ = RECORDING; | |
| 348 } | |
| 349 | 431 |
| 350 if (handler_) | 432 handler_->OnLog(this, "AIC::DoRecord"); |
| 351 handler_->OnLog(this, "AIC::DoRecord"); | |
| 352 | 433 |
| 353 stream_->Start(this); | 434 stream_->Start(audio_callback_.get()); |
| 354 } | 435 } |
| 355 | 436 |
| 356 void AudioInputController::DoClose() { | 437 void AudioInputController::DoClose() { |
| 357 DCHECK(task_runner_->BelongsToCurrentThread()); | 438 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 358 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); | 439 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); |
| 359 // If we have already logged something, this does nothing. | |
| 360 // Otherwise, we haven't recieved data. | |
| 361 LogCaptureStartupResult(CAPTURE_STARTUP_NEVER_GOT_DATA); | |
| 362 | 440 |
| 363 if (state_ == CLOSED) | 441 if (!stream_) |
| 364 return; | 442 return; |
| 365 | 443 |
| 366 // If this is a low-latency stream, log the total duration (since DoCreate) | 444 // Allow calling unconditionally and bail if we don't have a stream to close. |
| 367 // and add it to a UMA histogram. | 445 if (audio_callback_) { |
| 368 if (!low_latency_create_time_.is_null()) { | 446 stream_->Stop(); |
| 369 base::TimeDelta duration = | 447 |
| 370 base::TimeTicks::Now() - low_latency_create_time_; | 448 if (!low_latency_create_time_.is_null()) { |
| 371 UMA_HISTOGRAM_LONG_TIMES("Media.InputStreamDuration", duration); | 449 LogCaptureStartupResult(audio_callback_->received_callback() |
| 372 if (handler_) { | 450 ? CAPTURE_STARTUP_OK |
| 373 std::string log_string = | 451 : CAPTURE_STARTUP_NEVER_GOT_DATA); |
| 374 base::StringPrintf("AIC::DoClose: stream duration="); | 452 UMA_HISTOGRAM_BOOLEAN("Media.Audio.Capture.CallbackError", |
| 375 log_string += base::Int64ToString(duration.InSeconds()); | 453 audio_callback_->error_during_callback()); |
| 376 log_string += " seconds"; | 454 if (audio_callback_->received_callback()) { |
| 377 handler_->OnLog(this, log_string); | 455 // Log the total duration (since DoCreate) and update the histogram. |
| 456 const base::TimeDelta duration = | |
| 457 base::TimeTicks::Now() - low_latency_create_time_; | |
| 458 UMA_HISTOGRAM_LONG_TIMES("Media.InputStreamDuration", duration); | |
| 459 const std::string log_string = base::StringPrintf( | |
| 460 "AIC::DoClose: stream duration=%" PRId64 " seconds", | |
| 461 duration.InSeconds()); | |
| 462 handler_->OnLog(this, log_string); | |
| 463 } | |
| 378 } | 464 } |
| 465 | |
| 466 audio_callback_.reset(); | |
| 379 } | 467 } |
| 380 | 468 |
| 381 DoStopCloseAndClearStream(); | 469 stream_->Close(); |
| 470 stream_ = nullptr; | |
| 382 | 471 |
| 383 if (SharedMemoryAndSyncSocketMode()) | 472 sync_writer_->Close(); |
| 384 sync_writer_->Close(); | |
| 385 | 473 |
| 386 if (user_input_monitor_) | 474 if (user_input_monitor_) |
| 387 user_input_monitor_->DisableKeyPressMonitoring(); | 475 user_input_monitor_->DisableKeyPressMonitoring(); |
| 388 | 476 |
| 389 #if defined(AUDIO_POWER_MONITORING) | 477 #if defined(AUDIO_POWER_MONITORING) |
| 390 // Send UMA stats if enabled. | 478 // Send UMA stats if enabled. |
| 391 if (log_silence_state_) | 479 if (log_silence_state_) |
| 392 LogSilenceState(silence_state_); | 480 LogSilenceState(silence_state_); |
| 393 log_silence_state_ = false; | 481 log_silence_state_ = false; |
| 394 #endif | 482 #endif |
| 395 | 483 |
| 396 if (debug_writer_) | 484 if (debug_writer_) |
| 397 debug_writer_->Stop(); | 485 debug_writer_->Stop(); |
| 398 | 486 |
| 399 state_ = CLOSED; | 487 max_volume_ = 0.0; |
| 488 low_latency_create_time_ = base::TimeTicks(); // Reset to null. | |
| 489 weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
o1ka
2017/01/16 15:04:52
We can do it first thing after l.442 to avoid post
o1ka
2017/01/16 15:08:51
Ah, disregard this, I forgot that post does not ch
tommi (sloooow) - chröme
2017/01/16 17:02:32
Acknowledged.
| |
| 400 } | 490 } |
| 401 | 491 |
| 402 void AudioInputController::DoReportError() { | 492 void AudioInputController::DoReportError() { |
| 403 DCHECK(task_runner_->BelongsToCurrentThread()); | 493 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 404 if (handler_) | 494 handler_->OnError(this, STREAM_ERROR); |
| 405 handler_->OnError(this, STREAM_ERROR); | |
| 406 } | 495 } |
| 407 | 496 |
| 408 void AudioInputController::DoSetVolume(double volume) { | 497 void AudioInputController::DoSetVolume(double volume) { |
| 409 DCHECK(task_runner_->BelongsToCurrentThread()); | 498 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 410 DCHECK_GE(volume, 0); | 499 DCHECK_GE(volume, 0); |
| 411 DCHECK_LE(volume, 1.0); | 500 DCHECK_LE(volume, 1.0); |
| 412 | 501 |
| 413 if (state_ != CREATED && state_ != RECORDING) | 502 if (!stream_) |
| 414 return; | 503 return; |
| 415 | 504 |
| 416 // Only ask for the maximum volume at first call and use cached value | 505 // Only ask for the maximum volume at first call and use cached value |
| 417 // for remaining function calls. | 506 // for remaining function calls. |
| 418 if (!max_volume_) { | 507 if (!max_volume_) { |
| 419 max_volume_ = stream_->GetMaxVolume(); | 508 max_volume_ = stream_->GetMaxVolume(); |
| 420 } | 509 } |
| 421 | 510 |
| 422 if (max_volume_ == 0.0) { | 511 if (max_volume_ == 0.0) { |
| 423 DLOG(WARNING) << "Failed to access input volume control"; | 512 DLOG(WARNING) << "Failed to access input volume control"; |
| 424 return; | 513 return; |
| 425 } | 514 } |
| 426 | 515 |
| 427 // Set the stream volume and scale to a range matched to the platform. | 516 // Set the stream volume and scale to a range matched to the platform. |
| 428 stream_->SetVolume(max_volume_ * volume); | 517 stream_->SetVolume(max_volume_ * volume); |
| 429 } | 518 } |
| 430 | 519 |
| 431 void AudioInputController::OnData(AudioInputStream* stream, | |
| 432 const AudioBus* source, | |
| 433 uint32_t hardware_delay_bytes, | |
| 434 double volume) { | |
| 435 TRACE_EVENT0("audio", "AudioInputController::OnData"); | |
| 436 if (debug_writer_ && debug_writer_->WillWrite()) { | |
| 437 std::unique_ptr<AudioBus> source_copy = | |
| 438 AudioBus::Create(source->channels(), source->frames()); | |
| 439 source->CopyTo(source_copy.get()); | |
| 440 task_runner_->PostTask( | |
| 441 FROM_HERE, | |
| 442 base::Bind( | |
| 443 &AudioInputController::WriteInputDataForDebugging, | |
| 444 this, | |
| 445 base::Passed(&source_copy))); | |
| 446 } | |
| 447 // Now we have data, so we know for sure that startup was ok. | |
| 448 LogCaptureStartupResult(CAPTURE_STARTUP_OK); | |
| 449 | |
| 450 { | |
| 451 base::AutoLock auto_lock(lock_); | |
| 452 if (state_ != RECORDING) | |
| 453 return; | |
| 454 } | |
| 455 | |
| 456 bool key_pressed = false; | |
| 457 if (user_input_monitor_) { | |
| 458 size_t current_count = user_input_monitor_->GetKeyPressCount(); | |
| 459 key_pressed = current_count != prev_key_down_count_; | |
| 460 prev_key_down_count_ = current_count; | |
| 461 DVLOG_IF(6, key_pressed) << "Detected keypress."; | |
| 462 } | |
| 463 | |
| 464 // Use SharedMemory and SyncSocket if the client has created a SyncWriter. | |
| 465 // Used by all low-latency clients except WebSpeech. | |
| 466 if (SharedMemoryAndSyncSocketMode()) { | |
| 467 sync_writer_->Write(source, volume, key_pressed, hardware_delay_bytes); | |
| 468 | |
| 469 #if defined(AUDIO_POWER_MONITORING) | |
| 470 // Only do power-level measurements if DoCreate() has been called. It will | |
| 471 // ensure that logging will mainly be done for WebRTC and WebSpeech | |
| 472 // clients. | |
| 473 if (!power_measurement_is_enabled_) | |
| 474 return; | |
| 475 | |
| 476 // Perform periodic audio (power) level measurements. | |
| 477 if ((base::TimeTicks::Now() - last_audio_level_log_time_).InSeconds() > | |
| 478 kPowerMonitorLogIntervalSeconds) { | |
| 479 // Calculate the average power of the signal, or the energy per sample. | |
| 480 const float average_power_dbfs = AveragePower(*source); | |
| 481 | |
| 482 // Add current microphone volume to log and UMA histogram. | |
| 483 const int mic_volume_percent = static_cast<int>(100.0 * volume); | |
| 484 | |
| 485 // Use event handler on the audio thread to relay a message to the ARIH | |
| 486 // in content which does the actual logging on the IO thread. | |
| 487 task_runner_->PostTask(FROM_HERE, | |
| 488 base::Bind(&AudioInputController::DoLogAudioLevels, | |
| 489 this, | |
| 490 average_power_dbfs, | |
| 491 mic_volume_percent)); | |
| 492 | |
| 493 last_audio_level_log_time_ = base::TimeTicks::Now(); | |
| 494 } | |
| 495 #endif | |
| 496 return; | |
| 497 } | |
| 498 | |
| 499 // TODO(henrika): Investigate if we can avoid the extra copy here. | |
| 500 // (see http://crbug.com/249316 for details). AFAIK, this scope is only | |
| 501 // active for WebSpeech clients. | |
| 502 std::unique_ptr<AudioBus> audio_data = | |
| 503 AudioBus::Create(source->channels(), source->frames()); | |
| 504 source->CopyTo(audio_data.get()); | |
| 505 | |
| 506 // Ownership of the audio buffer will be with the callback until it is run, | |
| 507 // when ownership is passed to the callback function. | |
| 508 task_runner_->PostTask( | |
| 509 FROM_HERE, | |
| 510 base::Bind( | |
| 511 &AudioInputController::DoOnData, this, base::Passed(&audio_data))); | |
| 512 } | |
| 513 | |
| 514 void AudioInputController::DoOnData(std::unique_ptr<AudioBus> data) { | |
| 515 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 516 if (handler_) | |
| 517 handler_->OnData(this, data.get()); | |
| 518 } | |
| 519 | |
| 520 void AudioInputController::DoLogAudioLevels(float level_dbfs, | 520 void AudioInputController::DoLogAudioLevels(float level_dbfs, |
| 521 int microphone_volume_percent) { | 521 int microphone_volume_percent) { |
| 522 #if defined(AUDIO_POWER_MONITORING) | 522 #if defined(AUDIO_POWER_MONITORING) |
| 523 DCHECK(task_runner_->BelongsToCurrentThread()); | 523 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 524 if (!handler_) | 524 if (!stream_) |
| 525 return; | 525 return; |
| 526 | 526 |
| 527 // Detect if the user has enabled hardware mute by pressing the mute | 527 // Detect if the user has enabled hardware mute by pressing the mute |
| 528 // button in audio settings for the selected microphone. | 528 // button in audio settings for the selected microphone. |
| 529 const bool microphone_is_muted = stream_->IsMuted(); | 529 const bool microphone_is_muted = stream_->IsMuted(); |
| 530 if (microphone_is_muted) { | 530 if (microphone_is_muted) { |
| 531 LogMicrophoneMuteResult(MICROPHONE_IS_MUTED); | 531 LogMicrophoneMuteResult(MICROPHONE_IS_MUTED); |
| 532 handler_->OnLog(this, "AIC::OnData: microphone is muted!"); | 532 handler_->OnLog(this, "AIC::OnData: microphone is muted!"); |
| 533 // Return early if microphone is muted. No need to adding logs and UMA stats | 533 // Return early if microphone is muted. No need to adding logs and UMA stats |
| 534 // of audio levels if we know that the micropone is muted. | 534 // of audio levels if we know that the micropone is muted. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 548 | 548 |
| 549 UMA_HISTOGRAM_PERCENTAGE("Media.MicrophoneVolume", microphone_volume_percent); | 549 UMA_HISTOGRAM_PERCENTAGE("Media.MicrophoneVolume", microphone_volume_percent); |
| 550 log_string = base::StringPrintf( | 550 log_string = base::StringPrintf( |
| 551 "AIC::OnData: microphone volume=%d%%", microphone_volume_percent); | 551 "AIC::OnData: microphone volume=%d%%", microphone_volume_percent); |
| 552 if (microphone_volume_percent < kLowLevelMicrophoneLevelPercent) | 552 if (microphone_volume_percent < kLowLevelMicrophoneLevelPercent) |
| 553 log_string += " <=> low microphone level!"; | 553 log_string += " <=> low microphone level!"; |
| 554 handler_->OnLog(this, log_string); | 554 handler_->OnLog(this, log_string); |
| 555 #endif | 555 #endif |
| 556 } | 556 } |
| 557 | 557 |
| 558 void AudioInputController::OnError(AudioInputStream* stream) { | |
| 559 // Handle error on the audio-manager thread. | |
| 560 task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 561 &AudioInputController::DoReportError, this)); | |
| 562 } | |
| 563 | |
| 564 void AudioInputController::EnableDebugRecording( | 558 void AudioInputController::EnableDebugRecording( |
| 565 const base::FilePath& file_name) { | 559 const base::FilePath& file_name) { |
| 560 DCHECK(creator_task_runner_->BelongsToCurrentThread()); | |
| 566 task_runner_->PostTask( | 561 task_runner_->PostTask( |
| 567 FROM_HERE, base::Bind(&AudioInputController::DoEnableDebugRecording, this, | 562 FROM_HERE, base::Bind(&AudioInputController::DoEnableDebugRecording, this, |
| 568 file_name)); | 563 file_name)); |
| 569 } | 564 } |
| 570 | 565 |
| 571 void AudioInputController::DisableDebugRecording() { | 566 void AudioInputController::DisableDebugRecording() { |
| 572 DCHECK(creator_task_runner_->BelongsToCurrentThread()); | 567 DCHECK(creator_task_runner_->BelongsToCurrentThread()); |
| 573 task_runner_->PostTask( | 568 task_runner_->PostTask( |
| 574 FROM_HERE, | 569 FROM_HERE, |
| 575 base::Bind(&AudioInputController::DoDisableDebugRecording, this)); | 570 base::Bind(&AudioInputController::DoDisableDebugRecording, this)); |
| 576 } | 571 } |
| 577 | 572 |
| 578 void AudioInputController::DoStopCloseAndClearStream() { | |
| 579 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 580 | |
| 581 // Allow calling unconditionally and bail if we don't have a stream to close. | |
| 582 if (stream_ != nullptr) { | |
| 583 stream_->Stop(); | |
| 584 stream_->Close(); | |
| 585 stream_ = nullptr; | |
| 586 } | |
| 587 | |
| 588 // The event handler should not be touched after the stream has been closed. | |
| 589 handler_ = nullptr; | |
| 590 } | |
| 591 | |
| 592 #if defined(AUDIO_POWER_MONITORING) | 573 #if defined(AUDIO_POWER_MONITORING) |
| 593 void AudioInputController::UpdateSilenceState(bool silence) { | 574 void AudioInputController::UpdateSilenceState(bool silence) { |
| 594 if (silence) { | 575 if (silence) { |
| 595 if (silence_state_ == SILENCE_STATE_NO_MEASUREMENT) { | 576 if (silence_state_ == SILENCE_STATE_NO_MEASUREMENT) { |
| 596 silence_state_ = SILENCE_STATE_ONLY_SILENCE; | 577 silence_state_ = SILENCE_STATE_ONLY_SILENCE; |
| 597 } else if (silence_state_ == SILENCE_STATE_ONLY_AUDIO) { | 578 } else if (silence_state_ == SILENCE_STATE_ONLY_AUDIO) { |
| 598 silence_state_ = SILENCE_STATE_AUDIO_AND_SILENCE; | 579 silence_state_ = SILENCE_STATE_AUDIO_AND_SILENCE; |
| 599 } else { | 580 } else { |
| 600 DCHECK(silence_state_ == SILENCE_STATE_ONLY_SILENCE || | 581 DCHECK(silence_state_ == SILENCE_STATE_ONLY_SILENCE || |
| 601 silence_state_ == SILENCE_STATE_AUDIO_AND_SILENCE); | 582 silence_state_ == SILENCE_STATE_AUDIO_AND_SILENCE); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 614 | 595 |
| 615 void AudioInputController::LogSilenceState(SilenceState value) { | 596 void AudioInputController::LogSilenceState(SilenceState value) { |
| 616 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerSessionSilenceReport", | 597 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerSessionSilenceReport", |
| 617 value, | 598 value, |
| 618 SILENCE_STATE_MAX + 1); | 599 SILENCE_STATE_MAX + 1); |
| 619 } | 600 } |
| 620 #endif | 601 #endif |
| 621 | 602 |
| 622 void AudioInputController::LogCaptureStartupResult( | 603 void AudioInputController::LogCaptureStartupResult( |
| 623 CaptureStartupResult result) { | 604 CaptureStartupResult result) { |
| 624 // Decrement shall_report_stats and check if it was 1 before decrement, | 605 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerCaptureStartupSuccess", |
| 625 // which would imply that this is the first time this method is called | 606 result, CAPTURE_STARTUP_RESULT_MAX + 1); |
| 626 // after initialization. To avoid underflow, we | |
| 627 // also check if should_report_stats is one before decrementing. | |
| 628 if (base::AtomicRefCountIsOne(&should_report_stats) && | |
| 629 !base::AtomicRefCountDec(&should_report_stats)) { | |
| 630 UMA_HISTOGRAM_ENUMERATION("Media.AudioInputControllerCaptureStartupSuccess", | |
| 631 result, CAPTURE_STARTUP_RESULT_MAX + 1); | |
| 632 } | |
| 633 } | 607 } |
| 634 | 608 |
| 635 void AudioInputController::DoEnableDebugRecording( | 609 void AudioInputController::DoEnableDebugRecording( |
| 636 const base::FilePath& file_name) { | 610 const base::FilePath& file_name) { |
| 637 DCHECK(task_runner_->BelongsToCurrentThread()); | 611 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 638 if (debug_writer_) | 612 if (debug_writer_) |
| 639 debug_writer_->Start(file_name); | 613 debug_writer_->Start(file_name); |
| 640 } | 614 } |
| 641 | 615 |
| 642 void AudioInputController::DoDisableDebugRecording() { | 616 void AudioInputController::DoDisableDebugRecording() { |
| 643 DCHECK(task_runner_->BelongsToCurrentThread()); | 617 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 644 if (debug_writer_) | 618 if (debug_writer_) |
| 645 debug_writer_->Stop(); | 619 debug_writer_->Stop(); |
| 646 } | 620 } |
| 647 | 621 |
| 648 void AudioInputController::WriteInputDataForDebugging( | 622 void AudioInputController::WriteInputDataForDebugging( |
| 649 std::unique_ptr<AudioBus> data) { | 623 std::unique_ptr<AudioBus> data) { |
| 650 DCHECK(task_runner_->BelongsToCurrentThread()); | 624 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 651 if (debug_writer_) | 625 if (debug_writer_) |
| 652 debug_writer_->Write(std::move(data)); | 626 debug_writer_->Write(std::move(data)); |
| 653 } | 627 } |
| 654 | 628 |
| 655 void AudioInputController::LogMessage(const std::string& message) { | 629 void AudioInputController::LogMessage(const std::string& message) { |
| 656 DCHECK(task_runner_->BelongsToCurrentThread()); | 630 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 657 handler_->OnLog(this, message); | 631 handler_->OnLog(this, message); |
| 658 } | 632 } |
| 659 | 633 |
| 634 bool AudioInputController::CheckForKeyboardInput() { | |
| 635 if (!user_input_monitor_) | |
| 636 return false; | |
| 637 | |
| 638 const size_t current_count = user_input_monitor_->GetKeyPressCount(); | |
| 639 const bool key_pressed = current_count != prev_key_down_count_; | |
| 640 prev_key_down_count_ = current_count; | |
| 641 DVLOG_IF(6, key_pressed) << "Detected keypress."; | |
| 642 | |
| 643 return key_pressed; | |
| 644 } | |
| 645 | |
| 646 bool AudioInputController::CheckAudioPower(const AudioBus* source, | |
| 647 double volume, | |
| 648 float* average_power_dbfs, | |
| 649 int* mic_volume_percent) { | |
| 650 #if defined(AUDIO_POWER_MONITORING) | |
| 651 // Only do power-level measurements if DoCreate() has been called. It will | |
| 652 // ensure that logging will mainly be done for WebRTC and WebSpeech | |
| 653 // clients. | |
| 654 if (!power_measurement_is_enabled_) | |
| 655 return false; | |
| 656 | |
| 657 // Perform periodic audio (power) level measurements. | |
| 658 const auto now = base::TimeTicks::Now(); | |
| 659 if ((now - last_audio_level_log_time_).InSeconds() <= | |
| 660 kPowerMonitorLogIntervalSeconds) { | |
| 661 return false; | |
| 662 } | |
| 663 | |
| 664 *average_power_dbfs = AveragePower(*source); | |
| 665 *mic_volume_percent = static_cast<int>(100.0 * volume); | |
| 666 | |
| 667 last_audio_level_log_time_ = now; | |
| 668 | |
| 669 return true; | |
| 670 #else | |
| 671 return false; | |
| 672 #endif | |
| 673 } | |
| 674 | |
| 660 } // namespace media | 675 } // namespace media |
| OLD | NEW |