1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_coding/include/audio_coding_module.h"
12
13 #include <algorithm>
14 #include <cstdint>
15
16 #include "absl/strings/match.h"
17 #include "absl/strings/string_view.h"
18 #include "api/array_view.h"
19 #include "modules/audio_coding/acm2/acm_receiver.h"
20 #include "modules/audio_coding/acm2/acm_remixing.h"
21 #include "modules/audio_coding/acm2/acm_resampler.h"
22 #include "modules/include/module_common_types.h"
23 #include "modules/include/module_common_types_public.h"
24 #include "rtc_base/buffer.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/logging.h"
27 #include "rtc_base/numerics/safe_conversions.h"
28 #include "rtc_base/synchronization/mutex.h"
29 #include "rtc_base/thread_annotations.h"
30 #include "system_wrappers/include/metrics.h"
31
32 namespace webrtc {
33
34 namespace {
35
36 // Initial size for the buffer in InputBuffer. This matches 6 channels of 10 ms
37 // 48 kHz data.
38 constexpr size_t kInitialInputDataBufferSize = 6 * 480;
39
40 constexpr int32_t kMaxInputSampleRateHz = 192000;
41
42 class AudioCodingModuleImpl final : public AudioCodingModule {
43 public:
44 explicit AudioCodingModuleImpl(const AudioCodingModule::Config& config);
45 ~AudioCodingModuleImpl() override;
46
47 /////////////////////////////////////////
48 // Sender
49 //
50
51 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
52 modifier) override;
53
54 // Register a transport callback which will be
55 // called to deliver the encoded buffers.
56 int RegisterTransportCallback(AudioPacketizationCallback* transport) override;
57
58 // Add 10 ms of raw (PCM) audio data to the encoder.
59 int Add10MsData(const AudioFrame& audio_frame) override;
60
61 /////////////////////////////////////////
62 // (FEC) Forward Error Correction (codec internal)
63 //
64
65 // Set target packet loss rate
66 int SetPacketLossRate(int loss_rate) override;
67
68 /////////////////////////////////////////
69 // Receiver
70 //
71
72 // Initialize receiver, resets codec database etc.
73 int InitializeReceiver() override;
74
75 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
76
77 // Incoming packet from network parsed and ready for decode.
78 int IncomingPacket(const uint8_t* incoming_payload,
79 const size_t payload_length,
80 const RTPHeader& rtp_info) override;
81
82 // Get 10 milliseconds of raw audio data to play out, and
83 // automatic resample to the requested frequency if > 0.
84 int PlayoutData10Ms(int desired_freq_hz,
85 AudioFrame* audio_frame,
86 bool* muted) override;
87
88 /////////////////////////////////////////
89 // Statistics
90 //
91
92 int GetNetworkStatistics(NetworkStatistics* statistics) override;
93
94 ANAStats GetANAStats() const override;
95
96 int GetTargetBitrate() const override;
97
98 private:
99 struct InputData {
InputDatawebrtc::__anon885b20090111::AudioCodingModuleImpl::InputData100 InputData() : buffer(kInitialInputDataBufferSize) {}
101 uint32_t input_timestamp;
102 const int16_t* audio;
103 size_t length_per_channel;
104 size_t audio_channel;
105 // If a re-mix is required (up or down), this buffer will store a re-mixed
106 // version of the input.
107 std::vector<int16_t> buffer;
108 };
109
110 InputData input_data_ RTC_GUARDED_BY(acm_mutex_);
111
112 // This member class writes values to the named UMA histogram, but only if
113 // the value has changed since the last time (and always for the first call).
114 class ChangeLogger {
115 public:
ChangeLogger(absl::string_view histogram_name)116 explicit ChangeLogger(absl::string_view histogram_name)
117 : histogram_name_(histogram_name) {}
118 // Logs the new value if it is different from the last logged value, or if
119 // this is the first call.
120 void MaybeLog(int value);
121
122 private:
123 int last_value_ = 0;
124 int first_time_ = true;
125 const std::string histogram_name_;
126 };
127
128 int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data)
129 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
130
131 // TODO(bugs.webrtc.org/10739): change `absolute_capture_timestamp_ms` to
132 // int64_t when it always receives a valid value.
133 int Encode(const InputData& input_data,
134 absl::optional<int64_t> absolute_capture_timestamp_ms)
135 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
136
137 int InitializeReceiverSafe() RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
138
139 bool HaveValidEncoder(absl::string_view caller_name) const
140 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
141
142 // Preprocessing of input audio, including resampling and down-mixing if
143 // required, before pushing audio into encoder's buffer.
144 //
145 // in_frame: input audio-frame
146 // ptr_out: pointer to output audio_frame. If no preprocessing is required
147 // `ptr_out` will be pointing to `in_frame`, otherwise pointing to
148 // `preprocess_frame_`.
149 //
150 // Return value:
151 // -1: if encountering an error.
152 // 0: otherwise.
153 int PreprocessToAddData(const AudioFrame& in_frame,
154 const AudioFrame** ptr_out)
155 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
156
157 // Change required states after starting to receive the codec corresponding
158 // to `index`.
159 int UpdateUponReceivingCodec(int index);
160
161 mutable Mutex acm_mutex_;
162 rtc::Buffer encode_buffer_ RTC_GUARDED_BY(acm_mutex_);
163 uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_mutex_);
164 uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_mutex_);
165 acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_mutex_);
166 acm2::AcmReceiver receiver_; // AcmReceiver has it's own internal lock.
167 ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_mutex_);
168
169 // Current encoder stack, provided by a call to RegisterEncoder.
170 std::unique_ptr<AudioEncoder> encoder_stack_ RTC_GUARDED_BY(acm_mutex_);
171
172 // This is to keep track of CN instances where we can send DTMFs.
173 uint8_t previous_pltype_ RTC_GUARDED_BY(acm_mutex_);
174
175 bool receiver_initialized_ RTC_GUARDED_BY(acm_mutex_);
176
177 AudioFrame preprocess_frame_ RTC_GUARDED_BY(acm_mutex_);
178 bool first_10ms_data_ RTC_GUARDED_BY(acm_mutex_);
179
180 bool first_frame_ RTC_GUARDED_BY(acm_mutex_);
181 uint32_t last_timestamp_ RTC_GUARDED_BY(acm_mutex_);
182 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(acm_mutex_);
183
184 Mutex callback_mutex_;
185 AudioPacketizationCallback* packetization_callback_
186 RTC_GUARDED_BY(callback_mutex_);
187
188 int codec_histogram_bins_log_[static_cast<size_t>(
189 AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)];
190 int number_of_consecutive_empty_packets_;
191 };
192
193 // Adds a codec usage sample to the histogram.
UpdateCodecTypeHistogram(size_t codec_type)194 void UpdateCodecTypeHistogram(size_t codec_type) {
195 RTC_HISTOGRAM_ENUMERATION(
196 "WebRTC.Audio.Encoder.CodecType", static_cast<int>(codec_type),
197 static_cast<int>(
198 webrtc::AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes));
199 }
200
MaybeLog(int value)201 void AudioCodingModuleImpl::ChangeLogger::MaybeLog(int value) {
202 if (value != last_value_ || first_time_) {
203 first_time_ = false;
204 last_value_ = value;
205 RTC_HISTOGRAM_COUNTS_SPARSE_100(histogram_name_, value);
206 }
207 }
208
AudioCodingModuleImpl(const AudioCodingModule::Config & config)209 AudioCodingModuleImpl::AudioCodingModuleImpl(
210 const AudioCodingModule::Config& config)
211 : expected_codec_ts_(0xD87F3F9F),
212 expected_in_ts_(0xD87F3F9F),
213 receiver_(config),
214 bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"),
215 encoder_stack_(nullptr),
216 previous_pltype_(255),
217 receiver_initialized_(false),
218 first_10ms_data_(false),
219 first_frame_(true),
220 packetization_callback_(NULL),
221 codec_histogram_bins_log_(),
222 number_of_consecutive_empty_packets_(0) {
223 if (InitializeReceiverSafe() < 0) {
224 RTC_LOG(LS_ERROR) << "Cannot initialize receiver";
225 }
226 RTC_LOG(LS_INFO) << "Created";
227 }
228
229 AudioCodingModuleImpl::~AudioCodingModuleImpl() = default;
230
Encode(const InputData & input_data,absl::optional<int64_t> absolute_capture_timestamp_ms)231 int32_t AudioCodingModuleImpl::Encode(
232 const InputData& input_data,
233 absl::optional<int64_t> absolute_capture_timestamp_ms) {
234 // TODO(bugs.webrtc.org/10739): add dcheck that
235 // `audio_frame.absolute_capture_timestamp_ms()` always has a value.
236 AudioEncoder::EncodedInfo encoded_info;
237 uint8_t previous_pltype;
238
239 // Check if there is an encoder before.
240 if (!HaveValidEncoder("Process"))
241 return -1;
242
243 if (!first_frame_) {
244 RTC_DCHECK(IsNewerTimestamp(input_data.input_timestamp, last_timestamp_))
245 << "Time should not move backwards";
246 }
247
248 // Scale the timestamp to the codec's RTP timestamp rate.
249 uint32_t rtp_timestamp =
250 first_frame_
251 ? input_data.input_timestamp
252 : last_rtp_timestamp_ +
253 rtc::dchecked_cast<uint32_t>(rtc::CheckedDivExact(
254 int64_t{input_data.input_timestamp - last_timestamp_} *
255 encoder_stack_->RtpTimestampRateHz(),
256 int64_t{encoder_stack_->SampleRateHz()}));
257
258 last_timestamp_ = input_data.input_timestamp;
259 last_rtp_timestamp_ = rtp_timestamp;
260 first_frame_ = false;
261
262 // Clear the buffer before reuse - encoded data will get appended.
263 encode_buffer_.Clear();
264 encoded_info = encoder_stack_->Encode(
265 rtp_timestamp,
266 rtc::ArrayView<const int16_t>(
267 input_data.audio,
268 input_data.audio_channel * input_data.length_per_channel),
269 &encode_buffer_);
270
271 bitrate_logger_.MaybeLog(encoder_stack_->GetTargetBitrate() / 1000);
272 if (encode_buffer_.size() == 0 && !encoded_info.send_even_if_empty) {
273 // Not enough data.
274 return 0;
275 }
276 previous_pltype = previous_pltype_; // Read it while we have the critsect.
277
278 // Log codec type to histogram once every 500 packets.
279 if (encoded_info.encoded_bytes == 0) {
280 ++number_of_consecutive_empty_packets_;
281 } else {
282 size_t codec_type = static_cast<size_t>(encoded_info.encoder_type);
283 codec_histogram_bins_log_[codec_type] +=
284 number_of_consecutive_empty_packets_ + 1;
285 number_of_consecutive_empty_packets_ = 0;
286 if (codec_histogram_bins_log_[codec_type] >= 500) {
287 codec_histogram_bins_log_[codec_type] -= 500;
288 UpdateCodecTypeHistogram(codec_type);
289 }
290 }
291
292 AudioFrameType frame_type;
293 if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) {
294 frame_type = AudioFrameType::kEmptyFrame;
295 encoded_info.payload_type = previous_pltype;
296 } else {
297 RTC_DCHECK_GT(encode_buffer_.size(), 0);
298 frame_type = encoded_info.speech ? AudioFrameType::kAudioFrameSpeech
299 : AudioFrameType::kAudioFrameCN;
300 }
301
302 {
303 MutexLock lock(&callback_mutex_);
304 if (packetization_callback_) {
305 packetization_callback_->SendData(
306 frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp,
307 encode_buffer_.data(), encode_buffer_.size(),
308 absolute_capture_timestamp_ms.value_or(-1));
309 }
310 }
311 previous_pltype_ = encoded_info.payload_type;
312 return static_cast<int32_t>(encode_buffer_.size());
313 }
314
315 /////////////////////////////////////////
316 // Sender
317 //
318
ModifyEncoder(rtc::FunctionView<void (std::unique_ptr<AudioEncoder> *)> modifier)319 void AudioCodingModuleImpl::ModifyEncoder(
320 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
321 MutexLock lock(&acm_mutex_);
322 modifier(&encoder_stack_);
323 }
324
325 // Register a transport callback which will be called to deliver
326 // the encoded buffers.
RegisterTransportCallback(AudioPacketizationCallback * transport)327 int AudioCodingModuleImpl::RegisterTransportCallback(
328 AudioPacketizationCallback* transport) {
329 MutexLock lock(&callback_mutex_);
330 packetization_callback_ = transport;
331 return 0;
332 }
333
334 // Add 10MS of raw (PCM) audio data to the encoder.
Add10MsData(const AudioFrame & audio_frame)335 int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) {
336 MutexLock lock(&acm_mutex_);
337 int r = Add10MsDataInternal(audio_frame, &input_data_);
338 // TODO(bugs.webrtc.org/10739): add dcheck that
339 // `audio_frame.absolute_capture_timestamp_ms()` always has a value.
340 return r < 0
341 ? r
342 : Encode(input_data_, audio_frame.absolute_capture_timestamp_ms());
343 }
344
Add10MsDataInternal(const AudioFrame & audio_frame,InputData * input_data)345 int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame,
346 InputData* input_data) {
347 if (audio_frame.samples_per_channel_ == 0) {
348 RTC_DCHECK_NOTREACHED();
349 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, payload length is zero";
350 return -1;
351 }
352
353 if (audio_frame.sample_rate_hz_ > kMaxInputSampleRateHz) {
354 RTC_DCHECK_NOTREACHED();
355 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, input frequency not valid";
356 return -1;
357 }
358
359 // If the length and frequency matches. We currently just support raw PCM.
360 if (static_cast<size_t>(audio_frame.sample_rate_hz_ / 100) !=
361 audio_frame.samples_per_channel_) {
362 RTC_LOG(LS_ERROR)
363 << "Cannot Add 10 ms audio, input frequency and length doesn't match";
364 return -1;
365 }
366
367 if (audio_frame.num_channels_ != 1 && audio_frame.num_channels_ != 2 &&
368 audio_frame.num_channels_ != 4 && audio_frame.num_channels_ != 6 &&
369 audio_frame.num_channels_ != 8) {
370 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, invalid number of channels.";
371 return -1;
372 }
373
374 // Do we have a codec registered?
375 if (!HaveValidEncoder("Add10MsData")) {
376 return -1;
377 }
378
379 const AudioFrame* ptr_frame;
380 // Perform a resampling, also down-mix if it is required and can be
381 // performed before resampling (a down mix prior to resampling will take
382 // place if both primary and secondary encoders are mono and input is in
383 // stereo).
384 if (PreprocessToAddData(audio_frame, &ptr_frame) < 0) {
385 return -1;
386 }
387
388 // Check whether we need an up-mix or down-mix?
389 const size_t current_num_channels = encoder_stack_->NumChannels();
390 const bool same_num_channels =
391 ptr_frame->num_channels_ == current_num_channels;
392
393 // TODO(yujo): Skip encode of muted frames.
394 input_data->input_timestamp = ptr_frame->timestamp_;
395 input_data->length_per_channel = ptr_frame->samples_per_channel_;
396 input_data->audio_channel = current_num_channels;
397
398 if (!same_num_channels) {
399 // Remixes the input frame to the output data and in the process resize the
400 // output data if needed.
401 ReMixFrame(*ptr_frame, current_num_channels, &input_data->buffer);
402
403 // For pushing data to primary, point the `ptr_audio` to correct buffer.
404 input_data->audio = input_data->buffer.data();
405 RTC_DCHECK_GE(input_data->buffer.size(),
406 input_data->length_per_channel * input_data->audio_channel);
407 } else {
408 // When adding data to encoders this pointer is pointing to an audio buffer
409 // with correct number of channels.
410 input_data->audio = ptr_frame->data();
411 }
412
413 return 0;
414 }
415
416 // Perform a resampling and down-mix if required. We down-mix only if
417 // encoder is mono and input is stereo. In case of dual-streaming, both
418 // encoders has to be mono for down-mix to take place.
419 // |*ptr_out| will point to the pre-processed audio-frame. If no pre-processing
420 // is required, |*ptr_out| points to `in_frame`.
421 // TODO(yujo): Make this more efficient for muted frames.
PreprocessToAddData(const AudioFrame & in_frame,const AudioFrame ** ptr_out)422 int AudioCodingModuleImpl::PreprocessToAddData(const AudioFrame& in_frame,
423 const AudioFrame** ptr_out) {
424 const bool resample =
425 in_frame.sample_rate_hz_ != encoder_stack_->SampleRateHz();
426
427 // This variable is true if primary codec and secondary codec (if exists)
428 // are both mono and input is stereo.
429 // TODO(henrik.lundin): This condition should probably be
430 // in_frame.num_channels_ > encoder_stack_->NumChannels()
431 const bool down_mix =
432 in_frame.num_channels_ == 2 && encoder_stack_->NumChannels() == 1;
433
434 if (!first_10ms_data_) {
435 expected_in_ts_ = in_frame.timestamp_;
436 expected_codec_ts_ = in_frame.timestamp_;
437 first_10ms_data_ = true;
438 } else if (in_frame.timestamp_ != expected_in_ts_) {
439 RTC_LOG(LS_WARNING) << "Unexpected input timestamp: " << in_frame.timestamp_
440 << ", expected: " << expected_in_ts_;
441 expected_codec_ts_ +=
442 (in_frame.timestamp_ - expected_in_ts_) *
443 static_cast<uint32_t>(
444 static_cast<double>(encoder_stack_->SampleRateHz()) /
445 static_cast<double>(in_frame.sample_rate_hz_));
446 expected_in_ts_ = in_frame.timestamp_;
447 }
448
449 if (!down_mix && !resample) {
450 // No pre-processing is required.
451 if (expected_in_ts_ == expected_codec_ts_) {
452 // If we've never resampled, we can use the input frame as-is
453 *ptr_out = &in_frame;
454 } else {
455 // Otherwise we'll need to alter the timestamp. Since in_frame is const,
456 // we'll have to make a copy of it.
457 preprocess_frame_.CopyFrom(in_frame);
458 preprocess_frame_.timestamp_ = expected_codec_ts_;
459 *ptr_out = &preprocess_frame_;
460 }
461
462 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
463 expected_codec_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
464 return 0;
465 }
466
467 *ptr_out = &preprocess_frame_;
468 preprocess_frame_.num_channels_ = in_frame.num_channels_;
469 preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_;
470 std::array<int16_t, AudioFrame::kMaxDataSizeSamples> audio;
471 const int16_t* src_ptr_audio;
472 if (down_mix) {
473 // If a resampling is required, the output of a down-mix is written into a
474 // local buffer, otherwise, it will be written to the output frame.
475 int16_t* dest_ptr_audio =
476 resample ? audio.data() : preprocess_frame_.mutable_data();
477 RTC_DCHECK_GE(audio.size(), preprocess_frame_.samples_per_channel_);
478 RTC_DCHECK_GE(audio.size(), in_frame.samples_per_channel_);
479 DownMixFrame(in_frame,
480 rtc::ArrayView<int16_t>(
481 dest_ptr_audio, preprocess_frame_.samples_per_channel_));
482 preprocess_frame_.num_channels_ = 1;
483
484 // Set the input of the resampler to the down-mixed signal.
485 src_ptr_audio = audio.data();
486 } else {
487 // Set the input of the resampler to the original data.
488 src_ptr_audio = in_frame.data();
489 }
490
491 preprocess_frame_.timestamp_ = expected_codec_ts_;
492 preprocess_frame_.sample_rate_hz_ = in_frame.sample_rate_hz_;
493 // If it is required, we have to do a resampling.
494 if (resample) {
495 // The result of the resampler is written to output frame.
496 int16_t* dest_ptr_audio = preprocess_frame_.mutable_data();
497
498 int samples_per_channel = resampler_.Resample10Msec(
499 src_ptr_audio, in_frame.sample_rate_hz_, encoder_stack_->SampleRateHz(),
500 preprocess_frame_.num_channels_, AudioFrame::kMaxDataSizeSamples,
501 dest_ptr_audio);
502
503 if (samples_per_channel < 0) {
504 RTC_LOG(LS_ERROR) << "Cannot add 10 ms audio, resampling failed";
505 return -1;
506 }
507 preprocess_frame_.samples_per_channel_ =
508 static_cast<size_t>(samples_per_channel);
509 preprocess_frame_.sample_rate_hz_ = encoder_stack_->SampleRateHz();
510 }
511
512 expected_codec_ts_ +=
513 static_cast<uint32_t>(preprocess_frame_.samples_per_channel_);
514 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
515
516 return 0;
517 }
518
519 /////////////////////////////////////////
520 // (FEC) Forward Error Correction (codec internal)
521 //
522
SetPacketLossRate(int loss_rate)523 int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) {
524 MutexLock lock(&acm_mutex_);
525 if (HaveValidEncoder("SetPacketLossRate")) {
526 encoder_stack_->OnReceivedUplinkPacketLossFraction(loss_rate / 100.0);
527 }
528 return 0;
529 }
530
531 /////////////////////////////////////////
532 // Receiver
533 //
534
InitializeReceiver()535 int AudioCodingModuleImpl::InitializeReceiver() {
536 MutexLock lock(&acm_mutex_);
537 return InitializeReceiverSafe();
538 }
539
540 // Initialize receiver, resets codec database etc.
InitializeReceiverSafe()541 int AudioCodingModuleImpl::InitializeReceiverSafe() {
542 // If the receiver is already initialized then we want to destroy any
543 // existing decoders. After a call to this function, we should have a clean
544 // start-up.
545 if (receiver_initialized_)
546 receiver_.RemoveAllCodecs();
547 receiver_.FlushBuffers();
548
549 receiver_initialized_ = true;
550 return 0;
551 }
552
SetReceiveCodecs(const std::map<int,SdpAudioFormat> & codecs)553 void AudioCodingModuleImpl::SetReceiveCodecs(
554 const std::map<int, SdpAudioFormat>& codecs) {
555 MutexLock lock(&acm_mutex_);
556 receiver_.SetCodecs(codecs);
557 }
558
559 // Incoming packet from network parsed and ready for decode.
IncomingPacket(const uint8_t * incoming_payload,const size_t payload_length,const RTPHeader & rtp_header)560 int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload,
561 const size_t payload_length,
562 const RTPHeader& rtp_header) {
563 RTC_DCHECK_EQ(payload_length == 0, incoming_payload == nullptr);
564 return receiver_.InsertPacket(
565 rtp_header,
566 rtc::ArrayView<const uint8_t>(incoming_payload, payload_length));
567 }
568
569 // Get 10 milliseconds of raw audio data to play out.
570 // Automatic resample to the requested frequency.
PlayoutData10Ms(int desired_freq_hz,AudioFrame * audio_frame,bool * muted)571 int AudioCodingModuleImpl::PlayoutData10Ms(int desired_freq_hz,
572 AudioFrame* audio_frame,
573 bool* muted) {
574 // GetAudio always returns 10 ms, at the requested sample rate.
575 if (receiver_.GetAudio(desired_freq_hz, audio_frame, muted) != 0) {
576 RTC_LOG(LS_ERROR) << "PlayoutData failed, RecOut Failed";
577 return -1;
578 }
579 return 0;
580 }
581
582 /////////////////////////////////////////
583 // Statistics
584 //
585
586 // TODO(turajs) change the return value to void. Also change the corresponding
587 // NetEq function.
GetNetworkStatistics(NetworkStatistics * statistics)588 int AudioCodingModuleImpl::GetNetworkStatistics(NetworkStatistics* statistics) {
589 receiver_.GetNetworkStatistics(statistics);
590 return 0;
591 }
592
HaveValidEncoder(absl::string_view caller_name) const593 bool AudioCodingModuleImpl::HaveValidEncoder(
594 absl::string_view caller_name) const {
595 if (!encoder_stack_) {
596 RTC_LOG(LS_ERROR) << caller_name << " failed: No send codec is registered.";
597 return false;
598 }
599 return true;
600 }
601
GetANAStats() const602 ANAStats AudioCodingModuleImpl::GetANAStats() const {
603 MutexLock lock(&acm_mutex_);
604 if (encoder_stack_)
605 return encoder_stack_->GetANAStats();
606 // If no encoder is set, return default stats.
607 return ANAStats();
608 }
609
GetTargetBitrate() const610 int AudioCodingModuleImpl::GetTargetBitrate() const {
611 MutexLock lock(&acm_mutex_);
612 if (!encoder_stack_) {
613 return -1;
614 }
615 return encoder_stack_->GetTargetBitrate();
616 }
617
618 } // namespace
619
Config(rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)620 AudioCodingModule::Config::Config(
621 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
622 : neteq_config(),
623 clock(Clock::GetRealTimeClock()),
624 decoder_factory(decoder_factory) {
625 // Post-decode VAD is disabled by default in NetEq, however, Audio
626 // Conference Mixer relies on VAD decisions and fails without them.
627 neteq_config.enable_post_decode_vad = true;
628 }
629
630 AudioCodingModule::Config::Config(const Config&) = default;
631 AudioCodingModule::Config::~Config() = default;
632
Create(const Config & config)633 AudioCodingModule* AudioCodingModule::Create(const Config& config) {
634 return new AudioCodingModuleImpl(config);
635 }
636
637 } // namespace webrtc
638