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 // TODO(henrika): figure out a suitable size of this ring buffer.
21 // We have seen reports in Chrome where segments of repeated input audio has
22 // damaged AEC performance in WebRTC clients. By setting its value to 1, we
23 // reduce the number of places in Chrome where such a patteren could possibly
24 // be created. The original value of kRequestedSharedMemoryCount was 10.
25 // See b/13976602 for details.
26 static const int kRequestedSharedMemoryCount
= 1;
28 // Takes care of invoking the capture callback on the audio thread.
29 // An instance of this class is created for each capture stream in
30 // OnLowLatencyCreated().
31 class AudioInputDevice::AudioThreadCallback
32 : public AudioDeviceThread::Callback
{
34 AudioThreadCallback(const AudioParameters
& audio_parameters
,
35 base::SharedMemoryHandle memory
,
38 CaptureCallback
* capture_callback
);
39 ~AudioThreadCallback() override
;
41 void MapSharedMemory() override
;
43 // Called whenever we receive notifications about pending data.
44 void Process(uint32 pending_data
) override
;
47 int current_segment_id_
;
48 ScopedVector
<media::AudioBus
> audio_buses_
;
49 CaptureCallback
* capture_callback_
;
51 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback
);
54 AudioInputDevice::AudioInputDevice(
55 scoped_ptr
<AudioInputIPC
> ipc
,
56 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
57 : ScopedTaskRunnerObserver(io_task_runner
),
62 agc_is_enabled_(false),
63 stopping_hack_(false) {
66 // The correctness of the code depends on the relative values assigned in the
68 static_assert(IPC_CLOSED
< IDLE
, "invalid enum value assignment 0");
69 static_assert(IDLE
< CREATING_STREAM
, "invalid enum value assignment 1");
70 static_assert(CREATING_STREAM
< RECORDING
, "invalid enum value assignment 2");
73 void AudioInputDevice::Initialize(const AudioParameters
& params
,
74 CaptureCallback
* callback
,
76 DCHECK(params
.IsValid());
78 DCHECK_EQ(0, session_id_
);
79 audio_parameters_
= params
;
81 session_id_
= session_id
;
84 void AudioInputDevice::Start() {
85 DCHECK(callback_
) << "Initialize hasn't been called";
86 DVLOG(1) << "Start()";
87 task_runner()->PostTask(FROM_HERE
,
88 base::Bind(&AudioInputDevice::StartUpOnIOThread
, this));
91 void AudioInputDevice::Stop() {
95 base::AutoLock
auto_lock(audio_thread_lock_
);
96 audio_thread_
.Stop(base::MessageLoop::current());
97 stopping_hack_
= true;
100 task_runner()->PostTask(FROM_HERE
,
101 base::Bind(&AudioInputDevice::ShutDownOnIOThread
, this));
104 void AudioInputDevice::SetVolume(double volume
) {
105 if (volume
< 0 || volume
> 1.0) {
106 DLOG(ERROR
) << "Invalid volume value specified";
110 task_runner()->PostTask(FROM_HERE
,
111 base::Bind(&AudioInputDevice::SetVolumeOnIOThread
, this, volume
));
114 void AudioInputDevice::SetAutomaticGainControl(bool enabled
) {
115 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled
<< ")";
116 task_runner()->PostTask(FROM_HERE
,
117 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread
,
121 void AudioInputDevice::OnStreamCreated(
122 base::SharedMemoryHandle handle
,
123 base::SyncSocket::Handle socket_handle
,
125 int total_segments
) {
126 DCHECK(task_runner()->BelongsToCurrentThread());
127 DCHECK(base::SharedMemory::IsHandleValid(handle
));
129 DCHECK(socket_handle
);
131 DCHECK_GE(socket_handle
, 0);
133 DCHECK_GT(length
, 0);
135 if (state_
!= CREATING_STREAM
)
138 base::AutoLock
auto_lock(audio_thread_lock_
);
139 // TODO(miu): See TODO in OnStreamCreated method for AudioOutputDevice.
140 // Interface changes need to be made; likely, after AudioInputDevice is merged
141 // into AudioOutputDevice (http://crbug.com/179597).
145 DCHECK(audio_thread_
.IsStopped());
146 audio_callback_
.reset(new AudioInputDevice::AudioThreadCallback(
147 audio_parameters_
, handle
, length
, total_segments
, callback_
));
149 audio_callback_
.get(), socket_handle
, "AudioInputDevice", false);
152 ipc_
->RecordStream();
155 void AudioInputDevice::OnVolume(double volume
) {
159 void AudioInputDevice::OnStateChanged(
160 AudioInputIPCDelegateState state
) {
161 DCHECK(task_runner()->BelongsToCurrentThread());
163 // Do nothing if the stream has been closed.
164 if (state_
< CREATING_STREAM
)
167 // TODO(miu): Clean-up inconsistent and incomplete handling here.
168 // http://crbug.com/180640
170 case AUDIO_INPUT_IPC_DELEGATE_STATE_STOPPED
:
171 ShutDownOnIOThread();
173 case AUDIO_INPUT_IPC_DELEGATE_STATE_RECORDING
:
176 case AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR
:
177 DLOG(WARNING
) << "AudioInputDevice::OnStateChanged(ERROR)";
178 // Don't dereference the callback object if the audio thread
179 // is stopped or stopping. That could mean that the callback
180 // object has been deleted.
181 // TODO(tommi): Add an explicit contract for clearing the callback
182 // object. Possibly require calling Initialize again or provide
183 // a callback object via Start() and clear it in Stop().
184 if (!audio_thread_
.IsStopped())
185 callback_
->OnCaptureError();
193 void AudioInputDevice::OnIPCClosed() {
194 DCHECK(task_runner()->BelongsToCurrentThread());
199 AudioInputDevice::~AudioInputDevice() {
200 // TODO(henrika): The current design requires that the user calls
201 // Stop before deleting this class.
202 DCHECK(audio_thread_
.IsStopped());
205 void AudioInputDevice::StartUpOnIOThread() {
206 DCHECK(task_runner()->BelongsToCurrentThread());
208 // Make sure we don't call Start() more than once.
212 if (session_id_
<= 0) {
213 DLOG(WARNING
) << "Invalid session id for the input stream " << session_id_
;
217 state_
= CREATING_STREAM
;
218 ipc_
->CreateStream(this, session_id_
, audio_parameters_
,
219 agc_is_enabled_
, kRequestedSharedMemoryCount
);
222 void AudioInputDevice::ShutDownOnIOThread() {
223 DCHECK(task_runner()->BelongsToCurrentThread());
225 // Close the stream, if we haven't already.
226 if (state_
>= CREATING_STREAM
) {
229 agc_is_enabled_
= false;
232 // We can run into an issue where ShutDownOnIOThread is called right after
233 // OnStreamCreated is called in cases where Start/Stop are called before we
234 // get the OnStreamCreated callback. To handle that corner case, we call
235 // Stop(). In most cases, the thread will already be stopped.
237 // Another situation is when the IO thread goes away before Stop() is called
238 // in which case, we cannot use the message loop to close the thread handle
239 // and can't not rely on the main thread existing either.
240 base::AutoLock
auto_lock_(audio_thread_lock_
);
241 base::ThreadRestrictions::ScopedAllowIO allow_io
;
242 audio_thread_
.Stop(NULL
);
243 audio_callback_
.reset();
244 stopping_hack_
= false;
247 void AudioInputDevice::SetVolumeOnIOThread(double volume
) {
248 DCHECK(task_runner()->BelongsToCurrentThread());
249 if (state_
>= CREATING_STREAM
)
250 ipc_
->SetVolume(volume
);
253 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled
) {
254 DCHECK(task_runner()->BelongsToCurrentThread());
256 if (state_
>= CREATING_STREAM
) {
257 DLOG(WARNING
) << "The AGC state can not be modified after starting.";
261 // We simply store the new AGC setting here. This value will be used when
262 // a new stream is initialized and by GetAutomaticGainControl().
263 agc_is_enabled_
= enabled
;
266 void AudioInputDevice::WillDestroyCurrentMessageLoop() {
267 LOG(ERROR
) << "IO loop going away before the input device has been stopped";
268 ShutDownOnIOThread();
271 // AudioInputDevice::AudioThreadCallback
272 AudioInputDevice::AudioThreadCallback::AudioThreadCallback(
273 const AudioParameters
& audio_parameters
,
274 base::SharedMemoryHandle memory
,
277 CaptureCallback
* capture_callback
)
278 : AudioDeviceThread::Callback(audio_parameters
, memory
, memory_length
,
280 current_segment_id_(0),
281 capture_callback_(capture_callback
) {
284 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() {
287 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() {
288 shared_memory_
.Map(memory_length_
);
290 // Create vector of audio buses by wrapping existing blocks of memory.
291 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
.memory());
292 for (int i
= 0; i
< total_segments_
; ++i
) {
293 media::AudioInputBuffer
* buffer
=
294 reinterpret_cast<media::AudioInputBuffer
*>(ptr
);
295 scoped_ptr
<media::AudioBus
> audio_bus
=
296 media::AudioBus::WrapMemory(audio_parameters_
, buffer
->audio
);
297 audio_buses_
.push_back(audio_bus
.Pass());
298 ptr
+= segment_length_
;
302 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data
) {
303 // The shared memory represents parameters, size of the data buffer and the
304 // actual data buffer containing audio data. Map the memory into this
305 // structure and parse out parameters and the data area.
306 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
.memory());
307 ptr
+= current_segment_id_
* segment_length_
;
308 AudioInputBuffer
* buffer
= reinterpret_cast<AudioInputBuffer
*>(ptr
);
309 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz,
310 // the buffer may be bigger (on mac at least)).
311 DCHECK_GE(buffer
->params
.size
,
312 segment_length_
- sizeof(AudioInputBufferParameters
));
313 double volume
= buffer
->params
.volume
;
314 bool key_pressed
= buffer
->params
.key_pressed
;
316 // Use pre-allocated audio bus wrapping existing block of shared memory.
317 media::AudioBus
* audio_bus
= audio_buses_
[current_segment_id_
];
319 // Deliver captured data to the client in floating point format
320 // and update the audio-delay measurement.
321 int audio_delay_milliseconds
= pending_data
/ bytes_per_ms_
;
322 capture_callback_
->Capture(
323 audio_bus
, audio_delay_milliseconds
, volume
, key_pressed
);
325 if (++current_segment_id_
>= total_segments_
)
326 current_segment_id_
= 0;