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 media::AudioParameters& audio_parameters, | 24 AudioThreadCallback(const 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 media::AudioParameters& params, | 40 AudioInputDevice::AudioInputDevice(const 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_, | 120 stream_id_, audio_parameters_, AudioManagerBase::kDefaultDeviceId, |
121 media::AudioManagerBase::kDefaultDeviceId, | |
122 agc_is_enabled_)); | 121 agc_is_enabled_)); |
123 } else { | 122 } else { |
124 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); | 123 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); |
125 pending_device_ready_ = true; | 124 pending_device_ready_ = true; |
126 } | 125 } |
127 } | 126 } |
128 | 127 |
129 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 128 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
130 DCHECK(message_loop()->BelongsToCurrentThread()); | 129 DCHECK(message_loop()->BelongsToCurrentThread()); |
131 session_id_ = session_id; | 130 session_id_ = session_id; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 filter_->Send(message); | 290 filter_->Send(message); |
292 } | 291 } |
293 | 292 |
294 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 293 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
295 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 294 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
296 ShutDownOnIOThread(); | 295 ShutDownOnIOThread(); |
297 } | 296 } |
298 | 297 |
299 // AudioInputDevice::AudioThreadCallback | 298 // AudioInputDevice::AudioThreadCallback |
300 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( | 299 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( |
301 const media::AudioParameters& audio_parameters, | 300 const AudioParameters& audio_parameters, |
302 base::SharedMemoryHandle memory, | 301 base::SharedMemoryHandle memory, |
303 int memory_length, | 302 int memory_length, |
304 CaptureCallback* capture_callback) | 303 CaptureCallback* capture_callback) |
305 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 304 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), |
306 capture_callback_(capture_callback) { | 305 capture_callback_(capture_callback) { |
307 } | 306 } |
308 | 307 |
309 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { | 308 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { |
310 } | 309 } |
311 | 310 |
312 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { | 311 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { |
313 shared_memory_.Map(memory_length_); | 312 shared_memory_.Map(memory_length_); |
314 } | 313 } |
315 | 314 |
316 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { | 315 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { |
317 // The shared memory represents parameters, size of the data buffer and the | 316 // The shared memory represents parameters, size of the data buffer and the |
318 // actual data buffer containing audio data. Map the memory into this | 317 // actual data buffer containing audio data. Map the memory into this |
319 // structure and parse out parameters and the data area. | 318 // structure and parse out parameters and the data area. |
320 media::AudioInputBuffer* buffer = | 319 AudioInputBuffer* buffer = |
321 reinterpret_cast<media::AudioInputBuffer*>(shared_memory_.memory()); | 320 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); |
322 uint32 size = buffer->params.size; | 321 uint32 size = buffer->params.size; |
323 DCHECK_EQ(size, memory_length_ - sizeof(media::AudioInputBufferParameters)); | 322 DCHECK_EQ(size, memory_length_ - sizeof(AudioInputBufferParameters)); |
324 double volume = buffer->params.volume; | 323 double volume = buffer->params.volume; |
325 | 324 |
326 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 325 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
327 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); | 326 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
328 const size_t number_of_frames = audio_parameters_.frames_per_buffer(); | 327 const size_t number_of_frames = audio_parameters_.frames_per_buffer(); |
329 const int bytes_per_sample = sizeof(memory[0]); | 328 const int bytes_per_sample = sizeof(memory[0]); |
330 | 329 |
331 // Deinterleave each channel and convert to 32-bit floating-point | 330 // Deinterleave each channel and convert to 32-bit floating-point |
332 // with nominal range -1.0 -> +1.0. | 331 // with nominal range -1.0 -> +1.0. |
333 for (int channel_index = 0; channel_index < audio_parameters_.channels(); | 332 for (int channel_index = 0; channel_index < audio_parameters_.channels(); |
334 ++channel_index) { | 333 ++channel_index) { |
335 media::DeinterleaveAudioChannel(memory, | 334 media::DeinterleaveAudioChannel(memory, |
336 audio_data_[channel_index], | 335 audio_data_[channel_index], |
337 audio_parameters_.channels(), | 336 audio_parameters_.channels(), |
338 channel_index, | 337 channel_index, |
339 bytes_per_sample, | 338 bytes_per_sample, |
340 number_of_frames); | 339 number_of_frames); |
341 } | 340 } |
342 | 341 |
343 // Deliver captured data to the client in floating point format | 342 // Deliver captured data to the client in floating point format |
344 // and update the audio-delay measurement. | 343 // and update the audio-delay measurement. |
345 capture_callback_->Capture(audio_data_, number_of_frames, | 344 capture_callback_->Capture(audio_data_, number_of_frames, |
346 audio_delay_milliseconds, volume); | 345 audio_delay_milliseconds, volume); |
347 } | 346 } |
OLD | NEW |