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/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 CaptureCallback* capture_callback); | 32 CaptureCallback* capture_callback); |
33 ~AudioThreadCallback() override; | 33 ~AudioThreadCallback() override; |
34 | 34 |
35 void MapSharedMemory() override; | 35 void MapSharedMemory() override; |
36 | 36 |
37 // Called whenever we receive notifications about pending data. | 37 // Called whenever we receive notifications about pending data. |
38 void Process(uint32 pending_data) override; | 38 void Process(uint32 pending_data) override; |
39 | 39 |
40 private: | 40 private: |
41 int current_segment_id_; | 41 int current_segment_id_; |
| 42 uint32 last_buffer_id_; |
42 ScopedVector<media::AudioBus> audio_buses_; | 43 ScopedVector<media::AudioBus> audio_buses_; |
43 CaptureCallback* capture_callback_; | 44 CaptureCallback* capture_callback_; |
44 | 45 |
45 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 46 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
46 }; | 47 }; |
47 | 48 |
48 AudioInputDevice::AudioInputDevice( | 49 AudioInputDevice::AudioInputDevice( |
49 scoped_ptr<AudioInputIPC> ipc, | 50 scoped_ptr<AudioInputIPC> ipc, |
50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 51 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
51 : ScopedTaskRunnerObserver(io_task_runner), | 52 : ScopedTaskRunnerObserver(io_task_runner), |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // AudioInputDevice::AudioThreadCallback | 266 // AudioInputDevice::AudioThreadCallback |
266 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 267 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
267 const AudioParameters& audio_parameters, | 268 const AudioParameters& audio_parameters, |
268 base::SharedMemoryHandle memory, | 269 base::SharedMemoryHandle memory, |
269 int memory_length, | 270 int memory_length, |
270 int total_segments, | 271 int total_segments, |
271 CaptureCallback* capture_callback) | 272 CaptureCallback* capture_callback) |
272 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, | 273 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, |
273 total_segments), | 274 total_segments), |
274 current_segment_id_(0), | 275 current_segment_id_(0), |
| 276 last_buffer_id_(UINT32_MAX), |
275 capture_callback_(capture_callback) { | 277 capture_callback_(capture_callback) { |
276 } | 278 } |
277 | 279 |
278 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 280 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
279 } | 281 } |
280 | 282 |
281 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 283 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
282 shared_memory_.Map(memory_length_); | 284 shared_memory_.Map(memory_length_); |
283 | 285 |
284 // Create vector of audio buses by wrapping existing blocks of memory. | 286 // Create vector of audio buses by wrapping existing blocks of memory. |
285 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); | 287 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); |
286 for (int i = 0; i < total_segments_; ++i) { | 288 for (int i = 0; i < total_segments_; ++i) { |
287 media::AudioInputBuffer* buffer = | 289 media::AudioInputBuffer* buffer = |
288 reinterpret_cast<media::AudioInputBuffer*>(ptr); | 290 reinterpret_cast<media::AudioInputBuffer*>(ptr); |
289 scoped_ptr<media::AudioBus> audio_bus = | 291 scoped_ptr<media::AudioBus> audio_bus = |
290 media::AudioBus::WrapMemory(audio_parameters_, buffer->audio); | 292 media::AudioBus::WrapMemory(audio_parameters_, buffer->audio); |
291 audio_buses_.push_back(audio_bus.Pass()); | 293 audio_buses_.push_back(audio_bus.Pass()); |
292 ptr += segment_length_; | 294 ptr += segment_length_; |
293 } | 295 } |
294 } | 296 } |
295 | 297 |
296 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data) { | 298 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data) { |
| 299 CHECK_EQ(current_segment_id_, static_cast<int>(pending_data)); |
| 300 |
297 // The shared memory represents parameters, size of the data buffer and the | 301 // The shared memory represents parameters, size of the data buffer and the |
298 // actual data buffer containing audio data. Map the memory into this | 302 // actual data buffer containing audio data. Map the memory into this |
299 // structure and parse out parameters and the data area. | 303 // structure and parse out parameters and the data area. |
300 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); | 304 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); |
301 ptr += current_segment_id_ * segment_length_; | 305 ptr += current_segment_id_ * segment_length_; |
302 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); | 306 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); |
| 307 |
303 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, | 308 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, |
304 // the buffer may be bigger (on mac at least)). | 309 // the buffer may be bigger (on mac at least)). |
305 DCHECK_GE(buffer->params.size, | 310 DCHECK_GE(buffer->params.size, |
306 segment_length_ - sizeof(AudioInputBufferParameters)); | 311 segment_length_ - sizeof(AudioInputBufferParameters)); |
307 double volume = buffer->params.volume; | 312 |
308 bool key_pressed = buffer->params.key_pressed; | 313 // Verify correct sequence. |
| 314 CHECK_EQ(last_buffer_id_ + 1, buffer->params.id); |
| 315 last_buffer_id_ = buffer->params.id; |
309 | 316 |
310 // Use pre-allocated audio bus wrapping existing block of shared memory. | 317 // Use pre-allocated audio bus wrapping existing block of shared memory. |
311 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; | 318 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; |
312 | 319 |
313 // Deliver captured data to the client in floating point format | 320 // Deliver captured data to the client in floating point format and update |
314 // and update the audio-delay measurement. | 321 // the audio delay measurement. |
315 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | |
316 capture_callback_->Capture( | 322 capture_callback_->Capture( |
317 audio_bus, audio_delay_milliseconds, volume, key_pressed); | 323 audio_bus, |
| 324 buffer->params.hardware_delay_bytes / bytes_per_ms_, // Delay in ms |
| 325 buffer->params.volume, |
| 326 buffer->params.key_pressed); |
318 | 327 |
319 if (++current_segment_id_ >= total_segments_) | 328 if (++current_segment_id_ >= total_segments_) |
320 current_segment_id_ = 0; | 329 current_segment_id_ = 0; |
321 } | 330 } |
322 | 331 |
323 } // namespace media | 332 } // namespace media |
OLD | NEW |