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