| 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 9 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 11 #include "media/audio/audio_manager_base.h" | 12 #include "media/audio/audio_manager_base.h" |
| 12 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 // The number of shared memory buffer segments indicated to browser process | 17 // 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 // 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 // dependent how fast the renderer process can pick up captured data from |
| (...skipping 13 matching lines...) Expand all Loading... |
| 32 CaptureCallback* capture_callback); | 33 CaptureCallback* capture_callback); |
| 33 virtual ~AudioThreadCallback(); | 34 virtual ~AudioThreadCallback(); |
| 34 | 35 |
| 35 virtual void MapSharedMemory() OVERRIDE; | 36 virtual void MapSharedMemory() OVERRIDE; |
| 36 | 37 |
| 37 // Called whenever we receive notifications about pending data. | 38 // Called whenever we receive notifications about pending data. |
| 38 virtual void Process(int pending_data) OVERRIDE; | 39 virtual void Process(int pending_data) OVERRIDE; |
| 39 | 40 |
| 40 private: | 41 private: |
| 41 int current_segment_id_; | 42 int current_segment_id_; |
| 43 ScopedVector<media::AudioBus> audio_buses_; |
| 42 CaptureCallback* capture_callback_; | 44 CaptureCallback* capture_callback_; |
| 43 scoped_ptr<AudioBus> audio_bus_; | 45 |
| 44 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 46 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| 45 }; | 47 }; |
| 46 | 48 |
| 47 AudioInputDevice::AudioInputDevice( | 49 AudioInputDevice::AudioInputDevice( |
| 48 scoped_ptr<AudioInputIPC> ipc, | 50 scoped_ptr<AudioInputIPC> ipc, |
| 49 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 51 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 50 : ScopedTaskRunnerObserver(io_task_runner), | 52 : ScopedTaskRunnerObserver(io_task_runner), |
| 51 callback_(NULL), | 53 callback_(NULL), |
| 52 ipc_(ipc.Pass()), | 54 ipc_(ipc.Pass()), |
| 53 state_(IDLE), | 55 state_(IDLE), |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 268 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
| 267 const AudioParameters& audio_parameters, | 269 const AudioParameters& audio_parameters, |
| 268 base::SharedMemoryHandle memory, | 270 base::SharedMemoryHandle memory, |
| 269 int memory_length, | 271 int memory_length, |
| 270 int total_segments, | 272 int total_segments, |
| 271 CaptureCallback* capture_callback) | 273 CaptureCallback* capture_callback) |
| 272 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, | 274 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, |
| 273 total_segments), | 275 total_segments), |
| 274 current_segment_id_(0), | 276 current_segment_id_(0), |
| 275 capture_callback_(capture_callback) { | 277 capture_callback_(capture_callback) { |
| 276 audio_bus_ = AudioBus::Create(audio_parameters_); | |
| 277 } | 278 } |
| 278 | 279 |
| 279 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 280 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
| 280 } | 281 } |
| 281 | 282 |
| 282 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 283 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
| 283 shared_memory_.Map(memory_length_); | 284 shared_memory_.Map(memory_length_); |
| 285 |
| 286 // Create vector of audio buses by wrapping existing blocks of memory. |
| 287 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); |
| 288 for (int i = 0; i < total_segments_; ++i) { |
| 289 media::AudioInputBuffer* buffer = |
| 290 reinterpret_cast<media::AudioInputBuffer*>(ptr); |
| 291 scoped_ptr<media::AudioBus> audio_bus = |
| 292 media::AudioBus::WrapMemory(audio_parameters_, buffer->audio); |
| 293 audio_buses_.push_back(audio_bus.release()); |
| 294 ptr += segment_length_; |
| 295 } |
| 284 } | 296 } |
| 285 | 297 |
| 286 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 298 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { |
| 287 // The shared memory represents parameters, size of the data buffer and the | 299 // The shared memory represents parameters, size of the data buffer and the |
| 288 // actual data buffer containing audio data. Map the memory into this | 300 // actual data buffer containing audio data. Map the memory into this |
| 289 // structure and parse out parameters and the data area. | 301 // structure and parse out parameters and the data area. |
| 290 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); | 302 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); |
| 291 ptr += current_segment_id_ * segment_length_; | 303 ptr += current_segment_id_ * segment_length_; |
| 292 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); | 304 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); |
| 293 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, | 305 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, |
| 294 // the buffer may be bigger (on mac at least)). | 306 // the buffer may be bigger (on mac at least)). |
| 295 DCHECK_GE(buffer->params.size, | 307 DCHECK_GE(buffer->params.size, |
| 296 segment_length_ - sizeof(AudioInputBufferParameters)); | 308 segment_length_ - sizeof(AudioInputBufferParameters)); |
| 297 double volume = buffer->params.volume; | 309 double volume = buffer->params.volume; |
| 298 bool key_pressed = buffer->params.key_pressed; | 310 bool key_pressed = buffer->params.key_pressed; |
| 299 | 311 |
| 312 // Use pre-allocated audio bus wrapping existing block of shared memory. |
| 313 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; |
| 314 |
| 315 // Deliver captured data to the client in floating point format |
| 316 // and update the audio-delay measurement. |
| 300 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 317 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 301 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 318 capture_callback_->Capture( |
| 302 const int bytes_per_sample = sizeof(memory[0]); | 319 audio_bus, audio_delay_milliseconds, volume, key_pressed); |
| 303 | 320 |
| 304 if (++current_segment_id_ >= total_segments_) | 321 if (++current_segment_id_ >= total_segments_) |
| 305 current_segment_id_ = 0; | 322 current_segment_id_ = 0; |
| 306 | |
| 307 // Deinterleave each channel and convert to 32-bit floating-point | |
| 308 // with nominal range -1.0 -> +1.0. | |
| 309 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); | |
| 310 | |
| 311 // Deliver captured data to the client in floating point format | |
| 312 // and update the audio-delay measurement. | |
| 313 capture_callback_->Capture( | |
| 314 audio_bus_.get(), audio_delay_milliseconds, volume, key_pressed); | |
| 315 } | 323 } |
| 316 | 324 |
| 317 } // namespace media | 325 } // namespace media |
| OLD | NEW |