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 buffer segments indicated to browser process | |
| 16 // in order 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/07 02:04:37
static const int or make it an enum. media/ code
wjia(left Chromium)
2013/03/10 18:19:49
anonymous namespace is better than static since it
DaleCurtis
2013/03/11 20:00:47
While the Chromium style guide explicitly says unn
wjia(left Chromium)
2013/03/11 20:34:27
Done.
| |
| 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 base::SharedMemoryHandle memory, |
| 24 int memory_length, | 32 int memory_length, |
| 33 int total_segments, | |
| 25 CaptureCallback* capture_callback); | 34 CaptureCallback* capture_callback); |
| 26 virtual ~AudioThreadCallback(); | 35 virtual ~AudioThreadCallback(); |
| 27 | 36 |
| 28 virtual void MapSharedMemory() OVERRIDE; | 37 virtual void MapSharedMemory() OVERRIDE; |
| 29 | 38 |
| 30 // Called whenever we receive notifications about pending data. | 39 // Called whenever we receive notifications about pending data. |
| 31 virtual void Process(int pending_data) OVERRIDE; | 40 virtual void Process(int pending_data) OVERRIDE; |
| 32 | 41 |
| 33 private: | 42 private: |
| 43 int current_segment_id_; | |
| 34 CaptureCallback* capture_callback_; | 44 CaptureCallback* capture_callback_; |
| 35 scoped_ptr<AudioBus> audio_bus_; | 45 scoped_ptr<AudioBus> audio_bus_; |
| 36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 46 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| 37 }; | 47 }; |
| 38 | 48 |
| 39 AudioInputDevice::AudioInputDevice( | 49 AudioInputDevice::AudioInputDevice( |
| 40 AudioInputIPC* ipc, | 50 AudioInputIPC* ipc, |
| 41 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 51 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
| 42 : ScopedLoopObserver(io_loop), | 52 : ScopedLoopObserver(io_loop), |
| 43 callback_(NULL), | 53 callback_(NULL), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { | 107 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { |
| 98 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; | 108 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; |
| 99 message_loop()->PostTask(FROM_HERE, | 109 message_loop()->PostTask(FROM_HERE, |
| 100 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, | 110 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, |
| 101 this, enabled)); | 111 this, enabled)); |
| 102 } | 112 } |
| 103 | 113 |
| 104 void AudioInputDevice::OnStreamCreated( | 114 void AudioInputDevice::OnStreamCreated( |
| 105 base::SharedMemoryHandle handle, | 115 base::SharedMemoryHandle handle, |
| 106 base::SyncSocket::Handle socket_handle, | 116 base::SyncSocket::Handle socket_handle, |
| 107 int length) { | 117 int length, |
| 118 int total_segments) { | |
| 108 DCHECK(message_loop()->BelongsToCurrentThread()); | 119 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 109 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
| 110 DCHECK(handle); | 121 DCHECK(handle); |
| 111 DCHECK(socket_handle); | 122 DCHECK(socket_handle); |
| 112 #else | 123 #else |
| 113 DCHECK_GE(handle.fd, 0); | 124 DCHECK_GE(handle.fd, 0); |
| 114 DCHECK_GE(socket_handle, 0); | 125 DCHECK_GE(socket_handle, 0); |
| 115 #endif | 126 #endif |
| 116 DCHECK(length); | 127 DCHECK(length); |
| 117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; | 128 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; |
| 118 | 129 |
| 119 // We should only get this callback if stream_id_ is valid. If it is not, | 130 // 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 | 131 // the IPC layer should have closed the shared memory and socket handles |
| 121 // for us and not invoked the callback. The basic assertion is that when | 132 // 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 | 133 // stream_id_ is 0 the AudioInputDevice instance is not registered as a |
| 123 // delegate and hence it should not receive callbacks. | 134 // delegate and hence it should not receive callbacks. |
| 124 DCHECK(stream_id_); | 135 DCHECK(stream_id_); |
| 125 | 136 |
| 126 base::AutoLock auto_lock(audio_thread_lock_); | 137 base::AutoLock auto_lock(audio_thread_lock_); |
| 127 | 138 |
| 128 DCHECK(audio_thread_.IsStopped()); | 139 DCHECK(audio_thread_.IsStopped()); |
| 129 audio_callback_.reset( | 140 audio_callback_.reset( |
| 130 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, | 141 new AudioInputDevice::AudioThreadCallback( |
| 131 length, callback_)); | 142 audio_parameters_, handle, length, total_segments, callback_)); |
| 132 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); | 143 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); |
| 133 | 144 |
| 134 MessageLoop::current()->PostTask(FROM_HERE, | 145 MessageLoop::current()->PostTask(FROM_HERE, |
| 135 base::Bind(&AudioInputDevice::StartOnIOThread, this)); | 146 base::Bind(&AudioInputDevice::StartOnIOThread, this)); |
| 136 } | 147 } |
| 137 | 148 |
| 138 void AudioInputDevice::OnVolume(double volume) { | 149 void AudioInputDevice::OnVolume(double volume) { |
| 139 NOTIMPLEMENTED(); | 150 NOTIMPLEMENTED(); |
| 140 } | 151 } |
| 141 | 152 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 if (!pending_device_ready_) | 200 if (!pending_device_ready_) |
| 190 return; | 201 return; |
| 191 | 202 |
| 192 // If AudioInputDeviceManager returns an empty string, it means no device | 203 // If AudioInputDeviceManager returns an empty string, it means no device |
| 193 // is ready for start. | 204 // is ready for start. |
| 194 if (device_id.empty()) { | 205 if (device_id.empty()) { |
| 195 ipc_->RemoveDelegate(stream_id_); | 206 ipc_->RemoveDelegate(stream_id_); |
| 196 stream_id_ = 0; | 207 stream_id_ = 0; |
| 197 } else { | 208 } else { |
| 198 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, | 209 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, |
| 199 agc_is_enabled_); | 210 agc_is_enabled_, kRequestedSharedMemoryCount); |
| 200 } | 211 } |
| 201 | 212 |
| 202 pending_device_ready_ = false; | 213 pending_device_ready_ = false; |
| 203 // Notify the client that the device has been started. | 214 // Notify the client that the device has been started. |
| 204 if (event_handler_) | 215 if (event_handler_) |
| 205 event_handler_->OnDeviceStarted(device_id); | 216 event_handler_->OnDeviceStarted(device_id); |
| 206 } | 217 } |
| 207 | 218 |
| 208 void AudioInputDevice::OnIPCClosed() { | 219 void AudioInputDevice::OnIPCClosed() { |
| 209 ipc_ = NULL; | 220 ipc_ = NULL; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 221 DCHECK_EQ(0, stream_id_); | 232 DCHECK_EQ(0, stream_id_); |
| 222 if (stream_id_) | 233 if (stream_id_) |
| 223 return; | 234 return; |
| 224 | 235 |
| 225 stream_id_ = ipc_->AddDelegate(this); | 236 stream_id_ = ipc_->AddDelegate(this); |
| 226 // If |session_id_| is not specified, it will directly create the stream; | 237 // If |session_id_| is not specified, it will directly create the stream; |
| 227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 238 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser |
| 228 // and create the stream when getting a OnDeviceReady() callback. | 239 // and create the stream when getting a OnDeviceReady() callback. |
| 229 if (!session_id_) { | 240 if (!session_id_) { |
| 230 ipc_->CreateStream(stream_id_, audio_parameters_, | 241 ipc_->CreateStream(stream_id_, audio_parameters_, |
| 231 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); | 242 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_, |
| 243 kRequestedSharedMemoryCount); | |
| 232 } else { | 244 } else { |
| 233 ipc_->StartDevice(stream_id_, session_id_); | 245 ipc_->StartDevice(stream_id_, session_id_); |
| 234 pending_device_ready_ = true; | 246 pending_device_ready_ = true; |
| 235 } | 247 } |
| 236 } | 248 } |
| 237 | 249 |
| 238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 250 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
| 239 DCHECK(message_loop()->BelongsToCurrentThread()); | 251 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 240 session_id_ = session_id; | 252 session_id_ = session_id; |
| 241 } | 253 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 307 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
| 296 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 308 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
| 297 ShutDownOnIOThread(); | 309 ShutDownOnIOThread(); |
| 298 } | 310 } |
| 299 | 311 |
| 300 // AudioInputDevice::AudioThreadCallback | 312 // AudioInputDevice::AudioThreadCallback |
| 301 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 313 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
| 302 const AudioParameters& audio_parameters, | 314 const AudioParameters& audio_parameters, |
| 303 base::SharedMemoryHandle memory, | 315 base::SharedMemoryHandle memory, |
| 304 int memory_length, | 316 int memory_length, |
| 317 int total_segments, | |
| 305 CaptureCallback* capture_callback) | 318 CaptureCallback* capture_callback) |
| 306 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 319 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, |
| 320 total_segments), | |
| 321 current_segment_id_(0), | |
| 307 capture_callback_(capture_callback) { | 322 capture_callback_(capture_callback) { |
| 308 audio_bus_ = AudioBus::Create(audio_parameters_); | 323 audio_bus_ = AudioBus::Create(audio_parameters_); |
| 309 } | 324 } |
| 310 | 325 |
| 311 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 326 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
| 312 } | 327 } |
| 313 | 328 |
| 314 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 329 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
| 315 shared_memory_.Map(memory_length_); | 330 shared_memory_.Map(memory_length_); |
| 316 } | 331 } |
| 317 | 332 |
| 318 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 333 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { |
| 319 // The shared memory represents parameters, size of the data buffer and the | 334 // The shared memory represents parameters, size of the data buffer and the |
| 320 // actual data buffer containing audio data. Map the memory into this | 335 // actual data buffer containing audio data. Map the memory into this |
| 321 // structure and parse out parameters and the data area. | 336 // structure and parse out parameters and the data area. |
| 322 AudioInputBuffer* buffer = | 337 uint8* ptr = reinterpret_cast<uint8*>(shared_memory_.memory()); |
|
DaleCurtis
2013/03/07 02:04:37
static_cast ?
wjia(left Chromium)
2013/03/10 18:19:49
Done.
| |
| 323 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); | 338 ptr += current_segment_id_ * segment_length_; |
| 339 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); | |
| 324 DCHECK_EQ(buffer->params.size, | 340 DCHECK_EQ(buffer->params.size, |
| 325 memory_length_ - sizeof(AudioInputBufferParameters)); | 341 segment_length_ - sizeof(AudioInputBufferParameters)); |
| 326 double volume = buffer->params.volume; | 342 double volume = buffer->params.volume; |
| 327 | 343 |
| 328 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 344 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 329 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 345 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
| 330 const int bytes_per_sample = sizeof(memory[0]); | 346 const int bytes_per_sample = sizeof(memory[0]); |
| 331 | 347 |
| 348 if (++current_segment_id_ >= total_segments_) | |
|
DaleCurtis
2013/03/07 02:04:37
Same modulus comment.
wjia(left Chromium)
2013/03/10 18:19:49
Same reply. :-)
| |
| 349 current_segment_id_ = 0; | |
| 350 | |
| 332 // Deinterleave each channel and convert to 32-bit floating-point | 351 // Deinterleave each channel and convert to 32-bit floating-point |
| 333 // with nominal range -1.0 -> +1.0. | 352 // with nominal range -1.0 -> +1.0. |
| 334 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); | 353 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); |
| 335 | 354 |
| 336 // Deliver captured data to the client in floating point format | 355 // Deliver captured data to the client in floating point format |
| 337 // and update the audio-delay measurement. | 356 // and update the audio-delay measurement. |
| 338 capture_callback_->Capture(audio_bus_.get(), | 357 capture_callback_->Capture(audio_bus_.get(), |
| 339 audio_delay_milliseconds, volume); | 358 audio_delay_milliseconds, volume); |
| 340 } | 359 } |
| 341 | 360 |
| 342 } // namespace media | 361 } // namespace media |
| OLD | NEW |