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); |
303 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, | 307 // 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)). | 308 // the buffer may be bigger (on mac at least)). |
305 DCHECK_GE(buffer->params.size, | 309 DCHECK_GE(buffer->params.size, |
306 segment_length_ - sizeof(AudioInputBufferParameters)); | 310 segment_length_ - sizeof(AudioInputBufferParameters)); |
307 double volume = buffer->params.volume; | 311 double volume = buffer->params.volume; |
308 bool key_pressed = buffer->params.key_pressed; | 312 bool key_pressed = buffer->params.key_pressed; |
313 uint32 hardware_delay_bytes = buffer->params.hardware_delay_bytes; | |
tommi (sloooow) - chröme
2015/08/14 09:56:21
nit: are these temporaries necessary?
Henrik Grunell
2015/08/14 12:38:32
Good point, removed all of them.
| |
314 uint32 buffer_id = buffer->params.id; | |
315 | |
316 // Verify correct sequence. | |
317 if (last_buffer_id_ == UINT32_MAX) | |
DaleCurtis
2015/08/13 22:00:49
Again, shouldn't be necessary since overflow is de
Henrik Grunell
2015/08/14 12:38:32
Done.
| |
318 CHECK_EQ(0u, buffer_id); | |
319 else | |
320 CHECK_EQ(last_buffer_id_ + 1, buffer_id); | |
321 last_buffer_id_ = buffer_id; | |
309 | 322 |
310 // Use pre-allocated audio bus wrapping existing block of shared memory. | 323 // Use pre-allocated audio bus wrapping existing block of shared memory. |
311 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; | 324 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; |
312 | 325 |
313 // Deliver captured data to the client in floating point format | 326 // Deliver captured data to the client in floating point format |
314 // and update the audio-delay measurement. | 327 // and update the audio-delay measurement. |
315 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 328 int audio_delay_milliseconds = hardware_delay_bytes / bytes_per_ms_; |
316 capture_callback_->Capture( | 329 capture_callback_->Capture( |
317 audio_bus, audio_delay_milliseconds, volume, key_pressed); | 330 audio_bus, audio_delay_milliseconds, volume, key_pressed); |
318 | 331 |
319 if (++current_segment_id_ >= total_segments_) | 332 if (++current_segment_id_ >= total_segments_) |
320 current_segment_id_ = 0; | 333 current_segment_id_ = 0; |
321 } | 334 } |
322 | 335 |
323 } // namespace media | 336 } // namespace media |
OLD | NEW |