| 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/webrtc_audio_device_impl.h" | 17 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 17 #include "content/renderer/media/webrtc_local_audio_track.h" | 18 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 18 #include "content/renderer/media/webrtc_logging.h" | 19 #include "content/renderer/media/webrtc_logging.h" |
| 19 #include "media/audio/sample_rates.h" | 20 #include "media/audio/sample_rates.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Supported hardware sample rates for input and output sides. | 26 // Supported hardware sample rates for input and output sides. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 WebRtcLocalAudioTrack* delegate_; | 125 WebRtcLocalAudioTrack* delegate_; |
| 125 mutable base::Lock lock_; | 126 mutable base::Lock lock_; |
| 126 | 127 |
| 127 DISALLOW_COPY_AND_ASSIGN(TrackOwner); | 128 DISALLOW_COPY_AND_ASSIGN(TrackOwner); |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 // static | 131 // static |
| 131 scoped_refptr<WebRtcAudioCapturer> WebRtcAudioCapturer::CreateCapturer( | 132 scoped_refptr<WebRtcAudioCapturer> WebRtcAudioCapturer::CreateCapturer( |
| 132 int render_view_id, const StreamDeviceInfo& device_info, | 133 int render_view_id, const StreamDeviceInfo& device_info, |
| 133 const blink::WebMediaConstraints& constraints, | 134 const blink::WebMediaConstraints& constraints, |
| 134 WebRtcAudioDeviceImpl* audio_device) { | 135 WebRtcAudioDeviceImpl* audio_device, |
| 136 MediaStreamAudioSource* audio_source) { |
| 135 scoped_refptr<WebRtcAudioCapturer> capturer = new WebRtcAudioCapturer( | 137 scoped_refptr<WebRtcAudioCapturer> capturer = new WebRtcAudioCapturer( |
| 136 render_view_id, device_info, constraints, audio_device); | 138 render_view_id, device_info, constraints, audio_device, audio_source); |
| 137 if (capturer->Initialize()) | 139 if (capturer->Initialize()) |
| 138 return capturer; | 140 return capturer; |
| 139 | 141 |
| 140 return NULL; | 142 return NULL; |
| 141 } | 143 } |
| 142 | 144 |
| 143 bool WebRtcAudioCapturer::Initialize() { | 145 bool WebRtcAudioCapturer::Initialize() { |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
| 145 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; | 147 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; |
| 146 WebRtcLogMessage(base::StringPrintf( | 148 WebRtcLogMessage(base::StringPrintf( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if (audio_device_) | 211 if (audio_device_) |
| 210 audio_device_->AddAudioCapturer(this); | 212 audio_device_->AddAudioCapturer(this); |
| 211 | 213 |
| 212 return true; | 214 return true; |
| 213 } | 215 } |
| 214 | 216 |
| 215 WebRtcAudioCapturer::WebRtcAudioCapturer( | 217 WebRtcAudioCapturer::WebRtcAudioCapturer( |
| 216 int render_view_id, | 218 int render_view_id, |
| 217 const StreamDeviceInfo& device_info, | 219 const StreamDeviceInfo& device_info, |
| 218 const blink::WebMediaConstraints& constraints, | 220 const blink::WebMediaConstraints& constraints, |
| 219 WebRtcAudioDeviceImpl* audio_device) | 221 WebRtcAudioDeviceImpl* audio_device, |
| 222 MediaStreamAudioSource* audio_source) |
| 220 : constraints_(constraints), | 223 : constraints_(constraints), |
| 221 audio_processor_( | 224 audio_processor_( |
| 222 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( | 225 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( |
| 223 constraints, device_info.device.input.effects, | 226 constraints, device_info.device.input.effects, |
| 224 device_info.device.type, audio_device)), | 227 device_info.device.type, audio_device)), |
| 225 running_(false), | 228 running_(false), |
| 226 render_view_id_(render_view_id), | 229 render_view_id_(render_view_id), |
| 227 device_info_(device_info), | 230 device_info_(device_info), |
| 228 volume_(0), | 231 volume_(0), |
| 229 peer_connection_mode_(false), | 232 peer_connection_mode_(false), |
| 230 key_pressed_(false), | 233 key_pressed_(false), |
| 231 need_audio_processing_(false), | 234 need_audio_processing_(false), |
| 232 audio_device_(audio_device), | 235 audio_device_(audio_device), |
| 236 audio_source_(audio_source), |
| 233 audio_power_monitor_( | 237 audio_power_monitor_( |
| 234 device_info_.device.input.sample_rate, | 238 device_info_.device.input.sample_rate, |
| 235 base::TimeDelta::FromMilliseconds(kPowerMonitorTimeConstantMs)) { | 239 base::TimeDelta::FromMilliseconds(kPowerMonitorTimeConstantMs)) { |
| 236 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; | 240 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; |
| 237 } | 241 } |
| 238 | 242 |
| 239 WebRtcAudioCapturer::~WebRtcAudioCapturer() { | 243 WebRtcAudioCapturer::~WebRtcAudioCapturer() { |
| 240 DCHECK(thread_checker_.CalledOnValidThread()); | 244 DCHECK(thread_checker_.CalledOnValidThread()); |
| 241 DCHECK(tracks_.IsEmpty()); | 245 DCHECK(tracks_.IsEmpty()); |
| 242 DCHECK(!running_); | |
| 243 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; | 246 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; |
| 247 Stop(); |
| 244 } | 248 } |
| 245 | 249 |
| 246 void WebRtcAudioCapturer::AddTrack(WebRtcLocalAudioTrack* track) { | 250 void WebRtcAudioCapturer::AddTrack(WebRtcLocalAudioTrack* track) { |
| 247 DCHECK(track); | 251 DCHECK(track); |
| 248 DVLOG(1) << "WebRtcAudioCapturer::AddTrack()"; | 252 DVLOG(1) << "WebRtcAudioCapturer::AddTrack()"; |
| 249 | 253 |
| 250 { | 254 { |
| 251 base::AutoLock auto_lock(lock_); | 255 base::AutoLock auto_lock(lock_); |
| 252 // Verify that |track| is not already added to the list. | 256 // Verify that |track| is not already added to the list. |
| 253 DCHECK(!tracks_.Contains(TrackOwner::TrackWrapper(track))); | 257 DCHECK(!tracks_.Contains(TrackOwner::TrackWrapper(track))); |
| 254 | 258 |
| 255 // Add with a tag, so we remember to call OnSetFormat() on the new | 259 // Add with a tag, so we remember to call OnSetFormat() on the new |
| 256 // track. | 260 // track. |
| 257 scoped_refptr<TrackOwner> track_owner(new TrackOwner(track)); | 261 scoped_refptr<TrackOwner> track_owner(new TrackOwner(track)); |
| 258 tracks_.AddAndTag(track_owner); | 262 tracks_.AddAndTag(track_owner); |
| 259 } | 263 } |
| 260 | |
| 261 // Start the source if the first audio track is connected to the capturer. | |
| 262 // Start() will do nothing if the capturer has already been started. | |
| 263 Start(); | |
| 264 | |
| 265 } | 264 } |
| 266 | 265 |
| 267 void WebRtcAudioCapturer::RemoveTrack(WebRtcLocalAudioTrack* track) { | 266 void WebRtcAudioCapturer::RemoveTrack(WebRtcLocalAudioTrack* track) { |
| 268 DCHECK(thread_checker_.CalledOnValidThread()); | 267 DCHECK(thread_checker_.CalledOnValidThread()); |
| 269 base::AutoLock auto_lock(lock_); | 268 DVLOG(1) << "WebRtcAudioCapturer::RemoveTrack()"; |
| 269 bool stop_source = false; |
| 270 { |
| 271 base::AutoLock auto_lock(lock_); |
| 270 | 272 |
| 271 scoped_refptr<TrackOwner> removed_item = | 273 scoped_refptr<TrackOwner> removed_item = |
| 272 tracks_.Remove(TrackOwner::TrackWrapper(track)); | 274 tracks_.Remove(TrackOwner::TrackWrapper(track)); |
| 273 | 275 |
| 274 // Clear the delegate to ensure that no more capture callbacks will | 276 // Clear the delegate to ensure that no more capture callbacks will |
| 275 // be sent to this sink. Also avoids a possible crash which can happen | 277 // be sent to this sink. Also avoids a possible crash which can happen |
| 276 // if this method is called while capturing is active. | 278 // if this method is called while capturing is active. |
| 277 if (removed_item.get()) | 279 if (removed_item.get()) { |
| 278 removed_item->Reset(); | 280 removed_item->Reset(); |
| 281 stop_source = tracks_.IsEmpty(); |
| 282 } |
| 283 } |
| 284 if (stop_source) { |
| 285 // Since WebRtcAudioCapturer does not inherit MediaStreamAudioSource, |
| 286 // and instead MediaStreamAudioSource is composed of a WebRtcAudioCapturer, |
| 287 // we have to call StopSource on the MediaStreamSource. This will call |
| 288 // MediaStreamAudioSource::DoStopSource which in turn call |
| 289 // WebRtcAudioCapturerer::Stop(); |
| 290 audio_source_->StopSource(); |
| 291 } |
| 279 } | 292 } |
| 280 | 293 |
| 281 void WebRtcAudioCapturer::SetCapturerSource( | 294 void WebRtcAudioCapturer::SetCapturerSource( |
| 282 const scoped_refptr<media::AudioCapturerSource>& source, | 295 const scoped_refptr<media::AudioCapturerSource>& source, |
| 283 media::ChannelLayout channel_layout, | 296 media::ChannelLayout channel_layout, |
| 284 float sample_rate) { | 297 float sample_rate) { |
| 285 DCHECK(thread_checker_.CalledOnValidThread()); | 298 DCHECK(thread_checker_.CalledOnValidThread()); |
| 286 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," | 299 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," |
| 287 << "sample_rate=" << sample_rate << ")"; | 300 << "sample_rate=" << sample_rate << ")"; |
| 288 scoped_refptr<media::AudioCapturerSource> old_source; | 301 scoped_refptr<media::AudioCapturerSource> old_source; |
| 289 bool restart_source = false; | |
| 290 { | 302 { |
| 291 base::AutoLock auto_lock(lock_); | 303 base::AutoLock auto_lock(lock_); |
| 292 if (source_.get() == source.get()) | 304 if (source_.get() == source.get()) |
| 293 return; | 305 return; |
| 294 | 306 |
| 295 source_.swap(old_source); | 307 source_.swap(old_source); |
| 296 source_ = source; | 308 source_ = source; |
| 297 | 309 |
| 298 // Reset the flag to allow starting the new source. | 310 // Reset the flag to allow starting the new source. |
| 299 restart_source = running_; | |
| 300 running_ = false; | 311 running_ = false; |
| 301 } | 312 } |
| 302 | 313 |
| 303 DVLOG(1) << "Switching to a new capture source."; | 314 DVLOG(1) << "Switching to a new capture source."; |
| 304 if (old_source.get()) | 315 if (old_source.get()) |
| 305 old_source->Stop(); | 316 old_source->Stop(); |
| 306 | 317 |
| 307 // Dispatch the new parameters both to the sink(s) and to the new source, | 318 // Dispatch the new parameters both to the sink(s) and to the new source, |
| 308 // also apply the new |constraints|. | 319 // also apply the new |constraints|. |
| 309 // The idea is to get rid of any dependency of the microphone parameters | 320 // The idea is to get rid of any dependency of the microphone parameters |
| (...skipping 12 matching lines...) Expand all Loading... |
| 322 | 333 |
| 323 need_audio_processing_ = NeedsAudioProcessing( | 334 need_audio_processing_ = NeedsAudioProcessing( |
| 324 constraints_, device_info_.device.input.effects); | 335 constraints_, device_info_.device.input.effects); |
| 325 // Notify all tracks about the new format. | 336 // Notify all tracks about the new format. |
| 326 tracks_.TagAll(); | 337 tracks_.TagAll(); |
| 327 } | 338 } |
| 328 | 339 |
| 329 if (source.get()) | 340 if (source.get()) |
| 330 source->Initialize(params, this, session_id()); | 341 source->Initialize(params, this, session_id()); |
| 331 | 342 |
| 332 if (restart_source) | 343 Start(); |
| 333 Start(); | |
| 334 } | 344 } |
| 335 | 345 |
| 336 void WebRtcAudioCapturer::EnablePeerConnectionMode() { | 346 void WebRtcAudioCapturer::EnablePeerConnectionMode() { |
| 337 DCHECK(thread_checker_.CalledOnValidThread()); | 347 DCHECK(thread_checker_.CalledOnValidThread()); |
| 338 DVLOG(1) << "EnablePeerConnectionMode"; | 348 DVLOG(1) << "EnablePeerConnectionMode"; |
| 339 // Do nothing if the peer connection mode has been enabled. | 349 // Do nothing if the peer connection mode has been enabled. |
| 340 if (peer_connection_mode_) | 350 if (peer_connection_mode_) |
| 341 return; | 351 return; |
| 342 | 352 |
| 343 peer_connection_mode_ = true; | 353 peer_connection_mode_ = true; |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); | 615 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); |
| 606 audio_processor_->StartAecDump(aec_dump_file); | 616 audio_processor_->StartAecDump(aec_dump_file); |
| 607 } | 617 } |
| 608 | 618 |
| 609 void WebRtcAudioCapturer::StopAecDump() { | 619 void WebRtcAudioCapturer::StopAecDump() { |
| 610 DCHECK(thread_checker_.CalledOnValidThread()); | 620 DCHECK(thread_checker_.CalledOnValidThread()); |
| 611 audio_processor_->StopAecDump(); | 621 audio_processor_->StopAecDump(); |
| 612 } | 622 } |
| 613 | 623 |
| 614 } // namespace content | 624 } // namespace content |
| OLD | NEW |