blob: 036877ce11b0e34cbc973c85aa2e52405bbc2580 [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"
28#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
29#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// |vad_activity_| field of |audio_frame| is set to |previous_audio_activity_|
39// before the call to this function.
40void SetAudioFrameActivityAndType(bool vad_enabled,
41 NetEqOutputType type,
42 AudioFrame* audio_frame) {
43 if (vad_enabled) {
44 switch (type) {
45 case kOutputNormal: {
46 audio_frame->vad_activity_ = AudioFrame::kVadActive;
47 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
48 break;
49 }
50 case kOutputVADPassive: {
51 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
52 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
53 break;
54 }
55 case kOutputCNG: {
56 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
57 audio_frame->speech_type_ = AudioFrame::kCNG;
58 break;
59 }
60 case kOutputPLC: {
61 // Don't change |audio_frame->vad_activity_|, it should be the same as
62 // |previous_audio_activity_|.
63 audio_frame->speech_type_ = AudioFrame::kPLC;
64 break;
65 }
66 case kOutputPLCtoCNG: {
67 audio_frame->vad_activity_ = AudioFrame::kVadPassive;
68 audio_frame->speech_type_ = AudioFrame::kPLCCNG;
69 break;
70 }
71 default:
72 assert(false);
73 }
74 } else {
75 // Always return kVadUnknown when receive VAD is inactive
76 audio_frame->vad_activity_ = AudioFrame::kVadUnknown;
77 switch (type) {
78 case kOutputNormal: {
79 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
80 break;
81 }
82 case kOutputCNG: {
83 audio_frame->speech_type_ = AudioFrame::kCNG;
84 break;
85 }
86 case kOutputPLC: {
87 audio_frame->speech_type_ = AudioFrame::kPLC;
88 break;
89 }
90 case kOutputPLCtoCNG: {
91 audio_frame->speech_type_ = AudioFrame::kPLCCNG;
92 break;
93 }
94 case kOutputVADPassive: {
95 // Normally, we should no get any VAD decision if post-decoding VAD is
96 // not active. However, if post-decoding VAD has been active then
97 // disabled, we might be here for couple of frames.
98 audio_frame->speech_type_ = AudioFrame::kNormalSpeech;
Tommi92fbbb22015-05-27 22:07:35 +020099 LOG(WARNING) << "Post-decoding VAD is disabled but output is "
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000100 << "labeled VAD-passive";
101 break;
102 }
103 default:
104 assert(false);
105 }
106 }
107}
108
109// Is the given codec a CNG codec?
kwibergfce4a942015-10-27 11:40:24 -0700110// TODO(kwiberg): Move to RentACodec.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000111bool IsCng(int codec_id) {
kwibergfce4a942015-10-27 11:40:24 -0700112 auto i = RentACodec::CodecIdFromIndex(codec_id);
113 return (i && (*i == RentACodec::CodecId::kCNNB ||
114 *i == RentACodec::CodecId::kCNWB ||
115 *i == RentACodec::CodecId::kCNSWB ||
116 *i == RentACodec::CodecId::kCNFB));
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000117}
118
119} // namespace
120
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000121AcmReceiver::AcmReceiver(const AudioCodingModule::Config& config)
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000122 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
123 id_(config.id),
Jelena Marusica9907842015-03-26 14:01:30 +0100124 last_audio_decoder_(nullptr),
turaj@webrtc.org2086e0f2014-02-18 14:22:20 +0000125 previous_audio_activity_(AudioFrame::kVadPassive),
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000126 audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
127 last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000128 neteq_(NetEq::Create(config.neteq_config)),
henrik.lundin9bc26672015-11-02 03:25:57 -0800129 vad_enabled_(config.neteq_config.enable_post_decode_vad),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000130 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -0800131 resampled_last_output_frame_(true) {
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000132 assert(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000133 memset(audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
134 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000135}
136
137AcmReceiver::~AcmReceiver() {
138 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000139}
140
141int AcmReceiver::SetMinimumDelay(int delay_ms) {
142 if (neteq_->SetMinimumDelay(delay_ms))
143 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200144 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000145 return -1;
146}
147
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148int AcmReceiver::SetMaximumDelay(int delay_ms) {
149 if (neteq_->SetMaximumDelay(delay_ms))
150 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200151 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000152 return -1;
153}
154
155int AcmReceiver::LeastRequiredDelayMs() const {
156 return neteq_->LeastRequiredDelayMs();
157}
158
henrik.lundin057fb892015-11-23 08:19:52 -0800159rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
160 CriticalSectionScoped lock(crit_sect_.get());
161 return last_packet_sample_rate_hz_;
162}
163
henrik.lundind89814b2015-11-23 06:49:25 -0800164int AcmReceiver::last_output_sample_rate_hz() const {
165 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000166}
167
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000168int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800169 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000170 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000171 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
172
173 {
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000174 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000175
kwibergee2bac22015-11-11 10:34:00 -0800176 const Decoder* decoder = RtpHeaderToDecoder(*header, incoming_payload[0]);
Jelena Marusica9907842015-03-26 14:01:30 +0100177 if (!decoder) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000178 LOG_F(LS_ERROR) << "Payload-type "
179 << static_cast<int>(header->payloadType)
180 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000181 return -1;
182 }
kwibergfb3d8b32015-11-06 01:24:08 -0800183 const int sample_rate_hz = [&decoder] {
184 const auto ci = RentACodec::CodecIdFromIndex(decoder->acm_codec_id);
185 return ci ? RentACodec::CodecInstById(*ci)->plfreq : -1;
186 }();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000187 receive_timestamp = NowInTimestamp(sample_rate_hz);
188
henrik.lundin678c9032015-11-02 08:31:23 -0800189 // If this is a CNG while the audio codec is not mono, skip pushing in
190 // packets into NetEq.
191 if (IsCng(decoder->acm_codec_id) && last_audio_decoder_ &&
192 last_audio_decoder_->channels > 1)
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000193 return 0;
henrik.lundin678c9032015-11-02 08:31:23 -0800194 if (!IsCng(decoder->acm_codec_id) &&
195 decoder->acm_codec_id !=
196 *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
197 last_audio_decoder_ = decoder;
henrik.lundin057fb892015-11-23 08:19:52 -0800198 last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000199 }
200
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000201 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000202
kwibergee2bac22015-11-11 10:34:00 -0800203 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
204 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200205 LOG(LERROR) << "AcmReceiver::InsertPacket "
206 << static_cast<int>(header->payloadType)
207 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000208 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000209 }
210 return 0;
211}
212
213int AcmReceiver::GetAudio(int desired_freq_hz, AudioFrame* audio_frame) {
214 enum NetEqOutputType type;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700215 size_t samples_per_channel;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000216 int num_channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000217
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000218 // Accessing members, take the lock.
219 CriticalSectionScoped lock(crit_sect_.get());
220
221 // Always write the output to |audio_buffer_| first.
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000222 if (neteq_->GetAudio(AudioFrame::kMaxDataSizeSamples,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000223 audio_buffer_.get(),
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000224 &samples_per_channel,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000225 &num_channels,
226 &type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200227 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000228 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000229 }
230
henrik.lundind89814b2015-11-23 06:49:25 -0800231 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000232
233 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800234 const bool need_resampling =
235 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000236
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000237 if (need_resampling && !resampled_last_output_frame_) {
238 // Prime the resampler with the last frame.
239 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800240 int samples_per_channel_int = resampler_.Resample10Msec(
241 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
242 num_channels, AudioFrame::kMaxDataSizeSamples, temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700243 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200244 LOG(LERROR) << "AcmReceiver::GetAudio - "
245 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000246 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000247 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700248 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000249 }
250
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000251 // The audio in |audio_buffer_| is tansferred to |audio_frame_| below, either
252 // through resampling, or through straight memcpy.
253 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
254 // from NetEq changes. See WebRTC issue 3923.
255 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800256 int samples_per_channel_int = resampler_.Resample10Msec(
257 audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
258 num_channels, AudioFrame::kMaxDataSizeSamples, audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700259 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200260 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000261 return -1;
262 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700263 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000264 resampled_last_output_frame_ = true;
265 } else {
266 resampled_last_output_frame_ = false;
267 // We might end up here ONLY if codec is changed.
268 memcpy(audio_frame->data_,
269 audio_buffer_.get(),
270 samples_per_channel * num_channels * sizeof(int16_t));
271 }
272
273 // Swap buffers, so that the current audio is stored in |last_audio_buffer_|
274 // for next time.
275 audio_buffer_.swap(last_audio_buffer_);
276
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000277 audio_frame->num_channels_ = num_channels;
278 audio_frame->samples_per_channel_ = samples_per_channel;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700279 audio_frame->sample_rate_hz_ = static_cast<int>(samples_per_channel * 100);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000280
281 // Should set |vad_activity| before calling SetAudioFrameActivityAndType().
282 audio_frame->vad_activity_ = previous_audio_activity_;
283 SetAudioFrameActivityAndType(vad_enabled_, type, audio_frame);
284 previous_audio_activity_ = audio_frame->vad_activity_;
wu@webrtc.org24301a62013-12-13 19:17:43 +0000285 call_stats_.DecodedByNetEq(audio_frame->speech_type_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000286
287 // Computes the RTP timestamp of the first sample in |audio_frame| from
wu@webrtc.org94454b72014-06-05 20:34:08 +0000288 // |GetPlayoutTimestamp|, which is the timestamp of the last sample of
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000289 // |audio_frame|.
wu@webrtc.org94454b72014-06-05 20:34:08 +0000290 uint32_t playout_timestamp = 0;
291 if (GetPlayoutTimestamp(&playout_timestamp)) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700292 audio_frame->timestamp_ = playout_timestamp -
293 static_cast<uint32_t>(audio_frame->samples_per_channel_);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000294 } else {
295 // Remain 0 until we have a valid |playout_timestamp|.
296 audio_frame->timestamp_ = 0;
297 }
298
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000299 return 0;
300}
301
302int32_t AcmReceiver::AddCodec(int acm_codec_id,
303 uint8_t payload_type,
304 int channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200305 int sample_rate_hz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000306 AudioDecoder* audio_decoder) {
kwibergee1879c2015-10-29 06:20:28 -0700307 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
308 if (acm_codec_id == -1)
309 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100310 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700311 RentACodec::CodecIdFromIndex(acm_codec_id);
312 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100313 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700314 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
315 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
316 return *ned;
317 }();
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000318
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000319 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000320
321 // The corresponding NetEq decoder ID.
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000322 // If this codec has been registered before.
Jelena Marusica9907842015-03-26 14:01:30 +0100323 auto it = decoders_.find(payload_type);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000324 if (it != decoders_.end()) {
325 const Decoder& decoder = it->second;
kwiberg4e14f092015-08-24 05:27:22 -0700326 if (acm_codec_id != -1 && decoder.acm_codec_id == acm_codec_id &&
327 decoder.channels == channels &&
Karl Wibergd8399e62015-05-25 14:39:56 +0200328 decoder.sample_rate_hz == sample_rate_hz) {
Jelena Marusica9907842015-03-26 14:01:30 +0100329 // Re-registering the same codec. Do nothing and return.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000330 return 0;
331 }
332
kwiberg4e14f092015-08-24 05:27:22 -0700333 // Changing codec. First unregister the old codec, then register the new
334 // one.
Jelena Marusica9907842015-03-26 14:01:30 +0100335 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200336 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000337 return -1;
338 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000339
340 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000341 }
342
343 int ret_val;
344 if (!audio_decoder) {
345 ret_val = neteq_->RegisterPayloadType(neteq_decoder, payload_type);
346 } else {
Karl Wibergd8399e62015-05-25 14:39:56 +0200347 ret_val = neteq_->RegisterExternalDecoder(audio_decoder, neteq_decoder,
348 payload_type, sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000349 }
350 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200351 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
352 << static_cast<int>(payload_type)
353 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000354 return -1;
355 }
356
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000357 Decoder decoder;
358 decoder.acm_codec_id = acm_codec_id;
359 decoder.payload_type = payload_type;
360 decoder.channels = channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200361 decoder.sample_rate_hz = sample_rate_hz;
Jelena Marusica9907842015-03-26 14:01:30 +0100362 decoders_[payload_type] = decoder;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000363 return 0;
364}
365
366void AcmReceiver::EnableVad() {
367 neteq_->EnableVad();
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000368 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000369 vad_enabled_ = true;
370}
371
372void AcmReceiver::DisableVad() {
373 neteq_->DisableVad();
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000374 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000375 vad_enabled_ = false;
376}
377
378void AcmReceiver::FlushBuffers() {
379 neteq_->FlushBuffers();
380}
381
382// If failed in removing one of the codecs, this method continues to remove as
383// many as it can.
384int AcmReceiver::RemoveAllCodecs() {
385 int ret_val = 0;
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000386 CriticalSectionScoped lock(crit_sect_.get());
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000387 for (auto it = decoders_.begin(); it != decoders_.end(); ) {
388 auto cur = it;
389 ++it; // it will be valid even if we erase cur
390 if (neteq_->RemovePayloadType(cur->second.payload_type) == 0) {
391 decoders_.erase(cur);
392 } else {
393 LOG_F(LS_ERROR) << "Cannot remove payload "
394 << static_cast<int>(cur->second.payload_type);
395 ret_val = -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000396 }
397 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000398
turaj@webrtc.orgd6a7a5f2013-09-25 01:09:23 +0000399 // No codec is registered, invalidate last audio decoder.
Jelena Marusica9907842015-03-26 14:01:30 +0100400 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800401 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000402 return ret_val;
403}
404
405int AcmReceiver::RemoveCodec(uint8_t payload_type) {
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000406 CriticalSectionScoped lock(crit_sect_.get());
Jelena Marusica9907842015-03-26 14:01:30 +0100407 auto it = decoders_.find(payload_type);
408 if (it == decoders_.end()) { // Such a payload-type is not registered.
turaj@webrtc.orga92baea2013-12-13 00:10:44 +0000409 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000410 }
411 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200412 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000413 return -1;
414 }
henrik.lundin057fb892015-11-23 08:19:52 -0800415 if (last_audio_decoder_ == &it->second) {
Jelena Marusica9907842015-03-26 14:01:30 +0100416 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800417 last_packet_sample_rate_hz_ = rtc::Optional<int>();
418 }
Jelena Marusica9907842015-03-26 14:01:30 +0100419 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000420 return 0;
421}
422
423void AcmReceiver::set_id(int id) {
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000424 CriticalSectionScoped lock(crit_sect_.get());
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000425 id_ = id;
426}
427
wu@webrtc.org94454b72014-06-05 20:34:08 +0000428bool AcmReceiver::GetPlayoutTimestamp(uint32_t* timestamp) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000429 return neteq_->GetPlayoutTimestamp(timestamp);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000430}
431
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000432int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000433 CriticalSectionScoped lock(crit_sect_.get());
Jelena Marusica9907842015-03-26 14:01:30 +0100434 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000435 return -1;
436 }
kwiberg4b938e52015-11-03 12:38:27 -0800437 *codec = *RentACodec::CodecInstById(
438 *RentACodec::CodecIdFromIndex(last_audio_decoder_->acm_codec_id));
Jelena Marusica9907842015-03-26 14:01:30 +0100439 codec->pltype = last_audio_decoder_->payload_type;
440 codec->channels = last_audio_decoder_->channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200441 codec->plfreq = last_audio_decoder_->sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000442 return 0;
443}
444
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000445void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000446 NetEqNetworkStatistics neteq_stat;
447 // NetEq function always returns zero, so we don't check the return value.
448 neteq_->NetworkStatistics(&neteq_stat);
449
450 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
451 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000452 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000453 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
454 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
455 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000456 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000457 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
458 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000459 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000460 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000461 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200462 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
463 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
464 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
465 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000466}
467
468int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
469 CodecInst* codec) const {
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000470 CriticalSectionScoped lock(crit_sect_.get());
Jelena Marusica9907842015-03-26 14:01:30 +0100471 auto it = decoders_.find(payload_type);
472 if (it == decoders_.end()) {
Tommi92fbbb22015-05-27 22:07:35 +0200473 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
474 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000475 return -1;
476 }
Jelena Marusica9907842015-03-26 14:01:30 +0100477 const Decoder& decoder = it->second;
kwiberg4b938e52015-11-03 12:38:27 -0800478 *codec = *RentACodec::CodecInstById(
479 *RentACodec::CodecIdFromIndex(decoder.acm_codec_id));
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000480 codec->pltype = decoder.payload_type;
481 codec->channels = decoder.channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200482 codec->plfreq = decoder.sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000483 return 0;
484}
485
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000486int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700487 neteq_->EnableNack(max_nack_list_size);
488 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000489}
490
491void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700492 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000493}
494
495std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000496 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700497 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000498}
499
500void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000501 neteq_->SetMinimumDelay(0);
502 // TODO(turajs): Should NetEq Buffer be flushed?
503}
504
Jelena Marusica9907842015-03-26 14:01:30 +0100505const AcmReceiver::Decoder* AcmReceiver::RtpHeaderToDecoder(
506 const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800507 uint8_t payload_type) const {
Jelena Marusica9907842015-03-26 14:01:30 +0100508 auto it = decoders_.find(rtp_header.payloadType);
kwibergfce4a942015-10-27 11:40:24 -0700509 const auto red_index =
510 RentACodec::CodecIndexFromId(RentACodec::CodecId::kRED);
511 if (red_index && // This ensures that RED is defined in WebRTC.
512 it != decoders_.end() && it->second.acm_codec_id == *red_index) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000513 // This is a RED packet, get the payload of the audio codec.
kwibergee2bac22015-11-11 10:34:00 -0800514 it = decoders_.find(payload_type & 0x7F);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000515 }
516
517 // Check if the payload is registered.
Jelena Marusica9907842015-03-26 14:01:30 +0100518 return it != decoders_.end() ? &it->second : nullptr;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000519}
520
521uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
522 // Down-cast the time to (32-6)-bit since we only care about
523 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
524 // We masked 6 most significant bits of 32-bit so there is no overflow in
525 // the conversion from milliseconds to timestamp.
526 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000527 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000528 return static_cast<uint32_t>(
529 (decoder_sampling_rate / 1000) * now_in_ms);
530}
531
wu@webrtc.org24301a62013-12-13 19:17:43 +0000532void AcmReceiver::GetDecodingCallStatistics(
533 AudioDecodingCallStats* stats) const {
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000534 CriticalSectionScoped lock(crit_sect_.get());
wu@webrtc.org24301a62013-12-13 19:17:43 +0000535 *stats = call_stats_.GetDecodingStatistics();
536}
537
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000538} // namespace acm2
539
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000540} // namespace webrtc