blob: f8393ed7e0a792e6b62eaceb80e7b6592501d3f8 [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"
20#include "common_types.h"
21#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"
25#include "rtc_base/checks.h"
26#include "rtc_base/format_macros.h"
27#include "rtc_base/logging.h"
28#include "rtc_base/safe_conversions.h"
29#include "system_wrappers/include/clock.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000030
31namespace webrtc {
32
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000033namespace acm2 {
34
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000035AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
kwiberg6f0f6162016-09-20 03:07:46 -070036 : last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
ossue3525782016-05-25 07:37:43 -070037 neteq_(NetEq::Create(config.neteq_config, config.decoder_factory)),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000038 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -080039 resampled_last_output_frame_(true) {
Henrik Lundin02ed2012017-06-08 09:03:55 +020040 RTC_DCHECK(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +000041 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000042}
43
Henrik Lundin6af93992017-06-14 14:13:02 +020044AcmReceiver::~AcmReceiver() = default;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000045
46int AcmReceiver::SetMinimumDelay(int delay_ms) {
47 if (neteq_->SetMinimumDelay(delay_ms))
48 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020049 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000050 return -1;
51}
52
turaj@webrtc.org7959e162013-09-12 18:30:26 +000053int AcmReceiver::SetMaximumDelay(int delay_ms) {
54 if (neteq_->SetMaximumDelay(delay_ms))
55 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020056 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000057 return -1;
58}
59
60int AcmReceiver::LeastRequiredDelayMs() const {
61 return neteq_->LeastRequiredDelayMs();
62}
63
henrik.lundin057fb892015-11-23 08:19:52 -080064rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010065 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080066 return last_packet_sample_rate_hz_;
67}
68
henrik.lundind89814b2015-11-23 06:49:25 -080069int AcmReceiver::last_output_sample_rate_hz() const {
70 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000071}
72
turaj@webrtc.org7959e162013-09-12 18:30:26 +000073int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080074 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000075 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000076 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
77
henrik.lundinb8c55b12017-05-10 07:38:01 -070078 if (incoming_payload.empty()) {
79 neteq_->InsertEmptyPacket(rtp_header.header);
80 return 0;
81 }
82
turaj@webrtc.org7959e162013-09-12 18:30:26 +000083 {
Tommi9090e0b2016-01-20 13:39:36 +010084 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000085
kwiberg6f0f6162016-09-20 03:07:46 -070086 const rtc::Optional<CodecInst> ci =
87 RtpHeaderToDecoder(*header, incoming_payload[0]);
88 if (!ci) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +000089 LOG_F(LS_ERROR) << "Payload-type "
90 << static_cast<int>(header->payloadType)
91 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000092 return -1;
93 }
kwiberg6f0f6162016-09-20 03:07:46 -070094 receive_timestamp = NowInTimestamp(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000095
kwiberg6f0f6162016-09-20 03:07:46 -070096 if (STR_CASE_CMP(ci->plname, "cn") == 0) {
97 if (last_audio_decoder_ && last_audio_decoder_->channels > 1) {
98 // This is a CNG and the audio codec is not mono, so skip pushing in
99 // packets into NetEq.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000100 return 0;
kwiberg6f0f6162016-09-20 03:07:46 -0700101 }
102 } else {
103 last_audio_decoder_ = ci;
ossue280cde2016-10-12 11:04:10 -0700104 last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
105 RTC_DCHECK(last_audio_format_);
kwiberg6f0f6162016-09-20 03:07:46 -0700106 last_packet_sample_rate_hz_ = rtc::Optional<int>(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000107 }
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000108 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000109
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200110 if (neteq_->InsertPacket(rtp_header.header, incoming_payload,
111 receive_timestamp) < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200112 LOG(LERROR) << "AcmReceiver::InsertPacket "
113 << static_cast<int>(header->payloadType)
114 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000115 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000116 }
117 return 0;
118}
119
henrik.lundin834a6ea2016-05-13 03:45:24 -0700120int AcmReceiver::GetAudio(int desired_freq_hz,
121 AudioFrame* audio_frame,
122 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700123 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000124 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100125 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000126
henrik.lundin834a6ea2016-05-13 03:45:24 -0700127 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200128 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000129 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000130 }
131
henrik.lundind89814b2015-11-23 06:49:25 -0800132 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133
134 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800135 const bool need_resampling =
136 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000137
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000138 if (need_resampling && !resampled_last_output_frame_) {
139 // Prime the resampler with the last frame.
140 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800141 int samples_per_channel_int = resampler_.Resample10Msec(
142 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800143 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
144 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200146 LOG(LERROR) << "AcmReceiver::GetAudio - "
147 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000148 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000149 }
150 }
151
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000152 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
153 // from NetEq changes. See WebRTC issue 3923.
154 if (need_resampling) {
yujo36b1a5f2017-06-12 12:45:32 -0700155 // TODO(yujo): handle this more efficiently for muted frames.
henrik.lundind89814b2015-11-23 06:49:25 -0800156 int samples_per_channel_int = resampler_.Resample10Msec(
yujo36b1a5f2017-06-12 12:45:32 -0700157 audio_frame->data(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800158 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
yujo36b1a5f2017-06-12 12:45:32 -0700159 audio_frame->mutable_data());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200161 LOG(LERROR) << "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.
Karl Wibergbe579832015-11-10 22:34:18 +0100201 const rtc::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;
Karl Wibergbe579832015-11-10 22:34:18 +0100204 const rtc::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 }();
kwibergc4ccd4d2016-09-21 10:55:15 -0700209 const rtc::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) {
kwibergc4ccd4d2016-09-21 10:55:15 -0700221 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
222 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000223 }
224
225 int ret_val;
226 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800227 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000228 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800229 ret_val = neteq_->RegisterExternalDecoder(
kwiberg342f7402016-06-16 03:18:00 -0700230 audio_decoder, neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000231 }
232 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200233 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
234 << static_cast<int>(payload_type)
235 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000236 return -1;
237 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000238 return 0;
239}
240
kwiberg5adaf732016-10-04 09:33:27 -0700241bool AcmReceiver::AddCodec(int rtp_payload_type,
242 const SdpAudioFormat& audio_format) {
243 const auto old_format = neteq_->GetDecoderFormat(rtp_payload_type);
244 if (old_format && *old_format == audio_format) {
245 // Re-registering the same codec. Do nothing and return.
246 return true;
247 }
248
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200249 if (neteq_->RemovePayloadType(rtp_payload_type) != NetEq::kOK) {
kwiberg5adaf732016-10-04 09:33:27 -0700250 LOG(LERROR) << "AcmReceiver::AddCodec: Could not remove existing decoder"
251 " for payload type "
252 << rtp_payload_type;
253 return false;
254 }
255
256 const bool success =
257 neteq_->RegisterPayloadType(rtp_payload_type, audio_format);
258 if (!success) {
259 LOG(LERROR) << "AcmReceiver::AddCodec failed for payload type "
260 << rtp_payload_type << ", decoder format " << audio_format;
261 }
262 return success;
263}
264
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000265void AcmReceiver::FlushBuffers() {
266 neteq_->FlushBuffers();
267}
268
kwiberg6b19b562016-09-20 04:02:25 -0700269void AcmReceiver::RemoveAllCodecs() {
Tommi9090e0b2016-01-20 13:39:36 +0100270 rtc::CritScope lock(&crit_sect_);
kwiberg6b19b562016-09-20 04:02:25 -0700271 neteq_->RemoveAllPayloadTypes();
kwiberg6f0f6162016-09-20 03:07:46 -0700272 last_audio_decoder_ = rtc::Optional<CodecInst>();
ossue280cde2016-10-12 11:04:10 -0700273 last_audio_format_ = rtc::Optional<SdpAudioFormat>();
henrik.lundin057fb892015-11-23 08:19:52 -0800274 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000275}
276
277int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100278 rtc::CritScope lock(&crit_sect_);
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200279 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
280 LOG(LERROR) << "AcmReceiver::RemoveCodec "
281 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000282 return -1;
283 }
kwiberg6f0f6162016-09-20 03:07:46 -0700284 if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
285 last_audio_decoder_ = rtc::Optional<CodecInst>();
ossue280cde2016-10-12 11:04:10 -0700286 last_audio_format_ = rtc::Optional<SdpAudioFormat>();
henrik.lundin057fb892015-11-23 08:19:52 -0800287 last_packet_sample_rate_hz_ = rtc::Optional<int>();
288 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289 return 0;
290}
291
henrik.lundin9a410dd2016-04-06 01:39:22 -0700292rtc::Optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
293 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000294}
295
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700296int AcmReceiver::FilteredCurrentDelayMs() const {
297 return neteq_->FilteredCurrentDelayMs();
298}
299
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000300int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100301 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100302 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000303 return -1;
304 }
kwiberg6f0f6162016-09-20 03:07:46 -0700305 *codec = *last_audio_decoder_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000306 return 0;
307}
308
ossue280cde2016-10-12 11:04:10 -0700309rtc::Optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
310 rtc::CritScope lock(&crit_sect_);
311 return last_audio_format_;
312}
313
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000314void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000315 NetEqNetworkStatistics neteq_stat;
316 // NetEq function always returns zero, so we don't check the return value.
317 neteq_->NetworkStatistics(&neteq_stat);
318
319 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
320 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000321 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000322 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000323 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000324 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000325 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
326 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000327 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200328 acm_stat->currentSecondaryDiscardedRate = neteq_stat.secondary_discarded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000330 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200331 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
332 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
333 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
334 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700335
336 NetEqLifetimeStatistics neteq_lifetime_stat = neteq_->GetLifetimeStatistics();
337 acm_stat->totalSamplesReceived = neteq_lifetime_stat.total_samples_received;
338 acm_stat->concealedSamples = neteq_lifetime_stat.concealed_samples;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000339}
340
341int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
342 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100343 rtc::CritScope lock(&crit_sect_);
kwibergd1201922016-09-20 15:18:21 -0700344 const rtc::Optional<CodecInst> ci = neteq_->GetDecoder(payload_type);
345 if (ci) {
346 *codec = *ci;
347 return 0;
348 } else {
Tommi92fbbb22015-05-27 22:07:35 +0200349 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
350 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000351 return -1;
352 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000353}
354
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000355int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700356 neteq_->EnableNack(max_nack_list_size);
357 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000358}
359
360void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700361 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000362}
363
364std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000365 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700366 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000367}
368
369void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000370 neteq_->SetMinimumDelay(0);
371 // TODO(turajs): Should NetEq Buffer be flushed?
372}
373
kwiberg6f0f6162016-09-20 03:07:46 -0700374const rtc::Optional<CodecInst> AcmReceiver::RtpHeaderToDecoder(
Jelena Marusica9907842015-03-26 14:01:30 +0100375 const RTPHeader& rtp_header,
kwiberg6f0f6162016-09-20 03:07:46 -0700376 uint8_t first_payload_byte) const {
377 const rtc::Optional<CodecInst> ci =
378 neteq_->GetDecoder(rtp_header.payloadType);
379 if (ci && STR_CASE_CMP(ci->plname, "red") == 0) {
380 // This is a RED packet. Get the payload of the audio codec.
381 return neteq_->GetDecoder(first_payload_byte & 0x7f);
382 } else {
383 return ci;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000384 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000385}
386
387uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
388 // Down-cast the time to (32-6)-bit since we only care about
389 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
390 // We masked 6 most significant bits of 32-bit so there is no overflow in
391 // the conversion from milliseconds to timestamp.
392 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000393 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000394 return static_cast<uint32_t>(
395 (decoder_sampling_rate / 1000) * now_in_ms);
396}
397
wu@webrtc.org24301a62013-12-13 19:17:43 +0000398void AcmReceiver::GetDecodingCallStatistics(
399 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100400 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000401 *stats = call_stats_.GetDecodingStatistics();
402}
403
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000404} // namespace acm2
405
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000406} // namespace webrtc