blob: 97a204ac4f9dba18e348fa928cecb66a7333506a [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
Jonathan Yu36344a02017-07-30 01:55:34 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
Jonathan Yu36344a02017-07-30 01:55:34 -070015
Niels Möller2edab4c2018-10-22 09:48:08 +020016#include "absl/strings/match.h"
Ali Tofigh714e3cb2022-07-20 12:53:07 +020017#include "absl/strings/string_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "api/array_view.h"
Per Åhgren4dd56a32019-11-19 21:00:59 +010019#include "modules/audio_coding/acm2/acm_remixing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/acm2/acm_resampler.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020021#include "modules/include/module_common_types.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "modules/include/module_common_types_public.h"
23#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010026#include "rtc_base/numerics/safe_conversions.h"
Markus Handell0df0fae2020-07-07 15:53:34 +020027#include "rtc_base/synchronization/mutex.h"
Yves Gerey988cc082018-10-23 12:03:01 +020028#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/metrics.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000030
31namespace webrtc {
32
kwibergc13ded52016-06-17 06:00:45 -070033namespace {
34
Per Åhgren4f2e9402019-10-04 11:06:15 +020035// Initial size for the buffer in InputBuffer. This matches 6 channels of 10 ms
36// 48 kHz data.
37constexpr size_t kInitialInputDataBufferSize = 6 * 480;
38
Per Åhgrend82a02c2020-03-12 11:53:30 +010039constexpr int32_t kMaxInputSampleRateHz = 192000;
40
kwibergc13ded52016-06-17 06:00:45 -070041class AudioCodingModuleImpl final : public AudioCodingModule {
42 public:
Henrik Lundin84f75692023-02-01 12:07:10 +000043 explicit AudioCodingModuleImpl();
kwibergc13ded52016-06-17 06:00:45 -070044 ~AudioCodingModuleImpl() override;
45
46 /////////////////////////////////////////
47 // Sender
48 //
49
kwiberg24c7c122016-09-28 11:57:10 -070050 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
51 modifier) override;
kwibergc13ded52016-06-17 06:00:45 -070052
kwibergc13ded52016-06-17 06:00:45 -070053 // Register a transport callback which will be
54 // called to deliver the encoded buffers.
55 int RegisterTransportCallback(AudioPacketizationCallback* transport) override;
56
57 // Add 10 ms of raw (PCM) audio data to the encoder.
58 int Add10MsData(const AudioFrame& audio_frame) override;
59
60 /////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -070061 // (FEC) Forward Error Correction (codec internal)
62 //
63
kwibergc13ded52016-06-17 06:00:45 -070064 // Set target packet loss rate
65 int SetPacketLossRate(int loss_rate) override;
66
67 /////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -070068 // Statistics
69 //
70
ivoce1198e02017-09-08 08:13:19 -070071 ANAStats GetANAStats() const override;
72
Jakob Ivarssonbf087452021-11-11 13:43:49 +010073 int GetTargetBitrate() const override;
74
kwibergc13ded52016-06-17 06:00:45 -070075 private:
76 struct InputData {
Per Åhgren4f2e9402019-10-04 11:06:15 +020077 InputData() : buffer(kInitialInputDataBufferSize) {}
kwibergc13ded52016-06-17 06:00:45 -070078 uint32_t input_timestamp;
79 const int16_t* audio;
80 size_t length_per_channel;
81 size_t audio_channel;
82 // If a re-mix is required (up or down), this buffer will store a re-mixed
83 // version of the input.
Per Åhgren4f2e9402019-10-04 11:06:15 +020084 std::vector<int16_t> buffer;
kwibergc13ded52016-06-17 06:00:45 -070085 };
86
Markus Handell0df0fae2020-07-07 15:53:34 +020087 InputData input_data_ RTC_GUARDED_BY(acm_mutex_);
Per Åhgren4f2e9402019-10-04 11:06:15 +020088
kwibergc13ded52016-06-17 06:00:45 -070089 // This member class writes values to the named UMA histogram, but only if
90 // the value has changed since the last time (and always for the first call).
91 class ChangeLogger {
92 public:
Ali Tofigh714e3cb2022-07-20 12:53:07 +020093 explicit ChangeLogger(absl::string_view histogram_name)
kwibergc13ded52016-06-17 06:00:45 -070094 : histogram_name_(histogram_name) {}
95 // Logs the new value if it is different from the last logged value, or if
96 // this is the first call.
97 void MaybeLog(int value);
98
99 private:
100 int last_value_ = 0;
101 int first_time_ = true;
102 const std::string histogram_name_;
103 };
104
kwibergc13ded52016-06-17 06:00:45 -0700105 int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data)
Markus Handell0df0fae2020-07-07 15:53:34 +0200106 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
Minyue Lidea73ee2020-02-18 15:45:41 +0100107
Artem Titovd00ce742021-07-28 20:00:17 +0200108 // TODO(bugs.webrtc.org/10739): change `absolute_capture_timestamp_ms` to
Minyue Lidea73ee2020-02-18 15:45:41 +0100109 // int64_t when it always receives a valid value.
110 int Encode(const InputData& input_data,
111 absl::optional<int64_t> absolute_capture_timestamp_ms)
Markus Handell0df0fae2020-07-07 15:53:34 +0200112 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700113
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200114 bool HaveValidEncoder(absl::string_view caller_name) const
Markus Handell0df0fae2020-07-07 15:53:34 +0200115 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700116
117 // Preprocessing of input audio, including resampling and down-mixing if
118 // required, before pushing audio into encoder's buffer.
119 //
120 // in_frame: input audio-frame
121 // ptr_out: pointer to output audio_frame. If no preprocessing is required
Artem Titovd00ce742021-07-28 20:00:17 +0200122 // `ptr_out` will be pointing to `in_frame`, otherwise pointing to
123 // `preprocess_frame_`.
kwibergc13ded52016-06-17 06:00:45 -0700124 //
125 // Return value:
126 // -1: if encountering an error.
127 // 0: otherwise.
128 int PreprocessToAddData(const AudioFrame& in_frame,
129 const AudioFrame** ptr_out)
Markus Handell0df0fae2020-07-07 15:53:34 +0200130 RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700131
132 // Change required states after starting to receive the codec corresponding
Artem Titovd00ce742021-07-28 20:00:17 +0200133 // to `index`.
kwibergc13ded52016-06-17 06:00:45 -0700134 int UpdateUponReceivingCodec(int index);
135
Markus Handell0df0fae2020-07-07 15:53:34 +0200136 mutable Mutex acm_mutex_;
137 rtc::Buffer encode_buffer_ RTC_GUARDED_BY(acm_mutex_);
138 uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_mutex_);
139 uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_mutex_);
140 acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_mutex_);
Markus Handell0df0fae2020-07-07 15:53:34 +0200141 ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700142
Karl Wiberg49c33ce2018-11-12 14:21:58 +0100143 // Current encoder stack, provided by a call to RegisterEncoder.
Markus Handell0df0fae2020-07-07 15:53:34 +0200144 std::unique_ptr<AudioEncoder> encoder_stack_ RTC_GUARDED_BY(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700145
kwibergc13ded52016-06-17 06:00:45 -0700146 // This is to keep track of CN instances where we can send DTMFs.
Markus Handell0df0fae2020-07-07 15:53:34 +0200147 uint8_t previous_pltype_ RTC_GUARDED_BY(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700148
Markus Handell0df0fae2020-07-07 15:53:34 +0200149 AudioFrame preprocess_frame_ RTC_GUARDED_BY(acm_mutex_);
150 bool first_10ms_data_ RTC_GUARDED_BY(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700151
Markus Handell0df0fae2020-07-07 15:53:34 +0200152 bool first_frame_ RTC_GUARDED_BY(acm_mutex_);
153 uint32_t last_timestamp_ RTC_GUARDED_BY(acm_mutex_);
154 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700155
Markus Handell0df0fae2020-07-07 15:53:34 +0200156 Mutex callback_mutex_;
kwibergc13ded52016-06-17 06:00:45 -0700157 AudioPacketizationCallback* packetization_callback_
Markus Handell0df0fae2020-07-07 15:53:34 +0200158 RTC_GUARDED_BY(callback_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700159
160 int codec_histogram_bins_log_[static_cast<size_t>(
161 AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)];
162 int number_of_consecutive_empty_packets_;
163};
164
165// Adds a codec usage sample to the histogram.
166void UpdateCodecTypeHistogram(size_t codec_type) {
167 RTC_HISTOGRAM_ENUMERATION(
168 "WebRTC.Audio.Encoder.CodecType", static_cast<int>(codec_type),
169 static_cast<int>(
170 webrtc::AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes));
171}
172
kwibergc13ded52016-06-17 06:00:45 -0700173void AudioCodingModuleImpl::ChangeLogger::MaybeLog(int value) {
174 if (value != last_value_ || first_time_) {
175 first_time_ = false;
176 last_value_ = value;
177 RTC_HISTOGRAM_COUNTS_SPARSE_100(histogram_name_, value);
178 }
179}
180
Henrik Lundin84f75692023-02-01 12:07:10 +0000181AudioCodingModuleImpl::AudioCodingModuleImpl()
solenbergc7b4a452017-09-28 07:37:11 -0700182 : expected_codec_ts_(0xD87F3F9F),
kwibergc13ded52016-06-17 06:00:45 -0700183 expected_in_ts_(0xD87F3F9F),
kwibergc13ded52016-06-17 06:00:45 -0700184 bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"),
kwibergc13ded52016-06-17 06:00:45 -0700185 encoder_stack_(nullptr),
186 previous_pltype_(255),
kwibergc13ded52016-06-17 06:00:45 -0700187 first_10ms_data_(false),
188 first_frame_(true),
189 packetization_callback_(NULL),
kwibergc13ded52016-06-17 06:00:45 -0700190 codec_histogram_bins_log_(),
191 number_of_consecutive_empty_packets_(0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100192 RTC_LOG(LS_INFO) << "Created";
kwibergc13ded52016-06-17 06:00:45 -0700193}
194
195AudioCodingModuleImpl::~AudioCodingModuleImpl() = default;
196
Minyue Lidea73ee2020-02-18 15:45:41 +0100197int32_t AudioCodingModuleImpl::Encode(
198 const InputData& input_data,
199 absl::optional<int64_t> absolute_capture_timestamp_ms) {
200 // TODO(bugs.webrtc.org/10739): add dcheck that
Artem Titovcfea2182021-08-10 01:22:31 +0200201 // `audio_frame.absolute_capture_timestamp_ms()` always has a value.
kwibergc13ded52016-06-17 06:00:45 -0700202 AudioEncoder::EncodedInfo encoded_info;
203 uint8_t previous_pltype;
204
205 // Check if there is an encoder before.
206 if (!HaveValidEncoder("Process"))
207 return -1;
208
Yves Gerey665174f2018-06-19 15:03:05 +0200209 if (!first_frame_) {
deadbeeffcada902016-08-24 12:45:13 -0700210 RTC_DCHECK(IsNewerTimestamp(input_data.input_timestamp, last_timestamp_))
ossu63fb95a2016-07-06 09:34:22 -0700211 << "Time should not move backwards";
212 }
213
kwibergc13ded52016-06-17 06:00:45 -0700214 // Scale the timestamp to the codec's RTP timestamp rate.
215 uint32_t rtp_timestamp =
Karl Wiberg053c3712019-05-16 15:24:17 +0200216 first_frame_
217 ? input_data.input_timestamp
218 : last_rtp_timestamp_ +
219 rtc::dchecked_cast<uint32_t>(rtc::CheckedDivExact(
220 int64_t{input_data.input_timestamp - last_timestamp_} *
221 encoder_stack_->RtpTimestampRateHz(),
222 int64_t{encoder_stack_->SampleRateHz()}));
Minyue Liff0e4db2020-01-23 13:45:50 +0100223
kwibergc13ded52016-06-17 06:00:45 -0700224 last_timestamp_ = input_data.input_timestamp;
225 last_rtp_timestamp_ = rtp_timestamp;
226 first_frame_ = false;
227
228 // Clear the buffer before reuse - encoded data will get appended.
229 encode_buffer_.Clear();
230 encoded_info = encoder_stack_->Encode(
Yves Gerey665174f2018-06-19 15:03:05 +0200231 rtp_timestamp,
232 rtc::ArrayView<const int16_t>(
233 input_data.audio,
234 input_data.audio_channel * input_data.length_per_channel),
kwibergc13ded52016-06-17 06:00:45 -0700235 &encode_buffer_);
236
237 bitrate_logger_.MaybeLog(encoder_stack_->GetTargetBitrate() / 1000);
238 if (encode_buffer_.size() == 0 && !encoded_info.send_even_if_empty) {
239 // Not enough data.
240 return 0;
241 }
242 previous_pltype = previous_pltype_; // Read it while we have the critsect.
243
244 // Log codec type to histogram once every 500 packets.
245 if (encoded_info.encoded_bytes == 0) {
246 ++number_of_consecutive_empty_packets_;
247 } else {
248 size_t codec_type = static_cast<size_t>(encoded_info.encoder_type);
249 codec_histogram_bins_log_[codec_type] +=
250 number_of_consecutive_empty_packets_ + 1;
251 number_of_consecutive_empty_packets_ = 0;
252 if (codec_histogram_bins_log_[codec_type] >= 500) {
253 codec_histogram_bins_log_[codec_type] -= 500;
254 UpdateCodecTypeHistogram(codec_type);
255 }
256 }
257
Niels Möller87e2d782019-03-07 10:18:23 +0100258 AudioFrameType frame_type;
kwibergc13ded52016-06-17 06:00:45 -0700259 if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) {
Niels Möllerc936cb62019-03-19 14:10:16 +0100260 frame_type = AudioFrameType::kEmptyFrame;
kwibergc13ded52016-06-17 06:00:45 -0700261 encoded_info.payload_type = previous_pltype;
262 } else {
kwibergaf476c72016-11-28 15:21:39 -0800263 RTC_DCHECK_GT(encode_buffer_.size(), 0);
Niels Möllerc936cb62019-03-19 14:10:16 +0100264 frame_type = encoded_info.speech ? AudioFrameType::kAudioFrameSpeech
265 : AudioFrameType::kAudioFrameCN;
kwibergc13ded52016-06-17 06:00:45 -0700266 }
267
268 {
Markus Handell0df0fae2020-07-07 15:53:34 +0200269 MutexLock lock(&callback_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700270 if (packetization_callback_) {
271 packetization_callback_->SendData(
272 frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp,
Minyue Liff0e4db2020-01-23 13:45:50 +0100273 encode_buffer_.data(), encode_buffer_.size(),
Minyue Lidea73ee2020-02-18 15:45:41 +0100274 absolute_capture_timestamp_ms.value_or(-1));
kwibergc13ded52016-06-17 06:00:45 -0700275 }
kwibergc13ded52016-06-17 06:00:45 -0700276 }
277 previous_pltype_ = encoded_info.payload_type;
278 return static_cast<int32_t>(encode_buffer_.size());
279}
280
281/////////////////////////////////////////
282// Sender
283//
284
kwibergc13ded52016-06-17 06:00:45 -0700285void AudioCodingModuleImpl::ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -0700286 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
Markus Handell0df0fae2020-07-07 15:53:34 +0200287 MutexLock lock(&acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700288 modifier(&encoder_stack_);
289}
290
kwibergc13ded52016-06-17 06:00:45 -0700291// Register a transport callback which will be called to deliver
292// the encoded buffers.
293int AudioCodingModuleImpl::RegisterTransportCallback(
294 AudioPacketizationCallback* transport) {
Markus Handell0df0fae2020-07-07 15:53:34 +0200295 MutexLock lock(&callback_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700296 packetization_callback_ = transport;
297 return 0;
298}
299
300// Add 10MS of raw (PCM) audio data to the encoder.
301int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) {
Markus Handell0df0fae2020-07-07 15:53:34 +0200302 MutexLock lock(&acm_mutex_);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200303 int r = Add10MsDataInternal(audio_frame, &input_data_);
Minyue Lidea73ee2020-02-18 15:45:41 +0100304 // TODO(bugs.webrtc.org/10739): add dcheck that
Artem Titovcfea2182021-08-10 01:22:31 +0200305 // `audio_frame.absolute_capture_timestamp_ms()` always has a value.
Minyue Lidea73ee2020-02-18 15:45:41 +0100306 return r < 0
307 ? r
308 : Encode(input_data_, audio_frame.absolute_capture_timestamp_ms());
kwibergc13ded52016-06-17 06:00:45 -0700309}
310
311int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame,
312 InputData* input_data) {
313 if (audio_frame.samples_per_channel_ == 0) {
Artem Titovd3251962021-11-15 16:57:07 +0100314 RTC_DCHECK_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100315 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, payload length is zero";
kwibergc13ded52016-06-17 06:00:45 -0700316 return -1;
317 }
318
Per Åhgrend82a02c2020-03-12 11:53:30 +0100319 if (audio_frame.sample_rate_hz_ > kMaxInputSampleRateHz) {
Artem Titovd3251962021-11-15 16:57:07 +0100320 RTC_DCHECK_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100321 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, input frequency not valid";
kwibergc13ded52016-06-17 06:00:45 -0700322 return -1;
323 }
324
325 // If the length and frequency matches. We currently just support raw PCM.
326 if (static_cast<size_t>(audio_frame.sample_rate_hz_ / 100) !=
327 audio_frame.samples_per_channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100328 RTC_LOG(LS_ERROR)
Alex Loiko300ec8c2017-05-30 17:23:28 +0200329 << "Cannot Add 10 ms audio, input frequency and length doesn't match";
kwibergc13ded52016-06-17 06:00:45 -0700330 return -1;
331 }
332
Alex Loiko65438812019-02-22 10:13:44 +0100333 if (audio_frame.num_channels_ != 1 && audio_frame.num_channels_ != 2 &&
334 audio_frame.num_channels_ != 4 && audio_frame.num_channels_ != 6 &&
335 audio_frame.num_channels_ != 8) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100336 RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, invalid number of channels.";
kwibergc13ded52016-06-17 06:00:45 -0700337 return -1;
338 }
339
340 // Do we have a codec registered?
341 if (!HaveValidEncoder("Add10MsData")) {
342 return -1;
343 }
344
345 const AudioFrame* ptr_frame;
346 // Perform a resampling, also down-mix if it is required and can be
347 // performed before resampling (a down mix prior to resampling will take
348 // place if both primary and secondary encoders are mono and input is in
349 // stereo).
350 if (PreprocessToAddData(audio_frame, &ptr_frame) < 0) {
351 return -1;
352 }
353
354 // Check whether we need an up-mix or down-mix?
355 const size_t current_num_channels = encoder_stack_->NumChannels();
356 const bool same_num_channels =
357 ptr_frame->num_channels_ == current_num_channels;
358
yujo36b1a5f2017-06-12 12:45:32 -0700359 // TODO(yujo): Skip encode of muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700360 input_data->input_timestamp = ptr_frame->timestamp_;
kwibergc13ded52016-06-17 06:00:45 -0700361 input_data->length_per_channel = ptr_frame->samples_per_channel_;
362 input_data->audio_channel = current_num_channels;
363
Per Åhgren4f2e9402019-10-04 11:06:15 +0200364 if (!same_num_channels) {
365 // Remixes the input frame to the output data and in the process resize the
366 // output data if needed.
Per Åhgren4dd56a32019-11-19 21:00:59 +0100367 ReMixFrame(*ptr_frame, current_num_channels, &input_data->buffer);
Per Åhgren4f2e9402019-10-04 11:06:15 +0200368
Artem Titovd00ce742021-07-28 20:00:17 +0200369 // For pushing data to primary, point the `ptr_audio` to correct buffer.
Per Åhgren4f2e9402019-10-04 11:06:15 +0200370 input_data->audio = input_data->buffer.data();
371 RTC_DCHECK_GE(input_data->buffer.size(),
372 input_data->length_per_channel * input_data->audio_channel);
373 } else {
374 // When adding data to encoders this pointer is pointing to an audio buffer
375 // with correct number of channels.
376 input_data->audio = ptr_frame->data();
377 }
378
kwibergc13ded52016-06-17 06:00:45 -0700379 return 0;
380}
381
382// Perform a resampling and down-mix if required. We down-mix only if
383// encoder is mono and input is stereo. In case of dual-streaming, both
384// encoders has to be mono for down-mix to take place.
385// |*ptr_out| will point to the pre-processed audio-frame. If no pre-processing
Artem Titovd00ce742021-07-28 20:00:17 +0200386// is required, |*ptr_out| points to `in_frame`.
yujo36b1a5f2017-06-12 12:45:32 -0700387// TODO(yujo): Make this more efficient for muted frames.
kwibergc13ded52016-06-17 06:00:45 -0700388int AudioCodingModuleImpl::PreprocessToAddData(const AudioFrame& in_frame,
389 const AudioFrame** ptr_out) {
390 const bool resample =
391 in_frame.sample_rate_hz_ != encoder_stack_->SampleRateHz();
392
393 // This variable is true if primary codec and secondary codec (if exists)
394 // are both mono and input is stereo.
395 // TODO(henrik.lundin): This condition should probably be
396 // in_frame.num_channels_ > encoder_stack_->NumChannels()
397 const bool down_mix =
398 in_frame.num_channels_ == 2 && encoder_stack_->NumChannels() == 1;
399
400 if (!first_10ms_data_) {
401 expected_in_ts_ = in_frame.timestamp_;
402 expected_codec_ts_ = in_frame.timestamp_;
403 first_10ms_data_ = true;
404 } else if (in_frame.timestamp_ != expected_in_ts_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100405 RTC_LOG(LS_WARNING) << "Unexpected input timestamp: " << in_frame.timestamp_
406 << ", expected: " << expected_in_ts_;
kwibergc13ded52016-06-17 06:00:45 -0700407 expected_codec_ts_ +=
408 (in_frame.timestamp_ - expected_in_ts_) *
409 static_cast<uint32_t>(
410 static_cast<double>(encoder_stack_->SampleRateHz()) /
411 static_cast<double>(in_frame.sample_rate_hz_));
412 expected_in_ts_ = in_frame.timestamp_;
413 }
414
kwibergc13ded52016-06-17 06:00:45 -0700415 if (!down_mix && !resample) {
416 // No pre-processing is required.
ossu63fb95a2016-07-06 09:34:22 -0700417 if (expected_in_ts_ == expected_codec_ts_) {
418 // If we've never resampled, we can use the input frame as-is
419 *ptr_out = &in_frame;
420 } else {
421 // Otherwise we'll need to alter the timestamp. Since in_frame is const,
422 // we'll have to make a copy of it.
423 preprocess_frame_.CopyFrom(in_frame);
424 preprocess_frame_.timestamp_ = expected_codec_ts_;
425 *ptr_out = &preprocess_frame_;
426 }
427
kwibergc13ded52016-06-17 06:00:45 -0700428 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
429 expected_codec_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
kwibergc13ded52016-06-17 06:00:45 -0700430 return 0;
431 }
432
433 *ptr_out = &preprocess_frame_;
434 preprocess_frame_.num_channels_ = in_frame.num_channels_;
Per Åhgren4dd56a32019-11-19 21:00:59 +0100435 preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100436 std::array<int16_t, AudioFrame::kMaxDataSizeSamples> audio;
437 const int16_t* src_ptr_audio;
kwibergc13ded52016-06-17 06:00:45 -0700438 if (down_mix) {
Per Åhgrend82a02c2020-03-12 11:53:30 +0100439 // If a resampling is required, the output of a down-mix is written into a
kwibergc13ded52016-06-17 06:00:45 -0700440 // local buffer, otherwise, it will be written to the output frame.
Yves Gerey665174f2018-06-19 15:03:05 +0200441 int16_t* dest_ptr_audio =
Per Åhgren4dd56a32019-11-19 21:00:59 +0100442 resample ? audio.data() : preprocess_frame_.mutable_data();
Per Åhgrend82a02c2020-03-12 11:53:30 +0100443 RTC_DCHECK_GE(audio.size(), preprocess_frame_.samples_per_channel_);
Per Åhgren4dd56a32019-11-19 21:00:59 +0100444 RTC_DCHECK_GE(audio.size(), in_frame.samples_per_channel_);
445 DownMixFrame(in_frame,
446 rtc::ArrayView<int16_t>(
447 dest_ptr_audio, preprocess_frame_.samples_per_channel_));
kwibergc13ded52016-06-17 06:00:45 -0700448 preprocess_frame_.num_channels_ = 1;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100449
450 // Set the input of the resampler to the down-mixed signal.
Per Åhgren4dd56a32019-11-19 21:00:59 +0100451 src_ptr_audio = audio.data();
Per Åhgrend82a02c2020-03-12 11:53:30 +0100452 } else {
453 // Set the input of the resampler to the original data.
454 src_ptr_audio = in_frame.data();
kwibergc13ded52016-06-17 06:00:45 -0700455 }
456
457 preprocess_frame_.timestamp_ = expected_codec_ts_;
kwibergc13ded52016-06-17 06:00:45 -0700458 preprocess_frame_.sample_rate_hz_ = in_frame.sample_rate_hz_;
459 // If it is required, we have to do a resampling.
460 if (resample) {
461 // The result of the resampler is written to output frame.
yujo36b1a5f2017-06-12 12:45:32 -0700462 int16_t* dest_ptr_audio = preprocess_frame_.mutable_data();
kwibergc13ded52016-06-17 06:00:45 -0700463
464 int samples_per_channel = resampler_.Resample10Msec(
465 src_ptr_audio, in_frame.sample_rate_hz_, encoder_stack_->SampleRateHz(),
466 preprocess_frame_.num_channels_, AudioFrame::kMaxDataSizeSamples,
467 dest_ptr_audio);
468
469 if (samples_per_channel < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100470 RTC_LOG(LS_ERROR) << "Cannot add 10 ms audio, resampling failed";
kwibergc13ded52016-06-17 06:00:45 -0700471 return -1;
472 }
473 preprocess_frame_.samples_per_channel_ =
474 static_cast<size_t>(samples_per_channel);
475 preprocess_frame_.sample_rate_hz_ = encoder_stack_->SampleRateHz();
476 }
477
478 expected_codec_ts_ +=
479 static_cast<uint32_t>(preprocess_frame_.samples_per_channel_);
480 expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_);
481
482 return 0;
483}
484
485/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700486// (FEC) Forward Error Correction (codec internal)
487//
488
kwibergc13ded52016-06-17 06:00:45 -0700489int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) {
Markus Handell0df0fae2020-07-07 15:53:34 +0200490 MutexLock lock(&acm_mutex_);
kwibergc13ded52016-06-17 06:00:45 -0700491 if (HaveValidEncoder("SetPacketLossRate")) {
minyue4b9a2cb2016-11-30 06:49:59 -0800492 encoder_stack_->OnReceivedUplinkPacketLossFraction(loss_rate / 100.0);
kwibergc13ded52016-06-17 06:00:45 -0700493 }
494 return 0;
495}
496
497/////////////////////////////////////////
kwibergc13ded52016-06-17 06:00:45 -0700498// Statistics
499//
500
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200501bool AudioCodingModuleImpl::HaveValidEncoder(
502 absl::string_view caller_name) const {
kwibergc13ded52016-06-17 06:00:45 -0700503 if (!encoder_stack_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100504 RTC_LOG(LS_ERROR) << caller_name << " failed: No send codec is registered.";
kwibergc13ded52016-06-17 06:00:45 -0700505 return false;
506 }
507 return true;
508}
509
ivoce1198e02017-09-08 08:13:19 -0700510ANAStats AudioCodingModuleImpl::GetANAStats() const {
Markus Handell0df0fae2020-07-07 15:53:34 +0200511 MutexLock lock(&acm_mutex_);
ivoce1198e02017-09-08 08:13:19 -0700512 if (encoder_stack_)
513 return encoder_stack_->GetANAStats();
514 // If no encoder is set, return default stats.
515 return ANAStats();
516}
517
Jakob Ivarssonbf087452021-11-11 13:43:49 +0100518int AudioCodingModuleImpl::GetTargetBitrate() const {
519 MutexLock lock(&acm_mutex_);
520 if (!encoder_stack_) {
521 return -1;
522 }
523 return encoder_stack_->GetTargetBitrate();
524}
525
kwibergc13ded52016-06-17 06:00:45 -0700526} // namespace
527
Henrik Lundin84f75692023-02-01 12:07:10 +0000528std::unique_ptr<AudioCodingModule> AudioCodingModule::Create() {
529 return std::make_unique<AudioCodingModuleImpl>();
kwiberg36a43882016-08-29 05:33:32 -0700530}
531
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000532} // namespace webrtc