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/strings/stringprintf.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/time/time.h"
12 #include "media/audio/audio_manager_base.h"
13 #include "media/base/audio_bus.h"
17 // The number of shared memory buffer segments indicated to browser process
18 // in order to avoid data overwriting. This number can be any positive number,
19 // dependent how fast the renderer process can pick up captured data from
21 static const int kRequestedSharedMemoryCount
= 10;
23 // Takes care of invoking the capture callback on the audio thread.
24 // An instance of this class is created for each capture stream in
25 // OnLowLatencyCreated().
26 class AudioInputDevice::AudioThreadCallback
27 : public AudioDeviceThread::Callback
{
29 AudioThreadCallback(const AudioParameters
& audio_parameters
,
30 base::SharedMemoryHandle memory
,
33 CaptureCallback
* capture_callback
);
34 ~AudioThreadCallback() override
;
36 void MapSharedMemory() override
;
38 // Called whenever we receive notifications about pending data.
39 void Process(uint32 pending_data
) override
;
42 int current_segment_id_
;
43 uint32 last_buffer_id_
;
44 ScopedVector
<media::AudioBus
> audio_buses_
;
45 CaptureCallback
* capture_callback_
;
47 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback
);
50 AudioInputDevice::AudioInputDevice(
51 scoped_ptr
<AudioInputIPC
> ipc
,
52 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
)
53 : ScopedTaskRunnerObserver(io_task_runner
),
58 agc_is_enabled_(false),
59 stopping_hack_(false) {
62 // The correctness of the code depends on the relative values assigned in the
64 static_assert(IPC_CLOSED
< IDLE
, "invalid enum value assignment 0");
65 static_assert(IDLE
< CREATING_STREAM
, "invalid enum value assignment 1");
66 static_assert(CREATING_STREAM
< RECORDING
, "invalid enum value assignment 2");
69 void AudioInputDevice::Initialize(const AudioParameters
& params
,
70 CaptureCallback
* callback
,
72 DCHECK(params
.IsValid());
74 DCHECK_EQ(0, session_id_
);
75 audio_parameters_
= params
;
77 session_id_
= session_id
;
80 void AudioInputDevice::Start() {
81 DCHECK(callback_
) << "Initialize hasn't been called";
82 DVLOG(1) << "Start()";
83 task_runner()->PostTask(FROM_HERE
,
84 base::Bind(&AudioInputDevice::StartUpOnIOThread
, this));
87 void AudioInputDevice::Stop() {
91 base::AutoLock
auto_lock(audio_thread_lock_
);
92 audio_thread_
.Stop(base::MessageLoop::current());
93 stopping_hack_
= true;
96 task_runner()->PostTask(FROM_HERE
,
97 base::Bind(&AudioInputDevice::ShutDownOnIOThread
, this));
100 void AudioInputDevice::SetVolume(double volume
) {
101 if (volume
< 0 || volume
> 1.0) {
102 DLOG(ERROR
) << "Invalid volume value specified";
106 task_runner()->PostTask(FROM_HERE
,
107 base::Bind(&AudioInputDevice::SetVolumeOnIOThread
, this, volume
));
110 void AudioInputDevice::SetAutomaticGainControl(bool enabled
) {
111 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled
<< ")";
112 task_runner()->PostTask(FROM_HERE
,
113 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread
,
117 void AudioInputDevice::OnStreamCreated(
118 base::SharedMemoryHandle handle
,
119 base::SyncSocket::Handle socket_handle
,
121 int total_segments
) {
122 DCHECK(task_runner()->BelongsToCurrentThread());
123 DCHECK(base::SharedMemory::IsHandleValid(handle
));
125 DCHECK(socket_handle
);
127 DCHECK_GE(socket_handle
, 0);
129 DCHECK_GT(length
, 0);
131 if (state_
!= CREATING_STREAM
)
134 base::AutoLock
auto_lock(audio_thread_lock_
);
135 // TODO(miu): See TODO in OnStreamCreated method for AudioOutputDevice.
136 // Interface changes need to be made; likely, after AudioInputDevice is merged
137 // into AudioOutputDevice (http://crbug.com/179597).
141 DCHECK(audio_thread_
.IsStopped());
142 audio_callback_
.reset(new AudioInputDevice::AudioThreadCallback(
143 audio_parameters_
, handle
, length
, total_segments
, callback_
));
145 audio_callback_
.get(), socket_handle
, "AudioInputDevice", true);
148 ipc_
->RecordStream();
151 void AudioInputDevice::OnVolume(double volume
) {
155 void AudioInputDevice::OnStateChanged(
156 AudioInputIPCDelegateState state
) {
157 DCHECK(task_runner()->BelongsToCurrentThread());
159 // Do nothing if the stream has been closed.
160 if (state_
< CREATING_STREAM
)
163 // TODO(miu): Clean-up inconsistent and incomplete handling here.
164 // http://crbug.com/180640
166 case AUDIO_INPUT_IPC_DELEGATE_STATE_STOPPED
:
167 ShutDownOnIOThread();
169 case AUDIO_INPUT_IPC_DELEGATE_STATE_RECORDING
:
172 case AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR
:
173 DLOG(WARNING
) << "AudioInputDevice::OnStateChanged(ERROR)";
174 // Don't dereference the callback object if the audio thread
175 // is stopped or stopping. That could mean that the callback
176 // object has been deleted.
177 // TODO(tommi): Add an explicit contract for clearing the callback
178 // object. Possibly require calling Initialize again or provide
179 // a callback object via Start() and clear it in Stop().
180 if (!audio_thread_
.IsStopped())
181 callback_
->OnCaptureError(
182 "AudioInputDevice::OnStateChanged - audio thread still running");
190 void AudioInputDevice::OnIPCClosed() {
191 DCHECK(task_runner()->BelongsToCurrentThread());
196 AudioInputDevice::~AudioInputDevice() {
197 // TODO(henrika): The current design requires that the user calls
198 // Stop before deleting this class.
199 DCHECK(audio_thread_
.IsStopped());
202 void AudioInputDevice::StartUpOnIOThread() {
203 DCHECK(task_runner()->BelongsToCurrentThread());
205 // Make sure we don't call Start() more than once.
209 if (session_id_
<= 0) {
210 DLOG(WARNING
) << "Invalid session id for the input stream " << session_id_
;
214 state_
= CREATING_STREAM
;
215 ipc_
->CreateStream(this, session_id_
, audio_parameters_
,
216 agc_is_enabled_
, kRequestedSharedMemoryCount
);
219 void AudioInputDevice::ShutDownOnIOThread() {
220 DCHECK(task_runner()->BelongsToCurrentThread());
222 // Close the stream, if we haven't already.
223 if (state_
>= CREATING_STREAM
) {
226 agc_is_enabled_
= false;
229 // We can run into an issue where ShutDownOnIOThread is called right after
230 // OnStreamCreated is called in cases where Start/Stop are called before we
231 // get the OnStreamCreated callback. To handle that corner case, we call
232 // Stop(). In most cases, the thread will already be stopped.
234 // Another situation is when the IO thread goes away before Stop() is called
235 // in which case, we cannot use the message loop to close the thread handle
236 // and can't not rely on the main thread existing either.
237 base::AutoLock
auto_lock_(audio_thread_lock_
);
238 base::ThreadRestrictions::ScopedAllowIO allow_io
;
239 audio_thread_
.Stop(NULL
);
240 audio_callback_
.reset();
241 stopping_hack_
= false;
244 void AudioInputDevice::SetVolumeOnIOThread(double volume
) {
245 DCHECK(task_runner()->BelongsToCurrentThread());
246 if (state_
>= CREATING_STREAM
)
247 ipc_
->SetVolume(volume
);
250 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled
) {
251 DCHECK(task_runner()->BelongsToCurrentThread());
253 if (state_
>= CREATING_STREAM
) {
254 DLOG(WARNING
) << "The AGC state can not be modified after starting.";
258 // We simply store the new AGC setting here. This value will be used when
259 // a new stream is initialized and by GetAutomaticGainControl().
260 agc_is_enabled_
= enabled
;
263 void AudioInputDevice::WillDestroyCurrentMessageLoop() {
264 LOG(ERROR
) << "IO loop going away before the input device has been stopped";
265 ShutDownOnIOThread();
268 // AudioInputDevice::AudioThreadCallback
269 AudioInputDevice::AudioThreadCallback::AudioThreadCallback(
270 const AudioParameters
& audio_parameters
,
271 base::SharedMemoryHandle memory
,
274 CaptureCallback
* capture_callback
)
275 : AudioDeviceThread::Callback(audio_parameters
, memory
, memory_length
,
277 current_segment_id_(0),
278 last_buffer_id_(UINT32_MAX
),
279 capture_callback_(capture_callback
) {
282 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() {
285 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() {
286 shared_memory_
.Map(memory_length_
);
288 // Create vector of audio buses by wrapping existing blocks of memory.
289 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
.memory());
290 for (int i
= 0; i
< total_segments_
; ++i
) {
291 media::AudioInputBuffer
* buffer
=
292 reinterpret_cast<media::AudioInputBuffer
*>(ptr
);
293 scoped_ptr
<media::AudioBus
> audio_bus
=
294 media::AudioBus::WrapMemory(audio_parameters_
, buffer
->audio
);
295 audio_buses_
.push_back(audio_bus
.Pass());
296 ptr
+= segment_length_
;
300 void AudioInputDevice::AudioThreadCallback::Process(uint32 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 if (buffer
->params
.id
!= last_buffer_id_
+ 1) {
315 std::string message
= base::StringPrintf(
316 "Incorrect buffer sequence. Expected = %u. Actual = %u.",
317 last_buffer_id_
+ 1, buffer
->params
.id
);
318 LOG(ERROR
) << message
;
319 capture_callback_
->OnCaptureError(message
);
321 if (current_segment_id_
!= static_cast<int>(pending_data
)) {
322 std::string message
= base::StringPrintf(
323 "Segment id not matching. Remote = %u. Local = %d.",
324 pending_data
, current_segment_id_
);
325 LOG(ERROR
) << message
;
326 capture_callback_
->OnCaptureError(message
);
328 last_buffer_id_
= buffer
->params
.id
;
330 // Use pre-allocated audio bus wrapping existing block of shared memory.
331 media::AudioBus
* audio_bus
= audio_buses_
[current_segment_id_
];
333 // Deliver captured data to the client in floating point format and update
334 // the audio delay measurement.
335 capture_callback_
->Capture(
337 buffer
->params
.hardware_delay_bytes
/ bytes_per_ms_
, // Delay in ms
338 buffer
->params
.volume
,
339 buffer
->params
.key_pressed
);
341 if (++current_segment_id_
>= total_segments_
)
342 current_segment_id_
= 0;