Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / audio / audio_input_device.cc
blob07eb11c2b77b16c2ba02efda82c5c7491b87693c
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"
7 #include "base/bind.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"
14 namespace media {
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
19 // shared memory.
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 {
33 public:
34 AudioThreadCallback(const AudioParameters& audio_parameters,
35 base::SharedMemoryHandle memory,
36 int memory_length,
37 int total_segments,
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;
46 private:
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),
58 callback_(NULL),
59 ipc_(ipc.Pass()),
60 state_(IDLE),
61 session_id_(0),
62 agc_is_enabled_(false),
63 stopping_hack_(false) {
64 CHECK(ipc_);
66 // The correctness of the code depends on the relative values assigned in the
67 // State enum.
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,
75 int session_id) {
76 DCHECK(params.IsValid());
77 DCHECK(!callback_);
78 DCHECK_EQ(0, session_id_);
79 audio_parameters_ = params;
80 callback_ = callback;
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() {
92 DVLOG(1) << "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";
107 return;
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,
118 this, enabled));
121 void AudioInputDevice::OnStreamCreated(
122 base::SharedMemoryHandle handle,
123 base::SyncSocket::Handle socket_handle,
124 int length,
125 int total_segments) {
126 DCHECK(task_runner()->BelongsToCurrentThread());
127 DCHECK(base::SharedMemory::IsHandleValid(handle));
128 #if defined(OS_WIN)
129 DCHECK(socket_handle);
130 #else
131 DCHECK_GE(socket_handle, 0);
132 #endif
133 DCHECK_GT(length, 0);
135 if (state_ != CREATING_STREAM)
136 return;
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).
142 if (stopping_hack_)
143 return;
145 DCHECK(audio_thread_.IsStopped());
146 audio_callback_.reset(new AudioInputDevice::AudioThreadCallback(
147 audio_parameters_, handle, length, total_segments, callback_));
148 audio_thread_.Start(
149 audio_callback_.get(), socket_handle, "AudioInputDevice", false);
151 state_ = RECORDING;
152 ipc_->RecordStream();
155 void AudioInputDevice::OnVolume(double volume) {
156 NOTIMPLEMENTED();
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)
165 return;
167 // TODO(miu): Clean-up inconsistent and incomplete handling here.
168 // http://crbug.com/180640
169 switch (state) {
170 case AUDIO_INPUT_IPC_DELEGATE_STATE_STOPPED:
171 ShutDownOnIOThread();
172 break;
173 case AUDIO_INPUT_IPC_DELEGATE_STATE_RECORDING:
174 NOTIMPLEMENTED();
175 break;
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();
186 break;
187 default:
188 NOTREACHED();
189 break;
193 void AudioInputDevice::OnIPCClosed() {
194 DCHECK(task_runner()->BelongsToCurrentThread());
195 state_ = IPC_CLOSED;
196 ipc_.reset();
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.
209 if (state_ != IDLE)
210 return;
212 if (session_id_ <= 0) {
213 DLOG(WARNING) << "Invalid session id for the input stream " << session_id_;
214 return;
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) {
227 ipc_->CloseStream();
228 state_ = IDLE;
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.";
258 return;
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,
275 int memory_length,
276 int total_segments,
277 CaptureCallback* capture_callback)
278 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length,
279 total_segments),
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;
329 } // namespace media