Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/audio_input_device.h" | 5 #include "media/audio/audio_input_device.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "media/audio/audio_manager_base.h" | 11 #include "media/audio/audio_manager_base.h" |
| 12 #include "media/base/audio_bus.h" | 12 #include "media/base/audio_bus.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 // The number of shared memory buffers indicated to browser process in order | |
| 16 // to avoid data overwriting. This number can be any positive number, | |
| 17 // dependent how fast the renderer process can pick up captured data from | |
| 18 // shared memory. | |
| 19 int kRequestedSharedMemoryCount = 10; | |
|
DaleCurtis
2013/03/06 01:12:51
Seems like a lot. How did you choose this number?
wjia(left Chromium)
2013/03/06 05:32:22
I have seen renderer process missed 5 to 6 buffers
| |
| 20 } | |
| 21 | |
| 14 namespace media { | 22 namespace media { |
| 15 | 23 |
| 16 // Takes care of invoking the capture callback on the audio thread. | 24 // Takes care of invoking the capture callback on the audio thread. |
| 17 // An instance of this class is created for each capture stream in | 25 // An instance of this class is created for each capture stream in |
| 18 // OnLowLatencyCreated(). | 26 // OnLowLatencyCreated(). |
| 19 class AudioInputDevice::AudioThreadCallback | 27 class AudioInputDevice::AudioThreadCallback |
| 20 : public AudioDeviceThread::Callback { | 28 : public AudioDeviceThread::Callback { |
| 21 public: | 29 public: |
| 22 AudioThreadCallback(const AudioParameters& audio_parameters, | 30 AudioThreadCallback(const AudioParameters& audio_parameters, |
| 23 base::SharedMemoryHandle memory, | 31 SharedMemoryHandleVector memory, |
| 24 int memory_length, | 32 int memory_length, |
| 25 CaptureCallback* capture_callback); | 33 CaptureCallback* capture_callback); |
| 26 virtual ~AudioThreadCallback(); | 34 virtual ~AudioThreadCallback(); |
| 27 | 35 |
| 28 virtual void MapSharedMemory() OVERRIDE; | 36 virtual void MapSharedMemory() OVERRIDE; |
| 29 | 37 |
| 30 // Called whenever we receive notifications about pending data. | 38 // Called whenever we receive notifications about pending data. |
| 31 virtual void Process(int pending_data) OVERRIDE; | 39 // The |index| states which buffer is used. |
| 40 virtual void Process(int pending_data, int index) OVERRIDE; | |
| 32 | 41 |
| 33 private: | 42 private: |
| 34 CaptureCallback* capture_callback_; | 43 CaptureCallback* capture_callback_; |
| 35 scoped_ptr<AudioBus> audio_bus_; | 44 scoped_ptr<AudioBus> audio_bus_; |
| 36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 45 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| 37 }; | 46 }; |
| 38 | 47 |
| 39 AudioInputDevice::AudioInputDevice( | 48 AudioInputDevice::AudioInputDevice( |
| 40 AudioInputIPC* ipc, | 49 AudioInputIPC* ipc, |
| 41 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 50 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
| 42 : ScopedLoopObserver(io_loop), | 51 : ScopedLoopObserver(io_loop), |
| 43 callback_(NULL), | 52 callback_(NULL), |
| 44 event_handler_(NULL), | 53 event_handler_(NULL), |
| 45 ipc_(ipc), | 54 ipc_(ipc), |
| 55 socket_handle_(base::SyncSocket::kInvalidHandle), | |
| 56 received_shared_memory_count_(0), | |
| 46 stream_id_(0), | 57 stream_id_(0), |
| 47 session_id_(0), | 58 session_id_(0), |
| 48 pending_device_ready_(false), | 59 pending_device_ready_(false), |
| 49 agc_is_enabled_(false) { | 60 agc_is_enabled_(false), |
| 61 audio_thread_(true) { | |
| 50 CHECK(ipc_); | 62 CHECK(ipc_); |
| 51 } | 63 } |
| 52 | 64 |
| 53 void AudioInputDevice::Initialize(const AudioParameters& params, | 65 void AudioInputDevice::Initialize(const AudioParameters& params, |
| 54 CaptureCallback* callback, | 66 CaptureCallback* callback, |
| 55 CaptureEventHandler* event_handler) { | 67 CaptureEventHandler* event_handler) { |
| 56 DCHECK(!callback_); | 68 DCHECK(!callback_); |
| 57 DCHECK(!event_handler_); | 69 DCHECK(!event_handler_); |
| 58 audio_parameters_ = params; | 70 audio_parameters_ = params; |
| 59 callback_ = callback; | 71 callback_ = callback; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 } | 107 } |
| 96 | 108 |
| 97 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { | 109 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { |
| 98 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; | 110 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; |
| 99 message_loop()->PostTask(FROM_HERE, | 111 message_loop()->PostTask(FROM_HERE, |
| 100 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, | 112 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, |
| 101 this, enabled)); | 113 this, enabled)); |
| 102 } | 114 } |
| 103 | 115 |
| 104 void AudioInputDevice::OnStreamCreated( | 116 void AudioInputDevice::OnStreamCreated( |
| 117 base::SyncSocket::Handle socket_handle, | |
| 118 int total_handles) { | |
| 119 DCHECK(message_loop()->BelongsToCurrentThread()); | |
| 120 DCHECK_NE(socket_handle, base::SyncSocket::kInvalidHandle); | |
| 121 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; | |
| 122 | |
| 123 // We should only get this callback if stream_id_ is valid. If it is not, | |
| 124 // the IPC layer should have closed the socket handle for us and not invoked | |
| 125 // the callback. The basic assertion is that when | |
| 126 // stream_id_ is 0 the AudioInputDevice instance is not registered as a | |
| 127 // delegate and hence it should not receive callbacks. | |
| 128 DCHECK(stream_id_); | |
| 129 DCHECK_GT(total_handles, 0); | |
| 130 DCHECK_EQ(shared_memory_handles_.size(), 0u); | |
| 131 | |
| 132 base::AutoLock auto_lock(audio_thread_lock_); | |
| 133 DCHECK(audio_thread_.IsStopped()); | |
| 134 | |
| 135 socket_handle_ = socket_handle; | |
| 136 shared_memory_handles_.resize(total_handles, base::SharedMemory().handle()); | |
| 137 } | |
| 138 | |
| 139 void AudioInputDevice::OnSharedMemoryCreated( | |
| 105 base::SharedMemoryHandle handle, | 140 base::SharedMemoryHandle handle, |
| 106 base::SyncSocket::Handle socket_handle, | 141 int length, |
| 107 int length) { | 142 int index) { |
| 108 DCHECK(message_loop()->BelongsToCurrentThread()); | 143 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 109 #if defined(OS_WIN) | 144 DCHECK_NE(socket_handle_, base::SyncSocket::kInvalidHandle); |
| 110 DCHECK(handle); | 145 DCHECK(base::SharedMemory::IsHandleValid(handle)); |
| 111 DCHECK(socket_handle); | |
| 112 #else | |
| 113 DCHECK_GE(handle.fd, 0); | |
| 114 DCHECK_GE(socket_handle, 0); | |
| 115 #endif | |
| 116 DCHECK(length); | 146 DCHECK(length); |
| 117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; | 147 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; |
| 118 | 148 |
| 119 // We should only get this callback if stream_id_ is valid. If it is not, | 149 // 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 | 150 // the IPC layer should have closed the shared memory socket handle |
| 121 // for us and not invoked the callback. The basic assertion is that when | 151 // 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 | 152 // stream_id_ is 0 the AudioInputDevice instance is not registered as a |
| 123 // delegate and hence it should not receive callbacks. | 153 // delegate and hence it should not receive callbacks. |
| 124 DCHECK(stream_id_); | 154 DCHECK(stream_id_); |
| 125 | 155 |
| 126 base::AutoLock auto_lock(audio_thread_lock_); | 156 base::AutoLock auto_lock(audio_thread_lock_); |
| 127 | 157 |
| 128 DCHECK(audio_thread_.IsStopped()); | 158 DCHECK(audio_thread_.IsStopped()); |
| 129 audio_callback_.reset( | |
| 130 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, | |
| 131 length, callback_)); | |
| 132 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); | |
| 133 | 159 |
| 134 MessageLoop::current()->PostTask(FROM_HERE, | 160 int total_handles = static_cast<int>(shared_memory_handles_.size()); |
| 135 base::Bind(&AudioInputDevice::StartOnIOThread, this)); | 161 DCHECK_LT(index, total_handles); |
| 162 DCHECK(!base::SharedMemory::IsHandleValid(shared_memory_handles_[index])); | |
| 163 shared_memory_handles_[index] = handle; | |
| 164 received_shared_memory_count_++; | |
| 165 if (received_shared_memory_count_ == total_handles) { | |
| 166 audio_callback_.reset( | |
| 167 new AudioInputDevice::AudioThreadCallback( | |
| 168 audio_parameters_, shared_memory_handles_, length, callback_)); | |
| 169 audio_thread_.Start(audio_callback_.get(), socket_handle_, | |
| 170 "AudioInputDevice"); | |
| 171 | |
| 172 MessageLoop::current()->PostTask(FROM_HERE, | |
| 173 base::Bind(&AudioInputDevice::StartOnIOThread, this)); | |
| 174 } | |
| 136 } | 175 } |
| 137 | 176 |
| 138 void AudioInputDevice::OnVolume(double volume) { | 177 void AudioInputDevice::OnVolume(double volume) { |
| 139 NOTIMPLEMENTED(); | 178 NOTIMPLEMENTED(); |
| 140 } | 179 } |
| 141 | 180 |
| 142 void AudioInputDevice::OnStateChanged( | 181 void AudioInputDevice::OnStateChanged( |
| 143 AudioInputIPCDelegate::State state) { | 182 AudioInputIPCDelegate::State state) { |
| 144 DCHECK(message_loop()->BelongsToCurrentThread()); | 183 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 145 | 184 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 if (!pending_device_ready_) | 228 if (!pending_device_ready_) |
| 190 return; | 229 return; |
| 191 | 230 |
| 192 // If AudioInputDeviceManager returns an empty string, it means no device | 231 // If AudioInputDeviceManager returns an empty string, it means no device |
| 193 // is ready for start. | 232 // is ready for start. |
| 194 if (device_id.empty()) { | 233 if (device_id.empty()) { |
| 195 ipc_->RemoveDelegate(stream_id_); | 234 ipc_->RemoveDelegate(stream_id_); |
| 196 stream_id_ = 0; | 235 stream_id_ = 0; |
| 197 } else { | 236 } else { |
| 198 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, | 237 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, |
| 199 agc_is_enabled_); | 238 agc_is_enabled_, kRequestedSharedMemoryCount); |
| 200 } | 239 } |
| 201 | 240 |
| 202 pending_device_ready_ = false; | 241 pending_device_ready_ = false; |
| 203 // Notify the client that the device has been started. | 242 // Notify the client that the device has been started. |
| 204 if (event_handler_) | 243 if (event_handler_) |
| 205 event_handler_->OnDeviceStarted(device_id); | 244 event_handler_->OnDeviceStarted(device_id); |
| 206 } | 245 } |
| 207 | 246 |
| 208 void AudioInputDevice::OnIPCClosed() { | 247 void AudioInputDevice::OnIPCClosed() { |
| 209 ipc_ = NULL; | 248 ipc_ = NULL; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 221 DCHECK_EQ(0, stream_id_); | 260 DCHECK_EQ(0, stream_id_); |
| 222 if (stream_id_) | 261 if (stream_id_) |
| 223 return; | 262 return; |
| 224 | 263 |
| 225 stream_id_ = ipc_->AddDelegate(this); | 264 stream_id_ = ipc_->AddDelegate(this); |
| 226 // If |session_id_| is not specified, it will directly create the stream; | 265 // If |session_id_| is not specified, it will directly create the stream; |
| 227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 266 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser |
| 228 // and create the stream when getting a OnDeviceReady() callback. | 267 // and create the stream when getting a OnDeviceReady() callback. |
| 229 if (!session_id_) { | 268 if (!session_id_) { |
| 230 ipc_->CreateStream(stream_id_, audio_parameters_, | 269 ipc_->CreateStream(stream_id_, audio_parameters_, |
| 231 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); | 270 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_, |
| 271 kRequestedSharedMemoryCount); | |
| 232 } else { | 272 } else { |
| 233 ipc_->StartDevice(stream_id_, session_id_); | 273 ipc_->StartDevice(stream_id_, session_id_); |
| 234 pending_device_ready_ = true; | 274 pending_device_ready_ = true; |
| 235 } | 275 } |
| 236 } | 276 } |
| 237 | 277 |
| 238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 278 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
| 239 DCHECK(message_loop()->BelongsToCurrentThread()); | 279 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 240 session_id_ = session_id; | 280 session_id_ = session_id; |
| 241 } | 281 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 } | 333 } |
| 294 | 334 |
| 295 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 335 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
| 296 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 336 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
| 297 ShutDownOnIOThread(); | 337 ShutDownOnIOThread(); |
| 298 } | 338 } |
| 299 | 339 |
| 300 // AudioInputDevice::AudioThreadCallback | 340 // AudioInputDevice::AudioThreadCallback |
| 301 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 341 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
| 302 const AudioParameters& audio_parameters, | 342 const AudioParameters& audio_parameters, |
| 303 base::SharedMemoryHandle memory, | 343 SharedMemoryHandleVector memory, |
| 304 int memory_length, | 344 int memory_length, |
| 305 CaptureCallback* capture_callback) | 345 CaptureCallback* capture_callback) |
| 306 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 346 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), |
| 307 capture_callback_(capture_callback) { | 347 capture_callback_(capture_callback) { |
| 308 audio_bus_ = AudioBus::Create(audio_parameters_); | 348 audio_bus_ = AudioBus::Create(audio_parameters_); |
| 309 } | 349 } |
| 310 | 350 |
| 311 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 351 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
| 312 } | 352 } |
| 313 | 353 |
| 314 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 354 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
| 315 shared_memory_.Map(memory_length_); | 355 for (size_t id = 0; id < shared_memory_.size(); ++id) { |
| 356 shared_memory_[id]->Map(memory_length_); | |
| 357 } | |
| 316 } | 358 } |
| 317 | 359 |
| 318 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 360 void AudioInputDevice::AudioThreadCallback::Process( |
| 361 int pending_data, int index) { | |
| 319 // The shared memory represents parameters, size of the data buffer and the | 362 // The shared memory represents parameters, size of the data buffer and the |
| 320 // actual data buffer containing audio data. Map the memory into this | 363 // actual data buffer containing audio data. Map the memory into this |
| 321 // structure and parse out parameters and the data area. | 364 // structure and parse out parameters and the data area. |
| 322 AudioInputBuffer* buffer = | 365 AudioInputBuffer* buffer = |
| 323 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); | 366 reinterpret_cast<AudioInputBuffer*>(shared_memory_[index]->memory()); |
| 324 DCHECK_EQ(buffer->params.size, | 367 DCHECK_EQ(buffer->params.size, |
| 325 memory_length_ - sizeof(AudioInputBufferParameters)); | 368 memory_length_ - sizeof(AudioInputBufferParameters)); |
| 326 double volume = buffer->params.volume; | 369 double volume = buffer->params.volume; |
| 327 | 370 |
| 328 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 371 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 329 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 372 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
| 330 const int bytes_per_sample = sizeof(memory[0]); | 373 const int bytes_per_sample = sizeof(memory[0]); |
| 331 | 374 |
| 332 // Deinterleave each channel and convert to 32-bit floating-point | 375 // Deinterleave each channel and convert to 32-bit floating-point |
| 333 // with nominal range -1.0 -> +1.0. | 376 // with nominal range -1.0 -> +1.0. |
| 334 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); | 377 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); |
| 335 | 378 |
| 336 // Deliver captured data to the client in floating point format | 379 // Deliver captured data to the client in floating point format |
| 337 // and update the audio-delay measurement. | 380 // and update the audio-delay measurement. |
| 338 capture_callback_->Capture(audio_bus_.get(), | 381 capture_callback_->Capture(audio_bus_.get(), |
| 339 audio_delay_milliseconds, volume); | 382 audio_delay_milliseconds, volume); |
| 340 } | 383 } |
| 341 | 384 |
| 342 } // namespace media | 385 } // namespace media |
| OLD | NEW |