OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/webrtc_local_audio_track.h" | 5 #include "content/renderer/media/webrtc_local_audio_track.h" |
6 | 6 |
7 #include "content/public/renderer/media_stream_audio_sink.h" | 7 #include "content/public/renderer/media_stream_audio_sink.h" |
8 #include "content/renderer/media/media_stream_audio_level_calculator.h" | 8 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
9 #include "content/renderer/media/media_stream_audio_processor.h" | 9 #include "content/renderer/media/media_stream_audio_processor.h" |
10 #include "content/renderer/media/media_stream_audio_sink_owner.h" | 10 #include "content/renderer/media/media_stream_audio_sink_owner.h" |
11 #include "content/renderer/media/media_stream_audio_track_sink.h" | 11 #include "content/renderer/media/media_stream_audio_track_sink.h" |
12 #include "content/renderer/media/peer_connection_audio_sink_owner.h" | 12 #include "content/renderer/media/peer_connection_audio_sink_owner.h" |
13 #include "content/renderer/media/webaudio_capturer_source.h" | 13 #include "content/renderer/media/webaudio_capturer_source.h" |
14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | 14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
15 #include "content/renderer/media/webrtc_audio_capturer.h" | 15 #include "content/renderer/media/webrtc_audio_capturer.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( | 19 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( |
20 WebRtcLocalAudioTrackAdapter* adapter, | 20 WebRtcLocalAudioTrackAdapter* adapter, |
21 const scoped_refptr<WebRtcAudioCapturer>& capturer, | 21 const scoped_refptr<WebRtcAudioCapturer>& capturer, |
22 WebAudioCapturerSource* webaudio_source) | 22 WebAudioCapturerSource* webaudio_source) |
23 : MediaStreamTrack(adapter, true), | 23 : MediaStreamTrack(true), |
24 adapter_(adapter), | 24 adapter_(adapter), |
25 capturer_(capturer), | 25 capturer_(capturer), |
26 webaudio_source_(webaudio_source) { | 26 webaudio_source_(webaudio_source) { |
27 DCHECK(capturer.get() || webaudio_source); | 27 DCHECK(capturer.get() || webaudio_source); |
| 28 signal_thread_checker_.DetachFromThread(); |
28 | 29 |
29 adapter_->Initialize(this); | 30 adapter_->Initialize(this); |
30 | 31 |
31 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; | 32 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; |
32 } | 33 } |
33 | 34 |
34 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { | 35 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { |
35 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 36 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
36 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; | 37 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; |
37 // Users might not call Stop() on the track. | 38 // Users might not call Stop() on the track. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 const scoped_refptr<MediaStreamAudioProcessor>& processor) { | 116 const scoped_refptr<MediaStreamAudioProcessor>& processor) { |
116 // if the |processor| does not have audio processing, which can happen if | 117 // if the |processor| does not have audio processing, which can happen if |
117 // kDisableAudioTrackProcessing is set set or all the constraints in | 118 // kDisableAudioTrackProcessing is set set or all the constraints in |
118 // the |processor| are turned off. In such case, we pass NULL to the | 119 // the |processor| are turned off. In such case, we pass NULL to the |
119 // adapter to indicate that no stats can be gotten from the processor. | 120 // adapter to indicate that no stats can be gotten from the processor. |
120 adapter_->SetAudioProcessor(processor->has_audio_processing() ? | 121 adapter_->SetAudioProcessor(processor->has_audio_processing() ? |
121 processor : NULL); | 122 processor : NULL); |
122 } | 123 } |
123 | 124 |
124 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) { | 125 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) { |
125 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 126 // This method is called from webrtc, on the signaling thread, when the local |
| 127 // description is set and from the main thread from WebMediaPlayerMS::load |
| 128 // (via WebRtcLocalAudioRenderer::Start). |
| 129 DCHECK(main_render_thread_checker_.CalledOnValidThread() || |
| 130 signal_thread_checker_.CalledOnValidThread()); |
126 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; | 131 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; |
127 base::AutoLock auto_lock(lock_); | 132 base::AutoLock auto_lock(lock_); |
128 | 133 |
129 // Verify that |sink| is not already added to the list. | 134 // Verify that |sink| is not already added to the list. |
130 DCHECK(!sinks_.Contains( | 135 DCHECK(!sinks_.Contains( |
131 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink))); | 136 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink))); |
132 | 137 |
133 // Create (and add to the list) a new MediaStreamAudioTrackSink | 138 // Create (and add to the list) a new MediaStreamAudioTrackSink |
134 // which owns the |sink| and delagates all calls to the | 139 // which owns the |sink| and delagates all calls to the |
135 // MediaStreamAudioSink interface. It will be tagged in the list, so | 140 // MediaStreamAudioSink interface. It will be tagged in the list, so |
136 // we remember to call OnSetFormat() on the new sink. | 141 // we remember to call OnSetFormat() on the new sink. |
137 scoped_refptr<MediaStreamAudioTrackSink> sink_owner( | 142 scoped_refptr<MediaStreamAudioTrackSink> sink_owner( |
138 new MediaStreamAudioSinkOwner(sink)); | 143 new MediaStreamAudioSinkOwner(sink)); |
139 sinks_.AddAndTag(sink_owner.get()); | 144 sinks_.AddAndTag(sink_owner.get()); |
140 } | 145 } |
141 | 146 |
142 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { | 147 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { |
143 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 148 // See AddSink for additional context. When local audio is stopped from |
| 149 // webrtc, we'll be called here on the signaling thread. |
| 150 DCHECK(main_render_thread_checker_.CalledOnValidThread() || |
| 151 signal_thread_checker_.CalledOnValidThread()); |
144 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()"; | 152 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()"; |
145 | 153 |
146 base::AutoLock auto_lock(lock_); | 154 scoped_refptr<MediaStreamAudioTrackSink> removed_item; |
147 | 155 { |
148 scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove( | 156 base::AutoLock auto_lock(lock_); |
149 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)); | 157 removed_item = sinks_.Remove( |
| 158 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)); |
| 159 } |
150 | 160 |
151 // Clear the delegate to ensure that no more capture callbacks will | 161 // Clear the delegate to ensure that no more capture callbacks will |
152 // be sent to this sink. Also avoids a possible crash which can happen | 162 // be sent to this sink. Also avoids a possible crash which can happen |
153 // if this method is called while capturing is active. | 163 // if this method is called while capturing is active. |
154 if (removed_item.get()) | 164 if (removed_item.get()) |
155 removed_item->Reset(); | 165 removed_item->Reset(); |
156 } | 166 } |
157 | 167 |
158 void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) { | 168 void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) { |
159 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 169 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 base::AutoLock auto_lock(lock_); | 215 base::AutoLock auto_lock(lock_); |
206 sinks = sinks_.Items(); | 216 sinks = sinks_.Items(); |
207 } | 217 } |
208 for (SinkList::ItemList::const_iterator it = sinks.begin(); | 218 for (SinkList::ItemList::const_iterator it = sinks.begin(); |
209 it != sinks.end(); | 219 it != sinks.end(); |
210 ++it) { | 220 ++it) { |
211 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive); | 221 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive); |
212 } | 222 } |
213 } | 223 } |
214 | 224 |
| 225 void WebRtcLocalAudioTrack::SetEnabled(bool enabled) { |
| 226 DCHECK(thread_checker_.CalledOnValidThread()); |
| 227 if (adapter_.get()) |
| 228 adapter_->set_enabled(enabled); |
| 229 } |
| 230 |
215 void WebRtcLocalAudioTrack::Stop() { | 231 void WebRtcLocalAudioTrack::Stop() { |
216 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 232 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
217 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()"; | 233 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()"; |
218 if (!capturer_.get() && !webaudio_source_.get()) | 234 if (!capturer_.get() && !webaudio_source_.get()) |
219 return; | 235 return; |
220 | 236 |
221 if (webaudio_source_.get()) { | 237 if (webaudio_source_.get()) { |
222 // Called Stop() on the |webaudio_source_| explicitly so that | 238 // Called Stop() on the |webaudio_source_| explicitly so that |
223 // |webaudio_source_| won't push more data to the track anymore. | 239 // |webaudio_source_| won't push more data to the track anymore. |
224 // Also note that the track is not registered as a sink to the |capturer_| | 240 // Also note that the track is not registered as a sink to the |capturer_| |
(...skipping 17 matching lines...) Expand all Loading... |
242 } | 258 } |
243 | 259 |
244 for (SinkList::ItemList::const_iterator it = sinks.begin(); | 260 for (SinkList::ItemList::const_iterator it = sinks.begin(); |
245 it != sinks.end(); | 261 it != sinks.end(); |
246 ++it){ | 262 ++it){ |
247 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); | 263 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); |
248 (*it)->Reset(); | 264 (*it)->Reset(); |
249 } | 265 } |
250 } | 266 } |
251 | 267 |
| 268 webrtc::AudioTrackInterface* WebRtcLocalAudioTrack::GetAudioAdapter() { |
| 269 DCHECK(thread_checker_.CalledOnValidThread()); |
| 270 return adapter_.get(); |
| 271 } |
| 272 |
252 } // namespace content | 273 } // namespace content |
OLD | NEW |