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 "base/bind.h" | 7 #include "content/renderer/media/webrtc_local_audio_track.h" |
8 #include "content/renderer/media/media_stream_audio_track.h" | 8 #include "content/renderer/render_frame_impl.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" | |
11 | 10 |
12 namespace content { | 11 namespace content { |
13 | 12 |
14 MediaStreamAudioSource::MediaStreamAudioSource(bool is_local_source) | 13 MediaStreamAudioSource::MediaStreamAudioSource( |
15 : is_local_source_(is_local_source), | 14 int render_frame_id, |
16 is_stopped_(false), | 15 const StreamDeviceInfo& device_info, |
| 16 const SourceStoppedCallback& stop_callback, |
| 17 PeerConnectionDependencyFactory* factory) |
| 18 : render_frame_id_(render_frame_id), factory_(factory), |
17 weak_factory_(this) { | 19 weak_factory_(this) { |
18 DVLOG(1) << "MediaStreamAudioSource@" << this << "::MediaStreamAudioSource(" | 20 SetDeviceInfo(device_info); |
19 << (is_local_source_ ? "local" : "remote") << " source)"; | 21 SetStopCallback(stop_callback); |
20 } | 22 } |
21 | 23 |
22 MediaStreamAudioSource::~MediaStreamAudioSource() { | 24 MediaStreamAudioSource::MediaStreamAudioSource() |
23 DCHECK(thread_checker_.CalledOnValidThread()); | 25 : render_frame_id_(-1), factory_(NULL), weak_factory_(this) { |
24 DVLOG(1) << "MediaStreamAudioSource@" << this << " is being destroyed."; | |
25 } | 26 } |
26 | 27 |
| 28 MediaStreamAudioSource::~MediaStreamAudioSource() {} |
| 29 |
27 // static | 30 // static |
28 MediaStreamAudioSource* MediaStreamAudioSource::From( | 31 MediaStreamAudioSource* MediaStreamAudioSource::From( |
29 const blink::WebMediaStreamSource& source) { | 32 const blink::WebMediaStreamSource& source) { |
30 if (source.isNull() || | 33 if (source.isNull() || |
31 source.getType() != blink::WebMediaStreamSource::TypeAudio) { | 34 source.getType() != blink::WebMediaStreamSource::TypeAudio) { |
32 return nullptr; | 35 return nullptr; |
33 } | 36 } |
34 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); | 37 return static_cast<MediaStreamAudioSource*>(source.getExtraData()); |
35 } | 38 } |
36 | 39 |
37 bool MediaStreamAudioSource::ConnectToTrack( | 40 void MediaStreamAudioSource::DoStopSource() { |
38 const blink::WebMediaStreamTrack& blink_track) { | 41 if (audio_capturer_) |
39 DCHECK(thread_checker_.CalledOnValidThread()); | 42 audio_capturer_->Stop(); |
40 DCHECK(!blink_track.isNull()); | 43 if (webaudio_capturer_) |
| 44 webaudio_capturer_->Stop(); |
| 45 } |
41 | 46 |
42 // Sanity-check that there is not already a MediaStreamAudioTrack instance | 47 void MediaStreamAudioSource::AddTrack( |
43 // associated with |blink_track|. | 48 const blink::WebMediaStreamTrack& track, |
44 if (MediaStreamAudioTrack::From(blink_track)) { | 49 const blink::WebMediaConstraints& constraints, |
45 LOG(DFATAL) | 50 const ConstraintsCallback& callback) { |
46 << "Attempting to connect another source to a WebMediaStreamTrack."; | 51 // TODO(xians): Properly implement for audio sources. |
47 return false; | 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 } |
48 } | 64 } |
49 | 65 |
50 // Unless the source has already been permanently stopped, ensure it is | 66 factory_->CreateLocalAudioTrack(track); |
51 // started. If the source cannot start, the new MediaStreamAudioTrack will be | 67 callback.Run(this, MEDIA_DEVICE_OK, ""); |
52 // initialized to the stopped/ended state. | |
53 if (!is_stopped_) { | |
54 if (!EnsureSourceIsStarted()) | |
55 StopSource(); | |
56 } | |
57 | |
58 // Create and initialize a new MediaStreamAudioTrack and pass ownership of it | |
59 // to the WebMediaStreamTrack. | |
60 blink::WebMediaStreamTrack mutable_blink_track = blink_track; | |
61 mutable_blink_track.setExtraData( | |
62 CreateMediaStreamAudioTrack(blink_track.id().utf8()).release()); | |
63 | |
64 // Propagate initial "enabled" state. | |
65 MediaStreamAudioTrack* const track = MediaStreamAudioTrack::From(blink_track); | |
66 DCHECK(track); | |
67 track->SetEnabled(blink_track.isEnabled()); | |
68 | |
69 // If the source is stopped, do not start the track. | |
70 if (is_stopped_) | |
71 return false; | |
72 | |
73 track->Start(base::Bind(&MediaStreamAudioSource::StopAudioDeliveryTo, | |
74 weak_factory_.GetWeakPtr(), track)); | |
75 DVLOG(1) << "Adding MediaStreamAudioTrack@" << track | |
76 << " as a consumer of MediaStreamAudioSource@" << this << '.'; | |
77 deliverer_.AddConsumer(track); | |
78 return true; | |
79 } | |
80 | |
81 media::AudioParameters MediaStreamAudioSource::GetAudioParameters() const { | |
82 return deliverer_.GetAudioParameters(); | |
83 } | |
84 | |
85 void* MediaStreamAudioSource::GetClassIdentifier() const { | |
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; | |
124 } | 68 } |
125 | 69 |
126 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { | 70 void MediaStreamAudioSource::StopAudioDeliveryTo(MediaStreamAudioTrack* track) { |
127 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(track); |
128 | 72 if (audio_capturer_) { |
129 const bool did_remove_last_track = deliverer_.RemoveConsumer(track); | 73 // The cast here is safe because only WebRtcLocalAudioTracks are ever used |
130 DVLOG(1) << "Removed MediaStreamAudioTrack@" << track | 74 // with WebRtcAudioCapturer sources. |
131 << " as a consumer of MediaStreamAudioSource@" << this << '.'; | 75 // |
132 | 76 // TODO(miu): That said, this ugly cast won't be necessary after my |
133 // The W3C spec requires a source automatically stop when the last track is | 77 // soon-upcoming refactoring change. |
134 // stopped. | 78 audio_capturer_->RemoveTrack(static_cast<WebRtcLocalAudioTrack*>(track)); |
135 if (!is_stopped_ && did_remove_last_track) | 79 } |
136 MediaStreamSource::StopSource(); | 80 if (webaudio_capturer_) { |
| 81 // A separate source is created for each track, so just stop the source. |
| 82 webaudio_capturer_->Stop(); |
| 83 } |
137 } | 84 } |
138 | 85 |
139 } // namespace content | 86 } // namespace content |
OLD | NEW |