blob: 892abe57069801e4baaa188000f55c4966509606 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/acm2/acm_receiver.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000012
13#include <stdlib.h> // malloc
14
15#include <algorithm> // sort
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/audio_decoder.h"
19#include "common_audio/signal_processing/include/signal_processing_library.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/acm2/acm_resampler.h"
22#include "modules/audio_coding/acm2/call_statistics.h"
23#include "modules/audio_coding/acm2/rent_a_codec.h"
24#include "modules/audio_coding/neteq/include/neteq.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020025#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/format_macros.h"
28#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010029#include "rtc_base/numerics/safe_conversions.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020030#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "system_wrappers/include/clock.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000032
33namespace webrtc {
34
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000035namespace acm2 {
36
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000037AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
kwiberg6f0f6162016-09-20 03:07:46 -070038 : last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
ossue3525782016-05-25 07:37:43 -070039 neteq_(NetEq::Create(config.neteq_config, config.decoder_factory)),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000040 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -080041 resampled_last_output_frame_(true) {
Henrik Lundin02ed2012017-06-08 09:03:55 +020042 RTC_DCHECK(clock_);
Henrik Lundin76c10672018-05-07 13:47:28 +020043 memset(last_audio_buffer_.get(), 0,
44 sizeof(int16_t) * AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000045}
46
Henrik Lundin6af93992017-06-14 14:13:02 +020047AcmReceiver::~AcmReceiver() = default;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000048
49int AcmReceiver::SetMinimumDelay(int delay_ms) {
50 if (neteq_->SetMinimumDelay(delay_ms))
51 return 0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010052 RTC_LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000053 return -1;
54}
55
turaj@webrtc.org7959e162013-09-12 18:30:26 +000056int AcmReceiver::SetMaximumDelay(int delay_ms) {
57 if (neteq_->SetMaximumDelay(delay_ms))
58 return 0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000060 return -1;
61}
62
Danil Chapovalovb6021232018-06-19 13:26:36 +020063absl::optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010064 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080065 return last_packet_sample_rate_hz_;
66}
67
henrik.lundind89814b2015-11-23 06:49:25 -080068int AcmReceiver::last_output_sample_rate_hz() const {
69 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000070}
71
turaj@webrtc.org7959e162013-09-12 18:30:26 +000072int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080073 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000074 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000075 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
76
henrik.lundinb8c55b12017-05-10 07:38:01 -070077 if (incoming_payload.empty()) {
78 neteq_->InsertEmptyPacket(rtp_header.header);
79 return 0;
80 }
81
turaj@webrtc.org7959e162013-09-12 18:30:26 +000082 {
Tommi9090e0b2016-01-20 13:39:36 +010083 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000084
Danil Chapovalovb6021232018-06-19 13:26:36 +020085 const absl::optional<CodecInst> ci =
kwiberg6f0f6162016-09-20 03:07:46 -070086 RtpHeaderToDecoder(*header, incoming_payload[0]);
87 if (!ci) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG_F(LS_ERROR) << "Payload-type "
89 << static_cast<int>(header->payloadType)
90 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000091 return -1;
92 }
kwiberg6f0f6162016-09-20 03:07:46 -070093 receive_timestamp = NowInTimestamp(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000094
kwiberg6f0f6162016-09-20 03:07:46 -070095 if (STR_CASE_CMP(ci->plname, "cn") == 0) {
96 if (last_audio_decoder_ && last_audio_decoder_->channels > 1) {
97 // This is a CNG and the audio codec is not mono, so skip pushing in
98 // packets into NetEq.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000099 return 0;
kwiberg6f0f6162016-09-20 03:07:46 -0700100 }
101 } else {
102 last_audio_decoder_ = ci;
ossue280cde2016-10-12 11:04:10 -0700103 last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
104 RTC_DCHECK(last_audio_format_);
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100105 last_packet_sample_rate_hz_ = ci->plfreq;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000106 }
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000107 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000108
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200109 if (neteq_->InsertPacket(rtp_header.header, incoming_payload,
110 receive_timestamp) < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100111 RTC_LOG(LERROR) << "AcmReceiver::InsertPacket "
112 << static_cast<int>(header->payloadType)
113 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000114 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000115 }
116 return 0;
117}
118
henrik.lundin834a6ea2016-05-13 03:45:24 -0700119int AcmReceiver::GetAudio(int desired_freq_hz,
120 AudioFrame* audio_frame,
121 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700122 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000123 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100124 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000125
henrik.lundin834a6ea2016-05-13 03:45:24 -0700126 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100127 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000128 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000129 }
130
henrik.lundind89814b2015-11-23 06:49:25 -0800131 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132
133 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800134 const bool need_resampling =
135 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000136
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000137 if (need_resampling && !resampled_last_output_frame_) {
138 // Prime the resampler with the last frame.
139 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800140 int samples_per_channel_int = resampler_.Resample10Msec(
141 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800142 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
143 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - "
146 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000147 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148 }
149 }
150
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000151 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
152 // from NetEq changes. See WebRTC issue 3923.
153 if (need_resampling) {
yujo36b1a5f2017-06-12 12:45:32 -0700154 // TODO(yujo): handle this more efficiently for muted frames.
henrik.lundind89814b2015-11-23 06:49:25 -0800155 int samples_per_channel_int = resampler_.Resample10Msec(
yujo36b1a5f2017-06-12 12:45:32 -0700156 audio_frame->data(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800157 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
yujo36b1a5f2017-06-12 12:45:32 -0700158 audio_frame->mutable_data());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LERROR)
161 << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000162 return -1;
163 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800164 audio_frame->samples_per_channel_ =
165 static_cast<size_t>(samples_per_channel_int);
166 audio_frame->sample_rate_hz_ = desired_freq_hz;
167 RTC_DCHECK_EQ(
168 audio_frame->sample_rate_hz_,
kwibergd3edd772017-03-01 18:52:48 -0800169 rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000170 resampled_last_output_frame_ = true;
171 } else {
172 resampled_last_output_frame_ = false;
173 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000174 }
175
henrik.lundin6d8e0112016-03-04 10:34:21 -0800176 // Store current audio in |last_audio_buffer_| for next time.
yujo36b1a5f2017-06-12 12:45:32 -0700177 memcpy(last_audio_buffer_.get(), audio_frame->data(),
henrik.lundin6d8e0112016-03-04 10:34:21 -0800178 sizeof(int16_t) * audio_frame->samples_per_channel_ *
179 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000180
henrik.lundin63489782016-09-20 01:47:12 -0700181 call_stats_.DecodedByNetEq(audio_frame->speech_type_, *muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000182 return 0;
183}
184
kwiberg1c07c702017-03-27 07:15:49 -0700185void AcmReceiver::SetCodecs(const std::map<int, SdpAudioFormat>& codecs) {
186 neteq_->SetCodecs(codecs);
187}
188
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000189int32_t AcmReceiver::AddCodec(int acm_codec_id,
190 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800191 size_t channels,
kwibergc4ccd4d2016-09-21 10:55:15 -0700192 int /*sample_rate_hz*/,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800193 AudioDecoder* audio_decoder,
194 const std::string& name) {
kwibergc4ccd4d2016-09-21 10:55:15 -0700195 // TODO(kwiberg): This function has been ignoring the |sample_rate_hz|
196 // argument for a long time. Arguably, it should simply be removed.
197
kwibergee1879c2015-10-29 06:20:28 -0700198 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
199 if (acm_codec_id == -1)
200 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200201 const absl::optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700202 RentACodec::CodecIdFromIndex(acm_codec_id);
203 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200204 const absl::optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700205 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
206 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
207 return *ned;
208 }();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200209 const absl::optional<SdpAudioFormat> new_format =
kwiberg65cb70d2017-03-03 06:16:28 -0800210 NetEqDecoderToSdpAudioFormat(neteq_decoder);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000211
Tommi9090e0b2016-01-20 13:39:36 +0100212 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000213
ossuf1b08da2016-09-23 02:19:43 -0700214 const auto old_format = neteq_->GetDecoderFormat(payload_type);
kwibergc4ccd4d2016-09-21 10:55:15 -0700215 if (old_format && new_format && *old_format == *new_format) {
216 // Re-registering the same codec. Do nothing and return.
217 return 0;
218 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000219
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200220 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(LERROR) << "Cannot remove payload "
222 << static_cast<int>(payload_type);
kwibergc4ccd4d2016-09-21 10:55:15 -0700223 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000224 }
225
226 int ret_val;
227 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800228 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000229 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200230 ret_val = neteq_->RegisterExternalDecoder(audio_decoder, neteq_decoder,
231 name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000232 }
233 if (ret_val != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100234 RTC_LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
235 << static_cast<int>(payload_type)
236 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000237 return -1;
238 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000239 return 0;
240}
241
kwiberg5adaf732016-10-04 09:33:27 -0700242bool AcmReceiver::AddCodec(int rtp_payload_type,
243 const SdpAudioFormat& audio_format) {
244 const auto old_format = neteq_->GetDecoderFormat(rtp_payload_type);
245 if (old_format && *old_format == audio_format) {
246 // Re-registering the same codec. Do nothing and return.
247 return true;
248 }
249
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200250 if (neteq_->RemovePayloadType(rtp_payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LERROR)
252 << "AcmReceiver::AddCodec: Could not remove existing decoder"
253 " for payload type "
254 << rtp_payload_type;
kwiberg5adaf732016-10-04 09:33:27 -0700255 return false;
256 }
257
258 const bool success =
259 neteq_->RegisterPayloadType(rtp_payload_type, audio_format);
260 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100261 RTC_LOG(LERROR) << "AcmReceiver::AddCodec failed for payload type "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200262 << rtp_payload_type << ", decoder format "
263 << rtc::ToString(audio_format);
kwiberg5adaf732016-10-04 09:33:27 -0700264 }
265 return success;
266}
267
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000268void AcmReceiver::FlushBuffers() {
269 neteq_->FlushBuffers();
270}
271
kwiberg6b19b562016-09-20 04:02:25 -0700272void AcmReceiver::RemoveAllCodecs() {
Tommi9090e0b2016-01-20 13:39:36 +0100273 rtc::CritScope lock(&crit_sect_);
kwiberg6b19b562016-09-20 04:02:25 -0700274 neteq_->RemoveAllPayloadTypes();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200275 last_audio_decoder_ = absl::nullopt;
276 last_audio_format_ = absl::nullopt;
277 last_packet_sample_rate_hz_ = absl::nullopt;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000278}
279
280int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100281 rtc::CritScope lock(&crit_sect_);
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200282 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100283 RTC_LOG(LERROR) << "AcmReceiver::RemoveCodec "
284 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000285 return -1;
286 }
kwiberg6f0f6162016-09-20 03:07:46 -0700287 if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200288 last_audio_decoder_ = absl::nullopt;
289 last_audio_format_ = absl::nullopt;
290 last_packet_sample_rate_hz_ = absl::nullopt;
henrik.lundin057fb892015-11-23 08:19:52 -0800291 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000292 return 0;
293}
294
Danil Chapovalovb6021232018-06-19 13:26:36 +0200295absl::optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
henrik.lundin9a410dd2016-04-06 01:39:22 -0700296 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000297}
298
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700299int AcmReceiver::FilteredCurrentDelayMs() const {
300 return neteq_->FilteredCurrentDelayMs();
301}
302
Henrik Lundinabbff892017-11-29 09:14:04 +0100303int AcmReceiver::TargetDelayMs() const {
304 return neteq_->TargetDelayMs();
305}
306
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000307int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100308 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100309 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000310 return -1;
311 }
kwiberg6f0f6162016-09-20 03:07:46 -0700312 *codec = *last_audio_decoder_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000313 return 0;
314}
315
Danil Chapovalovb6021232018-06-19 13:26:36 +0200316absl::optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
ossue280cde2016-10-12 11:04:10 -0700317 rtc::CritScope lock(&crit_sect_);
318 return last_audio_format_;
319}
320
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000321void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000322 NetEqNetworkStatistics neteq_stat;
323 // NetEq function always returns zero, so we don't check the return value.
324 neteq_->NetworkStatistics(&neteq_stat);
325
326 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
327 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000328 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000330 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000331 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000332 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
333 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000334 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200335 acm_stat->currentSecondaryDiscardedRate = neteq_stat.secondary_discarded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000336 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000337 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200338 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
339 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
340 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
341 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700342
343 NetEqLifetimeStatistics neteq_lifetime_stat = neteq_->GetLifetimeStatistics();
344 acm_stat->totalSamplesReceived = neteq_lifetime_stat.total_samples_received;
345 acm_stat->concealedSamples = neteq_lifetime_stat.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200346 acm_stat->concealmentEvents = neteq_lifetime_stat.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200347 acm_stat->jitterBufferDelayMs = neteq_lifetime_stat.jitter_buffer_delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000348}
349
350int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
351 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100352 rtc::CritScope lock(&crit_sect_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200353 const absl::optional<CodecInst> ci = neteq_->GetDecoder(payload_type);
kwibergd1201922016-09-20 15:18:21 -0700354 if (ci) {
355 *codec = *ci;
356 return 0;
357 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 RTC_LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
359 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000360 return -1;
361 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000362}
363
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000364int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700365 neteq_->EnableNack(max_nack_list_size);
366 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000367}
368
369void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700370 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000371}
372
373std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000374 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700375 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000376}
377
378void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000379 neteq_->SetMinimumDelay(0);
380 // TODO(turajs): Should NetEq Buffer be flushed?
381}
382
Danil Chapovalovb6021232018-06-19 13:26:36 +0200383const absl::optional<CodecInst> AcmReceiver::RtpHeaderToDecoder(
Jelena Marusica9907842015-03-26 14:01:30 +0100384 const RTPHeader& rtp_header,
kwiberg6f0f6162016-09-20 03:07:46 -0700385 uint8_t first_payload_byte) const {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200386 const absl::optional<CodecInst> ci =
kwiberg6f0f6162016-09-20 03:07:46 -0700387 neteq_->GetDecoder(rtp_header.payloadType);
388 if (ci && STR_CASE_CMP(ci->plname, "red") == 0) {
389 // This is a RED packet. Get the payload of the audio codec.
390 return neteq_->GetDecoder(first_payload_byte & 0x7f);
391 } else {
392 return ci;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000393 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000394}
395
396uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
397 // Down-cast the time to (32-6)-bit since we only care about
398 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
399 // We masked 6 most significant bits of 32-bit so there is no overflow in
400 // the conversion from milliseconds to timestamp.
Yves Gerey665174f2018-06-19 15:03:05 +0200401 const uint32_t now_in_ms =
402 static_cast<uint32_t>(clock_->TimeInMilliseconds() & 0x03ffffff);
403 return static_cast<uint32_t>((decoder_sampling_rate / 1000) * now_in_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000404}
405
wu@webrtc.org24301a62013-12-13 19:17:43 +0000406void AcmReceiver::GetDecodingCallStatistics(
407 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100408 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000409 *stats = call_stats_.GetDecodingStatistics();
410}
411
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000412} // namespace acm2
413
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000414} // namespace webrtc