audio_input_controller.cc revision 010d83a9304c5a91596085d917d248abff47903a
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 3// found in the LICENSE file. 4 5#include "media/audio/audio_input_controller.h" 6 7#include "base/bind.h" 8#include "base/threading/thread_restrictions.h" 9#include "media/base/limits.h" 10#include "media/base/scoped_histogram_timer.h" 11#include "media/base/user_input_monitor.h" 12 13namespace { 14const int kMaxInputChannels = 3; 15 16// TODO(henrika): remove usage of timers and add support for proper 17// notification of when the input device is removed. This was originally added 18// to resolve http://crbug.com/79936 for Windows platforms. This then caused 19// breakage (very hard to repro bugs!) on other platforms: See 20// http://crbug.com/226327 and http://crbug.com/230972. 21// See also that the timer has been disabled on Mac now due to 22// crbug.com/357501. 23const int kTimerResetIntervalSeconds = 1; 24// We have received reports that the timer can be too trigger happy on some 25// Mac devices and the initial timer interval has therefore been increased 26// from 1 second to 5 seconds. 27const int kTimerInitialIntervalSeconds = 5; 28} 29 30namespace media { 31 32// static 33AudioInputController::Factory* AudioInputController::factory_ = NULL; 34 35AudioInputController::AudioInputController(EventHandler* handler, 36 SyncWriter* sync_writer, 37 UserInputMonitor* user_input_monitor) 38 : creator_task_runner_(base::MessageLoopProxy::current()), 39 handler_(handler), 40 stream_(NULL), 41 data_is_active_(false), 42 state_(CLOSED), 43 sync_writer_(sync_writer), 44 max_volume_(0.0), 45 user_input_monitor_(user_input_monitor), 46 prev_key_down_count_(0) { 47 DCHECK(creator_task_runner_.get()); 48} 49 50AudioInputController::~AudioInputController() { 51 DCHECK_EQ(state_, CLOSED); 52} 53 54// static 55scoped_refptr<AudioInputController> AudioInputController::Create( 56 AudioManager* audio_manager, 57 EventHandler* event_handler, 58 const AudioParameters& params, 59 const std::string& device_id, 60 UserInputMonitor* user_input_monitor) { 61 DCHECK(audio_manager); 62 63 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) 64 return NULL; 65 66 if (factory_) { 67 return factory_->Create( 68 audio_manager, event_handler, params, user_input_monitor); 69 } 70 scoped_refptr<AudioInputController> controller( 71 new AudioInputController(event_handler, NULL, user_input_monitor)); 72 73 controller->task_runner_ = audio_manager->GetTaskRunner(); 74 75 // Create and open a new audio input stream from the existing 76 // audio-device thread. 77 if (!controller->task_runner_->PostTask(FROM_HERE, 78 base::Bind(&AudioInputController::DoCreate, controller, 79 base::Unretained(audio_manager), params, device_id))) { 80 controller = NULL; 81 } 82 83 return controller; 84} 85 86// static 87scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency( 88 AudioManager* audio_manager, 89 EventHandler* event_handler, 90 const AudioParameters& params, 91 const std::string& device_id, 92 SyncWriter* sync_writer, 93 UserInputMonitor* user_input_monitor) { 94 DCHECK(audio_manager); 95 DCHECK(sync_writer); 96 97 if (!params.IsValid() || (params.channels() > kMaxInputChannels)) 98 return NULL; 99 100 // Create the AudioInputController object and ensure that it runs on 101 // the audio-manager thread. 102 scoped_refptr<AudioInputController> controller( 103 new AudioInputController(event_handler, sync_writer, user_input_monitor)); 104 controller->task_runner_ = audio_manager->GetTaskRunner(); 105 106 // Create and open a new audio input stream from the existing 107 // audio-device thread. Use the provided audio-input device. 108 if (!controller->task_runner_->PostTask(FROM_HERE, 109 base::Bind(&AudioInputController::DoCreate, controller, 110 base::Unretained(audio_manager), params, device_id))) { 111 controller = NULL; 112 } 113 114 return controller; 115} 116 117// static 118scoped_refptr<AudioInputController> AudioInputController::CreateForStream( 119 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 120 EventHandler* event_handler, 121 AudioInputStream* stream, 122 SyncWriter* sync_writer, 123 UserInputMonitor* user_input_monitor) { 124 DCHECK(sync_writer); 125 DCHECK(stream); 126 127 // Create the AudioInputController object and ensure that it runs on 128 // the audio-manager thread. 129 scoped_refptr<AudioInputController> controller( 130 new AudioInputController(event_handler, sync_writer, user_input_monitor)); 131 controller->task_runner_ = task_runner; 132 133 // TODO(miu): See TODO at top of file. Until that's resolved, we need to 134 // disable the error auto-detection here (since the audio mirroring 135 // implementation will reliably report error and close events). Note, of 136 // course, that we're assuming CreateForStream() has been called for the audio 137 // mirroring use case only. 138 if (!controller->task_runner_->PostTask( 139 FROM_HERE, 140 base::Bind(&AudioInputController::DoCreateForStream, controller, 141 stream, false))) { 142 controller = NULL; 143 } 144 145 return controller; 146} 147 148void AudioInputController::Record() { 149 task_runner_->PostTask(FROM_HERE, base::Bind( 150 &AudioInputController::DoRecord, this)); 151} 152 153void AudioInputController::Close(const base::Closure& closed_task) { 154 DCHECK(!closed_task.is_null()); 155 DCHECK(creator_task_runner_->BelongsToCurrentThread()); 156 157 task_runner_->PostTaskAndReply( 158 FROM_HERE, base::Bind(&AudioInputController::DoClose, this), closed_task); 159} 160 161void AudioInputController::SetVolume(double volume) { 162 task_runner_->PostTask(FROM_HERE, base::Bind( 163 &AudioInputController::DoSetVolume, this, volume)); 164} 165 166void AudioInputController::SetAutomaticGainControl(bool enabled) { 167 task_runner_->PostTask(FROM_HERE, base::Bind( 168 &AudioInputController::DoSetAutomaticGainControl, this, enabled)); 169} 170 171void AudioInputController::DoCreate(AudioManager* audio_manager, 172 const AudioParameters& params, 173 const std::string& device_id) { 174 DCHECK(task_runner_->BelongsToCurrentThread()); 175 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CreateTime"); 176 // TODO(miu): See TODO at top of file. Until that's resolved, assume all 177 // platform audio input requires the |no_data_timer_| be used to auto-detect 178 // errors. In reality, probably only Windows needs to be treated as 179 // unreliable here. 180 DoCreateForStream(audio_manager->MakeAudioInputStream(params, device_id), 181 true); 182} 183 184void AudioInputController::DoCreateForStream( 185 AudioInputStream* stream_to_control, bool enable_nodata_timer) { 186 DCHECK(task_runner_->BelongsToCurrentThread()); 187 188 DCHECK(!stream_); 189 stream_ = stream_to_control; 190 191 if (!stream_) { 192 if (handler_) 193 handler_->OnError(this, STREAM_CREATE_ERROR); 194 return; 195 } 196 197 if (stream_ && !stream_->Open()) { 198 stream_->Close(); 199 stream_ = NULL; 200 if (handler_) 201 handler_->OnError(this, STREAM_OPEN_ERROR); 202 return; 203 } 204 205 DCHECK(!no_data_timer_.get()); 206 207 // The timer is enabled for logging purposes. The NO_DATA_ERROR triggered 208 // from the timer must be ignored by the EventHandler. 209 // TODO(henrika): remove usage of timer when it has been verified on Canary 210 // that we are safe doing so. Goal is to get rid of |no_data_timer_| and 211 // everything that is tied to it. crbug.com/357569. 212 enable_nodata_timer = true; 213 214 if (enable_nodata_timer) { 215 // Create the data timer which will call DoCheckForNoData(). The timer 216 // is started in DoRecord() and restarted in each DoCheckForNoData() 217 // callback. 218 no_data_timer_.reset(new base::Timer( 219 FROM_HERE, base::TimeDelta::FromSeconds(kTimerInitialIntervalSeconds), 220 base::Bind(&AudioInputController::DoCheckForNoData, 221 base::Unretained(this)), false)); 222 } else { 223 DVLOG(1) << "Disabled: timer check for no data."; 224 } 225 226 state_ = CREATED; 227 if (handler_) 228 handler_->OnCreated(this); 229 230 if (user_input_monitor_) { 231 user_input_monitor_->EnableKeyPressMonitoring(); 232 prev_key_down_count_ = user_input_monitor_->GetKeyPressCount(); 233 } 234} 235 236void AudioInputController::DoRecord() { 237 DCHECK(task_runner_->BelongsToCurrentThread()); 238 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime"); 239 240 if (state_ != CREATED) 241 return; 242 243 { 244 base::AutoLock auto_lock(lock_); 245 state_ = RECORDING; 246 } 247 248 if (no_data_timer_) { 249 // Start the data timer. Once |kTimerResetIntervalSeconds| have passed, 250 // a callback to DoCheckForNoData() is made. 251 no_data_timer_->Reset(); 252 } 253 254 stream_->Start(this); 255 if (handler_) 256 handler_->OnRecording(this); 257} 258 259void AudioInputController::DoClose() { 260 DCHECK(task_runner_->BelongsToCurrentThread()); 261 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime"); 262 263 if (state_ == CLOSED) 264 return; 265 266 // Delete the timer on the same thread that created it. 267 no_data_timer_.reset(); 268 269 DoStopCloseAndClearStream(); 270 SetDataIsActive(false); 271 272 if (SharedMemoryAndSyncSocketMode()) 273 sync_writer_->Close(); 274 275 if (user_input_monitor_) 276 user_input_monitor_->DisableKeyPressMonitoring(); 277 278 state_ = CLOSED; 279} 280 281void AudioInputController::DoReportError() { 282 DCHECK(task_runner_->BelongsToCurrentThread()); 283 if (handler_) 284 handler_->OnError(this, STREAM_ERROR); 285} 286 287void AudioInputController::DoSetVolume(double volume) { 288 DCHECK(task_runner_->BelongsToCurrentThread()); 289 DCHECK_GE(volume, 0); 290 DCHECK_LE(volume, 1.0); 291 292 if (state_ != CREATED && state_ != RECORDING) 293 return; 294 295 // Only ask for the maximum volume at first call and use cached value 296 // for remaining function calls. 297 if (!max_volume_) { 298 max_volume_ = stream_->GetMaxVolume(); 299 } 300 301 if (max_volume_ == 0.0) { 302 DLOG(WARNING) << "Failed to access input volume control"; 303 return; 304 } 305 306 // Set the stream volume and scale to a range matched to the platform. 307 stream_->SetVolume(max_volume_ * volume); 308} 309 310void AudioInputController::DoSetAutomaticGainControl(bool enabled) { 311 DCHECK(task_runner_->BelongsToCurrentThread()); 312 DCHECK_NE(state_, RECORDING); 313 314 // Ensure that the AGC state only can be modified before streaming starts. 315 if (state_ != CREATED) 316 return; 317 318 stream_->SetAutomaticGainControl(enabled); 319} 320 321void AudioInputController::DoCheckForNoData() { 322 DCHECK(task_runner_->BelongsToCurrentThread()); 323 324 if (!GetDataIsActive()) { 325 // The data-is-active marker will be false only if it has been more than 326 // one second since a data packet was recorded. This can happen if a 327 // capture device has been removed or disabled. 328 if (handler_) 329 handler_->OnError(this, NO_DATA_ERROR); 330 } 331 332 // Mark data as non-active. The flag will be re-enabled in OnData() each 333 // time a data packet is received. Hence, under normal conditions, the 334 // flag will only be disabled during a very short period. 335 SetDataIsActive(false); 336 337 // Restart the timer to ensure that we check the flag again in 338 // |kTimerResetIntervalSeconds|. 339 no_data_timer_->Start( 340 FROM_HERE, base::TimeDelta::FromSeconds(kTimerResetIntervalSeconds), 341 base::Bind(&AudioInputController::DoCheckForNoData, 342 base::Unretained(this))); 343} 344 345void AudioInputController::OnData(AudioInputStream* stream, 346 const uint8* data, 347 uint32 size, 348 uint32 hardware_delay_bytes, 349 double volume) { 350 { 351 base::AutoLock auto_lock(lock_); 352 if (state_ != RECORDING) 353 return; 354 } 355 356 bool key_pressed = false; 357 if (user_input_monitor_) { 358 size_t current_count = user_input_monitor_->GetKeyPressCount(); 359 key_pressed = current_count != prev_key_down_count_; 360 prev_key_down_count_ = current_count; 361 DVLOG_IF(6, key_pressed) << "Detected keypress."; 362 } 363 364 // Mark data as active to ensure that the periodic calls to 365 // DoCheckForNoData() does not report an error to the event handler. 366 SetDataIsActive(true); 367 368 // Use SharedMemory and SyncSocket if the client has created a SyncWriter. 369 // Used by all low-latency clients except WebSpeech. 370 if (SharedMemoryAndSyncSocketMode()) { 371 sync_writer_->Write(data, size, volume, key_pressed); 372 sync_writer_->UpdateRecordedBytes(hardware_delay_bytes); 373 return; 374 } 375 376 // TODO(henrika): Investigate if we can avoid the extra copy here. 377 // (see http://crbug.com/249316 for details). AFAIK, this scope is only 378 // active for WebSpeech clients. 379 scoped_ptr<uint8[]> audio_data(new uint8[size]); 380 memcpy(audio_data.get(), data, size); 381 382 // Ownership of the audio buffer will be with the callback until it is run, 383 // when ownership is passed to the callback function. 384 task_runner_->PostTask(FROM_HERE, base::Bind( 385 &AudioInputController::DoOnData, this, base::Passed(&audio_data), size)); 386} 387 388void AudioInputController::DoOnData(scoped_ptr<uint8[]> data, uint32 size) { 389 DCHECK(task_runner_->BelongsToCurrentThread()); 390 if (handler_) 391 handler_->OnData(this, data.get(), size); 392} 393 394void AudioInputController::OnError(AudioInputStream* stream) { 395 // Handle error on the audio-manager thread. 396 task_runner_->PostTask(FROM_HERE, base::Bind( 397 &AudioInputController::DoReportError, this)); 398} 399 400void AudioInputController::DoStopCloseAndClearStream() { 401 DCHECK(task_runner_->BelongsToCurrentThread()); 402 403 // Allow calling unconditionally and bail if we don't have a stream to close. 404 if (stream_ != NULL) { 405 stream_->Stop(); 406 stream_->Close(); 407 stream_ = NULL; 408 } 409 410 // The event handler should not be touched after the stream has been closed. 411 handler_ = NULL; 412} 413 414void AudioInputController::SetDataIsActive(bool enabled) { 415 base::subtle::Release_Store(&data_is_active_, enabled); 416} 417 418bool AudioInputController::GetDataIsActive() { 419 return (base::subtle::Acquire_Load(&data_is_active_) != false); 420} 421 422} // namespace media 423