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. | |
52 if (!is_stopped_) { | |
53 if (!EnsureSourceIsStarted()) | |
54 return false; | |
perkj_chrome
2016/04/20 13:34:54
is it safe to return here? ie- that means that the
miu
2016/04/20 22:04:52
Done. I see your point. Agree that it's safer to
| |
55 } | |
56 | |
57 // Create and initialize a new MediaStreamAudioTrack and pass ownership of it | |
58 // to the WebMediaStreamTrack. | |
59 blink::WebMediaStreamTrack mutable_blink_track = blink_track; | |
60 mutable_blink_track.setExtraData( | |
61 CreateMediaStreamAudioTrack(blink_track.id().utf8()).release()); | |
62 | |
63 // Start the track and add it as a consumer of audio from this source. | |
64 MediaStreamAudioTrack* const track = MediaStreamAudioTrack::From(blink_track); | |
65 DCHECK(track); | |
66 track->Start(base::Bind(&MediaStreamAudioSource::StopAudioDeliveryTo, | |
67 weak_factory_.GetWeakPtr(), track)); | |
68 track->SetEnabled(blink_track.isEnabled()); | |
69 DVLOG(1) << "Adding MediaStreamAudioTrack@" << track | |
70 << " as a consumer of MediaStreamAudioSource@" << this << '.'; | |
71 deliverer_.AddConsumer(track); | |
72 | |
73 // If the source has already been permanently stopped, stop the track to set | |
74 // its ready state to "ended." | |
75 if (is_stopped_) | |
76 track->Stop(); | |
77 | |
78 return true; | |
45 } | 79 } |
46 | 80 |
47 void MediaStreamAudioSource::AddTrack( | 81 media::AudioParameters MediaStreamAudioSource::GetAudioParameters() const { |
48 const blink::WebMediaStreamTrack& track, | 82 return deliverer_.GetAudioParameters(); |
49 const blink::WebMediaConstraints& constraints, | 83 } |
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 | 84 |
66 factory_->CreateLocalAudioTrack(track); | 85 void* MediaStreamAudioSource::GetClassIdentifier() const { |
67 callback.Run(this, MEDIA_DEVICE_OK, ""); | 86 return nullptr; |
87 } | |
88 | |
89 std::unique_ptr<MediaStreamAudioTrack> | |
90 MediaStreamAudioSource::CreateMediaStreamAudioTrack(const std::string& id) { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 return std::unique_ptr<MediaStreamAudioTrack>( | |
93 new MediaStreamAudioTrack(is_local_source())); | |
94 } | |
95 | |
96 bool MediaStreamAudioSource::EnsureSourceIsStarted() { | |
97 DCHECK(thread_checker_.CalledOnValidThread()); | |
98 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStarted()"; | |
99 return true; | |
100 } | |
101 | |
102 void MediaStreamAudioSource::EnsureSourceIsStopped() { | |
103 DCHECK(thread_checker_.CalledOnValidThread()); | |
104 DVLOG(1) << "MediaStreamAudioSource@" << this << "::EnsureSourceIsStopped()"; | |
105 } | |
106 | |
107 void MediaStreamAudioSource::SetFormat(const media::AudioParameters& params) { | |
108 DVLOG(1) << "MediaStreamAudioSource@" << this << "::SetFormat(" | |
109 << params.AsHumanReadableString() << "), was previously set to {" | |
110 << deliverer_.GetAudioParameters().AsHumanReadableString() << "}."; | |
111 deliverer_.OnSetFormat(params); | |
112 } | |
113 | |
114 void MediaStreamAudioSource::DeliverDataToTracks( | |
115 const media::AudioBus& audio_bus, | |
116 base::TimeTicks reference_time) { | |
117 deliverer_.OnData(audio_bus, reference_time); | |
118 } | |
119 | |
120 void MediaStreamAudioSource::DoStopSource() { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 EnsureSourceIsStopped(); | |
123 is_stopped_ = true; | |
68 } | 124 } |
69 | 125 |
70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { | 126 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
71 DCHECK(track); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
72 if (audio_capturer_) { | 128 |
73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used | 129 const bool did_remove_last_track = deliverer_.RemoveConsumer(track); |
74 // with WebRtcAudioCapturer sources. | 130 DVLOG(1) << "Removed MediaStreamAudioTrack@" << track |
75 // | 131 << " as a consumer of MediaStreamAudioSource@" << this << '.'; |
76 // TODO(miu): That said, this ugly cast won't be necessary after my | 132 |
77 // soon-upcoming refactoring change. | 133 // The W3C spec requires a source automatically stop when the last track is |
78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); | 134 // stopped. |
79 } | 135 if (!is_stopped_ && did_remove_last_track) |
80 if (webaudio_capturer_) { | 136 MediaStreamSource::StopSource(); |
81 // A separate source is created for each track, so just stop the source. | |
82 webaudio_capturer_->Stop(); | |
83 } | |
84 } | 137 } |
85 | 138 |
86 } // namespace content | 139 } // namespace content |
OLD | NEW |