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