blob: a2d08ac004ca7f3d54547a0ff781c726e9fc5119 [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>
Jonathan Yu36344a02017-07-30 01:55:34 -070014#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
Jonathan Yu36344a02017-07-30 01:55:34 -070016
Niels Möller2edab4c2018-10-22 09:48:08 +020017#include "absl/strings/match.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/acm2/acm_receiver.h"
Per Åhgren4dd56a32019-11-19 21:00:59 +010020#include "modules/audio_coding/acm2/acm_remixing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#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
Per Åhgrend82a02c2020-03-12 11:53:30 +010040constexpr int32_t kMaxInputSampleRateHz = 192000;
41
kwibergc13ded52016-06-17 06:00:45 -070042class AudioCodingModuleImpl final : public AudioCodingModule {
43 public:
44 explicit AudioCodingModuleImpl(const AudioCodingModule::Config& config);
45 ~AudioCodingModuleImpl() override;
46
47 /////////////////////////////////////////
48 // Sender
49 //
50
kwiberg24c7c122016-09-28 11:57:10 -070051 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
52 modifier) override;
kwibergc13ded52016-06-17 06:00:45 -070053
kwibergc13ded52016-06-17 06:00:45 -070054 // 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 /////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -070062 // (FEC) Forward Error Correction (codec internal)
63 //
64
kwibergc13ded52016-06-17 06:00:45 -070065 // Set target packet loss rate
66 int SetPacketLossRate(int loss_rate) override;
67
68 /////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -070069 // Receiver
70 //
71
72 // Initialize receiver, resets codec database etc.
73 int InitializeReceiver() override;
74
kwiberg1c07c702017-03-27 07:15:49 -070075 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
76
kwibergc13ded52016-06-17 06:00:45 -070077 // Incoming packet from network parsed and ready for decode.
78 int IncomingPacket(const uint8_t* incoming_payload,
79 const size_t payload_length,
Niels Möllerafb5dbb2019-02-15 15:21:47 +010080 const RTPHeader& rtp_info) override;
kwibergc13ded52016-06-17 06:00:45 -070081
kwibergc13ded52016-06-17 06:00:45 -070082 // 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;
kwibergc13ded52016-06-17 06:00:45 -070087
88 /////////////////////////////////////////
89 // Statistics
90 //
91
92 int GetNetworkStatistics(NetworkStatistics* statistics) override;
93
ivoce1198e02017-09-08 08:13:19 -070094 ANAStats GetANAStats() const override;
95
kwibergc13ded52016-06-17 06:00:45 -070096 private:
97 struct InputData {
Per Åhgren4f2e9402019-10-04 11:06:15 +020098 InputData() : buffer(kInitialInputDataBufferSize) {}
kwibergc13ded52016-06-17 06:00:45 -070099 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.
Per Åhgren4f2e9402019-10-04 11:06:15 +0200105 std::vector<int16_t> buffer;
kwibergc13ded52016-06-17 06:00:45 -0700106 };
107
Per Åhgren4f2e9402019-10-04 11:06:15 +0200108 InputData input_data_ RTC_GUARDED_BY(acm_crit_sect_);
109
kwibergc13ded52016-06-17 06:00:45 -0700110 // 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:
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
kwibergc13ded52016-06-17 06:00:45 -0700126 int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data)
danilchap56359be2017-09-07 07:53:45 -0700127 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
Minyue Lidea73ee2020-02-18 15:45:41 +0100128
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)
danilchap56359be2017-09-07 07:53:45 -0700133 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700134
danilchap56359be2017-09-07 07:53:45 -0700135 int InitializeReceiverSafe() RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700136
137 bool HaveValidEncoder(const char* caller_name) const
danilchap56359be2017-09-07 07:53:45 -0700138 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700139
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)
danilchap56359be2017-09-07 07:53:45 -0700153 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700154
155 // Change required states after starting to receive the codec corresponding
156 // to |index|.
157 int UpdateUponReceivingCodec(int index);
158
159 rtc::CriticalSection acm_crit_sect_;
danilchap56359be2017-09-07 07:53:45 -0700160 rtc::Buffer encode_buffer_ RTC_GUARDED_BY(acm_crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700161 uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_crit_sect_);
162 uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_crit_sect_);
163 acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700164 acm2::AcmReceiver receiver_; // AcmReceiver has it's own internal lock.
danilchap56359be2017-09-07 07:53:45 -0700165 ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700166
Karl Wiberg49c33ce2018-11-12 14:21:58 +0100167 // Current encoder stack, provided by a call to RegisterEncoder.
danilchap56359be2017-09-07 07:53:45 -0700168 std::unique_ptr<AudioEncoder> encoder_stack_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700169
kwibergc13ded52016-06-17 06:00:45 -0700170 // This is to keep track of CN instances where we can send DTMFs.
danilchap56359be2017-09-07 07:53:45 -0700171 uint8_t previous_pltype_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700172
danilchap56359be2017-09-07 07:53:45 -0700173 bool receiver_initialized_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700174
danilchap56359be2017-09-07 07:53:45 -0700175 AudioFrame preprocess_frame_ RTC_GUARDED_BY(acm_crit_sect_);
176 bool first_10ms_data_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700177
danilchap56359be2017-09-07 07:53:45 -0700178 bool first_frame_ RTC_GUARDED_BY(acm_crit_sect_);
179 uint32_t last_timestamp_ RTC_GUARDED_BY(acm_crit_sect_);
180 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700181
182 rtc::CriticalSection callback_crit_sect_;
183 AudioPacketizationCallback* packetization_callback_
danilchap56359be2017-09-07 07:53:45 -0700184 RTC_GUARDED_BY(callback_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700185
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.
192void 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
kwibergc13ded52016-06-17 06:00:45 -0700199void 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
207AudioCodingModuleImpl::AudioCodingModuleImpl(
208 const AudioCodingModule::Config& config)
solenbergc7b4a452017-09-28 07:37:11 -0700209 : expected_codec_ts_(0xD87F3F9F),
kwibergc13ded52016-06-17 06:00:45 -0700210 expected_in_ts_(0xD87F3F9F),
211 receiver_(config),
212 bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"),
kwibergc13ded52016-06-17 06:00:45 -0700213 encoder_stack_(nullptr),
214 previous_pltype_(255),
215 receiver_initialized_(false),
216 first_10ms_data_(false),
217 first_frame_(true),
218 packetization_callback_(NULL),
kwibergc13ded52016-06-17 06:00:45 -0700219 codec_histogram_bins_log_(),
220 number_of_consecutive_empty_packets_(0) {
221 if (InitializeReceiverSafe() < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100222 RTC_LOG(LS_ERROR) << "Cannot initialize receiver";
kwibergc13ded52016-06-17 06:00:45 -0700223 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100224 RTC_LOG(LS_INFO) << "Created";
kwibergc13ded52016-06-17 06:00:45 -0700225}
226
227AudioCodingModuleImpl::~AudioCodingModuleImpl() = default;
228
Minyue Lidea73ee2020-02-18 15:45:41 +0100229int32_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.
kwibergc13ded52016-06-17 06:00:45 -0700234 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
Yves Gerey665174f2018-06-19 15:03:05 +0200241 if (!first_frame_) {
deadbeeffcada902016-08-24 12:45:13 -0700242 RTC_DCHECK(IsNewerTimestamp(input_data.input_timestamp, last_timestamp_))
ossu63fb95a2016-07-06 09:34:22 -0700243 << "Time should not move backwards";
244 }
245
kwibergc13ded52016-06-17 06:00:45 -0700246 // Scale the timestamp to the codec's RTP timestamp rate.
247 uint32_t rtp_timestamp =
Karl Wiberg053c3712019-05-16 15:24:17 +0200248 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()}));
Minyue Liff0e4db2020-01-23 13:45:50 +0100255
kwibergc13ded52016-06-17 06:00:45 -0700256 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(
Yves Gerey665174f2018-06-19 15:03:05 +0200263 rtp_timestamp,
264 rtc::ArrayView<const int16_t>(
265 input_data.audio,
266 input_data.audio_channel * input_data.length_per_channel),
kwibergc13ded52016-06-17 06:00:45 -0700267 &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
Niels Möller87e2d782019-03-07 10:18:23 +0100290 AudioFrameType frame_type;
kwibergc13ded52016-06-17 06:00:45 -0700291 if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) {
Niels Möllerc936cb62019-03-19 14:10:16 +0100292 frame_type = AudioFrameType::kEmptyFrame;
kwibergc13ded52016-06-17 06:00:45 -0700293 encoded_info.payload_type = previous_pltype;
294 } else {
kwibergaf476c72016-11-28 15:21:39 -0800295 RTC_DCHECK_GT(encode_buffer_.size(), 0);
Niels Möllerc936cb62019-03-19 14:10:16 +0100296 frame_type = encoded_info.speech ? AudioFrameType::kAudioFrameSpeech
297 : AudioFrameType::kAudioFrameCN;
kwibergc13ded52016-06-17 06:00:45 -0700298 }
299
300 {
301 rtc::CritScope lock(&callback_crit_sect_);
302 if (packetization_callback_) {
303 packetization_callback_->SendData(
304 frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp,
Minyue Liff0e4db2020-01-23 13:45:50 +0100305 encode_buffer_.data(), encode_buffer_.size(),
Minyue Lidea73ee2020-02-18 15:45:41 +0100306 absolute_capture_timestamp_ms.value_or(-1));
kwibergc13ded52016-06-17 06:00:45 -0700307 }
kwibergc13ded52016-06-17 06:00:45 -0700308 }
309 previous_pltype_ = encoded_info.payload_type;
310 return static_cast<int32_t>(encode_buffer_.size());
311}
312
313/////////////////////////////////////////
314// Sender
315//
316
kwibergc13ded52016-06-17 06:00:45 -0700317void AudioCodingModuleImpl::ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -0700318 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
kwibergc13ded52016-06-17 06:00:45 -0700319 rtc::CritScope lock(&acm_crit_sect_);
kwibergc13ded52016-06-17 06:00:45 -0700320 modifier(&encoder_stack_);
321}
322
kwibergc13ded52016-06-17 06:00:45 -0700323// Register a transport callback which will be called to deliver
324// the encoded buffers.
325int AudioCodingModuleImpl::RegisterTransportCallback(
326 AudioPacketizationCallback* transport) {
327 rtc::CritScope lock(&callback_crit_sect_);
328 packetization_callback_ = transport;
329 return 0;
330}
331
332// Add 10MS of raw (PCM) audio data to the encoder.
333int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) {
kwibergc13ded52016-06-17 06:00:45 -0700334 rtc::CritScope lock(&acm_crit_sect_);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200335 int r = Add10MsDataInternal(audio_frame, &input_data_);
Minyue Lidea73ee2020-02-18 15:45:41 +0100336 // 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());
kwibergc13ded52016-06-17 06:00:45 -0700341}
342
343int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame,
344 InputData* input_data) {
345 if (audio_frame.samples_per_channel_ == 0) {
346 assert(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100347 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, payload length is zero";
kwibergc13ded52016-06-17 06:00:45 -0700348 return -1;
349 }
350
Per Åhgrend82a02c2020-03-12 11:53:30 +0100351 if (audio_frame.sample_rate_hz_ > kMaxInputSampleRateHz) {
kwibergc13ded52016-06-17 06:00:45 -0700352 assert(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100353 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, input frequency not valid";
kwibergc13ded52016-06-17 06:00:45 -0700354 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_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG(LS_ERROR)
Alex Loiko300ec8c2017-05-30 17:23:28 +0200361 << "Cannot Add 10 ms audio, input frequency and length doesn't match";
kwibergc13ded52016-06-17 06:00:45 -0700362 return -1;
363 }
364
Alex Loiko65438812019-02-22 10:13:44 +0100365 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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100368 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, invalid number of channels.";
kwibergc13ded52016-06-17 06:00:45 -0700369 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
yujo36b1a5f2017-06-12 12:45:32 -0700391 // TODO(yujo): Skip encode of muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700392 input_data->input_timestamp = ptr_frame->timestamp_;
kwibergc13ded52016-06-17 06:00:45 -0700393 input_data->length_per_channel = ptr_frame->samples_per_channel_;
394 input_data->audio_channel = current_num_channels;
395
Per Åhgren4f2e9402019-10-04 11:06:15 +0200396 if (!same_num_channels) {
397 // Remixes the input frame to the output data and in the process resize the
398 // output data if needed.
Per Åhgren4dd56a32019-11-19 21:00:59 +0100399 ReMixFrame(*ptr_frame, current_num_channels, &input_data->buffer);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200400
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
kwibergc13ded52016-06-17 06:00:45 -0700411 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|.
yujo36b1a5f2017-06-12 12:45:32 -0700419// TODO(yujo): Make this more efficient for muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700420int 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_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100437 RTC_LOG(LS_WARNING) << "Unexpected input timestamp: " << in_frame.timestamp_
438 << ", expected: " << expected_in_ts_;
kwibergc13ded52016-06-17 06:00:45 -0700439 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
kwibergc13ded52016-06-17 06:00:45 -0700447 if (!down_mix && !resample) {
448 // No pre-processing is required.
ossu63fb95a2016-07-06 09:34:22 -0700449 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
kwibergc13ded52016-06-17 06:00:45 -0700460 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_);
kwibergc13ded52016-06-17 06:00:45 -0700462 return 0;
463 }
464
465 *ptr_out = &preprocess_frame_;
466 preprocess_frame_.num_channels_ = in_frame.num_channels_;
Per Åhgren4dd56a32019-11-19 21:00:59 +0100467 preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100468 std::array<int16_t, AudioFrame::kMaxDataSizeSamples> audio;
469 const int16_t* src_ptr_audio;
kwibergc13ded52016-06-17 06:00:45 -0700470 if (down_mix) {
Per Åhgrend82a02c2020-03-12 11:53:30 +0100471 // If a resampling is required, the output of a down-mix is written into a
kwibergc13ded52016-06-17 06:00:45 -0700472 // local buffer, otherwise, it will be written to the output frame.
Yves Gerey665174f2018-06-19 15:03:05 +0200473 int16_t* dest_ptr_audio =
Per Åhgren4dd56a32019-11-19 21:00:59 +0100474 resample ? audio.data() : preprocess_frame_.mutable_data();
Per Åhgrend82a02c2020-03-12 11:53:30 +0100475 RTC_DCHECK_GE(audio.size(), preprocess_frame_.samples_per_channel_);
Per Åhgren4dd56a32019-11-19 21:00:59 +0100476 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_));
kwibergc13ded52016-06-17 06:00:45 -0700480 preprocess_frame_.num_channels_ = 1;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100481
482 // Set the input of the resampler to the down-mixed signal.
Per Åhgren4dd56a32019-11-19 21:00:59 +0100483 src_ptr_audio = audio.data();
Per Åhgrend82a02c2020-03-12 11:53:30 +0100484 } else {
485 // Set the input of the resampler to the original data.
486 src_ptr_audio = in_frame.data();
kwibergc13ded52016-06-17 06:00:45 -0700487 }
488
489 preprocess_frame_.timestamp_ = expected_codec_ts_;
kwibergc13ded52016-06-17 06:00:45 -0700490 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.
yujo36b1a5f2017-06-12 12:45:32 -0700494 int16_t* dest_ptr_audio = preprocess_frame_.mutable_data();
kwibergc13ded52016-06-17 06:00:45 -0700495
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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100502 RTC_LOG(LS_ERROR) << "Cannot add 10 ms audio, resampling failed";
kwibergc13ded52016-06-17 06:00:45 -0700503 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/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700518// (FEC) Forward Error Correction (codec internal)
519//
520
kwibergc13ded52016-06-17 06:00:45 -0700521int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) {
522 rtc::CritScope lock(&acm_crit_sect_);
523 if (HaveValidEncoder("SetPacketLossRate")) {
minyue4b9a2cb2016-11-30 06:49:59 -0800524 encoder_stack_->OnReceivedUplinkPacketLossFraction(loss_rate / 100.0);
kwibergc13ded52016-06-17 06:00:45 -0700525 }
526 return 0;
527}
528
529/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700530// Receiver
531//
532
533int AudioCodingModuleImpl::InitializeReceiver() {
534 rtc::CritScope lock(&acm_crit_sect_);
535 return InitializeReceiverSafe();
536}
537
538// Initialize receiver, resets codec database etc.
539int 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.
kwiberg6b19b562016-09-20 04:02:25 -0700543 if (receiver_initialized_)
544 receiver_.RemoveAllCodecs();
kwibergc13ded52016-06-17 06:00:45 -0700545 receiver_.FlushBuffers();
546
kwibergc13ded52016-06-17 06:00:45 -0700547 receiver_initialized_ = true;
548 return 0;
549}
550
kwiberg1c07c702017-03-27 07:15:49 -0700551void AudioCodingModuleImpl::SetReceiveCodecs(
552 const std::map<int, SdpAudioFormat>& codecs) {
553 rtc::CritScope lock(&acm_crit_sect_);
554 receiver_.SetCodecs(codecs);
555}
556
kwibergc13ded52016-06-17 06:00:45 -0700557// Incoming packet from network parsed and ready for decode.
558int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload,
559 const size_t payload_length,
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100560 const RTPHeader& rtp_header) {
henrik.lundinb8c55b12017-05-10 07:38:01 -0700561 RTC_DCHECK_EQ(payload_length == 0, incoming_payload == nullptr);
kwibergc13ded52016-06-17 06:00:45 -0700562 return receiver_.InsertPacket(
563 rtp_header,
564 rtc::ArrayView<const uint8_t>(incoming_payload, payload_length));
565}
566
kwibergc13ded52016-06-17 06:00:45 -0700567// Get 10 milliseconds of raw audio data to play out.
568// Automatic resample to the requested frequency.
569int 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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100574 RTC_LOG(LS_ERROR) << "PlayoutData failed, RecOut Failed";
kwibergc13ded52016-06-17 06:00:45 -0700575 return -1;
576 }
kwibergc13ded52016-06-17 06:00:45 -0700577 return 0;
578}
579
kwibergc13ded52016-06-17 06:00:45 -0700580/////////////////////////////////////////
581// Statistics
582//
583
584// TODO(turajs) change the return value to void. Also change the corresponding
585// NetEq function.
586int AudioCodingModuleImpl::GetNetworkStatistics(NetworkStatistics* statistics) {
587 receiver_.GetNetworkStatistics(statistics);
588 return 0;
589}
590
kwibergc13ded52016-06-17 06:00:45 -0700591bool AudioCodingModuleImpl::HaveValidEncoder(const char* caller_name) const {
592 if (!encoder_stack_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100593 RTC_LOG(LS_ERROR) << caller_name << " failed: No send codec is registered.";
kwibergc13ded52016-06-17 06:00:45 -0700594 return false;
595 }
596 return true;
597}
598
ivoce1198e02017-09-08 08:13:19 -0700599ANAStats AudioCodingModuleImpl::GetANAStats() const {
600 rtc::CritScope lock(&acm_crit_sect_);
601 if (encoder_stack_)
602 return encoder_stack_->GetANAStats();
603 // If no encoder is set, return default stats.
604 return ANAStats();
605}
606
kwibergc13ded52016-06-17 06:00:45 -0700607} // namespace
608
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200609AudioCodingModule::Config::Config(
610 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
611 : neteq_config(),
612 clock(Clock::GetRealTimeClock()),
613 decoder_factory(decoder_factory) {
kwiberg36a43882016-08-29 05:33:32 -0700614 // 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
619AudioCodingModule::Config::Config(const Config&) = default;
620AudioCodingModule::Config::~Config() = default;
621
Henrik Lundin64dad832015-05-11 12:44:23 +0200622AudioCodingModule* AudioCodingModule::Create(const Config& config) {
kwibergc13ded52016-06-17 06:00:45 -0700623 return new AudioCodingModuleImpl(config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000624}
625
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000626} // namespace webrtc