blob: 0a88e70ec93b1b0aefdac136fb131da60cb16868 [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
63int AcmReceiver::LeastRequiredDelayMs() const {
64 return neteq_->LeastRequiredDelayMs();
65}
66
Danil Chapovalovb6021232018-06-19 13:26:36 +020067absl::optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010068 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080069 return last_packet_sample_rate_hz_;
70}
71
henrik.lundind89814b2015-11-23 06:49:25 -080072int AcmReceiver::last_output_sample_rate_hz() const {
73 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000074}
75
turaj@webrtc.org7959e162013-09-12 18:30:26 +000076int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080077 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000078 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000079 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
80
henrik.lundinb8c55b12017-05-10 07:38:01 -070081 if (incoming_payload.empty()) {
82 neteq_->InsertEmptyPacket(rtp_header.header);
83 return 0;
84 }
85
turaj@webrtc.org7959e162013-09-12 18:30:26 +000086 {
Tommi9090e0b2016-01-20 13:39:36 +010087 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000088
Danil Chapovalovb6021232018-06-19 13:26:36 +020089 const absl::optional<CodecInst> ci =
kwiberg6f0f6162016-09-20 03:07:46 -070090 RtpHeaderToDecoder(*header, incoming_payload[0]);
91 if (!ci) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG_F(LS_ERROR) << "Payload-type "
93 << static_cast<int>(header->payloadType)
94 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000095 return -1;
96 }
kwiberg6f0f6162016-09-20 03:07:46 -070097 receive_timestamp = NowInTimestamp(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000098
kwiberg6f0f6162016-09-20 03:07:46 -070099 if (STR_CASE_CMP(ci->plname, "cn") == 0) {
100 if (last_audio_decoder_ && last_audio_decoder_->channels > 1) {
101 // This is a CNG and the audio codec is not mono, so skip pushing in
102 // packets into NetEq.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000103 return 0;
kwiberg6f0f6162016-09-20 03:07:46 -0700104 }
105 } else {
106 last_audio_decoder_ = ci;
ossue280cde2016-10-12 11:04:10 -0700107 last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
108 RTC_DCHECK(last_audio_format_);
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100109 last_packet_sample_rate_hz_ = ci->plfreq;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000110 }
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000111 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000112
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200113 if (neteq_->InsertPacket(rtp_header.header, incoming_payload,
114 receive_timestamp) < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LERROR) << "AcmReceiver::InsertPacket "
116 << static_cast<int>(header->payloadType)
117 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000118 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000119 }
120 return 0;
121}
122
henrik.lundin834a6ea2016-05-13 03:45:24 -0700123int AcmReceiver::GetAudio(int desired_freq_hz,
124 AudioFrame* audio_frame,
125 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700126 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000127 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100128 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000129
henrik.lundin834a6ea2016-05-13 03:45:24 -0700130 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100131 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000132 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133 }
134
henrik.lundind89814b2015-11-23 06:49:25 -0800135 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000136
137 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800138 const bool need_resampling =
139 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000140
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000141 if (need_resampling && !resampled_last_output_frame_) {
142 // Prime the resampler with the last frame.
143 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800144 int samples_per_channel_int = resampler_.Resample10Msec(
145 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800146 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
147 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700148 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - "
150 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000151 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000152 }
153 }
154
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000155 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
156 // from NetEq changes. See WebRTC issue 3923.
157 if (need_resampling) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 // TODO(yujo): handle this more efficiently for muted frames.
henrik.lundind89814b2015-11-23 06:49:25 -0800159 int samples_per_channel_int = resampler_.Resample10Msec(
yujo36b1a5f2017-06-12 12:45:32 -0700160 audio_frame->data(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800161 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
yujo36b1a5f2017-06-12 12:45:32 -0700162 audio_frame->mutable_data());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700163 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LERROR)
165 << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000166 return -1;
167 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800168 audio_frame->samples_per_channel_ =
169 static_cast<size_t>(samples_per_channel_int);
170 audio_frame->sample_rate_hz_ = desired_freq_hz;
171 RTC_DCHECK_EQ(
172 audio_frame->sample_rate_hz_,
kwibergd3edd772017-03-01 18:52:48 -0800173 rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000174 resampled_last_output_frame_ = true;
175 } else {
176 resampled_last_output_frame_ = false;
177 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000178 }
179
henrik.lundin6d8e0112016-03-04 10:34:21 -0800180 // Store current audio in |last_audio_buffer_| for next time.
yujo36b1a5f2017-06-12 12:45:32 -0700181 memcpy(last_audio_buffer_.get(), audio_frame->data(),
henrik.lundin6d8e0112016-03-04 10:34:21 -0800182 sizeof(int16_t) * audio_frame->samples_per_channel_ *
183 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000184
henrik.lundin63489782016-09-20 01:47:12 -0700185 call_stats_.DecodedByNetEq(audio_frame->speech_type_, *muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000186 return 0;
187}
188
kwiberg1c07c702017-03-27 07:15:49 -0700189void AcmReceiver::SetCodecs(const std::map<int, SdpAudioFormat>& codecs) {
190 neteq_->SetCodecs(codecs);
191}
192
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000193int32_t AcmReceiver::AddCodec(int acm_codec_id,
194 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800195 size_t channels,
kwibergc4ccd4d2016-09-21 10:55:15 -0700196 int /*sample_rate_hz*/,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800197 AudioDecoder* audio_decoder,
198 const std::string& name) {
kwibergc4ccd4d2016-09-21 10:55:15 -0700199 // TODO(kwiberg): This function has been ignoring the |sample_rate_hz|
200 // argument for a long time. Arguably, it should simply be removed.
201
kwibergee1879c2015-10-29 06:20:28 -0700202 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
203 if (acm_codec_id == -1)
204 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200205 const absl::optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700206 RentACodec::CodecIdFromIndex(acm_codec_id);
207 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200208 const absl::optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700209 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
210 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
211 return *ned;
212 }();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200213 const absl::optional<SdpAudioFormat> new_format =
kwiberg65cb70d2017-03-03 06:16:28 -0800214 NetEqDecoderToSdpAudioFormat(neteq_decoder);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000215
Tommi9090e0b2016-01-20 13:39:36 +0100216 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000217
ossuf1b08da2016-09-23 02:19:43 -0700218 const auto old_format = neteq_->GetDecoderFormat(payload_type);
kwibergc4ccd4d2016-09-21 10:55:15 -0700219 if (old_format && new_format && *old_format == *new_format) {
220 // Re-registering the same codec. Do nothing and return.
221 return 0;
222 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000223
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200224 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LERROR) << "Cannot remove payload "
226 << static_cast<int>(payload_type);
kwibergc4ccd4d2016-09-21 10:55:15 -0700227 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000228 }
229
230 int ret_val;
231 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800232 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000233 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200234 ret_val = neteq_->RegisterExternalDecoder(audio_decoder, neteq_decoder,
235 name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000236 }
237 if (ret_val != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
239 << static_cast<int>(payload_type)
240 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000241 return -1;
242 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000243 return 0;
244}
245
kwiberg5adaf732016-10-04 09:33:27 -0700246bool AcmReceiver::AddCodec(int rtp_payload_type,
247 const SdpAudioFormat& audio_format) {
248 const auto old_format = neteq_->GetDecoderFormat(rtp_payload_type);
249 if (old_format && *old_format == audio_format) {
250 // Re-registering the same codec. Do nothing and return.
251 return true;
252 }
253
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200254 if (neteq_->RemovePayloadType(rtp_payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LERROR)
256 << "AcmReceiver::AddCodec: Could not remove existing decoder"
257 " for payload type "
258 << rtp_payload_type;
kwiberg5adaf732016-10-04 09:33:27 -0700259 return false;
260 }
261
262 const bool success =
263 neteq_->RegisterPayloadType(rtp_payload_type, audio_format);
264 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100265 RTC_LOG(LERROR) << "AcmReceiver::AddCodec failed for payload type "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200266 << rtp_payload_type << ", decoder format "
267 << rtc::ToString(audio_format);
kwiberg5adaf732016-10-04 09:33:27 -0700268 }
269 return success;
270}
271
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000272void AcmReceiver::FlushBuffers() {
273 neteq_->FlushBuffers();
274}
275
kwiberg6b19b562016-09-20 04:02:25 -0700276void AcmReceiver::RemoveAllCodecs() {
Tommi9090e0b2016-01-20 13:39:36 +0100277 rtc::CritScope lock(&crit_sect_);
kwiberg6b19b562016-09-20 04:02:25 -0700278 neteq_->RemoveAllPayloadTypes();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200279 last_audio_decoder_ = absl::nullopt;
280 last_audio_format_ = absl::nullopt;
281 last_packet_sample_rate_hz_ = absl::nullopt;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000282}
283
284int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100285 rtc::CritScope lock(&crit_sect_);
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200286 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100287 RTC_LOG(LERROR) << "AcmReceiver::RemoveCodec "
288 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289 return -1;
290 }
kwiberg6f0f6162016-09-20 03:07:46 -0700291 if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200292 last_audio_decoder_ = absl::nullopt;
293 last_audio_format_ = absl::nullopt;
294 last_packet_sample_rate_hz_ = absl::nullopt;
henrik.lundin057fb892015-11-23 08:19:52 -0800295 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000296 return 0;
297}
298
Danil Chapovalovb6021232018-06-19 13:26:36 +0200299absl::optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
henrik.lundin9a410dd2016-04-06 01:39:22 -0700300 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000301}
302
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700303int AcmReceiver::FilteredCurrentDelayMs() const {
304 return neteq_->FilteredCurrentDelayMs();
305}
306
Henrik Lundinabbff892017-11-29 09:14:04 +0100307int AcmReceiver::TargetDelayMs() const {
308 return neteq_->TargetDelayMs();
309}
310
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000311int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100312 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100313 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000314 return -1;
315 }
kwiberg6f0f6162016-09-20 03:07:46 -0700316 *codec = *last_audio_decoder_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000317 return 0;
318}
319
Danil Chapovalovb6021232018-06-19 13:26:36 +0200320absl::optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
ossue280cde2016-10-12 11:04:10 -0700321 rtc::CritScope lock(&crit_sect_);
322 return last_audio_format_;
323}
324
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000325void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000326 NetEqNetworkStatistics neteq_stat;
327 // NetEq function always returns zero, so we don't check the return value.
328 neteq_->NetworkStatistics(&neteq_stat);
329
330 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
331 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000332 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000333 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000334 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000335 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000336 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
337 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000338 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200339 acm_stat->currentSecondaryDiscardedRate = neteq_stat.secondary_discarded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000340 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000341 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200342 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
343 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
344 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
345 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700346
347 NetEqLifetimeStatistics neteq_lifetime_stat = neteq_->GetLifetimeStatistics();
348 acm_stat->totalSamplesReceived = neteq_lifetime_stat.total_samples_received;
349 acm_stat->concealedSamples = neteq_lifetime_stat.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200350 acm_stat->concealmentEvents = neteq_lifetime_stat.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200351 acm_stat->jitterBufferDelayMs = neteq_lifetime_stat.jitter_buffer_delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000352}
353
354int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
355 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100356 rtc::CritScope lock(&crit_sect_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200357 const absl::optional<CodecInst> ci = neteq_->GetDecoder(payload_type);
kwibergd1201922016-09-20 15:18:21 -0700358 if (ci) {
359 *codec = *ci;
360 return 0;
361 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100362 RTC_LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
363 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000364 return -1;
365 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000366}
367
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000368int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700369 neteq_->EnableNack(max_nack_list_size);
370 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000371}
372
373void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700374 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000375}
376
377std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000378 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700379 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000380}
381
382void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000383 neteq_->SetMinimumDelay(0);
384 // TODO(turajs): Should NetEq Buffer be flushed?
385}
386
Danil Chapovalovb6021232018-06-19 13:26:36 +0200387const absl::optional<CodecInst> AcmReceiver::RtpHeaderToDecoder(
Jelena Marusica9907842015-03-26 14:01:30 +0100388 const RTPHeader& rtp_header,
kwiberg6f0f6162016-09-20 03:07:46 -0700389 uint8_t first_payload_byte) const {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200390 const absl::optional<CodecInst> ci =
kwiberg6f0f6162016-09-20 03:07:46 -0700391 neteq_->GetDecoder(rtp_header.payloadType);
392 if (ci && STR_CASE_CMP(ci->plname, "red") == 0) {
393 // This is a RED packet. Get the payload of the audio codec.
394 return neteq_->GetDecoder(first_payload_byte & 0x7f);
395 } else {
396 return ci;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000397 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000398}
399
400uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
401 // Down-cast the time to (32-6)-bit since we only care about
402 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
403 // We masked 6 most significant bits of 32-bit so there is no overflow in
404 // the conversion from milliseconds to timestamp.
Yves Gerey665174f2018-06-19 15:03:05 +0200405 const uint32_t now_in_ms =
406 static_cast<uint32_t>(clock_->TimeInMilliseconds() & 0x03ffffff);
407 return static_cast<uint32_t>((decoder_sampling_rate / 1000) * now_in_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000408}
409
wu@webrtc.org24301a62013-12-13 19:17:43 +0000410void AcmReceiver::GetDecodingCallStatistics(
411 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100412 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000413 *stats = call_stats_.GetDecodingStatistics();
414}
415
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000416} // namespace acm2
417
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000418} // namespace webrtc