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" |
11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
12 #include "content/common/media/audio_messages.h" | 12 #include "content/common/media/audio_messages.h" |
13 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
15 #include "media/audio/audio_manager_base.h" | 15 #include "media/audio/audio_manager_base.h" |
16 #include "media/audio/audio_util.h" | 16 #include "media/audio/audio_util.h" |
17 | 17 |
18 // Takes care of invoking the capture callback on the audio thread. | 18 // Takes care of invoking the capture callback on the audio thread. |
19 // An instance of this class is created for each capture stream in | 19 // An instance of this class is created for each capture stream in |
20 // OnLowLatencyCreated(). | 20 // OnLowLatencyCreated(). |
21 class AudioInputDevice::AudioThreadCallback | 21 class AudioInputDevice::AudioThreadCallback |
22 : public AudioDeviceThread::Callback { | 22 : public AudioDeviceThread::Callback { |
23 public: | 23 public: |
24 AudioThreadCallback(const AudioParameters& audio_parameters, | 24 AudioThreadCallback(const media::AudioParameters& audio_parameters, |
25 base::SharedMemoryHandle memory, | 25 base::SharedMemoryHandle memory, |
26 int memory_length, | 26 int memory_length, |
27 CaptureCallback* capture_callback); | 27 CaptureCallback* capture_callback); |
28 virtual ~AudioThreadCallback(); | 28 virtual ~AudioThreadCallback(); |
29 | 29 |
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(const AudioParameters& params, | 40 AudioInputDevice::AudioInputDevice(const media::AudioParameters& params, |
41 CaptureCallback* callback, | 41 CaptureCallback* callback, |
42 CaptureEventHandler* event_handler) | 42 CaptureEventHandler* event_handler) |
43 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), | 43 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), |
44 audio_parameters_(params), | 44 audio_parameters_(params), |
45 callback_(callback), | 45 callback_(callback), |
46 event_handler_(event_handler), | 46 event_handler_(event_handler), |
47 volume_(1.0), | 47 volume_(1.0), |
48 stream_id_(0), | 48 stream_id_(0), |
49 session_id_(0), | 49 session_id_(0), |
50 pending_device_ready_(false), | 50 pending_device_ready_(false), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 DCHECK_EQ(0, stream_id_); | 110 DCHECK_EQ(0, stream_id_); |
111 if (stream_id_) | 111 if (stream_id_) |
112 return; | 112 return; |
113 | 113 |
114 stream_id_ = filter_->AddDelegate(this); | 114 stream_id_ = filter_->AddDelegate(this); |
115 // If |session_id_| is not specified, it will directly create the stream; | 115 // If |session_id_| is not specified, it will directly create the stream; |
116 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 116 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser |
117 // and create the stream when getting a OnDeviceReady() callback. | 117 // and create the stream when getting a OnDeviceReady() callback. |
118 if (!session_id_) { | 118 if (!session_id_) { |
119 Send(new AudioInputHostMsg_CreateStream( | 119 Send(new AudioInputHostMsg_CreateStream( |
120 stream_id_, audio_parameters_, AudioManagerBase::kDefaultDeviceId, | 120 stream_id_, audio_parameters_, |
| 121 media::AudioManagerBase::kDefaultDeviceId, |
121 agc_is_enabled_)); | 122 agc_is_enabled_)); |
122 } else { | 123 } else { |
123 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); | 124 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); |
124 pending_device_ready_ = true; | 125 pending_device_ready_ = true; |
125 } | 126 } |
126 } | 127 } |
127 | 128 |
128 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 129 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
129 DCHECK(message_loop()->BelongsToCurrentThread()); | 130 DCHECK(message_loop()->BelongsToCurrentThread()); |
130 session_id_ = session_id; | 131 session_id_ = session_id; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 filter_->Send(message); | 291 filter_->Send(message); |
291 } | 292 } |
292 | 293 |
293 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 294 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
294 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 295 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
295 ShutDownOnIOThread(); | 296 ShutDownOnIOThread(); |
296 } | 297 } |
297 | 298 |
298 // AudioInputDevice::AudioThreadCallback | 299 // AudioInputDevice::AudioThreadCallback |
299 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 300 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
300 const AudioParameters& audio_parameters, | 301 const media::AudioParameters& audio_parameters, |
301 base::SharedMemoryHandle memory, | 302 base::SharedMemoryHandle memory, |
302 int memory_length, | 303 int memory_length, |
303 CaptureCallback* capture_callback) | 304 CaptureCallback* capture_callback) |
304 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 305 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), |
305 capture_callback_(capture_callback) { | 306 capture_callback_(capture_callback) { |
306 } | 307 } |
307 | 308 |
308 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 309 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
309 } | 310 } |
310 | 311 |
311 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 312 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
312 shared_memory_.Map(memory_length_); | 313 shared_memory_.Map(memory_length_); |
313 } | 314 } |
314 | 315 |
315 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 316 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { |
316 // The shared memory represents parameters, size of the data buffer and the | 317 // The shared memory represents parameters, size of the data buffer and the |
317 // actual data buffer containing audio data. Map the memory into this | 318 // actual data buffer containing audio data. Map the memory into this |
318 // structure and parse out parameters and the data area. | 319 // structure and parse out parameters and the data area. |
319 AudioInputBuffer* buffer = | 320 media::AudioInputBuffer* buffer = |
320 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); | 321 reinterpret_cast<media::AudioInputBuffer*>(shared_memory_.memory()); |
321 uint32 size = buffer->params.size; | 322 uint32 size = buffer->params.size; |
322 DCHECK_EQ(size, memory_length_ - sizeof(AudioInputBufferParameters)); | 323 DCHECK_EQ(size, memory_length_ - sizeof(media::AudioInputBufferParameters)); |
323 double volume = buffer->params.volume; | 324 double volume = buffer->params.volume; |
324 | 325 |
325 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 326 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
326 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 327 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
327 const size_t number_of_frames = audio_parameters_.frames_per_buffer(); | 328 const size_t number_of_frames = audio_parameters_.frames_per_buffer(); |
328 const int bytes_per_sample = sizeof(memory[0]); | 329 const int bytes_per_sample = sizeof(memory[0]); |
329 | 330 |
330 // Deinterleave each channel and convert to 32-bit floating-point | 331 // Deinterleave each channel and convert to 32-bit floating-point |
331 // with nominal range -1.0 -> +1.0. | 332 // with nominal range -1.0 -> +1.0. |
332 for (int channel_index = 0; channel_index < audio_parameters_.channels(); | 333 for (int channel_index = 0; channel_index < audio_parameters_.channels(); |
333 ++channel_index) { | 334 ++channel_index) { |
334 media::DeinterleaveAudioChannel(memory, | 335 media::DeinterleaveAudioChannel(memory, |
335 audio_data_[channel_index], | 336 audio_data_[channel_index], |
336 audio_parameters_.channels(), | 337 audio_parameters_.channels(), |
337 channel_index, | 338 channel_index, |
338 bytes_per_sample, | 339 bytes_per_sample, |
339 number_of_frames); | 340 number_of_frames); |
340 } | 341 } |
341 | 342 |
342 // Deliver captured data to the client in floating point format | 343 // Deliver captured data to the client in floating point format |
343 // and update the audio-delay measurement. | 344 // and update the audio-delay measurement. |
344 capture_callback_->Capture(audio_data_, number_of_frames, | 345 capture_callback_->Capture(audio_data_, number_of_frames, |
345 audio_delay_milliseconds, volume); | 346 audio_delay_milliseconds, volume); |
346 } | 347 } |
OLD | NEW |