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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CreateTime"); | 195 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CreateTime"); |
196 | 196 |
197 #if defined(AUDIO_POWER_MONITORING) | 197 #if defined(AUDIO_POWER_MONITORING) |
198 // Create the audio (power) level meter given the provided audio parameters. | 198 // Create the audio (power) level meter given the provided audio parameters. |
199 // An AudioBus is also needed to wrap the raw data buffer from the native | 199 // An AudioBus is also needed to wrap the raw data buffer from the native |
200 // layer to match AudioPowerMonitor::Scan(). | 200 // layer to match AudioPowerMonitor::Scan(). |
201 // TODO(henrika): Remove use of extra AudioBus. See http://crbug.com/375155. | 201 // TODO(henrika): Remove use of extra AudioBus. See http://crbug.com/375155. |
202 audio_level_.reset(new media::AudioPowerMonitor( | 202 audio_level_.reset(new media::AudioPowerMonitor( |
203 params.sample_rate(), | 203 params.sample_rate(), |
204 TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMilliseconds))); | 204 TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMilliseconds))); |
205 audio_bus_ = AudioBus::Create(params); | |
206 audio_params_ = params; | 205 audio_params_ = params; |
207 #endif | 206 #endif |
208 | 207 |
209 // TODO(miu): See TODO at top of file. Until that's resolved, assume all | 208 // TODO(miu): See TODO at top of file. Until that's resolved, assume all |
210 // platform audio input requires the |no_data_timer_| be used to auto-detect | 209 // platform audio input requires the |no_data_timer_| be used to auto-detect |
211 // errors. In reality, probably only Windows needs to be treated as | 210 // errors. In reality, probably only Windows needs to be treated as |
212 // unreliable here. | 211 // unreliable here. |
213 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id), | 212 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id), |
214 true); | 213 true); |
215 } | 214 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 375 |
377 // Restart the timer to ensure that we check the flag again in | 376 // Restart the timer to ensure that we check the flag again in |
378 // |kTimerResetIntervalSeconds|. | 377 // |kTimerResetIntervalSeconds|. |
379 no_data_timer_->Start( | 378 no_data_timer_->Start( |
380 FROM_HERE, base::TimeDelta::FromSeconds(kTimerResetIntervalSeconds), | 379 FROM_HERE, base::TimeDelta::FromSeconds(kTimerResetIntervalSeconds), |
381 base::Bind(&AudioInputController::DoCheckForNoData, | 380 base::Bind(&AudioInputController::DoCheckForNoData, |
382 base::Unretained(this))); | 381 base::Unretained(this))); |
383 } | 382 } |
384 | 383 |
385 void AudioInputController::OnData(AudioInputStream* stream, | 384 void AudioInputController::OnData(AudioInputStream* stream, |
386 const uint8* data, | 385 const AudioBus* source, |
387 uint32 size, | |
388 uint32 hardware_delay_bytes, | 386 uint32 hardware_delay_bytes, |
389 double volume) { | 387 double volume) { |
390 // Mark data as active to ensure that the periodic calls to | 388 // Mark data as active to ensure that the periodic calls to |
391 // DoCheckForNoData() does not report an error to the event handler. | 389 // DoCheckForNoData() does not report an error to the event handler. |
392 SetDataIsActive(true); | 390 SetDataIsActive(true); |
393 | 391 |
394 { | 392 { |
395 base::AutoLock auto_lock(lock_); | 393 base::AutoLock auto_lock(lock_); |
396 if (state_ != RECORDING) | 394 if (state_ != RECORDING) |
397 return; | 395 return; |
398 } | 396 } |
399 | 397 |
400 bool key_pressed = false; | 398 bool key_pressed = false; |
401 if (user_input_monitor_) { | 399 if (user_input_monitor_) { |
402 size_t current_count = user_input_monitor_->GetKeyPressCount(); | 400 size_t current_count = user_input_monitor_->GetKeyPressCount(); |
403 key_pressed = current_count != prev_key_down_count_; | 401 key_pressed = current_count != prev_key_down_count_; |
404 prev_key_down_count_ = current_count; | 402 prev_key_down_count_ = current_count; |
405 DVLOG_IF(6, key_pressed) << "Detected keypress."; | 403 DVLOG_IF(6, key_pressed) << "Detected keypress."; |
406 } | 404 } |
407 | 405 |
408 // Use SharedMemory and SyncSocket if the client has created a SyncWriter. | 406 // Use SharedMemory and SyncSocket if the client has created a SyncWriter. |
409 // Used by all low-latency clients except WebSpeech. | 407 // Used by all low-latency clients except WebSpeech. |
410 if (SharedMemoryAndSyncSocketMode()) { | 408 if (SharedMemoryAndSyncSocketMode()) { |
411 sync_writer_->Write(data, size, volume, key_pressed); | 409 sync_writer_->Write(source, volume, key_pressed); |
412 sync_writer_->UpdateRecordedBytes(hardware_delay_bytes); | 410 sync_writer_->UpdateRecordedBytes(hardware_delay_bytes); |
413 | 411 |
414 #if defined(AUDIO_POWER_MONITORING) | 412 #if defined(AUDIO_POWER_MONITORING) |
415 // Only do power-level measurements if an AudioPowerMonitor object has | 413 // Only do power-level measurements if an AudioPowerMonitor object has |
416 // been created. Done in DoCreate() but not DoCreateForStream(), hence | 414 // been created. Done in DoCreate() but not DoCreateForStream(), hence |
417 // logging will mainly be done for WebRTC and WebSpeech clients. | 415 // logging will mainly be done for WebRTC and WebSpeech clients. |
418 if (!audio_level_) | 416 if (!audio_level_) |
419 return; | 417 return; |
420 | 418 |
421 // Perform periodic audio (power) level measurements. | 419 // Perform periodic audio (power) level measurements. |
422 if ((base::TimeTicks::Now() - last_audio_level_log_time_).InSeconds() > | 420 if ((base::TimeTicks::Now() - last_audio_level_log_time_).InSeconds() > |
423 kPowerMonitorLogIntervalSeconds) { | 421 kPowerMonitorLogIntervalSeconds) { |
424 // Wrap data into an AudioBus to match AudioPowerMonitor::Scan. | 422 // Wrap data into an AudioBus to match AudioPowerMonitor::Scan. |
425 // TODO(henrika): remove this section when capture side uses AudioBus. | 423 // TODO(henrika): remove this section when capture side uses AudioBus. |
426 // See http://crbug.com/375155 for details. | 424 // See http://crbug.com/375155 for details. |
427 audio_bus_->FromInterleaved( | 425 audio_level_->Scan(*source, source->frames()); |
428 data, audio_bus_->frames(), audio_params_.bits_per_sample() / 8); | |
429 audio_level_->Scan(*audio_bus_, audio_bus_->frames()); | |
430 | 426 |
431 // Get current average power level and add it to the log. | 427 // Get current average power level and add it to the log. |
432 // Possible range is given by [-inf, 0] dBFS. | 428 // Possible range is given by [-inf, 0] dBFS. |
433 std::pair<float, bool> result = audio_level_->ReadCurrentPowerAndClip(); | 429 std::pair<float, bool> result = audio_level_->ReadCurrentPowerAndClip(); |
434 | 430 |
435 // Use event handler on the audio thread to relay a message to the ARIH | 431 // Use event handler on the audio thread to relay a message to the ARIH |
436 // in content which does the actual logging on the IO thread. | 432 // in content which does the actual logging on the IO thread. |
437 task_runner_->PostTask( | 433 task_runner_->PostTask( |
438 FROM_HERE, | 434 FROM_HERE, |
439 base::Bind( | 435 base::Bind( |
440 &AudioInputController::DoLogAudioLevel, this, result.first)); | 436 &AudioInputController::DoLogAudioLevel, this, result.first)); |
441 | 437 |
442 last_audio_level_log_time_ = base::TimeTicks::Now(); | 438 last_audio_level_log_time_ = base::TimeTicks::Now(); |
443 | 439 |
444 // Reset the average power level (since we don't log continuously). | 440 // Reset the average power level (since we don't log continuously). |
445 audio_level_->Reset(); | 441 audio_level_->Reset(); |
446 } | 442 } |
447 #endif | 443 #endif |
448 | |
449 return; | 444 return; |
450 } | 445 } |
451 | 446 |
452 // TODO(henrika): Investigate if we can avoid the extra copy here. | 447 // TODO(henrika): Investigate if we can avoid the extra copy here. |
453 // (see http://crbug.com/249316 for details). AFAIK, this scope is only | 448 // (see http://crbug.com/249316 for details). AFAIK, this scope is only |
454 // active for WebSpeech clients. | 449 // active for WebSpeech clients. |
455 scoped_ptr<uint8[]> audio_data(new uint8[size]); | 450 scoped_ptr<AudioBus> audio_data = |
456 memcpy(audio_data.get(), data, size); | 451 AudioBus::Create(source->channels(), source->frames()); |
| 452 source->CopyTo(audio_data.get()); |
457 | 453 |
458 // Ownership of the audio buffer will be with the callback until it is run, | 454 // Ownership of the audio buffer will be with the callback until it is run, |
459 // when ownership is passed to the callback function. | 455 // when ownership is passed to the callback function. |
460 task_runner_->PostTask(FROM_HERE, base::Bind( | 456 task_runner_->PostTask( |
461 &AudioInputController::DoOnData, this, base::Passed(&audio_data), size)); | 457 FROM_HERE, |
| 458 base::Bind( |
| 459 &AudioInputController::DoOnData, this, base::Passed(&audio_data))); |
462 } | 460 } |
463 | 461 |
464 void AudioInputController::DoOnData(scoped_ptr<uint8[]> data, uint32 size) { | 462 void AudioInputController::DoOnData(scoped_ptr<AudioBus> data) { |
465 DCHECK(task_runner_->BelongsToCurrentThread()); | 463 DCHECK(task_runner_->BelongsToCurrentThread()); |
466 if (handler_) | 464 if (handler_) |
467 handler_->OnData(this, data.get(), size); | 465 handler_->OnData(this, data.get()); |
468 } | 466 } |
469 | 467 |
470 void AudioInputController::DoLogAudioLevel(float level_dbfs) { | 468 void AudioInputController::DoLogAudioLevel(float level_dbfs) { |
471 #if defined(AUDIO_POWER_MONITORING) | 469 #if defined(AUDIO_POWER_MONITORING) |
472 DCHECK(task_runner_->BelongsToCurrentThread()); | 470 DCHECK(task_runner_->BelongsToCurrentThread()); |
473 if (!handler_) | 471 if (!handler_) |
474 return; | 472 return; |
475 | 473 |
476 std::string log_string = base::StringPrintf( | 474 std::string log_string = base::StringPrintf( |
477 "AIC::OnData: average audio level=%.2f dBFS", level_dbfs); | 475 "AIC::OnData: average audio level=%.2f dBFS", level_dbfs); |
(...skipping 27 matching lines...) Expand all Loading... |
505 | 503 |
506 void AudioInputController::SetDataIsActive(bool enabled) { | 504 void AudioInputController::SetDataIsActive(bool enabled) { |
507 base::subtle::Release_Store(&data_is_active_, enabled); | 505 base::subtle::Release_Store(&data_is_active_, enabled); |
508 } | 506 } |
509 | 507 |
510 bool AudioInputController::GetDataIsActive() { | 508 bool AudioInputController::GetDataIsActive() { |
511 return (base::subtle::Acquire_Load(&data_is_active_) != false); | 509 return (base::subtle::Acquire_Load(&data_is_active_) != false); |
512 } | 510 } |
513 | 511 |
514 } // namespace media | 512 } // namespace media |
OLD | NEW |