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_device.h"
8 #include "base/message_loop.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "base/time.h"
11 #include "media/audio/audio_manager_base.h"
12 #include "media/base/audio_bus.h"
16 // Takes care of invoking the capture callback on the audio thread.
17 // An instance of this class is created for each capture stream in
18 // OnLowLatencyCreated().
19 class AudioInputDevice::AudioThreadCallback
20 : public AudioDeviceThread::Callback
{
22 AudioThreadCallback(const AudioParameters
& audio_parameters
,
23 base::SharedMemoryHandle memory
,
25 CaptureCallback
* capture_callback
);
26 virtual ~AudioThreadCallback();
28 virtual void MapSharedMemory() OVERRIDE
;
30 // Called whenever we receive notifications about pending data.
31 virtual void Process(int pending_data
) OVERRIDE
;
34 CaptureCallback
* capture_callback_
;
35 scoped_ptr
<AudioBus
> audio_bus_
;
36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback
);
39 AudioInputDevice::AudioInputDevice(
41 const scoped_refptr
<base::MessageLoopProxy
>& io_loop
)
42 : ScopedLoopObserver(io_loop
),
48 pending_device_ready_(false),
49 agc_is_enabled_(false) {
53 void AudioInputDevice::Initialize(const AudioParameters
& params
,
54 CaptureCallback
* callback
,
55 CaptureEventHandler
* event_handler
) {
57 DCHECK(!event_handler_
);
58 audio_parameters_
= params
;
60 event_handler_
= event_handler
;
63 void AudioInputDevice::SetDevice(int session_id
) {
64 DVLOG(1) << "SetDevice (session_id=" << session_id
<< ")";
65 message_loop()->PostTask(FROM_HERE
,
66 base::Bind(&AudioInputDevice::SetSessionIdOnIOThread
, this, session_id
));
69 void AudioInputDevice::Start() {
70 DVLOG(1) << "Start()";
71 message_loop()->PostTask(FROM_HERE
,
72 base::Bind(&AudioInputDevice::InitializeOnIOThread
, this));
75 void AudioInputDevice::Stop() {
79 base::AutoLock
auto_lock(audio_thread_lock_
);
80 audio_thread_
.Stop(MessageLoop::current());
83 message_loop()->PostTask(FROM_HERE
,
84 base::Bind(&AudioInputDevice::ShutDownOnIOThread
, this));
87 void AudioInputDevice::SetVolume(double volume
) {
88 if (volume
< 0 || volume
> 1.0) {
89 DLOG(ERROR
) << "Invalid volume value specified";
93 message_loop()->PostTask(FROM_HERE
,
94 base::Bind(&AudioInputDevice::SetVolumeOnIOThread
, this, volume
));
97 void AudioInputDevice::SetAutomaticGainControl(bool enabled
) {
98 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled
<< ")";
99 message_loop()->PostTask(FROM_HERE
,
100 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread
,
104 void AudioInputDevice::OnStreamCreated(
105 base::SharedMemoryHandle handle
,
106 base::SyncSocket::Handle socket_handle
,
108 DCHECK(message_loop()->BelongsToCurrentThread());
111 DCHECK(socket_handle
);
113 DCHECK_GE(handle
.fd
, 0);
114 DCHECK_GE(socket_handle
, 0);
117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_
<< ")";
119 // We should only get this callback if stream_id_ is valid. If it is not,
120 // the IPC layer should have closed the shared memory and socket handles
121 // for us and not invoked the callback. The basic assertion is that when
122 // stream_id_ is 0 the AudioInputDevice instance is not registered as a
123 // delegate and hence it should not receive callbacks.
126 base::AutoLock
auto_lock(audio_thread_lock_
);
128 DCHECK(audio_thread_
.IsStopped());
129 audio_callback_
.reset(
130 new AudioInputDevice::AudioThreadCallback(audio_parameters_
, handle
,
132 audio_thread_
.Start(audio_callback_
.get(), socket_handle
, "AudioInputDevice");
134 MessageLoop::current()->PostTask(FROM_HERE
,
135 base::Bind(&AudioInputDevice::StartOnIOThread
, this));
138 void AudioInputDevice::OnVolume(double volume
) {
142 void AudioInputDevice::OnStateChanged(
143 AudioInputIPCDelegate::State state
) {
144 DCHECK(message_loop()->BelongsToCurrentThread());
146 // Do nothing if the stream has been closed.
151 case AudioInputIPCDelegate::kStopped
:
152 // TODO(xians): Should we just call ShutDownOnIOThread here instead?
153 ipc_
->RemoveDelegate(stream_id_
);
155 audio_thread_
.Stop(MessageLoop::current());
156 audio_callback_
.reset();
159 event_handler_
->OnDeviceStopped();
162 pending_device_ready_
= false;
164 case AudioInputIPCDelegate::kRecording
:
167 case AudioInputIPCDelegate::kError
:
168 DLOG(WARNING
) << "AudioInputDevice::OnStateChanged(kError)";
169 // Don't dereference the callback object if the audio thread
170 // is stopped or stopping. That could mean that the callback
171 // object has been deleted.
172 // TODO(tommi): Add an explicit contract for clearing the callback
173 // object. Possibly require calling Initialize again or provide
174 // a callback object via Start() and clear it in Stop().
175 if (!audio_thread_
.IsStopped())
176 callback_
->OnCaptureError();
184 void AudioInputDevice::OnDeviceReady(const std::string
& device_id
) {
185 DCHECK(message_loop()->BelongsToCurrentThread());
186 DVLOG(1) << "OnDeviceReady (device_id=" << device_id
<< ")";
188 // Takes care of the case when Stop() is called before OnDeviceReady().
189 if (!pending_device_ready_
)
192 // If AudioInputDeviceManager returns an empty string, it means no device
193 // is ready for start.
194 if (device_id
.empty()) {
195 ipc_
->RemoveDelegate(stream_id_
);
198 ipc_
->CreateStream(stream_id_
, audio_parameters_
, device_id
,
202 pending_device_ready_
= false;
203 // Notify the client that the device has been started.
205 event_handler_
->OnDeviceStarted(device_id
);
208 void AudioInputDevice::OnIPCClosed() {
212 AudioInputDevice::~AudioInputDevice() {
213 // TODO(henrika): The current design requires that the user calls
214 // Stop before deleting this class.
215 CHECK_EQ(0, stream_id_
);
218 void AudioInputDevice::InitializeOnIOThread() {
219 DCHECK(message_loop()->BelongsToCurrentThread());
220 // Make sure we don't call Start() more than once.
221 DCHECK_EQ(0, stream_id_
);
225 stream_id_
= ipc_
->AddDelegate(this);
226 // If |session_id_| is not specified, it will directly create the stream;
227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser
228 // and create the stream when getting a OnDeviceReady() callback.
230 ipc_
->CreateStream(stream_id_
, audio_parameters_
,
231 AudioManagerBase::kDefaultDeviceId
, agc_is_enabled_
);
233 ipc_
->StartDevice(stream_id_
, session_id_
);
234 pending_device_ready_
= true;
238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id
) {
239 DCHECK(message_loop()->BelongsToCurrentThread());
240 session_id_
= session_id
;
243 void AudioInputDevice::StartOnIOThread() {
244 DCHECK(message_loop()->BelongsToCurrentThread());
246 ipc_
->RecordStream(stream_id_
);
249 void AudioInputDevice::ShutDownOnIOThread() {
250 DCHECK(message_loop()->BelongsToCurrentThread());
251 // NOTE: |completion| may be NULL.
252 // Make sure we don't call shutdown more than once.
255 ipc_
->CloseStream(stream_id_
);
256 ipc_
->RemoveDelegate(stream_id_
);
261 pending_device_ready_
= false;
262 agc_is_enabled_
= false;
265 // We can run into an issue where ShutDownOnIOThread is called right after
266 // OnStreamCreated is called in cases where Start/Stop are called before we
267 // get the OnStreamCreated callback. To handle that corner case, we call
268 // Stop(). In most cases, the thread will already be stopped.
269 // Another situation is when the IO thread goes away before Stop() is called
270 // in which case, we cannot use the message loop to close the thread handle
271 // and can't not rely on the main thread existing either.
272 base::ThreadRestrictions::ScopedAllowIO allow_io
;
273 audio_thread_
.Stop(NULL
);
274 audio_callback_
.reset();
277 void AudioInputDevice::SetVolumeOnIOThread(double volume
) {
278 DCHECK(message_loop()->BelongsToCurrentThread());
280 ipc_
->SetVolume(stream_id_
, volume
);
283 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled
) {
284 DCHECK(message_loop()->BelongsToCurrentThread());
285 DCHECK_EQ(0, stream_id_
) <<
286 "The AGC state can not be modified while capturing is active.";
290 // We simply store the new AGC setting here. This value will be used when
291 // a new stream is initialized and by GetAutomaticGainControl().
292 agc_is_enabled_
= enabled
;
295 void AudioInputDevice::WillDestroyCurrentMessageLoop() {
296 LOG(ERROR
) << "IO loop going away before the input device has been stopped";
297 ShutDownOnIOThread();
300 // AudioInputDevice::AudioThreadCallback
301 AudioInputDevice::AudioThreadCallback::AudioThreadCallback(
302 const AudioParameters
& audio_parameters
,
303 base::SharedMemoryHandle memory
,
305 CaptureCallback
* capture_callback
)
306 : AudioDeviceThread::Callback(audio_parameters
, 0, memory
, memory_length
),
307 capture_callback_(capture_callback
) {
308 audio_bus_
= AudioBus::Create(audio_parameters_
);
311 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() {
314 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() {
315 shared_memory_
.Map(memory_length_
);
318 void AudioInputDevice::AudioThreadCallback::Process(int pending_data
) {
319 // The shared memory represents parameters, size of the data buffer and the
320 // actual data buffer containing audio data. Map the memory into this
321 // structure and parse out parameters and the data area.
322 AudioInputBuffer
* buffer
=
323 reinterpret_cast<AudioInputBuffer
*>(shared_memory_
.memory());
324 DCHECK_EQ(buffer
->params
.size
,
325 memory_length_
- sizeof(AudioInputBufferParameters
));
326 double volume
= buffer
->params
.volume
;
328 int audio_delay_milliseconds
= pending_data
/ bytes_per_ms_
;
329 int16
* memory
= reinterpret_cast<int16
*>(&buffer
->audio
[0]);
330 const int bytes_per_sample
= sizeof(memory
[0]);
332 // Deinterleave each channel and convert to 32-bit floating-point
333 // with nominal range -1.0 -> +1.0.
334 audio_bus_
->FromInterleaved(memory
, audio_bus_
->frames(), bytes_per_sample
);
336 // Deliver captured data to the client in floating point format
337 // and update the audio-delay measurement.
338 capture_callback_
->Capture(audio_bus_
.get(),
339 audio_delay_milliseconds
, volume
);