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_audio_capturer.h" | 5 #include "content/renderer/media/webrtc_audio_capturer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 // will be overwritten if an external client later calls SetCapturerSource() | 157 // will be overwritten if an external client later calls SetCapturerSource() |
158 // providing an alternative media::AudioCapturerSource. | 158 // providing an alternative media::AudioCapturerSource. |
159 SetCapturerSource(AudioDeviceFactory::NewInputDevice(render_view_id), | 159 SetCapturerSource(AudioDeviceFactory::NewInputDevice(render_view_id), |
160 channel_layout, | 160 channel_layout, |
161 static_cast<float>(sample_rate)); | 161 static_cast<float>(sample_rate)); |
162 | 162 |
163 return true; | 163 return true; |
164 } | 164 } |
165 | 165 |
166 WebRtcAudioCapturer::WebRtcAudioCapturer() | 166 WebRtcAudioCapturer::WebRtcAudioCapturer() |
167 : source_(NULL), | 167 : default_sink_(NULL), |
168 source_(NULL), | |
168 running_(false), | 169 running_(false), |
169 agc_is_enabled_(false), | 170 agc_is_enabled_(false), |
170 session_id_(0) { | 171 session_id_(0) { |
171 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; | 172 DVLOG(1) << "WebRtcAudioCapturer::WebRtcAudioCapturer()"; |
172 } | 173 } |
173 | 174 |
174 WebRtcAudioCapturer::~WebRtcAudioCapturer() { | 175 WebRtcAudioCapturer::~WebRtcAudioCapturer() { |
175 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
176 DCHECK(tracks_.empty()); | 177 DCHECK(tracks_.empty()); |
177 DCHECK(!running_); | 178 DCHECK(!running_); |
179 DCHECK(!default_sink_); | |
178 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; | 180 DVLOG(1) << "WebRtcAudioCapturer::~WebRtcAudioCapturer()"; |
179 } | 181 } |
180 | 182 |
181 void WebRtcAudioCapturer::AddSink( | 183 void WebRtcAudioCapturer::SetDefaultSink(WebRtcAudioCapturerSink* sink) { |
182 WebRtcAudioCapturerSink* track) { | 184 DVLOG(1) << "WebRtcAudioCapturer::SetDefaultSink()"; |
183 DCHECK(thread_checker_.CalledOnValidThread()); | 185 if (sink) { |
186 DCHECK(!default_sink_); | |
187 default_sink_ = sink; | |
188 AddSink(sink); | |
189 } else { | |
190 DCHECK(default_sink_); | |
191 RemoveSink(default_sink_); | |
192 default_sink_ = NULL; | |
193 } | |
194 } | |
195 | |
196 void WebRtcAudioCapturer::AddSink(WebRtcAudioCapturerSink* track) { | |
184 DCHECK(track); | 197 DCHECK(track); |
185 DVLOG(1) << "WebRtcAudioCapturer::AddSink()"; | 198 DVLOG(1) << "WebRtcAudioCapturer::AddSink()"; |
199 | |
200 // Start the source if a real audio track is connected to the capturer. | |
henrika (OOO until Aug 14)
2013/06/05 09:09:20
Can you please extend the comment and give an exam
no longer working on chromium
2013/06/05 16:29:45
Done.
| |
201 if (track != default_sink_) | |
202 Start(); | |
203 | |
186 base::AutoLock auto_lock(lock_); | 204 base::AutoLock auto_lock(lock_); |
187 // Verify that |track| is not already added to the list. | 205 // Verify that |track| is not already added to the list. |
henrika (OOO until Aug 14)
2013/06/05 09:09:20
What happens if it is (in release mode)?
no longer working on chromium
2013/06/05 16:29:45
It is a push_back, then we will have two reference
| |
188 DCHECK(std::find_if( | 206 DCHECK(std::find_if( |
189 tracks_.begin(), tracks_.end(), | 207 tracks_.begin(), tracks_.end(), |
190 WebRtcAudioCapturerSinkOwner::WrapsSink(track)) == tracks_.end()); | 208 WebRtcAudioCapturerSinkOwner::WrapsSink(track)) == tracks_.end()); |
191 | 209 |
192 if (buffer_.get()) { | 210 if (buffer_.get()) { |
193 track->SetCaptureFormat(buffer_->params()); | 211 track->SetCaptureFormat(buffer_->params()); |
194 } else { | 212 } else { |
195 DLOG(WARNING) << "The format of the capturer has not been correctly " | 213 DLOG(WARNING) << "The format of the capturer has not been correctly " |
196 << "initialized"; | 214 << "initialized"; |
197 } | 215 } |
198 | 216 |
199 // Create (and add to the list) a new WebRtcAudioCapturerSinkOwner which owns | 217 // Create (and add to the list) a new WebRtcAudioCapturerSinkOwner which owns |
200 // the |track| and delagates all calls to the WebRtcAudioCapturerSink | 218 // the |track| and delagates all calls to the WebRtcAudioCapturerSink |
201 // interface. | 219 // interface. |
202 tracks_.push_back(new WebRtcAudioCapturerSinkOwner(track)); | 220 tracks_.push_back(new WebRtcAudioCapturerSinkOwner(track)); |
203 // TODO(xians): should we call SetCapturerFormat() to each track? | 221 // TODO(xians): should we call SetCapturerFormat() to each track? |
204 } | 222 } |
205 | 223 |
206 void WebRtcAudioCapturer::RemoveSink( | 224 void WebRtcAudioCapturer::RemoveSink( |
207 WebRtcAudioCapturerSink* track) { | 225 WebRtcAudioCapturerSink* track) { |
208 DCHECK(thread_checker_.CalledOnValidThread()); | 226 DCHECK(thread_checker_.CalledOnValidThread()); |
209 DVLOG(1) << "WebRtcAudioCapturer::RemoveSink()"; | 227 DVLOG(1) << "WebRtcAudioCapturer::RemoveSink()"; |
210 | 228 |
211 base::AutoLock auto_lock(lock_); | 229 bool stop_source = false; |
230 { | |
231 base::AutoLock auto_lock(lock_); | |
212 | 232 |
213 // Get iterator to the first element for which WrapsSink(track) returns true. | 233 // Get iterator to the first element for which WrapsSink(track) returns |
214 TrackList::iterator it = std::find_if( | 234 // true. |
215 tracks_.begin(), tracks_.end(), | 235 TrackList::iterator it = std::find_if( |
216 WebRtcAudioCapturerSinkOwner::WrapsSink(track)); | 236 tracks_.begin(), tracks_.end(), |
217 if (it != tracks_.end()) { | 237 WebRtcAudioCapturerSinkOwner::WrapsSink(track)); |
218 // Clear the delegate to ensure that no more capture callbacks will | 238 if (it != tracks_.end()) { |
219 // be sent to this sink. Also avoids a possible crash which can happen | 239 // Clear the delegate to ensure that no more capture callbacks will |
220 // if this method is called while capturing is active. | 240 // be sent to this sink. Also avoids a possible crash which can happen |
221 (*it)->Reset(); | 241 // if this method is called while capturing is active. |
222 tracks_.erase(it); | 242 (*it)->Reset(); |
243 tracks_.erase(it); | |
244 } | |
245 | |
246 if (tracks_.size() == 1 && default_sink_ && | |
henrika (OOO until Aug 14)
2013/06/05 09:09:20
You must really add clear comments here explaining
no longer working on chromium
2013/06/05 16:29:45
Done.
| |
247 (*tracks_.begin())->IsEqual(default_sink_)) { | |
248 stop_source = true; | |
249 } else { | |
250 stop_source = tracks_.empty(); | |
251 } | |
223 } | 252 } |
253 | |
254 if (stop_source) | |
255 Stop(); | |
224 } | 256 } |
225 | 257 |
226 void WebRtcAudioCapturer::SetCapturerSource( | 258 void WebRtcAudioCapturer::SetCapturerSource( |
227 const scoped_refptr<media::AudioCapturerSource>& source, | 259 const scoped_refptr<media::AudioCapturerSource>& source, |
228 media::ChannelLayout channel_layout, | 260 media::ChannelLayout channel_layout, |
229 float sample_rate) { | 261 float sample_rate) { |
230 DCHECK(thread_checker_.CalledOnValidThread()); | 262 DCHECK(thread_checker_.CalledOnValidThread()); |
231 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," | 263 DVLOG(1) << "SetCapturerSource(channel_layout=" << channel_layout << "," |
232 << "sample_rate=" << sample_rate << ")"; | 264 << "sample_rate=" << sample_rate << ")"; |
233 scoped_refptr<media::AudioCapturerSource> old_source; | 265 scoped_refptr<media::AudioCapturerSource> old_source; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 } | 399 } |
368 | 400 |
369 media::AudioParameters WebRtcAudioCapturer::audio_parameters() const { | 401 media::AudioParameters WebRtcAudioCapturer::audio_parameters() const { |
370 base::AutoLock auto_lock(lock_); | 402 base::AutoLock auto_lock(lock_); |
371 // |buffer_| can be NULL when SetCapturerSource() or Initialize() has not | 403 // |buffer_| can be NULL when SetCapturerSource() or Initialize() has not |
372 // been called. | 404 // been called. |
373 return buffer_.get() ? buffer_->params() : media::AudioParameters(); | 405 return buffer_.get() ? buffer_->params() : media::AudioParameters(); |
374 } | 406 } |
375 | 407 |
376 } // namespace content | 408 } // namespace content |
OLD | NEW |