1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/media_stream_audio_processor.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "content/public/common/content_switches.h"
#include "content/renderer/media/media_stream_audio_processor_options.h"
#include "content/renderer/media/rtc_media_constraints.h"
#include "media/audio/audio_parameters.h"
#include "media/base/audio_converter.h"
#include "media/base/audio_fifo.h"
#include "media/base/channel_layout.h"
#include "third_party/WebKit/public/platform/WebMediaConstraints.h"
#include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
namespace content {
namespace {
using webrtc::AudioProcessing;
using webrtc::MediaConstraintsInterface;
#if defined(ANDROID)
const int kAudioProcessingSampleRate = 16000;
#else
const int kAudioProcessingSampleRate = 32000;
#endif
const int kAudioProcessingNumberOfChannel = 1;
const int kMaxNumberOfBuffersInFifo = 2;
} // namespace
class MediaStreamAudioProcessor::MediaStreamAudioConverter
: public media::AudioConverter::InputCallback {
public:
MediaStreamAudioConverter(const media::AudioParameters& source_params,
const media::AudioParameters& sink_params)
: source_params_(source_params),
sink_params_(sink_params),
audio_converter_(source_params, sink_params_, false) {
// An instance of MediaStreamAudioConverter may be created in the main
// render thread and used in the audio thread, for example, the
// |MediaStreamAudioProcessor::capture_converter_|.
thread_checker_.DetachFromThread();
audio_converter_.AddInput(this);
// Create and initialize audio fifo and audio bus wrapper.
// The size of the FIFO should be at least twice of the source buffer size
// or twice of the sink buffer size.
int buffer_size = std::max(
kMaxNumberOfBuffersInFifo * source_params_.frames_per_buffer(),
kMaxNumberOfBuffersInFifo * sink_params_.frames_per_buffer());
fifo_.reset(new media::AudioFifo(source_params_.channels(), buffer_size));
// TODO(xians): Use CreateWrapper to save one memcpy.
audio_wrapper_ = media::AudioBus::Create(sink_params_.channels(),
sink_params_.frames_per_buffer());
}
virtual ~MediaStreamAudioConverter() {
audio_converter_.RemoveInput(this);
}
void Push(media::AudioBus* audio_source) {
// Called on the audio thread, which is the capture audio thread for
// |MediaStreamAudioProcessor::capture_converter_|, and render audio thread
// for |MediaStreamAudioProcessor::render_converter_|.
// And it must be the same thread as calling Convert().
DCHECK(thread_checker_.CalledOnValidThread());
fifo_->Push(audio_source);
}
bool Convert(webrtc::AudioFrame* out) {
// Called on the audio thread, which is the capture audio thread for
// |MediaStreamAudioProcessor::capture_converter_|, and render audio thread
// for |MediaStreamAudioProcessor::render_converter_|.
DCHECK(thread_checker_.CalledOnValidThread());
// Return false if there is not enough data in the FIFO, this happens when
// fifo_->frames() / source_params_.sample_rate() is less than
// sink_params.frames_per_buffer() / sink_params.sample_rate().
if (fifo_->frames() * sink_params_.sample_rate() <
sink_params_.frames_per_buffer() * source_params_.sample_rate()) {
return false;
}
// Convert data to the output format, this will trigger ProvideInput().
audio_converter_.Convert(audio_wrapper_.get());
// TODO(xians): Figure out a better way to handle the interleaved and
// deinterleaved format switching.
DCHECK_EQ(audio_wrapper_->frames(), sink_params_.frames_per_buffer());
audio_wrapper_->ToInterleaved(audio_wrapper_->frames(),
sink_params_.bits_per_sample() / 8,
out->data_);
out->samples_per_channel_ = sink_params_.frames_per_buffer();
out->sample_rate_hz_ = sink_params_.sample_rate();
out->speech_type_ = webrtc::AudioFrame::kNormalSpeech;
out->vad_activity_ = webrtc::AudioFrame::kVadUnknown;
out->num_channels_ = sink_params_.channels();
return true;
}
const media::AudioParameters& source_parameters() const {
return source_params_;
}
const media::AudioParameters& sink_parameters() const {
return sink_params_;
}
private:
// AudioConverter::InputCallback implementation.
virtual double ProvideInput(media::AudioBus* audio_bus,
base::TimeDelta buffer_delay) OVERRIDE {
// Called on realtime audio thread.
// TODO(xians): Figure out why the first Convert() triggers ProvideInput
// two times.
if (fifo_->frames() < audio_bus->frames())
return 0;
fifo_->Consume(audio_bus, 0, audio_bus->frames());
// Return 1.0 to indicate no volume scaling on the data.
return 1.0;
}
base::ThreadChecker thread_checker_;
const media::AudioParameters source_params_;
const media::AudioParameters sink_params_;
// TODO(xians): consider using SincResampler to save some memcpy.
// Handles mixing and resampling between input and output parameters.
media::AudioConverter audio_converter_;
scoped_ptr<media::AudioBus> audio_wrapper_;
scoped_ptr<media::AudioFifo> fifo_;
};
MediaStreamAudioProcessor::MediaStreamAudioProcessor(
const media::AudioParameters& source_params,
const blink::WebMediaConstraints& constraints,
int effects)
: render_delay_ms_(0) {
capture_thread_checker_.DetachFromThread();
render_thread_checker_.DetachFromThread();
InitializeAudioProcessingModule(constraints, effects);
InitializeCaptureConverter(source_params);
}
MediaStreamAudioProcessor::~MediaStreamAudioProcessor() {
DCHECK(main_thread_checker_.CalledOnValidThread());
StopAudioProcessing();
}
void MediaStreamAudioProcessor::PushCaptureData(media::AudioBus* audio_source) {
DCHECK(capture_thread_checker_.CalledOnValidThread());
capture_converter_->Push(audio_source);
}
void MediaStreamAudioProcessor::PushRenderData(
const int16* render_audio, int sample_rate, int number_of_channels,
int number_of_frames, base::TimeDelta render_delay) {
DCHECK(render_thread_checker_.CalledOnValidThread());
// Return immediately if the echo cancellation is off.
if (!audio_processing_ ||
!audio_processing_->echo_cancellation()->is_enabled()) {
return;
}
TRACE_EVENT0("audio",
"MediaStreamAudioProcessor::FeedRenderDataToAudioProcessing");
int64 new_render_delay_ms = render_delay.InMilliseconds();
DCHECK_LT(new_render_delay_ms,
std::numeric_limits<base::subtle::Atomic32>::max());
base::subtle::Release_Store(&render_delay_ms_, new_render_delay_ms);
InitializeRenderConverterIfNeeded(sample_rate, number_of_channels,
number_of_frames);
// TODO(xians): Avoid this extra interleave/deinterleave.
render_data_bus_->FromInterleaved(render_audio,
render_data_bus_->frames(),
sizeof(render_audio[0]));
render_converter_->Push(render_data_bus_.get());
while (render_converter_->Convert(&render_frame_))
audio_processing_->AnalyzeReverseStream(&render_frame_);
}
bool MediaStreamAudioProcessor::ProcessAndConsumeData(
base::TimeDelta capture_delay, int volume, bool key_pressed,
int16** out) {
DCHECK(capture_thread_checker_.CalledOnValidThread());
TRACE_EVENT0("audio",
"MediaStreamAudioProcessor::ProcessAndConsumeData");
if (!capture_converter_->Convert(&capture_frame_))
return false;
ProcessData(&capture_frame_, capture_delay, volume, key_pressed);
*out = capture_frame_.data_;
return true;
}
const media::AudioParameters& MediaStreamAudioProcessor::InputFormat() const {
return capture_converter_->source_parameters();
}
const media::AudioParameters& MediaStreamAudioProcessor::OutputFormat() const {
return capture_converter_->sink_parameters();
}
void MediaStreamAudioProcessor::InitializeAudioProcessingModule(
const blink::WebMediaConstraints& constraints, int effects) {
DCHECK(!audio_processing_);
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableAudioTrackProcessing)) {
return;
}
RTCMediaConstraints native_constraints(constraints);
ApplyFixedAudioConstraints(&native_constraints);
if (effects & media::AudioParameters::ECHO_CANCELLER) {
// If platform echo cancellator is enabled, disable the software AEC.
native_constraints.AddMandatory(
MediaConstraintsInterface::kEchoCancellation,
MediaConstraintsInterface::kValueFalse, true);
}
const bool enable_aec = GetPropertyFromConstraints(
&native_constraints, MediaConstraintsInterface::kEchoCancellation);
const bool enable_ns = GetPropertyFromConstraints(
&native_constraints, MediaConstraintsInterface::kNoiseSuppression);
const bool enable_high_pass_filter = GetPropertyFromConstraints(
&native_constraints, MediaConstraintsInterface::kHighpassFilter);
#if defined(IOS) || defined(ANDROID)
const bool enable_experimental_aec = false;
const bool enable_typing_detection = false;
#else
const bool enable_experimental_aec = GetPropertyFromConstraints(
&native_constraints,
MediaConstraintsInterface::kExperimentalEchoCancellation);
const bool enable_typing_detection = GetPropertyFromConstraints(
&native_constraints, MediaConstraintsInterface::kTypingNoiseDetection);
#endif
// Return immediately if no audio processing component is enabled.
if (!enable_aec && !enable_experimental_aec && !enable_ns &&
!enable_high_pass_filter && !enable_typing_detection) {
return;
}
// Create and configure the webrtc::AudioProcessing.
audio_processing_.reset(webrtc::AudioProcessing::Create(0));
// Enable the audio processing components.
if (enable_aec) {
EnableEchoCancellation(audio_processing_.get());
if (enable_experimental_aec)
EnableExperimentalEchoCancellation(audio_processing_.get());
}
if (enable_ns)
EnableNoiseSuppression(audio_processing_.get());
if (enable_high_pass_filter)
EnableHighPassFilter(audio_processing_.get());
if (enable_typing_detection)
EnableTypingDetection(audio_processing_.get());
// Configure the audio format the audio processing is running on. This
// has to be done after all the needed components are enabled.
CHECK_EQ(audio_processing_->set_sample_rate_hz(kAudioProcessingSampleRate),
0);
CHECK_EQ(audio_processing_->set_num_channels(kAudioProcessingNumberOfChannel,
kAudioProcessingNumberOfChannel),
0);
}
void MediaStreamAudioProcessor::InitializeCaptureConverter(
const media::AudioParameters& source_params) {
DCHECK(source_params.IsValid());
// Create and initialize audio converter for the source data.
// When the webrtc AudioProcessing is enabled, the sink format of the
// converter will be the same as the post-processed data format, which is
// 32k mono for desktops and 16k mono for Android. When the AudioProcessing
// is disabled, the sink format will be the same as the source format.
const int sink_sample_rate = audio_processing_ ?
kAudioProcessingSampleRate : source_params.sample_rate();
const media::ChannelLayout sink_channel_layout = audio_processing_ ?
media::CHANNEL_LAYOUT_MONO : source_params.channel_layout();
// WebRtc AudioProcessing requires 10ms as its packet size. We use this
// native size when processing is enabled. While processing is disabled, and
// the source is running with a buffer size smaller than 10ms buffer, we use
// same buffer size as the incoming format to avoid extra FIFO for WebAudio.
int sink_buffer_size = sink_sample_rate / 100;
if (!audio_processing_ &&
source_params.frames_per_buffer() < sink_buffer_size) {
sink_buffer_size = source_params.frames_per_buffer();
}
media::AudioParameters sink_params(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY, sink_channel_layout,
sink_sample_rate, 16, sink_buffer_size);
capture_converter_.reset(
new MediaStreamAudioConverter(source_params, sink_params));
}
void MediaStreamAudioProcessor::InitializeRenderConverterIfNeeded(
int sample_rate, int number_of_channels, int frames_per_buffer) {
DCHECK(render_thread_checker_.CalledOnValidThread());
// TODO(xians): Figure out if we need to handle the buffer size change.
if (render_converter_.get() &&
render_converter_->source_parameters().sample_rate() == sample_rate &&
render_converter_->source_parameters().channels() == number_of_channels) {
// Do nothing if the |render_converter_| has been setup properly.
return;
}
// Create and initialize audio converter for the render data.
// webrtc::AudioProcessing accepts the same format as what it uses to process
// capture data, which is 32k mono for desktops and 16k mono for Android.
media::AudioParameters source_params(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::GuessChannelLayout(number_of_channels), sample_rate, 16,
frames_per_buffer);
media::AudioParameters sink_params(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::CHANNEL_LAYOUT_MONO, kAudioProcessingSampleRate, 16,
kAudioProcessingSampleRate / 100);
render_converter_.reset(
new MediaStreamAudioConverter(source_params, sink_params));
render_data_bus_ = media::AudioBus::Create(number_of_channels,
frames_per_buffer);
}
void MediaStreamAudioProcessor::ProcessData(webrtc::AudioFrame* audio_frame,
base::TimeDelta capture_delay,
int volume,
bool key_pressed) {
DCHECK(capture_thread_checker_.CalledOnValidThread());
if (!audio_processing_)
return;
TRACE_EVENT0("audio", "MediaStreamAudioProcessor::Process10MsData");
DCHECK_EQ(audio_processing_->sample_rate_hz(),
capture_converter_->sink_parameters().sample_rate());
DCHECK_EQ(audio_processing_->num_input_channels(),
capture_converter_->sink_parameters().channels());
DCHECK_EQ(audio_processing_->num_output_channels(),
capture_converter_->sink_parameters().channels());
base::subtle::Atomic32 render_delay_ms =
base::subtle::Acquire_Load(&render_delay_ms_);
int64 capture_delay_ms = capture_delay.InMilliseconds();
DCHECK_LT(capture_delay_ms,
std::numeric_limits<base::subtle::Atomic32>::max());
int total_delay_ms = capture_delay_ms + render_delay_ms;
if (total_delay_ms > 1000) {
LOG(WARNING) << "Large audio delay, capture delay: " << capture_delay_ms
<< "ms; render delay: " << render_delay_ms << "ms";
}
audio_processing_->set_stream_delay_ms(total_delay_ms);
webrtc::GainControl* agc = audio_processing_->gain_control();
int err = agc->set_stream_analog_level(volume);
DCHECK_EQ(err, 0) << "set_stream_analog_level() error: " << err;
err = audio_processing_->ProcessStream(audio_frame);
DCHECK_EQ(err, 0) << "ProcessStream() error: " << err;
// TODO(xians): Add support for AGC, typing detection, audio level
// calculation, stereo swapping.
}
void MediaStreamAudioProcessor::StopAudioProcessing() {
if (!audio_processing_.get())
return;
audio_processing_.reset();
}
} // namespace content
|