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(adapter, true),
26 webaudio_source_(webaudio_source) {
27 DCHECK(capturer.get() || webaudio_source);
29 adapter_->Initialize(this);
31 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()";
34 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() {
35 DCHECK(main_render_thread_checker_.CalledOnValidThread());
36 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()";
37 // Users might not call Stop() on the track.
41 void WebRtcLocalAudioTrack::Capture(const int16* audio_data,
42 base::TimeDelta delay,
45 bool need_audio_processing,
46 bool force_report_nonzero_energy) {
47 DCHECK(capture_thread_checker_.CalledOnValidThread());
49 // Calculate the signal level regardless if the track is disabled or enabled.
50 int signal_level = level_calculator_->Calculate(
51 audio_data, audio_parameters_.channels(),
52 audio_parameters_.frames_per_buffer(), force_report_nonzero_energy);
53 adapter_->SetSignalLevel(signal_level);
55 scoped_refptr<WebRtcAudioCapturer> capturer;
56 SinkList::ItemList sinks;
57 SinkList::ItemList sinks_to_notify_format;
59 base::AutoLock auto_lock(lock_);
61 sinks = sinks_.Items();
62 sinks_.RetrieveAndClearTags(&sinks_to_notify_format);
65 // Notify the tracks on when the format changes. This will do nothing if
66 // |sinks_to_notify_format| is empty.
67 for (SinkList::ItemList::const_iterator it = sinks_to_notify_format.begin();
68 it != sinks_to_notify_format.end(); ++it) {
69 (*it)->OnSetFormat(audio_parameters_);
72 // Feed the data to the sinks.
73 // TODO(jiayl): we should not pass the real audio data down if the track is
74 // disabled. This is currently done so to feed input to WebRTC typing
75 // detection and should be changed when audio processing is moved from
76 // WebRTC to the track.
77 std::vector<int> voe_channels = adapter_->VoeChannels();
78 for (SinkList::ItemList::const_iterator it = sinks.begin();
81 int new_volume = (*it)->OnData(audio_data,
82 audio_parameters_.sample_rate(),
83 audio_parameters_.channels(),
84 audio_parameters_.frames_per_buffer(),
86 delay.InMilliseconds(),
88 need_audio_processing,
90 if (new_volume != 0 && capturer.get() && !webaudio_source_.get()) {
91 // Feed the new volume to WebRtc while changing the volume on the
93 capturer->SetVolume(new_volume);
98 void WebRtcLocalAudioTrack::OnSetFormat(
99 const media::AudioParameters& params) {
100 DVLOG(1) << "WebRtcLocalAudioTrack::OnSetFormat()";
101 // If the source is restarted, we might have changed to another capture
103 capture_thread_checker_.DetachFromThread();
104 DCHECK(capture_thread_checker_.CalledOnValidThread());
106 audio_parameters_ = params;
107 level_calculator_.reset(new MediaStreamAudioLevelCalculator());
109 base::AutoLock auto_lock(lock_);
110 // Remember to notify all sinks of the new format.
114 void WebRtcLocalAudioTrack::SetAudioProcessor(
115 const scoped_refptr<MediaStreamAudioProcessor>& processor) {
116 // if the |processor| does not have audio processing, which can happen if
117 // kDisableAudioTrackProcessing is set set or all the constraints in
118 // 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_->SetAudioProcessor(processor->has_audio_processing() ?
124 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) {
125 DCHECK(main_render_thread_checker_.CalledOnValidThread());
126 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
127 base::AutoLock auto_lock(lock_);
129 // Verify that |sink| is not already added to the list.
130 DCHECK(!sinks_.Contains(
131 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)));
133 // Create (and add to the list) a new MediaStreamAudioTrackSink
134 // which owns the |sink| and delagates all calls to the
135 // MediaStreamAudioSink interface. It will be tagged in the list, so
136 // we remember to call OnSetFormat() on the new sink.
137 scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
138 new MediaStreamAudioSinkOwner(sink));
139 sinks_.AddAndTag(sink_owner.get());
142 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) {
143 DCHECK(main_render_thread_checker_.CalledOnValidThread());
144 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
146 base::AutoLock auto_lock(lock_);
148 scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
149 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink));
151 // 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
153 // if this method is called while capturing is active.
154 if (removed_item.get())
155 removed_item->Reset();
158 void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) {
159 DCHECK(main_render_thread_checker_.CalledOnValidThread());
160 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
161 base::AutoLock auto_lock(lock_);
163 // Verify that |sink| is not already added to the list.
164 DCHECK(!sinks_.Contains(
165 MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink)));
167 // Create (and add to the list) a new MediaStreamAudioTrackSink
168 // which owns the |sink| and delagates all calls to the
169 // MediaStreamAudioSink interface. It will be tagged in the list, so
170 // we remember to call OnSetFormat() on the new sink.
171 scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
172 new PeerConnectionAudioSinkOwner(sink));
173 sinks_.AddAndTag(sink_owner.get());
176 void WebRtcLocalAudioTrack::RemoveSink(PeerConnectionAudioSink* sink) {
177 DCHECK(main_render_thread_checker_.CalledOnValidThread());
178 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
180 base::AutoLock auto_lock(lock_);
182 scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
183 MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink));
184 // Clear the delegate to ensure that no more capture callbacks will
185 // be sent to this sink. Also avoids a possible crash which can happen
186 // if this method is called while capturing is active.
187 if (removed_item.get())
188 removed_item->Reset();
191 void WebRtcLocalAudioTrack::Start() {
192 DCHECK(main_render_thread_checker_.CalledOnValidThread());
193 DVLOG(1) << "WebRtcLocalAudioTrack::Start()";
194 if (webaudio_source_.get()) {
195 // If the track is hooking up with WebAudio, do NOT add the track to the
196 // capturer as its sink otherwise two streams in different clock will be
197 // pushed through the same track.
198 webaudio_source_->Start(this, capturer_.get());
199 } else if (capturer_.get()) {
200 capturer_->AddTrack(this);
203 SinkList::ItemList sinks;
205 base::AutoLock auto_lock(lock_);
206 sinks = sinks_.Items();
208 for (SinkList::ItemList::const_iterator it = sinks.begin();
211 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive);
215 void WebRtcLocalAudioTrack::Stop() {
216 DCHECK(main_render_thread_checker_.CalledOnValidThread());
217 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()";
218 if (!capturer_.get() && !webaudio_source_.get())
221 if (webaudio_source_.get()) {
222 // Called Stop() on the |webaudio_source_| explicitly so that
223 // |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_|
225 // in such case and no need to call RemoveTrack().
226 webaudio_source_->Stop();
228 // It is necessary to call RemoveTrack on the |capturer_| to avoid getting
229 // audio callback after Stop().
230 capturer_->RemoveTrack(this);
233 // Protect the pointers using the lock when accessing |sinks_| and
234 // setting the |capturer_| to NULL.
235 SinkList::ItemList sinks;
237 base::AutoLock auto_lock(lock_);
238 sinks = sinks_.Items();
240 webaudio_source_ = NULL;
244 for (SinkList::ItemList::const_iterator it = sinks.begin();
247 (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded);
252 } // namespace content