| 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; |
| 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 // The |index| states which buffer segment is used. |
| 41 virtual void Process(int pending_data, int index) OVERRIDE; |
| 32 | 42 |
| 33 private: | 43 private: |
| 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), |
| 44 event_handler_(NULL), | 54 event_handler_(NULL), |
| 45 ipc_(ipc), | 55 ipc_(ipc), |
| 46 stream_id_(0), | 56 stream_id_(0), |
| 47 session_id_(0), | 57 session_id_(0), |
| 48 pending_device_ready_(false), | 58 pending_device_ready_(false), |
| 49 agc_is_enabled_(false) { | 59 agc_is_enabled_(false), |
| 60 audio_thread_(true) { |
| 50 CHECK(ipc_); | 61 CHECK(ipc_); |
| 51 } | 62 } |
| 52 | 63 |
| 53 void AudioInputDevice::Initialize(const AudioParameters& params, | 64 void AudioInputDevice::Initialize(const AudioParameters& params, |
| 54 CaptureCallback* callback, | 65 CaptureCallback* callback, |
| 55 CaptureEventHandler* event_handler) { | 66 CaptureEventHandler* event_handler) { |
| 56 DCHECK(!callback_); | 67 DCHECK(!callback_); |
| 57 DCHECK(!event_handler_); | 68 DCHECK(!event_handler_); |
| 58 audio_parameters_ = params; | 69 audio_parameters_ = params; |
| 59 callback_ = callback; | 70 callback_ = callback; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { | 108 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { |
| 98 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; | 109 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; |
| 99 message_loop()->PostTask(FROM_HERE, | 110 message_loop()->PostTask(FROM_HERE, |
| 100 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, | 111 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, |
| 101 this, enabled)); | 112 this, enabled)); |
| 102 } | 113 } |
| 103 | 114 |
| 104 void AudioInputDevice::OnStreamCreated( | 115 void AudioInputDevice::OnStreamCreated( |
| 105 base::SharedMemoryHandle handle, | 116 base::SharedMemoryHandle handle, |
| 106 base::SyncSocket::Handle socket_handle, | 117 base::SyncSocket::Handle socket_handle, |
| 107 int length) { | 118 int length, |
| 119 int total_segments) { |
| 108 DCHECK(message_loop()->BelongsToCurrentThread()); | 120 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 109 #if defined(OS_WIN) | 121 #if defined(OS_WIN) |
| 110 DCHECK(handle); | 122 DCHECK(handle); |
| 111 DCHECK(socket_handle); | 123 DCHECK(socket_handle); |
| 112 #else | 124 #else |
| 113 DCHECK_GE(handle.fd, 0); | 125 DCHECK_GE(handle.fd, 0); |
| 114 DCHECK_GE(socket_handle, 0); | 126 DCHECK_GE(socket_handle, 0); |
| 115 #endif | 127 #endif |
| 116 DCHECK(length); | 128 DCHECK(length); |
| 117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; | 129 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; |
| 118 | 130 |
| 119 // We should only get this callback if stream_id_ is valid. If it is not, | 131 // 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 | 132 // 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 | 133 // 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 | 134 // stream_id_ is 0 the AudioInputDevice instance is not registered as a |
| 123 // delegate and hence it should not receive callbacks. | 135 // delegate and hence it should not receive callbacks. |
| 124 DCHECK(stream_id_); | 136 DCHECK(stream_id_); |
| 125 | 137 |
| 126 base::AutoLock auto_lock(audio_thread_lock_); | 138 base::AutoLock auto_lock(audio_thread_lock_); |
| 127 | 139 |
| 128 DCHECK(audio_thread_.IsStopped()); | 140 DCHECK(audio_thread_.IsStopped()); |
| 129 audio_callback_.reset( | 141 audio_callback_.reset( |
| 130 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, | 142 new AudioInputDevice::AudioThreadCallback( |
| 131 length, callback_)); | 143 audio_parameters_, handle, length, total_segments, callback_)); |
| 132 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); | 144 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); |
| 133 | 145 |
| 134 MessageLoop::current()->PostTask(FROM_HERE, | 146 MessageLoop::current()->PostTask(FROM_HERE, |
| 135 base::Bind(&AudioInputDevice::StartOnIOThread, this)); | 147 base::Bind(&AudioInputDevice::StartOnIOThread, this)); |
| 136 } | 148 } |
| 137 | 149 |
| 138 void AudioInputDevice::OnVolume(double volume) { | 150 void AudioInputDevice::OnVolume(double volume) { |
| 139 NOTIMPLEMENTED(); | 151 NOTIMPLEMENTED(); |
| 140 } | 152 } |
| 141 | 153 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 if (!pending_device_ready_) | 201 if (!pending_device_ready_) |
| 190 return; | 202 return; |
| 191 | 203 |
| 192 // If AudioInputDeviceManager returns an empty string, it means no device | 204 // If AudioInputDeviceManager returns an empty string, it means no device |
| 193 // is ready for start. | 205 // is ready for start. |
| 194 if (device_id.empty()) { | 206 if (device_id.empty()) { |
| 195 ipc_->RemoveDelegate(stream_id_); | 207 ipc_->RemoveDelegate(stream_id_); |
| 196 stream_id_ = 0; | 208 stream_id_ = 0; |
| 197 } else { | 209 } else { |
| 198 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, | 210 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, |
| 199 agc_is_enabled_); | 211 agc_is_enabled_, kRequestedSharedMemoryCount); |
| 200 } | 212 } |
| 201 | 213 |
| 202 pending_device_ready_ = false; | 214 pending_device_ready_ = false; |
| 203 // Notify the client that the device has been started. | 215 // Notify the client that the device has been started. |
| 204 if (event_handler_) | 216 if (event_handler_) |
| 205 event_handler_->OnDeviceStarted(device_id); | 217 event_handler_->OnDeviceStarted(device_id); |
| 206 } | 218 } |
| 207 | 219 |
| 208 void AudioInputDevice::OnIPCClosed() { | 220 void AudioInputDevice::OnIPCClosed() { |
| 209 ipc_ = NULL; | 221 ipc_ = NULL; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 221 DCHECK_EQ(0, stream_id_); | 233 DCHECK_EQ(0, stream_id_); |
| 222 if (stream_id_) | 234 if (stream_id_) |
| 223 return; | 235 return; |
| 224 | 236 |
| 225 stream_id_ = ipc_->AddDelegate(this); | 237 stream_id_ = ipc_->AddDelegate(this); |
| 226 // If |session_id_| is not specified, it will directly create the stream; | 238 // If |session_id_| is not specified, it will directly create the stream; |
| 227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 239 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser |
| 228 // and create the stream when getting a OnDeviceReady() callback. | 240 // and create the stream when getting a OnDeviceReady() callback. |
| 229 if (!session_id_) { | 241 if (!session_id_) { |
| 230 ipc_->CreateStream(stream_id_, audio_parameters_, | 242 ipc_->CreateStream(stream_id_, audio_parameters_, |
| 231 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); | 243 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_, |
| 244 kRequestedSharedMemoryCount); |
| 232 } else { | 245 } else { |
| 233 ipc_->StartDevice(stream_id_, session_id_); | 246 ipc_->StartDevice(stream_id_, session_id_); |
| 234 pending_device_ready_ = true; | 247 pending_device_ready_ = true; |
| 235 } | 248 } |
| 236 } | 249 } |
| 237 | 250 |
| 238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 251 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
| 239 DCHECK(message_loop()->BelongsToCurrentThread()); | 252 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 240 session_id_ = session_id; | 253 session_id_ = session_id; |
| 241 } | 254 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 308 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
| 296 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 309 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
| 297 ShutDownOnIOThread(); | 310 ShutDownOnIOThread(); |
| 298 } | 311 } |
| 299 | 312 |
| 300 // AudioInputDevice::AudioThreadCallback | 313 // AudioInputDevice::AudioThreadCallback |
| 301 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 314 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
| 302 const AudioParameters& audio_parameters, | 315 const AudioParameters& audio_parameters, |
| 303 base::SharedMemoryHandle memory, | 316 base::SharedMemoryHandle memory, |
| 304 int memory_length, | 317 int memory_length, |
| 318 int total_segments, |
| 305 CaptureCallback* capture_callback) | 319 CaptureCallback* capture_callback) |
| 306 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 320 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, |
| 321 total_segments), |
| 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( |
| 334 int pending_data, int index) { |
| 319 // The shared memory represents parameters, size of the data buffer and the | 335 // The shared memory represents parameters, size of the data buffer and the |
| 320 // actual data buffer containing audio data. Map the memory into this | 336 // actual data buffer containing audio data. Map the memory into this |
| 321 // structure and parse out parameters and the data area. | 337 // structure and parse out parameters and the data area. |
| 322 AudioInputBuffer* buffer = | 338 uint8* ptr = reinterpret_cast<uint8*>(shared_memory_.memory()); |
| 323 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); | 339 ptr += index * segment_length_; |
| 340 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); |
| 324 DCHECK_EQ(buffer->params.size, | 341 DCHECK_EQ(buffer->params.size, |
| 325 memory_length_ - sizeof(AudioInputBufferParameters)); | 342 segment_length_ - sizeof(AudioInputBufferParameters)); |
| 326 double volume = buffer->params.volume; | 343 double volume = buffer->params.volume; |
| 327 | 344 |
| 328 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 345 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 329 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 346 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
| 330 const int bytes_per_sample = sizeof(memory[0]); | 347 const int bytes_per_sample = sizeof(memory[0]); |
| 331 | 348 |
| 332 // Deinterleave each channel and convert to 32-bit floating-point | 349 // Deinterleave each channel and convert to 32-bit floating-point |
| 333 // with nominal range -1.0 -> +1.0. | 350 // with nominal range -1.0 -> +1.0. |
| 334 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); | 351 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); |
| 335 | 352 |
| 336 // Deliver captured data to the client in floating point format | 353 // Deliver captured data to the client in floating point format |
| 337 // and update the audio-delay measurement. | 354 // and update the audio-delay measurement. |
| 338 capture_callback_->Capture(audio_bus_.get(), | 355 capture_callback_->Capture(audio_bus_.get(), |
| 339 audio_delay_milliseconds, volume); | 356 audio_delay_milliseconds, volume); |
| 340 } | 357 } |
| 341 | 358 |
| 342 } // namespace media | 359 } // namespace media |
| OLD | NEW |