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 "base/bind.h" |
| 8 #include "content/renderer/render_frame_impl.h" | 8 #include "content/renderer/media/media_stream_audio_track.h" |
| 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 10 #include "third_party/WebKit/public/platform/WebString.h" | |
| 10 | 11 |
| 11 namespace content { | 12 namespace content { |
| 12 | 13 |
| 13 MediaStreamAudioSource::MediaStreamAudioSource( | 14 MediaStreamAudioSource::MediaStreamAudioSource(bool is_local_source) |
| 14 int render_frame_id, | 15 : is_local_source_(is_local_source), |
| 15 const StreamDeviceInfo& device_info, | 16 is_stopped_(false), |
| 16 const SourceStoppedCallback& stop_callback, | |
| 17 PeerConnectionDependencyFactory* factory) | |
| 18 : render_frame_id_(render_frame_id), factory_(factory), | |
| 19 weak_factory_(this) { | 17 weak_factory_(this) { |
| 20 SetDeviceInfo(device_info); | 18 DVLOG(1) << "MediaStreamAudioSource@" << this << "::MediaStreamAudioSource(" |
| 21 SetStopCallback(stop_callback); | 19 << (is_local_source_ ? "local" : "remote") << " source)"; |
| 22 } | 20 } |
| 23 | 21 |
| 24 MediaStreamAudioSource::MediaStreamAudioSource() | 22 MediaStreamAudioSource::~MediaStreamAudioSource() { |
| 25 : render_frame_id_(-1), factory_(NULL), weak_factory_(this) { | 23 DCHECK(thread_checker_.CalledOnValidThread()); |
| 24 DVLOG(1) << "MediaStreamAudioSource@" << this << " is being destroyed."; | |
| 26 } | 25 } |
| 27 | 26 |
| 28 MediaStreamAudioSource::~MediaStreamAudioSource() {} | |
| 29 | |
| 30 // static | 27 // static |
| 31 MediaStreamAudioSource* MediaStreamAudioSource::From( | 28 MediaStreamAudioSource* MediaStreamAudioSource::From( |
| 32 const blink::WebMediaStreamSource& source) { | 29 const blink::WebMediaStreamSource& source) { |
| 33 if (source.isNull() || | 30 if (source.isNull() || |
| 34 source.getType() != blink::WebMediaStreamSource::TypeAudio) { | 31 source.getType() != blink::WebMediaStreamSource::TypeAudio) { |
| 35 return nullptr; | 32 return nullptr; |
| 36 } | 33 } |
| 37 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); | 34 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); |
| 38 } | 35 } |
| 39 | 36 |
| 40 void MediaStreamAudioSource::DoStopSource() { | 37 bool MediaStreamAudioSource::ConnectToTrack( |
| 41 if (audio_capturer_) | 38 const blink::WebMediaStreamTrack& blink_track) { |
| 42 audio_capturer_->Stop(); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 if (webaudio_capturer_) | 40 DCHECK(!blink_track.isNull()); |
| 44 webaudio_capturer_->Stop(); | 41 |
| 42 // Sanity-check that there is not already a MediaStreamAudioTrack instance | |
| 43 // associated with |blink_track|. | |
| 44 if (MediaStreamAudioTrack::From(blink_track)) { | |
| 45 LOG(DFATAL) | |
| 46 << "Attempting to connect another source to a WebMediaStreamTrack."; | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 // Unless the source has already been permanently stopped, ensure it is | |
| 51 // started. If the source is not started, the new MediaStreamAudioTrack will | |
| 52 // be initialized to the stopped/ended state. | |
| 53 const bool initializing_ended_track = is_stopped_ || !EnsureSourceIsStarted(); | |
| 54 | |
| 55 // Create and initialize a new MediaStreamAudioTrack and pass ownership of it | |
| 56 // to the WebMediaStreamTrack. | |
| 57 blink::WebMediaStreamTrack mutable_blink_track = blink_track; | |
| 58 mutable_blink_track.setExtraData( | |
| 59 CreateMediaStreamAudioTrack(blink_track.id().utf8()).release()); | |
| 60 | |
| 61 // Start the track and add it as a consumer of audio from this source. | |
| 62 MediaStreamAudioTrack* const track = MediaStreamAudioTrack::From(blink_track); | |
| 63 DCHECK(track); | |
| 64 track->Start(base::Bind(&MediaStreamAudioSource::StopAudioDeliveryTo, | |
| 65 weak_factory_.GetWeakPtr(), track)); | |
| 66 track->SetEnabled(blink_track.isEnabled()); | |
| 67 DVLOG(1) << "Adding MediaStreamAudioTrack@" << track | |
| 68 << " as a consumer of MediaStreamAudioSource@" << this << '.'; | |
| 69 deliverer_.AddConsumer(track); | |
| 70 | |
| 71 // If the source failed to start, or has already been permanently stopped, | |
| 72 // stop the track to set its ready state to "ended." | |
|
o1ka
2016/04/21 18:51:22
"ended."->"ended".
It's not quite clear what "read
miu
2016/04/21 20:42:30
Done.
o1ka
2016/04/22 11:29:25
Yes, I realized it, the problem about the comment
| |
| 73 if (initializing_ended_track) | |
| 74 track->Stop(); | |
|
o1ka
2016/04/21 18:51:22
Consumer is added at l.69 and it is removed here w
miu
2016/04/21 20:42:30
It was. Per suggested (previous round of comments
o1ka
2016/04/22 11:29:25
Yes, I understand the reason of a changed, just wa
| |
| 75 | |
| 76 return !initializing_ended_track; | |
| 45 } | 77 } |
| 46 | 78 |
| 47 void MediaStreamAudioSource::AddTrack( | 79 media::AudioParameters MediaStreamAudioSource::GetAudioParameters() const { |
| 48 const blink::WebMediaStreamTrack& track, | 80 return deliverer_.GetAudioParameters(); |
| 49 const blink::WebMediaConstraints& constraints, | 81 } |
| 50 const ConstraintsCallback& callback) { | |
| 51 // TODO(xians): Properly implement for audio sources. | |
| 52 if (!local_audio_source_.get()) { | |
| 53 if (!factory_->InitializeMediaStreamAudioSource(render_frame_id_, | |
| 54 constraints, this)) { | |
| 55 // The source failed to start. | |
| 56 // UserMediaClientImpl rely on the |stop_callback| to be triggered when | |
| 57 // the last track is removed from the source. But in this case, the | |
| 58 // source is is not even started. So we need to fail both adding the | |
| 59 // track and trigger |stop_callback|. | |
| 60 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | |
| 61 StopSource(); | |
| 62 return; | |
| 63 } | |
| 64 } | |
| 65 | 82 |
| 66 factory_->CreateLocalAudioTrack(track); | 83 void* MediaStreamAudioSource::GetClassIdentifier() const { |
| 67 callback.Run(this, MEDIA_DEVICE_OK, ""); | 84 return nullptr; |
| 85 } | |
| 86 | |
| 87 std::unique_ptr<MediaStreamAudioTrack> | |
| 88 MediaStreamAudioSource::CreateMediaStreamAudioTrack(const std::string& id) { | |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 90 return std::unique_ptr<MediaStreamAudioTrack>( | |
| 91 new MediaStreamAudioTrack(is_local_source())); | |
| 92 } | |
| 93 | |
| 94 bool MediaStreamAudioSource::EnsureSourceIsStarted() { | |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 96 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStarted()"; | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 void MediaStreamAudioSource::EnsureSourceIsStopped() { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStopped()"; | |
| 103 } | |
| 104 | |
| 105 void MediaStreamAudioSource::SetFormat(const media::AudioParameters& params) { | |
| 106 DVLOG(1) << "MediaStreamAudioSource@" << this << "::SetFormat(" | |
| 107 << params.AsHumanReadableString() << "), was previously set to {" | |
| 108 << deliverer_.GetAudioParameters().AsHumanReadableString() << "}."; | |
| 109 deliverer_.OnSetFormat(params); | |
| 110 } | |
| 111 | |
| 112 void MediaStreamAudioSource::DeliverDataToTracks( | |
| 113 const media::AudioBus& audio_bus, | |
| 114 base::TimeTicks reference_time) { | |
| 115 deliverer_.OnData(audio_bus, reference_time); | |
| 116 } | |
| 117 | |
| 118 void MediaStreamAudioSource::DoStopSource() { | |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 120 EnsureSourceIsStopped(); | |
| 121 is_stopped_ = true; | |
| 68 } | 122 } |
| 69 | 123 |
| 70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { | 124 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
| 71 DCHECK(track); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 if (audio_capturer_) { | 126 |
| 73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used | 127 const bool did_remove_last_track = deliverer_.RemoveConsumer(track); |
| 74 // with WebRtcAudioCapturer sources. | 128 DVLOG(1) << "Removed MediaStreamAudioTrack@" << track |
| 75 // | 129 << " as a consumer of MediaStreamAudioSource@" << this << '.'; |
| 76 // TODO(miu): That said, this ugly cast won't be necessary after my | 130 |
| 77 // soon-upcoming refactoring change. | 131 // The W3C spec requires a source automatically stop when the last track is |
| 78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); | 132 // stopped. |
| 79 } | 133 if (!is_stopped_ && did_remove_last_track) |
| 80 if (webaudio_capturer_) { | 134 MediaStreamSource::StopSource(); |
| 81 // A separate source is created for each track, so just stop the source. | |
| 82 webaudio_capturer_->Stop(); | |
| 83 } | |
| 84 } | 135 } |
| 85 | 136 |
| 86 } // namespace content | 137 } // namespace content |
| OLD | NEW |