| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 WebRtcLocalAudioTrack* delegate_; | 118 WebRtcLocalAudioTrack* delegate_; |
| 118 mutable base::Lock lock_; | 119 mutable base::Lock lock_; |
| 119 | 120 |
| 120 DISALLOW_COPY_AND_ASSIGN(TrackOwner); | 121 DISALLOW_COPY_AND_ASSIGN(TrackOwner); |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 // static | 124 // static |
| 124 scoped_refptr<WebRtcAudioCapturer> WebRtcAudioCapturer::CreateCapturer( | 125 scoped_refptr<WebRtcAudioCapturer> WebRtcAudioCapturer::CreateCapturer( |
| 125 int render_view_id, const StreamDeviceInfo& device_info, | 126 int render_view_id, const StreamDeviceInfo& device_info, |
| 126 const blink::WebMediaConstraints& constraints, | 127 const blink::WebMediaConstraints& constraints, |
| 127 WebRtcAudioDeviceImpl* audio_device) { | 128 WebRtcAudioDeviceImpl* audio_device, |
| 129 MediaStreamAudioSource* audio_source) { |
| 128 scoped_refptr<WebRtcAudioCapturer> capturer = new WebRtcAudioCapturer( | 130 scoped_refptr<WebRtcAudioCapturer> capturer = new WebRtcAudioCapturer( |
| 129 render_view_id, device_info, constraints, audio_device); | 131 render_view_id, device_info, constraints, audio_device, audio_source); |
| 130 if (capturer->Initialize()) | 132 if (capturer->Initialize()) |
| 131 return capturer; | 133 return capturer; |
| 132 | 134 |
| 133 return NULL; | 135 return NULL; |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool WebRtcAudioCapturer::Initialize() { | 138 bool WebRtcAudioCapturer::Initialize() { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; | 140 DVLOG(1) << "WebRtcAudioCapturer::Initialize()"; |
| 139 WebRtcLogMessage(base::StringPrintf( | 141 WebRtcLogMessage(base::StringPrintf( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 if (audio_device_) | 204 if (audio_device_) |
| 203 audio_device_->AddAudioCapturer(this); | 205 audio_device_->AddAudioCapturer(this); |
| 204 | 206 |
| 205 return true; | 207 return true; |
| 206 } | 208 } |
| 207 | 209 |
| 208 WebRtcAudioCapturer::WebRtcAudioCapturer( | 210 WebRtcAudioCapturer::WebRtcAudioCapturer( |
| 209 int render_view_id, | 211 int render_view_id, |
| 210 const StreamDeviceInfo& device_info, | 212 const StreamDeviceInfo& device_info, |
| 211 const blink::WebMediaConstraints& constraints, | 213 const blink::WebMediaConstraints& constraints, |
| 212 WebRtcAudioDeviceImpl* audio_device) | 214 WebRtcAudioDeviceImpl* audio_device, |
| 215 MediaStreamAudioSource* audio_source) |
| 213 : constraints_(constraints), | 216 : constraints_(constraints), |
| 214 audio_processor_( | 217 audio_processor_( |
| 215 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( | 218 new talk_base::RefCountedObject<MediaStreamAudioProcessor>( |
| 216 constraints, device_info.device.input.effects, | 219 constraints, device_info.device.input.effects, |
| 217 device_info.device.type, audio_device)), | 220 device_info.device.type, audio_device)), |
| 218 running_(false), | 221 running_(false), |
| 219 render_view_id_(render_view_id), | 222 render_view_id_(render_view_id), |
| 220 device_info_(device_info), | 223 device_info_(device_info), |
| 221 volume_(0), | 224 volume_(0), |
| 222 peer_connection_mode_(false), | 225 peer_connection_mode_(false), |
| 223 key_pressed_(false), | 226 key_pressed_(false), |
| 224 need_audio_processing_(false), | 227 need_audio_processing_(false), |
| 225 audio_device_(audio_device) { | 228 audio_device_(audio_device), |
| 229 audio_source_(audio_source) { |
| 226 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; | 230 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; |
| 227 } | 231 } |
| 228 | 232 |
| 229 WebRtcAudioCapturer::~WebRtcAudioCapturer() { | 233 WebRtcAudioCapturer::~WebRtcAudioCapturer() { |
| 230 DCHECK(thread_checker_.CalledOnValidThread()); | 234 DCHECK(thread_checker_.CalledOnValidThread()); |
| 231 DCHECK(tracks_.IsEmpty()); | 235 DCHECK(tracks_.IsEmpty()); |
| 232 DCHECK(!running_); | |
| 233 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; | 236 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; |
| 237 Stop(); |
| 234 } | 238 } |
| 235 | 239 |
| 236 void WebRtcAudioCapturer::AddTrack(WebRtcLocalAudioTrack* track) { | 240 void WebRtcAudioCapturer::AddTrack(WebRtcLocalAudioTrack* track) { |
| 237 DCHECK(track); | 241 DCHECK(track); |
| 238 DVLOG(1) << "WebRtcAudioCapturer::AddTrack()"; | 242 DVLOG(1) << "WebRtcAudioCapturer::AddTrack()"; |
| 239 | 243 |
| 240 { | 244 { |
| 241 base::AutoLock auto_lock(lock_); | 245 base::AutoLock auto_lock(lock_); |
| 242 // Verify that |track| is not already added to the list. | 246 // Verify that |track| is not already added to the list. |
| 243 DCHECK(!tracks_.Contains(TrackOwner::TrackWrapper(track))); | 247 DCHECK(!tracks_.Contains(TrackOwner::TrackWrapper(track))); |
| 244 | 248 |
| 245 // Add with a tag, so we remember to call OnSetFormat() on the new | 249 // Add with a tag, so we remember to call OnSetFormat() on the new |
| 246 // track. | 250 // track. |
| 247 scoped_refptr<TrackOwner> track_owner(new TrackOwner(track)); | 251 scoped_refptr<TrackOwner> track_owner(new TrackOwner(track)); |
| 248 tracks_.AddAndTag(track_owner); | 252 tracks_.AddAndTag(track_owner); |
| 249 } | 253 } |
| 250 | |
| 251 // Start the source if the first audio track is connected to the capturer. | |
| 252 // Start() will do nothing if the capturer has already been started. | |
| 253 Start(); | |
| 254 | |
| 255 } | 254 } |
| 256 | 255 |
| 257 void WebRtcAudioCapturer::RemoveTrack(WebRtcLocalAudioTrack* track) { | 256 void WebRtcAudioCapturer::RemoveTrack(WebRtcLocalAudioTrack* track) { |
| 258 DCHECK(thread_checker_.CalledOnValidThread()); | 257 DCHECK(thread_checker_.CalledOnValidThread()); |
| 259 base::AutoLock auto_lock(lock_); | 258 DVLOG(1) << "WebRtcAudioCapturer::RemoveTrack()"; |
| 259 bool stop_source = false; |
| 260 { |
| 261 base::AutoLock auto_lock(lock_); |
| 260 | 262 |
| 261 scoped_refptr<TrackOwner> removed_item = | 263 scoped_refptr<TrackOwner> removed_item = |
| 262 tracks_.Remove(TrackOwner::TrackWrapper(track)); | 264 tracks_.Remove(TrackOwner::TrackWrapper(track)); |
| 263 | 265 |
| 264 // Clear the delegate to ensure that no more capture callbacks will | 266 // Clear the delegate to ensure that no more capture callbacks will |
| 265 // be sent to this sink. Also avoids a possible crash which can happen | 267 // be sent to this sink. Also avoids a possible crash which can happen |
| 266 // if this method is called while capturing is active. | 268 // if this method is called while capturing is active. |
| 267 if (removed_item.get()) | 269 if (removed_item.get()) { |
| 268 removed_item->Reset(); | 270 removed_item->Reset(); |
| 271 stop_source = tracks_.IsEmpty(); |
| 272 } |
| 273 } |
| 274 if (stop_source) { |
| 275 // Since WebRtcAudioCapturer does not inherit MediaStreamAudioSource, |
| 276 // and instead MediaStreamAudioSource is composed of a WebRtcAudioCapturer, |
| 277 // we have to call StopSource on the MediaStreamSource. This will call |
| 278 // MediaStreamAudioSource::DoStopSource which in turn call |
| 279 // WebRtcAudioCapturerer::Stop(); |
| 280 audio_source_->StopSource(); |
| 281 } |
| 269 } | 282 } |
| 270 | 283 |
| 271 void WebRtcAudioCapturer::SetCapturerSource( | 284 void WebRtcAudioCapturer::SetCapturerSource( |
| 272 const scoped_refptr<media::AudioCapturerSource>& source, | 285 const scoped_refptr<media::AudioCapturerSource>& source, |
| 273 media::ChannelLayout channel_layout, | 286 media::ChannelLayout channel_layout, |
| 274 float sample_rate) { | 287 float sample_rate) { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 288 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," | 289 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," |
| 277 << "sample_rate=" << sample_rate << ")"; | 290 << "sample_rate=" << sample_rate << ")"; |
| 278 scoped_refptr<media::AudioCapturerSource> old_source; | 291 scoped_refptr<media::AudioCapturerSource> old_source; |
| 279 bool restart_source = false; | |
| 280 { | 292 { |
| 281 base::AutoLock auto_lock(lock_); | 293 base::AutoLock auto_lock(lock_); |
| 282 if (source_.get() == source.get()) | 294 if (source_.get() == source.get()) |
| 283 return; | 295 return; |
| 284 | 296 |
| 285 source_.swap(old_source); | 297 source_.swap(old_source); |
| 286 source_ = source; | 298 source_ = source; |
| 287 | 299 |
| 288 // Reset the flag to allow starting the new source. | 300 // Reset the flag to allow starting the new source. |
| 289 restart_source = running_; | |
| 290 running_ = false; | 301 running_ = false; |
| 291 } | 302 } |
| 292 | 303 |
| 293 DVLOG(1) << "Switching to a new capture source."; | 304 DVLOG(1) << "Switching to a new capture source."; |
| 294 if (old_source.get()) | 305 if (old_source.get()) |
| 295 old_source->Stop(); | 306 old_source->Stop(); |
| 296 | 307 |
| 297 // Dispatch the new parameters both to the sink(s) and to the new source, | 308 // Dispatch the new parameters both to the sink(s) and to the new source, |
| 298 // also apply the new |constraints|. | 309 // also apply the new |constraints|. |
| 299 // The idea is to get rid of any dependency of the microphone parameters | 310 // The idea is to get rid of any dependency of the microphone parameters |
| (...skipping 12 matching lines...) Expand all Loading... |
| 312 | 323 |
| 313 need_audio_processing_ = NeedsAudioProcessing( | 324 need_audio_processing_ = NeedsAudioProcessing( |
| 314 constraints_, device_info_.device.input.effects); | 325 constraints_, device_info_.device.input.effects); |
| 315 // Notify all tracks about the new format. | 326 // Notify all tracks about the new format. |
| 316 tracks_.TagAll(); | 327 tracks_.TagAll(); |
| 317 } | 328 } |
| 318 | 329 |
| 319 if (source.get()) | 330 if (source.get()) |
| 320 source->Initialize(params, this, session_id()); | 331 source->Initialize(params, this, session_id()); |
| 321 | 332 |
| 322 if (restart_source) | 333 Start(); |
| 323 Start(); | |
| 324 } | 334 } |
| 325 | 335 |
| 326 void WebRtcAudioCapturer::EnablePeerConnectionMode() { | 336 void WebRtcAudioCapturer::EnablePeerConnectionMode() { |
| 327 DCHECK(thread_checker_.CalledOnValidThread()); | 337 DCHECK(thread_checker_.CalledOnValidThread()); |
| 328 DVLOG(1) << "EnablePeerConnectionMode"; | 338 DVLOG(1) << "EnablePeerConnectionMode"; |
| 329 // Do nothing if the peer connection mode has been enabled. | 339 // Do nothing if the peer connection mode has been enabled. |
| 330 if (peer_connection_mode_) | 340 if (peer_connection_mode_) |
| 331 return; | 341 return; |
| 332 | 342 |
| 333 peer_connection_mode_ = true; | 343 peer_connection_mode_ = true; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); | 591 DCHECK_NE(aec_dump_file, base::kInvalidPlatformFileValue); |
| 582 audio_processor_->StartAecDump(aec_dump_file); | 592 audio_processor_->StartAecDump(aec_dump_file); |
| 583 } | 593 } |
| 584 | 594 |
| 585 void WebRtcAudioCapturer::StopAecDump() { | 595 void WebRtcAudioCapturer::StopAecDump() { |
| 586 DCHECK(thread_checker_.CalledOnValidThread()); | 596 DCHECK(thread_checker_.CalledOnValidThread()); |
| 587 audio_processor_->StopAecDump(); | 597 audio_processor_->StopAecDump(); |
| 588 } | 598 } |
| 589 | 599 |
| 590 } // namespace content | 600 } // namespace content |
| OLD | NEW |