| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/media_stream_audio_source.h" | 5 #include "content/renderer/media/media_stream_audio_source.h" |
| 6 | 6 |
| 7 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 7 #include "content/renderer/render_frame_impl.h" | 8 #include "content/renderer/render_frame_impl.h" |
| 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 8 | 10 |
| 9 namespace content { | 11 namespace content { |
| 10 | 12 |
| 11 MediaStreamAudioSource::MediaStreamAudioSource( | 13 MediaStreamAudioSource::MediaStreamAudioSource( |
| 12 int render_frame_id, | 14 int render_frame_id, |
| 13 const StreamDeviceInfo& device_info, | 15 const StreamDeviceInfo& device_info, |
| 14 const SourceStoppedCallback& stop_callback, | 16 const SourceStoppedCallback& stop_callback, |
| 15 PeerConnectionDependencyFactory* factory) | 17 PeerConnectionDependencyFactory* factory) |
| 16 : render_frame_id_(render_frame_id), factory_(factory) { | 18 : render_frame_id_(render_frame_id), factory_(factory), |
| 19 weak_factory_(this) { |
| 17 SetDeviceInfo(device_info); | 20 SetDeviceInfo(device_info); |
| 18 SetStopCallback(stop_callback); | 21 SetStopCallback(stop_callback); |
| 19 } | 22 } |
| 20 | 23 |
| 21 MediaStreamAudioSource::MediaStreamAudioSource() | 24 MediaStreamAudioSource::MediaStreamAudioSource() |
| 22 : render_frame_id_(-1), factory_(NULL) { | 25 : render_frame_id_(-1), factory_(NULL), weak_factory_(this) { |
| 23 } | 26 } |
| 24 | 27 |
| 25 MediaStreamAudioSource::~MediaStreamAudioSource() {} | 28 MediaStreamAudioSource::~MediaStreamAudioSource() {} |
| 26 | 29 |
| 30 // static |
| 31 MediaStreamAudioSource* MediaStreamAudioSource::From( |
| 32 const blink::WebMediaStreamSource& source) { |
| 33 if (source.isNull() || |
| 34 source.getType() != blink::WebMediaStreamSource::TypeAudio) { |
| 35 return nullptr; |
| 36 } |
| 37 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); |
| 38 } |
| 39 |
| 27 void MediaStreamAudioSource::DoStopSource() { | 40 void MediaStreamAudioSource::DoStopSource() { |
| 28 if (audio_capturer_.get()) | 41 if (audio_capturer_) |
| 29 audio_capturer_->Stop(); | 42 audio_capturer_->Stop(); |
| 43 if (webaudio_capturer_) |
| 44 webaudio_capturer_->Stop(); |
| 30 } | 45 } |
| 31 | 46 |
| 32 void MediaStreamAudioSource::AddTrack( | 47 void MediaStreamAudioSource::AddTrack( |
| 33 const blink::WebMediaStreamTrack& track, | 48 const blink::WebMediaStreamTrack& track, |
| 34 const blink::WebMediaConstraints& constraints, | 49 const blink::WebMediaConstraints& constraints, |
| 35 const ConstraintsCallback& callback) { | 50 const ConstraintsCallback& callback) { |
| 36 // TODO(xians): Properly implement for audio sources. | 51 // TODO(xians): Properly implement for audio sources. |
| 37 if (!local_audio_source_.get()) { | 52 if (!local_audio_source_.get()) { |
| 38 if (!factory_->InitializeMediaStreamAudioSource(render_frame_id_, | 53 if (!factory_->InitializeMediaStreamAudioSource(render_frame_id_, |
| 39 constraints, this)) { | 54 constraints, this)) { |
| 40 // The source failed to start. | 55 // The source failed to start. |
| 41 // UserMediaClientImpl rely on the |stop_callback| to be triggered when | 56 // UserMediaClientImpl rely on the |stop_callback| to be triggered when |
| 42 // the last track is removed from the source. But in this case, the | 57 // the last track is removed from the source. But in this case, the |
| 43 // source is is not even started. So we need to fail both adding the | 58 // source is is not even started. So we need to fail both adding the |
| 44 // track and trigger |stop_callback|. | 59 // track and trigger |stop_callback|. |
| 45 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | 60 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); |
| 46 StopSource(); | 61 StopSource(); |
| 47 return; | 62 return; |
| 48 } | 63 } |
| 49 } | 64 } |
| 50 | 65 |
| 51 factory_->CreateLocalAudioTrack(track); | 66 factory_->CreateLocalAudioTrack(track); |
| 52 callback.Run(this, MEDIA_DEVICE_OK, ""); | 67 callback.Run(this, MEDIA_DEVICE_OK, ""); |
| 53 } | 68 } |
| 54 | 69 |
| 70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
| 71 DCHECK(track); |
| 72 if (audio_capturer_) { |
| 73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used |
| 74 // with WebRtcAudioCapturer sources. |
| 75 // |
| 76 // TODO(miu): That said, this ugly cast won't be necessary after my |
| 77 // soon-upcoming refactoring change. |
| 78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); |
| 79 } |
| 80 if (webaudio_capturer_) { |
| 81 // A separate source is created for each track, so just stop the source. |
| 82 webaudio_capturer_->Stop(); |
| 83 } |
| 84 } |
| 85 |
| 55 } // namespace content | 86 } // namespace content |
| OLD | NEW |