blob: 0232f2722d8a1b62b03ddba0374726feba93e077 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/include/audio_coding_module.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <assert.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Jonathan Yu36344a02017-07-30 01:55:34 -070015#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <cstdint>
Jonathan Yu36344a02017-07-30 01:55:34 -070017
Niels Möller2edab4c2018-10-22 09:48:08 +020018#include "absl/strings/match.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/acm2/acm_receiver.h"
21#include "modules/audio_coding/acm2/acm_resampler.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020022#include "modules/include/module_common_types.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "modules/include/module_common_types_public.h"
24#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010028#include "rtc_base/numerics/safe_conversions.h"
Yves Gerey988cc082018-10-23 12:03:01 +020029#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "system_wrappers/include/metrics.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000031
32namespace webrtc {
33
kwibergc13ded52016-06-17 06:00:45 -070034namespace {
35
Per Åhgren4f2e9402019-10-04 11:06:15 +020036// Initial size for the buffer in InputBuffer. This matches 6 channels of 10 ms
37// 48 kHz data.
38constexpr size_t kInitialInputDataBufferSize = 6 * 480;
39
kwibergc13ded52016-06-17 06:00:45 -070040class AudioCodingModuleImpl final : public AudioCodingModule {
41 public:
42 explicit AudioCodingModuleImpl(const AudioCodingModule::Config& config);
43 ~AudioCodingModuleImpl() override;
44
45 /////////////////////////////////////////
46 // Sender
47 //
48
kwiberg24c7c122016-09-28 11:57:10 -070049 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
50 modifier) override;
kwibergc13ded52016-06-17 06:00:45 -070051
kwibergc13ded52016-06-17 06:00:45 -070052 // Register a transport callback which will be
53 // called to deliver the encoded buffers.
54 int RegisterTransportCallback(AudioPacketizationCallback* transport) override;
55
56 // Add 10 ms of raw (PCM) audio data to the encoder.
57 int Add10MsData(const AudioFrame& audio_frame) override;
58
59 /////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -070060 // (FEC) Forward Error Correction (codec internal)
61 //
62
kwibergc13ded52016-06-17 06:00:45 -070063 // Set target packet loss rate
64 int SetPacketLossRate(int loss_rate) override;
65
66 /////////////////////////////////////////
67 // (VAD) Voice Activity Detection
68 // and
69 // (CNG) Comfort Noise Generation
70 //
71
kwibergc13ded52016-06-17 06:00:45 -070072 int RegisterVADCallback(ACMVADCallback* vad_callback) override;
73
74 /////////////////////////////////////////
75 // Receiver
76 //
77
78 // Initialize receiver, resets codec database etc.
79 int InitializeReceiver() override;
80
kwiberg1c07c702017-03-27 07:15:49 -070081 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
82
kwibergc13ded52016-06-17 06:00:45 -070083 // Incoming packet from network parsed and ready for decode.
84 int IncomingPacket(const uint8_t* incoming_payload,
85 const size_t payload_length,
Niels Möllerafb5dbb2019-02-15 15:21:47 +010086 const RTPHeader& rtp_info) override;
kwibergc13ded52016-06-17 06:00:45 -070087
kwibergc13ded52016-06-17 06:00:45 -070088 // Get 10 milliseconds of raw audio data to play out, and
89 // automatic resample to the requested frequency if > 0.
90 int PlayoutData10Ms(int desired_freq_hz,
91 AudioFrame* audio_frame,
92 bool* muted) override;
kwibergc13ded52016-06-17 06:00:45 -070093
94 /////////////////////////////////////////
95 // Statistics
96 //
97
98 int GetNetworkStatistics(NetworkStatistics* statistics) override;
99
ivoce1198e02017-09-08 08:13:19 -0700100 ANAStats GetANAStats() const override;
101
kwibergc13ded52016-06-17 06:00:45 -0700102 private:
103 struct InputData {
Per Åhgren4f2e9402019-10-04 11:06:15 +0200104 InputData() : buffer(kInitialInputDataBufferSize) {}
kwibergc13ded52016-06-17 06:00:45 -0700105 uint32_t input_timestamp;
106 const int16_t* audio;
107 size_t length_per_channel;
108 size_t audio_channel;
109 // If a re-mix is required (up or down), this buffer will store a re-mixed
110 // version of the input.
Per Åhgren4f2e9402019-10-04 11:06:15 +0200111 std::vector<int16_t> buffer;
kwibergc13ded52016-06-17 06:00:45 -0700112 };
113
Per Åhgren4f2e9402019-10-04 11:06:15 +0200114 InputData input_data_ RTC_GUARDED_BY(acm_crit_sect_);
115
kwibergc13ded52016-06-17 06:00:45 -0700116 // This member class writes values to the named UMA histogram, but only if
117 // the value has changed since the last time (and always for the first call).
118 class ChangeLogger {
119 public:
120 explicit ChangeLogger(const std::string& histogram_name)
121 : histogram_name_(histogram_name) {}
122 // Logs the new value if it is different from the last logged value, or if
123 // this is the first call.
124 void MaybeLog(int value);
125
126 private:
127 int last_value_ = 0;
128 int first_time_ = true;
129 const std::string histogram_name_;
130 };
131
kwibergc13ded52016-06-17 06:00:45 -0700132 int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data)
danilchap56359be2017-09-07 07:53:45 -0700133 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700134 int Encode(const InputData& input_data)
danilchap56359be2017-09-07 07:53:45 -0700135 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700136
danilchap56359be2017-09-07 07:53:45 -0700137 int InitializeReceiverSafe() RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700138
139 bool HaveValidEncoder(const char* caller_name) const
danilchap56359be2017-09-07 07:53:45 -0700140 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700141
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)
danilchap56359be2017-09-07 07:53:45 -0700155 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700156
157 // Change required states after starting to receive the codec corresponding
158 // to |index|.
159 int UpdateUponReceivingCodec(int index);
160
161 rtc::CriticalSection acm_crit_sect_;
danilchap56359be2017-09-07 07:53:45 -0700162 rtc::Buffer encode_buffer_ RTC_GUARDED_BY(acm_crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700163 uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_crit_sect_);
164 uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_crit_sect_);
165 acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700166 acm2::AcmReceiver receiver_; // AcmReceiver has it's own internal lock.
danilchap56359be2017-09-07 07:53:45 -0700167 ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700168
Karl Wiberg49c33ce2018-11-12 14:21:58 +0100169 // Current encoder stack, provided by a call to RegisterEncoder.
danilchap56359be2017-09-07 07:53:45 -0700170 std::unique_ptr<AudioEncoder> encoder_stack_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700171
kwibergc13ded52016-06-17 06:00:45 -0700172 // This is to keep track of CN instances where we can send DTMFs.
danilchap56359be2017-09-07 07:53:45 -0700173 uint8_t previous_pltype_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700174
danilchap56359be2017-09-07 07:53:45 -0700175 bool receiver_initialized_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700176
danilchap56359be2017-09-07 07:53:45 -0700177 AudioFrame preprocess_frame_ RTC_GUARDED_BY(acm_crit_sect_);
178 bool first_10ms_data_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700179
danilchap56359be2017-09-07 07:53:45 -0700180 bool first_frame_ RTC_GUARDED_BY(acm_crit_sect_);
181 uint32_t last_timestamp_ RTC_GUARDED_BY(acm_crit_sect_);
182 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700183
184 rtc::CriticalSection callback_crit_sect_;
185 AudioPacketizationCallback* packetization_callback_
danilchap56359be2017-09-07 07:53:45 -0700186 RTC_GUARDED_BY(callback_crit_sect_);
187 ACMVADCallback* vad_callback_ RTC_GUARDED_BY(callback_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700188
189 int codec_histogram_bins_log_[static_cast<size_t>(
190 AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)];
191 int number_of_consecutive_empty_packets_;
192};
193
194// Adds a codec usage sample to the histogram.
195void UpdateCodecTypeHistogram(size_t codec_type) {
196 RTC_HISTOGRAM_ENUMERATION(
197 "WebRTC.Audio.Encoder.CodecType", static_cast<int>(codec_type),
198 static_cast<int>(
199 webrtc::AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes));
200}
201
kwibergc13ded52016-06-17 06:00:45 -0700202// Stereo-to-mono can be used as in-place.
Per Åhgren4f2e9402019-10-04 11:06:15 +0200203void DownMix(const AudioFrame& frame,
204 size_t length_out_buff,
205 int16_t* out_buff) {
yujo36b1a5f2017-06-12 12:45:32 -0700206 RTC_DCHECK_EQ(frame.num_channels_, 2);
207 RTC_DCHECK_GE(length_out_buff, frame.samples_per_channel_);
208
209 if (!frame.muted()) {
210 const int16_t* frame_data = frame.data();
211 for (size_t n = 0; n < frame.samples_per_channel_; ++n) {
Yves Gerey665174f2018-06-19 15:03:05 +0200212 out_buff[n] =
213 static_cast<int16_t>((static_cast<int32_t>(frame_data[2 * n]) +
214 static_cast<int32_t>(frame_data[2 * n + 1])) >>
215 1);
yujo36b1a5f2017-06-12 12:45:32 -0700216 }
217 } else {
Jonathan Yu36344a02017-07-30 01:55:34 -0700218 std::fill(out_buff, out_buff + frame.samples_per_channel_, 0);
kwibergc13ded52016-06-17 06:00:45 -0700219 }
kwibergc13ded52016-06-17 06:00:45 -0700220}
221
Per Åhgren4f2e9402019-10-04 11:06:15 +0200222// Remixes the input frame to an output data vector. The output vector is
223// resized if needed.
224void ReMix(const AudioFrame& input,
225 size_t num_output_channels,
226 std::vector<int16_t>* output) {
227 const size_t output_size = num_output_channels * input.samples_per_channel_;
yujo36b1a5f2017-06-12 12:45:32 -0700228
Per Åhgren4f2e9402019-10-04 11:06:15 +0200229 if (output->size() != output_size) {
230 output->resize(output_size);
kwibergc13ded52016-06-17 06:00:45 -0700231 }
Per Åhgren4f2e9402019-10-04 11:06:15 +0200232
233 // For muted frames, fill the frame with zeros.
234 if (input.muted()) {
235 std::fill(output->begin(), output->end(), 0);
236 return;
237 }
238
239 // Ensure that the special case of zero input channels is handled correctly
240 // (zero samples per channel is already handled correctly in the code below).
241 if (input.num_channels_ == 0) {
242 return;
243 }
244
245 const int16_t* input_data = input.data();
246 size_t in_index = 0;
247 size_t out_index = 0;
248
Per Åhgren048b10a2019-11-13 15:44:57 +0100249 // When upmixing is needed, copy the available channels directly, and set the
250 // remaining channels to zero.
Per Åhgren4f2e9402019-10-04 11:06:15 +0200251 if (input.num_channels_ < num_output_channels) {
252 for (size_t k = 0; k < input.samples_per_channel_; ++k) {
253 for (size_t j = 0; j < input.num_channels_; ++j) {
254 (*output)[out_index++] = input_data[in_index++];
255 }
Per Åhgren4f2e9402019-10-04 11:06:15 +0200256 for (size_t j = input.num_channels_; j < num_output_channels; ++j) {
Per Åhgren048b10a2019-11-13 15:44:57 +0100257 (*output)[out_index++] = 0;
Per Åhgren4f2e9402019-10-04 11:06:15 +0200258 }
Per Åhgren048b10a2019-11-13 15:44:57 +0100259 RTC_DCHECK_EQ(in_index, (k + 1) * input.num_channels_);
260 RTC_DCHECK_EQ(out_index, (k + 1) * num_output_channels);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200261 }
Per Åhgren048b10a2019-11-13 15:44:57 +0100262 RTC_DCHECK_EQ(in_index, input.samples_per_channel_ * input.num_channels_);
263 RTC_DCHECK_EQ(out_index, input.samples_per_channel_ * num_output_channels);
264
Per Åhgren4f2e9402019-10-04 11:06:15 +0200265 return;
266 }
267
268 // When downmixing is needed, and the input is stereo, average the channels.
269 if (input.num_channels_ == 2) {
270 for (size_t n = 0; n < input.samples_per_channel_; ++n) {
271 (*output)[n] =
272 static_cast<int16_t>((static_cast<int32_t>(input_data[2 * n]) +
273 static_cast<int32_t>(input_data[2 * n + 1])) >>
274 1);
275 }
276 return;
277 }
278
279 // When downmixing is needed, and the input is multichannel, drop the surplus
280 // channels.
281 const size_t num_channels_to_drop = input.num_channels_ - num_output_channels;
282 for (size_t k = 0; k < input.samples_per_channel_; ++k) {
283 for (size_t j = 0; j < num_output_channels; ++j) {
284 (*output)[out_index++] = input_data[in_index++];
285 }
286 in_index += num_channels_to_drop;
287 }
kwibergc13ded52016-06-17 06:00:45 -0700288}
289
kwibergc13ded52016-06-17 06:00:45 -0700290void AudioCodingModuleImpl::ChangeLogger::MaybeLog(int value) {
291 if (value != last_value_ || first_time_) {
292 first_time_ = false;
293 last_value_ = value;
294 RTC_HISTOGRAM_COUNTS_SPARSE_100(histogram_name_, value);
295 }
296}
297
298AudioCodingModuleImpl::AudioCodingModuleImpl(
299 const AudioCodingModule::Config& config)
solenbergc7b4a452017-09-28 07:37:11 -0700300 : expected_codec_ts_(0xD87F3F9F),
kwibergc13ded52016-06-17 06:00:45 -0700301 expected_in_ts_(0xD87F3F9F),
302 receiver_(config),
303 bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"),
kwibergc13ded52016-06-17 06:00:45 -0700304 encoder_stack_(nullptr),
305 previous_pltype_(255),
306 receiver_initialized_(false),
307 first_10ms_data_(false),
308 first_frame_(true),
309 packetization_callback_(NULL),
310 vad_callback_(NULL),
311 codec_histogram_bins_log_(),
312 number_of_consecutive_empty_packets_(0) {
313 if (InitializeReceiverSafe() < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100314 RTC_LOG(LS_ERROR) << "Cannot initialize receiver";
kwibergc13ded52016-06-17 06:00:45 -0700315 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100316 RTC_LOG(LS_INFO) << "Created";
kwibergc13ded52016-06-17 06:00:45 -0700317}
318
319AudioCodingModuleImpl::~AudioCodingModuleImpl() = default;
320
321int32_t AudioCodingModuleImpl::Encode(const InputData& input_data) {
322 AudioEncoder::EncodedInfo encoded_info;
323 uint8_t previous_pltype;
324
325 // Check if there is an encoder before.
326 if (!HaveValidEncoder("Process"))
327 return -1;
328
Yves Gerey665174f2018-06-19 15:03:05 +0200329 if (!first_frame_) {
deadbeeffcada902016-08-24 12:45:13 -0700330 RTC_DCHECK(IsNewerTimestamp(input_data.input_timestamp, last_timestamp_))
ossu63fb95a2016-07-06 09:34:22 -0700331 << "Time should not move backwards";
332 }
333
kwibergc13ded52016-06-17 06:00:45 -0700334 // Scale the timestamp to the codec's RTP timestamp rate.
335 uint32_t rtp_timestamp =
Karl Wiberg053c3712019-05-16 15:24:17 +0200336 first_frame_
337 ? input_data.input_timestamp
338 : last_rtp_timestamp_ +
339 rtc::dchecked_cast<uint32_t>(rtc::CheckedDivExact(
340 int64_t{input_data.input_timestamp - last_timestamp_} *
341 encoder_stack_->RtpTimestampRateHz(),
342 int64_t{encoder_stack_->SampleRateHz()}));
kwibergc13ded52016-06-17 06:00:45 -0700343 last_timestamp_ = input_data.input_timestamp;
344 last_rtp_timestamp_ = rtp_timestamp;
345 first_frame_ = false;
346
347 // Clear the buffer before reuse - encoded data will get appended.
348 encode_buffer_.Clear();
349 encoded_info = encoder_stack_->Encode(
Yves Gerey665174f2018-06-19 15:03:05 +0200350 rtp_timestamp,
351 rtc::ArrayView<const int16_t>(
352 input_data.audio,
353 input_data.audio_channel * input_data.length_per_channel),
kwibergc13ded52016-06-17 06:00:45 -0700354 &encode_buffer_);
355
356 bitrate_logger_.MaybeLog(encoder_stack_->GetTargetBitrate() / 1000);
357 if (encode_buffer_.size() == 0 && !encoded_info.send_even_if_empty) {
358 // Not enough data.
359 return 0;
360 }
361 previous_pltype = previous_pltype_; // Read it while we have the critsect.
362
363 // Log codec type to histogram once every 500 packets.
364 if (encoded_info.encoded_bytes == 0) {
365 ++number_of_consecutive_empty_packets_;
366 } else {
367 size_t codec_type = static_cast<size_t>(encoded_info.encoder_type);
368 codec_histogram_bins_log_[codec_type] +=
369 number_of_consecutive_empty_packets_ + 1;
370 number_of_consecutive_empty_packets_ = 0;
371 if (codec_histogram_bins_log_[codec_type] >= 500) {
372 codec_histogram_bins_log_[codec_type] -= 500;
373 UpdateCodecTypeHistogram(codec_type);
374 }
375 }
376
Niels Möller87e2d782019-03-07 10:18:23 +0100377 AudioFrameType frame_type;
kwibergc13ded52016-06-17 06:00:45 -0700378 if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) {
Niels Möllerc936cb62019-03-19 14:10:16 +0100379 frame_type = AudioFrameType::kEmptyFrame;
kwibergc13ded52016-06-17 06:00:45 -0700380 encoded_info.payload_type = previous_pltype;
381 } else {
kwibergaf476c72016-11-28 15:21:39 -0800382 RTC_DCHECK_GT(encode_buffer_.size(), 0);
Niels Möllerc936cb62019-03-19 14:10:16 +0100383 frame_type = encoded_info.speech ? AudioFrameType::kAudioFrameSpeech
384 : AudioFrameType::kAudioFrameCN;
kwibergc13ded52016-06-17 06:00:45 -0700385 }
386
387 {
388 rtc::CritScope lock(&callback_crit_sect_);
389 if (packetization_callback_) {
390 packetization_callback_->SendData(
391 frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200392 encode_buffer_.data(), encode_buffer_.size());
kwibergc13ded52016-06-17 06:00:45 -0700393 }
394
395 if (vad_callback_) {
396 // Callback with VAD decision.
397 vad_callback_->InFrameType(frame_type);
398 }
399 }
400 previous_pltype_ = encoded_info.payload_type;
401 return static_cast<int32_t>(encode_buffer_.size());
402}
403
404/////////////////////////////////////////
405// Sender
406//
407
kwibergc13ded52016-06-17 06:00:45 -0700408void AudioCodingModuleImpl::ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -0700409 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
kwibergc13ded52016-06-17 06:00:45 -0700410 rtc::CritScope lock(&acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700411 modifier(&encoder_stack_);
412}
413
kwibergc13ded52016-06-17 06:00:45 -0700414// Register a transport callback which will be called to deliver
415// the encoded buffers.
416int AudioCodingModuleImpl::RegisterTransportCallback(
417 AudioPacketizationCallback* transport) {
418 rtc::CritScope lock(&callback_crit_sect_);
419 packetization_callback_ = transport;
420 return 0;
421}
422
423// Add 10MS of raw (PCM) audio data to the encoder.
424int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) {
kwibergc13ded52016-06-17 06:00:45 -0700425 rtc::CritScope lock(&acm_crit_sect_);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200426 int r = Add10MsDataInternal(audio_frame, &input_data_);
427 return r < 0 ? r : Encode(input_data_);
kwibergc13ded52016-06-17 06:00:45 -0700428}
429
430int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame,
431 InputData* input_data) {
432 if (audio_frame.samples_per_channel_ == 0) {
433 assert(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100434 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, payload length is zero";
kwibergc13ded52016-06-17 06:00:45 -0700435 return -1;
436 }
437
henrika33541572019-09-10 14:27:40 +0200438 if (audio_frame.sample_rate_hz_ > 192000) {
kwibergc13ded52016-06-17 06:00:45 -0700439 assert(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, input frequency not valid";
kwibergc13ded52016-06-17 06:00:45 -0700441 return -1;
442 }
443
444 // If the length and frequency matches. We currently just support raw PCM.
445 if (static_cast<size_t>(audio_frame.sample_rate_hz_ / 100) !=
446 audio_frame.samples_per_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100447 RTC_LOG(LS_ERROR)
Alex Loiko300ec8c2017-05-30 17:23:28 +0200448 << "Cannot Add 10 ms audio, input frequency and length doesn't match";
kwibergc13ded52016-06-17 06:00:45 -0700449 return -1;
450 }
451
Alex Loiko65438812019-02-22 10:13:44 +0100452 if (audio_frame.num_channels_ != 1 && audio_frame.num_channels_ != 2 &&
453 audio_frame.num_channels_ != 4 && audio_frame.num_channels_ != 6 &&
454 audio_frame.num_channels_ != 8) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100455 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, invalid number of channels.";
kwibergc13ded52016-06-17 06:00:45 -0700456 return -1;
457 }
458
459 // Do we have a codec registered?
460 if (!HaveValidEncoder("Add10MsData")) {
461 return -1;
462 }
463
464 const AudioFrame* ptr_frame;
465 // Perform a resampling, also down-mix if it is required and can be
466 // performed before resampling (a down mix prior to resampling will take
467 // place if both primary and secondary encoders are mono and input is in
468 // stereo).
469 if (PreprocessToAddData(audio_frame, &ptr_frame) < 0) {
470 return -1;
471 }
472
473 // Check whether we need an up-mix or down-mix?
474 const size_t current_num_channels = encoder_stack_->NumChannels();
475 const bool same_num_channels =
476 ptr_frame->num_channels_ == current_num_channels;
477
yujo36b1a5f2017-06-12 12:45:32 -0700478 // TODO(yujo): Skip encode of muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700479 input_data->input_timestamp = ptr_frame->timestamp_;
kwibergc13ded52016-06-17 06:00:45 -0700480 input_data->length_per_channel = ptr_frame->samples_per_channel_;
481 input_data->audio_channel = current_num_channels;
482
Per Åhgren4f2e9402019-10-04 11:06:15 +0200483 if (!same_num_channels) {
484 // Remixes the input frame to the output data and in the process resize the
485 // output data if needed.
486 ReMix(*ptr_frame, current_num_channels, &input_data->buffer);
487
488 // For pushing data to primary, point the |ptr_audio| to correct buffer.
489 input_data->audio = input_data->buffer.data();
490 RTC_DCHECK_GE(input_data->buffer.size(),
491 input_data->length_per_channel * input_data->audio_channel);
492 } else {
493 // When adding data to encoders this pointer is pointing to an audio buffer
494 // with correct number of channels.
495 input_data->audio = ptr_frame->data();
496 }
497
kwibergc13ded52016-06-17 06:00:45 -0700498 return 0;
499}
500
501// Perform a resampling and down-mix if required. We down-mix only if
502// encoder is mono and input is stereo. In case of dual-streaming, both
503// encoders has to be mono for down-mix to take place.
504// |*ptr_out| will point to the pre-processed audio-frame. If no pre-processing
505// is required, |*ptr_out| points to |in_frame|.
yujo36b1a5f2017-06-12 12:45:32 -0700506// TODO(yujo): Make this more efficient for muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700507int AudioCodingModuleImpl::PreprocessToAddData(const AudioFrame& in_frame,
508 const AudioFrame** ptr_out) {
509 const bool resample =
510 in_frame.sample_rate_hz_ != encoder_stack_->SampleRateHz();
511
512 // This variable is true if primary codec and secondary codec (if exists)
513 // are both mono and input is stereo.
514 // TODO(henrik.lundin): This condition should probably be
515 // in_frame.num_channels_ > encoder_stack_->NumChannels()
516 const bool down_mix =
517 in_frame.num_channels_ == 2 && encoder_stack_->NumChannels() == 1;
518
519 if (!first_10ms_data_) {
520 expected_in_ts_ = in_frame.timestamp_;
521 expected_codec_ts_ = in_frame.timestamp_;
522 first_10ms_data_ = true;
523 } else if (in_frame.timestamp_ != expected_in_ts_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100524 RTC_LOG(LS_WARNING) << "Unexpected input timestamp: " << in_frame.timestamp_
525 << ", expected: " << expected_in_ts_;
kwibergc13ded52016-06-17 06:00:45 -0700526 expected_codec_ts_ +=
527 (in_frame.timestamp_ - expected_in_ts_) *
528 static_cast<uint32_t>(
529 static_cast<double>(encoder_stack_->SampleRateHz()) /
530 static_cast<double>(in_frame.sample_rate_hz_));
531 expected_in_ts_ = in_frame.timestamp_;
532 }
533
kwibergc13ded52016-06-17 06:00:45 -0700534 if (!down_mix && !resample) {
535 // No pre-processing is required.
ossu63fb95a2016-07-06 09:34:22 -0700536 if (expected_in_ts_ == expected_codec_ts_) {
537 // If we've never resampled, we can use the input frame as-is
538 *ptr_out = &in_frame;
539 } else {
540 // Otherwise we'll need to alter the timestamp. Since in_frame is const,
541 // we'll have to make a copy of it.
542 preprocess_frame_.CopyFrom(in_frame);
543 preprocess_frame_.timestamp_ = expected_codec_ts_;
544 *ptr_out = &preprocess_frame_;
545 }
546
kwibergc13ded52016-06-17 06:00:45 -0700547 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
548 expected_codec_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
kwibergc13ded52016-06-17 06:00:45 -0700549 return 0;
550 }
551
552 *ptr_out = &preprocess_frame_;
553 preprocess_frame_.num_channels_ = in_frame.num_channels_;
554 int16_t audio[WEBRTC_10MS_PCM_AUDIO];
yujo36b1a5f2017-06-12 12:45:32 -0700555 const int16_t* src_ptr_audio = in_frame.data();
kwibergc13ded52016-06-17 06:00:45 -0700556 if (down_mix) {
557 // If a resampling is required the output of a down-mix is written into a
558 // local buffer, otherwise, it will be written to the output frame.
Yves Gerey665174f2018-06-19 15:03:05 +0200559 int16_t* dest_ptr_audio =
560 resample ? audio : preprocess_frame_.mutable_data();
Per Åhgren4f2e9402019-10-04 11:06:15 +0200561 DownMix(in_frame, WEBRTC_10MS_PCM_AUDIO, dest_ptr_audio);
kwibergc13ded52016-06-17 06:00:45 -0700562 preprocess_frame_.num_channels_ = 1;
563 // Set the input of the resampler is the down-mixed signal.
564 src_ptr_audio = audio;
565 }
566
567 preprocess_frame_.timestamp_ = expected_codec_ts_;
568 preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_;
569 preprocess_frame_.sample_rate_hz_ = in_frame.sample_rate_hz_;
570 // If it is required, we have to do a resampling.
571 if (resample) {
572 // The result of the resampler is written to output frame.
yujo36b1a5f2017-06-12 12:45:32 -0700573 int16_t* dest_ptr_audio = preprocess_frame_.mutable_data();
kwibergc13ded52016-06-17 06:00:45 -0700574
575 int samples_per_channel = resampler_.Resample10Msec(
576 src_ptr_audio, in_frame.sample_rate_hz_, encoder_stack_->SampleRateHz(),
577 preprocess_frame_.num_channels_, AudioFrame::kMaxDataSizeSamples,
578 dest_ptr_audio);
579
580 if (samples_per_channel < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100581 RTC_LOG(LS_ERROR) << "Cannot add 10 ms audio, resampling failed";
kwibergc13ded52016-06-17 06:00:45 -0700582 return -1;
583 }
584 preprocess_frame_.samples_per_channel_ =
585 static_cast<size_t>(samples_per_channel);
586 preprocess_frame_.sample_rate_hz_ = encoder_stack_->SampleRateHz();
587 }
588
589 expected_codec_ts_ +=
590 static_cast<uint32_t>(preprocess_frame_.samples_per_channel_);
591 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
592
593 return 0;
594}
595
596/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700597// (FEC) Forward Error Correction (codec internal)
598//
599
kwibergc13ded52016-06-17 06:00:45 -0700600int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) {
601 rtc::CritScope lock(&acm_crit_sect_);
602 if (HaveValidEncoder("SetPacketLossRate")) {
minyue4b9a2cb2016-11-30 06:49:59 -0800603 encoder_stack_->OnReceivedUplinkPacketLossFraction(loss_rate / 100.0);
kwibergc13ded52016-06-17 06:00:45 -0700604 }
605 return 0;
606}
607
608/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700609// Receiver
610//
611
612int AudioCodingModuleImpl::InitializeReceiver() {
613 rtc::CritScope lock(&acm_crit_sect_);
614 return InitializeReceiverSafe();
615}
616
617// Initialize receiver, resets codec database etc.
618int AudioCodingModuleImpl::InitializeReceiverSafe() {
619 // If the receiver is already initialized then we want to destroy any
620 // existing decoders. After a call to this function, we should have a clean
621 // start-up.
kwiberg6b19b562016-09-20 04:02:25 -0700622 if (receiver_initialized_)
623 receiver_.RemoveAllCodecs();
kwibergc13ded52016-06-17 06:00:45 -0700624 receiver_.FlushBuffers();
625
kwibergc13ded52016-06-17 06:00:45 -0700626 receiver_initialized_ = true;
627 return 0;
628}
629
kwiberg1c07c702017-03-27 07:15:49 -0700630void AudioCodingModuleImpl::SetReceiveCodecs(
631 const std::map<int, SdpAudioFormat>& codecs) {
632 rtc::CritScope lock(&acm_crit_sect_);
633 receiver_.SetCodecs(codecs);
634}
635
kwibergc13ded52016-06-17 06:00:45 -0700636// Incoming packet from network parsed and ready for decode.
637int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload,
638 const size_t payload_length,
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100639 const RTPHeader& rtp_header) {
henrik.lundinb8c55b12017-05-10 07:38:01 -0700640 RTC_DCHECK_EQ(payload_length == 0, incoming_payload == nullptr);
kwibergc13ded52016-06-17 06:00:45 -0700641 return receiver_.InsertPacket(
642 rtp_header,
643 rtc::ArrayView<const uint8_t>(incoming_payload, payload_length));
644}
645
kwibergc13ded52016-06-17 06:00:45 -0700646// Get 10 milliseconds of raw audio data to play out.
647// Automatic resample to the requested frequency.
648int AudioCodingModuleImpl::PlayoutData10Ms(int desired_freq_hz,
649 AudioFrame* audio_frame,
650 bool* muted) {
651 // GetAudio always returns 10 ms, at the requested sample rate.
652 if (receiver_.GetAudio(desired_freq_hz, audio_frame, muted) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100653 RTC_LOG(LS_ERROR) << "PlayoutData failed, RecOut Failed";
kwibergc13ded52016-06-17 06:00:45 -0700654 return -1;
655 }
kwibergc13ded52016-06-17 06:00:45 -0700656 return 0;
657}
658
kwibergc13ded52016-06-17 06:00:45 -0700659/////////////////////////////////////////
660// Statistics
661//
662
663// TODO(turajs) change the return value to void. Also change the corresponding
664// NetEq function.
665int AudioCodingModuleImpl::GetNetworkStatistics(NetworkStatistics* statistics) {
666 receiver_.GetNetworkStatistics(statistics);
667 return 0;
668}
669
670int AudioCodingModuleImpl::RegisterVADCallback(ACMVADCallback* vad_callback) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100671 RTC_LOG(LS_VERBOSE) << "RegisterVADCallback()";
kwibergc13ded52016-06-17 06:00:45 -0700672 rtc::CritScope lock(&callback_crit_sect_);
673 vad_callback_ = vad_callback;
674 return 0;
675}
676
kwibergc13ded52016-06-17 06:00:45 -0700677bool AudioCodingModuleImpl::HaveValidEncoder(const char* caller_name) const {
678 if (!encoder_stack_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100679 RTC_LOG(LS_ERROR) << caller_name << " failed: No send codec is registered.";
kwibergc13ded52016-06-17 06:00:45 -0700680 return false;
681 }
682 return true;
683}
684
ivoce1198e02017-09-08 08:13:19 -0700685ANAStats AudioCodingModuleImpl::GetANAStats() const {
686 rtc::CritScope lock(&acm_crit_sect_);
687 if (encoder_stack_)
688 return encoder_stack_->GetANAStats();
689 // If no encoder is set, return default stats.
690 return ANAStats();
691}
692
kwibergc13ded52016-06-17 06:00:45 -0700693} // namespace
694
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200695AudioCodingModule::Config::Config(
696 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
697 : neteq_config(),
698 clock(Clock::GetRealTimeClock()),
699 decoder_factory(decoder_factory) {
kwiberg36a43882016-08-29 05:33:32 -0700700 // Post-decode VAD is disabled by default in NetEq, however, Audio
701 // Conference Mixer relies on VAD decisions and fails without them.
702 neteq_config.enable_post_decode_vad = true;
703}
704
705AudioCodingModule::Config::Config(const Config&) = default;
706AudioCodingModule::Config::~Config() = default;
707
Henrik Lundin64dad832015-05-11 12:44:23 +0200708AudioCodingModule* AudioCodingModule::Create(const Config& config) {
kwibergc13ded52016-06-17 06:00:45 -0700709 return new AudioCodingModuleImpl(config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000710}
711
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000712} // namespace webrtc