Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: media/audio/audio_input_controller.cc

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

Powered by Google App Engine
This is Rietveld 408576698