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