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