| 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 "content/renderer/media/audio_input_device.h" | 5 #include "content/renderer/media/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" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 virtual void MapSharedMemory() OVERRIDE; | 30 virtual void MapSharedMemory() OVERRIDE; |
| 31 | 31 |
| 32 // Called whenever we receive notifications about pending data. | 32 // Called whenever we receive notifications about pending data. |
| 33 virtual void Process(int pending_data) OVERRIDE; | 33 virtual void Process(int pending_data) OVERRIDE; |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 CaptureCallback* capture_callback_; | 36 CaptureCallback* capture_callback_; |
| 37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 AudioInputDevice::AudioInputDevice(size_t buffer_size, | 40 AudioInputDevice::AudioInputDevice(const AudioParameters& params, |
| 41 int channels, | |
| 42 double sample_rate, | |
| 43 CaptureCallback* callback, | 41 CaptureCallback* callback, |
| 44 CaptureEventHandler* event_handler) | 42 CaptureEventHandler* event_handler) |
| 45 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), | 43 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), |
| 44 audio_parameters_(params), |
| 46 callback_(callback), | 45 callback_(callback), |
| 47 event_handler_(event_handler), | 46 event_handler_(event_handler), |
| 48 volume_(1.0), | 47 volume_(1.0), |
| 49 stream_id_(0), | 48 stream_id_(0), |
| 50 session_id_(0), | 49 session_id_(0), |
| 51 pending_device_ready_(false) { | 50 pending_device_ready_(false) { |
| 52 filter_ = RenderThreadImpl::current()->audio_input_message_filter(); | 51 filter_ = RenderThreadImpl::current()->audio_input_message_filter(); |
| 53 #if defined(OS_MACOSX) | |
| 54 DVLOG(1) << "Using AUDIO_PCM_LOW_LATENCY as input mode on Mac OS X."; | |
| 55 audio_parameters_.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
| 56 #elif defined(OS_WIN) | |
| 57 DVLOG(1) << "Using AUDIO_PCM_LOW_LATENCY as input mode on Windows."; | |
| 58 audio_parameters_.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
| 59 #else | |
| 60 // TODO(henrika): add support for AUDIO_PCM_LOW_LATENCY on Linux as well. | |
| 61 audio_parameters_.format = AudioParameters::AUDIO_PCM_LINEAR; | |
| 62 #endif | |
| 63 audio_parameters_.channels = channels; | |
| 64 audio_parameters_.sample_rate = static_cast<int>(sample_rate); | |
| 65 audio_parameters_.bits_per_sample = 16; | |
| 66 audio_parameters_.samples_per_packet = buffer_size; | |
| 67 } | 52 } |
| 68 | 53 |
| 69 AudioInputDevice::~AudioInputDevice() { | 54 AudioInputDevice::~AudioInputDevice() { |
| 70 // TODO(henrika): The current design requires that the user calls | 55 // TODO(henrika): The current design requires that the user calls |
| 71 // Stop before deleting this class. | 56 // Stop before deleting this class. |
| 72 CHECK_EQ(0, stream_id_); | 57 CHECK_EQ(0, stream_id_); |
| 73 } | 58 } |
| 74 | 59 |
| 75 void AudioInputDevice::Start() { | 60 void AudioInputDevice::Start() { |
| 76 DVLOG(1) << "Start()"; | 61 DVLOG(1) << "Start()"; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 277 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
| 293 } | 278 } |
| 294 | 279 |
| 295 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 280 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
| 296 shared_memory_.Map(memory_length_); | 281 shared_memory_.Map(memory_length_); |
| 297 } | 282 } |
| 298 | 283 |
| 299 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 284 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { |
| 300 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 285 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 301 int16* memory = reinterpret_cast<int16*>(shared_memory_.memory()); | 286 int16* memory = reinterpret_cast<int16*>(shared_memory_.memory()); |
| 302 const size_t number_of_frames = audio_parameters_.samples_per_packet; | 287 const size_t number_of_frames = audio_parameters_.frames_per_buffer(); |
| 303 const int bytes_per_sample = sizeof(memory[0]); | 288 const int bytes_per_sample = sizeof(memory[0]); |
| 304 | 289 |
| 305 // Deinterleave each channel and convert to 32-bit floating-point | 290 // Deinterleave each channel and convert to 32-bit floating-point |
| 306 // with nominal range -1.0 -> +1.0. | 291 // with nominal range -1.0 -> +1.0. |
| 307 for (int channel_index = 0; channel_index < audio_parameters_.channels; | 292 for (int channel_index = 0; channel_index < audio_parameters_.channels(); |
| 308 ++channel_index) { | 293 ++channel_index) { |
| 309 media::DeinterleaveAudioChannel(memory, | 294 media::DeinterleaveAudioChannel(memory, |
| 310 audio_data_[channel_index], | 295 audio_data_[channel_index], |
| 311 audio_parameters_.channels, | 296 audio_parameters_.channels(), |
| 312 channel_index, | 297 channel_index, |
| 313 bytes_per_sample, | 298 bytes_per_sample, |
| 314 number_of_frames); | 299 number_of_frames); |
| 315 } | 300 } |
| 316 | 301 |
| 317 // Deliver captured data to the client in floating point format | 302 // Deliver captured data to the client in floating point format |
| 318 // and update the audio-delay measurement. | 303 // and update the audio-delay measurement. |
| 319 capture_callback_->Capture(audio_data_, number_of_frames, | 304 capture_callback_->Capture(audio_data_, number_of_frames, |
| 320 audio_delay_milliseconds); | 305 audio_delay_milliseconds); |
| 321 } | 306 } |
| OLD | NEW |