blob: 2130a246ac71c93003321bed3b9bab64ebde2a8e [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 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/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020023#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
27#include "modules/audio_coding/neteq/tools/audio_loop.h"
Henrik Lundin7687ad52018-07-02 10:14:46 +020028#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
29#include "modules/audio_coding/neteq/tools/neteq_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/ignore_wundef.h"
Joachim Bauch4e909192017-12-19 22:27:51 +010032#include "rtc_base/messagedigest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010033#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/protobuf_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/stringencode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020036#include "rtc_base/strings/string_builder.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020037#include "rtc_base/system/arch.h"
Henrik Lundine9619f82017-11-27 14:05:27 +010038#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "test/gtest.h"
40#include "test/testsupport/fileutils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
Mirko Bonadei81ca3bf2018-01-09 09:40:39 +010042// This must come after test/gtest.h
43#include "rtc_base/flags.h" // NOLINT(build/include)
44
minyue5f026d02015-12-16 07:36:04 -080045#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070046RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
48#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
49#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080051#endif
kwiberg77eab702016-09-28 17:42:01 -070052RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080053#endif
54
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020055WEBRTC_DEFINE_bool(gen_ref, false, "Generate reference files.");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000056
kwiberg5adaf732016-10-04 09:33:27 -070057namespace webrtc {
58
minyue5f026d02015-12-16 07:36:04 -080059namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
minyue4f906772016-04-29 11:05:14 -070061const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020062 const std::string& checksum_android_32,
63 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070064 const std::string& checksum_win_32,
65 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070066#if defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +020067#ifdef WEBRTC_ARCH_64_BITS
68 return checksum_android_64;
69#else
70 return checksum_android_32;
71#endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070072#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020073#ifdef WEBRTC_ARCH_64_BITS
74 return checksum_win_64;
75#else
76 return checksum_win_32;
77#endif // WEBRTC_ARCH_64_BITS
minyue4f906772016-04-29 11:05:14 -070078#else
79 return checksum_general;
80#endif // WEBRTC_WIN
81}
82
minyue5f026d02015-12-16 07:36:04 -080083#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
84void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
85 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
86 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
87 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
88 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
89 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
minyue5f026d02015-12-16 07:36:04 -080090 stats->set_expand_rate(stats_raw.expand_rate);
91 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
92 stats->set_preemptive_rate(stats_raw.preemptive_rate);
93 stats->set_accelerate_rate(stats_raw.accelerate_rate);
94 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020095 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
minyue5f026d02015-12-16 07:36:04 -080096 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
97 stats->set_added_zero_samples(stats_raw.added_zero_samples);
98 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
99 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
100 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
101 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
102}
103
104void Convert(const webrtc::RtcpStatistics& stats_raw,
105 webrtc::neteq_unittest::RtcpStatistics* stats) {
106 stats->set_fraction_lost(stats_raw.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700107 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800108 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700109 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800110 stats->set_jitter(stats_raw.jitter);
111}
112
Yves Gerey665174f2018-06-19 15:03:05 +0200113void AddMessage(FILE* file,
114 rtc::MessageDigest* digest,
minyue4f906772016-04-29 11:05:14 -0700115 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800116 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700117 if (file)
118 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
119 digest->Update(&size, sizeof(size));
120
121 if (file)
122 ASSERT_EQ(static_cast<size_t>(size),
123 fwrite(message.data(), sizeof(char), size, file));
124 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800125}
126
minyue5f026d02015-12-16 07:36:04 -0800127#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
128
henrik.lundin7a926812016-05-12 13:51:28 -0700129void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700130 ASSERT_EQ(true,
131 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
132 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
133 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700134 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
135 "pcma", 8));
136#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700137 ASSERT_EQ(true,
138 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700139#endif
140#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700141 ASSERT_EQ(true,
142 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700143#endif
144#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700145 ASSERT_EQ(true,
146 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700147#endif
148#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700149 ASSERT_EQ(true,
150 neteq->RegisterPayloadType(
151 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700152#endif
kwiberg5adaf732016-10-04 09:33:27 -0700153 ASSERT_EQ(true,
154 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
155 ASSERT_EQ(true,
156 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
157 ASSERT_EQ(true,
158 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
159 ASSERT_EQ(true,
160 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
161 ASSERT_EQ(true,
162 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700163}
minyue5f026d02015-12-16 07:36:04 -0800164} // namespace
165
minyue4f906772016-04-29 11:05:14 -0700166class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 public:
minyue4f906772016-04-29 11:05:14 -0700168 explicit ResultSink(const std::string& output_file);
169 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170
Yves Gerey665174f2018-06-19 15:03:05 +0200171 template <typename T>
172 void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700173
174 void AddResult(const NetEqNetworkStatistics& stats);
175 void AddResult(const RtcpStatistics& stats);
176
177 void VerifyChecksum(const std::string& ref_check_sum);
178
179 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700181 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182};
183
Joachim Bauch4e909192017-12-19 22:27:51 +0100184ResultSink::ResultSink(const std::string& output_file)
minyue4f906772016-04-29 11:05:14 -0700185 : output_fp_(nullptr),
Joachim Bauch4e909192017-12-19 22:27:51 +0100186 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 if (!output_file.empty()) {
188 output_fp_ = fopen(output_file.c_str(), "wb");
189 EXPECT_TRUE(output_fp_ != NULL);
190 }
191}
192
minyue4f906772016-04-29 11:05:14 -0700193ResultSink::~ResultSink() {
194 if (output_fp_)
195 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196}
197
Yves Gerey665174f2018-06-19 15:03:05 +0200198template <typename T>
yujo36b1a5f2017-06-12 12:45:32 -0700199void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700201 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 }
yujo36b1a5f2017-06-12 12:45:32 -0700203 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204}
205
minyue4f906772016-04-29 11:05:14 -0700206void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800207#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800208 neteq_unittest::NetEqNetworkStatistics stats;
209 Convert(stats_raw, &stats);
210
mbonadei7c2c8432017-04-07 00:59:12 -0700211 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800212 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700213 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800214#else
215 FAIL() << "Writing to reference file requires Proto Buffer.";
216#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000217}
218
minyue4f906772016-04-29 11:05:14 -0700219void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800220#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800221 neteq_unittest::RtcpStatistics stats;
222 Convert(stats_raw, &stats);
223
mbonadei7c2c8432017-04-07 00:59:12 -0700224 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800225 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700226 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800227#else
228 FAIL() << "Writing to reference file requires Proto Buffer.";
229#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000230}
231
minyue4f906772016-04-29 11:05:14 -0700232void ResultSink::VerifyChecksum(const std::string& checksum) {
233 std::vector<char> buffer;
234 buffer.resize(digest_->Size());
235 digest_->Finish(&buffer[0], buffer.size());
236 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
Yves Gereya038e712018-11-14 10:45:50 +0100237 if (checksum.size() == result.size()) {
238 EXPECT_EQ(checksum, result);
239 } else {
240 // Check result is one the '|'-separated checksums.
241 EXPECT_NE(checksum.find(result), std::string::npos)
242 << result << " should be one of these:\n"
243 << checksum;
244 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000245}
246
247class NetEqDecodingTest : public ::testing::Test {
248 protected:
249 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
250 // constants below can be changed.
251 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700252 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
253 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
254 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800255 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 static const int kInitSampleRateHz = 8000;
257
258 NetEqDecodingTest();
259 virtual void SetUp();
260 virtual void TearDown();
261 void SelectDecoders(NetEqDecoder* used_codec);
Yves Gerey665174f2018-06-19 15:03:05 +0200262 void OpenInputFile(const std::string& rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800263 void Process();
minyue5f026d02015-12-16 07:36:04 -0800264
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000265 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700266 const std::string& output_checksum,
267 const std::string& network_stats_checksum,
268 const std::string& rtcp_stats_checksum,
269 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800270
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271 static void PopulateRtpInfo(int frame_index,
272 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700273 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 static void PopulateCng(int frame_index,
275 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700276 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000278 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279
Yves Gerey665174f2018-06-19 15:03:05 +0200280 void WrapTest(uint16_t start_seq_no,
281 uint32_t start_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000282 const std::set<uint16_t>& drop_seq_numbers,
Yves Gerey665174f2018-06-19 15:03:05 +0200283 bool expect_seq_no_wrap,
284 bool expect_timestamp_wrap);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000285
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000286 void LongCngWithClockDrift(double drift_factor,
287 double network_freeze_ms,
288 bool pull_audio_during_freeze,
289 int delay_tolerance_ms,
290 int max_time_to_speech_ms);
291
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000292 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000293
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000295 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800296 std::unique_ptr<test::RtpFileSource> rtp_source_;
297 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800299 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000301 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000302};
303
304// Allocating the static const so that it can be passed by reference.
305const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700306const size_t NetEqDecodingTest::kBlockSize8kHz;
307const size_t NetEqDecodingTest::kBlockSize16kHz;
308const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309const int NetEqDecodingTest::kInitSampleRateHz;
310
311NetEqDecodingTest::NetEqDecodingTest()
312 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000313 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000315 output_sample_rate_(kInitSampleRateHz),
316 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000317 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000318}
319
320void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700321 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000322 NetEqNetworkStatistics stat;
323 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
324 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700326 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327}
328
329void NetEqDecodingTest::TearDown() {
330 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331}
332
Yves Gerey665174f2018-06-19 15:03:05 +0200333void NetEqDecodingTest::OpenInputFile(const std::string& rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000334 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335}
336
henrik.lundin6d8e0112016-03-04 10:34:21 -0800337void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000339 while (packet_ && sim_clock_ >= packet_->time_ms()) {
340 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800341#ifndef WEBRTC_CODEC_ISAC
342 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700343 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800344#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200345 ASSERT_EQ(0,
346 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700347 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200348 rtc::ArrayView<const uint8_t>(
349 packet_->payload(), packet_->payload_length_bytes()),
350 static_cast<uint32_t>(packet_->time_ms() *
351 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 }
353 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700354 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 }
356
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000357 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700358 bool muted;
359 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
360 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800361 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
362 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
363 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
364 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
365 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800366 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367
368 // Increase time.
369 sim_clock_ += kTimeStepMs;
370}
371
minyue4f906772016-04-29 11:05:14 -0700372void NetEqDecodingTest::DecodeAndCompare(
373 const std::string& rtp_file,
374 const std::string& output_checksum,
375 const std::string& network_stats_checksum,
376 const std::string& rtcp_stats_checksum,
377 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 OpenInputFile(rtp_file);
379
minyue4f906772016-04-29 11:05:14 -0700380 std::string ref_out_file =
381 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
382 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383
minyue4f906772016-04-29 11:05:14 -0700384 std::string stat_out_file =
385 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
386 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000387
minyue4f906772016-04-29 11:05:14 -0700388 std::string rtcp_out_file =
389 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
390 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000391
henrik.lundin46ba49c2016-05-24 22:50:47 -0700392 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200394 uint64_t last_concealed_samples = 0;
395 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000396 while (packet_) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200397 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
399 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800400 ASSERT_NO_FATAL_FAILURE(Process());
Yves Gerey665174f2018-06-19 15:03:05 +0200401 ASSERT_NO_FATAL_FAILURE(
402 output.AddResult(out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403
404 // Query the network statistics API once per second
405 if (sim_clock_ % 1000 == 0) {
406 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700407 NetEqNetworkStatistics current_network_stats;
408 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
409 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
410
henrik.lundin9c3efd02015-08-27 13:12:22 -0700411 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700412 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
413 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414
Henrik Lundinac0a5032017-09-25 12:22:46 +0200415 // Verify that liftime stats and network stats report similar loss
416 // concealment rates.
417 auto lifetime_stats = neteq_->GetLifetimeStatistics();
418 const uint64_t delta_concealed_samples =
419 lifetime_stats.concealed_samples - last_concealed_samples;
420 last_concealed_samples = lifetime_stats.concealed_samples;
421 const uint64_t delta_total_samples_received =
422 lifetime_stats.total_samples_received - last_total_samples_received;
423 last_total_samples_received = lifetime_stats.total_samples_received;
424 // The tolerance is 1% but expressed in Q14.
425 EXPECT_NEAR(
426 (delta_concealed_samples << 14) / delta_total_samples_received,
427 current_network_stats.expand_rate, (2 << 14) / 100.0);
428
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000429 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700430 RtcpStatistics current_rtcp_stats;
431 neteq_->GetRtcpStatistics(&current_rtcp_stats);
432 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000433 }
434 }
minyue4f906772016-04-29 11:05:14 -0700435
436 SCOPED_TRACE("Check output audio.");
437 output.VerifyChecksum(output_checksum);
438 SCOPED_TRACE("Check network stats.");
439 network_stats.VerifyChecksum(network_stats_checksum);
440 SCOPED_TRACE("Check rtcp stats.");
441 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442}
443
444void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
445 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700446 RTPHeader* rtp_info) {
447 rtp_info->sequenceNumber = frame_index;
448 rtp_info->timestamp = timestamp;
449 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
450 rtp_info->payloadType = 94; // PCM16b WB codec.
451 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000452}
453
454void NetEqDecodingTest::PopulateCng(int frame_index,
455 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700456 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000457 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000458 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700459 rtp_info->sequenceNumber = frame_index;
460 rtp_info->timestamp = timestamp;
461 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
462 rtp_info->payloadType = 98; // WB CNG.
463 rtp_info->markerBit = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200464 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000465 *payload_len = 1; // Only noise level, no spectral parameters.
466}
467
ivoc72c08ed2016-01-20 07:26:24 -0800468#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
469 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +0100470 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800471#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700472#else
minyue5f026d02015-12-16 07:36:04 -0800473#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700474#endif
minyue5f026d02015-12-16 07:36:04 -0800475TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800476 const std::string input_rtp_file =
477 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000478
Yves Gerey665174f2018-06-19 15:03:05 +0200479 const std::string output_checksum =
480 PlatformChecksum("0c6dc227f781c81a229970f8fceda1a012498cba",
481 "15c4a2202877a414515e218bdb7992f0ad53e5af", "not used",
482 "0c6dc227f781c81a229970f8fceda1a012498cba",
483 "25fc4c863caa499aa447a5b8d059f5452cbcc500");
minyue4f906772016-04-29 11:05:14 -0700484
henrik.lundin2979f552017-05-05 05:04:16 -0700485 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200486 PlatformChecksum("4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
Yves Gerey665174f2018-06-19 15:03:05 +0200487 "e339cb2adf5ab3dfc21cb7205d670a34751e8336", "not used",
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200488 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
489 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9");
minyue4f906772016-04-29 11:05:14 -0700490
Yves Gerey665174f2018-06-19 15:03:05 +0200491 const std::string rtcp_stats_checksum =
492 PlatformChecksum("b8880bf9fed2487efbddcb8d94b9937a29ae521d",
493 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4", "not used",
494 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
495 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
minyue4f906772016-04-29 11:05:14 -0700496
Yves Gerey665174f2018-06-19 15:03:05 +0200497 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
498 rtcp_stats_checksum, FLAG_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000499}
500
Yves Gerey665174f2018-06-19 15:03:05 +0200501#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200502 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800503#define MAYBE_TestOpusBitExactness TestOpusBitExactness
504#else
505#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
506#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200507TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800508 const std::string input_rtp_file =
509 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800510
Yves Gereya038e712018-11-14 10:45:50 +0100511 // Checksum depends on libopus being compiled with or without SSE.
512 const std::string maybe_sse =
513 "14a63b3c7b925c82296be4bafc71bec85f2915c2|"
514 "2c05677daa968d6c68b92adf4affb7cd9bb4d363";
515 const std::string output_checksum = PlatformChecksum(
516 maybe_sse, "b7b7ed802b0e18ee416973bf3b9ae98599b0181d",
517 "5876e52dda90d5ca433c3726555b907b97c86374", maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700518
henrik.lundin2979f552017-05-05 05:04:16 -0700519 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200520 PlatformChecksum("adb3272498e436d1c019cbfd71610e9510c54497",
521 "fa935a91abc7291db47428a2d7c5361b98713a92",
522 "42106aa5267300f709f63737707ef07afd9dac61",
523 "adb3272498e436d1c019cbfd71610e9510c54497",
524 "adb3272498e436d1c019cbfd71610e9510c54497");
minyue4f906772016-04-29 11:05:14 -0700525
Yves Gerey665174f2018-06-19 15:03:05 +0200526 const std::string rtcp_stats_checksum =
527 PlatformChecksum("e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
528 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
529 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
530 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
531 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
minyue4f906772016-04-29 11:05:14 -0700532
Yves Gerey665174f2018-06-19 15:03:05 +0200533 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
534 rtcp_stats_checksum, FLAG_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800535}
536
Yves Gerey665174f2018-06-19 15:03:05 +0200537#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100538 defined(WEBRTC_CODEC_OPUS)
539#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
540#else
541#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
542#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100543TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100544 const std::string input_rtp_file =
545 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
546
Yves Gereya038e712018-11-14 10:45:50 +0100547 const std::string maybe_sse =
548 "713af6c92881f5aab1285765ee6680da9d1c06ce|"
549 "2ac10c4e79aeedd0df2863b079da5848b40f00b5";
550 const std::string output_checksum = PlatformChecksum(
551 maybe_sse, "3ec991b96872123f1554c03c543ca5d518431e46",
552 "da9f9a2d94e0c2d67342fad4965d7b91cda50b25", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100553
554 const std::string network_stats_checksum =
555 "bab58dc587d956f326056d7340c96eb9d2d3cc21";
556
557 const std::string rtcp_stats_checksum =
558 "ac27a7f305efb58b39bf123dccee25dee5758e63";
559
560 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
561 rtcp_stats_checksum, FLAG_gen_ref);
562}
563
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000564// Use fax mode to avoid time-scaling. This is to simplify the testing of
565// packet waiting times in the packet buffer.
566class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
567 protected:
568 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200569 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000570 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200571 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000572};
573
574TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000575 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
576 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000577 const size_t kSamples = 10 * 16;
578 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800580 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700581 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200582 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
583 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700584 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
585 rtp_info.payloadType = 94; // PCM16b WB codec.
586 rtp_info.markerBit = 0;
587 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000588 }
589 // Pull out all data.
590 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700591 bool muted;
592 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800593 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000594 }
595
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200596 NetEqNetworkStatistics stats;
597 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
599 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200600 // each packet. Thus, we are calculating the statistics for a series from 10
601 // to 300, in steps of 10 ms.
602 EXPECT_EQ(155, stats.mean_waiting_time_ms);
603 EXPECT_EQ(155, stats.median_waiting_time_ms);
604 EXPECT_EQ(10, stats.min_waiting_time_ms);
605 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606
607 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200608 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
609 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
610 EXPECT_EQ(-1, stats.median_waiting_time_ms);
611 EXPECT_EQ(-1, stats.min_waiting_time_ms);
612 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000613}
614
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000615TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 const int kNumFrames = 3000; // Needed for convergence.
617 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000618 const size_t kSamples = 10 * 16;
619 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000620 while (frame_index < kNumFrames) {
621 // Insert one packet each time, except every 10th time where we insert two
622 // packets at once. This will create a negative clock-drift of approx. 10%.
623 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
624 for (int n = 0; n < num_packets; ++n) {
625 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700626 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000627 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700628 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629 ++frame_index;
630 }
631
632 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700633 bool muted;
634 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800635 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 }
637
638 NetEqNetworkStatistics network_stats;
639 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700640 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000641}
642
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000643TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000644 const int kNumFrames = 5000; // Needed for convergence.
645 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000646 const size_t kSamples = 10 * 16;
647 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000648 for (int i = 0; i < kNumFrames; ++i) {
649 // Insert one packet each time, except every 10th time where we don't insert
650 // any packet. This will create a positive clock-drift of approx. 11%.
651 int num_packets = (i % 10 == 9 ? 0 : 1);
652 for (int n = 0; n < num_packets; ++n) {
653 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700654 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700656 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 ++frame_index;
658 }
659
660 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700661 bool muted;
662 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800663 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 }
665
666 NetEqNetworkStatistics network_stats;
667 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700668 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000669}
670
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000671void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
672 double network_freeze_ms,
673 bool pull_audio_during_freeze,
674 int delay_tolerance_ms,
675 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000676 uint16_t seq_no = 0;
677 uint32_t timestamp = 0;
678 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000679 const size_t kSamples = kFrameSizeMs * 16;
680 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000681 double next_input_time_ms = 0.0;
682 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700683 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000684
685 // Insert speech for 5 seconds.
686 const int kSpeechDurationMs = 5000;
687 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
688 // Each turn in this for loop is 10 ms.
689 while (next_input_time_ms <= t_ms) {
690 // Insert one 30 ms speech frame.
691 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700692 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700694 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000695 ++seq_no;
696 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000697 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000698 }
699 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700700 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800701 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000702 }
703
henrik.lundin55480f52016-03-08 02:37:57 -0800704 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200705 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700706 ASSERT_TRUE(playout_timestamp);
707 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000708
709 // Insert CNG for 1 minute (= 60000 ms).
710 const int kCngPeriodMs = 100;
711 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
712 const int kCngDurationMs = 60000;
713 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
714 // Each turn in this for loop is 10 ms.
715 while (next_input_time_ms <= t_ms) {
716 // Insert one CNG frame each 100 ms.
717 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000718 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700719 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800721 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700722 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800723 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724 ++seq_no;
725 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000726 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000727 }
728 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700729 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800730 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 }
732
henrik.lundin55480f52016-03-08 02:37:57 -0800733 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000735 if (network_freeze_ms > 0) {
736 // First keep pulling audio for |network_freeze_ms| without inserting
737 // any data, then insert CNG data corresponding to |network_freeze_ms|
738 // without pulling any output audio.
739 const double loop_end_time = t_ms + network_freeze_ms;
740 for (; t_ms < loop_end_time; t_ms += 10) {
741 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700742 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800743 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800744 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000745 }
746 bool pull_once = pull_audio_during_freeze;
747 // If |pull_once| is true, GetAudio will be called once half-way through
748 // the network recovery period.
749 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
750 while (next_input_time_ms <= t_ms) {
751 if (pull_once && next_input_time_ms >= pull_time_ms) {
752 pull_once = false;
753 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700754 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800755 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800756 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000757 t_ms += 10;
758 }
759 // Insert one CNG frame each 100 ms.
760 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000761 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700762 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000763 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800764 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700765 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800766 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 ++seq_no;
768 timestamp += kCngPeriodSamples;
769 next_input_time_ms += kCngPeriodMs * drift_factor;
770 }
771 }
772
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000773 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000774 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800775 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000776 // Each turn in this for loop is 10 ms.
777 while (next_input_time_ms <= t_ms) {
778 // Insert one 30 ms speech frame.
779 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700780 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000781 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700782 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000783 ++seq_no;
784 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000785 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000786 }
787 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700788 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800789 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000790 // Increase clock.
791 t_ms += 10;
792 }
793
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000794 // Check that the speech starts again within reasonable time.
795 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
796 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700797 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700798 ASSERT_TRUE(playout_timestamp);
799 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000800 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
802 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000803}
804
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000805TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000806 // Apply a clock drift of -25 ms / s (sender faster than receiver).
807 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000808 const double kNetworkFreezeTimeMs = 0.0;
809 const bool kGetAudioDuringFreezeRecovery = false;
810 const int kDelayToleranceMs = 20;
811 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200812 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
813 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000814 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000815}
816
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000817TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000818 // Apply a clock drift of +25 ms / s (sender slower than receiver).
819 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000820 const double kNetworkFreezeTimeMs = 0.0;
821 const bool kGetAudioDuringFreezeRecovery = false;
822 const int kDelayToleranceMs = 20;
823 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200824 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
825 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000826 kMaxTimeToSpeechMs);
827}
828
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000829TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000830 // Apply a clock drift of -25 ms / s (sender faster than receiver).
831 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
832 const double kNetworkFreezeTimeMs = 5000.0;
833 const bool kGetAudioDuringFreezeRecovery = false;
834 const int kDelayToleranceMs = 50;
835 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200836 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
837 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000838 kMaxTimeToSpeechMs);
839}
840
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000841TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000842 // Apply a clock drift of +25 ms / s (sender slower than receiver).
843 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
844 const double kNetworkFreezeTimeMs = 5000.0;
845 const bool kGetAudioDuringFreezeRecovery = false;
846 const int kDelayToleranceMs = 20;
847 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200848 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
849 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000850 kMaxTimeToSpeechMs);
851}
852
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000853TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000854 // Apply a clock drift of +25 ms / s (sender slower than receiver).
855 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
856 const double kNetworkFreezeTimeMs = 5000.0;
857 const bool kGetAudioDuringFreezeRecovery = true;
858 const int kDelayToleranceMs = 20;
859 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200860 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
861 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000862 kMaxTimeToSpeechMs);
863}
864
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000865TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000866 const double kDriftFactor = 1.0; // No drift.
867 const double kNetworkFreezeTimeMs = 0.0;
868 const bool kGetAudioDuringFreezeRecovery = false;
869 const int kDelayToleranceMs = 10;
870 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200871 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
872 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000873 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000874}
875
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000876TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000877 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700879 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700881 rtp_info.payloadType = 1; // Not registered as a decoder.
882 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000883}
884
Peter Boströme2976c82016-01-04 22:44:05 +0100885#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800886#define MAYBE_DecoderError DecoderError
887#else
888#define MAYBE_DecoderError DISABLED_DecoderError
889#endif
890
Peter Boströme2976c82016-01-04 22:44:05 +0100891TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000892 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000893 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700894 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000895 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700896 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
897 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
899 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700900 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800901 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700902 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000903 }
henrik.lundin7a926812016-05-12 13:51:28 -0700904 bool muted;
905 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
906 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800907
yujo36b1a5f2017-06-12 12:45:32 -0700908 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700910 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000911 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200912 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000913 ss << "i = " << i;
914 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700915 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 }
917}
918
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000919TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
921 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700922 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800923 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700924 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000925 }
henrik.lundin7a926812016-05-12 13:51:28 -0700926 bool muted;
927 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
928 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000929 // Verify that the first block of samples is set to 0.
930 static const int kExpectedOutputLength =
931 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700932 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000933 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200934 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000935 ss << "i = " << i;
936 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700937 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000938 }
henrik.lundind89814b2015-11-23 06:49:25 -0800939 // Verify that the sample rate did not change from the initial configuration.
940 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000941}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000942
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000943class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000944 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000945 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700946 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000947 uint8_t payload_type = 0xFF; // Invalid.
948 if (sampling_rate_hz == 8000) {
949 expected_samples_per_channel = kBlockSize8kHz;
950 payload_type = 93; // PCM 16, 8 kHz.
951 } else if (sampling_rate_hz == 16000) {
952 expected_samples_per_channel = kBlockSize16kHz;
953 payload_type = 94; // PCM 16, 16 kHZ.
954 } else if (sampling_rate_hz == 32000) {
955 expected_samples_per_channel = kBlockSize32kHz;
956 payload_type = 95; // PCM 16, 32 kHz.
957 } else {
958 ASSERT_TRUE(false); // Unsupported test case.
959 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000960
henrik.lundin6d8e0112016-03-04 10:34:21 -0800961 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000962 test::AudioLoop input;
963 // We are using the same 32 kHz input file for all tests, regardless of
964 // |sampling_rate_hz|. The output may sound weird, but the test is still
965 // valid.
966 ASSERT_TRUE(input.Init(
967 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
968 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700969 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000970
971 // Payload of 10 ms of PCM16 32 kHz.
972 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700973 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000974 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700975 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000976
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000977 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700978 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000979 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800980 auto block = input.GetNextBlock();
981 ASSERT_EQ(expected_samples_per_channel, block.size());
982 size_t enc_len_bytes =
983 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000984 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
985
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200986 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700987 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200988 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
989 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800990 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700991 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800992 ASSERT_EQ(1u, output.num_channels_);
993 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800994 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000995
996 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200997 rtp_info.timestamp +=
998 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700999 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +02001000 receive_timestamp +=
1001 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001002 }
1003
henrik.lundin6d8e0112016-03-04 10:34:21 -08001004 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001005
1006 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1007 // one frame without checking speech-type. This is the first frame pulled
1008 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -07001009 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001010 ASSERT_EQ(1u, output.num_channels_);
1011 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001012
1013 // To be able to test the fading of background noise we need at lease to
1014 // pull 611 frames.
1015 const int kFadingThreshold = 611;
1016
1017 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1018 // is arbitrary, but sufficiently large to test enough number of frames.
1019 const int kNumPlcToCngTestFrames = 20;
1020 bool plc_to_cng = false;
1021 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001022 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -07001023 // Set to non-zero.
1024 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -07001025 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1026 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001027 ASSERT_EQ(1u, output.num_channels_);
1028 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001029 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001030 plc_to_cng = true;
1031 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001032 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001033 for (size_t k = 0;
1034 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001035 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +02001036 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001037 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001038 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001039 }
1040 }
1041 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1042 }
1043};
1044
Henrik Lundin67190172018-04-20 15:34:48 +02001045TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001046 CheckBgn(8000);
1047 CheckBgn(16000);
1048 CheckBgn(32000);
1049}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001050
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001051void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1052 uint32_t start_timestamp,
1053 const std::set<uint16_t>& drop_seq_numbers,
1054 bool expect_seq_no_wrap,
1055 bool expect_timestamp_wrap) {
1056 uint16_t seq_no = start_seq_no;
1057 uint32_t timestamp = start_timestamp;
1058 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1059 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1060 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001061 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001062 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001063 uint32_t receive_timestamp = 0;
1064
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001065 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001066 const int kSpeechDurationMs = 2000;
1067 int packets_inserted = 0;
1068 uint16_t last_seq_no;
1069 uint32_t last_timestamp;
1070 bool timestamp_wrapped = false;
1071 bool seq_no_wrapped = false;
1072 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1073 // Each turn in this for loop is 10 ms.
1074 while (next_input_time_ms <= t_ms) {
1075 // Insert one 30 ms speech frame.
1076 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001077 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001078 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1079 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1080 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001081 ASSERT_EQ(0,
1082 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001083 ++packets_inserted;
1084 }
1085 NetEqNetworkStatistics network_stats;
1086 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1087
1088 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1089 // packet size for first few packets. Therefore we refrain from checking
1090 // the criteria.
1091 if (packets_inserted > 4) {
1092 // Expect preferred and actual buffer size to be no more than 2 frames.
1093 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
Yves Gerey665174f2018-06-19 15:03:05 +02001094 EXPECT_LE(network_stats.current_buffer_size_ms,
1095 kFrameSizeMs * 2 + algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001096 }
1097 last_seq_no = seq_no;
1098 last_timestamp = timestamp;
1099
1100 ++seq_no;
1101 timestamp += kSamples;
1102 receive_timestamp += kSamples;
1103 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1104
1105 seq_no_wrapped |= seq_no < last_seq_no;
1106 timestamp_wrapped |= timestamp < last_timestamp;
1107 }
1108 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001109 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001110 bool muted;
1111 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001112 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1113 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001114
1115 // Expect delay (in samples) to be less than 2 packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +02001116 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001117 ASSERT_TRUE(playout_timestamp);
1118 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001119 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001120 }
1121 // Make sure we have actually tested wrap-around.
1122 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1123 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1124}
1125
1126TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1127 // Start with a sequence number that will soon wrap.
1128 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1129 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1130}
1131
1132TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1133 // Start with a sequence number that will soon wrap.
1134 std::set<uint16_t> drop_seq_numbers;
1135 drop_seq_numbers.insert(0xFFFF);
1136 drop_seq_numbers.insert(0x0);
1137 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1138}
1139
1140TEST_F(NetEqDecodingTest, TimestampWrap) {
1141 // Start with a timestamp that will soon wrap.
1142 std::set<uint16_t> drop_seq_numbers;
1143 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1144}
1145
1146TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1147 // Start with a timestamp and a sequence number that will wrap at the same
1148 // time.
1149 std::set<uint16_t> drop_seq_numbers;
1150 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1151}
1152
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001153void NetEqDecodingTest::DuplicateCng() {
1154 uint16_t seq_no = 0;
1155 uint32_t timestamp = 0;
1156 const int kFrameSizeMs = 10;
1157 const int kSampleRateKhz = 16;
1158 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001159 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001160
Yves Gerey665174f2018-06-19 15:03:05 +02001161 const int algorithmic_delay_samples =
1162 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001163 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001164 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001165 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001166 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001167 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001168 for (int i = 0; i < 3; ++i) {
1169 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001170 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001171 ++seq_no;
1172 timestamp += kSamples;
1173
1174 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001175 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001176 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001177 }
1178 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001179 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001180
1181 // Insert same CNG packet twice.
1182 const int kCngPeriodMs = 100;
1183 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001184 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001185 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1186 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001187 ASSERT_EQ(
1188 0, neteq_->InsertPacket(
1189 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001190
1191 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001192 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001193 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001194 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001195 EXPECT_FALSE(
1196 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001197 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1198 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001199
1200 // Insert the same CNG packet again. Note that at this point it is old, since
1201 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001202 ASSERT_EQ(
1203 0, neteq_->InsertPacket(
1204 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001205
1206 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1207 // we have already pulled out CNG once.
1208 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001209 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001210 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001211 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001212 EXPECT_FALSE(
1213 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001214 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001215 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001216 }
1217
1218 // Insert speech again.
1219 ++seq_no;
1220 timestamp += kCngPeriodSamples;
1221 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001223
1224 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001225 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001226 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001227 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +02001228 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001229 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001230 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001231 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001232}
1233
Yves Gerey665174f2018-06-19 15:03:05 +02001234TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
1235 DuplicateCng();
1236}
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001237
1238TEST_F(NetEqDecodingTest, CngFirst) {
1239 uint16_t seq_no = 0;
1240 uint32_t timestamp = 0;
1241 const int kFrameSizeMs = 10;
1242 const int kSampleRateKhz = 16;
1243 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1244 const int kPayloadBytes = kSamples * 2;
1245 const int kCngPeriodMs = 100;
1246 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1247 size_t payload_len;
1248
1249 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001250 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001251
1252 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001253 ASSERT_EQ(
1254 NetEq::kOK,
1255 neteq_->InsertPacket(
1256 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001257 ++seq_no;
1258 timestamp += kCngPeriodSamples;
1259
1260 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001261 bool muted;
1262 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001263 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001264 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001265
1266 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001267 const uint32_t first_speech_timestamp = timestamp;
1268 int timeout_counter = 0;
1269 do {
1270 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001271 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001272 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001273 ++seq_no;
1274 timestamp += kSamples;
1275
1276 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001277 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001278 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001279 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001280 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001281 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001282}
henrik.lundin7a926812016-05-12 13:51:28 -07001283
1284class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1285 public:
1286 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1287 config_.enable_muted_state = true;
1288 }
1289
1290 protected:
1291 static constexpr size_t kSamples = 10 * 16;
1292 static constexpr size_t kPayloadBytes = kSamples * 2;
1293
1294 void InsertPacket(uint32_t rtp_timestamp) {
1295 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001296 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001297 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001298 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001299 }
1300
henrik.lundin42feb512016-09-20 06:51:40 -07001301 void InsertCngPacket(uint32_t rtp_timestamp) {
1302 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001303 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001304 size_t payload_len;
1305 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001306 EXPECT_EQ(
1307 NetEq::kOK,
1308 neteq_->InsertPacket(
1309 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001310 }
1311
henrik.lundin7a926812016-05-12 13:51:28 -07001312 bool GetAudioReturnMuted() {
1313 bool muted;
1314 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1315 return muted;
1316 }
1317
1318 void GetAudioUntilMuted() {
1319 while (!GetAudioReturnMuted()) {
1320 ASSERT_LT(counter_++, 1000) << "Test timed out";
1321 }
1322 }
1323
1324 void GetAudioUntilNormal() {
1325 bool muted = false;
1326 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1327 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1328 ASSERT_LT(counter_++, 1000) << "Test timed out";
1329 }
1330 EXPECT_FALSE(muted);
1331 }
1332
1333 int counter_ = 0;
1334};
1335
1336// Verifies that NetEq goes in and out of muted state as expected.
1337TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1338 // Insert one speech packet.
1339 InsertPacket(0);
1340 // Pull out audio once and expect it not to be muted.
1341 EXPECT_FALSE(GetAudioReturnMuted());
1342 // Pull data until faded out.
1343 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001344 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001345
1346 // Verify that output audio is not written during muted mode. Other parameters
1347 // should be correct, though.
1348 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001349 int16_t* frame_data = new_frame.mutable_data();
1350 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1351 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001352 }
1353 bool muted;
1354 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1355 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001356 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001357 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1358 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001359 }
1360 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1361 new_frame.timestamp_);
1362 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1363 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1364 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1365 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1366 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1367
1368 // Insert new data. Timestamp is corrected for the time elapsed since the last
1369 // packet. Verify that normal operation resumes.
1370 InsertPacket(kSamples * counter_);
1371 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001372 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001373
1374 NetEqNetworkStatistics stats;
1375 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1376 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1377 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1378 // concealment samples in this test.
1379 EXPECT_GT(stats.expand_rate, 14000);
1380 // And, it should be greater than the speech_expand_rate.
1381 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001382}
1383
1384// Verifies that NetEq goes out of muted state when given a delayed packet.
1385TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1386 // Insert one speech packet.
1387 InsertPacket(0);
1388 // Pull out audio once and expect it not to be muted.
1389 EXPECT_FALSE(GetAudioReturnMuted());
1390 // Pull data until faded out.
1391 GetAudioUntilMuted();
1392 // Insert new data. Timestamp is only corrected for the half of the time
1393 // elapsed since the last packet. That is, the new packet is delayed. Verify
1394 // that normal operation resumes.
1395 InsertPacket(kSamples * counter_ / 2);
1396 GetAudioUntilNormal();
1397}
1398
1399// Verifies that NetEq goes out of muted state when given a future packet.
1400TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1401 // Insert one speech packet.
1402 InsertPacket(0);
1403 // Pull out audio once and expect it not to be muted.
1404 EXPECT_FALSE(GetAudioReturnMuted());
1405 // Pull data until faded out.
1406 GetAudioUntilMuted();
1407 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1408 // last packet. That is, the new packet is too early. Verify that normal
1409 // operation resumes.
1410 InsertPacket(kSamples * counter_ * 2);
1411 GetAudioUntilNormal();
1412}
1413
1414// Verifies that NetEq goes out of muted state when given an old packet.
1415TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1416 // Insert one speech packet.
1417 InsertPacket(0);
1418 // Pull out audio once and expect it not to be muted.
1419 EXPECT_FALSE(GetAudioReturnMuted());
1420 // Pull data until faded out.
1421 GetAudioUntilMuted();
1422
1423 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1424 // Insert packet which is older than the first packet.
1425 InsertPacket(kSamples * (counter_ - 1000));
1426 EXPECT_FALSE(GetAudioReturnMuted());
1427 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1428}
1429
henrik.lundin42feb512016-09-20 06:51:40 -07001430// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1431// packet stream is suspended for a long time.
1432TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1433 // Insert one CNG packet.
1434 InsertCngPacket(0);
1435
1436 // Pull 10 seconds of audio (10 ms audio generated per lap).
1437 for (int i = 0; i < 1000; ++i) {
1438 bool muted;
1439 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1440 ASSERT_FALSE(muted);
1441 }
1442 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1443}
1444
1445// Verifies that NetEq goes back to normal after a long CNG period with the
1446// packet stream suspended.
1447TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1448 // Insert one CNG packet.
1449 InsertCngPacket(0);
1450
1451 // Pull 10 seconds of audio (10 ms audio generated per lap).
1452 for (int i = 0; i < 1000; ++i) {
1453 bool muted;
1454 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1455 }
1456
1457 // Insert new data. Timestamp is corrected for the time elapsed since the last
1458 // packet. Verify that normal operation resumes.
1459 InsertPacket(kSamples * counter_);
1460 GetAudioUntilNormal();
1461}
1462
henrik.lundin7a926812016-05-12 13:51:28 -07001463class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1464 public:
1465 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1466
1467 void SetUp() override {
1468 NetEqDecodingTest::SetUp();
1469 config2_ = config_;
1470 }
1471
1472 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001473 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001474 ASSERT_TRUE(neteq2_);
1475 LoadDecoders(neteq2_.get());
1476 }
1477
1478 protected:
1479 std::unique_ptr<NetEq> neteq2_;
1480 NetEq::Config config2_;
1481};
1482
1483namespace {
1484::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1485 const AudioFrame& b) {
1486 if (a.timestamp_ != b.timestamp_)
1487 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1488 << " != " << b.timestamp_ << ")";
1489 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +02001490 return ::testing::AssertionFailure()
1491 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
1492 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001493 if (a.samples_per_channel_ != b.samples_per_channel_)
1494 return ::testing::AssertionFailure()
1495 << "samples_per_channel_ diff (" << a.samples_per_channel_
1496 << " != " << b.samples_per_channel_ << ")";
1497 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +02001498 return ::testing::AssertionFailure()
1499 << "num_channels_ diff (" << a.num_channels_
1500 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001501 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +02001502 return ::testing::AssertionFailure()
1503 << "speech_type_ diff (" << a.speech_type_
1504 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001505 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +02001506 return ::testing::AssertionFailure()
1507 << "vad_activity_ diff (" << a.vad_activity_
1508 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001509 return ::testing::AssertionSuccess();
1510}
1511
1512::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1513 const AudioFrame& b) {
1514 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1515 if (!res)
1516 return res;
Yves Gerey665174f2018-06-19 15:03:05 +02001517 if (memcmp(a.data(), b.data(),
1518 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
1519 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001520 return ::testing::AssertionFailure() << "data_ diff";
1521 }
1522 return ::testing::AssertionSuccess();
1523}
1524
1525} // namespace
1526
1527TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1528 ASSERT_FALSE(config_.enable_muted_state);
1529 config2_.enable_muted_state = true;
1530 CreateSecondInstance();
1531
1532 // Insert one speech packet into both NetEqs.
1533 const size_t kSamples = 10 * 16;
1534 const size_t kPayloadBytes = kSamples * 2;
1535 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001536 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001537 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001538 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1539 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001540
1541 AudioFrame out_frame1, out_frame2;
1542 bool muted;
1543 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001544 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001545 ss << "i = " << i;
1546 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1547 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1548 EXPECT_FALSE(muted);
1549 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1550 if (muted) {
1551 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1552 } else {
1553 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1554 }
1555 }
1556 EXPECT_TRUE(muted);
1557
1558 // Insert new data. Timestamp is corrected for the time elapsed since the last
1559 // packet.
1560 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001561 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1562 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001563
1564 int counter = 0;
1565 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1566 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +02001567 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001568 ss << "counter = " << counter;
1569 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1570 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1571 EXPECT_FALSE(muted);
1572 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1573 if (muted) {
1574 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1575 } else {
1576 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1577 }
1578 }
1579 EXPECT_FALSE(muted);
1580}
1581
henrik.lundin114c1b32017-04-26 07:47:32 -07001582TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1583 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1584
1585 // Pull out data once.
1586 AudioFrame output;
1587 bool muted;
1588 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1589
1590 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1591}
1592
1593TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1594 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1595 // default). Make the length 10 ms.
1596 constexpr size_t kPayloadSamples = 16 * 10;
1597 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1598 uint8_t payload[kPayloadBytes] = {0};
1599
1600 RTPHeader rtp_info;
1601 constexpr uint32_t kRtpTimestamp = 0x1234;
1602 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1603 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1604
1605 // Pull out data once.
1606 AudioFrame output;
1607 bool muted;
1608 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1609
1610 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1611 neteq_->LastDecodedTimestamps());
1612
1613 // Nothing decoded on the second call.
1614 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1615 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1616}
1617
1618TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1619 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1620 // by default). Make the length 5 ms so that NetEq must decode them both in
1621 // the same GetAudio call.
1622 constexpr size_t kPayloadSamples = 16 * 5;
1623 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1624 uint8_t payload[kPayloadBytes] = {0};
1625
1626 RTPHeader rtp_info;
1627 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1628 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1629 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1630 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1631 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1632 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1633
1634 // Pull out data once.
1635 AudioFrame output;
1636 bool muted;
1637 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1638
1639 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1640 neteq_->LastDecodedTimestamps());
1641}
1642
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001643TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1644 const int kNumConcealmentEvents = 19;
1645 const size_t kSamples = 10 * 16;
1646 const size_t kPayloadBytes = kSamples * 2;
1647 int seq_no = 0;
1648 RTPHeader rtp_info;
1649 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1650 rtp_info.payloadType = 94; // PCM16b WB codec.
1651 rtp_info.markerBit = 0;
1652 const uint8_t payload[kPayloadBytes] = {0};
1653 bool muted;
1654
1655 for (int i = 0; i < kNumConcealmentEvents; i++) {
1656 // Insert some packets of 10 ms size.
1657 for (int j = 0; j < 10; j++) {
1658 rtp_info.sequenceNumber = seq_no++;
1659 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1660 neteq_->InsertPacket(rtp_info, payload, 0);
1661 neteq_->GetAudio(&out_frame_, &muted);
1662 }
1663
1664 // Lose a number of packets.
1665 int num_lost = 1 + i;
1666 for (int j = 0; j < num_lost; j++) {
1667 seq_no++;
1668 neteq_->GetAudio(&out_frame_, &muted);
1669 }
1670 }
1671
1672 // Check number of concealment events.
1673 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1674 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1675}
1676
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001677// Test that the jitter buffer delay stat is computed correctly.
1678void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1679 const int kNumPackets = 10;
1680 const int kDelayInNumPackets = 2;
1681 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1682 const size_t kSamples = kPacketLenMs * 16;
1683 const size_t kPayloadBytes = kSamples * 2;
1684 RTPHeader rtp_info;
1685 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1686 rtp_info.payloadType = 94; // PCM16b WB codec.
1687 rtp_info.markerBit = 0;
1688 const uint8_t payload[kPayloadBytes] = {0};
1689 bool muted;
1690 int packets_sent = 0;
1691 int packets_received = 0;
1692 int expected_delay = 0;
1693 while (packets_received < kNumPackets) {
1694 // Insert packet.
1695 if (packets_sent < kNumPackets) {
1696 rtp_info.sequenceNumber = packets_sent++;
1697 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1698 neteq_->InsertPacket(rtp_info, payload, 0);
1699 }
1700
1701 // Get packet.
1702 if (packets_sent > kDelayInNumPackets) {
1703 neteq_->GetAudio(&out_frame_, &muted);
1704 packets_received++;
1705
1706 // The delay reported by the jitter buffer never exceeds
1707 // the number of samples previously fetched with GetAudio
1708 // (hence the min()).
1709 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1710
1711 // The increase of the expected delay is the product of
1712 // the current delay of the jitter buffer in ms * the
1713 // number of samples that are sent for play out.
1714 int current_delay_ms = packets_delay * kPacketLenMs;
1715 expected_delay += current_delay_ms * kSamples;
1716 }
1717 }
1718
1719 if (apply_packet_loss) {
1720 // Extra call to GetAudio to cause concealment.
1721 neteq_->GetAudio(&out_frame_, &muted);
1722 }
1723
1724 // Check jitter buffer delay.
1725 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1726 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
1727}
1728
1729TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1730 TestJitterBufferDelay(false);
1731}
1732
1733TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1734 TestJitterBufferDelay(true);
1735}
1736
Henrik Lundin7687ad52018-07-02 10:14:46 +02001737namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001738TEST(NetEqNoTimeStretchingMode, RunTest) {
1739 NetEq::Config config;
1740 config.for_test_no_time_stretching = true;
1741 auto codecs = NetEqTest::StandardDecoderMap();
1742 NetEqTest::ExtDecoderMap ext_codecs;
1743 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1744 {1, kRtpExtensionAudioLevel},
1745 {3, kRtpExtensionAbsoluteSendTime},
1746 {5, kRtpExtensionTransportSequenceNumber},
1747 {7, kRtpExtensionVideoContentType},
1748 {8, kRtpExtensionVideoTiming}};
1749 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1750 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001751 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001752 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1753 new TimeLimitedNetEqInput(std::move(input), 20000));
1754 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1755 NetEqTest::Callbacks callbacks;
1756 NetEqTest test(config, codecs, ext_codecs, std::move(input_time_limit),
1757 std::move(output), callbacks);
1758 test.Run();
1759 const auto stats = test.SimulationStats();
1760 EXPECT_EQ(0, stats.accelerate_rate);
1761 EXPECT_EQ(0, stats.preemptive_rate);
1762}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001763
1764} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001765} // namespace webrtc