turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/include/audio_coding_module.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <assert.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Jonathan Yu | 36344a0 | 2017-07-30 01:55:34 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <cstdint> |
Jonathan Yu | 36344a0 | 2017-07-30 01:55:34 -0700 | [diff] [blame] | 17 | |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 18 | #include "absl/strings/match.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include "api/array_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/acm2/acm_receiver.h" |
| 21 | #include "modules/audio_coding/acm2/acm_resampler.h" |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 22 | #include "modules/include/module_common_types.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 23 | #include "modules/include/module_common_types_public.h" |
| 24 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 28 | #include "rtc_base/numerics/safe_conversions.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "system_wrappers/include/metrics.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 36 | class AudioCodingModuleImpl final : public AudioCodingModule { |
| 37 | public: |
| 38 | explicit AudioCodingModuleImpl(const AudioCodingModule::Config& config); |
| 39 | ~AudioCodingModuleImpl() override; |
| 40 | |
| 41 | ///////////////////////////////////////// |
| 42 | // Sender |
| 43 | // |
| 44 | |
kwiberg | 24c7c12 | 2016-09-28 11:57:10 -0700 | [diff] [blame] | 45 | void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> |
| 46 | modifier) override; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 47 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 48 | // Register a transport callback which will be |
| 49 | // called to deliver the encoded buffers. |
| 50 | int RegisterTransportCallback(AudioPacketizationCallback* transport) override; |
| 51 | |
| 52 | // Add 10 ms of raw (PCM) audio data to the encoder. |
| 53 | int Add10MsData(const AudioFrame& audio_frame) override; |
| 54 | |
| 55 | ///////////////////////////////////////// |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 56 | // (FEC) Forward Error Correction (codec internal) |
| 57 | // |
| 58 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 59 | // Set target packet loss rate |
| 60 | int SetPacketLossRate(int loss_rate) override; |
| 61 | |
| 62 | ///////////////////////////////////////// |
| 63 | // (VAD) Voice Activity Detection |
| 64 | // and |
| 65 | // (CNG) Comfort Noise Generation |
| 66 | // |
| 67 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 68 | int RegisterVADCallback(ACMVADCallback* vad_callback) override; |
| 69 | |
| 70 | ///////////////////////////////////////// |
| 71 | // Receiver |
| 72 | // |
| 73 | |
| 74 | // Initialize receiver, resets codec database etc. |
| 75 | int InitializeReceiver() override; |
| 76 | |
| 77 | // Get current receive frequency. |
| 78 | int ReceiveFrequency() const override; |
| 79 | |
| 80 | // Get current playout frequency. |
| 81 | int PlayoutFrequency() const override; |
| 82 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 83 | void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override; |
| 84 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 85 | // Get current received codec. |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 86 | absl::optional<std::pair<int, SdpAudioFormat>> ReceiveCodec() const override; |
ossu | e280cde | 2016-10-12 11:04:10 -0700 | [diff] [blame] | 87 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 88 | // Incoming packet from network parsed and ready for decode. |
| 89 | int IncomingPacket(const uint8_t* incoming_payload, |
| 90 | const size_t payload_length, |
Niels Möller | afb5dbb | 2019-02-15 15:21:47 +0100 | [diff] [blame] | 91 | const RTPHeader& rtp_info) override; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 92 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 93 | // Minimum playout delay. |
| 94 | int SetMinimumPlayoutDelay(int time_ms) override; |
| 95 | |
| 96 | // Maximum playout delay. |
| 97 | int SetMaximumPlayoutDelay(int time_ms) override; |
| 98 | |
Ruslan Burakov | 3b50f9f | 2019-02-06 09:45:56 +0100 | [diff] [blame] | 99 | bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override; |
| 100 | |
| 101 | int GetBaseMinimumPlayoutDelayMs() const override; |
| 102 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 103 | absl::optional<uint32_t> PlayoutTimestamp() override; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 104 | |
henrik.lundin | b3f1c5d | 2016-08-22 15:39:53 -0700 | [diff] [blame] | 105 | int FilteredCurrentDelayMs() const override; |
| 106 | |
Henrik Lundin | abbff89 | 2017-11-29 09:14:04 +0100 | [diff] [blame] | 107 | int TargetDelayMs() const override; |
| 108 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 109 | // Get 10 milliseconds of raw audio data to play out, and |
| 110 | // automatic resample to the requested frequency if > 0. |
| 111 | int PlayoutData10Ms(int desired_freq_hz, |
| 112 | AudioFrame* audio_frame, |
| 113 | bool* muted) override; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 114 | |
| 115 | ///////////////////////////////////////// |
| 116 | // Statistics |
| 117 | // |
| 118 | |
| 119 | int GetNetworkStatistics(NetworkStatistics* statistics) override; |
| 120 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 121 | // If current send codec is Opus, informs it about the maximum playback rate |
| 122 | // the receiver will render. |
| 123 | int SetOpusMaxPlaybackRate(int frequency_hz) override; |
| 124 | |
| 125 | int EnableOpusDtx() override; |
| 126 | |
| 127 | int DisableOpusDtx() override; |
| 128 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 129 | int EnableNack(size_t max_nack_list_size) override; |
| 130 | |
| 131 | void DisableNack() override; |
| 132 | |
| 133 | std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const override; |
| 134 | |
| 135 | void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const override; |
| 136 | |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 137 | ANAStats GetANAStats() const override; |
| 138 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 139 | private: |
| 140 | struct InputData { |
| 141 | uint32_t input_timestamp; |
| 142 | const int16_t* audio; |
| 143 | size_t length_per_channel; |
| 144 | size_t audio_channel; |
| 145 | // If a re-mix is required (up or down), this buffer will store a re-mixed |
| 146 | // version of the input. |
| 147 | int16_t buffer[WEBRTC_10MS_PCM_AUDIO]; |
| 148 | }; |
| 149 | |
| 150 | // This member class writes values to the named UMA histogram, but only if |
| 151 | // the value has changed since the last time (and always for the first call). |
| 152 | class ChangeLogger { |
| 153 | public: |
| 154 | explicit ChangeLogger(const std::string& histogram_name) |
| 155 | : histogram_name_(histogram_name) {} |
| 156 | // Logs the new value if it is different from the last logged value, or if |
| 157 | // this is the first call. |
| 158 | void MaybeLog(int value); |
| 159 | |
| 160 | private: |
| 161 | int last_value_ = 0; |
| 162 | int first_time_ = true; |
| 163 | const std::string histogram_name_; |
| 164 | }; |
| 165 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 166 | int Add10MsDataInternal(const AudioFrame& audio_frame, InputData* input_data) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 167 | RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 168 | int Encode(const InputData& input_data) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 169 | RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 170 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 171 | int InitializeReceiverSafe() RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 172 | |
| 173 | bool HaveValidEncoder(const char* caller_name) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 174 | RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 175 | |
| 176 | // Preprocessing of input audio, including resampling and down-mixing if |
| 177 | // required, before pushing audio into encoder's buffer. |
| 178 | // |
| 179 | // in_frame: input audio-frame |
| 180 | // ptr_out: pointer to output audio_frame. If no preprocessing is required |
| 181 | // |ptr_out| will be pointing to |in_frame|, otherwise pointing to |
| 182 | // |preprocess_frame_|. |
| 183 | // |
| 184 | // Return value: |
| 185 | // -1: if encountering an error. |
| 186 | // 0: otherwise. |
| 187 | int PreprocessToAddData(const AudioFrame& in_frame, |
| 188 | const AudioFrame** ptr_out) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 189 | RTC_EXCLUSIVE_LOCKS_REQUIRED(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 190 | |
| 191 | // Change required states after starting to receive the codec corresponding |
| 192 | // to |index|. |
| 193 | int UpdateUponReceivingCodec(int index); |
| 194 | |
| 195 | rtc::CriticalSection acm_crit_sect_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 196 | rtc::Buffer encode_buffer_ RTC_GUARDED_BY(acm_crit_sect_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 197 | uint32_t expected_codec_ts_ RTC_GUARDED_BY(acm_crit_sect_); |
| 198 | uint32_t expected_in_ts_ RTC_GUARDED_BY(acm_crit_sect_); |
| 199 | acm2::ACMResampler resampler_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 200 | acm2::AcmReceiver receiver_; // AcmReceiver has it's own internal lock. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 201 | ChangeLogger bitrate_logger_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 202 | |
Karl Wiberg | 49c33ce | 2018-11-12 14:21:58 +0100 | [diff] [blame] | 203 | // Current encoder stack, provided by a call to RegisterEncoder. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 204 | std::unique_ptr<AudioEncoder> encoder_stack_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 205 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 206 | // This is to keep track of CN instances where we can send DTMFs. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 207 | uint8_t previous_pltype_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 208 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 209 | bool receiver_initialized_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 210 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 211 | AudioFrame preprocess_frame_ RTC_GUARDED_BY(acm_crit_sect_); |
| 212 | bool first_10ms_data_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 213 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 214 | bool first_frame_ RTC_GUARDED_BY(acm_crit_sect_); |
| 215 | uint32_t last_timestamp_ RTC_GUARDED_BY(acm_crit_sect_); |
| 216 | uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 217 | |
| 218 | rtc::CriticalSection callback_crit_sect_; |
| 219 | AudioPacketizationCallback* packetization_callback_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 220 | RTC_GUARDED_BY(callback_crit_sect_); |
| 221 | ACMVADCallback* vad_callback_ RTC_GUARDED_BY(callback_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 222 | |
| 223 | int codec_histogram_bins_log_[static_cast<size_t>( |
| 224 | AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)]; |
| 225 | int number_of_consecutive_empty_packets_; |
| 226 | }; |
| 227 | |
| 228 | // Adds a codec usage sample to the histogram. |
| 229 | void UpdateCodecTypeHistogram(size_t codec_type) { |
| 230 | RTC_HISTOGRAM_ENUMERATION( |
| 231 | "WebRTC.Audio.Encoder.CodecType", static_cast<int>(codec_type), |
| 232 | static_cast<int>( |
| 233 | webrtc::AudioEncoder::CodecType::kMaxLoggedAudioCodecTypes)); |
| 234 | } |
| 235 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 236 | // Stereo-to-mono can be used as in-place. |
| 237 | int DownMix(const AudioFrame& frame, |
| 238 | size_t length_out_buff, |
| 239 | int16_t* out_buff) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 240 | RTC_DCHECK_EQ(frame.num_channels_, 2); |
| 241 | RTC_DCHECK_GE(length_out_buff, frame.samples_per_channel_); |
| 242 | |
| 243 | if (!frame.muted()) { |
| 244 | const int16_t* frame_data = frame.data(); |
| 245 | for (size_t n = 0; n < frame.samples_per_channel_; ++n) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 246 | out_buff[n] = |
| 247 | static_cast<int16_t>((static_cast<int32_t>(frame_data[2 * n]) + |
| 248 | static_cast<int32_t>(frame_data[2 * n + 1])) >> |
| 249 | 1); |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 250 | } |
| 251 | } else { |
Jonathan Yu | 36344a0 | 2017-07-30 01:55:34 -0700 | [diff] [blame] | 252 | std::fill(out_buff, out_buff + frame.samples_per_channel_, 0); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 253 | } |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | // Mono-to-stereo can be used as in-place. |
| 258 | int UpMix(const AudioFrame& frame, size_t length_out_buff, int16_t* out_buff) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 259 | RTC_DCHECK_EQ(frame.num_channels_, 1); |
| 260 | RTC_DCHECK_GE(length_out_buff, 2 * frame.samples_per_channel_); |
| 261 | |
| 262 | if (!frame.muted()) { |
| 263 | const int16_t* frame_data = frame.data(); |
| 264 | for (size_t n = frame.samples_per_channel_; n != 0; --n) { |
| 265 | size_t i = n - 1; |
| 266 | int16_t sample = frame_data[i]; |
| 267 | out_buff[2 * i + 1] = sample; |
| 268 | out_buff[2 * i] = sample; |
| 269 | } |
| 270 | } else { |
Jonathan Yu | 36344a0 | 2017-07-30 01:55:34 -0700 | [diff] [blame] | 271 | std::fill(out_buff, out_buff + frame.samples_per_channel_ * 2, 0); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 276 | void AudioCodingModuleImpl::ChangeLogger::MaybeLog(int value) { |
| 277 | if (value != last_value_ || first_time_) { |
| 278 | first_time_ = false; |
| 279 | last_value_ = value; |
| 280 | RTC_HISTOGRAM_COUNTS_SPARSE_100(histogram_name_, value); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | AudioCodingModuleImpl::AudioCodingModuleImpl( |
| 285 | const AudioCodingModule::Config& config) |
solenberg | c7b4a45 | 2017-09-28 07:37:11 -0700 | [diff] [blame] | 286 | : expected_codec_ts_(0xD87F3F9F), |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 287 | expected_in_ts_(0xD87F3F9F), |
| 288 | receiver_(config), |
| 289 | bitrate_logger_("WebRTC.Audio.TargetBitrateInKbps"), |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 290 | encoder_stack_(nullptr), |
| 291 | previous_pltype_(255), |
| 292 | receiver_initialized_(false), |
| 293 | first_10ms_data_(false), |
| 294 | first_frame_(true), |
| 295 | packetization_callback_(NULL), |
| 296 | vad_callback_(NULL), |
| 297 | codec_histogram_bins_log_(), |
| 298 | number_of_consecutive_empty_packets_(0) { |
| 299 | if (InitializeReceiverSafe() < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 300 | RTC_LOG(LS_ERROR) << "Cannot initialize receiver"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 301 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 302 | RTC_LOG(LS_INFO) << "Created"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | AudioCodingModuleImpl::~AudioCodingModuleImpl() = default; |
| 306 | |
| 307 | int32_t AudioCodingModuleImpl::Encode(const InputData& input_data) { |
| 308 | AudioEncoder::EncodedInfo encoded_info; |
| 309 | uint8_t previous_pltype; |
| 310 | |
| 311 | // Check if there is an encoder before. |
| 312 | if (!HaveValidEncoder("Process")) |
| 313 | return -1; |
| 314 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 315 | if (!first_frame_) { |
deadbeef | fcada90 | 2016-08-24 12:45:13 -0700 | [diff] [blame] | 316 | RTC_DCHECK(IsNewerTimestamp(input_data.input_timestamp, last_timestamp_)) |
ossu | 63fb95a | 2016-07-06 09:34:22 -0700 | [diff] [blame] | 317 | << "Time should not move backwards"; |
| 318 | } |
| 319 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 320 | // Scale the timestamp to the codec's RTP timestamp rate. |
| 321 | uint32_t rtp_timestamp = |
Karl Wiberg | 053c371 | 2019-05-16 15:24:17 +0200 | [diff] [blame] | 322 | first_frame_ |
| 323 | ? input_data.input_timestamp |
| 324 | : last_rtp_timestamp_ + |
| 325 | rtc::dchecked_cast<uint32_t>(rtc::CheckedDivExact( |
| 326 | int64_t{input_data.input_timestamp - last_timestamp_} * |
| 327 | encoder_stack_->RtpTimestampRateHz(), |
| 328 | int64_t{encoder_stack_->SampleRateHz()})); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 329 | last_timestamp_ = input_data.input_timestamp; |
| 330 | last_rtp_timestamp_ = rtp_timestamp; |
| 331 | first_frame_ = false; |
| 332 | |
| 333 | // Clear the buffer before reuse - encoded data will get appended. |
| 334 | encode_buffer_.Clear(); |
| 335 | encoded_info = encoder_stack_->Encode( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 336 | rtp_timestamp, |
| 337 | rtc::ArrayView<const int16_t>( |
| 338 | input_data.audio, |
| 339 | input_data.audio_channel * input_data.length_per_channel), |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 340 | &encode_buffer_); |
| 341 | |
| 342 | bitrate_logger_.MaybeLog(encoder_stack_->GetTargetBitrate() / 1000); |
| 343 | if (encode_buffer_.size() == 0 && !encoded_info.send_even_if_empty) { |
| 344 | // Not enough data. |
| 345 | return 0; |
| 346 | } |
| 347 | previous_pltype = previous_pltype_; // Read it while we have the critsect. |
| 348 | |
| 349 | // Log codec type to histogram once every 500 packets. |
| 350 | if (encoded_info.encoded_bytes == 0) { |
| 351 | ++number_of_consecutive_empty_packets_; |
| 352 | } else { |
| 353 | size_t codec_type = static_cast<size_t>(encoded_info.encoder_type); |
| 354 | codec_histogram_bins_log_[codec_type] += |
| 355 | number_of_consecutive_empty_packets_ + 1; |
| 356 | number_of_consecutive_empty_packets_ = 0; |
| 357 | if (codec_histogram_bins_log_[codec_type] >= 500) { |
| 358 | codec_histogram_bins_log_[codec_type] -= 500; |
| 359 | UpdateCodecTypeHistogram(codec_type); |
| 360 | } |
| 361 | } |
| 362 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 363 | AudioFrameType frame_type; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 364 | if (encode_buffer_.size() == 0 && encoded_info.send_even_if_empty) { |
Niels Möller | c936cb6 | 2019-03-19 14:10:16 +0100 | [diff] [blame] | 365 | frame_type = AudioFrameType::kEmptyFrame; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 366 | encoded_info.payload_type = previous_pltype; |
| 367 | } else { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 368 | RTC_DCHECK_GT(encode_buffer_.size(), 0); |
Niels Möller | c936cb6 | 2019-03-19 14:10:16 +0100 | [diff] [blame] | 369 | frame_type = encoded_info.speech ? AudioFrameType::kAudioFrameSpeech |
| 370 | : AudioFrameType::kAudioFrameCN; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | { |
| 374 | rtc::CritScope lock(&callback_crit_sect_); |
| 375 | if (packetization_callback_) { |
| 376 | packetization_callback_->SendData( |
| 377 | frame_type, encoded_info.payload_type, encoded_info.encoded_timestamp, |
Niels Möller | c35b6e6 | 2019-04-25 16:31:18 +0200 | [diff] [blame] | 378 | encode_buffer_.data(), encode_buffer_.size()); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (vad_callback_) { |
| 382 | // Callback with VAD decision. |
| 383 | vad_callback_->InFrameType(frame_type); |
| 384 | } |
| 385 | } |
| 386 | previous_pltype_ = encoded_info.payload_type; |
| 387 | return static_cast<int32_t>(encode_buffer_.size()); |
| 388 | } |
| 389 | |
| 390 | ///////////////////////////////////////// |
| 391 | // Sender |
| 392 | // |
| 393 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 394 | void AudioCodingModuleImpl::ModifyEncoder( |
kwiberg | 24c7c12 | 2016-09-28 11:57:10 -0700 | [diff] [blame] | 395 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 396 | rtc::CritScope lock(&acm_crit_sect_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 397 | modifier(&encoder_stack_); |
| 398 | } |
| 399 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 400 | // Register a transport callback which will be called to deliver |
| 401 | // the encoded buffers. |
| 402 | int AudioCodingModuleImpl::RegisterTransportCallback( |
| 403 | AudioPacketizationCallback* transport) { |
| 404 | rtc::CritScope lock(&callback_crit_sect_); |
| 405 | packetization_callback_ = transport; |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | // Add 10MS of raw (PCM) audio data to the encoder. |
| 410 | int AudioCodingModuleImpl::Add10MsData(const AudioFrame& audio_frame) { |
| 411 | InputData input_data; |
| 412 | rtc::CritScope lock(&acm_crit_sect_); |
| 413 | int r = Add10MsDataInternal(audio_frame, &input_data); |
| 414 | return r < 0 ? r : Encode(input_data); |
| 415 | } |
| 416 | |
| 417 | int AudioCodingModuleImpl::Add10MsDataInternal(const AudioFrame& audio_frame, |
| 418 | InputData* input_data) { |
| 419 | if (audio_frame.samples_per_channel_ == 0) { |
| 420 | assert(false); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 421 | RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, payload length is zero"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | if (audio_frame.sample_rate_hz_ > 48000) { |
| 426 | assert(false); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 427 | RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, input frequency not valid"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | // If the length and frequency matches. We currently just support raw PCM. |
| 432 | if (static_cast<size_t>(audio_frame.sample_rate_hz_ / 100) != |
| 433 | audio_frame.samples_per_channel_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 434 | RTC_LOG(LS_ERROR) |
Alex Loiko | 300ec8c | 2017-05-30 17:23:28 +0200 | [diff] [blame] | 435 | << "Cannot Add 10 ms audio, input frequency and length doesn't match"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | |
Alex Loiko | 6543881 | 2019-02-22 10:13:44 +0100 | [diff] [blame] | 439 | if (audio_frame.num_channels_ != 1 && audio_frame.num_channels_ != 2 && |
| 440 | audio_frame.num_channels_ != 4 && audio_frame.num_channels_ != 6 && |
| 441 | audio_frame.num_channels_ != 8) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 442 | RTC_LOG(LS_ERROR) << "Cannot Add 10 ms audio, invalid number of channels."; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | // Do we have a codec registered? |
| 447 | if (!HaveValidEncoder("Add10MsData")) { |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | const AudioFrame* ptr_frame; |
| 452 | // Perform a resampling, also down-mix if it is required and can be |
| 453 | // performed before resampling (a down mix prior to resampling will take |
| 454 | // place if both primary and secondary encoders are mono and input is in |
| 455 | // stereo). |
| 456 | if (PreprocessToAddData(audio_frame, &ptr_frame) < 0) { |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | // Check whether we need an up-mix or down-mix? |
| 461 | const size_t current_num_channels = encoder_stack_->NumChannels(); |
| 462 | const bool same_num_channels = |
| 463 | ptr_frame->num_channels_ == current_num_channels; |
| 464 | |
| 465 | if (!same_num_channels) { |
| 466 | if (ptr_frame->num_channels_ == 1) { |
| 467 | if (UpMix(*ptr_frame, WEBRTC_10MS_PCM_AUDIO, input_data->buffer) < 0) |
| 468 | return -1; |
| 469 | } else { |
| 470 | if (DownMix(*ptr_frame, WEBRTC_10MS_PCM_AUDIO, input_data->buffer) < 0) |
| 471 | return -1; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // When adding data to encoders this pointer is pointing to an audio buffer |
| 476 | // with correct number of channels. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 477 | const int16_t* ptr_audio = ptr_frame->data(); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 478 | |
| 479 | // For pushing data to primary, point the |ptr_audio| to correct buffer. |
| 480 | if (!same_num_channels) |
| 481 | ptr_audio = input_data->buffer; |
| 482 | |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 483 | // TODO(yujo): Skip encode of muted frames. |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 484 | input_data->input_timestamp = ptr_frame->timestamp_; |
| 485 | input_data->audio = ptr_audio; |
| 486 | input_data->length_per_channel = ptr_frame->samples_per_channel_; |
| 487 | input_data->audio_channel = current_num_channels; |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | // Perform a resampling and down-mix if required. We down-mix only if |
| 493 | // encoder is mono and input is stereo. In case of dual-streaming, both |
| 494 | // encoders has to be mono for down-mix to take place. |
| 495 | // |*ptr_out| will point to the pre-processed audio-frame. If no pre-processing |
| 496 | // is required, |*ptr_out| points to |in_frame|. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 497 | // TODO(yujo): Make this more efficient for muted frames. |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 498 | int AudioCodingModuleImpl::PreprocessToAddData(const AudioFrame& in_frame, |
| 499 | const AudioFrame** ptr_out) { |
| 500 | const bool resample = |
| 501 | in_frame.sample_rate_hz_ != encoder_stack_->SampleRateHz(); |
| 502 | |
| 503 | // This variable is true if primary codec and secondary codec (if exists) |
| 504 | // are both mono and input is stereo. |
| 505 | // TODO(henrik.lundin): This condition should probably be |
| 506 | // in_frame.num_channels_ > encoder_stack_->NumChannels() |
| 507 | const bool down_mix = |
| 508 | in_frame.num_channels_ == 2 && encoder_stack_->NumChannels() == 1; |
| 509 | |
| 510 | if (!first_10ms_data_) { |
| 511 | expected_in_ts_ = in_frame.timestamp_; |
| 512 | expected_codec_ts_ = in_frame.timestamp_; |
| 513 | first_10ms_data_ = true; |
| 514 | } else if (in_frame.timestamp_ != expected_in_ts_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 515 | RTC_LOG(LS_WARNING) << "Unexpected input timestamp: " << in_frame.timestamp_ |
| 516 | << ", expected: " << expected_in_ts_; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 517 | expected_codec_ts_ += |
| 518 | (in_frame.timestamp_ - expected_in_ts_) * |
| 519 | static_cast<uint32_t>( |
| 520 | static_cast<double>(encoder_stack_->SampleRateHz()) / |
| 521 | static_cast<double>(in_frame.sample_rate_hz_)); |
| 522 | expected_in_ts_ = in_frame.timestamp_; |
| 523 | } |
| 524 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 525 | if (!down_mix && !resample) { |
| 526 | // No pre-processing is required. |
ossu | 63fb95a | 2016-07-06 09:34:22 -0700 | [diff] [blame] | 527 | if (expected_in_ts_ == expected_codec_ts_) { |
| 528 | // If we've never resampled, we can use the input frame as-is |
| 529 | *ptr_out = &in_frame; |
| 530 | } else { |
| 531 | // Otherwise we'll need to alter the timestamp. Since in_frame is const, |
| 532 | // we'll have to make a copy of it. |
| 533 | preprocess_frame_.CopyFrom(in_frame); |
| 534 | preprocess_frame_.timestamp_ = expected_codec_ts_; |
| 535 | *ptr_out = &preprocess_frame_; |
| 536 | } |
| 537 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 538 | expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_); |
| 539 | expected_codec_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | *ptr_out = &preprocess_frame_; |
| 544 | preprocess_frame_.num_channels_ = in_frame.num_channels_; |
| 545 | int16_t audio[WEBRTC_10MS_PCM_AUDIO]; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 546 | const int16_t* src_ptr_audio = in_frame.data(); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 547 | if (down_mix) { |
| 548 | // If a resampling is required the output of a down-mix is written into a |
| 549 | // local buffer, otherwise, it will be written to the output frame. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 550 | int16_t* dest_ptr_audio = |
| 551 | resample ? audio : preprocess_frame_.mutable_data(); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 552 | if (DownMix(in_frame, WEBRTC_10MS_PCM_AUDIO, dest_ptr_audio) < 0) |
| 553 | return -1; |
| 554 | preprocess_frame_.num_channels_ = 1; |
| 555 | // Set the input of the resampler is the down-mixed signal. |
| 556 | src_ptr_audio = audio; |
| 557 | } |
| 558 | |
| 559 | preprocess_frame_.timestamp_ = expected_codec_ts_; |
| 560 | preprocess_frame_.samples_per_channel_ = in_frame.samples_per_channel_; |
| 561 | preprocess_frame_.sample_rate_hz_ = in_frame.sample_rate_hz_; |
| 562 | // If it is required, we have to do a resampling. |
| 563 | if (resample) { |
| 564 | // The result of the resampler is written to output frame. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 565 | int16_t* dest_ptr_audio = preprocess_frame_.mutable_data(); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 566 | |
| 567 | int samples_per_channel = resampler_.Resample10Msec( |
| 568 | src_ptr_audio, in_frame.sample_rate_hz_, encoder_stack_->SampleRateHz(), |
| 569 | preprocess_frame_.num_channels_, AudioFrame::kMaxDataSizeSamples, |
| 570 | dest_ptr_audio); |
| 571 | |
| 572 | if (samples_per_channel < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 573 | RTC_LOG(LS_ERROR) << "Cannot add 10 ms audio, resampling failed"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 574 | return -1; |
| 575 | } |
| 576 | preprocess_frame_.samples_per_channel_ = |
| 577 | static_cast<size_t>(samples_per_channel); |
| 578 | preprocess_frame_.sample_rate_hz_ = encoder_stack_->SampleRateHz(); |
| 579 | } |
| 580 | |
| 581 | expected_codec_ts_ += |
| 582 | static_cast<uint32_t>(preprocess_frame_.samples_per_channel_); |
| 583 | expected_in_ts_ += static_cast<uint32_t>(in_frame.samples_per_channel_); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | ///////////////////////////////////////// |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 589 | // (FEC) Forward Error Correction (codec internal) |
| 590 | // |
| 591 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 592 | int AudioCodingModuleImpl::SetPacketLossRate(int loss_rate) { |
| 593 | rtc::CritScope lock(&acm_crit_sect_); |
| 594 | if (HaveValidEncoder("SetPacketLossRate")) { |
minyue | 4b9a2cb | 2016-11-30 06:49:59 -0800 | [diff] [blame] | 595 | encoder_stack_->OnReceivedUplinkPacketLossFraction(loss_rate / 100.0); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | ///////////////////////////////////////// |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 601 | // Receiver |
| 602 | // |
| 603 | |
| 604 | int AudioCodingModuleImpl::InitializeReceiver() { |
| 605 | rtc::CritScope lock(&acm_crit_sect_); |
| 606 | return InitializeReceiverSafe(); |
| 607 | } |
| 608 | |
| 609 | // Initialize receiver, resets codec database etc. |
| 610 | int AudioCodingModuleImpl::InitializeReceiverSafe() { |
| 611 | // If the receiver is already initialized then we want to destroy any |
| 612 | // existing decoders. After a call to this function, we should have a clean |
| 613 | // start-up. |
kwiberg | 6b19b56 | 2016-09-20 04:02:25 -0700 | [diff] [blame] | 614 | if (receiver_initialized_) |
| 615 | receiver_.RemoveAllCodecs(); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 616 | receiver_.FlushBuffers(); |
| 617 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 618 | receiver_initialized_ = true; |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | // Get current receive frequency. |
| 623 | int AudioCodingModuleImpl::ReceiveFrequency() const { |
| 624 | const auto last_packet_sample_rate = receiver_.last_packet_sample_rate_hz(); |
| 625 | return last_packet_sample_rate ? *last_packet_sample_rate |
| 626 | : receiver_.last_output_sample_rate_hz(); |
| 627 | } |
| 628 | |
| 629 | // Get current playout frequency. |
| 630 | int AudioCodingModuleImpl::PlayoutFrequency() const { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 631 | return receiver_.last_output_sample_rate_hz(); |
| 632 | } |
| 633 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 634 | void AudioCodingModuleImpl::SetReceiveCodecs( |
| 635 | const std::map<int, SdpAudioFormat>& codecs) { |
| 636 | rtc::CritScope lock(&acm_crit_sect_); |
| 637 | receiver_.SetCodecs(codecs); |
| 638 | } |
| 639 | |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 640 | absl::optional<std::pair<int, SdpAudioFormat>> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 641 | AudioCodingModuleImpl::ReceiveCodec() const { |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 642 | rtc::CritScope lock(&acm_crit_sect_); |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 643 | return receiver_.LastDecoder(); |
ossu | e280cde | 2016-10-12 11:04:10 -0700 | [diff] [blame] | 644 | } |
| 645 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 646 | // Incoming packet from network parsed and ready for decode. |
| 647 | int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload, |
| 648 | const size_t payload_length, |
Niels Möller | afb5dbb | 2019-02-15 15:21:47 +0100 | [diff] [blame] | 649 | const RTPHeader& rtp_header) { |
henrik.lundin | b8c55b1 | 2017-05-10 07:38:01 -0700 | [diff] [blame] | 650 | RTC_DCHECK_EQ(payload_length == 0, incoming_payload == nullptr); |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 651 | return receiver_.InsertPacket( |
| 652 | rtp_header, |
| 653 | rtc::ArrayView<const uint8_t>(incoming_payload, payload_length)); |
| 654 | } |
| 655 | |
| 656 | // Minimum playout delay (Used for lip-sync). |
| 657 | int AudioCodingModuleImpl::SetMinimumPlayoutDelay(int time_ms) { |
| 658 | if ((time_ms < 0) || (time_ms > 10000)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 659 | RTC_LOG(LS_ERROR) << "Delay must be in the range of 0-10000 milliseconds."; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | return receiver_.SetMinimumDelay(time_ms); |
| 663 | } |
| 664 | |
| 665 | int AudioCodingModuleImpl::SetMaximumPlayoutDelay(int time_ms) { |
| 666 | if ((time_ms < 0) || (time_ms > 10000)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 667 | RTC_LOG(LS_ERROR) << "Delay must be in the range of 0-10000 milliseconds."; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 668 | return -1; |
| 669 | } |
| 670 | return receiver_.SetMaximumDelay(time_ms); |
| 671 | } |
| 672 | |
Ruslan Burakov | 3b50f9f | 2019-02-06 09:45:56 +0100 | [diff] [blame] | 673 | bool AudioCodingModuleImpl::SetBaseMinimumPlayoutDelayMs(int delay_ms) { |
| 674 | // All necessary validation happens on NetEq level. |
| 675 | return receiver_.SetBaseMinimumDelayMs(delay_ms); |
| 676 | } |
| 677 | |
| 678 | int AudioCodingModuleImpl::GetBaseMinimumPlayoutDelayMs() const { |
| 679 | return receiver_.GetBaseMinimumDelayMs(); |
| 680 | } |
| 681 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 682 | // Get 10 milliseconds of raw audio data to play out. |
| 683 | // Automatic resample to the requested frequency. |
| 684 | int AudioCodingModuleImpl::PlayoutData10Ms(int desired_freq_hz, |
| 685 | AudioFrame* audio_frame, |
| 686 | bool* muted) { |
| 687 | // GetAudio always returns 10 ms, at the requested sample rate. |
| 688 | if (receiver_.GetAudio(desired_freq_hz, audio_frame, muted) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 689 | RTC_LOG(LS_ERROR) << "PlayoutData failed, RecOut Failed"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 690 | return -1; |
| 691 | } |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 695 | ///////////////////////////////////////// |
| 696 | // Statistics |
| 697 | // |
| 698 | |
| 699 | // TODO(turajs) change the return value to void. Also change the corresponding |
| 700 | // NetEq function. |
| 701 | int AudioCodingModuleImpl::GetNetworkStatistics(NetworkStatistics* statistics) { |
| 702 | receiver_.GetNetworkStatistics(statistics); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | int AudioCodingModuleImpl::RegisterVADCallback(ACMVADCallback* vad_callback) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 707 | RTC_LOG(LS_VERBOSE) << "RegisterVADCallback()"; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 708 | rtc::CritScope lock(&callback_crit_sect_); |
| 709 | vad_callback_ = vad_callback; |
| 710 | return 0; |
| 711 | } |
| 712 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 713 | // Informs Opus encoder of the maximum playback rate the receiver will render. |
| 714 | int AudioCodingModuleImpl::SetOpusMaxPlaybackRate(int frequency_hz) { |
| 715 | rtc::CritScope lock(&acm_crit_sect_); |
| 716 | if (!HaveValidEncoder("SetOpusMaxPlaybackRate")) { |
| 717 | return -1; |
| 718 | } |
| 719 | encoder_stack_->SetMaxPlaybackRate(frequency_hz); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | int AudioCodingModuleImpl::EnableOpusDtx() { |
| 724 | rtc::CritScope lock(&acm_crit_sect_); |
| 725 | if (!HaveValidEncoder("EnableOpusDtx")) { |
| 726 | return -1; |
| 727 | } |
| 728 | return encoder_stack_->SetDtx(true) ? 0 : -1; |
| 729 | } |
| 730 | |
| 731 | int AudioCodingModuleImpl::DisableOpusDtx() { |
| 732 | rtc::CritScope lock(&acm_crit_sect_); |
| 733 | if (!HaveValidEncoder("DisableOpusDtx")) { |
| 734 | return -1; |
| 735 | } |
| 736 | return encoder_stack_->SetDtx(false) ? 0 : -1; |
| 737 | } |
| 738 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 739 | absl::optional<uint32_t> AudioCodingModuleImpl::PlayoutTimestamp() { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 740 | return receiver_.GetPlayoutTimestamp(); |
| 741 | } |
| 742 | |
henrik.lundin | b3f1c5d | 2016-08-22 15:39:53 -0700 | [diff] [blame] | 743 | int AudioCodingModuleImpl::FilteredCurrentDelayMs() const { |
| 744 | return receiver_.FilteredCurrentDelayMs(); |
| 745 | } |
| 746 | |
Henrik Lundin | abbff89 | 2017-11-29 09:14:04 +0100 | [diff] [blame] | 747 | int AudioCodingModuleImpl::TargetDelayMs() const { |
| 748 | return receiver_.TargetDelayMs(); |
| 749 | } |
| 750 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 751 | bool AudioCodingModuleImpl::HaveValidEncoder(const char* caller_name) const { |
| 752 | if (!encoder_stack_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 753 | RTC_LOG(LS_ERROR) << caller_name << " failed: No send codec is registered."; |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 754 | return false; |
| 755 | } |
| 756 | return true; |
| 757 | } |
| 758 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 759 | int AudioCodingModuleImpl::EnableNack(size_t max_nack_list_size) { |
| 760 | return receiver_.EnableNack(max_nack_list_size); |
| 761 | } |
| 762 | |
| 763 | void AudioCodingModuleImpl::DisableNack() { |
| 764 | receiver_.DisableNack(); |
| 765 | } |
| 766 | |
| 767 | std::vector<uint16_t> AudioCodingModuleImpl::GetNackList( |
| 768 | int64_t round_trip_time_ms) const { |
| 769 | return receiver_.GetNackList(round_trip_time_ms); |
| 770 | } |
| 771 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 772 | void AudioCodingModuleImpl::GetDecodingCallStatistics( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 773 | AudioDecodingCallStats* call_stats) const { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 774 | receiver_.GetDecodingCallStatistics(call_stats); |
| 775 | } |
| 776 | |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 777 | ANAStats AudioCodingModuleImpl::GetANAStats() const { |
| 778 | rtc::CritScope lock(&acm_crit_sect_); |
| 779 | if (encoder_stack_) |
| 780 | return encoder_stack_->GetANAStats(); |
| 781 | // If no encoder is set, return default stats. |
| 782 | return ANAStats(); |
| 783 | } |
| 784 | |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 785 | } // namespace |
| 786 | |
Karl Wiberg | 5817d3d | 2018-04-06 10:06:42 +0200 | [diff] [blame] | 787 | AudioCodingModule::Config::Config( |
| 788 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) |
| 789 | : neteq_config(), |
| 790 | clock(Clock::GetRealTimeClock()), |
| 791 | decoder_factory(decoder_factory) { |
kwiberg | 36a4388 | 2016-08-29 05:33:32 -0700 | [diff] [blame] | 792 | // Post-decode VAD is disabled by default in NetEq, however, Audio |
| 793 | // Conference Mixer relies on VAD decisions and fails without them. |
| 794 | neteq_config.enable_post_decode_vad = true; |
| 795 | } |
| 796 | |
| 797 | AudioCodingModule::Config::Config(const Config&) = default; |
| 798 | AudioCodingModule::Config::~Config() = default; |
| 799 | |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 800 | AudioCodingModule* AudioCodingModule::Create(const Config& config) { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 801 | return new AudioCodingModuleImpl(config); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 802 | } |
| 803 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 804 | } // namespace webrtc |