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.
5 #include "media/audio/audio_input_controller.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"
14 const int kMaxInputChannels
= 2;
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
23 const 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.
27 const int kTimerInitialIntervalSeconds
= 5;
33 AudioInputController::Factory
* AudioInputController::factory_
= NULL
;
35 AudioInputController::AudioInputController(EventHandler
* handler
,
36 SyncWriter
* sync_writer
,
37 UserInputMonitor
* user_input_monitor
)
38 : creator_task_runner_(base::MessageLoopProxy::current()),
41 data_is_active_(false),
43 sync_writer_(sync_writer
),
45 user_input_monitor_(user_input_monitor
),
46 prev_key_down_count_(0) {
47 DCHECK(creator_task_runner_
.get());
50 AudioInputController::~AudioInputController() {
51 DCHECK_EQ(state_
, CLOSED
);
55 scoped_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
);
63 if (!params
.IsValid() || (params
.channels() > kMaxInputChannels
))
67 return factory_
->Create(
68 audio_manager
, event_handler
, params
, user_input_monitor
);
70 scoped_refptr
<AudioInputController
> controller(
71 new AudioInputController(event_handler
, NULL
, user_input_monitor
));
73 controller
->task_runner_
= audio_manager
->GetTaskRunner();
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
))) {
87 scoped_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
);
97 if (!params
.IsValid() || (params
.channels() > kMaxInputChannels
))
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();
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
))) {
118 scoped_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
) {
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
;
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(
140 base::Bind(&AudioInputController::DoCreateForStream
, controller
,
148 void AudioInputController::Record() {
149 task_runner_
->PostTask(FROM_HERE
, base::Bind(
150 &AudioInputController::DoRecord
, this));
153 void AudioInputController::Close(const base::Closure
& closed_task
) {
154 DCHECK(!closed_task
.is_null());
155 DCHECK(creator_task_runner_
->BelongsToCurrentThread());
157 task_runner_
->PostTaskAndReply(
158 FROM_HERE
, base::Bind(&AudioInputController::DoClose
, this), closed_task
);
161 void AudioInputController::SetVolume(double volume
) {
162 task_runner_
->PostTask(FROM_HERE
, base::Bind(
163 &AudioInputController::DoSetVolume
, this, volume
));
166 void AudioInputController::SetAutomaticGainControl(bool enabled
) {
167 task_runner_
->PostTask(FROM_HERE
, base::Bind(
168 &AudioInputController::DoSetAutomaticGainControl
, this, enabled
));
171 void 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
180 DoCreateForStream(audio_manager
->MakeAudioInputStream(params
, device_id
),
184 void AudioInputController::DoCreateForStream(
185 AudioInputStream
* stream_to_control
, bool enable_nodata_timer
) {
186 DCHECK(task_runner_
->BelongsToCurrentThread());
189 stream_
= stream_to_control
;
192 handler_
->OnError(this, STREAM_CREATE_ERROR
);
196 if (stream_
&& !stream_
->Open()) {
199 handler_
->OnError(this, STREAM_OPEN_ERROR
);
203 DCHECK(!no_data_timer_
.get());
205 // The timer is enabled for logging purposes. The NO_DATA_ERROR triggered
206 // from the timer must be ignored by the EventHandler.
207 // TODO(henrika): remove usage of timer when it has been verified on Canary
208 // that we are safe doing so. Goal is to get rid of |no_data_timer_| and
209 // everything that is tied to it. crbug.com/357569.
210 enable_nodata_timer
= true;
212 if (enable_nodata_timer
) {
213 // Create the data timer which will call DoCheckForNoData(). The timer
214 // is started in DoRecord() and restarted in each DoCheckForNoData()
216 no_data_timer_
.reset(new base::Timer(
217 FROM_HERE
, base::TimeDelta::FromSeconds(kTimerInitialIntervalSeconds
),
218 base::Bind(&AudioInputController::DoCheckForNoData
,
219 base::Unretained(this)), false));
221 DVLOG(1) << "Disabled: timer check for no data.";
225 handler_
->OnCreated(this);
227 if (user_input_monitor_
) {
228 user_input_monitor_
->EnableKeyPressMonitoring();
229 prev_key_down_count_
= user_input_monitor_
->GetKeyPressCount();
233 void AudioInputController::DoRecord() {
234 DCHECK(task_runner_
->BelongsToCurrentThread());
235 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.RecordTime");
237 if (state_
!= CREATED
)
241 base::AutoLock
auto_lock(lock_
);
245 if (no_data_timer_
) {
246 // Start the data timer. Once |kTimerResetIntervalSeconds| have passed,
247 // a callback to DoCheckForNoData() is made.
248 no_data_timer_
->Reset();
251 stream_
->Start(this);
252 handler_
->OnRecording(this);
255 void AudioInputController::DoClose() {
256 DCHECK(task_runner_
->BelongsToCurrentThread());
257 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioInputController.CloseTime");
259 if (state_
== CLOSED
)
262 // Delete the timer on the same thread that created it.
263 no_data_timer_
.reset();
265 DoStopCloseAndClearStream(NULL
);
266 SetDataIsActive(false);
268 if (LowLatencyMode())
269 sync_writer_
->Close();
271 if (user_input_monitor_
)
272 user_input_monitor_
->DisableKeyPressMonitoring();
277 void AudioInputController::DoReportError() {
278 DCHECK(task_runner_
->BelongsToCurrentThread());
279 handler_
->OnError(this, STREAM_ERROR
);
282 void AudioInputController::DoSetVolume(double volume
) {
283 DCHECK(task_runner_
->BelongsToCurrentThread());
284 DCHECK_GE(volume
, 0);
285 DCHECK_LE(volume
, 1.0);
287 if (state_
!= CREATED
&& state_
!= RECORDING
)
290 // Only ask for the maximum volume at first call and use cached value
291 // for remaining function calls.
293 max_volume_
= stream_
->GetMaxVolume();
296 if (max_volume_
== 0.0) {
297 DLOG(WARNING
) << "Failed to access input volume control";
301 // Set the stream volume and scale to a range matched to the platform.
302 stream_
->SetVolume(max_volume_
* volume
);
305 void AudioInputController::DoSetAutomaticGainControl(bool enabled
) {
306 DCHECK(task_runner_
->BelongsToCurrentThread());
307 DCHECK_NE(state_
, RECORDING
);
309 // Ensure that the AGC state only can be modified before streaming starts.
310 if (state_
!= CREATED
)
313 stream_
->SetAutomaticGainControl(enabled
);
316 void AudioInputController::DoCheckForNoData() {
317 DCHECK(task_runner_
->BelongsToCurrentThread());
319 if (!GetDataIsActive()) {
320 // The data-is-active marker will be false only if it has been more than
321 // one second since a data packet was recorded. This can happen if a
322 // capture device has been removed or disabled.
323 handler_
->OnError(this, NO_DATA_ERROR
);
326 // Mark data as non-active. The flag will be re-enabled in OnData() each
327 // time a data packet is received. Hence, under normal conditions, the
328 // flag will only be disabled during a very short period.
329 SetDataIsActive(false);
331 // Restart the timer to ensure that we check the flag again in
332 // |kTimerResetIntervalSeconds|.
333 no_data_timer_
->Start(
334 FROM_HERE
, base::TimeDelta::FromSeconds(kTimerResetIntervalSeconds
),
335 base::Bind(&AudioInputController::DoCheckForNoData
,
336 base::Unretained(this)));
339 void AudioInputController::OnData(AudioInputStream
* stream
,
342 uint32 hardware_delay_bytes
,
345 base::AutoLock
auto_lock(lock_
);
346 if (state_
!= RECORDING
)
350 bool key_pressed
= false;
351 if (user_input_monitor_
) {
352 size_t current_count
= user_input_monitor_
->GetKeyPressCount();
353 key_pressed
= current_count
!= prev_key_down_count_
;
354 prev_key_down_count_
= current_count
;
355 DVLOG_IF(6, key_pressed
) << "Detected keypress.";
358 // Mark data as active to ensure that the periodic calls to
359 // DoCheckForNoData() does not report an error to the event handler.
360 SetDataIsActive(true);
362 // Use SyncSocket if we are in a low-latency mode.
363 if (LowLatencyMode()) {
364 sync_writer_
->Write(data
, size
, volume
, key_pressed
);
365 sync_writer_
->UpdateRecordedBytes(hardware_delay_bytes
);
369 handler_
->OnData(this, data
, size
);
372 void AudioInputController::OnError(AudioInputStream
* stream
) {
373 // Handle error on the audio-manager thread.
374 task_runner_
->PostTask(FROM_HERE
, base::Bind(
375 &AudioInputController::DoReportError
, this));
378 void AudioInputController::DoStopCloseAndClearStream(
379 base::WaitableEvent
* done
) {
380 DCHECK(task_runner_
->BelongsToCurrentThread());
382 // Allow calling unconditionally and bail if we don't have a stream to close.
383 if (stream_
!= NULL
) {
389 // Should be last in the method, do not touch "this" from here on.
394 void AudioInputController::SetDataIsActive(bool enabled
) {
395 base::subtle::Release_Store(&data_is_active_
, enabled
);
398 bool AudioInputController::GetDataIsActive() {
399 return (base::subtle::Acquire_Load(&data_is_active_
) != false);