Chromium Code Reviews| 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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "content/child/child_process.h" | 12 #include "content/child/child_process.h" |
| 13 #include "content/renderer/media/audio_device_factory.h" | 13 #include "content/renderer/media/audio_device_factory.h" |
| 14 #include "content/renderer/media/media_stream_audio_processor.h" | 14 #include "content/renderer/media/media_stream_audio_processor.h" |
| 15 #include "content/renderer/media/media_stream_audio_processor_options.h" | 15 #include "content/renderer/media/media_stream_audio_processor_options.h" |
| 16 #include "content/renderer/media/media_stream_audio_source.h" | 16 #include "content/renderer/media/media_stream_audio_source.h" |
| 17 #include "content/renderer/media/media_stream_constraints_util.h" | |
| 17 #include "content/renderer/media/webrtc_audio_device_impl.h" | 18 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 18 #include "content/renderer/media/webrtc_local_audio_track.h" | 19 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 19 #include "content/renderer/media/webrtc_logging.h" | 20 #include "content/renderer/media/webrtc_logging.h" |
| 20 #include "media/audio/sample_rates.h" | 21 #include "media/audio/sample_rates.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 27 // Audio buffer sizes are specified in milliseconds. | |
| 28 const char kAudioLatency[] = "latencyMs"; | |
| 29 const int kMinAudioLatencyMs = 0; | |
| 30 const int kMaxAudioLatencyMs = 10000; | |
| 31 | |
| 26 // Method to check if any of the data in |audio_source| has energy. | 32 // Method to check if any of the data in |audio_source| has energy. |
| 27 bool HasDataEnergy(const media::AudioBus& audio_source) { | 33 bool HasDataEnergy(const media::AudioBus& audio_source) { |
| 28 for (int ch = 0; ch < audio_source.channels(); ++ch) { | 34 for (int ch = 0; ch < audio_source.channels(); ++ch) { |
| 29 const float* channel_ptr = audio_source.channel(ch); | 35 const float* channel_ptr = audio_source.channel(ch); |
| 30 for (int frame = 0; frame < audio_source.frames(); ++frame) { | 36 for (int frame = 0; frame < audio_source.frames(); ++frame) { |
| 31 if (channel_ptr[frame] != 0) | 37 if (channel_ptr[frame] != 0) |
| 32 return true; | 38 return true; |
| 33 } | 39 } |
| 34 } | 40 } |
| 35 | 41 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 | 88 |
| 83 // This can be reentrant so reset |delegate_| before calling out. | 89 // This can be reentrant so reset |delegate_| before calling out. |
| 84 WebRtcLocalAudioTrack* temp = delegate_; | 90 WebRtcLocalAudioTrack* temp = delegate_; |
| 85 delegate_ = NULL; | 91 delegate_ = NULL; |
| 86 temp->Stop(); | 92 temp->Stop(); |
| 87 } | 93 } |
| 88 | 94 |
| 89 // Wrapper which allows to use std::find_if() when adding and removing | 95 // Wrapper which allows to use std::find_if() when adding and removing |
| 90 // sinks to/from the list. | 96 // sinks to/from the list. |
| 91 struct TrackWrapper { | 97 struct TrackWrapper { |
| 92 TrackWrapper(WebRtcLocalAudioTrack* track) : track_(track) {} | 98 explicit TrackWrapper(WebRtcLocalAudioTrack* track) : track_(track) {} |
| 93 bool operator()( | 99 bool operator()( |
| 94 const scoped_refptr<WebRtcAudioCapturer::TrackOwner>& owner) const { | 100 const scoped_refptr<WebRtcAudioCapturer::TrackOwner>& owner) const { |
| 95 return owner->IsEqual(track_); | 101 return owner->IsEqual(track_); |
| 96 } | 102 } |
| 97 WebRtcLocalAudioTrack* track_; | 103 WebRtcLocalAudioTrack* track_; |
| 98 }; | 104 }; |
| 99 | 105 |
| 100 protected: | 106 protected: |
| 101 virtual ~TrackOwner() {} | 107 virtual ~TrackOwner() {} |
| 102 | 108 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 << device_info_.device.input.sample_rate; | 198 << device_info_.device.input.sample_rate; |
| 193 media::AudioSampleRate asr; | 199 media::AudioSampleRate asr; |
| 194 if (media::ToAudioSampleRate(device_info_.device.input.sample_rate, &asr)) { | 200 if (media::ToAudioSampleRate(device_info_.device.input.sample_rate, &asr)) { |
| 195 UMA_HISTOGRAM_ENUMERATION( | 201 UMA_HISTOGRAM_ENUMERATION( |
| 196 "WebRTC.AudioInputSampleRate", asr, media::kAudioSampleRateMax + 1); | 202 "WebRTC.AudioInputSampleRate", asr, media::kAudioSampleRateMax + 1); |
| 197 } else { | 203 } else { |
| 198 UMA_HISTOGRAM_COUNTS("WebRTC.AudioInputSampleRateUnexpected", | 204 UMA_HISTOGRAM_COUNTS("WebRTC.AudioInputSampleRateUnexpected", |
| 199 device_info_.device.input.sample_rate); | 205 device_info_.device.input.sample_rate); |
| 200 } | 206 } |
| 201 | 207 |
| 208 // Initialize the buffer size to zero, which means it wasn't specified. | |
| 209 // If it is out of range, we return it to zero. | |
| 210 int buffer_size_ms = 0; | |
| 211 GetConstraintValueAsInteger(constraints_, kAudioLatency, &buffer_size_ms); | |
| 212 if (buffer_size_ms < kMinAudioLatencyMs || | |
| 213 buffer_size_ms > kMaxAudioLatencyMs) { | |
| 214 DVLOG(1) << "Ignoring out of range buffer size " << buffer_size_ms; | |
| 215 buffer_size_ms = 0; | |
| 216 } | |
| 217 | |
| 218 int buffer_size_samples = | |
| 219 device_info_.device.input.sample_rate * buffer_size_ms / 1000; | |
|
tommi (sloooow) - chröme
2015/05/15 06:55:25
Can we move this calculation into an else clause o
Charlie
2015/05/15 17:19:32
Done.
| |
| 220 DVLOG_IF(1, buffer_size_samples > 0) | |
| 221 << "Custom audio buffer size: " << buffer_size_samples << " samples"; | |
| 222 | |
| 202 // Create and configure the default audio capturing source. | 223 // Create and configure the default audio capturing source. |
| 203 SetCapturerSourceInternal( | 224 SetCapturerSourceInternal( |
| 204 AudioDeviceFactory::NewInputDevice(render_frame_id_), channel_layout, | 225 AudioDeviceFactory::NewInputDevice(render_frame_id_), |
| 205 static_cast<float>(device_info_.device.input.sample_rate)); | 226 channel_layout, |
| 227 device_info_.device.input.sample_rate, | |
| 228 buffer_size_samples); | |
| 206 | 229 |
| 207 // Add the capturer to the WebRtcAudioDeviceImpl since it needs some hardware | 230 // Add the capturer to the WebRtcAudioDeviceImpl since it needs some hardware |
| 208 // information from the capturer. | 231 // information from the capturer. |
| 209 if (audio_device_) | 232 if (audio_device_) |
| 210 audio_device_->AddAudioCapturer(this); | 233 audio_device_->AddAudioCapturer(this); |
| 211 | 234 |
| 212 return true; | 235 return true; |
| 213 } | 236 } |
| 214 | 237 |
| 215 WebRtcAudioCapturer::WebRtcAudioCapturer( | 238 WebRtcAudioCapturer::WebRtcAudioCapturer( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 // we have to call StopSource on the MediaStreamSource. This will call | 303 // we have to call StopSource on the MediaStreamSource. This will call |
| 281 // MediaStreamAudioSource::DoStopSource which in turn call | 304 // MediaStreamAudioSource::DoStopSource which in turn call |
| 282 // WebRtcAudioCapturerer::Stop(); | 305 // WebRtcAudioCapturerer::Stop(); |
| 283 audio_source_->StopSource(); | 306 audio_source_->StopSource(); |
| 284 } | 307 } |
| 285 } | 308 } |
| 286 | 309 |
| 287 void WebRtcAudioCapturer::SetCapturerSourceInternal( | 310 void WebRtcAudioCapturer::SetCapturerSourceInternal( |
| 288 const scoped_refptr<media::AudioCapturerSource>& source, | 311 const scoped_refptr<media::AudioCapturerSource>& source, |
| 289 media::ChannelLayout channel_layout, | 312 media::ChannelLayout channel_layout, |
| 290 float sample_rate) { | 313 int sample_rate, |
| 314 int buffer_size) { | |
| 291 DCHECK(thread_checker_.CalledOnValidThread()); | 315 DCHECK(thread_checker_.CalledOnValidThread()); |
| 292 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," | 316 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," |
| 293 << "sample_rate=" << sample_rate << ")"; | 317 << "sample_rate=" << sample_rate << ")"; |
| 294 scoped_refptr<media::AudioCapturerSource> old_source; | 318 scoped_refptr<media::AudioCapturerSource> old_source; |
| 295 { | 319 { |
| 296 base::AutoLock auto_lock(lock_); | 320 base::AutoLock auto_lock(lock_); |
| 297 if (source_.get() == source.get()) | 321 if (source_.get() == source.get()) |
| 298 return; | 322 return; |
| 299 | 323 |
| 300 source_.swap(old_source); | 324 source_.swap(old_source); |
| 301 source_ = source; | 325 source_ = source; |
| 302 | 326 |
| 303 // Reset the flag to allow starting the new source. | 327 // Reset the flag to allow starting the new source. |
| 304 running_ = false; | 328 running_ = false; |
| 305 } | 329 } |
| 306 | 330 |
| 307 DVLOG(1) << "Switching to a new capture source."; | 331 DVLOG(1) << "Switching to a new capture source."; |
| 308 if (old_source.get()) | 332 if (old_source.get()) |
| 309 old_source->Stop(); | 333 old_source->Stop(); |
| 310 | 334 |
| 335 // If the buffer size is zero, it has not been specified. | |
| 336 // We either default to 10ms, or use the hardware buffer size. | |
| 337 if (buffer_size == 0) | |
| 338 buffer_size = GetBufferSize(sample_rate); | |
| 339 | |
| 311 // Dispatch the new parameters both to the sink(s) and to the new source, | 340 // Dispatch the new parameters both to the sink(s) and to the new source, |
| 312 // also apply the new |constraints|. | 341 // also apply the new |constraints|. |
| 313 // The idea is to get rid of any dependency of the microphone parameters | 342 // The idea is to get rid of any dependency of the microphone parameters |
| 314 // which would normally be used by default. | 343 // which would normally be used by default. |
| 315 // bits_per_sample is always 16 for now. | 344 // bits_per_sample is always 16 for now. |
| 316 int buffer_size = GetBufferSize(sample_rate); | |
| 317 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 345 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 318 channel_layout, sample_rate, | 346 channel_layout, |
| 319 16, buffer_size, | 347 sample_rate, |
| 348 16, | |
| 349 buffer_size, | |
| 320 device_info_.device.input.effects); | 350 device_info_.device.input.effects); |
| 321 | 351 |
| 322 { | 352 { |
| 323 base::AutoLock auto_lock(lock_); | 353 base::AutoLock auto_lock(lock_); |
| 324 // Notify the |audio_processor_| of the new format. | 354 // Notify the |audio_processor_| of the new format. |
| 325 audio_processor_->OnCaptureFormatChanged(params); | 355 audio_processor_->OnCaptureFormatChanged(params); |
| 326 | 356 |
| 327 // Notify all tracks about the new format. | 357 // Notify all tracks about the new format. |
| 328 tracks_.TagAll(); | 358 tracks_.TagAll(); |
| 329 } | 359 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 358 // Do nothing if the current buffer size is the WebRtc native buffer size. | 388 // Do nothing if the current buffer size is the WebRtc native buffer size. |
| 359 if (GetBufferSize(input_params.sample_rate()) == | 389 if (GetBufferSize(input_params.sample_rate()) == |
| 360 input_params.frames_per_buffer()) { | 390 input_params.frames_per_buffer()) { |
| 361 return; | 391 return; |
| 362 } | 392 } |
| 363 | 393 |
| 364 // Create a new audio stream as source which will open the hardware using | 394 // Create a new audio stream as source which will open the hardware using |
| 365 // WebRtc native buffer size. | 395 // WebRtc native buffer size. |
| 366 SetCapturerSourceInternal(AudioDeviceFactory::NewInputDevice(render_frame_id), | 396 SetCapturerSourceInternal(AudioDeviceFactory::NewInputDevice(render_frame_id), |
| 367 input_params.channel_layout(), | 397 input_params.channel_layout(), |
| 368 static_cast<float>(input_params.sample_rate())); | 398 input_params.sample_rate(), |
| 399 0); | |
| 369 } | 400 } |
| 370 | 401 |
| 371 void WebRtcAudioCapturer::Start() { | 402 void WebRtcAudioCapturer::Start() { |
| 372 DCHECK(thread_checker_.CalledOnValidThread()); | 403 DCHECK(thread_checker_.CalledOnValidThread()); |
| 373 DVLOG(1) << "WebRtcAudioCapturer::Start()"; | 404 DVLOG(1) << "WebRtcAudioCapturer::Start()"; |
| 374 base::AutoLock auto_lock(lock_); | 405 base::AutoLock auto_lock(lock_); |
| 375 if (running_ || !source_.get()) | 406 if (running_ || !source_.get()) |
| 376 return; | 407 return; |
| 377 | 408 |
| 378 // Start the data source, i.e., start capturing data from the current source. | 409 // Start the data source, i.e., start capturing data from the current source. |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 return hardware_buffer_size; | 612 return hardware_buffer_size; |
| 582 } | 613 } |
| 583 | 614 |
| 584 return (sample_rate / 100); | 615 return (sample_rate / 100); |
| 585 } | 616 } |
| 586 | 617 |
| 587 void WebRtcAudioCapturer::SetCapturerSource( | 618 void WebRtcAudioCapturer::SetCapturerSource( |
| 588 const scoped_refptr<media::AudioCapturerSource>& source, | 619 const scoped_refptr<media::AudioCapturerSource>& source, |
| 589 media::AudioParameters params) { | 620 media::AudioParameters params) { |
| 590 // Create a new audio stream as source which uses the new source. | 621 // Create a new audio stream as source which uses the new source. |
| 591 SetCapturerSourceInternal(source, params.channel_layout(), | 622 SetCapturerSourceInternal(source, |
| 592 static_cast<float>(params.sample_rate())); | 623 params.channel_layout(), |
| 624 params.sample_rate(), | |
| 625 0); | |
| 593 } | 626 } |
| 594 | 627 |
| 595 } // namespace content | 628 } // namespace content |
| OLD | NEW |