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/memory/scoped_vector.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "base/time/time.h"
11 #include "media/audio/audio_manager_base.h"
12 #include "media/base/audio_bus.h"
16 // The number of shared memory buffer segments indicated to browser process
17 // in order to avoid data overwriting. This number can be any positive number,
18 // dependent how fast the renderer process can pick up captured data from
20 static const int kRequestedSharedMemoryCount
= 10;
22 // Takes care of invoking the capture callback on the audio thread.
23 // An instance of this class is created for each capture stream in
24 // OnLowLatencyCreated().
25 class AudioInputDevice::AudioThreadCallback
26 : public AudioDeviceThread::Callback
{
28 AudioThreadCallback(const AudioParameters
& audio_parameters
,
29 base::SharedMemoryHandle memory
,
32 CaptureCallback
* capture_callback
);
33 ~AudioThreadCallback() override
;
35 void MapSharedMemory() override
;
37 // Called whenever we receive notifications about pending data.
38 void Process(uint32 pending_data
) override
;
41 int current_segment_id_
;
42 uint32 last_buffer_id_
;
43 ScopedVector
<media::AudioBus
> audio_buses_
;
44 CaptureCallback
* capture_callback_
;
46 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback
);
49 AudioInputDevice::AudioInputDevice(
50 scoped_ptr
<AudioInputIPC
> ipc
,
51 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
52 : ScopedTaskRunnerObserver(io_task_runner
),
57 agc_is_enabled_(false),
58 stopping_hack_(false) {
61 // The correctness of the code depends on the relative values assigned in the
63 static_assert(IPC_CLOSED
< IDLE
, "invalid enum value assignment 0");
64 static_assert(IDLE
< CREATING_STREAM
, "invalid enum value assignment 1");
65 static_assert(CREATING_STREAM
< RECORDING
, "invalid enum value assignment 2");
68 void AudioInputDevice::Initialize(const AudioParameters
& params
,
69 CaptureCallback
* callback
,
71 DCHECK(params
.IsValid());
73 DCHECK_EQ(0, session_id_
);
74 audio_parameters_
= params
;
76 session_id_
= session_id
;
79 void AudioInputDevice::Start() {
80 DCHECK(callback_
) << "Initialize hasn't been called";
81 DVLOG(1) << "Start()";
82 task_runner()->PostTask(FROM_HERE
,
83 base::Bind(&AudioInputDevice::StartUpOnIOThread
, this));
86 void AudioInputDevice::Stop() {
90 base::AutoLock
auto_lock(audio_thread_lock_
);
91 audio_thread_
.Stop(base::MessageLoop::current());
92 stopping_hack_
= true;
95 task_runner()->PostTask(FROM_HERE
,
96 base::Bind(&AudioInputDevice::ShutDownOnIOThread
, this));
99 void AudioInputDevice::SetVolume(double volume
) {
100 if (volume
< 0 || volume
> 1.0) {
101 DLOG(ERROR
) << "Invalid volume value specified";
105 task_runner()->PostTask(FROM_HERE
,
106 base::Bind(&AudioInputDevice::SetVolumeOnIOThread
, this, volume
));
109 void AudioInputDevice::SetAutomaticGainControl(bool enabled
) {
110 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled
<< ")";
111 task_runner()->PostTask(FROM_HERE
,
112 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread
,
116 void AudioInputDevice::OnStreamCreated(
117 base::SharedMemoryHandle handle
,
118 base::SyncSocket::Handle socket_handle
,
120 int total_segments
) {
121 DCHECK(task_runner()->BelongsToCurrentThread());
122 DCHECK(base::SharedMemory::IsHandleValid(handle
));
124 DCHECK(socket_handle
);
126 DCHECK_GE(socket_handle
, 0);
128 DCHECK_GT(length
, 0);
130 if (state_
!= CREATING_STREAM
)
133 base::AutoLock
auto_lock(audio_thread_lock_
);
134 // TODO(miu): See TODO in OnStreamCreated method for AudioOutputDevice.
135 // Interface changes need to be made; likely, after AudioInputDevice is merged
136 // into AudioOutputDevice (http://crbug.com/179597).
140 DCHECK(audio_thread_
.IsStopped());
141 audio_callback_
.reset(new AudioInputDevice::AudioThreadCallback(
142 audio_parameters_
, handle
, length
, total_segments
, callback_
));
144 audio_callback_
.get(), socket_handle
, "AudioInputDevice", false);
147 ipc_
->RecordStream();
150 void AudioInputDevice::OnVolume(double volume
) {
154 void AudioInputDevice::OnStateChanged(
155 AudioInputIPCDelegateState state
) {
156 DCHECK(task_runner()->BelongsToCurrentThread());
158 // Do nothing if the stream has been closed.
159 if (state_
< CREATING_STREAM
)
162 // TODO(miu): Clean-up inconsistent and incomplete handling here.
163 // http://crbug.com/180640
165 case AUDIO_INPUT_IPC_DELEGATE_STATE_STOPPED
:
166 ShutDownOnIOThread();
168 case AUDIO_INPUT_IPC_DELEGATE_STATE_RECORDING
:
171 case AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR
:
172 DLOG(WARNING
) << "AudioInputDevice::OnStateChanged(ERROR)";
173 // Don't dereference the callback object if the audio thread
174 // is stopped or stopping. That could mean that the callback
175 // object has been deleted.
176 // TODO(tommi): Add an explicit contract for clearing the callback
177 // object. Possibly require calling Initialize again or provide
178 // a callback object via Start() and clear it in Stop().
179 if (!audio_thread_
.IsStopped())
180 callback_
->OnCaptureError();
188 void AudioInputDevice::OnIPCClosed() {
189 DCHECK(task_runner()->BelongsToCurrentThread());
194 AudioInputDevice::~AudioInputDevice() {
195 // TODO(henrika): The current design requires that the user calls
196 // Stop before deleting this class.
197 DCHECK(audio_thread_
.IsStopped());
200 void AudioInputDevice::StartUpOnIOThread() {
201 DCHECK(task_runner()->BelongsToCurrentThread());
203 // Make sure we don't call Start() more than once.
207 if (session_id_
<= 0) {
208 DLOG(WARNING
) << "Invalid session id for the input stream " << session_id_
;
212 state_
= CREATING_STREAM
;
213 ipc_
->CreateStream(this, session_id_
, audio_parameters_
,
214 agc_is_enabled_
, kRequestedSharedMemoryCount
);
217 void AudioInputDevice::ShutDownOnIOThread() {
218 DCHECK(task_runner()->BelongsToCurrentThread());
220 // Close the stream, if we haven't already.
221 if (state_
>= CREATING_STREAM
) {
224 agc_is_enabled_
= false;
227 // We can run into an issue where ShutDownOnIOThread is called right after
228 // OnStreamCreated is called in cases where Start/Stop are called before we
229 // get the OnStreamCreated callback. To handle that corner case, we call
230 // Stop(). In most cases, the thread will already be stopped.
232 // Another situation is when the IO thread goes away before Stop() is called
233 // in which case, we cannot use the message loop to close the thread handle
234 // and can't not rely on the main thread existing either.
235 base::AutoLock
auto_lock_(audio_thread_lock_
);
236 base::ThreadRestrictions::ScopedAllowIO allow_io
;
237 audio_thread_
.Stop(NULL
);
238 audio_callback_
.reset();
239 stopping_hack_
= false;
242 void AudioInputDevice::SetVolumeOnIOThread(double volume
) {
243 DCHECK(task_runner()->BelongsToCurrentThread());
244 if (state_
>= CREATING_STREAM
)
245 ipc_
->SetVolume(volume
);
248 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled
) {
249 DCHECK(task_runner()->BelongsToCurrentThread());
251 if (state_
>= CREATING_STREAM
) {
252 DLOG(WARNING
) << "The AGC state can not be modified after starting.";
256 // We simply store the new AGC setting here. This value will be used when
257 // a new stream is initialized and by GetAutomaticGainControl().
258 agc_is_enabled_
= enabled
;
261 void AudioInputDevice::WillDestroyCurrentMessageLoop() {
262 LOG(ERROR
) << "IO loop going away before the input device has been stopped";
263 ShutDownOnIOThread();
266 // AudioInputDevice::AudioThreadCallback
267 AudioInputDevice::AudioThreadCallback::AudioThreadCallback(
268 const AudioParameters
& audio_parameters
,
269 base::SharedMemoryHandle memory
,
272 CaptureCallback
* capture_callback
)
273 : AudioDeviceThread::Callback(audio_parameters
, memory
, memory_length
,
275 current_segment_id_(0),
276 last_buffer_id_(UINT32_MAX
),
277 capture_callback_(capture_callback
) {
280 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() {
283 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() {
284 shared_memory_
.Map(memory_length_
);
286 // Create vector of audio buses by wrapping existing blocks of memory.
287 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
.memory());
288 for (int i
= 0; i
< total_segments_
; ++i
) {
289 media::AudioInputBuffer
* buffer
=
290 reinterpret_cast<media::AudioInputBuffer
*>(ptr
);
291 scoped_ptr
<media::AudioBus
> audio_bus
=
292 media::AudioBus::WrapMemory(audio_parameters_
, buffer
->audio
);
293 audio_buses_
.push_back(audio_bus
.Pass());
294 ptr
+= segment_length_
;
298 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data
) {
299 CHECK_EQ(current_segment_id_
, static_cast<int>(pending_data
));
301 // The shared memory represents parameters, size of the data buffer and the
302 // actual data buffer containing audio data. Map the memory into this
303 // structure and parse out parameters and the data area.
304 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
.memory());
305 ptr
+= current_segment_id_
* segment_length_
;
306 AudioInputBuffer
* buffer
= reinterpret_cast<AudioInputBuffer
*>(ptr
);
308 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz,
309 // the buffer may be bigger (on mac at least)).
310 DCHECK_GE(buffer
->params
.size
,
311 segment_length_
- sizeof(AudioInputBufferParameters
));
313 // Verify correct sequence.
314 CHECK_EQ(last_buffer_id_
+ 1, buffer
->params
.id
);
315 last_buffer_id_
= buffer
->params
.id
;
317 // Use pre-allocated audio bus wrapping existing block of shared memory.
318 media::AudioBus
* audio_bus
= audio_buses_
[current_segment_id_
];
320 // Deliver captured data to the client in floating point format and update
321 // the audio delay measurement.
322 capture_callback_
->Capture(
324 buffer
->params
.hardware_delay_bytes
/ bytes_per_ms_
, // Delay in ms
325 buffer
->params
.volume
,
326 buffer
->params
.key_pressed
);
328 if (++current_segment_id_
>= total_segments_
)
329 current_segment_id_
= 0;