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
3 // found in the LICENSE file.
5 #include "content/renderer/media/webrtc_local_audio_track.h"
7 #include "content/public/renderer/media_stream_audio_sink.h"
8 #include "content/renderer/media/media_stream_audio_level_calculator.h"
9 #include "content/renderer/media/media_stream_audio_processor.h"
10 #include "content/renderer/media/media_stream_audio_sink_owner.h"
11 #include "content/renderer/media/media_stream_audio_track_sink.h"
12 #include "content/renderer/media/peer_connection_audio_sink_owner.h"
13 #include "content/renderer/media/webaudio_capturer_source.h"
14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
15 #include "content/renderer/media/webrtc_audio_capturer.h"
19 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack(
20 WebRtcLocalAudioTrackAdapter* adapter,
21 const scoped_refptr<WebRtcAudioCapturer>& capturer,
22 WebAudioCapturerSource* webaudio_source)
23 : MediaStreamTrack(true),
26 webaudio_source_(webaudio_source) {
27 DCHECK(capturer.get() || webaudio_source);
28 signal_thread_checker_.DetachFromThread();
30 adapter_->Initialize(this);
32 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()";
35 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() {
36 DCHECK(main_render_thread_checker_.CalledOnValidThread());
37 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()";
38 // Users might not call Stop() on the track.
42 void WebRtcLocalAudioTrack::Capture(const int16* audio_data,
43 base::TimeDelta delay,
46 bool need_audio_processing,
47 bool force_report_nonzero_energy) {
48 DCHECK(capture_thread_checker_.CalledOnValidThread());
50 // Calculate the signal level regardless if the track is disabled or enabled.
51 int signal_level = level_calculator_->Calculate(
52 audio_data, audio_parameters_.channels(),
53 audio_parameters_.frames_per_buffer(), force_report_nonzero_energy);
54 adapter_->SetSignalLevel(signal_level);
56 scoped_refptr<WebRtcAudioCapturer> capturer;
57 SinkList::ItemList sinks;
58 SinkList::ItemList sinks_to_notify_format;
60 base::AutoLock auto_lock(lock_);
62 sinks = sinks_.Items();
63 sinks_.RetrieveAndClearTags(&sinks_to_notify_format);
66 // Notify the tracks on when the format changes. This will do nothing if
67 // |sinks_to_notify_format| is empty.
68 for (SinkList::ItemList::const_iterator it = sinks_to_notify_format.begin();
69 it != sinks_to_notify_format.end(); ++it) {
70 (*it)->OnSetFormat(audio_parameters_);
73 // Feed the data to the sinks.
74 // TODO(jiayl): we should not pass the real audio data down if the track is
75 // disabled. This is currently done so to feed input to WebRTC typing
76 // detection and should be changed when audio processing is moved from
77 // WebRTC to the track.
78 std::vector<int> voe_channels = adapter_->VoeChannels();
79 for (SinkList::ItemList::const_iterator it = sinks.begin();
82 int new_volume = (*it)->OnData(audio_data,
83 audio_parameters_.sample_rate(),
84 audio_parameters_.channels(),
85 audio_parameters_.frames_per_buffer(),
87 delay.InMilliseconds(),
89 need_audio_processing,
91 if (new_volume != 0 && capturer.get() && !webaudio_source_.get()) {
92 // Feed the new volume to WebRtc while changing the volume on the
94 capturer->SetVolume(new_volume);
99 void WebRtcLocalAudioTrack::OnSetFormat(
100 const media::AudioParameters& params) {
101 DVLOG(1) << "WebRtcLocalAudioTrack::OnSetFormat()";
102 // If the source is restarted, we might have changed to another capture
104 capture_thread_checker_.DetachFromThread();
105 DCHECK(capture_thread_checker_.CalledOnValidThread());
107 audio_parameters_ = params;
108 level_calculator_.reset(new MediaStreamAudioLevelCalculator());
110 base::AutoLock auto_lock(lock_);
111 // Remember to notify all sinks of the new format.
115 void WebRtcLocalAudioTrack::SetAudioProcessor(
116 const scoped_refptr<MediaStreamAudioProcessor>& processor) {
117 // if the |processor| does not have audio processing, which can happen if
118 // kDisableAudioTrackProcessing is set set or all the constraints in
119 // the |processor| are turned off. In such case, we pass NULL to the
120 // adapter to indicate that no stats can be gotten from the processor.
121 adapter_->SetAudioProcessor(processor->has_audio_processing() ?
125 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) {
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());
131 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
132 base::AutoLock auto_lock(lock_);
134 // Verify that |sink| is not already added to the list.
135 DCHECK(!sinks_.Contains(
136 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)));
138 // Create (and add to the list) a new MediaStreamAudioTrackSink
139 // which owns the |sink| and delagates all calls to the
140 // MediaStreamAudioSink interface. It will be tagged in the list, so
141 // we remember to call OnSetFormat() on the new sink.
142 scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
143 new MediaStreamAudioSinkOwner(sink));
144 sinks_.AddAndTag(sink_owner.get());
147 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) {
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());
152 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
154 scoped_refptr<MediaStreamAudioTrackSink> removed_item;
156 base::AutoLock auto_lock(lock_);
157 removed_item = sinks_.Remove(
158 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink));
161 // Clear the delegate to ensure that no more capture callbacks will
162 // be sent to this sink. Also avoids a possible crash which can happen
163 // if this method is called while capturing is active.
164 if (removed_item.get())
165 removed_item->Reset();
168 void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) {
169 DCHECK(main_render_thread_checker_.CalledOnValidThread());
170 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
171 base::AutoLock auto_lock(lock_);
173 // Verify that |sink| is not already added to the list.
174 DCHECK(!sinks_.Contains(
175 MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink)));
177 // Create (and add to the list) a new MediaStreamAudioTrackSink
178 // which owns the |sink| and delagates all calls to the
179 // MediaStreamAudioSink interface. It will be tagged in the list, so
180 // we remember to call OnSetFormat() on the new sink.
181 scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
182 new PeerConnectionAudioSinkOwner(sink));
183 sinks_.AddAndTag(sink_owner.get());
186 void WebRtcLocalAudioTrack::RemoveSink(PeerConnectionAudioSink* sink) {
187 DCHECK(main_render_thread_checker_.CalledOnValidThread());
188 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
190 base::AutoLock auto_lock(lock_);
192 scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
193 MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink));
194 // Clear the delegate to ensure that no more capture callbacks will
195 // be sent to this sink. Also avoids a possible crash which can happen
196 // if this method is called while capturing is active.
197 if (removed_item.get())
198 removed_item->Reset();
201 void WebRtcLocalAudioTrack::Start() {
202 DCHECK(main_render_thread_checker_.CalledOnValidThread());
203 DVLOG(1) << "WebRtcLocalAudioTrack::Start()";
204 if (webaudio_source_.get()) {
205 // If the track is hooking up with WebAudio, do NOT add the track to the
206 // capturer as its sink otherwise two streams in different clock will be
207 // pushed through the same track.
208 webaudio_source_->Start(this, capturer_.get());
209 } else if (capturer_.get()) {
210 capturer_->AddTrack(this);
213 SinkList::ItemList sinks;
215 base::AutoLock auto_lock(lock_);
216 sinks = sinks_.Items();
218 for (SinkList::ItemList::const_iterator it = sinks.begin();
221 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive);
225 void WebRtcLocalAudioTrack::SetEnabled(bool enabled) {
226 DCHECK(thread_checker_.CalledOnValidThread());
228 adapter_->set_enabled(enabled);
231 void WebRtcLocalAudioTrack::Stop() {
232 DCHECK(main_render_thread_checker_.CalledOnValidThread());
233 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()";
234 if (!capturer_.get() && !webaudio_source_.get())
237 if (webaudio_source_.get()) {
238 // Called Stop() on the |webaudio_source_| explicitly so that
239 // |webaudio_source_| won't push more data to the track anymore.
240 // Also note that the track is not registered as a sink to the |capturer_|
241 // in such case and no need to call RemoveTrack().
242 webaudio_source_->Stop();
244 // It is necessary to call RemoveTrack on the |capturer_| to avoid getting
245 // audio callback after Stop().
246 capturer_->RemoveTrack(this);
249 // Protect the pointers using the lock when accessing |sinks_| and
250 // setting the |capturer_| to NULL.
251 SinkList::ItemList sinks;
253 base::AutoLock auto_lock(lock_);
254 sinks = sinks_.Items();
256 webaudio_source_ = NULL;
260 for (SinkList::ItemList::const_iterator it = sinks.begin();
263 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded);
268 webrtc::AudioTrackInterface* WebRtcLocalAudioTrack::GetAudioAdapter() {
269 DCHECK(thread_checker_.CalledOnValidThread());
270 return adapter_.get();
273 } // namespace content