blob: 1990768bc7d6278375bc6840be08803236fba21c [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"
henrik.lundin6d8e0112016-03-04 10:34:21 -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/tick_util.h"
30#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000031
32namespace webrtc {
33
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000034namespace acm2 {
35
turaj@webrtc.org7959e162013-09-12 18:30:26 +000036namespace {
37
turaj@webrtc.org7959e162013-09-12 18:30:26 +000038// Is the given codec a CNG codec?
kwibergfce4a942015-10-27 11:40:24 -070039// TODO(kwiberg): Move to RentACodec.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000040bool IsCng(int codec_id) {
kwibergfce4a942015-10-27 11:40:24 -070041 auto i = RentACodec::CodecIdFromIndex(codec_id);
42 return (i && (*i == RentACodec::CodecId::kCNNB ||
43 *i == RentACodec::CodecId::kCNWB ||
44 *i == RentACodec::CodecId::kCNSWB ||
45 *i == RentACodec::CodecId::kCNFB));
turaj@webrtc.org7959e162013-09-12 18:30:26 +000046}
47
48} // namespace
49
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000050AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
henrik.lundin0023fdf2016-03-03 23:05:39 -080051 : last_audio_decoder_(nullptr),
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +000052 last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +000053 neteq_(NetEq::Create(config.neteq_config)),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000054 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -080055 resampled_last_output_frame_(true) {
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000056 assert(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +000057 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000058}
59
60AcmReceiver::~AcmReceiver() {
61 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000062}
63
64int AcmReceiver::SetMinimumDelay(int delay_ms) {
65 if (neteq_->SetMinimumDelay(delay_ms))
66 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020067 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000068 return -1;
69}
70
turaj@webrtc.org7959e162013-09-12 18:30:26 +000071int AcmReceiver::SetMaximumDelay(int delay_ms) {
72 if (neteq_->SetMaximumDelay(delay_ms))
73 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020074 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000075 return -1;
76}
77
78int AcmReceiver::LeastRequiredDelayMs() const {
79 return neteq_->LeastRequiredDelayMs();
80}
81
henrik.lundin057fb892015-11-23 08:19:52 -080082rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010083 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080084 return last_packet_sample_rate_hz_;
85}
86
henrik.lundind89814b2015-11-23 06:49:25 -080087int AcmReceiver::last_output_sample_rate_hz() const {
88 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000089}
90
turaj@webrtc.org7959e162013-09-12 18:30:26 +000091int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080092 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000093 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000094 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
95
96 {
Tommi9090e0b2016-01-20 13:39:36 +010097 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000098
kwibergee2bac22015-11-11 10:34:00 -080099 const Decoder* decoder = RtpHeaderToDecoder(*header, incoming_payload[0]);
Jelena Marusica9907842015-03-26 14:01:30 +0100100 if (!decoder) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000101 LOG_F(LS_ERROR) << "Payload-type "
102 << static_cast<int>(header->payloadType)
103 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000104 return -1;
105 }
kwibergfb3d8b32015-11-06 01:24:08 -0800106 const int sample_rate_hz = [&decoder] {
107 const auto ci = RentACodec::CodecIdFromIndex(decoder->acm_codec_id);
108 return ci ? RentACodec::CodecInstById(*ci)->plfreq : -1;
109 }();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000110 receive_timestamp = NowInTimestamp(sample_rate_hz);
111
henrik.lundin678c9032015-11-02 08:31:23 -0800112 // If this is a CNG while the audio codec is not mono, skip pushing in
113 // packets into NetEq.
114 if (IsCng(decoder->acm_codec_id) && last_audio_decoder_ &&
115 last_audio_decoder_->channels > 1)
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000116 return 0;
henrik.lundin678c9032015-11-02 08:31:23 -0800117 if (!IsCng(decoder->acm_codec_id) &&
118 decoder->acm_codec_id !=
119 *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
120 last_audio_decoder_ = decoder;
henrik.lundin057fb892015-11-23 08:19:52 -0800121 last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000122 }
123
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000124 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000125
kwibergee2bac22015-11-11 10:34:00 -0800126 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
127 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200128 LOG(LERROR) << "AcmReceiver::InsertPacket "
129 << static_cast<int>(header->payloadType)
130 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000131 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132 }
133 return 0;
134}
135
136int AcmReceiver::GetAudio(int desired_freq_hz, AudioFrame* audio_frame) {
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000137 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100138 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000139
henrik.lundin6d8e0112016-03-04 10:34:21 -0800140 enum NetEqOutputType type;
141 if (neteq_->GetAudio(audio_frame, &type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200142 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000143 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000144 }
145
henrik.lundind89814b2015-11-23 06:49:25 -0800146 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000147
148 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800149 const bool need_resampling =
150 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000151
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000152 if (need_resampling && !resampled_last_output_frame_) {
153 // Prime the resampler with the last frame.
154 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800155 int samples_per_channel_int = resampler_.Resample10Msec(
156 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800157 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
158 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200160 LOG(LERROR) << "AcmReceiver::GetAudio - "
161 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000162 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000163 }
164 }
165
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000166 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
167 // from NetEq changes. See WebRTC issue 3923.
168 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800169 int samples_per_channel_int = resampler_.Resample10Msec(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800170 audio_frame->data_, current_sample_rate_hz, desired_freq_hz,
171 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
172 audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700173 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200174 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000175 return -1;
176 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800177 audio_frame->samples_per_channel_ =
178 static_cast<size_t>(samples_per_channel_int);
179 audio_frame->sample_rate_hz_ = desired_freq_hz;
180 RTC_DCHECK_EQ(
181 audio_frame->sample_rate_hz_,
182 rtc::checked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000183 resampled_last_output_frame_ = true;
184 } else {
185 resampled_last_output_frame_ = false;
186 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000187 }
188
henrik.lundin6d8e0112016-03-04 10:34:21 -0800189 // Store current audio in |last_audio_buffer_| for next time.
190 memcpy(last_audio_buffer_.get(), audio_frame->data_,
191 sizeof(int16_t) * audio_frame->samples_per_channel_ *
192 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000193
wu@webrtc.org24301a62013-12-13 19:17:43 +0000194 call_stats_.DecodedByNetEq(audio_frame->speech_type_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000195
196 // Computes the RTP timestamp of the first sample in |audio_frame| from
wu@webrtc.org94454b72014-06-05 20:34:08 +0000197 // |GetPlayoutTimestamp|, which is the timestamp of the last sample of
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000198 // |audio_frame|.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800199 // TODO(henrik.lundin) Move setting of audio_frame->timestamp_ inside NetEq.
wu@webrtc.org94454b72014-06-05 20:34:08 +0000200 uint32_t playout_timestamp = 0;
201 if (GetPlayoutTimestamp(&playout_timestamp)) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700202 audio_frame->timestamp_ = playout_timestamp -
203 static_cast<uint32_t>(audio_frame->samples_per_channel_);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000204 } else {
205 // Remain 0 until we have a valid |playout_timestamp|.
206 audio_frame->timestamp_ = 0;
207 }
208
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000209 return 0;
210}
211
212int32_t AcmReceiver::AddCodec(int acm_codec_id,
213 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800214 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200215 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800216 AudioDecoder* audio_decoder,
217 const std::string& name) {
kwibergee1879c2015-10-29 06:20:28 -0700218 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
219 if (acm_codec_id == -1)
220 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100221 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700222 RentACodec::CodecIdFromIndex(acm_codec_id);
223 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100224 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700225 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
226 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
227 return *ned;
228 }();
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000229
Tommi9090e0b2016-01-20 13:39:36 +0100230 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000231
232 // The corresponding NetEq decoder ID.
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000233 // If this codec has been registered before.
Jelena Marusica9907842015-03-26 14:01:30 +0100234 auto it = decoders_.find(payload_type);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000235 if (it != decoders_.end()) {
236 const Decoder& decoder = it->second;
kwiberg4e14f092015-08-24 05:27:22 -0700237 if (acm_codec_id != -1 && decoder.acm_codec_id == acm_codec_id &&
238 decoder.channels == channels &&
Karl Wibergd8399e62015-05-25 14:39:56 +0200239 decoder.sample_rate_hz == sample_rate_hz) {
Jelena Marusica9907842015-03-26 14:01:30 +0100240 // Re-registering the same codec. Do nothing and return.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000241 return 0;
242 }
243
kwiberg4e14f092015-08-24 05:27:22 -0700244 // Changing codec. First unregister the old codec, then register the new
245 // one.
Jelena Marusica9907842015-03-26 14:01:30 +0100246 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200247 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000248 return -1;
249 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000250
251 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000252 }
253
254 int ret_val;
255 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800256 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000257 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800258 ret_val = neteq_->RegisterExternalDecoder(
259 audio_decoder, neteq_decoder, name, payload_type, sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000260 }
261 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200262 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
263 << static_cast<int>(payload_type)
264 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000265 return -1;
266 }
267
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000268 Decoder decoder;
269 decoder.acm_codec_id = acm_codec_id;
270 decoder.payload_type = payload_type;
271 decoder.channels = channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200272 decoder.sample_rate_hz = sample_rate_hz;
Jelena Marusica9907842015-03-26 14:01:30 +0100273 decoders_[payload_type] = decoder;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000274 return 0;
275}
276
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000277void AcmReceiver::FlushBuffers() {
278 neteq_->FlushBuffers();
279}
280
281// If failed in removing one of the codecs, this method continues to remove as
282// many as it can.
283int AcmReceiver::RemoveAllCodecs() {
284 int ret_val = 0;
Tommi9090e0b2016-01-20 13:39:36 +0100285 rtc::CritScope lock(&crit_sect_);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000286 for (auto it = decoders_.begin(); it != decoders_.end(); ) {
287 auto cur = it;
288 ++it; // it will be valid even if we erase cur
289 if (neteq_->RemovePayloadType(cur->second.payload_type) == 0) {
290 decoders_.erase(cur);
291 } else {
292 LOG_F(LS_ERROR) << "Cannot remove payload "
293 << static_cast<int>(cur->second.payload_type);
294 ret_val = -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000295 }
296 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000297
turaj@webrtc.orgd6a7a5f2013-09-25 01:09:23 +0000298 // No codec is registered, invalidate last audio decoder.
Jelena Marusica9907842015-03-26 14:01:30 +0100299 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800300 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000301 return ret_val;
302}
303
304int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100305 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100306 auto it = decoders_.find(payload_type);
307 if (it == decoders_.end()) { // Such a payload-type is not registered.
turaj@webrtc.orga92baea2013-12-13 00:10:44 +0000308 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000309 }
310 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200311 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000312 return -1;
313 }
henrik.lundin057fb892015-11-23 08:19:52 -0800314 if (last_audio_decoder_ == &it->second) {
Jelena Marusica9907842015-03-26 14:01:30 +0100315 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800316 last_packet_sample_rate_hz_ = rtc::Optional<int>();
317 }
Jelena Marusica9907842015-03-26 14:01:30 +0100318 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000319 return 0;
320}
321
wu@webrtc.org94454b72014-06-05 20:34:08 +0000322bool AcmReceiver::GetPlayoutTimestamp(uint32_t* timestamp) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000323 return neteq_->GetPlayoutTimestamp(timestamp);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000324}
325
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000326int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100327 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100328 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 return -1;
330 }
kwiberg4b938e52015-11-03 12:38:27 -0800331 *codec = *RentACodec::CodecInstById(
332 *RentACodec::CodecIdFromIndex(last_audio_decoder_->acm_codec_id));
Jelena Marusica9907842015-03-26 14:01:30 +0100333 codec->pltype = last_audio_decoder_->payload_type;
334 codec->channels = last_audio_decoder_->channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200335 codec->plfreq = last_audio_decoder_->sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000336 return 0;
337}
338
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000339void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000340 NetEqNetworkStatistics neteq_stat;
341 // NetEq function always returns zero, so we don't check the return value.
342 neteq_->NetworkStatistics(&neteq_stat);
343
344 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
345 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000346 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000347 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
348 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
349 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000350 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000351 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
352 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000353 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000354 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000355 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200356 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
357 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
358 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
359 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000360}
361
362int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
363 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100364 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100365 auto it = decoders_.find(payload_type);
366 if (it == decoders_.end()) {
Tommi92fbbb22015-05-27 22:07:35 +0200367 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
368 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000369 return -1;
370 }
Jelena Marusica9907842015-03-26 14:01:30 +0100371 const Decoder& decoder = it->second;
kwiberg4b938e52015-11-03 12:38:27 -0800372 *codec = *RentACodec::CodecInstById(
373 *RentACodec::CodecIdFromIndex(decoder.acm_codec_id));
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000374 codec->pltype = decoder.payload_type;
375 codec->channels = decoder.channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200376 codec->plfreq = decoder.sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000377 return 0;
378}
379
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000380int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700381 neteq_->EnableNack(max_nack_list_size);
382 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000383}
384
385void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700386 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000387}
388
389std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000390 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700391 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000392}
393
394void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000395 neteq_->SetMinimumDelay(0);
396 // TODO(turajs): Should NetEq Buffer be flushed?
397}
398
Jelena Marusica9907842015-03-26 14:01:30 +0100399const AcmReceiver::Decoder* AcmReceiver::RtpHeaderToDecoder(
400 const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800401 uint8_t payload_type) const {
Jelena Marusica9907842015-03-26 14:01:30 +0100402 auto it = decoders_.find(rtp_header.payloadType);
kwibergfce4a942015-10-27 11:40:24 -0700403 const auto red_index =
404 RentACodec::CodecIndexFromId(RentACodec::CodecId::kRED);
405 if (red_index && // This ensures that RED is defined in WebRTC.
406 it != decoders_.end() && it->second.acm_codec_id == *red_index) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000407 // This is a RED packet, get the payload of the audio codec.
kwibergee2bac22015-11-11 10:34:00 -0800408 it = decoders_.find(payload_type & 0x7F);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000409 }
410
411 // Check if the payload is registered.
Jelena Marusica9907842015-03-26 14:01:30 +0100412 return it != decoders_.end() ? &it->second : nullptr;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000413}
414
415uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
416 // Down-cast the time to (32-6)-bit since we only care about
417 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
418 // We masked 6 most significant bits of 32-bit so there is no overflow in
419 // the conversion from milliseconds to timestamp.
420 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000421 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000422 return static_cast<uint32_t>(
423 (decoder_sampling_rate / 1000) * now_in_ms);
424}
425
wu@webrtc.org24301a62013-12-13 19:17:43 +0000426void AcmReceiver::GetDecodingCallStatistics(
427 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100428 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000429 *stats = call_stats_.GetDecodingStatistics();
430}
431
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000432} // namespace acm2
433
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000434} // namespace webrtc