Chromium Code Reviews| 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 <algorithm> |
| 8 #include "content/renderer/render_frame_impl.h" | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "content/renderer/media/media_stream_audio_track.h" | |
| 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 11 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 12 #include "third_party/WebKit/public/platform/WebString.h" | |
| 10 | 13 |
| 11 namespace content { | 14 namespace content { |
| 12 | 15 |
| 13 MediaStreamAudioSource::MediaStreamAudioSource( | 16 MediaStreamAudioSource::MediaStreamAudioSource(bool is_local_source) |
| 14 int render_frame_id, | 17 : is_local_source_(is_local_source), |
|
perkj_chrome
2016/04/08 14:05:41
remove?
miu
2016/04/19 00:40:22
Looked into this. To make a long-story short, thi
perkj_chrome
2016/04/20 13:34:53
sure
| |
| 15 const StreamDeviceInfo& device_info, | 18 is_stopped_(false), |
| 16 const SourceStoppedCallback& stop_callback, | |
| 17 PeerConnectionDependencyFactory* factory) | |
| 18 : render_frame_id_(render_frame_id), factory_(factory), | |
| 19 weak_factory_(this) { | 19 weak_factory_(this) { |
| 20 SetDeviceInfo(device_info); | 20 DVLOG(1) << "MediaStreamAudioSource::MediaStreamAudioSource(is a " |
| 21 SetStopCallback(stop_callback); | 21 << (is_local_source_ ? "local" : "remote") << " source)"; |
| 22 } | 22 } |
| 23 | 23 |
| 24 MediaStreamAudioSource::MediaStreamAudioSource() | 24 MediaStreamAudioSource::~MediaStreamAudioSource() { |
| 25 : render_frame_id_(-1), factory_(NULL), weak_factory_(this) { | 25 DCHECK(thread_checker_.CalledOnValidThread()); |
| 26 DVLOG(1) << "MediaStreamAudioSource::~MediaStreamAudioSource()"; | |
| 26 } | 27 } |
| 27 | 28 |
| 28 MediaStreamAudioSource::~MediaStreamAudioSource() {} | |
| 29 | |
| 30 // static | 29 // static |
| 31 MediaStreamAudioSource* MediaStreamAudioSource::From( | 30 MediaStreamAudioSource* MediaStreamAudioSource::From( |
| 32 const blink::WebMediaStreamSource& source) { | 31 const blink::WebMediaStreamSource& source) { |
| 33 if (source.isNull() || | 32 if (source.isNull() || |
| 34 source.getType() != blink::WebMediaStreamSource::TypeAudio) { | 33 source.getType() != blink::WebMediaStreamSource::TypeAudio) { |
| 35 return nullptr; | 34 return nullptr; |
| 36 } | 35 } |
| 37 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); | 36 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void MediaStreamAudioSource::DoStopSource() { | 39 bool MediaStreamAudioSource::ConnectToTrack( |
| 41 if (audio_capturer_) | 40 const blink::WebMediaStreamTrack& track) { |
| 42 audio_capturer_->Stop(); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 if (webaudio_capturer_) | 42 DCHECK(!track.isNull()); |
| 44 webaudio_capturer_->Stop(); | 43 |
| 44 // Sanity-check that there is not already a MediaStreamAudioTrack instance | |
| 45 // associated with |track|. | |
| 46 if (MediaStreamAudioTrack::From(track)) { | |
| 47 LOG(DFATAL) | |
| 48 << "Attempting to connect another source to a WebMediaStreamTrack."; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 if (is_stopped_) | |
|
perkj_chrome
2016/04/08 14:05:41
Is it necessary to check for if the source is stop
miu
2016/04/19 00:40:22
Good point. Done. I also just moved the code fro
| |
| 53 return false; | |
| 54 if (!EnsureSourceIsStarted()) | |
| 55 return false; | |
| 56 ConnectStartedSourceToTrack(track); | |
| 57 return true; | |
| 45 } | 58 } |
| 46 | 59 |
| 47 void MediaStreamAudioSource::AddTrack( | 60 media::AudioParameters MediaStreamAudioSource::GetAudioParameters() const { |
| 48 const blink::WebMediaStreamTrack& track, | 61 base::AutoLock auto_lock(lock_); |
| 49 const blink::WebMediaConstraints& constraints, | 62 return params_; |
| 50 const ConstraintsCallback& callback) { | 63 } |
| 51 // TODO(xians): Properly implement for audio sources. | 64 |
| 52 if (!local_audio_source_.get()) { | 65 void* MediaStreamAudioSource::GetClassIdentifier() const { |
| 53 if (!factory_->InitializeMediaStreamAudioSource(render_frame_id_, | 66 return nullptr; |
| 54 constraints, this)) { | 67 } |
| 55 // The source failed to start. | 68 |
| 56 // UserMediaClientImpl rely on the |stop_callback| to be triggered when | 69 scoped_ptr<MediaStreamAudioTrack> |
| 57 // the last track is removed from the source. But in this case, the | 70 MediaStreamAudioSource::CreateMediaStreamAudioTrack(const std::string& id) { |
| 58 // source is is not even started. So we need to fail both adding the | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 // track and trigger |stop_callback|. | 72 return make_scoped_ptr(new MediaStreamAudioTrack(is_local_source())); |
| 60 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | 73 } |
| 61 StopSource(); | 74 |
| 62 return; | 75 bool MediaStreamAudioSource::EnsureSourceIsStarted() { |
| 63 } | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 DVLOG(1) << "MediaStreamAudioSource::EnsureSourceIsStarted()"; | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 void MediaStreamAudioSource::EnsureSourceIsStopped() { | |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 83 DVLOG(1) << "MediaStreamAudioSource::EnsureSourceIsStopped()"; | |
| 84 } | |
| 85 | |
| 86 void MediaStreamAudioSource::SetFormat(const media::AudioParameters& params) { | |
| 87 // Note: May be called on any thread. | |
| 88 DCHECK(params.IsValid()); | |
| 89 base::AutoLock auto_lock(lock_); | |
| 90 DVLOG(1) << "MediaStreamAudioSource::SetFormat(" | |
| 91 << params.AsHumanReadableString() << "), was previously set to {" | |
| 92 << params_.AsHumanReadableString() << "}."; | |
| 93 if (params_.Equals(params)) | |
| 94 return; | |
| 95 params_ = params; | |
| 96 for (MediaStreamAudioTrack* track : tracks_) | |
| 97 track->SetFormat(params); | |
| 98 } | |
| 99 | |
| 100 void MediaStreamAudioSource::DeliverDataToTracks( | |
| 101 const media::AudioBus& audio_bus, | |
| 102 base::TimeTicks reference_time) { | |
| 103 // Note: May be called on any thread. | |
| 104 base::AutoLock auto_lock(lock_); | |
| 105 DCHECK(params_.IsValid()); | |
| 106 for (MediaStreamAudioTrack* track : tracks_) | |
| 107 track->DeliverDataToSinks(audio_bus, reference_time); | |
| 108 } | |
| 109 | |
| 110 void MediaStreamAudioSource::DoStopSource() { | |
|
perkj_chrome
2016/04/08 14:05:41
why this extra indirection? Just for naming?
miu
2016/04/19 00:40:22
Are you asking why I have this call EnsureSourceIs
perkj_chrome
2016/04/20 13:34:53
Acknowledged.
| |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 EnsureSourceIsStopped(); | |
| 113 is_stopped_ = true; | |
| 114 } | |
| 115 | |
| 116 void MediaStreamAudioSource::ConnectStartedSourceToTrack( | |
| 117 const blink::WebMediaStreamTrack& blink_track) { | |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 119 DCHECK(!is_stopped_); | |
| 120 | |
| 121 // Create a MediaStreamAudioTrack to deliver audio data directly from the | |
| 122 // calls to Capture() to all its managed sinks. Pass ownership of it to the | |
| 123 // WebMediaStreamTrack. | |
| 124 scoped_ptr<MediaStreamAudioTrack> track = | |
| 125 CreateMediaStreamAudioTrack(blink_track.id().utf8()); | |
| 126 track->Start(base::Bind(&MediaStreamAudioSource::StopAudioDeliveryTo, | |
| 127 weak_factory_.GetWeakPtr(), track.get())); | |
| 128 track->SetEnabled(blink_track.isEnabled()); | |
| 129 { | |
| 130 base::AutoLock auto_lock(lock_); | |
| 131 if (params_.IsValid()) | |
| 132 track->SetFormat(params_); | |
|
o1ka
2016/04/01 15:11:41
I would probably use a separate lock for |params_|
miu
2016/04/19 00:40:22
Done. BTW--I realized MediaStreamAudioSource and
o1ka
2016/04/21 18:51:22
Really like it!
| |
| 133 tracks_.push_back(track.get()); | |
| 64 } | 134 } |
| 65 | 135 blink::WebMediaStreamTrack mutable_blink_track = blink_track; |
| 66 factory_->CreateLocalAudioTrack(track); | 136 mutable_blink_track.setExtraData(track.release()); |
| 67 callback.Run(this, MEDIA_DEVICE_OK, ""); | |
| 68 } | 137 } |
| 69 | 138 |
| 70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { | 139 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
| 71 DCHECK(track); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 if (audio_capturer_) { | 141 |
| 73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used | 142 // Remove |track| from the list of tracks, which will immediatly halt all |
| 74 // with WebRtcAudioCapturer sources. | 143 // further audio data delivery. |
| 75 // | 144 bool did_remove_last_track = false; |
| 76 // TODO(miu): That said, this ugly cast won't be necessary after my | 145 { |
| 77 // soon-upcoming refactoring change. | 146 base::AutoLock auto_lock(lock_); |
| 78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); | 147 const bool had_tracks = !tracks_.empty(); |
| 148 const auto it = std::find(tracks_.begin(), tracks_.end(), track); | |
| 149 if (it != tracks_.end()) | |
| 150 tracks_.erase(it); | |
| 151 did_remove_last_track = had_tracks && tracks_.empty(); | |
| 79 } | 152 } |
| 80 if (webaudio_capturer_) { | 153 // TODO(miu): Is this the behavior we want? Perhaps sources should be |
| 81 // A separate source is created for each track, so just stop the source. | 154 // explicitly closed, rather than auto-stop on the removal of the last track? |
|
perkj_chrome
2016/04/08 14:05:41
This is according to spec.
If you have two track
miu
2016/04/19 00:40:22
Thanks for the clarification. I've replaced this
| |
| 82 webaudio_capturer_->Stop(); | 155 // As of this writing, UserMediaClientImpl depends on this. |
| 83 } | 156 if (!is_stopped_ && did_remove_last_track) |
| 157 MediaStreamSource::StopSource(); | |
| 84 } | 158 } |
| 85 | 159 |
| 86 } // namespace content | 160 } // namespace content |
| OLD | NEW |