blob: ba362780597d2a0d7420ac43663cd274eb8a4549 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/acm2/acm_receiver.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stdlib.h>
14#include <string.h>
15#include <cstdint>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000016#include <vector>
17
Niels Möller2edab4c2018-10-22 09:48:08 +020018#include "absl/strings/match.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio_codecs/audio_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/acm2/acm_resampler.h"
22#include "modules/audio_coding/acm2/call_statistics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/include/neteq.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020024#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010027#include "rtc_base/numerics/safe_conversions.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020028#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/clock.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 Lundin02ed2012017-06-08 09:03:55 +020040 RTC_DCHECK(clock_);
Henrik Lundin76c10672018-05-07 13:47:28 +020041 memset(last_audio_buffer_.get(), 0,
42 sizeof(int16_t) * AudioFrame::kMaxDataSizeSamples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000043}
44
Henrik Lundin6af93992017-06-14 14:13:02 +020045AcmReceiver::~AcmReceiver() = default;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000046
47int AcmReceiver::SetMinimumDelay(int delay_ms) {
48 if (neteq_->SetMinimumDelay(delay_ms))
49 return 0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010050 RTC_LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000051 return -1;
52}
53
turaj@webrtc.org7959e162013-09-12 18:30:26 +000054int AcmReceiver::SetMaximumDelay(int delay_ms) {
55 if (neteq_->SetMaximumDelay(delay_ms))
56 return 0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG(LERROR) << "AcmReceiver::SetExtraDelay " << delay_ms;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000058 return -1;
59}
60
Danil Chapovalovb6021232018-06-19 13:26:36 +020061absl::optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
Tommi9090e0b2016-01-20 13:39:36 +010062 rtc::CritScope lock(&crit_sect_);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010063 if (!last_decoder_) {
64 return absl::nullopt;
65 }
66 return last_decoder_->second.clockrate_hz;
henrik.lundin057fb892015-11-23 08:19:52 -080067}
68
henrik.lundind89814b2015-11-23 06:49:25 -080069int AcmReceiver::last_output_sample_rate_hz() const {
70 return neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +000071}
72
turaj@webrtc.org7959e162013-09-12 18:30:26 +000073int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080074 rtc::ArrayView<const uint8_t> incoming_payload) {
henrik.lundinb8c55b12017-05-10 07:38:01 -070075 if (incoming_payload.empty()) {
76 neteq_->InsertEmptyPacket(rtp_header.header);
77 return 0;
78 }
79
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010080 const RTPHeader& header = rtp_header.header; // Just a shorthand.
81 int payload_type = header.payloadType;
82 auto format = neteq_->GetDecoderFormat(payload_type);
83 if (format && absl::EqualsIgnoreCase(format->name, "red")) {
84 // This is a RED packet. Get the format of the audio codec.
85 payload_type = incoming_payload[0] & 0x7f;
86 format = neteq_->GetDecoderFormat(payload_type);
87 }
88 if (!format) {
89 RTC_LOG_F(LS_ERROR) << "Payload-type "
90 << payload_type
91 << " is not registered.";
92 return -1;
93 }
94
turaj@webrtc.org7959e162013-09-12 18:30:26 +000095 {
Tommi9090e0b2016-01-20 13:39:36 +010096 rtc::CritScope lock(&crit_sect_);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010097 if (absl::EqualsIgnoreCase(format->name, "cn")) {
98 if (last_decoder_ && last_decoder_->second.num_channels > 1) {
kwiberg6f0f6162016-09-20 03:07:46 -070099 // This is a CNG and the audio codec is not mono, so skip pushing in
100 // packets into NetEq.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000101 return 0;
kwiberg6f0f6162016-09-20 03:07:46 -0700102 }
103 } else {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100104 RTC_DCHECK(format);
105 last_decoder_ = std::make_pair(payload_type, *format);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000106 }
henrik.lundin@webrtc.orga90abde2014-06-09 18:35:11 +0000107 } // |crit_sect_| is released.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000108
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100109 uint32_t receive_timestamp = NowInTimestamp(format->clockrate_hz);
110 if (neteq_->InsertPacket(header, incoming_payload, receive_timestamp) < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100111 RTC_LOG(LERROR) << "AcmReceiver::InsertPacket "
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100112 << static_cast<int>(header.payloadType)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 << " Failed to insert packet";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000114 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000115 }
116 return 0;
117}
118
henrik.lundin834a6ea2016-05-13 03:45:24 -0700119int AcmReceiver::GetAudio(int desired_freq_hz,
120 AudioFrame* audio_frame,
121 bool* muted) {
henrik.lundin63489782016-09-20 01:47:12 -0700122 RTC_DCHECK(muted);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000123 // Accessing members, take the lock.
Tommi9090e0b2016-01-20 13:39:36 +0100124 rtc::CritScope lock(&crit_sect_);
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000125
henrik.lundin834a6ea2016-05-13 03:45:24 -0700126 if (neteq_->GetAudio(audio_frame, muted) != NetEq::kOK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100127 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - NetEq Failed.";
henrik.lundin@webrtc.orgeecf5e62014-06-24 13:11:22 +0000128 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000129 }
130
henrik.lundind89814b2015-11-23 06:49:25 -0800131 const int current_sample_rate_hz = neteq_->last_output_sample_rate_hz();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132
133 // Update if resampling is required.
henrik.lundind89814b2015-11-23 06:49:25 -0800134 const bool need_resampling =
135 (desired_freq_hz != -1) && (current_sample_rate_hz != desired_freq_hz);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000136
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000137 if (need_resampling && !resampled_last_output_frame_) {
138 // Prime the resampler with the last frame.
139 int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
henrik.lundind89814b2015-11-23 06:49:25 -0800140 int samples_per_channel_int = resampler_.Resample10Msec(
141 last_audio_buffer_.get(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800142 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
143 temp_output);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100145 RTC_LOG(LERROR) << "AcmReceiver::GetAudio - "
146 "Resampling last_audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000147 return -1;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148 }
149 }
150
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000151 // TODO(henrik.lundin) Glitches in the output may appear if the output rate
152 // from NetEq changes. See WebRTC issue 3923.
153 if (need_resampling) {
yujo36b1a5f2017-06-12 12:45:32 -0700154 // TODO(yujo): handle this more efficiently for muted frames.
henrik.lundind89814b2015-11-23 06:49:25 -0800155 int samples_per_channel_int = resampler_.Resample10Msec(
yujo36b1a5f2017-06-12 12:45:32 -0700156 audio_frame->data(), current_sample_rate_hz, desired_freq_hz,
henrik.lundin6d8e0112016-03-04 10:34:21 -0800157 audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
yujo36b1a5f2017-06-12 12:45:32 -0700158 audio_frame->mutable_data());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 if (samples_per_channel_int < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LERROR)
161 << "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000162 return -1;
163 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800164 audio_frame->samples_per_channel_ =
165 static_cast<size_t>(samples_per_channel_int);
166 audio_frame->sample_rate_hz_ = desired_freq_hz;
167 RTC_DCHECK_EQ(
168 audio_frame->sample_rate_hz_,
kwibergd3edd772017-03-01 18:52:48 -0800169 rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000170 resampled_last_output_frame_ = true;
171 } else {
172 resampled_last_output_frame_ = false;
173 // We might end up here ONLY if codec is changed.
henrik.lundin@webrtc.org913f7b82014-10-21 06:54:23 +0000174 }
175
henrik.lundin6d8e0112016-03-04 10:34:21 -0800176 // Store current audio in |last_audio_buffer_| for next time.
yujo36b1a5f2017-06-12 12:45:32 -0700177 memcpy(last_audio_buffer_.get(), audio_frame->data(),
henrik.lundin6d8e0112016-03-04 10:34:21 -0800178 sizeof(int16_t) * audio_frame->samples_per_channel_ *
179 audio_frame->num_channels_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000180
henrik.lundin63489782016-09-20 01:47:12 -0700181 call_stats_.DecodedByNetEq(audio_frame->speech_type_, *muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000182 return 0;
183}
184
kwiberg1c07c702017-03-27 07:15:49 -0700185void AcmReceiver::SetCodecs(const std::map<int, SdpAudioFormat>& codecs) {
186 neteq_->SetCodecs(codecs);
187}
188
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000189void AcmReceiver::FlushBuffers() {
190 neteq_->FlushBuffers();
191}
192
kwiberg6b19b562016-09-20 04:02:25 -0700193void AcmReceiver::RemoveAllCodecs() {
Tommi9090e0b2016-01-20 13:39:36 +0100194 rtc::CritScope lock(&crit_sect_);
kwiberg6b19b562016-09-20 04:02:25 -0700195 neteq_->RemoveAllPayloadTypes();
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100196 last_decoder_ = absl::nullopt;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000197}
198
Danil Chapovalovb6021232018-06-19 13:26:36 +0200199absl::optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
henrik.lundin9a410dd2016-04-06 01:39:22 -0700200 return neteq_->GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000201}
202
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700203int AcmReceiver::FilteredCurrentDelayMs() const {
204 return neteq_->FilteredCurrentDelayMs();
205}
206
Henrik Lundinabbff892017-11-29 09:14:04 +0100207int AcmReceiver::TargetDelayMs() const {
208 return neteq_->TargetDelayMs();
209}
210
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100211absl::optional<std::pair<int, SdpAudioFormat>>
212 AcmReceiver::LastDecoder() const {
Tommi9090e0b2016-01-20 13:39:36 +0100213 rtc::CritScope lock(&crit_sect_);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100214 if (!last_decoder_) {
215 return absl::nullopt;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000216 }
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100217 RTC_DCHECK_NE(-1, last_decoder_->first); // Payload type should be valid.
218 return last_decoder_;
ossue280cde2016-10-12 11:04:10 -0700219}
220
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000221void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000222 NetEqNetworkStatistics neteq_stat;
223 // NetEq function always returns zero, so we don't check the return value.
224 neteq_->NetworkStatistics(&neteq_stat);
225
226 acm_stat->currentBufferSize = neteq_stat.current_buffer_size_ms;
227 acm_stat->preferredBufferSize = neteq_stat.preferred_buffer_size_ms;
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000228 acm_stat->jitterPeaksFound = neteq_stat.jitter_peaks_found ? true : false;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000229 acm_stat->currentPacketLossRate = neteq_stat.packet_loss_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000230 acm_stat->currentExpandRate = neteq_stat.expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000231 acm_stat->currentSpeechExpandRate = neteq_stat.speech_expand_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000232 acm_stat->currentPreemptiveRate = neteq_stat.preemptive_rate;
233 acm_stat->currentAccelerateRate = neteq_stat.accelerate_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000234 acm_stat->currentSecondaryDecodedRate = neteq_stat.secondary_decoded_rate;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200235 acm_stat->currentSecondaryDiscardedRate = neteq_stat.secondary_discarded_rate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000236 acm_stat->clockDriftPPM = neteq_stat.clockdrift_ppm;
henrik.lundin@webrtc.org20c71fd2014-04-22 10:11:21 +0000237 acm_stat->addedSamples = neteq_stat.added_zero_samples;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200238 acm_stat->meanWaitingTimeMs = neteq_stat.mean_waiting_time_ms;
239 acm_stat->medianWaitingTimeMs = neteq_stat.median_waiting_time_ms;
240 acm_stat->minWaitingTimeMs = neteq_stat.min_waiting_time_ms;
241 acm_stat->maxWaitingTimeMs = neteq_stat.max_waiting_time_ms;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700242
243 NetEqLifetimeStatistics neteq_lifetime_stat = neteq_->GetLifetimeStatistics();
244 acm_stat->totalSamplesReceived = neteq_lifetime_stat.total_samples_received;
245 acm_stat->concealedSamples = neteq_lifetime_stat.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200246 acm_stat->concealmentEvents = neteq_lifetime_stat.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200247 acm_stat->jitterBufferDelayMs = neteq_lifetime_stat.jitter_buffer_delay_ms;
Chen Xing0acffb52019-01-15 15:46:29 +0100248 acm_stat->jitterBufferEmittedCount =
249 neteq_lifetime_stat.jitter_buffer_emitted_count;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100250 acm_stat->delayedPacketOutageSamples =
251 neteq_lifetime_stat.delayed_packet_outage_samples;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100252
253 NetEqOperationsAndState neteq_operations_and_state =
254 neteq_->GetOperationsAndState();
255 acm_stat->packetBufferFlushes =
256 neteq_operations_and_state.packet_buffer_flushes;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000257}
258
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000259int AcmReceiver::EnableNack(size_t max_nack_list_size) {
henrik.lundin48ed9302015-10-29 05:36:24 -0700260 neteq_->EnableNack(max_nack_list_size);
261 return 0;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000262}
263
264void AcmReceiver::DisableNack() {
henrik.lundin48ed9302015-10-29 05:36:24 -0700265 neteq_->DisableNack();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000266}
267
268std::vector<uint16_t> AcmReceiver::GetNackList(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000269 int64_t round_trip_time_ms) const {
henrik.lundin48ed9302015-10-29 05:36:24 -0700270 return neteq_->GetNackList(round_trip_time_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000271}
272
273void AcmReceiver::ResetInitialDelay() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000274 neteq_->SetMinimumDelay(0);
275 // TODO(turajs): Should NetEq Buffer be flushed?
276}
277
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000278uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
279 // Down-cast the time to (32-6)-bit since we only care about
280 // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.
281 // We masked 6 most significant bits of 32-bit so there is no overflow in
282 // the conversion from milliseconds to timestamp.
Yves Gerey665174f2018-06-19 15:03:05 +0200283 const uint32_t now_in_ms =
284 static_cast<uint32_t>(clock_->TimeInMilliseconds() & 0x03ffffff);
285 return static_cast<uint32_t>((decoder_sampling_rate / 1000) * now_in_ms);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000286}
287
wu@webrtc.org24301a62013-12-13 19:17:43 +0000288void AcmReceiver::GetDecodingCallStatistics(
289 AudioDecodingCallStats* stats) const {
Tommi9090e0b2016-01-20 13:39:36 +0100290 rtc::CritScope lock(&crit_sect_);
wu@webrtc.org24301a62013-12-13 19:17:43 +0000291 *stats = call_stats_.GetDecodingStatistics();
292}
293
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000294} // namespace acm2
295
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000296} // namespace webrtc