| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/time.h" | 9 #include "base/time.h" |
| 10 #include "content/common/child_process.h" | 10 #include "content/common/child_process.h" |
| 11 #include "content/common/media/audio_messages.h" | 11 #include "content/common/media/audio_messages.h" |
| 12 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 13 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 14 #include "media/audio/audio_manager_base.h" |
| 14 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
| 15 | 16 |
| 16 AudioInputDevice::AudioInputDevice(size_t buffer_size, | 17 AudioInputDevice::AudioInputDevice(size_t buffer_size, |
| 17 int channels, | 18 int channels, |
| 18 double sample_rate, | 19 double sample_rate, |
| 19 CaptureCallback* callback, | 20 CaptureCallback* callback, |
| 20 CaptureEventHandler* event_handler) | 21 CaptureEventHandler* event_handler) |
| 21 : callback_(callback), | 22 : callback_(callback), |
| 22 event_handler_(event_handler), | 23 event_handler_(event_handler), |
| 23 audio_delay_milliseconds_(0), | 24 audio_delay_milliseconds_(0), |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 // Make sure we don't call Start() more than once. | 120 // Make sure we don't call Start() more than once. |
| 120 DCHECK_EQ(0, stream_id_); | 121 DCHECK_EQ(0, stream_id_); |
| 121 if (stream_id_) | 122 if (stream_id_) |
| 122 return; | 123 return; |
| 123 | 124 |
| 124 stream_id_ = filter_->AddDelegate(this); | 125 stream_id_ = filter_->AddDelegate(this); |
| 125 // If |session_id_| is not specified, it will directly create the stream; | 126 // If |session_id_| is not specified, it will directly create the stream; |
| 126 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 127 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser |
| 127 // and create the stream when getting a OnDeviceReady() callback. | 128 // and create the stream when getting a OnDeviceReady() callback. |
| 128 if (!session_id_) { | 129 if (!session_id_) { |
| 129 Send(new AudioInputHostMsg_CreateStream(stream_id_, audio_parameters_, | 130 Send(new AudioInputHostMsg_CreateStream( |
| 130 true)); | 131 stream_id_, audio_parameters_, true, |
| 132 AudioManagerBase::kDefaultDeviceId)); |
| 131 } else { | 133 } else { |
| 132 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); | 134 Send(new AudioInputHostMsg_StartDevice(stream_id_, session_id_)); |
| 133 pending_device_ready_ = true; | 135 pending_device_ready_ = true; |
| 134 } | 136 } |
| 135 } | 137 } |
| 136 | 138 |
| 137 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 139 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
| 138 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | 140 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 139 session_id_ = session_id; | 141 session_id_ = session_id; |
| 140 } | 142 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 break; | 241 break; |
| 240 case kAudioStreamError: | 242 case kAudioStreamError: |
| 241 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(kError)"; | 243 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(kError)"; |
| 242 break; | 244 break; |
| 243 default: | 245 default: |
| 244 NOTREACHED(); | 246 NOTREACHED(); |
| 245 break; | 247 break; |
| 246 } | 248 } |
| 247 } | 249 } |
| 248 | 250 |
| 249 void AudioInputDevice::OnDeviceReady(int index) { | 251 void AudioInputDevice::OnDeviceReady(const std::string& device_id) { |
| 250 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | 252 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 251 VLOG(1) << "OnDeviceReady (index=" << index << ")"; | 253 VLOG(1) << "OnDeviceReady (device_id=" << device_id << ")"; |
| 252 | 254 |
| 253 // Takes care of the case when Stop() is called before OnDeviceReady(). | 255 // Takes care of the case when Stop() is called before OnDeviceReady(). |
| 254 if (!pending_device_ready_) | 256 if (!pending_device_ready_) |
| 255 return; | 257 return; |
| 256 | 258 |
| 257 // -1 means no device has been started. | 259 // If AudioInputDeviceManager returns an empty string, it means no device |
| 258 if (index == -1) { | 260 // is ready for start. |
| 261 if (device_id.empty()) { |
| 259 filter_->RemoveDelegate(stream_id_); | 262 filter_->RemoveDelegate(stream_id_); |
| 260 stream_id_ = 0; | 263 stream_id_ = 0; |
| 261 } else { | 264 } else { |
| 262 Send(new AudioInputHostMsg_CreateStream( | 265 Send(new AudioInputHostMsg_CreateStream( |
| 263 stream_id_, audio_parameters_, true)); | 266 stream_id_, audio_parameters_, true, device_id)); |
| 264 } | 267 } |
| 265 | 268 |
| 266 pending_device_ready_ = false; | 269 pending_device_ready_ = false; |
| 267 // Notify the client that the device has been started. | 270 // Notify the client that the device has been started. |
| 268 if (event_handler_) | 271 if (event_handler_) |
| 269 event_handler_->OnDeviceStarted(index); | 272 event_handler_->OnDeviceStarted(device_id); |
| 270 } | 273 } |
| 271 | 274 |
| 272 void AudioInputDevice::Send(IPC::Message* message) { | 275 void AudioInputDevice::Send(IPC::Message* message) { |
| 273 filter_->Send(message); | 276 filter_->Send(message); |
| 274 } | 277 } |
| 275 | 278 |
| 276 // Our audio thread runs here. We receive captured audio samples on | 279 // Our audio thread runs here. We receive captured audio samples on |
| 277 // this thread. | 280 // this thread. |
| 278 void AudioInputDevice::Run() { | 281 void AudioInputDevice::Run() { |
| 279 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 282 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 bytes_per_sample, | 322 bytes_per_sample, |
| 320 number_of_frames); | 323 number_of_frames); |
| 321 } | 324 } |
| 322 | 325 |
| 323 // Deliver captured data to the client in floating point format | 326 // Deliver captured data to the client in floating point format |
| 324 // and update the audio-delay measurement. | 327 // and update the audio-delay measurement. |
| 325 callback_->Capture(audio_data_, | 328 callback_->Capture(audio_data_, |
| 326 number_of_frames, | 329 number_of_frames, |
| 327 audio_delay_milliseconds_); | 330 audio_delay_milliseconds_); |
| 328 } | 331 } |
| OLD | NEW |