blob: 417a34637c768dc7b0208910c020d350e75be466 [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
kjellander3e6db232015-11-26 04:44:54 -080011#include "webrtc/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
Henrik Lundin1bb8cf82015-08-25 13:08:04 +020018#include "webrtc/base/checks.h"
pkasting@chromium.org16825b12015-01-12 21:51:21 +000019#include "webrtc/base/format_macros.h"
Tommi92fbbb22015-05-27 22:07:35 +020020#include "webrtc/base/logging.h"
Tommid44c0772016-03-11 17:12:32 -080021#include "webrtc/base/safe_conversions.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000022#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
23#include "webrtc/common_types.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000024#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
kjellander3e6db232015-11-26 04:44:54 -080025#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
26#include "webrtc/modules/audio_coding/acm2/call_statistics.h"
Henrik Kjellander74640892015-10-29 11:31:02 +010027#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/clock.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000030
31namespace webrtc {
32
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000033namespace acm2 {
34
kwiberg7a0f2c52016-09-18 05:35:52 -070035namespace {
36
37// Is the given codec a CNG codec?
38// TODO(kwiberg): Move to RentACodec.
39bool IsCng(int codec_id) {
40 auto i = RentACodec::CodecIdFromIndex(codec_id);
41 return (i && (*i == RentACodec::CodecId::kCNNB ||
42 *i == RentACodec::CodecId::kCNWB ||
43 *i == RentACodec::CodecId::kCNSWB ||
44 *i == RentACodec::CodecId::kCNFB));
45}
46
47} // namespace
48
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000049AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
kwiberg7a0f2c52016-09-18 05:35:52 -070050 : last_audio_decoder_(nullptr),
51 last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
ossue3525782016-05-25 07:37:43 -070052 neteq_(NetEq::Create(config.neteq_config, config.decoder_factory)),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000053 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -080054 resampled_last_output_frame_(true) {
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000055 assert(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +000056 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000057}
58
59AcmReceiver::~AcmReceiver() {
60 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000061}
62
63int AcmReceiver::SetMinimumDelay(int delay_ms) {
64 if (neteq_->SetMinimumDelay(delay_ms))
65 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020066 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000067 return -1;
68}
69
turaj@webrtc.org7959e162013-09-12 18:30:26 +000070int AcmReceiver::SetMaximumDelay(int delay_ms) {
71 if (neteq_->SetMaximumDelay(delay_ms))
72 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020073 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000074 return -1;
75}
76
77int AcmReceiver::LeastRequiredDelayMs() const {
78 return neteq_->LeastRequiredDelayMs();
79}
80
henrik.lundin057fb892015-11-23 08:19:52 -080081rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010082 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080083 return last_packet_sample_rate_hz_;
84}
85
henrik.lundind89814b2015-11-23 06:49:25 -080086int AcmReceiver::last_output_sample_rate_hz() const {
87 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000088}
89
turaj@webrtc.org7959e162013-09-12 18:30:26 +000090int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080091 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000092 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000093 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
94
95 {
Tommi9090e0b2016-01-20 13:39:36 +010096 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000097
kwiberg7a0f2c52016-09-18 05:35:52 -070098 const Decoder* decoder = RtpHeaderToDecoder(*header, incoming_payload[0]);
99 if (!decoder) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000100 LOG_F(LS_ERROR) << "Payload-type "
101 << static_cast<int>(header->payloadType)
102 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000103 return -1;
104 }
kwiberg7a0f2c52016-09-18 05:35:52 -0700105 const int sample_rate_hz = [&decoder] {
106 const auto ci = RentACodec::CodecIdFromIndex(decoder->acm_codec_id);
107 return ci ? RentACodec::CodecInstById(*ci)->plfreq : -1;
108 }();
109 receive_timestamp = NowInTimestamp(sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000110
kwiberg7a0f2c52016-09-18 05:35:52 -0700111 // If this is a CNG while the audio codec is not mono, skip pushing in
112 // packets into NetEq.
113 if (IsCng(decoder->acm_codec_id) && last_audio_decoder_ &&
114 last_audio_decoder_->channels > 1)
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000115 return 0;
kwiberg7a0f2c52016-09-18 05:35:52 -0700116 if (!IsCng(decoder->acm_codec_id) &&
117 decoder->acm_codec_id !=
118 *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
119 last_audio_decoder_ = decoder;
120 last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000121 }
122
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000123 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124
kwibergee2bac22015-11-11 10:34:00 -0800125 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
126 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200127 LOG(LERROR) << "AcmReceiver::InsertPacket "
128 << static_cast<int>(header->payloadType)
129 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000130 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000131 }
132 return 0;
133}
134
henrik.lundin834a6ea2016-05-13 03:45:24 -0700135int AcmReceiver::GetAudio(int desired_freq_hz,
136 AudioFrame* audio_frame,
137 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700138 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000139 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100140 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000141
henrik.lundin834a6ea2016-05-13 03:45:24 -0700142 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200143 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000144 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000145 }
146
henrik.lundind89814b2015-11-23 06:49:25 -0800147 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148
149 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800150 const bool need_resampling =
151 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000152
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000153 if (need_resampling && !resampled_last_output_frame_) {
154 // Prime the resampler with the last frame.
155 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800156 int samples_per_channel_int = resampler_.Resample10Msec(
157 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800158 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
159 temp_output);
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 - "
162 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000163 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000164 }
165 }
166
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000167 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
168 // from NetEq changes. See WebRTC issue 3923.
169 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800170 int samples_per_channel_int = resampler_.Resample10Msec(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800171 audio_frame->data_, current_sample_rate_hz, desired_freq_hz,
172 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
173 audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700174 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200175 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000176 return -1;
177 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800178 audio_frame->samples_per_channel_ =
179 static_cast<size_t>(samples_per_channel_int);
180 audio_frame->sample_rate_hz_ = desired_freq_hz;
181 RTC_DCHECK_EQ(
182 audio_frame->sample_rate_hz_,
183 rtc::checked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000184 resampled_last_output_frame_ = true;
185 } else {
186 resampled_last_output_frame_ = false;
187 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000188 }
189
henrik.lundin6d8e0112016-03-04 10:34:21 -0800190 // Store current audio in |last_audio_buffer_| for next time.
191 memcpy(last_audio_buffer_.get(), audio_frame->data_,
192 sizeof(int16_t) * audio_frame->samples_per_channel_ *
193 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000194
henrik.lundin63489782016-09-20 01:47:12 -0700195 call_stats_.DecodedByNetEq(audio_frame->speech_type_, *muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196 return 0;
197}
198
199int32_t AcmReceiver::AddCodec(int acm_codec_id,
200 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800201 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200202 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800203 AudioDecoder* audio_decoder,
204 const std::string& name) {
kwibergee1879c2015-10-29 06:20:28 -0700205 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
206 if (acm_codec_id == -1)
207 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100208 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700209 RentACodec::CodecIdFromIndex(acm_codec_id);
210 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100211 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700212 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
213 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
214 return *ned;
215 }();
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000216
Tommi9090e0b2016-01-20 13:39:36 +0100217 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000218
219 // The corresponding NetEq decoder ID.
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000220 // If this codec has been registered before.
Jelena Marusica9907842015-03-26 14:01:30 +0100221 auto it = decoders_.find(payload_type);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000222 if (it != decoders_.end()) {
223 const Decoder& decoder = it->second;
kwiberg4e14f092015-08-24 05:27:22 -0700224 if (acm_codec_id != -1 && decoder.acm_codec_id == acm_codec_id &&
225 decoder.channels == channels &&
Karl Wibergd8399e62015-05-25 14:39:56 +0200226 decoder.sample_rate_hz == sample_rate_hz) {
Jelena Marusica9907842015-03-26 14:01:30 +0100227 // Re-registering the same codec. Do nothing and return.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000228 return 0;
229 }
230
kwiberg4e14f092015-08-24 05:27:22 -0700231 // Changing codec. First unregister the old codec, then register the new
232 // one.
Jelena Marusica9907842015-03-26 14:01:30 +0100233 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200234 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000235 return -1;
236 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000237
238 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000239 }
240
241 int ret_val;
242 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800243 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000244 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800245 ret_val = neteq_->RegisterExternalDecoder(
kwiberg342f7402016-06-16 03:18:00 -0700246 audio_decoder, neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000247 }
248 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200249 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
250 << static_cast<int>(payload_type)
251 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000252 return -1;
253 }
254
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000255 Decoder decoder;
256 decoder.acm_codec_id = acm_codec_id;
257 decoder.payload_type = payload_type;
258 decoder.channels = channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200259 decoder.sample_rate_hz = sample_rate_hz;
Jelena Marusica9907842015-03-26 14:01:30 +0100260 decoders_[payload_type] = decoder;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000261 return 0;
262}
263
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000264void AcmReceiver::FlushBuffers() {
265 neteq_->FlushBuffers();
266}
267
kwibergbfb78d12016-09-18 05:33:41 -0700268// If failed in removing one of the codecs, this method continues to remove as
269// many as it can.
270int AcmReceiver::RemoveAllCodecs() {
271 int ret_val = 0;
Tommi9090e0b2016-01-20 13:39:36 +0100272 rtc::CritScope lock(&crit_sect_);
kwibergbfb78d12016-09-18 05:33:41 -0700273 for (auto it = decoders_.begin(); it != decoders_.end(); ) {
274 auto cur = it;
275 ++it; // it will be valid even if we erase cur
276 if (neteq_->RemovePayloadType(cur->second.payload_type) == 0) {
277 decoders_.erase(cur);
278 } else {
279 LOG_F(LS_ERROR) << "Cannot remove payload "
280 << static_cast<int>(cur->second.payload_type);
281 ret_val = -1;
282 }
283 }
284
285 // No codec is registered, invalidate last audio decoder.
kwiberg7a0f2c52016-09-18 05:35:52 -0700286 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800287 last_packet_sample_rate_hz_ = rtc::Optional<int>();
kwibergbfb78d12016-09-18 05:33:41 -0700288 return ret_val;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289}
290
291int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100292 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100293 auto it = decoders_.find(payload_type);
294 if (it == decoders_.end()) { // Such a payload-type is not registered.
turaj@webrtc.orga92baea2013-12-13 00:10:44 +0000295 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000296 }
297 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200298 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000299 return -1;
300 }
kwiberg7a0f2c52016-09-18 05:35:52 -0700301 if (last_audio_decoder_ == &it->second) {
302 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800303 last_packet_sample_rate_hz_ = rtc::Optional<int>();
304 }
Jelena Marusica9907842015-03-26 14:01:30 +0100305 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000306 return 0;
307}
308
henrik.lundin9a410dd2016-04-06 01:39:22 -0700309rtc::Optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
310 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000311}
312
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700313int AcmReceiver::FilteredCurrentDelayMs() const {
314 return neteq_->FilteredCurrentDelayMs();
315}
316
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000317int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100318 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100319 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000320 return -1;
321 }
kwiberg7a0f2c52016-09-18 05:35:52 -0700322 *codec = *RentACodec::CodecInstById(
323 *RentACodec::CodecIdFromIndex(last_audio_decoder_->acm_codec_id));
324 codec->pltype = last_audio_decoder_->payload_type;
325 codec->channels = last_audio_decoder_->channels;
326 codec->plfreq = last_audio_decoder_->sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000327 return 0;
328}
329
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000330void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000331 NetEqNetworkStatistics neteq_stat;
332 // NetEq function always returns zero, so we don't check the return value.
333 neteq_->NetworkStatistics(&neteq_stat);
334
335 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
336 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000337 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000338 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
339 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
340 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000341 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000342 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
343 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000344 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000345 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000346 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200347 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
348 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
349 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
350 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000351}
352
353int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
354 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100355 rtc::CritScope lock(&crit_sect_);
kwibergf62b82e2016-09-18 05:32:10 -0700356 auto it = decoders_.find(payload_type);
357 if (it == decoders_.end()) {
Tommi92fbbb22015-05-27 22:07:35 +0200358 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
359 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000360 return -1;
361 }
kwibergf62b82e2016-09-18 05:32:10 -0700362 const Decoder& decoder = it->second;
363 *codec = *RentACodec::CodecInstById(
364 *RentACodec::CodecIdFromIndex(decoder.acm_codec_id));
365 codec->pltype = decoder.payload_type;
366 codec->channels = decoder.channels;
367 codec->plfreq = decoder.sample_rate_hz;
368 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000369}
370
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000371int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700372 neteq_->EnableNack(max_nack_list_size);
373 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000374}
375
376void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700377 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000378}
379
380std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000381 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700382 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000383}
384
385void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000386 neteq_->SetMinimumDelay(0);
387 // TODO(turajs): Should NetEq Buffer be flushed?
388}
389
kwiberg7a0f2c52016-09-18 05:35:52 -0700390const AcmReceiver::Decoder* AcmReceiver::RtpHeaderToDecoder(
Jelena Marusica9907842015-03-26 14:01:30 +0100391 const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800392 uint8_t payload_type) const {
kwiberg7a0f2c52016-09-18 05:35:52 -0700393 auto it = decoders_.find(rtp_header.payloadType);
394 const auto red_index =
395 RentACodec::CodecIndexFromId(RentACodec::CodecId::kRED);
396 if (red_index && // This ensures that RED is defined in WebRTC.
397 it != decoders_.end() && it->second.acm_codec_id == *red_index) {
398 // This is a RED packet, get the payload of the audio codec.
399 it = decoders_.find(payload_type & 0x7F);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000400 }
kwiberg7a0f2c52016-09-18 05:35:52 -0700401
402 // Check if the payload is registered.
403 return it != decoders_.end() ? &it->second : nullptr;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000404}
405
406uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
407 // Down-cast the time to (32-6)-bit since we only care about
408 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
409 // We masked 6 most significant bits of 32-bit so there is no overflow in
410 // the conversion from milliseconds to timestamp.
411 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000412 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000413 return static_cast<uint32_t>(
414 (decoder_sampling_rate / 1000) * now_in_ms);
415}
416
wu@webrtc.org24301a62013-12-13 19:17:43 +0000417void AcmReceiver::GetDecodingCallStatistics(
418 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100419 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000420 *stats = call_stats_.GetDecodingStatistics();
421}
422
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000423} // namespace acm2
424
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000425} // namespace webrtc