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