blob: 73518b84fc9ac7f5a6c8fac0f359e5e2e536178f [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
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.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000040 assert(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
44AcmReceiver::~AcmReceiver() {
45 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000046}
47
48int AcmReceiver::SetMinimumDelay(int delay_ms) {
49 if (neteq_->SetMinimumDelay(delay_ms))
50 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020051 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000052 return -1;
53}
54
turaj@webrtc.org7959e162013-09-12 18:30:26 +000055int AcmReceiver::SetMaximumDelay(int delay_ms) {
56 if (neteq_->SetMaximumDelay(delay_ms))
57 return 0;
Tommi92fbbb22015-05-27 22:07:35 +020058 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000059 return -1;
60}
61
62int AcmReceiver::LeastRequiredDelayMs() const {
63 return neteq_->LeastRequiredDelayMs();
64}
65
henrik.lundin057fb892015-11-23 08:19:52 -080066rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010067 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -080068 return last_packet_sample_rate_hz_;
69}
70
henrik.lundind89814b2015-11-23 06:49:25 -080071int AcmReceiver::last_output_sample_rate_hz() const {
72 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000073}
74
turaj@webrtc.org7959e162013-09-12 18:30:26 +000075int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080076 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000077 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000078 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
79
80 {
Tommi9090e0b2016-01-20 13:39:36 +010081 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000082
kwiberg6f0f6162016-09-20 03:07:46 -070083 const rtc::Optional<CodecInst> ci =
84 RtpHeaderToDecoder(*header, incoming_payload[0]);
85 if (!ci) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +000086 LOG_F(LS_ERROR) << "Payload-type "
87 << static_cast<int>(header->payloadType)
88 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000089 return -1;
90 }
kwiberg6f0f6162016-09-20 03:07:46 -070091 receive_timestamp = NowInTimestamp(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000092
kwiberg6f0f6162016-09-20 03:07:46 -070093 if (STR_CASE_CMP(ci->plname, "cn") == 0) {
94 if (last_audio_decoder_ && last_audio_decoder_->channels > 1) {
95 // This is a CNG and the audio codec is not mono, so skip pushing in
96 // packets into NetEq.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000097 return 0;
kwiberg6f0f6162016-09-20 03:07:46 -070098 }
99 } else {
100 last_audio_decoder_ = ci;
ossue280cde2016-10-12 11:04:10 -0700101 last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
102 RTC_DCHECK(last_audio_format_);
kwiberg6f0f6162016-09-20 03:07:46 -0700103 last_packet_sample_rate_hz_ = rtc::Optional<int>(ci->plfreq);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000104 }
105
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000106 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000107
kwibergee2bac22015-11-11 10:34:00 -0800108 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
109 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200110 LOG(LERROR) << "AcmReceiver::InsertPacket "
111 << static_cast<int>(header->payloadType)
112 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000113 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000114 }
115 return 0;
116}
117
henrik.lundin834a6ea2016-05-13 03:45:24 -0700118int AcmReceiver::GetAudio(int desired_freq_hz,
119 AudioFrame* audio_frame,
120 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700121 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000122 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100123 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000124
henrik.lundin834a6ea2016-05-13 03:45:24 -0700125 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200126 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000127 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000128 }
129
henrik.lundind89814b2015-11-23 06:49:25 -0800130 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000131
132 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800133 const bool need_resampling =
134 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000135
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000136 if (need_resampling && !resampled_last_output_frame_) {
137 // Prime the resampler with the last frame.
138 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800139 int samples_per_channel_int = resampler_.Resample10Msec(
140 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800141 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
142 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200144 LOG(LERROR) << "AcmReceiver::GetAudio - "
145 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000146 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000147 }
148 }
149
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000150 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
151 // from NetEq changes. See WebRTC issue 3923.
152 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800153 int samples_per_channel_int = resampler_.Resample10Msec(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800154 audio_frame->data_, current_sample_rate_hz, desired_freq_hz,
155 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
156 audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700157 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200158 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000159 return -1;
160 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800161 audio_frame->samples_per_channel_ =
162 static_cast<size_t>(samples_per_channel_int);
163 audio_frame->sample_rate_hz_ = desired_freq_hz;
164 RTC_DCHECK_EQ(
165 audio_frame->sample_rate_hz_,
166 rtc::checked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000167 resampled_last_output_frame_ = true;
168 } else {
169 resampled_last_output_frame_ = false;
170 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000171 }
172
henrik.lundin6d8e0112016-03-04 10:34:21 -0800173 // Store current audio in |last_audio_buffer_| for next time.
174 memcpy(last_audio_buffer_.get(), audio_frame->data_,
175 sizeof(int16_t) * audio_frame->samples_per_channel_ *
176 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000177
henrik.lundin63489782016-09-20 01:47:12 -0700178 call_stats_.DecodedByNetEq(audio_frame->speech_type_, *muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000179 return 0;
180}
181
182int32_t AcmReceiver::AddCodec(int acm_codec_id,
183 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800184 size_t channels,
kwibergc4ccd4d2016-09-21 10:55:15 -0700185 int /*sample_rate_hz*/,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800186 AudioDecoder* audio_decoder,
187 const std::string& name) {
kwibergc4ccd4d2016-09-21 10:55:15 -0700188 // TODO(kwiberg): This function has been ignoring the |sample_rate_hz|
189 // argument for a long time. Arguably, it should simply be removed.
190
kwibergee1879c2015-10-29 06:20:28 -0700191 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
192 if (acm_codec_id == -1)
193 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100194 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700195 RentACodec::CodecIdFromIndex(acm_codec_id);
196 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100197 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700198 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
199 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
200 return *ned;
201 }();
kwibergc4ccd4d2016-09-21 10:55:15 -0700202 const rtc::Optional<SdpAudioFormat> new_format =
203 RentACodec::NetEqDecoderToSdpAudioFormat(neteq_decoder);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000204
Tommi9090e0b2016-01-20 13:39:36 +0100205 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000206
ossuf1b08da2016-09-23 02:19:43 -0700207 const auto old_format = neteq_->GetDecoderFormat(payload_type);
kwibergc4ccd4d2016-09-21 10:55:15 -0700208 if (old_format && new_format && *old_format == *new_format) {
209 // Re-registering the same codec. Do nothing and return.
210 return 0;
211 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000212
kwibergc4ccd4d2016-09-21 10:55:15 -0700213 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK &&
214 neteq_->LastError() != NetEq::kDecoderNotFound) {
215 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
216 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000217 }
218
219 int ret_val;
220 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800221 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000222 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800223 ret_val = neteq_->RegisterExternalDecoder(
kwiberg342f7402016-06-16 03:18:00 -0700224 audio_decoder, neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000225 }
226 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200227 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
228 << static_cast<int>(payload_type)
229 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000230 return -1;
231 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000232 return 0;
233}
234
kwiberg5adaf732016-10-04 09:33:27 -0700235bool AcmReceiver::AddCodec(int rtp_payload_type,
236 const SdpAudioFormat& audio_format) {
237 const auto old_format = neteq_->GetDecoderFormat(rtp_payload_type);
238 if (old_format && *old_format == audio_format) {
239 // Re-registering the same codec. Do nothing and return.
240 return true;
241 }
242
243 if (neteq_->RemovePayloadType(rtp_payload_type) != NetEq::kOK &&
244 neteq_->LastError() != NetEq::kDecoderNotFound) {
245 LOG(LERROR) << "AcmReceiver::AddCodec: Could not remove existing decoder"
246 " for payload type "
247 << rtp_payload_type;
248 return false;
249 }
250
251 const bool success =
252 neteq_->RegisterPayloadType(rtp_payload_type, audio_format);
253 if (!success) {
254 LOG(LERROR) << "AcmReceiver::AddCodec failed for payload type "
255 << rtp_payload_type << ", decoder format " << audio_format;
256 }
257 return success;
258}
259
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000260void AcmReceiver::FlushBuffers() {
261 neteq_->FlushBuffers();
262}
263
kwiberg6b19b562016-09-20 04:02:25 -0700264void AcmReceiver::RemoveAllCodecs() {
Tommi9090e0b2016-01-20 13:39:36 +0100265 rtc::CritScope lock(&crit_sect_);
kwiberg6b19b562016-09-20 04:02:25 -0700266 neteq_->RemoveAllPayloadTypes();
kwiberg6f0f6162016-09-20 03:07:46 -0700267 last_audio_decoder_ = rtc::Optional<CodecInst>();
ossue280cde2016-10-12 11:04:10 -0700268 last_audio_format_ = rtc::Optional<SdpAudioFormat>();
henrik.lundin057fb892015-11-23 08:19:52 -0800269 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000270}
271
272int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100273 rtc::CritScope lock(&crit_sect_);
kwibergc4ccd4d2016-09-21 10:55:15 -0700274 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK &&
275 neteq_->LastError() != NetEq::kDecoderNotFound) {
Tommi92fbbb22015-05-27 22:07:35 +0200276 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000277 return -1;
278 }
kwiberg6f0f6162016-09-20 03:07:46 -0700279 if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
280 last_audio_decoder_ = rtc::Optional<CodecInst>();
ossue280cde2016-10-12 11:04:10 -0700281 last_audio_format_ = rtc::Optional<SdpAudioFormat>();
henrik.lundin057fb892015-11-23 08:19:52 -0800282 last_packet_sample_rate_hz_ = rtc::Optional<int>();
283 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000284 return 0;
285}
286
henrik.lundin9a410dd2016-04-06 01:39:22 -0700287rtc::Optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
288 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289}
290
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700291int AcmReceiver::FilteredCurrentDelayMs() const {
292 return neteq_->FilteredCurrentDelayMs();
293}
294
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000295int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100296 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100297 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000298 return -1;
299 }
kwiberg6f0f6162016-09-20 03:07:46 -0700300 *codec = *last_audio_decoder_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000301 return 0;
302}
303
ossue280cde2016-10-12 11:04:10 -0700304rtc::Optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
305 rtc::CritScope lock(&crit_sect_);
306 return last_audio_format_;
307}
308
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000309void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000310 NetEqNetworkStatistics neteq_stat;
311 // NetEq function always returns zero, so we don't check the return value.
312 neteq_->NetworkStatistics(&neteq_stat);
313
314 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
315 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000316 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000317 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
318 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
319 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000320 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000321 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
322 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000323 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000324 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000325 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200326 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
327 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
328 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
329 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000330}
331
332int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
333 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100334 rtc::CritScope lock(&crit_sect_);
kwibergd1201922016-09-20 15:18:21 -0700335 const rtc::Optional<CodecInst> ci = neteq_->GetDecoder(payload_type);
336 if (ci) {
337 *codec = *ci;
338 return 0;
339 } else {
Tommi92fbbb22015-05-27 22:07:35 +0200340 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
341 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000342 return -1;
343 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000344}
345
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000346int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700347 neteq_->EnableNack(max_nack_list_size);
348 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000349}
350
351void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700352 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000353}
354
355std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000356 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700357 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000358}
359
360void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000361 neteq_->SetMinimumDelay(0);
362 // TODO(turajs): Should NetEq Buffer be flushed?
363}
364
kwiberg6f0f6162016-09-20 03:07:46 -0700365const rtc::Optional<CodecInst> AcmReceiver::RtpHeaderToDecoder(
Jelena Marusica9907842015-03-26 14:01:30 +0100366 const RTPHeader& rtp_header,
kwiberg6f0f6162016-09-20 03:07:46 -0700367 uint8_t first_payload_byte) const {
368 const rtc::Optional<CodecInst> ci =
369 neteq_->GetDecoder(rtp_header.payloadType);
370 if (ci && STR_CASE_CMP(ci->plname, "red") == 0) {
371 // This is a RED packet. Get the payload of the audio codec.
372 return neteq_->GetDecoder(first_payload_byte & 0x7f);
373 } else {
374 return ci;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000375 }
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000376}
377
378uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
379 // Down-cast the time to (32-6)-bit since we only care about
380 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
381 // We masked 6 most significant bits of 32-bit so there is no overflow in
382 // the conversion from milliseconds to timestamp.
383 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000384 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000385 return static_cast<uint32_t>(
386 (decoder_sampling_rate / 1000) * now_in_ms);
387}
388
wu@webrtc.org24301a62013-12-13 19:17:43 +0000389void AcmReceiver::GetDecodingCallStatistics(
390 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100391 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000392 *stats = call_stats_.GetDecodingStatistics();
393}
394
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000395} // namespace acm2
396
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000397} // namespace webrtc