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 "webrtc/modules/audio_coding/acm2/audio_coding_module_impl.h"
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <vector>
16 
17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/safe_conversions.h"
19 #include "webrtc/engine_configurations.h"
20 #include "webrtc/modules/audio_coding/include/audio_coding_module_typedefs.h"
21 #include "webrtc/modules/audio_coding/acm2/acm_common_defs.h"
22 #include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
23 #include "webrtc/modules/audio_coding/acm2/call_statistics.h"
24 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
25 #include "webrtc/system_wrappers/include/logging.h"
26 #include "webrtc/system_wrappers/include/metrics.h"
27 #include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
28 #include "webrtc/system_wrappers/include/trace.h"
29 #include "webrtc/typedefs.h"
30 
31 namespace webrtc {
32 
33 namespace acm2 {
34 
35 namespace {
36 
37 // TODO(turajs): the same functionality is used in NetEq. If both classes
38 // need them, make it a static function in ACMCodecDB.
IsCodecRED(const CodecInst & codec)39 bool IsCodecRED(const CodecInst& codec) {
40   return (STR_CASE_CMP(codec.plname, "RED") == 0);
41 }
42 
IsCodecCN(const CodecInst & codec)43 bool IsCodecCN(const CodecInst& codec) {
44   return (STR_CASE_CMP(codec.plname, "CN") == 0);
45 }
46 
47 // Stereo-to-mono can be used as in-place.
DownMix(const AudioFrame & frame,size_t length_out_buff,int16_t * out_buff)48 int DownMix(const AudioFrame& frame,
49             size_t length_out_buff,
50             int16_t* out_buff) {
51   if (length_out_buff < frame.samples_per_channel_) {
52     return -1;
53   }
54   for (size_t n = 0; n < frame.samples_per_channel_; ++n)
55     out_buff[n] = (frame.data_[2 * n] + frame.data_[2 * n + 1]) >> 1;
56   return 0;
57 }
58 
59 // Mono-to-stereo can be used as in-place.
UpMix(const AudioFrame & frame,size_t length_out_buff,int16_t * out_buff)60 int UpMix(const AudioFrame& frame, size_t length_out_buff, int16_t* out_buff) {
61   if (length_out_buff < frame.samples_per_channel_) {
62     return -1;
63   }
64   for (size_t n = frame.samples_per_channel_; n != 0; --n) {
65     size_t i = n - 1;
66     int16_t sample = frame.data_[i];
67     out_buff[2 * i + 1] = sample;
68     out_buff[2 * i] = sample;
69   }
70   return 0;
71 }
72 
ConvertEncodedInfoToFragmentationHeader(const AudioEncoder::EncodedInfo & info,RTPFragmentationHeader * frag)73 void ConvertEncodedInfoToFragmentationHeader(
74     const AudioEncoder::EncodedInfo& info,
75     RTPFragmentationHeader* frag) {
76   if (info.redundant.empty()) {
77     frag->fragmentationVectorSize = 0;
78     return;
79   }
80 
81   frag->VerifyAndAllocateFragmentationHeader(
82       static_cast<uint16_t>(info.redundant.size()));
83   frag->fragmentationVectorSize = static_cast<uint16_t>(info.redundant.size());
84   size_t offset = 0;
85   for (size_t i = 0; i < info.redundant.size(); ++i) {
86     frag->fragmentationOffset[i] = offset;
87     offset += info.redundant[i].encoded_bytes;
88     frag->fragmentationLength[i] = info.redundant[i].encoded_bytes;
89     frag->fragmentationTimeDiff[i] = rtc::checked_cast<uint16_t>(
90         info.encoded_timestamp - info.redundant[i].encoded_timestamp);
91     frag->fragmentationPlType[i] = info.redundant[i].payload_type;
92   }
93 }
94 }  // namespace
95 
MaybeLog(int value)96 void AudioCodingModuleImpl::ChangeLogger::MaybeLog(int value) {
97   if (value != last_value_ || first_time_) {
98     first_time_ = false;
99     last_value_ = value;
100     RTC_HISTOGRAM_COUNTS_SPARSE_100(histogram_name_, value);
101   }
102 }
103 
AudioCodingModuleImpl(const AudioCodingModule::Config & config)104 AudioCodingModuleImpl::AudioCodingModuleImpl(
105     const AudioCodingModule::Config& config)
106     : acm_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
107       id_(config.id),
108       expected_codec_ts_(0xD87F3F9F),
109       expected_in_ts_(0xD87F3F9F),
110       receiver_(config),
111       bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"),
112       previous_pltype_(255),
113       receiver_initialized_(false),
114       first_10ms_data_(false),
115       first_frame_(true),
116       callback_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
117       packetization_callback_(NULL),
118       vad_callback_(NULL) {
119   if (InitializeReceiverSafe() < 0) {
120     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
121                  "Cannot initialize receiver");
122   }
123   WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceAudioCoding, id_, "Created");
124 }
125 
126 AudioCodingModuleImpl::~AudioCodingModuleImpl() = default;
127 
Encode(const InputData & input_data)128 int32_t AudioCodingModuleImpl::Encode(const InputData& input_data) {
129   AudioEncoder::EncodedInfo encoded_info;
130   uint8_t previous_pltype;
131 
132   // Check if there is an encoder before.
133   if (!HaveValidEncoder("Process"))
134     return -1;
135 
136   AudioEncoder* audio_encoder = rent_a_codec_.GetEncoderStack();
137   // Scale the timestamp to the codec's RTP timestamp rate.
138   uint32_t rtp_timestamp =
139       first_frame_ ? input_data.input_timestamp
140                    : last_rtp_timestamp_ +
141                          rtc::CheckedDivExact(
142                              input_data.input_timestamp - last_timestamp_,
143                              static_cast<uint32_t>(rtc::CheckedDivExact(
144                                  audio_encoder->SampleRateHz(),
145                                  audio_encoder->RtpTimestampRateHz())));
146   last_timestamp_ = input_data.input_timestamp;
147   last_rtp_timestamp_ = rtp_timestamp;
148   first_frame_ = false;
149 
150   encode_buffer_.SetSize(audio_encoder->MaxEncodedBytes());
151   encoded_info = audio_encoder->Encode(
152       rtp_timestamp, rtc::ArrayView<const int16_t>(
153                          input_data.audio, input_data.audio_channel *
154                                                input_data.length_per_channel),
155       encode_buffer_.size(), encode_buffer_.data());
156   encode_buffer_.SetSize(encoded_info.encoded_bytes);
157   bitrate_logger_.MaybeLog(audio_encoder->GetTargetBitrate() / 1000);
158   if (encode_buffer_.size() == 0 && !encoded_info.send_even_if_empty) {
159     // Not enough data.
160     return 0;
161   }
162   previous_pltype = previous_pltype_;  // Read it while we have the critsect.
163 
164   RTPFragmentationHeader my_fragmentation;
165   ConvertEncodedInfoToFragmentationHeader(encoded_info, &my_fragmentation);
166   FrameType frame_type;
167   if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) {
168     frame_type = kEmptyFrame;
169     encoded_info.payload_type = previous_pltype;
170   } else {
171     RTC_DCHECK_GT(encode_buffer_.size(), 0u);
172     frame_type = encoded_info.speech ? kAudioFrameSpeech : kAudioFrameCN;
173   }
174 
175   {
176     CriticalSectionScoped lock(callback_crit_sect_.get());
177     if (packetization_callback_) {
178       packetization_callback_->SendData(
179           frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp,
180           encode_buffer_.data(), encode_buffer_.size(),
181           my_fragmentation.fragmentationVectorSize > 0 ? &my_fragmentation
182                                                        : nullptr);
183     }
184 
185     if (vad_callback_) {
186       // Callback with VAD decision.
187       vad_callback_->InFrameType(frame_type);
188     }
189   }
190   previous_pltype_ = encoded_info.payload_type;
191   return static_cast<int32_t>(encode_buffer_.size());
192 }
193 
194 /////////////////////////////////////////
195 //   Sender
196 //
197 
198 // Can be called multiple times for Codec, CNG, RED.
RegisterSendCodec(const CodecInst & send_codec)199 int AudioCodingModuleImpl::RegisterSendCodec(const CodecInst& send_codec) {
200   CriticalSectionScoped lock(acm_crit_sect_.get());
201   if (!codec_manager_.RegisterEncoder(send_codec)) {
202     return -1;
203   }
204   auto* sp = codec_manager_.GetStackParams();
205   if (!sp->speech_encoder && codec_manager_.GetCodecInst()) {
206     // We have no speech encoder, but we have a specification for making one.
207     AudioEncoder* enc =
208         rent_a_codec_.RentEncoder(*codec_manager_.GetCodecInst());
209     if (!enc)
210       return -1;
211     sp->speech_encoder = enc;
212   }
213   if (sp->speech_encoder)
214     rent_a_codec_.RentEncoderStack(sp);
215   return 0;
216 }
217 
RegisterExternalSendCodec(AudioEncoder * external_speech_encoder)218 void AudioCodingModuleImpl::RegisterExternalSendCodec(
219     AudioEncoder* external_speech_encoder) {
220   CriticalSectionScoped lock(acm_crit_sect_.get());
221   auto* sp = codec_manager_.GetStackParams();
222   sp->speech_encoder = external_speech_encoder;
223   rent_a_codec_.RentEncoderStack(sp);
224 }
225 
226 // Get current send codec.
SendCodec() const227 rtc::Optional<CodecInst> AudioCodingModuleImpl::SendCodec() const {
228   CriticalSectionScoped lock(acm_crit_sect_.get());
229   auto* ci = codec_manager_.GetCodecInst();
230   if (ci) {
231     return rtc::Optional<CodecInst>(*ci);
232   }
233   auto* enc = codec_manager_.GetStackParams()->speech_encoder;
234   if (enc) {
235     return rtc::Optional<CodecInst>(CodecManager::ForgeCodecInst(enc));
236   }
237   return rtc::Optional<CodecInst>();
238 }
239 
240 // Get current send frequency.
SendFrequency() const241 int AudioCodingModuleImpl::SendFrequency() const {
242   WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, id_,
243                "SendFrequency()");
244   CriticalSectionScoped lock(acm_crit_sect_.get());
245 
246   const auto* enc = rent_a_codec_.GetEncoderStack();
247   if (!enc) {
248     WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, id_,
249                  "SendFrequency Failed, no codec is registered");
250     return -1;
251   }
252 
253   return enc->SampleRateHz();
254 }
255 
SetBitRate(int bitrate_bps)256 void AudioCodingModuleImpl::SetBitRate(int bitrate_bps) {
257   CriticalSectionScoped lock(acm_crit_sect_.get());
258   auto* enc = rent_a_codec_.GetEncoderStack();
259   if (enc) {
260     enc->SetTargetBitrate(bitrate_bps);
261   }
262 }
263 
264 // Register a transport callback which will be called to deliver
265 // the encoded buffers.
RegisterTransportCallback(AudioPacketizationCallback * transport)266 int AudioCodingModuleImpl::RegisterTransportCallback(
267     AudioPacketizationCallback* transport) {
268   CriticalSectionScoped lock(callback_crit_sect_.get());
269   packetization_callback_ = transport;
270   return 0;
271 }
272 
273 // Add 10MS of raw (PCM) audio data to the encoder.
Add10MsData(const AudioFrame & audio_frame)274 int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) {
275   InputData input_data;
276   CriticalSectionScoped lock(acm_crit_sect_.get());
277   int r = Add10MsDataInternal(audio_frame, &input_data);
278   return r < 0 ? r : Encode(input_data);
279 }
280 
Add10MsDataInternal(const AudioFrame & audio_frame,InputData * input_data)281 int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame,
282                                                InputData* input_data) {
283   if (audio_frame.samples_per_channel_ == 0) {
284     assert(false);
285     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
286                  "Cannot Add 10 ms audio, payload length is zero");
287     return -1;
288   }
289 
290   if (audio_frame.sample_rate_hz_ > 48000) {
291     assert(false);
292     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
293                  "Cannot Add 10 ms audio, input frequency not valid");
294     return -1;
295   }
296 
297   // If the length and frequency matches. We currently just support raw PCM.
298   if (static_cast<size_t>(audio_frame.sample_rate_hz_ / 100) !=
299       audio_frame.samples_per_channel_) {
300     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
301                  "Cannot Add 10 ms audio, input frequency and length doesn't"
302                  " match");
303     return -1;
304   }
305 
306   if (audio_frame.num_channels_ != 1 && audio_frame.num_channels_ != 2) {
307     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
308                  "Cannot Add 10 ms audio, invalid number of channels.");
309     return -1;
310   }
311 
312   // Do we have a codec registered?
313   if (!HaveValidEncoder("Add10MsData")) {
314     return -1;
315   }
316 
317   const AudioFrame* ptr_frame;
318   // Perform a resampling, also down-mix if it is required and can be
319   // performed before resampling (a down mix prior to resampling will take
320   // place if both primary and secondary encoders are mono and input is in
321   // stereo).
322   if (PreprocessToAddData(audio_frame, &ptr_frame) < 0) {
323     return -1;
324   }
325 
326   // Check whether we need an up-mix or down-mix?
327   const size_t current_num_channels =
328       rent_a_codec_.GetEncoderStack()->NumChannels();
329   const bool same_num_channels =
330       ptr_frame->num_channels_ == current_num_channels;
331 
332   if (!same_num_channels) {
333     if (ptr_frame->num_channels_ == 1) {
334       if (UpMix(*ptr_frame, WEBRTC_10MS_PCM_AUDIO, input_data->buffer) < 0)
335         return -1;
336     } else {
337       if (DownMix(*ptr_frame, WEBRTC_10MS_PCM_AUDIO, input_data->buffer) < 0)
338         return -1;
339     }
340   }
341 
342   // When adding data to encoders this pointer is pointing to an audio buffer
343   // with correct number of channels.
344   const int16_t* ptr_audio = ptr_frame->data_;
345 
346   // For pushing data to primary, point the |ptr_audio| to correct buffer.
347   if (!same_num_channels)
348     ptr_audio = input_data->buffer;
349 
350   input_data->input_timestamp = ptr_frame->timestamp_;
351   input_data->audio = ptr_audio;
352   input_data->length_per_channel = ptr_frame->samples_per_channel_;
353   input_data->audio_channel = current_num_channels;
354 
355   return 0;
356 }
357 
358 // Perform a resampling and down-mix if required. We down-mix only if
359 // encoder is mono and input is stereo. In case of dual-streaming, both
360 // encoders has to be mono for down-mix to take place.
361 // |*ptr_out| will point to the pre-processed audio-frame. If no pre-processing
362 // is required, |*ptr_out| points to |in_frame|.
PreprocessToAddData(const AudioFrame & in_frame,const AudioFrame ** ptr_out)363 int AudioCodingModuleImpl::PreprocessToAddData(const AudioFrame& in_frame,
364                                                const AudioFrame** ptr_out) {
365   const auto* enc = rent_a_codec_.GetEncoderStack();
366   const bool resample = in_frame.sample_rate_hz_ != enc->SampleRateHz();
367 
368   // This variable is true if primary codec and secondary codec (if exists)
369   // are both mono and input is stereo.
370   // TODO(henrik.lundin): This condition should probably be
371   //   in_frame.num_channels_ > enc->NumChannels()
372   const bool down_mix = in_frame.num_channels_ == 2 && enc->NumChannels() == 1;
373 
374   if (!first_10ms_data_) {
375     expected_in_ts_ = in_frame.timestamp_;
376     expected_codec_ts_ = in_frame.timestamp_;
377     first_10ms_data_ = true;
378   } else if (in_frame.timestamp_ != expected_in_ts_) {
379     // TODO(turajs): Do we need a warning here.
380     expected_codec_ts_ +=
381         (in_frame.timestamp_ - expected_in_ts_) *
382         static_cast<uint32_t>(static_cast<double>(enc->SampleRateHz()) /
383                               static_cast<double>(in_frame.sample_rate_hz_));
384     expected_in_ts_ = in_frame.timestamp_;
385   }
386 
387 
388   if (!down_mix && !resample) {
389     // No pre-processing is required.
390     expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
391     expected_codec_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
392     *ptr_out = &in_frame;
393     return 0;
394   }
395 
396   *ptr_out = &preprocess_frame_;
397   preprocess_frame_.num_channels_ = in_frame.num_channels_;
398   int16_t audio[WEBRTC_10MS_PCM_AUDIO];
399   const int16_t* src_ptr_audio = in_frame.data_;
400   int16_t* dest_ptr_audio = preprocess_frame_.data_;
401   if (down_mix) {
402     // If a resampling is required the output of a down-mix is written into a
403     // local buffer, otherwise, it will be written to the output frame.
404     if (resample)
405       dest_ptr_audio = audio;
406     if (DownMix(in_frame, WEBRTC_10MS_PCM_AUDIO, dest_ptr_audio) < 0)
407       return -1;
408     preprocess_frame_.num_channels_ = 1;
409     // Set the input of the resampler is the down-mixed signal.
410     src_ptr_audio = audio;
411   }
412 
413   preprocess_frame_.timestamp_ = expected_codec_ts_;
414   preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_;
415   preprocess_frame_.sample_rate_hz_ = in_frame.sample_rate_hz_;
416   // If it is required, we have to do a resampling.
417   if (resample) {
418     // The result of the resampler is written to output frame.
419     dest_ptr_audio = preprocess_frame_.data_;
420 
421     int samples_per_channel = resampler_.Resample10Msec(
422         src_ptr_audio, in_frame.sample_rate_hz_, enc->SampleRateHz(),
423         preprocess_frame_.num_channels_, AudioFrame::kMaxDataSizeSamples,
424         dest_ptr_audio);
425 
426     if (samples_per_channel < 0) {
427       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
428                    "Cannot add 10 ms audio, resampling failed");
429       return -1;
430     }
431     preprocess_frame_.samples_per_channel_ =
432         static_cast<size_t>(samples_per_channel);
433     preprocess_frame_.sample_rate_hz_ = enc->SampleRateHz();
434   }
435 
436   expected_codec_ts_ +=
437       static_cast<uint32_t>(preprocess_frame_.samples_per_channel_);
438   expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
439 
440   return 0;
441 }
442 
443 /////////////////////////////////////////
444 //   (RED) Redundant Coding
445 //
446 
REDStatus() const447 bool AudioCodingModuleImpl::REDStatus() const {
448   CriticalSectionScoped lock(acm_crit_sect_.get());
449   return codec_manager_.GetStackParams()->use_red;
450 }
451 
452 // Configure RED status i.e on/off.
SetREDStatus(bool enable_red)453 int AudioCodingModuleImpl::SetREDStatus(bool enable_red) {
454 #ifdef WEBRTC_CODEC_RED
455   CriticalSectionScoped lock(acm_crit_sect_.get());
456   if (!codec_manager_.SetCopyRed(enable_red)) {
457     return -1;
458   }
459   auto* sp = codec_manager_.GetStackParams();
460   if (sp->speech_encoder)
461     rent_a_codec_.RentEncoderStack(sp);
462   return 0;
463 #else
464   WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, id_,
465                "  WEBRTC_CODEC_RED is undefined");
466   return -1;
467 #endif
468 }
469 
470 /////////////////////////////////////////
471 //   (FEC) Forward Error Correction (codec internal)
472 //
473 
CodecFEC() const474 bool AudioCodingModuleImpl::CodecFEC() const {
475   CriticalSectionScoped lock(acm_crit_sect_.get());
476   return codec_manager_.GetStackParams()->use_codec_fec;
477 }
478 
SetCodecFEC(bool enable_codec_fec)479 int AudioCodingModuleImpl::SetCodecFEC(bool enable_codec_fec) {
480   CriticalSectionScoped lock(acm_crit_sect_.get());
481   if (!codec_manager_.SetCodecFEC(enable_codec_fec)) {
482     return -1;
483   }
484   auto* sp = codec_manager_.GetStackParams();
485   if (sp->speech_encoder)
486     rent_a_codec_.RentEncoderStack(sp);
487   if (enable_codec_fec) {
488     return sp->use_codec_fec ? 0 : -1;
489   } else {
490     RTC_DCHECK(!sp->use_codec_fec);
491     return 0;
492   }
493 }
494 
SetPacketLossRate(int loss_rate)495 int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) {
496   CriticalSectionScoped lock(acm_crit_sect_.get());
497   if (HaveValidEncoder("SetPacketLossRate")) {
498     rent_a_codec_.GetEncoderStack()->SetProjectedPacketLossRate(loss_rate /
499                                                                 100.0);
500   }
501   return 0;
502 }
503 
504 /////////////////////////////////////////
505 //   (VAD) Voice Activity Detection
506 //
SetVAD(bool enable_dtx,bool enable_vad,ACMVADMode mode)507 int AudioCodingModuleImpl::SetVAD(bool enable_dtx,
508                                   bool enable_vad,
509                                   ACMVADMode mode) {
510   // Note: |enable_vad| is not used; VAD is enabled based on the DTX setting.
511   RTC_DCHECK_EQ(enable_dtx, enable_vad);
512   CriticalSectionScoped lock(acm_crit_sect_.get());
513   if (!codec_manager_.SetVAD(enable_dtx, mode)) {
514     return -1;
515   }
516   auto* sp = codec_manager_.GetStackParams();
517   if (sp->speech_encoder)
518     rent_a_codec_.RentEncoderStack(sp);
519   return 0;
520 }
521 
522 // Get VAD/DTX settings.
VAD(bool * dtx_enabled,bool * vad_enabled,ACMVADMode * mode) const523 int AudioCodingModuleImpl::VAD(bool* dtx_enabled, bool* vad_enabled,
524                                ACMVADMode* mode) const {
525   CriticalSectionScoped lock(acm_crit_sect_.get());
526   const auto* sp = codec_manager_.GetStackParams();
527   *dtx_enabled = *vad_enabled = sp->use_cng;
528   *mode = sp->vad_mode;
529   return 0;
530 }
531 
532 /////////////////////////////////////////
533 //   Receiver
534 //
535 
InitializeReceiver()536 int AudioCodingModuleImpl::InitializeReceiver() {
537   CriticalSectionScoped lock(acm_crit_sect_.get());
538   return InitializeReceiverSafe();
539 }
540 
541 // Initialize receiver, resets codec database etc.
InitializeReceiverSafe()542 int AudioCodingModuleImpl::InitializeReceiverSafe() {
543   // If the receiver is already initialized then we want to destroy any
544   // existing decoders. After a call to this function, we should have a clean
545   // start-up.
546   if (receiver_initialized_) {
547     if (receiver_.RemoveAllCodecs() < 0)
548       return -1;
549   }
550   receiver_.set_id(id_);
551   receiver_.ResetInitialDelay();
552   receiver_.SetMinimumDelay(0);
553   receiver_.SetMaximumDelay(0);
554   receiver_.FlushBuffers();
555 
556   // Register RED and CN.
557   auto db = RentACodec::Database();
558   for (size_t i = 0; i < db.size(); i++) {
559     if (IsCodecRED(db[i]) || IsCodecCN(db[i])) {
560       if (receiver_.AddCodec(static_cast<int>(i),
561                              static_cast<uint8_t>(db[i].pltype), 1,
562                              db[i].plfreq, nullptr, db[i].plname) < 0) {
563         WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
564                      "Cannot register master codec.");
565         return -1;
566       }
567     }
568   }
569   receiver_initialized_ = true;
570   return 0;
571 }
572 
573 // Get current receive frequency.
ReceiveFrequency() const574 int AudioCodingModuleImpl::ReceiveFrequency() const {
575   const auto last_packet_sample_rate = receiver_.last_packet_sample_rate_hz();
576   return last_packet_sample_rate ? *last_packet_sample_rate
577                                  : receiver_.last_output_sample_rate_hz();
578 }
579 
580 // Get current playout frequency.
PlayoutFrequency() const581 int AudioCodingModuleImpl::PlayoutFrequency() const {
582   WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, id_,
583                "PlayoutFrequency()");
584   return receiver_.last_output_sample_rate_hz();
585 }
586 
587 // Register possible receive codecs, can be called multiple times,
588 // for codecs, CNG (NB, WB and SWB), DTMF, RED.
RegisterReceiveCodec(const CodecInst & codec)589 int AudioCodingModuleImpl::RegisterReceiveCodec(const CodecInst& codec) {
590   CriticalSectionScoped lock(acm_crit_sect_.get());
591   RTC_DCHECK(receiver_initialized_);
592   if (codec.channels > 2) {
593     LOG_F(LS_ERROR) << "Unsupported number of channels: " << codec.channels;
594     return -1;
595   }
596 
597   auto codec_id =
598       RentACodec::CodecIdByParams(codec.plname, codec.plfreq, codec.channels);
599   if (!codec_id) {
600     LOG_F(LS_ERROR) << "Wrong codec params to be registered as receive codec";
601     return -1;
602   }
603   auto codec_index = RentACodec::CodecIndexFromId(*codec_id);
604   RTC_CHECK(codec_index) << "Invalid codec ID: " << static_cast<int>(*codec_id);
605 
606   // Check if the payload-type is valid.
607   if (!RentACodec::IsPayloadTypeValid(codec.pltype)) {
608     LOG_F(LS_ERROR) << "Invalid payload type " << codec.pltype << " for "
609                     << codec.plname;
610     return -1;
611   }
612 
613   // Get |decoder| associated with |codec|. |decoder| is NULL if |codec| does
614   // not own its decoder.
615   return receiver_.AddCodec(
616       *codec_index, codec.pltype, codec.channels, codec.plfreq,
617       STR_CASE_CMP(codec.plname, "isac") == 0 ? rent_a_codec_.RentIsacDecoder()
618                                               : nullptr,
619       codec.plname);
620 }
621 
RegisterExternalReceiveCodec(int rtp_payload_type,AudioDecoder * external_decoder,int sample_rate_hz,int num_channels,const std::string & name)622 int AudioCodingModuleImpl::RegisterExternalReceiveCodec(
623     int rtp_payload_type,
624     AudioDecoder* external_decoder,
625     int sample_rate_hz,
626     int num_channels,
627     const std::string& name) {
628   CriticalSectionScoped lock(acm_crit_sect_.get());
629   RTC_DCHECK(receiver_initialized_);
630   if (num_channels > 2 || num_channels < 0) {
631     LOG_F(LS_ERROR) << "Unsupported number of channels: " << num_channels;
632     return -1;
633   }
634 
635   // Check if the payload-type is valid.
636   if (!RentACodec::IsPayloadTypeValid(rtp_payload_type)) {
637     LOG_F(LS_ERROR) << "Invalid payload-type " << rtp_payload_type
638                     << " for external decoder.";
639     return -1;
640   }
641 
642   return receiver_.AddCodec(-1 /* external */, rtp_payload_type, num_channels,
643                             sample_rate_hz, external_decoder, name);
644 }
645 
646 // Get current received codec.
ReceiveCodec(CodecInst * current_codec) const647 int AudioCodingModuleImpl::ReceiveCodec(CodecInst* current_codec) const {
648   CriticalSectionScoped lock(acm_crit_sect_.get());
649   return receiver_.LastAudioCodec(current_codec);
650 }
651 
652 // Incoming packet from network parsed and ready for decode.
IncomingPacket(const uint8_t * incoming_payload,const size_t payload_length,const WebRtcRTPHeader & rtp_header)653 int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload,
654                                           const size_t payload_length,
655                                           const WebRtcRTPHeader& rtp_header) {
656   return receiver_.InsertPacket(
657       rtp_header,
658       rtc::ArrayView<const uint8_t>(incoming_payload, payload_length));
659 }
660 
661 // Minimum playout delay (Used for lip-sync).
SetMinimumPlayoutDelay(int time_ms)662 int AudioCodingModuleImpl::SetMinimumPlayoutDelay(int time_ms) {
663   if ((time_ms < 0) || (time_ms > 10000)) {
664     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
665                  "Delay must be in the range of 0-1000 milliseconds.");
666     return -1;
667   }
668   return receiver_.SetMinimumDelay(time_ms);
669 }
670 
SetMaximumPlayoutDelay(int time_ms)671 int AudioCodingModuleImpl::SetMaximumPlayoutDelay(int time_ms) {
672   if ((time_ms < 0) || (time_ms > 10000)) {
673     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
674                  "Delay must be in the range of 0-1000 milliseconds.");
675     return -1;
676   }
677   return receiver_.SetMaximumDelay(time_ms);
678 }
679 
680 // Get 10 milliseconds of raw audio data to play out.
681 // Automatic resample to the requested frequency.
PlayoutData10Ms(int desired_freq_hz,AudioFrame * audio_frame)682 int AudioCodingModuleImpl::PlayoutData10Ms(int desired_freq_hz,
683                                            AudioFrame* audio_frame) {
684   // GetAudio always returns 10 ms, at the requested sample rate.
685   if (receiver_.GetAudio(desired_freq_hz, audio_frame) != 0) {
686     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
687                  "PlayoutData failed, RecOut Failed");
688     return -1;
689   }
690   audio_frame->id_ = id_;
691   return 0;
692 }
693 
694 /////////////////////////////////////////
695 //   Statistics
696 //
697 
698 // TODO(turajs) change the return value to void. Also change the corresponding
699 // NetEq function.
GetNetworkStatistics(NetworkStatistics * statistics)700 int AudioCodingModuleImpl::GetNetworkStatistics(NetworkStatistics* statistics) {
701   receiver_.GetNetworkStatistics(statistics);
702   return 0;
703 }
704 
RegisterVADCallback(ACMVADCallback * vad_callback)705 int AudioCodingModuleImpl::RegisterVADCallback(ACMVADCallback* vad_callback) {
706   WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceAudioCoding, id_,
707                "RegisterVADCallback()");
708   CriticalSectionScoped lock(callback_crit_sect_.get());
709   vad_callback_ = vad_callback;
710   return 0;
711 }
712 
713 // TODO(kwiberg): Remove this method, and have callers call IncomingPacket
714 // instead. The translation logic and state belong with them, not with
715 // AudioCodingModuleImpl.
IncomingPayload(const uint8_t * incoming_payload,size_t payload_length,uint8_t payload_type,uint32_t timestamp)716 int AudioCodingModuleImpl::IncomingPayload(const uint8_t* incoming_payload,
717                                            size_t payload_length,
718                                            uint8_t payload_type,
719                                            uint32_t timestamp) {
720   // We are not acquiring any lock when interacting with |aux_rtp_header_| no
721   // other method uses this member variable.
722   if (!aux_rtp_header_) {
723     // This is the first time that we are using |dummy_rtp_header_|
724     // so we have to create it.
725     aux_rtp_header_.reset(new WebRtcRTPHeader);
726     aux_rtp_header_->header.payloadType = payload_type;
727     // Don't matter in this case.
728     aux_rtp_header_->header.ssrc = 0;
729     aux_rtp_header_->header.markerBit = false;
730     // Start with random numbers.
731     aux_rtp_header_->header.sequenceNumber = 0x1234;  // Arbitrary.
732     aux_rtp_header_->type.Audio.channel = 1;
733   }
734 
735   aux_rtp_header_->header.timestamp = timestamp;
736   IncomingPacket(incoming_payload, payload_length, *aux_rtp_header_);
737   // Get ready for the next payload.
738   aux_rtp_header_->header.sequenceNumber++;
739   return 0;
740 }
741 
SetOpusApplication(OpusApplicationMode application)742 int AudioCodingModuleImpl::SetOpusApplication(OpusApplicationMode application) {
743   CriticalSectionScoped lock(acm_crit_sect_.get());
744   if (!HaveValidEncoder("SetOpusApplication")) {
745     return -1;
746   }
747   AudioEncoder::Application app;
748   switch (application) {
749     case kVoip:
750       app = AudioEncoder::Application::kSpeech;
751       break;
752     case kAudio:
753       app = AudioEncoder::Application::kAudio;
754       break;
755     default:
756       FATAL();
757       return 0;
758   }
759   return rent_a_codec_.GetEncoderStack()->SetApplication(app) ? 0 : -1;
760 }
761 
762 // Informs Opus encoder of the maximum playback rate the receiver will render.
SetOpusMaxPlaybackRate(int frequency_hz)763 int AudioCodingModuleImpl::SetOpusMaxPlaybackRate(int frequency_hz) {
764   CriticalSectionScoped lock(acm_crit_sect_.get());
765   if (!HaveValidEncoder("SetOpusMaxPlaybackRate")) {
766     return -1;
767   }
768   rent_a_codec_.GetEncoderStack()->SetMaxPlaybackRate(frequency_hz);
769   return 0;
770 }
771 
EnableOpusDtx()772 int AudioCodingModuleImpl::EnableOpusDtx() {
773   CriticalSectionScoped lock(acm_crit_sect_.get());
774   if (!HaveValidEncoder("EnableOpusDtx")) {
775     return -1;
776   }
777   return rent_a_codec_.GetEncoderStack()->SetDtx(true) ? 0 : -1;
778 }
779 
DisableOpusDtx()780 int AudioCodingModuleImpl::DisableOpusDtx() {
781   CriticalSectionScoped lock(acm_crit_sect_.get());
782   if (!HaveValidEncoder("DisableOpusDtx")) {
783     return -1;
784   }
785   return rent_a_codec_.GetEncoderStack()->SetDtx(false) ? 0 : -1;
786 }
787 
PlayoutTimestamp(uint32_t * timestamp)788 int AudioCodingModuleImpl::PlayoutTimestamp(uint32_t* timestamp) {
789   return receiver_.GetPlayoutTimestamp(timestamp) ? 0 : -1;
790 }
791 
HaveValidEncoder(const char * caller_name) const792 bool AudioCodingModuleImpl::HaveValidEncoder(const char* caller_name) const {
793   if (!rent_a_codec_.GetEncoderStack()) {
794     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_,
795                  "%s failed: No send codec is registered.", caller_name);
796     return false;
797   }
798   return true;
799 }
800 
UnregisterReceiveCodec(uint8_t payload_type)801 int AudioCodingModuleImpl::UnregisterReceiveCodec(uint8_t payload_type) {
802   return receiver_.RemoveCodec(payload_type);
803 }
804 
EnableNack(size_t max_nack_list_size)805 int AudioCodingModuleImpl::EnableNack(size_t max_nack_list_size) {
806   return receiver_.EnableNack(max_nack_list_size);
807 }
808 
DisableNack()809 void AudioCodingModuleImpl::DisableNack() {
810   receiver_.DisableNack();
811 }
812 
GetNackList(int64_t round_trip_time_ms) const813 std::vector<uint16_t> AudioCodingModuleImpl::GetNackList(
814     int64_t round_trip_time_ms) const {
815   return receiver_.GetNackList(round_trip_time_ms);
816 }
817 
LeastRequiredDelayMs() const818 int AudioCodingModuleImpl::LeastRequiredDelayMs() const {
819   return receiver_.LeastRequiredDelayMs();
820 }
821 
GetDecodingCallStatistics(AudioDecodingCallStats * call_stats) const822 void AudioCodingModuleImpl::GetDecodingCallStatistics(
823       AudioDecodingCallStats* call_stats) const {
824   receiver_.GetDecodingCallStatistics(call_stats);
825 }
826 
827 }  // namespace acm2
828 }  // namespace webrtc
829