blob: 53ec8841e50c9a2b6e6b694a119d535ff236dfc9 [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)
Tommi9090e0b2016-01-20 13:39:36 +0100121 : id_(config.id),
Jelena Marusica9907842015-03-26 14:01:30 +0100122 last_audio_decoder_(nullptr),
turaj@webrtc.org2086e0f2014-02-18 14:22:20 +0000123 previous_audio_activity_(AudioFrame::kVadPassive),
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000124 audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
125 last_audio_buffer_(new int16_t[AudioFrame::kMaxDataSizeSamples]),
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000126 neteq_(NetEq::Create(config.neteq_config)),
henrik.lundin9bc26672015-11-02 03:25:57 -0800127 vad_enabled_(config.neteq_config.enable_post_decode_vad),
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000128 clock_(config.clock),
henrik.lundin678c9032015-11-02 08:31:23 -0800129 resampled_last_output_frame_(true) {
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +0000130 assert(clock_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000131 memset(audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
132 memset(last_audio_buffer_.get(), 0, AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133}
134
135AcmReceiver::~AcmReceiver() {
136 delete neteq_;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000137}
138
139int AcmReceiver::SetMinimumDelay(int delay_ms) {
140 if (neteq_->SetMinimumDelay(delay_ms))
141 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200142 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000143 return -1;
144}
145
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000146int AcmReceiver::SetMaximumDelay(int delay_ms) {
147 if (neteq_->SetMaximumDelay(delay_ms))
148 return 0;
Tommi92fbbb22015-05-27 22:07:35 +0200149 LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000150 return -1;
151}
152
153int AcmReceiver::LeastRequiredDelayMs() const {
154 return neteq_->LeastRequiredDelayMs();
155}
156
henrik.lundin057fb892015-11-23 08:19:52 -0800157rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +0100158 rtc::CritScope lock(&crit_sect_);
henrik.lundin057fb892015-11-23 08:19:52 -0800159 return last_packet_sample_rate_hz_;
160}
161
henrik.lundind89814b2015-11-23 06:49:25 -0800162int AcmReceiver::last_output_sample_rate_hz() const {
163 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000164}
165
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000166int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800167 rtc::ArrayView<const uint8_t> incoming_payload) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000168 uint32_t receive_timestamp = 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000169 const RTPHeader* header = &rtp_header.header; // Just a shorthand.
170
171 {
Tommi9090e0b2016-01-20 13:39:36 +0100172 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000173
kwibergee2bac22015-11-11 10:34:00 -0800174 const Decoder* decoder = RtpHeaderToDecoder(*header, incoming_payload[0]);
Jelena Marusica9907842015-03-26 14:01:30 +0100175 if (!decoder) {
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000176 LOG_F(LS_ERROR) << "Payload-type "
177 << static_cast<int>(header->payloadType)
178 << " is not registered.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000179 return -1;
180 }
kwibergfb3d8b32015-11-06 01:24:08 -0800181 const int sample_rate_hz = [&decoder] {
182 const auto ci = RentACodec::CodecIdFromIndex(decoder->acm_codec_id);
183 return ci ? RentACodec::CodecInstById(*ci)->plfreq : -1;
184 }();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000185 receive_timestamp = NowInTimestamp(sample_rate_hz);
186
henrik.lundin678c9032015-11-02 08:31:23 -0800187 // If this is a CNG while the audio codec is not mono, skip pushing in
188 // packets into NetEq.
189 if (IsCng(decoder->acm_codec_id) && last_audio_decoder_ &&
190 last_audio_decoder_->channels > 1)
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000191 return 0;
henrik.lundin678c9032015-11-02 08:31:23 -0800192 if (!IsCng(decoder->acm_codec_id) &&
193 decoder->acm_codec_id !=
194 *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
195 last_audio_decoder_ = decoder;
henrik.lundin057fb892015-11-23 08:19:52 -0800196 last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000197 }
198
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000199 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000200
kwibergee2bac22015-11-11 10:34:00 -0800201 if (neteq_->InsertPacket(rtp_header, incoming_payload, receive_timestamp) <
202 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200203 LOG(LERROR) << "AcmReceiver::InsertPacket "
204 << static_cast<int>(header->payloadType)
205 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000206 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000207 }
208 return 0;
209}
210
211int AcmReceiver::GetAudio(int desired_freq_hz, AudioFrame* audio_frame) {
212 enum NetEqOutputType type;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700213 size_t samples_per_channel;
Peter Kasting69558702016-01-12 16:26:35 -0800214 size_t num_channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000215
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000216 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100217 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000218
219 // Always write the output to |audio_buffer_| first.
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000220 if (neteq_->GetAudio(AudioFrame::kMaxDataSizeSamples,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000221 audio_buffer_.get(),
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000222 &samples_per_channel,
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000223 &num_channels,
224 &type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200225 LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000226 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000227 }
228
henrik.lundind89814b2015-11-23 06:49:25 -0800229 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000230
231 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800232 const bool need_resampling =
233 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000234
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000235 if (need_resampling && !resampled_last_output_frame_) {
236 // Prime the resampler with the last frame.
237 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800238 int samples_per_channel_int = resampler_.Resample10Msec(
239 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
240 num_channels, AudioFrame::kMaxDataSizeSamples, temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700241 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200242 LOG(LERROR) << "AcmReceiver::GetAudio - "
243 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000244 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000245 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700246 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000247 }
248
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000249 // The audio in |audio_buffer_| is tansferred to |audio_frame_| below, either
250 // through resampling, or through straight memcpy.
251 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
252 // from NetEq changes. See WebRTC issue 3923.
253 if (need_resampling) {
henrik.lundind89814b2015-11-23 06:49:25 -0800254 int samples_per_channel_int = resampler_.Resample10Msec(
255 audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
256 num_channels, AudioFrame::kMaxDataSizeSamples, audio_frame->data_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700257 if (samples_per_channel_int < 0) {
Tommi92fbbb22015-05-27 22:07:35 +0200258 LOG(LERROR) << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000259 return -1;
260 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700261 samples_per_channel = static_cast<size_t>(samples_per_channel_int);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000262 resampled_last_output_frame_ = true;
263 } else {
264 resampled_last_output_frame_ = false;
265 // We might end up here ONLY if codec is changed.
266 memcpy(audio_frame->data_,
267 audio_buffer_.get(),
268 samples_per_channel * num_channels * sizeof(int16_t));
269 }
270
271 // Swap buffers, so that the current audio is stored in |last_audio_buffer_|
272 // for next time.
273 audio_buffer_.swap(last_audio_buffer_);
274
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000275 audio_frame->num_channels_ = num_channels;
276 audio_frame->samples_per_channel_ = samples_per_channel;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700277 audio_frame->sample_rate_hz_ = static_cast<int>(samples_per_channel * 100);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000278
279 // Should set |vad_activity| before calling SetAudioFrameActivityAndType().
280 audio_frame->vad_activity_ = previous_audio_activity_;
281 SetAudioFrameActivityAndType(vad_enabled_, type, audio_frame);
282 previous_audio_activity_ = audio_frame->vad_activity_;
wu@webrtc.org24301a62013-12-13 19:17:43 +0000283 call_stats_.DecodedByNetEq(audio_frame->speech_type_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000284
285 // Computes the RTP timestamp of the first sample in |audio_frame| from
wu@webrtc.org94454b72014-06-05 20:34:08 +0000286 // |GetPlayoutTimestamp|, which is the timestamp of the last sample of
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000287 // |audio_frame|.
wu@webrtc.org94454b72014-06-05 20:34:08 +0000288 uint32_t playout_timestamp = 0;
289 if (GetPlayoutTimestamp(&playout_timestamp)) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700290 audio_frame->timestamp_ = playout_timestamp -
291 static_cast<uint32_t>(audio_frame->samples_per_channel_);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000292 } else {
293 // Remain 0 until we have a valid |playout_timestamp|.
294 audio_frame->timestamp_ = 0;
295 }
296
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000297 return 0;
298}
299
300int32_t AcmReceiver::AddCodec(int acm_codec_id,
301 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800302 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200303 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800304 AudioDecoder* audio_decoder,
305 const std::string& name) {
kwibergee1879c2015-10-29 06:20:28 -0700306 const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
307 if (acm_codec_id == -1)
308 return NetEqDecoder::kDecoderArbitrary; // External decoder.
Karl Wibergbe579832015-11-10 22:34:18 +0100309 const rtc::Optional<RentACodec::CodecId> cid =
kwibergee1879c2015-10-29 06:20:28 -0700310 RentACodec::CodecIdFromIndex(acm_codec_id);
311 RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
Karl Wibergbe579832015-11-10 22:34:18 +0100312 const rtc::Optional<NetEqDecoder> ned =
kwibergee1879c2015-10-29 06:20:28 -0700313 RentACodec::NetEqDecoderFromCodecId(*cid, channels);
314 RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
315 return *ned;
316 }();
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000317
Tommi9090e0b2016-01-20 13:39:36 +0100318 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000319
320 // The corresponding NetEq decoder ID.
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000321 // If this codec has been registered before.
Jelena Marusica9907842015-03-26 14:01:30 +0100322 auto it = decoders_.find(payload_type);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000323 if (it != decoders_.end()) {
324 const Decoder& decoder = it->second;
kwiberg4e14f092015-08-24 05:27:22 -0700325 if (acm_codec_id != -1 && decoder.acm_codec_id == acm_codec_id &&
326 decoder.channels == channels &&
Karl Wibergd8399e62015-05-25 14:39:56 +0200327 decoder.sample_rate_hz == sample_rate_hz) {
Jelena Marusica9907842015-03-26 14:01:30 +0100328 // Re-registering the same codec. Do nothing and return.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 return 0;
330 }
331
kwiberg4e14f092015-08-24 05:27:22 -0700332 // Changing codec. First unregister the old codec, then register the new
333 // one.
Jelena Marusica9907842015-03-26 14:01:30 +0100334 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200335 LOG(LERROR) << "Cannot remove payload " << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000336 return -1;
337 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000338
339 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000340 }
341
342 int ret_val;
343 if (!audio_decoder) {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800344 ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000345 } else {
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800346 ret_val = neteq_->RegisterExternalDecoder(
347 audio_decoder, neteq_decoder, name, payload_type, sample_rate_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000348 }
349 if (ret_val != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200350 LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
351 << static_cast<int>(payload_type)
352 << " channels: " << channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000353 return -1;
354 }
355
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000356 Decoder decoder;
357 decoder.acm_codec_id = acm_codec_id;
358 decoder.payload_type = payload_type;
359 decoder.channels = channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200360 decoder.sample_rate_hz = sample_rate_hz;
Jelena Marusica9907842015-03-26 14:01:30 +0100361 decoders_[payload_type] = decoder;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000362 return 0;
363}
364
365void AcmReceiver::EnableVad() {
366 neteq_->EnableVad();
Tommi9090e0b2016-01-20 13:39:36 +0100367 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000368 vad_enabled_ = true;
369}
370
371void AcmReceiver::DisableVad() {
372 neteq_->DisableVad();
Tommi9090e0b2016-01-20 13:39:36 +0100373 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000374 vad_enabled_ = false;
375}
376
377void AcmReceiver::FlushBuffers() {
378 neteq_->FlushBuffers();
379}
380
381// If failed in removing one of the codecs, this method continues to remove as
382// many as it can.
383int AcmReceiver::RemoveAllCodecs() {
384 int ret_val = 0;
Tommi9090e0b2016-01-20 13:39:36 +0100385 rtc::CritScope lock(&crit_sect_);
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000386 for (auto it = decoders_.begin(); it != decoders_.end(); ) {
387 auto cur = it;
388 ++it; // it will be valid even if we erase cur
389 if (neteq_->RemovePayloadType(cur->second.payload_type) == 0) {
390 decoders_.erase(cur);
391 } else {
392 LOG_F(LS_ERROR) << "Cannot remove payload "
393 << static_cast<int>(cur->second.payload_type);
394 ret_val = -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000395 }
396 }
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000397
turaj@webrtc.orgd6a7a5f2013-09-25 01:09:23 +0000398 // No codec is registered, invalidate last audio decoder.
Jelena Marusica9907842015-03-26 14:01:30 +0100399 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800400 last_packet_sample_rate_hz_ = rtc::Optional<int>();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000401 return ret_val;
402}
403
404int AcmReceiver::RemoveCodec(uint8_t payload_type) {
Tommi9090e0b2016-01-20 13:39:36 +0100405 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100406 auto it = decoders_.find(payload_type);
407 if (it == decoders_.end()) { // Such a payload-type is not registered.
turaj@webrtc.orga92baea2013-12-13 00:10:44 +0000408 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000409 }
410 if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
Tommi92fbbb22015-05-27 22:07:35 +0200411 LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000412 return -1;
413 }
henrik.lundin057fb892015-11-23 08:19:52 -0800414 if (last_audio_decoder_ == &it->second) {
Jelena Marusica9907842015-03-26 14:01:30 +0100415 last_audio_decoder_ = nullptr;
henrik.lundin057fb892015-11-23 08:19:52 -0800416 last_packet_sample_rate_hz_ = rtc::Optional<int>();
417 }
Jelena Marusica9907842015-03-26 14:01:30 +0100418 decoders_.erase(it);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000419 return 0;
420}
421
422void AcmReceiver::set_id(int id) {
Tommi9090e0b2016-01-20 13:39:36 +0100423 rtc::CritScope lock(&crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000424 id_ = id;
425}
426
wu@webrtc.org94454b72014-06-05 20:34:08 +0000427bool AcmReceiver::GetPlayoutTimestamp(uint32_t* timestamp) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000428 return neteq_->GetPlayoutTimestamp(timestamp);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000429}
430
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000431int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100432 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100433 if (!last_audio_decoder_) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000434 return -1;
435 }
kwiberg4b938e52015-11-03 12:38:27 -0800436 *codec = *RentACodec::CodecInstById(
437 *RentACodec::CodecIdFromIndex(last_audio_decoder_->acm_codec_id));
Jelena Marusica9907842015-03-26 14:01:30 +0100438 codec->pltype = last_audio_decoder_->payload_type;
439 codec->channels = last_audio_decoder_->channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200440 codec->plfreq = last_audio_decoder_->sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000441 return 0;
442}
443
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000444void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000445 NetEqNetworkStatistics neteq_stat;
446 // NetEq function always returns zero, so we don't check the return value.
447 neteq_->NetworkStatistics(&neteq_stat);
448
449 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
450 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000451 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000452 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
453 acm_stat->currentDiscardRate = neteq_stat.packet_discard_rate;
454 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000455 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000456 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
457 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000458 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000459 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000460 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200461 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
462 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
463 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
464 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000465}
466
467int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
468 CodecInst* codec) const {
Tommi9090e0b2016-01-20 13:39:36 +0100469 rtc::CritScope lock(&crit_sect_);
Jelena Marusica9907842015-03-26 14:01:30 +0100470 auto it = decoders_.find(payload_type);
471 if (it == decoders_.end()) {
Tommi92fbbb22015-05-27 22:07:35 +0200472 LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
473 << static_cast<int>(payload_type);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000474 return -1;
475 }
Jelena Marusica9907842015-03-26 14:01:30 +0100476 const Decoder& decoder = it->second;
kwiberg4b938e52015-11-03 12:38:27 -0800477 *codec = *RentACodec::CodecInstById(
478 *RentACodec::CodecIdFromIndex(decoder.acm_codec_id));
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +0000479 codec->pltype = decoder.payload_type;
480 codec->channels = decoder.channels;
Karl Wibergd8399e62015-05-25 14:39:56 +0200481 codec->plfreq = decoder.sample_rate_hz;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000482 return 0;
483}
484
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000485int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700486 neteq_->EnableNack(max_nack_list_size);
487 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000488}
489
490void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700491 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000492}
493
494std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000495 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700496 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000497}
498
499void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000500 neteq_->SetMinimumDelay(0);
501 // TODO(turajs): Should NetEq Buffer be flushed?
502}
503
Jelena Marusica9907842015-03-26 14:01:30 +0100504const AcmReceiver::Decoder* AcmReceiver::RtpHeaderToDecoder(
505 const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -0800506 uint8_t payload_type) const {
Jelena Marusica9907842015-03-26 14:01:30 +0100507 auto it = decoders_.find(rtp_header.payloadType);
kwibergfce4a942015-10-27 11:40:24 -0700508 const auto red_index =
509 RentACodec::CodecIndexFromId(RentACodec::CodecId::kRED);
510 if (red_index && // This ensures that RED is defined in WebRTC.
511 it != decoders_.end() && it->second.acm_codec_id == *red_index) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000512 // This is a RED packet, get the payload of the audio codec.
kwibergee2bac22015-11-11 10:34:00 -0800513 it = decoders_.find(payload_type & 0x7F);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000514 }
515
516 // Check if the payload is registered.
Jelena Marusica9907842015-03-26 14:01:30 +0100517 return it != decoders_.end() ? &it->second : nullptr;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000518}
519
520uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
521 // Down-cast the time to (32-6)-bit since we only care about
522 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
523 // We masked 6 most significant bits of 32-bit so there is no overflow in
524 // the conversion from milliseconds to timestamp.
525 const uint32_t now_in_ms = static_cast<uint32_t>(
henrik.lundin@webrtc.org0c1444c2014-04-22 08:18:42 +0000526 clock_->TimeInMilliseconds() & 0x03ffffff);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000527 return static_cast<uint32_t>(
528 (decoder_sampling_rate / 1000) * now_in_ms);
529}
530
wu@webrtc.org24301a62013-12-13 19:17:43 +0000531void AcmReceiver::GetDecodingCallStatistics(
532 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100533 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000534 *stats = call_stats_.GetDecodingStatistics();
535}
536
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000537} // namespace acm2
538
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000539} // namespace webrtc