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