| 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/webrtc_audio_capturer.h" | 5 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 198 } |
| 199 | 199 |
| 200 // Tell all sinks which format we use. | 200 // Tell all sinks which format we use. |
| 201 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) | 201 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) |
| 202 (*it)->SetCaptureFormat(new_buffer->params()); | 202 (*it)->SetCaptureFormat(new_buffer->params()); |
| 203 | 203 |
| 204 return true; | 204 return true; |
| 205 } | 205 } |
| 206 | 206 |
| 207 bool WebRtcAudioCapturer::Initialize(media::ChannelLayout channel_layout, | 207 bool WebRtcAudioCapturer::Initialize(media::ChannelLayout channel_layout, |
| 208 int sample_rate) { | 208 int sample_rate, |
| 209 int session_id) { |
| 209 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 210 DCHECK(!sinks_.empty()); | 211 DCHECK(!sinks_.empty()); |
| 211 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; | 212 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; |
| 212 | 213 |
| 213 DVLOG(1) << "Audio input hardware channel layout: " << channel_layout; | 214 DVLOG(1) << "Audio input hardware channel layout: " << channel_layout; |
| 214 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", | 215 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", |
| 215 channel_layout, media::CHANNEL_LAYOUT_MAX); | 216 channel_layout, media::CHANNEL_LAYOUT_MAX); |
| 216 | 217 |
| 218 session_id_ = session_id; |
| 219 |
| 217 // Verify that the reported input channel configuration is supported. | 220 // Verify that the reported input channel configuration is supported. |
| 218 if (channel_layout != media::CHANNEL_LAYOUT_MONO && | 221 if (channel_layout != media::CHANNEL_LAYOUT_MONO && |
| 219 channel_layout != media::CHANNEL_LAYOUT_STEREO) { | 222 channel_layout != media::CHANNEL_LAYOUT_STEREO) { |
| 220 DLOG(ERROR) << channel_layout | 223 DLOG(ERROR) << channel_layout |
| 221 << " is not a supported input channel configuration."; | 224 << " is not a supported input channel configuration."; |
| 222 return false; | 225 return false; |
| 223 } | 226 } |
| 224 | 227 |
| 225 DVLOG(1) << "Audio input hardware sample rate: " << sample_rate; | 228 DVLOG(1) << "Audio input hardware sample rate: " << sample_rate; |
| 226 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputSampleRate", | 229 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputSampleRate", |
| 227 sample_rate, media::kUnexpectedAudioSampleRate); | 230 sample_rate, media::kUnexpectedAudioSampleRate); |
| 228 | 231 |
| 229 // Verify that the reported input hardware sample rate is supported | 232 // Verify that the reported input hardware sample rate is supported |
| 230 // on the current platform. | 233 // on the current platform. |
| 231 if (std::find(&kValidInputRates[0], | 234 if (std::find(&kValidInputRates[0], |
| 232 &kValidInputRates[0] + arraysize(kValidInputRates), | 235 &kValidInputRates[0] + arraysize(kValidInputRates), |
| 233 sample_rate) == | 236 sample_rate) == |
| 234 &kValidInputRates[arraysize(kValidInputRates)]) { | 237 &kValidInputRates[arraysize(kValidInputRates)]) { |
| 235 DLOG(ERROR) << sample_rate << " is not a supported input rate."; | 238 DLOG(ERROR) << sample_rate << " is not a supported input rate."; |
| 236 return false; | 239 return false; |
| 237 } | 240 } |
| 238 | 241 |
| 239 if (!Reconfigure(sample_rate, channel_layout)) | 242 if (!Reconfigure(sample_rate, channel_layout)) |
| 240 return false; | 243 return false; |
| 241 | 244 |
| 242 // Create and configure the default audio capturing source. The |source_| | 245 // Create and configure the default audio capturing source. The |source_| |
| 243 // will be overwritten if an external client later calls SetCapturerSource() | 246 // will be overwritten if an external client later calls SetCapturerSource() |
| 244 // providing an alternaive media::AudioCapturerSource. | 247 // providing an alternative media::AudioCapturerSource. |
| 245 SetCapturerSource(AudioDeviceFactory::NewInputDevice(), | 248 SetCapturerSource(AudioDeviceFactory::NewInputDevice(), |
| 246 channel_layout, | 249 channel_layout, |
| 247 static_cast<float>(sample_rate)); | 250 static_cast<float>(sample_rate)); |
| 248 | 251 |
| 249 return true; | 252 return true; |
| 250 } | 253 } |
| 251 | 254 |
| 252 WebRtcAudioCapturer::WebRtcAudioCapturer() | 255 WebRtcAudioCapturer::WebRtcAudioCapturer() |
| 253 : source_(NULL), | 256 : source_(NULL), |
| 254 running_(false), | 257 running_(false), |
| 255 agc_is_enabled_(false) { | 258 agc_is_enabled_(false), |
| 259 session_id_(0) { |
| 256 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; | 260 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; |
| 257 } | 261 } |
| 258 | 262 |
| 259 WebRtcAudioCapturer::~WebRtcAudioCapturer() { | 263 WebRtcAudioCapturer::~WebRtcAudioCapturer() { |
| 260 DCHECK(thread_checker_.CalledOnValidThread()); | 264 DCHECK(thread_checker_.CalledOnValidThread()); |
| 261 DCHECK(sinks_.empty()); | 265 DCHECK(sinks_.empty()); |
| 262 DCHECK(!running_); | 266 DCHECK(!running_); |
| 263 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; | 267 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; |
| 264 } | 268 } |
| 265 | 269 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 292 (*it)->Reset(); | 296 (*it)->Reset(); |
| 293 sinks_.erase(it); | 297 sinks_.erase(it); |
| 294 } | 298 } |
| 295 } | 299 } |
| 296 | 300 |
| 297 void WebRtcAudioCapturer::SetCapturerSource( | 301 void WebRtcAudioCapturer::SetCapturerSource( |
| 298 const scoped_refptr<media::AudioCapturerSource>& source, | 302 const scoped_refptr<media::AudioCapturerSource>& source, |
| 299 media::ChannelLayout channel_layout, | 303 media::ChannelLayout channel_layout, |
| 300 float sample_rate) { | 304 float sample_rate) { |
| 301 DCHECK(thread_checker_.CalledOnValidThread()); | 305 DCHECK(thread_checker_.CalledOnValidThread()); |
| 306 CHECK_GT(session_id_, 0); |
| 302 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," | 307 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," |
| 303 << "sample_rate=" << sample_rate << ")"; | 308 << "sample_rate=" << sample_rate << ")"; |
| 304 scoped_refptr<media::AudioCapturerSource> old_source; | 309 scoped_refptr<media::AudioCapturerSource> old_source; |
| 305 scoped_refptr<ConfiguredBuffer> current_buffer; | 310 scoped_refptr<ConfiguredBuffer> current_buffer; |
| 306 { | 311 { |
| 307 base::AutoLock auto_lock(lock_); | 312 base::AutoLock auto_lock(lock_); |
| 308 if (source_ == source) | 313 if (source_ == source) |
| 309 return; | 314 return; |
| 310 | 315 |
| 311 source_.swap(old_source); | 316 source_.swap(old_source); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 331 return; | 336 return; |
| 332 } else { | 337 } else { |
| 333 // The buffer has been reconfigured. Update |current_buffer|. | 338 // The buffer has been reconfigured. Update |current_buffer|. |
| 334 base::AutoLock auto_lock(lock_); | 339 base::AutoLock auto_lock(lock_); |
| 335 current_buffer = buffer_; | 340 current_buffer = buffer_; |
| 336 } | 341 } |
| 337 } | 342 } |
| 338 | 343 |
| 339 if (source) { | 344 if (source) { |
| 340 // Make sure to grab the new parameters in case they were reconfigured. | 345 // Make sure to grab the new parameters in case they were reconfigured. |
| 341 source->Initialize(current_buffer->params(), this, this); | 346 source->Initialize(current_buffer->params(), this, session_id_); |
| 342 } | 347 } |
| 343 } | 348 } |
| 344 | 349 |
| 345 void WebRtcAudioCapturer::Start() { | 350 void WebRtcAudioCapturer::Start() { |
| 346 DVLOG(1) << "WebRtcAudioCapturer::Start()"; | 351 DVLOG(1) << "WebRtcAudioCapturer::Start()"; |
| 347 base::AutoLock auto_lock(lock_); | 352 base::AutoLock auto_lock(lock_); |
| 348 if (running_) | 353 if (running_) |
| 349 return; | 354 return; |
| 350 | 355 |
| 351 // Start the data source, i.e., start capturing data from the current source. | 356 // Start the data source, i.e., start capturing data from the current source. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 375 source->Stop(); | 380 source->Stop(); |
| 376 } | 381 } |
| 377 | 382 |
| 378 void WebRtcAudioCapturer::SetVolume(double volume) { | 383 void WebRtcAudioCapturer::SetVolume(double volume) { |
| 379 DVLOG(1) << "WebRtcAudioCapturer::SetVolume()"; | 384 DVLOG(1) << "WebRtcAudioCapturer::SetVolume()"; |
| 380 base::AutoLock auto_lock(lock_); | 385 base::AutoLock auto_lock(lock_); |
| 381 if (source_) | 386 if (source_) |
| 382 source_->SetVolume(volume); | 387 source_->SetVolume(volume); |
| 383 } | 388 } |
| 384 | 389 |
| 385 void WebRtcAudioCapturer::SetDevice(int session_id) { | |
| 386 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 387 DVLOG(1) << "WebRtcAudioCapturer::SetDevice(" << session_id << ")"; | |
| 388 base::AutoLock auto_lock(lock_); | |
| 389 if (source_) | |
| 390 source_->SetDevice(session_id); | |
| 391 } | |
| 392 | |
| 393 void WebRtcAudioCapturer::SetAutomaticGainControl(bool enable) { | 390 void WebRtcAudioCapturer::SetAutomaticGainControl(bool enable) { |
| 394 base::AutoLock auto_lock(lock_); | 391 base::AutoLock auto_lock(lock_); |
| 395 // Store the setting since SetAutomaticGainControl() can be called before | 392 // Store the setting since SetAutomaticGainControl() can be called before |
| 396 // Initialize(), in this case stored setting will be applied in Start(). | 393 // Initialize(), in this case stored setting will be applied in Start(). |
| 397 agc_is_enabled_ = enable; | 394 agc_is_enabled_ = enable; |
| 398 | 395 |
| 399 if (source_) | 396 if (source_) |
| 400 source_->SetAutomaticGainControl(enable); | 397 source_->SetAutomaticGainControl(enable); |
| 401 } | 398 } |
| 402 | 399 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 (*it)->CaptureData(buffer_ref_while_calling->buffer(), | 432 (*it)->CaptureData(buffer_ref_while_calling->buffer(), |
| 436 audio_source->channels(), audio_source->frames(), | 433 audio_source->channels(), audio_source->frames(), |
| 437 audio_delay_milliseconds, volume); | 434 audio_delay_milliseconds, volume); |
| 438 } | 435 } |
| 439 } | 436 } |
| 440 | 437 |
| 441 void WebRtcAudioCapturer::OnCaptureError() { | 438 void WebRtcAudioCapturer::OnCaptureError() { |
| 442 NOTIMPLEMENTED(); | 439 NOTIMPLEMENTED(); |
| 443 } | 440 } |
| 444 | 441 |
| 445 void WebRtcAudioCapturer::OnDeviceStarted(const std::string& device_id) { | |
| 446 device_id_ = device_id; | |
| 447 } | |
| 448 | |
| 449 void WebRtcAudioCapturer::OnDeviceStopped() { | |
| 450 NOTIMPLEMENTED(); | |
| 451 } | |
| 452 | |
| 453 media::AudioParameters WebRtcAudioCapturer::audio_parameters() const { | 442 media::AudioParameters WebRtcAudioCapturer::audio_parameters() const { |
| 454 base::AutoLock auto_lock(lock_); | 443 base::AutoLock auto_lock(lock_); |
| 455 return buffer_->params(); | 444 return buffer_->params(); |
| 456 } | 445 } |
| 457 | 446 |
| 458 } // namespace content | 447 } // namespace content |
| OLD | NEW |