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