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"
9 #include "content/public/renderer/media_stream_audio_sink.h"
10 #include "content/renderer/media/media_stream_audio_level_calculator.h"
11 #include "content/renderer/media/media_stream_audio_processor.h"
12 #include "content/renderer/media/media_stream_audio_sink_owner.h"
13 #include "content/renderer/media/media_stream_audio_track_sink.h"
14 #include "content/renderer/media/webaudio_capturer_source.h"
15 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
16 #include "content/renderer/media/webrtc_audio_capturer.h"
20 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack(
21 WebRtcLocalAudioTrackAdapter
* adapter
,
22 const scoped_refptr
<WebRtcAudioCapturer
>& capturer
,
23 WebAudioCapturerSource
* webaudio_source
)
24 : MediaStreamTrack(true),
27 webaudio_source_(webaudio_source
) {
28 DCHECK(capturer
.get() || webaudio_source
);
29 signal_thread_checker_
.DetachFromThread();
31 adapter_
->Initialize(this);
33 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()";
36 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() {
37 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
38 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()";
39 // Users might not call Stop() on the track.
43 media::AudioParameters
WebRtcLocalAudioTrack::GetOutputFormat() const {
44 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
45 if (webaudio_source_
.get()) {
46 return media::AudioParameters();
48 return capturer_
->GetOutputFormat();
52 void WebRtcLocalAudioTrack::Capture(const media::AudioBus
& audio_bus
,
53 base::TimeTicks estimated_capture_time
,
54 bool force_report_nonzero_energy
) {
55 DCHECK(capture_thread_checker_
.CalledOnValidThread());
56 DCHECK(!estimated_capture_time
.is_null());
58 // Calculate the signal level regardless of whether the track is disabled or
59 // enabled. If |force_report_nonzero_energy| is true, |audio_bus| contains
60 // post-processed data that may be all zeros even though the signal contained
61 // energy before the processing. In this case, report nonzero energy even if
62 // the energy of the data in |audio_bus| is zero.
63 const float minimum_signal_level
= force_report_nonzero_energy
?
64 1.0f
/ std::numeric_limits
<int16
>::max() : 0.0f
;
65 const float signal_level
= std::max(
67 std::min(1.0f
, level_calculator_
->Calculate(audio_bus
)));
68 const int signal_level_as_pcm16
=
69 static_cast<int>(signal_level
* std::numeric_limits
<int16
>::max() +
70 0.5f
/* rounding to nearest int */);
71 adapter_
->SetSignalLevel(signal_level_as_pcm16
);
73 scoped_refptr
<WebRtcAudioCapturer
> capturer
;
74 SinkList::ItemList sinks
;
75 SinkList::ItemList sinks_to_notify_format
;
77 base::AutoLock
auto_lock(lock_
);
79 sinks
= sinks_
.Items();
80 sinks_
.RetrieveAndClearTags(&sinks_to_notify_format
);
83 // Notify the tracks on when the format changes. This will do nothing if
84 // |sinks_to_notify_format| is empty.
85 for (const auto& sink
: sinks_to_notify_format
)
86 sink
->OnSetFormat(audio_parameters_
);
88 // Feed the data to the sinks.
89 // TODO(jiayl): we should not pass the real audio data down if the track is
90 // disabled. This is currently done so to feed input to WebRTC typing
91 // detection and should be changed when audio processing is moved from
92 // WebRTC to the track.
93 std::vector
<int> voe_channels
= adapter_
->VoeChannels();
94 for (const auto& sink
: sinks
)
95 sink
->OnData(audio_bus
, estimated_capture_time
);
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 // This method is called from webrtc, on the signaling thread, when the local
126 // description is set and from the main thread from WebMediaPlayerMS::load
127 // (via WebRtcLocalAudioRenderer::Start).
128 DCHECK(main_render_thread_checker_
.CalledOnValidThread() ||
129 signal_thread_checker_
.CalledOnValidThread());
130 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
131 base::AutoLock
auto_lock(lock_
);
133 // Verify that |sink| is not already added to the list.
134 DCHECK(!sinks_
.Contains(
135 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink
)));
137 // Create (and add to the list) a new MediaStreamAudioTrackSink
138 // which owns the |sink| and delagates all calls to the
139 // MediaStreamAudioSink interface. It will be tagged in the list, so
140 // we remember to call OnSetFormat() on the new sink.
141 scoped_refptr
<MediaStreamAudioTrackSink
> sink_owner(
142 new MediaStreamAudioSinkOwner(sink
));
143 sinks_
.AddAndTag(sink_owner
.get());
146 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink
* sink
) {
147 // See AddSink for additional context. When local audio is stopped from
148 // webrtc, we'll be called here on the signaling thread.
149 DCHECK(main_render_thread_checker_
.CalledOnValidThread() ||
150 signal_thread_checker_
.CalledOnValidThread());
151 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
153 scoped_refptr
<MediaStreamAudioTrackSink
> removed_item
;
155 base::AutoLock
auto_lock(lock_
);
156 removed_item
= sinks_
.Remove(
157 MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink
));
160 // Clear the delegate to ensure that no more capture callbacks will
161 // be sent to this sink. Also avoids a possible crash which can happen
162 // if this method is called while capturing is active.
163 if (removed_item
.get())
164 removed_item
->Reset();
167 void WebRtcLocalAudioTrack::Start() {
168 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
169 DVLOG(1) << "WebRtcLocalAudioTrack::Start()";
170 if (webaudio_source_
.get()) {
171 // If the track is hooking up with WebAudio, do NOT add the track to the
172 // capturer as its sink otherwise two streams in different clock will be
173 // pushed through the same track.
174 webaudio_source_
->Start(this);
175 } else if (capturer_
.get()) {
176 capturer_
->AddTrack(this);
179 SinkList::ItemList sinks
;
181 base::AutoLock
auto_lock(lock_
);
182 sinks
= sinks_
.Items();
184 for (SinkList::ItemList::const_iterator it
= sinks
.begin();
187 (*it
)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive
);
191 void WebRtcLocalAudioTrack::SetEnabled(bool enabled
) {
192 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
194 adapter_
->set_enabled(enabled
);
197 void WebRtcLocalAudioTrack::Stop() {
198 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
199 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()";
200 if (!capturer_
.get() && !webaudio_source_
.get())
203 if (webaudio_source_
.get()) {
204 // Called Stop() on the |webaudio_source_| explicitly so that
205 // |webaudio_source_| won't push more data to the track anymore.
206 // Also note that the track is not registered as a sink to the |capturer_|
207 // in such case and no need to call RemoveTrack().
208 webaudio_source_
->Stop();
210 // It is necessary to call RemoveTrack on the |capturer_| to avoid getting
211 // audio callback after Stop().
212 capturer_
->RemoveTrack(this);
215 // Protect the pointers using the lock when accessing |sinks_| and
216 // setting the |capturer_| to NULL.
217 SinkList::ItemList sinks
;
219 base::AutoLock
auto_lock(lock_
);
220 sinks
= sinks_
.Items();
222 webaudio_source_
= NULL
;
226 for (SinkList::ItemList::const_iterator it
= sinks
.begin();
229 (*it
)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded
);
234 webrtc::AudioTrackInterface
* WebRtcLocalAudioTrack::GetAudioAdapter() {
235 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
236 return adapter_
.get();
239 } // namespace content