blob: 39d8a0d175e44aa5432a2997759cf0ae671481f0 [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"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
22#include "webrtc/common_types.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000023#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
kjellander3e6db232015-11-26 04:44:54 -080024#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
25#include "webrtc/modules/audio_coding/acm2/call_statistics.h"
Henrik Kjellander74640892015-10-29 11:31:02 +010026#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010027#include "webrtc/system_wrappers/include/clock.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/tick_util.h"
29#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
turaj@webrtc.org7959e162013-09-12 18:30:26 +000035namespace {
36
turaj@webrtc.org7959e162013-09-12 18:30:26 +000037// |vad_activity_| field of |audio_frame| is set to |previous_audio_activity_|
38// before the call to this function.
39void SetAudioFrameActivityAndType(bool vad_enabled,
40 NetEqOutputType type,
41 AudioFrame* audio_frame) {
42 if (vad_enabled) {
43 switch (type) {
44 case kOutputNormal: {
45 audio_frame->vad_activity_ = AudioFrame::kVadActive;
46 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
47 break;
48 }
49 case kOutputVADPassive: {
50 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
51 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
52 break;
53 }
54 case kOutputCNG: {
55 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
56 audio_frame->speech_type_ = AudioFrame::kCNG;
57 break;
58 }
59 case kOutputPLC: {
60 // Don't change |audio_frame->vad_activity_|, it should be the same as
61 // |previous_audio_activity_|.
62 audio_frame->speech_type_ = AudioFrame::kPLC;
63 break;
64 }
65 case kOutputPLCtoCNG: {
66 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
67 audio_frame->speech_type_ = AudioFrame::kPLCCNG;
68 break;
69 }
70 default:
71 assert(false);
72 }
73 } else {
74 // Always return kVadUnknown when receive VAD is inactive
75 audio_frame->vad_activity_ = AudioFrame::kVadUnknown;
76 switch (type) {
77 case kOutputNormal: {
78 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
79 break;
80 }
81 case kOutputCNG: {
82 audio_frame->speech_type_ = AudioFrame::kCNG;
83 break;
84 }
85 case kOutputPLC: {
86 audio_frame->speech_type_ = AudioFrame::kPLC;
87 break;
88 }
89 case kOutputPLCtoCNG: {
90 audio_frame->speech_type_ = AudioFrame::kPLCCNG;
91 break;
92 }
93 case kOutputVADPassive: {
94 // Normally, we should no get any VAD decision if post-decoding VAD is
95 // not active. However, if post-decoding VAD has been active then
96 // disabled, we might be here for couple of frames.
97 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
Tommi92fbbb22015-05-27 22:07:35 +020098 LOG(WARNING) << "Post-decoding VAD is disabled but output is "
turaj@webrtc.org7959e162013-09-12 18:30:26 +000099 << "labeled VAD-passive";
100 break;
101 }
102 default:
103 assert(false);
104 }
105 }
106}
107
108// Is the given codec a CNG codec?
kwibergfce4a942015-10-27 11:40:24 -0700109// TODO(kwiberg): Move to RentACodec.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000110bool IsCng(int codec_id) {
kwibergfce4a942015-10-27 11:40:24 -0700111 auto i = RentACodec::CodecIdFromIndex(codec_id);
112 return (i && (*i == RentACodec::CodecId::kCNNB ||
113 *i == RentACodec::CodecId::kCNWB ||
114 *i == RentACodec::CodecId::kCNSWB ||
115 *i == RentACodec::CodecId::kCNFB));
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000116}
117
118} // namespace
119
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000120AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
henrik.lundin0023fdf2016-03-03 23:05:39 -0800121 : last_audio_decoder_(nullptr),
turaj@webrtc.org2086e0f2014-02-18 14:22:20 +0000122 previous_audio_activity_(AudioFrame::kVadPassive),
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000123 audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
124 last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000125 neteq_(NetEq::Create(config.neteq_config)),
henrik.lundin9bc26672015-11-02 03:25:57 -0800126 vad_enabled_(config.neteq_config.enable_post_decode_vad),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000127 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -0800128 resampled_last_output_frame_(true) {
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000129 assert(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000130 memset(audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
131 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132}
133
134AcmReceiver::~AcmReceiver() {
135 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000136}
137
138int AcmReceiver::SetMinimumDelay(int delay_ms) {
139 if (neteq_->SetMinimumDelay(delay_ms))
140 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200141 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000142 return -1;
143}
144
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000145int AcmReceiver::SetMaximumDelay(int delay_ms) {
146 if (neteq_->SetMaximumDelay(delay_ms))
147 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200148 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000149 return -1;
150}
151
152int AcmReceiver::LeastRequiredDelayMs() const {
153 return neteq_->LeastRequiredDelayMs();
154}
155
henrik.lundin057fb892015-11-23 08:19:52 -0800156rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +0100157 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -0800158 return last_packet_sample_rate_hz_;
159}
160
henrik.lundind89814b2015-11-23 06:49:25 -0800161int AcmReceiver::last_output_sample_rate_hz() const {
162 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000163}
164
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000165int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800166 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000167 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000168 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
169
170 {
Tommi9090e0b2016-01-20 13:39:36 +0100171 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000172
kwibergee2bac22015-11-11 10:34:00 -0800173 const Decoder* decoder = RtpHeaderToDecoder(*header, incoming_payload[0]);
Jelena Marusica9907842015-03-26 14:01:30 +0100174 if (!decoder) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000175 LOG_F(LS_ERROR) << "Payload-type "
176 << static_cast<int>(header->payloadType)
177 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000178 return -1;
179 }
kwibergfb3d8b32015-11-06 01:24:08 -0800180 const int sample_rate_hz = [&decoder] {
181 const auto ci = RentACodec::CodecIdFromIndex(decoder->acm_codec_id);
182 return ci ? RentACodec::CodecInstById(*ci)->plfreq : -1;
183 }();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000184 receive_timestamp = NowInTimestamp(sample_rate_hz);
185
henrik.lundin678c9032015-11-02 08:31:23 -0800186 // If this is a CNG while the audio codec is not mono, skip pushing in
187 // packets into NetEq.
188 if (IsCng(decoder->acm_codec_id) && last_audio_decoder_ &&
189 last_audio_decoder_->channels > 1)
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000190 return 0;
henrik.lundin678c9032015-11-02 08:31:23 -0800191 if (!IsCng(decoder->acm_codec_id) &&
192 decoder->acm_codec_id !=
193 *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
194 last_audio_decoder_ = decoder;
henrik.lundin057fb892015-11-23 08:19:52 -0800195 last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196 }
197
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000198 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000199
kwibergee2bac22015-11-11 10:34:00 -0800200 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
201 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200202 LOG(LERROR) << "AcmReceiver::InsertPacket "
203 << static_cast<int>(header->payloadType)
204 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000205 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000206 }
207 return 0;
208}
209
210int AcmReceiver::GetAudio(int desired_freq_hz, AudioFrame* audio_frame) {
211 enum NetEqOutputType type;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700212 size_t samples_per_channel;
Peter Kasting69558702016-01-12 16:26:35 -0800213 size_t num_channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000214
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000215 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100216 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000217
218 // Always write the output to |audio_buffer_| first.
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000219 if (neteq_->GetAudio(AudioFrame::kMaxDataSizeSamples,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000220 audio_buffer_.get(),
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000221 &samples_per_channel,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000222 &num_channels,
223 &type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200224 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000225 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000226 }
227
henrik.lundind89814b2015-11-23 06:49:25 -0800228 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000229
230 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800231 const bool need_resampling =
232 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000233
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000234 if (need_resampling && !resampled_last_output_frame_) {
235 // Prime the resampler with the last frame.
236 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800237 int samples_per_channel_int = resampler_.Resample10Msec(
238 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
239 num_channels, AudioFrame::kMaxDataSizeSamples, temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700240 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200241 LOG(LERROR) << "AcmReceiver::GetAudio - "
242 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000243 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000244 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700245 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000246 }
247
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000248 // The audio in |audio_buffer_| is tansferred to |audio_frame_| below, either
249 // through resampling, or through straight memcpy.
250 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
251 // from NetEq changes. See WebRTC issue 3923.
252 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800253 int samples_per_channel_int = resampler_.Resample10Msec(
254 audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
255 num_channels, AudioFrame::kMaxDataSizeSamples, audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700256 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200257 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000258 return -1;
259 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700260 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000261 resampled_last_output_frame_ = true;
262 } else {
263 resampled_last_output_frame_ = false;
264 // We might end up here ONLY if codec is changed.
265 memcpy(audio_frame->data_,
266 audio_buffer_.get(),
267 samples_per_channel * num_channels * sizeof(int16_t));
268 }
269
270 // Swap buffers, so that the current audio is stored in |last_audio_buffer_|
271 // for next time.
272 audio_buffer_.swap(last_audio_buffer_);
273
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000274 audio_frame->num_channels_ = num_channels;
275 audio_frame->samples_per_channel_ = samples_per_channel;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700276 audio_frame->sample_rate_hz_ = static_cast<int>(samples_per_channel * 100);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000277
278 // Should set |vad_activity| before calling SetAudioFrameActivityAndType().
279 audio_frame->vad_activity_ = previous_audio_activity_;
280 SetAudioFrameActivityAndType(vad_enabled_, type, audio_frame);
281 previous_audio_activity_ = audio_frame->vad_activity_;
wu@webrtc.org24301a62013-12-13 19:17:43 +0000282 call_stats_.DecodedByNetEq(audio_frame->speech_type_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000283
284 // Computes the RTP timestamp of the first sample in |audio_frame| from
wu@webrtc.org94454b72014-06-05 20:34:08 +0000285 // |GetPlayoutTimestamp|, which is the timestamp of the last sample of
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000286 // |audio_frame|.
wu@webrtc.org94454b72014-06-05 20:34:08 +0000287 uint32_t playout_timestamp = 0;
288 if (GetPlayoutTimestamp(&playout_timestamp)) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700289 audio_frame->timestamp_ = playout_timestamp -
290 static_cast<uint32_t>(audio_frame->samples_per_channel_);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000291 } else {
292 // Remain 0 until we have a valid |playout_timestamp|.
293 audio_frame->timestamp_ = 0;
294 }
295
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000296 return 0;
297}
298
299int32_t AcmReceiver::AddCodec(int acm_codec_id,
300 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800301 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200302 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800303 AudioDecoder* audio_decoder,
304 const std::string& name) {
kwibergee1879c2015-10-29 06:20:28 -0700305 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
306 if (acm_codec_id == -1)
307 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100308 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700309 RentACodec::CodecIdFromIndex(acm_codec_id);
310 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100311 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700312 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
313 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
314 return *ned;
315 }();
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000316
Tommi9090e0b2016-01-20 13:39:36 +0100317 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000318
319 // The corresponding NetEq decoder ID.
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000320 // If this codec has been registered before.
Jelena Marusica9907842015-03-26 14:01:30 +0100321 auto it = decoders_.find(payload_type);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000322 if (it != decoders_.end()) {
323 const Decoder& decoder = it->second;
kwiberg4e14f092015-08-24 05:27:22 -0700324 if (acm_codec_id != -1 && decoder.acm_codec_id == acm_codec_id &&
325 decoder.channels == channels &&
Karl Wibergd8399e62015-05-25 14:39:56 +0200326 decoder.sample_rate_hz == sample_rate_hz) {
Jelena Marusica9907842015-03-26 14:01:30 +0100327 // Re-registering the same codec. Do nothing and return.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000328 return 0;
329 }
330
kwiberg4e14f092015-08-24 05:27:22 -0700331 // Changing codec. First unregister the old codec, then register the new
332 // one.
Jelena Marusica9907842015-03-26 14:01:30 +0100333 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200334 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000335 return -1;
336 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000337
338 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000339 }
340
341 int ret_val;
342 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800343 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000344 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800345 ret_val = neteq_->RegisterExternalDecoder(
346 audio_decoder, neteq_decoder, name, payload_type, sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000347 }
348 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200349 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
350 << static_cast<int>(payload_type)
351 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000352 return -1;
353 }
354
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000355 Decoder decoder;
356 decoder.acm_codec_id = acm_codec_id;
357 decoder.payload_type = payload_type;
358 decoder.channels = channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200359 decoder.sample_rate_hz = sample_rate_hz;
Jelena Marusica9907842015-03-26 14:01:30 +0100360 decoders_[payload_type] = decoder;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000361 return 0;
362}
363
364void AcmReceiver::EnableVad() {
365 neteq_->EnableVad();
Tommi9090e0b2016-01-20 13:39:36 +0100366 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000367 vad_enabled_ = true;
368}
369
370void AcmReceiver::DisableVad() {
371 neteq_->DisableVad();
Tommi9090e0b2016-01-20 13:39:36 +0100372 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000373 vad_enabled_ = false;
374}
375
376void AcmReceiver::FlushBuffers() {
377 neteq_->FlushBuffers();
378}
379
380// If failed in removing one of the codecs, this method continues to remove as
381// many as it can.
382int AcmReceiver::RemoveAllCodecs() {
383 int ret_val = 0;
Tommi9090e0b2016-01-20 13:39:36 +0100384 rtc::CritScope lock(&crit_sect_);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000385 for (auto it = decoders_.begin(); it != decoders_.end(); ) {
386 auto cur = it;
387 ++it; // it will be valid even if we erase cur
388 if (neteq_->RemovePayloadType(cur->second.payload_type) == 0) {
389 decoders_.erase(cur);
390 } else {
391 LOG_F(LS_ERROR) << "Cannot remove payload "
392 << static_cast<int>(cur->second.payload_type);
393 ret_val = -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000394 }
395 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000396
turaj@webrtc.orgd6a7a5f2013-09-25 01:09:23 +0000397 // No codec is registered, invalidate last audio decoder.
Jelena Marusica9907842015-03-26 14:01:30 +0100398 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800399 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000400 return ret_val;
401}
402
403int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100404 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100405 auto it = decoders_.find(payload_type);
406 if (it == decoders_.end()) { // Such a payload-type is not registered.
turaj@webrtc.orga92baea2013-12-13 00:10:44 +0000407 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000408 }
409 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200410 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000411 return -1;
412 }
henrik.lundin057fb892015-11-23 08:19:52 -0800413 if (last_audio_decoder_ == &it->second) {
Jelena Marusica9907842015-03-26 14:01:30 +0100414 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800415 last_packet_sample_rate_hz_ = rtc::Optional<int>();
416 }
Jelena Marusica9907842015-03-26 14:01:30 +0100417 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000418 return 0;
419}
420
wu@webrtc.org94454b72014-06-05 20:34:08 +0000421bool AcmReceiver::GetPlayoutTimestamp(uint32_t* timestamp) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000422 return neteq_->GetPlayoutTimestamp(timestamp);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000423}
424
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000425int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100426 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100427 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000428 return -1;
429 }
kwiberg4b938e52015-11-03 12:38:27 -0800430 *codec = *RentACodec::CodecInstById(
431 *RentACodec::CodecIdFromIndex(last_audio_decoder_->acm_codec_id));
Jelena Marusica9907842015-03-26 14:01:30 +0100432 codec->pltype = last_audio_decoder_->payload_type;
433 codec->channels = last_audio_decoder_->channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200434 codec->plfreq = last_audio_decoder_->sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000435 return 0;
436}
437
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000438void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000439 NetEqNetworkStatistics neteq_stat;
440 // NetEq function always returns zero, so we don't check the return value.
441 neteq_->NetworkStatistics(&neteq_stat);
442
443 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
444 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000445 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000446 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
447 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
448 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000449 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000450 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
451 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000452 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000453 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000454 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200455 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
456 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
457 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
458 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000459}
460
461int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
462 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100463 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100464 auto it = decoders_.find(payload_type);
465 if (it == decoders_.end()) {
Tommi92fbbb22015-05-27 22:07:35 +0200466 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
467 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000468 return -1;
469 }
Jelena Marusica9907842015-03-26 14:01:30 +0100470 const Decoder& decoder = it->second;
kwiberg4b938e52015-11-03 12:38:27 -0800471 *codec = *RentACodec::CodecInstById(
472 *RentACodec::CodecIdFromIndex(decoder.acm_codec_id));
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000473 codec->pltype = decoder.payload_type;
474 codec->channels = decoder.channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200475 codec->plfreq = decoder.sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000476 return 0;
477}
478
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000479int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700480 neteq_->EnableNack(max_nack_list_size);
481 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000482}
483
484void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700485 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000486}
487
488std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000489 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700490 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000491}
492
493void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000494 neteq_->SetMinimumDelay(0);
495 // TODO(turajs): Should NetEq Buffer be flushed?
496}
497
Jelena Marusica9907842015-03-26 14:01:30 +0100498const AcmReceiver::Decoder* AcmReceiver::RtpHeaderToDecoder(
499 const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800500 uint8_t payload_type) const {
Jelena Marusica9907842015-03-26 14:01:30 +0100501 auto it = decoders_.find(rtp_header.payloadType);
kwibergfce4a942015-10-27 11:40:24 -0700502 const auto red_index =
503 RentACodec::CodecIndexFromId(RentACodec::CodecId::kRED);
504 if (red_index && // This ensures that RED is defined in WebRTC.
505 it != decoders_.end() && it->second.acm_codec_id == *red_index) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000506 // This is a RED packet, get the payload of the audio codec.
kwibergee2bac22015-11-11 10:34:00 -0800507 it = decoders_.find(payload_type & 0x7F);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000508 }
509
510 // Check if the payload is registered.
Jelena Marusica9907842015-03-26 14:01:30 +0100511 return it != decoders_.end() ? &it->second : nullptr;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000512}
513
514uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
515 // Down-cast the time to (32-6)-bit since we only care about
516 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
517 // We masked 6 most significant bits of 32-bit so there is no overflow in
518 // the conversion from milliseconds to timestamp.
519 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000520 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000521 return static_cast<uint32_t>(
522 (decoder_sampling_rate / 1000) * now_in_ms);
523}
524
wu@webrtc.org24301a62013-12-13 19:17:43 +0000525void AcmReceiver::GetDecodingCallStatistics(
526 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100527 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000528 *stats = call_stats_.GetDecodingStatistics();
529}
530
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000531} // namespace acm2
532
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000533} // namespace webrtc