blob: 479fe7e34e4ed1ece52533e9c8a969f96256e29f [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
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020023#include "absl/flags/flag.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020024#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
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"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "modules/include/module_common_types_public.h"
Niels Möller53382cb2018-11-27 14:05:08 +010032#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/ignore_wundef.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/message_digest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020039#include "rtc_base/system/arch.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000040#include "system_wrappers/include/clock.h"
Henrik Lundine9619f82017-11-27 14:05:27 +010041#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
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 Bonadei2ab97f62019-07-18 13:44:12 +020055ABSL_FLAG(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)));
Niels Möller05543682019-01-10 16:55:06 +0100132 ASSERT_EQ(true,
133 neteq->RegisterPayloadType(8, SdpAudioFormat("pcma", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700134#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700135 ASSERT_EQ(true,
136 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700137#endif
138#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700139 ASSERT_EQ(true,
140 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700141#endif
142#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700143 ASSERT_EQ(true,
144 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700145#endif
146#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700147 ASSERT_EQ(true,
148 neteq->RegisterPayloadType(
149 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700150#endif
kwiberg5adaf732016-10-04 09:33:27 -0700151 ASSERT_EQ(true,
152 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
153 ASSERT_EQ(true,
154 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
155 ASSERT_EQ(true,
156 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
157 ASSERT_EQ(true,
158 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
159 ASSERT_EQ(true,
160 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700161}
minyue5f026d02015-12-16 07:36:04 -0800162} // namespace
163
minyue4f906772016-04-29 11:05:14 -0700164class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165 public:
minyue4f906772016-04-29 11:05:14 -0700166 explicit ResultSink(const std::string& output_file);
167 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168
Yves Gerey665174f2018-06-19 15:03:05 +0200169 template <typename T>
170 void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700171
172 void AddResult(const NetEqNetworkStatistics& stats);
173 void AddResult(const RtcpStatistics& stats);
174
175 void VerifyChecksum(const std::string& ref_check_sum);
176
177 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700179 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180};
181
Joachim Bauch4e909192017-12-19 22:27:51 +0100182ResultSink::ResultSink(const std::string& output_file)
minyue4f906772016-04-29 11:05:14 -0700183 : output_fp_(nullptr),
Joachim Bauch4e909192017-12-19 22:27:51 +0100184 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 if (!output_file.empty()) {
186 output_fp_ = fopen(output_file.c_str(), "wb");
187 EXPECT_TRUE(output_fp_ != NULL);
188 }
189}
190
minyue4f906772016-04-29 11:05:14 -0700191ResultSink::~ResultSink() {
192 if (output_fp_)
193 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194}
195
Yves Gerey665174f2018-06-19 15:03:05 +0200196template <typename T>
yujo36b1a5f2017-06-12 12:45:32 -0700197void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700199 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 }
yujo36b1a5f2017-06-12 12:45:32 -0700201 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202}
203
minyue4f906772016-04-29 11:05:14 -0700204void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800205#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800206 neteq_unittest::NetEqNetworkStatistics stats;
207 Convert(stats_raw, &stats);
208
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100209 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800210 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700211 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800212#else
213 FAIL() << "Writing to reference file requires Proto Buffer.";
214#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000215}
216
minyue4f906772016-04-29 11:05:14 -0700217void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800218#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800219 neteq_unittest::RtcpStatistics stats;
220 Convert(stats_raw, &stats);
221
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100222 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800223 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700224 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800225#else
226 FAIL() << "Writing to reference file requires Proto Buffer.";
227#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228}
229
minyue4f906772016-04-29 11:05:14 -0700230void ResultSink::VerifyChecksum(const std::string& checksum) {
231 std::vector<char> buffer;
232 buffer.resize(digest_->Size());
233 digest_->Finish(&buffer[0], buffer.size());
234 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
Yves Gereya038e712018-11-14 10:45:50 +0100235 if (checksum.size() == result.size()) {
236 EXPECT_EQ(checksum, result);
237 } else {
238 // Check result is one the '|'-separated checksums.
239 EXPECT_NE(checksum.find(result), std::string::npos)
240 << result << " should be one of these:\n"
241 << checksum;
242 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243}
244
245class NetEqDecodingTest : public ::testing::Test {
246 protected:
247 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
248 // constants below can be changed.
249 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700250 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
251 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
252 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800253 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000254 static const int kInitSampleRateHz = 8000;
255
256 NetEqDecodingTest();
257 virtual void SetUp();
258 virtual void TearDown();
Yves Gerey665174f2018-06-19 15:03:05 +0200259 void OpenInputFile(const std::string& rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800260 void Process();
minyue5f026d02015-12-16 07:36:04 -0800261
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000262 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700263 const std::string& output_checksum,
264 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700265 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800266
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 static void PopulateRtpInfo(int frame_index,
268 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700269 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270 static void PopulateCng(int frame_index,
271 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700272 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000274 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275
Yves Gerey665174f2018-06-19 15:03:05 +0200276 void WrapTest(uint16_t start_seq_no,
277 uint32_t start_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000278 const std::set<uint16_t>& drop_seq_numbers,
Yves Gerey665174f2018-06-19 15:03:05 +0200279 bool expect_seq_no_wrap,
280 bool expect_timestamp_wrap);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000281
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000282 void LongCngWithClockDrift(double drift_factor,
283 double network_freeze_ms,
284 bool pull_audio_during_freeze,
285 int delay_tolerance_ms,
286 int max_time_to_speech_ms);
287
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000288 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000289
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000290 SimulatedClock clock_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000292 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800293 std::unique_ptr<test::RtpFileSource> rtp_source_;
294 std::unique_ptr<test::Packet> packet_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800295 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000297 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298};
299
300// Allocating the static const so that it can be passed by reference.
301const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700302const size_t NetEqDecodingTest::kBlockSize8kHz;
303const size_t NetEqDecodingTest::kBlockSize16kHz;
304const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305const int NetEqDecodingTest::kInitSampleRateHz;
306
307NetEqDecodingTest::NetEqDecodingTest()
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000308 : clock_(0),
309 neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000310 config_(),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000311 output_sample_rate_(kInitSampleRateHz),
312 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000313 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314}
315
316void NetEqDecodingTest::SetUp() {
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000317 neteq_ = NetEq::Create(config_, &clock_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000318 NetEqNetworkStatistics stat;
319 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
320 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700322 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323}
324
325void NetEqDecodingTest::TearDown() {
326 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327}
328
Yves Gerey665174f2018-06-19 15:03:05 +0200329void NetEqDecodingTest::OpenInputFile(const std::string& rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000330 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331}
332
henrik.lundin6d8e0112016-03-04 10:34:21 -0800333void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 // Check if time to receive.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000335 while (packet_ && clock_.TimeInMilliseconds() >= packet_->time_ms()) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000336 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800337#ifndef WEBRTC_CODEC_ISAC
338 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700339 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800340#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200341 ASSERT_EQ(0,
342 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700343 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200344 rtc::ArrayView<const uint8_t>(
345 packet_->payload(), packet_->payload_length_bytes()),
346 static_cast<uint32_t>(packet_->time_ms() *
347 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 }
349 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700350 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 }
352
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000353 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700354 bool muted;
355 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
356 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800357 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
358 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
359 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
360 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
361 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800362 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363
364 // Increase time.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000365 clock_.AdvanceTimeMilliseconds(kTimeStepMs);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366}
367
minyue4f906772016-04-29 11:05:14 -0700368void NetEqDecodingTest::DecodeAndCompare(
369 const std::string& rtp_file,
370 const std::string& output_checksum,
371 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700372 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 OpenInputFile(rtp_file);
374
minyue4f906772016-04-29 11:05:14 -0700375 std::string ref_out_file =
376 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
377 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378
minyue4f906772016-04-29 11:05:14 -0700379 std::string stat_out_file =
380 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
381 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000382
henrik.lundin46ba49c2016-05-24 22:50:47 -0700383 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200385 uint64_t last_concealed_samples = 0;
386 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000387 while (packet_) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200388 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
390 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800391 ASSERT_NO_FATAL_FAILURE(Process());
Yves Gerey665174f2018-06-19 15:03:05 +0200392 ASSERT_NO_FATAL_FAILURE(
393 output.AddResult(out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394
395 // Query the network statistics API once per second
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000396 if (clock_.TimeInMilliseconds() % 1000 == 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700398 NetEqNetworkStatistics current_network_stats;
399 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
400 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
401
Henrik Lundinac0a5032017-09-25 12:22:46 +0200402 // Verify that liftime stats and network stats report similar loss
403 // concealment rates.
404 auto lifetime_stats = neteq_->GetLifetimeStatistics();
405 const uint64_t delta_concealed_samples =
406 lifetime_stats.concealed_samples - last_concealed_samples;
407 last_concealed_samples = lifetime_stats.concealed_samples;
408 const uint64_t delta_total_samples_received =
409 lifetime_stats.total_samples_received - last_total_samples_received;
410 last_total_samples_received = lifetime_stats.total_samples_received;
411 // The tolerance is 1% but expressed in Q14.
412 EXPECT_NEAR(
413 (delta_concealed_samples << 14) / delta_total_samples_received,
414 current_network_stats.expand_rate, (2 << 14) / 100.0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000415 }
416 }
minyue4f906772016-04-29 11:05:14 -0700417
418 SCOPED_TRACE("Check output audio.");
419 output.VerifyChecksum(output_checksum);
420 SCOPED_TRACE("Check network stats.");
421 network_stats.VerifyChecksum(network_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000422}
423
424void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
425 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700426 RTPHeader* rtp_info) {
427 rtp_info->sequenceNumber = frame_index;
428 rtp_info->timestamp = timestamp;
429 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
430 rtp_info->payloadType = 94; // PCM16b WB codec.
431 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432}
433
434void NetEqDecodingTest::PopulateCng(int frame_index,
435 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700436 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000437 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000438 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700439 rtp_info->sequenceNumber = frame_index;
440 rtp_info->timestamp = timestamp;
441 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
442 rtp_info->payloadType = 98; // WB CNG.
443 rtp_info->markerBit = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200444 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000445 *payload_len = 1; // Only noise level, no spectral parameters.
446}
447
ivoc72c08ed2016-01-20 07:26:24 -0800448#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
449 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +0100450 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800451#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700452#else
minyue5f026d02015-12-16 07:36:04 -0800453#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700454#endif
minyue5f026d02015-12-16 07:36:04 -0800455TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800456 const std::string input_rtp_file =
457 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000458
Yves Gerey665174f2018-06-19 15:03:05 +0200459 const std::string output_checksum =
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200460 PlatformChecksum("998be2e5a707e636af0b6298f54bedfabe72aae1",
461 "61e238ece4cd3b67d66a0b7047e06b20607dcb79", "not used",
462 "998be2e5a707e636af0b6298f54bedfabe72aae1",
463 "4116ac2a6e75baac3194b712d6fabe28b384275e");
minyue4f906772016-04-29 11:05:14 -0700464
henrik.lundin2979f552017-05-05 05:04:16 -0700465 const std::string network_stats_checksum =
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200466 PlatformChecksum("3689c9f0ab9e50cefab3e44c37c3d7aa0de82ca4",
467 "0a596217fccd8d90eff7d1666b8cc63143eeda12", "not used",
468 "3689c9f0ab9e50cefab3e44c37c3d7aa0de82ca4",
469 "3689c9f0ab9e50cefab3e44c37c3d7aa0de82ca4");
minyue4f906772016-04-29 11:05:14 -0700470
Yves Gerey665174f2018-06-19 15:03:05 +0200471 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200472 absl::GetFlag(FLAGS_gen_ref));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000473}
474
Yves Gerey665174f2018-06-19 15:03:05 +0200475#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200476 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800477#define MAYBE_TestOpusBitExactness TestOpusBitExactness
478#else
479#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
480#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200481TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800482 const std::string input_rtp_file =
483 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800484
Yves Gereya038e712018-11-14 10:45:50 +0100485 // Checksum depends on libopus being compiled with or without SSE.
486 const std::string maybe_sse =
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200487 "6b602683ca7285a98118b4824d72f4257952c18f|"
488 "eb0b68bddcac00fc85403df64f83126f8ea9bc93";
Yves Gerey110a4de2019-08-12 17:19:09 +0200489 // The neon implementation may differ.
490 const std::string maybe_neon =
491 "f95f2a220c9ca5d60b81c4653d46e0de2bee159f|"
492 "63651b8cc7711a66c9491d6b6ce94b774b64a0ce";
Yves Gereya038e712018-11-14 10:45:50 +0100493 const std::string output_checksum = PlatformChecksum(
Yves Gerey110a4de2019-08-12 17:19:09 +0200494 maybe_sse, maybe_neon, "6f288a03d34958f62496f18fa85655593eef4dbe",
495 maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700496
Yves Gerey110a4de2019-08-12 17:19:09 +0200497 // The neon implementation may differ.
498 const std::string stats_maybe_neon =
499 "a71dce66c7bea85ba22d4e29a5298f606f810444|"
500 "889cae8977da9ad7563864726f4eeb5ae66ac7da";
501 const std::string network_stats_checksum = PlatformChecksum(
502 "0b3d34baffaf651812ffaf06ea1b5ce45ea1c47a", stats_maybe_neon,
503 "7c64e1e915bace7c4bf583484efd64eaf234552f",
504 "0b3d34baffaf651812ffaf06ea1b5ce45ea1c47a",
505 "0b3d34baffaf651812ffaf06ea1b5ce45ea1c47a");
minyue4f906772016-04-29 11:05:14 -0700506
Yves Gerey665174f2018-06-19 15:03:05 +0200507 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200508 absl::GetFlag(FLAGS_gen_ref));
minyue93c08b72015-12-22 09:57:41 -0800509}
510
Yves Gerey665174f2018-06-19 15:03:05 +0200511#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100512 defined(WEBRTC_CODEC_OPUS)
513#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
514#else
515#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
516#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100517TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100518 const std::string input_rtp_file =
519 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
520
Yves Gereya038e712018-11-14 10:45:50 +0100521 const std::string maybe_sse =
522 "713af6c92881f5aab1285765ee6680da9d1c06ce|"
523 "2ac10c4e79aeedd0df2863b079da5848b40f00b5";
524 const std::string output_checksum = PlatformChecksum(
525 maybe_sse, "3ec991b96872123f1554c03c543ca5d518431e46",
526 "da9f9a2d94e0c2d67342fad4965d7b91cda50b25", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100527
528 const std::string network_stats_checksum =
529 "bab58dc587d956f326056d7340c96eb9d2d3cc21";
530
Henrik Lundine9619f82017-11-27 14:05:27 +0100531 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200532 absl::GetFlag(FLAGS_gen_ref));
Henrik Lundine9619f82017-11-27 14:05:27 +0100533}
534
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000535// Use fax mode to avoid time-scaling. This is to simplify the testing of
536// packet waiting times in the packet buffer.
537class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
538 protected:
539 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200540 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000541 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200542 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000543};
544
545TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000546 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
547 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000548 const size_t kSamples = 10 * 16;
549 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000550 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800551 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700552 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200553 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
554 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700555 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
556 rtp_info.payloadType = 94; // PCM16b WB codec.
557 rtp_info.markerBit = 0;
558 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559 }
560 // Pull out all data.
561 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700562 bool muted;
563 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800564 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000565 }
566
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200567 NetEqNetworkStatistics stats;
568 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000569 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
570 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200571 // each packet. Thus, we are calculating the statistics for a series from 10
572 // to 300, in steps of 10 ms.
573 EXPECT_EQ(155, stats.mean_waiting_time_ms);
574 EXPECT_EQ(155, stats.median_waiting_time_ms);
575 EXPECT_EQ(10, stats.min_waiting_time_ms);
576 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000577
578 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200579 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
580 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
581 EXPECT_EQ(-1, stats.median_waiting_time_ms);
582 EXPECT_EQ(-1, stats.min_waiting_time_ms);
583 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000584}
585
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000586TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587 const int kNumFrames = 3000; // Needed for convergence.
588 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000589 const size_t kSamples = 10 * 16;
590 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 while (frame_index < kNumFrames) {
592 // Insert one packet each time, except every 10th time where we insert two
593 // packets at once. This will create a negative clock-drift of approx. 10%.
594 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
595 for (int n = 0; n < num_packets; ++n) {
596 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700597 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700599 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000600 ++frame_index;
601 }
602
603 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700604 bool muted;
605 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800606 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 }
608
609 NetEqNetworkStatistics network_stats;
610 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700611 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612}
613
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000614TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000615 const int kNumFrames = 5000; // Needed for convergence.
616 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000617 const size_t kSamples = 10 * 16;
618 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 for (int i = 0; i < kNumFrames; ++i) {
620 // Insert one packet each time, except every 10th time where we don't insert
621 // any packet. This will create a positive clock-drift of approx. 11%.
622 int num_packets = (i % 10 == 9 ? 0 : 1);
623 for (int n = 0; n < num_packets; ++n) {
624 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700625 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000626 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700627 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000628 ++frame_index;
629 }
630
631 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700632 bool muted;
633 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800634 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635 }
636
637 NetEqNetworkStatistics network_stats;
638 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700639 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640}
641
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000642void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
643 double network_freeze_ms,
644 bool pull_audio_during_freeze,
645 int delay_tolerance_ms,
646 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000647 uint16_t seq_no = 0;
648 uint32_t timestamp = 0;
649 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000650 const size_t kSamples = kFrameSizeMs * 16;
651 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000652 double next_input_time_ms = 0.0;
653 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700654 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655
656 // Insert speech for 5 seconds.
657 const int kSpeechDurationMs = 5000;
658 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
659 // Each turn in this for loop is 10 ms.
660 while (next_input_time_ms <= t_ms) {
661 // Insert one 30 ms speech frame.
662 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700663 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700665 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 ++seq_no;
667 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000668 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000669 }
670 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700671 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800672 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673 }
674
henrik.lundin55480f52016-03-08 02:37:57 -0800675 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200676 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700677 ASSERT_TRUE(playout_timestamp);
678 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000679
680 // Insert CNG for 1 minute (= 60000 ms).
681 const int kCngPeriodMs = 100;
682 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
683 const int kCngDurationMs = 60000;
684 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
685 // Each turn in this for loop is 10 ms.
686 while (next_input_time_ms <= t_ms) {
687 // Insert one CNG frame each 100 ms.
688 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000689 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700690 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000691 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800692 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700693 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800694 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000695 ++seq_no;
696 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000697 next_input_time_ms += static_cast<double>(kCngPeriodMs) * 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::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000705
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000706 if (network_freeze_ms > 0) {
707 // First keep pulling audio for |network_freeze_ms| without inserting
708 // any data, then insert CNG data corresponding to |network_freeze_ms|
709 // without pulling any output audio.
710 const double loop_end_time = t_ms + network_freeze_ms;
711 for (; t_ms < loop_end_time; t_ms += 10) {
712 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700713 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800714 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800715 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000716 }
717 bool pull_once = pull_audio_during_freeze;
718 // If |pull_once| is true, GetAudio will be called once half-way through
719 // the network recovery period.
720 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
721 while (next_input_time_ms <= t_ms) {
722 if (pull_once && next_input_time_ms >= pull_time_ms) {
723 pull_once = false;
724 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700725 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800726 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800727 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000728 t_ms += 10;
729 }
730 // Insert one CNG frame each 100 ms.
731 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000732 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700733 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000734 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800735 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700736 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800737 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000738 ++seq_no;
739 timestamp += kCngPeriodSamples;
740 next_input_time_ms += kCngPeriodMs * drift_factor;
741 }
742 }
743
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000745 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800746 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747 // Each turn in this for loop is 10 ms.
748 while (next_input_time_ms <= t_ms) {
749 // Insert one 30 ms speech frame.
750 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700751 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000752 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700753 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000754 ++seq_no;
755 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000756 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000757 }
758 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700759 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800760 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000761 // Increase clock.
762 t_ms += 10;
763 }
764
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000765 // Check that the speech starts again within reasonable time.
766 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
767 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700768 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700769 ASSERT_TRUE(playout_timestamp);
770 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000771 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000772 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
773 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000774}
775
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000776TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000777 // Apply a clock drift of -25 ms / s (sender faster than receiver).
778 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000779 const double kNetworkFreezeTimeMs = 0.0;
780 const bool kGetAudioDuringFreezeRecovery = false;
781 const int kDelayToleranceMs = 20;
782 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200783 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
784 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000785 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000786}
787
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000788TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000789 // Apply a clock drift of +25 ms / s (sender slower than receiver).
790 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000791 const double kNetworkFreezeTimeMs = 0.0;
792 const bool kGetAudioDuringFreezeRecovery = false;
793 const int kDelayToleranceMs = 20;
794 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200795 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
796 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000797 kMaxTimeToSpeechMs);
798}
799
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000800TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 // Apply a clock drift of -25 ms / s (sender faster than receiver).
802 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
803 const double kNetworkFreezeTimeMs = 5000.0;
804 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200805 const int kDelayToleranceMs = 60;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000806 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200807 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
808 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000809 kMaxTimeToSpeechMs);
810}
811
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000812TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000813 // Apply a clock drift of +25 ms / s (sender slower than receiver).
814 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
815 const double kNetworkFreezeTimeMs = 5000.0;
816 const bool kGetAudioDuringFreezeRecovery = false;
817 const int kDelayToleranceMs = 20;
818 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200819 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
820 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000821 kMaxTimeToSpeechMs);
822}
823
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000824TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000825 // Apply a clock drift of +25 ms / s (sender slower than receiver).
826 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
827 const double kNetworkFreezeTimeMs = 5000.0;
828 const bool kGetAudioDuringFreezeRecovery = true;
829 const int kDelayToleranceMs = 20;
830 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200831 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
832 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000833 kMaxTimeToSpeechMs);
834}
835
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000836TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000837 const double kDriftFactor = 1.0; // No drift.
838 const double kNetworkFreezeTimeMs = 0.0;
839 const bool kGetAudioDuringFreezeRecovery = false;
840 const int kDelayToleranceMs = 10;
841 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200842 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
843 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000844 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000845}
846
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000847TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000848 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700850 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700852 rtp_info.payloadType = 1; // Not registered as a decoder.
853 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854}
855
Peter Boströme2976c82016-01-04 22:44:05 +0100856#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800857#define MAYBE_DecoderError DecoderError
858#else
859#define MAYBE_DecoderError DISABLED_DecoderError
860#endif
861
Peter Boströme2976c82016-01-04 22:44:05 +0100862TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000863 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000864 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700865 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000866 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700867 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
868 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000869 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
870 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700871 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800872 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700873 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000874 }
henrik.lundin7a926812016-05-12 13:51:28 -0700875 bool muted;
876 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
877 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800878
yujo36b1a5f2017-06-12 12:45:32 -0700879 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700881 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200883 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000884 ss << "i = " << i;
885 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700886 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000887 }
888}
889
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000890TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000891 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
892 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700893 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800894 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700895 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000896 }
henrik.lundin7a926812016-05-12 13:51:28 -0700897 bool muted;
898 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
899 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000900 // Verify that the first block of samples is set to 0.
901 static const int kExpectedOutputLength =
902 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700903 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000904 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200905 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 ss << "i = " << i;
907 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700908 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 }
henrik.lundind89814b2015-11-23 06:49:25 -0800910 // Verify that the sample rate did not change from the initial configuration.
911 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000913
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000914class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000915 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000916 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700917 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000918 uint8_t payload_type = 0xFF; // Invalid.
919 if (sampling_rate_hz == 8000) {
920 expected_samples_per_channel = kBlockSize8kHz;
921 payload_type = 93; // PCM 16, 8 kHz.
922 } else if (sampling_rate_hz == 16000) {
923 expected_samples_per_channel = kBlockSize16kHz;
924 payload_type = 94; // PCM 16, 16 kHZ.
925 } else if (sampling_rate_hz == 32000) {
926 expected_samples_per_channel = kBlockSize32kHz;
927 payload_type = 95; // PCM 16, 32 kHz.
928 } else {
929 ASSERT_TRUE(false); // Unsupported test case.
930 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000931
henrik.lundin6d8e0112016-03-04 10:34:21 -0800932 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000933 test::AudioLoop input;
934 // We are using the same 32 kHz input file for all tests, regardless of
935 // |sampling_rate_hz|. The output may sound weird, but the test is still
936 // valid.
937 ASSERT_TRUE(input.Init(
938 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
939 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700940 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000941
942 // Payload of 10 ms of PCM16 32 kHz.
943 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700944 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000945 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700946 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000947
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000948 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700949 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000950 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800951 auto block = input.GetNextBlock();
952 ASSERT_EQ(expected_samples_per_channel, block.size());
953 size_t enc_len_bytes =
954 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000955 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
956
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200957 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700958 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200959 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
960 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800961 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700962 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800963 ASSERT_EQ(1u, output.num_channels_);
964 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800965 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000966
967 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200968 rtp_info.timestamp +=
969 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700970 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200971 receive_timestamp +=
972 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000973 }
974
henrik.lundin6d8e0112016-03-04 10:34:21 -0800975 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000976
977 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
978 // one frame without checking speech-type. This is the first frame pulled
979 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700980 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800981 ASSERT_EQ(1u, output.num_channels_);
982 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000983
984 // To be able to test the fading of background noise we need at lease to
985 // pull 611 frames.
986 const int kFadingThreshold = 611;
987
988 // Test several CNG-to-PLC packet for the expected behavior. The number 20
989 // is arbitrary, but sufficiently large to test enough number of frames.
990 const int kNumPlcToCngTestFrames = 20;
991 bool plc_to_cng = false;
992 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800993 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700994 // Set to non-zero.
995 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700996 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
997 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800998 ASSERT_EQ(1u, output.num_channels_);
999 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001000 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001001 plc_to_cng = true;
1002 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001003 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001004 for (size_t k = 0;
1005 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001006 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +02001007 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001008 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001009 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001010 }
1011 }
1012 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1013 }
1014};
1015
Henrik Lundin67190172018-04-20 15:34:48 +02001016TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001017 CheckBgn(8000);
1018 CheckBgn(16000);
1019 CheckBgn(32000);
1020}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001021
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001022void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1023 uint32_t start_timestamp,
1024 const std::set<uint16_t>& drop_seq_numbers,
1025 bool expect_seq_no_wrap,
1026 bool expect_timestamp_wrap) {
1027 uint16_t seq_no = start_seq_no;
1028 uint32_t timestamp = start_timestamp;
1029 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1030 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1031 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001032 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001033 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001034 uint32_t receive_timestamp = 0;
1035
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001036 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001037 const int kSpeechDurationMs = 2000;
1038 int packets_inserted = 0;
1039 uint16_t last_seq_no;
1040 uint32_t last_timestamp;
1041 bool timestamp_wrapped = false;
1042 bool seq_no_wrapped = false;
1043 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1044 // Each turn in this for loop is 10 ms.
1045 while (next_input_time_ms <= t_ms) {
1046 // Insert one 30 ms speech frame.
1047 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001048 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001049 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1050 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1051 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001052 ASSERT_EQ(0,
1053 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001054 ++packets_inserted;
1055 }
1056 NetEqNetworkStatistics network_stats;
1057 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1058
1059 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1060 // packet size for first few packets. Therefore we refrain from checking
1061 // the criteria.
1062 if (packets_inserted > 4) {
1063 // Expect preferred and actual buffer size to be no more than 2 frames.
1064 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
Yves Gerey665174f2018-06-19 15:03:05 +02001065 EXPECT_LE(network_stats.current_buffer_size_ms,
1066 kFrameSizeMs * 2 + algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001067 }
1068 last_seq_no = seq_no;
1069 last_timestamp = timestamp;
1070
1071 ++seq_no;
1072 timestamp += kSamples;
1073 receive_timestamp += kSamples;
1074 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1075
1076 seq_no_wrapped |= seq_no < last_seq_no;
1077 timestamp_wrapped |= timestamp < last_timestamp;
1078 }
1079 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001080 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001081 bool muted;
1082 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001083 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1084 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001085
1086 // Expect delay (in samples) to be less than 2 packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +02001087 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001088 ASSERT_TRUE(playout_timestamp);
1089 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001090 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001091 }
1092 // Make sure we have actually tested wrap-around.
1093 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1094 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1095}
1096
1097TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1098 // Start with a sequence number that will soon wrap.
1099 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1100 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1101}
1102
1103TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1104 // Start with a sequence number that will soon wrap.
1105 std::set<uint16_t> drop_seq_numbers;
1106 drop_seq_numbers.insert(0xFFFF);
1107 drop_seq_numbers.insert(0x0);
1108 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1109}
1110
1111TEST_F(NetEqDecodingTest, TimestampWrap) {
1112 // Start with a timestamp that will soon wrap.
1113 std::set<uint16_t> drop_seq_numbers;
1114 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1115}
1116
1117TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1118 // Start with a timestamp and a sequence number that will wrap at the same
1119 // time.
1120 std::set<uint16_t> drop_seq_numbers;
1121 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1122}
1123
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001124void NetEqDecodingTest::DuplicateCng() {
1125 uint16_t seq_no = 0;
1126 uint32_t timestamp = 0;
1127 const int kFrameSizeMs = 10;
1128 const int kSampleRateKhz = 16;
1129 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001130 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001131
Yves Gerey665174f2018-06-19 15:03:05 +02001132 const int algorithmic_delay_samples =
1133 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001134 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001135 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001136 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001137 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001138 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001139 for (int i = 0; i < 3; ++i) {
1140 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001141 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001142 ++seq_no;
1143 timestamp += kSamples;
1144
1145 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001146 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001147 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001148 }
1149 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001150 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001151
1152 // Insert same CNG packet twice.
1153 const int kCngPeriodMs = 100;
1154 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001155 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001156 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1157 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001158 ASSERT_EQ(
1159 0, neteq_->InsertPacket(
1160 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001161
1162 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001163 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001164 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001165 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001166 EXPECT_FALSE(
1167 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001168 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1169 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001170
1171 // Insert the same CNG packet again. Note that at this point it is old, since
1172 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001173 ASSERT_EQ(
1174 0, neteq_->InsertPacket(
1175 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001176
1177 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1178 // we have already pulled out CNG once.
1179 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001180 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001181 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001182 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001183 EXPECT_FALSE(
1184 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001185 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001186 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001187 }
1188
1189 // Insert speech again.
1190 ++seq_no;
1191 timestamp += kCngPeriodSamples;
1192 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001193 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001194
1195 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001196 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001197 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001198 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +02001199 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001200 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001201 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001202 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001203}
1204
Yves Gerey665174f2018-06-19 15:03:05 +02001205TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
1206 DuplicateCng();
1207}
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001208
1209TEST_F(NetEqDecodingTest, CngFirst) {
1210 uint16_t seq_no = 0;
1211 uint32_t timestamp = 0;
1212 const int kFrameSizeMs = 10;
1213 const int kSampleRateKhz = 16;
1214 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1215 const int kPayloadBytes = kSamples * 2;
1216 const int kCngPeriodMs = 100;
1217 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1218 size_t payload_len;
1219
1220 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001221 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001222
1223 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001224 ASSERT_EQ(
1225 NetEq::kOK,
1226 neteq_->InsertPacket(
1227 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001228 ++seq_no;
1229 timestamp += kCngPeriodSamples;
1230
1231 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001232 bool muted;
1233 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001234 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001235 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001236
1237 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001238 const uint32_t first_speech_timestamp = timestamp;
1239 int timeout_counter = 0;
1240 do {
1241 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001242 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001243 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001244 ++seq_no;
1245 timestamp += kSamples;
1246
1247 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001248 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001249 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001250 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001251 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001252 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001253}
henrik.lundin7a926812016-05-12 13:51:28 -07001254
1255class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1256 public:
1257 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1258 config_.enable_muted_state = true;
1259 }
1260
1261 protected:
1262 static constexpr size_t kSamples = 10 * 16;
1263 static constexpr size_t kPayloadBytes = kSamples * 2;
1264
1265 void InsertPacket(uint32_t rtp_timestamp) {
1266 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001267 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001268 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001269 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001270 }
1271
henrik.lundin42feb512016-09-20 06:51:40 -07001272 void InsertCngPacket(uint32_t rtp_timestamp) {
1273 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001274 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001275 size_t payload_len;
1276 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001277 EXPECT_EQ(
1278 NetEq::kOK,
1279 neteq_->InsertPacket(
1280 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001281 }
1282
henrik.lundin7a926812016-05-12 13:51:28 -07001283 bool GetAudioReturnMuted() {
1284 bool muted;
1285 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1286 return muted;
1287 }
1288
1289 void GetAudioUntilMuted() {
1290 while (!GetAudioReturnMuted()) {
1291 ASSERT_LT(counter_++, 1000) << "Test timed out";
1292 }
1293 }
1294
1295 void GetAudioUntilNormal() {
1296 bool muted = false;
1297 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1298 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1299 ASSERT_LT(counter_++, 1000) << "Test timed out";
1300 }
1301 EXPECT_FALSE(muted);
1302 }
1303
1304 int counter_ = 0;
1305};
1306
1307// Verifies that NetEq goes in and out of muted state as expected.
1308TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1309 // Insert one speech packet.
1310 InsertPacket(0);
1311 // Pull out audio once and expect it not to be muted.
1312 EXPECT_FALSE(GetAudioReturnMuted());
1313 // Pull data until faded out.
1314 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001315 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001316
1317 // Verify that output audio is not written during muted mode. Other parameters
1318 // should be correct, though.
1319 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001320 int16_t* frame_data = new_frame.mutable_data();
1321 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1322 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001323 }
1324 bool muted;
1325 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1326 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001327 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001328 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1329 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001330 }
1331 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1332 new_frame.timestamp_);
1333 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1334 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1335 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1336 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1337 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1338
1339 // Insert new data. Timestamp is corrected for the time elapsed since the last
1340 // packet. Verify that normal operation resumes.
1341 InsertPacket(kSamples * counter_);
1342 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001343 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001344
1345 NetEqNetworkStatistics stats;
1346 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1347 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1348 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1349 // concealment samples in this test.
1350 EXPECT_GT(stats.expand_rate, 14000);
1351 // And, it should be greater than the speech_expand_rate.
1352 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001353}
1354
1355// Verifies that NetEq goes out of muted state when given a delayed packet.
1356TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1357 // Insert one speech packet.
1358 InsertPacket(0);
1359 // Pull out audio once and expect it not to be muted.
1360 EXPECT_FALSE(GetAudioReturnMuted());
1361 // Pull data until faded out.
1362 GetAudioUntilMuted();
1363 // Insert new data. Timestamp is only corrected for the half of the time
1364 // elapsed since the last packet. That is, the new packet is delayed. Verify
1365 // that normal operation resumes.
1366 InsertPacket(kSamples * counter_ / 2);
1367 GetAudioUntilNormal();
1368}
1369
1370// Verifies that NetEq goes out of muted state when given a future packet.
1371TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1372 // Insert one speech packet.
1373 InsertPacket(0);
1374 // Pull out audio once and expect it not to be muted.
1375 EXPECT_FALSE(GetAudioReturnMuted());
1376 // Pull data until faded out.
1377 GetAudioUntilMuted();
1378 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1379 // last packet. That is, the new packet is too early. Verify that normal
1380 // operation resumes.
1381 InsertPacket(kSamples * counter_ * 2);
1382 GetAudioUntilNormal();
1383}
1384
1385// Verifies that NetEq goes out of muted state when given an old packet.
1386TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1387 // Insert one speech packet.
1388 InsertPacket(0);
1389 // Pull out audio once and expect it not to be muted.
1390 EXPECT_FALSE(GetAudioReturnMuted());
1391 // Pull data until faded out.
1392 GetAudioUntilMuted();
1393
1394 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1395 // Insert packet which is older than the first packet.
1396 InsertPacket(kSamples * (counter_ - 1000));
1397 EXPECT_FALSE(GetAudioReturnMuted());
1398 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1399}
1400
henrik.lundin42feb512016-09-20 06:51:40 -07001401// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1402// packet stream is suspended for a long time.
1403TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1404 // Insert one CNG packet.
1405 InsertCngPacket(0);
1406
1407 // Pull 10 seconds of audio (10 ms audio generated per lap).
1408 for (int i = 0; i < 1000; ++i) {
1409 bool muted;
1410 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1411 ASSERT_FALSE(muted);
1412 }
1413 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1414}
1415
1416// Verifies that NetEq goes back to normal after a long CNG period with the
1417// packet stream suspended.
1418TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1419 // Insert one CNG packet.
1420 InsertCngPacket(0);
1421
1422 // Pull 10 seconds of audio (10 ms audio generated per lap).
1423 for (int i = 0; i < 1000; ++i) {
1424 bool muted;
1425 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1426 }
1427
1428 // Insert new data. Timestamp is corrected for the time elapsed since the last
1429 // packet. Verify that normal operation resumes.
1430 InsertPacket(kSamples * counter_);
1431 GetAudioUntilNormal();
1432}
1433
henrik.lundin7a926812016-05-12 13:51:28 -07001434class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1435 public:
1436 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1437
1438 void SetUp() override {
1439 NetEqDecodingTest::SetUp();
1440 config2_ = config_;
1441 }
1442
1443 void CreateSecondInstance() {
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001444 neteq2_.reset(
1445 NetEq::Create(config2_, &clock_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001446 ASSERT_TRUE(neteq2_);
1447 LoadDecoders(neteq2_.get());
1448 }
1449
1450 protected:
1451 std::unique_ptr<NetEq> neteq2_;
1452 NetEq::Config config2_;
1453};
1454
1455namespace {
1456::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1457 const AudioFrame& b) {
1458 if (a.timestamp_ != b.timestamp_)
1459 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1460 << " != " << b.timestamp_ << ")";
1461 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +02001462 return ::testing::AssertionFailure()
1463 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
1464 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001465 if (a.samples_per_channel_ != b.samples_per_channel_)
1466 return ::testing::AssertionFailure()
1467 << "samples_per_channel_ diff (" << a.samples_per_channel_
1468 << " != " << b.samples_per_channel_ << ")";
1469 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +02001470 return ::testing::AssertionFailure()
1471 << "num_channels_ diff (" << a.num_channels_
1472 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001473 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +02001474 return ::testing::AssertionFailure()
1475 << "speech_type_ diff (" << a.speech_type_
1476 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001477 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +02001478 return ::testing::AssertionFailure()
1479 << "vad_activity_ diff (" << a.vad_activity_
1480 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001481 return ::testing::AssertionSuccess();
1482}
1483
1484::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1485 const AudioFrame& b) {
1486 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1487 if (!res)
1488 return res;
Yves Gerey665174f2018-06-19 15:03:05 +02001489 if (memcmp(a.data(), b.data(),
1490 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
1491 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001492 return ::testing::AssertionFailure() << "data_ diff";
1493 }
1494 return ::testing::AssertionSuccess();
1495}
1496
1497} // namespace
1498
1499TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1500 ASSERT_FALSE(config_.enable_muted_state);
1501 config2_.enable_muted_state = true;
1502 CreateSecondInstance();
1503
1504 // Insert one speech packet into both NetEqs.
1505 const size_t kSamples = 10 * 16;
1506 const size_t kPayloadBytes = kSamples * 2;
1507 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001508 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001509 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001510 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1511 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001512
1513 AudioFrame out_frame1, out_frame2;
1514 bool muted;
1515 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001516 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001517 ss << "i = " << i;
1518 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1519 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1520 EXPECT_FALSE(muted);
1521 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1522 if (muted) {
1523 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1524 } else {
1525 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1526 }
1527 }
1528 EXPECT_TRUE(muted);
1529
1530 // Insert new data. Timestamp is corrected for the time elapsed since the last
1531 // packet.
1532 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001533 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1534 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001535
1536 int counter = 0;
1537 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1538 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +02001539 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001540 ss << "counter = " << counter;
1541 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1542 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1543 EXPECT_FALSE(muted);
1544 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1545 if (muted) {
1546 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1547 } else {
1548 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1549 }
1550 }
1551 EXPECT_FALSE(muted);
1552}
1553
henrik.lundin114c1b32017-04-26 07:47:32 -07001554TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1555 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1556
1557 // Pull out data once.
1558 AudioFrame output;
1559 bool muted;
1560 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1561
1562 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1563}
1564
1565TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1566 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1567 // default). Make the length 10 ms.
1568 constexpr size_t kPayloadSamples = 16 * 10;
1569 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1570 uint8_t payload[kPayloadBytes] = {0};
1571
1572 RTPHeader rtp_info;
1573 constexpr uint32_t kRtpTimestamp = 0x1234;
1574 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1575 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1576
1577 // Pull out data once.
1578 AudioFrame output;
1579 bool muted;
1580 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1581
1582 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1583 neteq_->LastDecodedTimestamps());
1584
1585 // Nothing decoded on the second call.
1586 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1587 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1588}
1589
1590TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1591 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1592 // by default). Make the length 5 ms so that NetEq must decode them both in
1593 // the same GetAudio call.
1594 constexpr size_t kPayloadSamples = 16 * 5;
1595 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1596 uint8_t payload[kPayloadBytes] = {0};
1597
1598 RTPHeader rtp_info;
1599 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1600 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1601 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1602 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1603 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1604 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1605
1606 // Pull out data once.
1607 AudioFrame output;
1608 bool muted;
1609 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1610
1611 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1612 neteq_->LastDecodedTimestamps());
1613}
1614
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001615TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1616 const int kNumConcealmentEvents = 19;
1617 const size_t kSamples = 10 * 16;
1618 const size_t kPayloadBytes = kSamples * 2;
1619 int seq_no = 0;
1620 RTPHeader rtp_info;
1621 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1622 rtp_info.payloadType = 94; // PCM16b WB codec.
1623 rtp_info.markerBit = 0;
1624 const uint8_t payload[kPayloadBytes] = {0};
1625 bool muted;
1626
1627 for (int i = 0; i < kNumConcealmentEvents; i++) {
1628 // Insert some packets of 10 ms size.
1629 for (int j = 0; j < 10; j++) {
1630 rtp_info.sequenceNumber = seq_no++;
1631 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1632 neteq_->InsertPacket(rtp_info, payload, 0);
1633 neteq_->GetAudio(&out_frame_, &muted);
1634 }
1635
1636 // Lose a number of packets.
1637 int num_lost = 1 + i;
1638 for (int j = 0; j < num_lost; j++) {
1639 seq_no++;
1640 neteq_->GetAudio(&out_frame_, &muted);
1641 }
1642 }
1643
1644 // Check number of concealment events.
1645 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1646 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1647}
1648
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001649// Test that the jitter buffer delay stat is computed correctly.
1650void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1651 const int kNumPackets = 10;
1652 const int kDelayInNumPackets = 2;
1653 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1654 const size_t kSamples = kPacketLenMs * 16;
1655 const size_t kPayloadBytes = kSamples * 2;
1656 RTPHeader rtp_info;
1657 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1658 rtp_info.payloadType = 94; // PCM16b WB codec.
1659 rtp_info.markerBit = 0;
1660 const uint8_t payload[kPayloadBytes] = {0};
1661 bool muted;
1662 int packets_sent = 0;
1663 int packets_received = 0;
1664 int expected_delay = 0;
Chen Xing0acffb52019-01-15 15:46:29 +01001665 uint64_t expected_emitted_count = 0;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001666 while (packets_received < kNumPackets) {
1667 // Insert packet.
1668 if (packets_sent < kNumPackets) {
1669 rtp_info.sequenceNumber = packets_sent++;
1670 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1671 neteq_->InsertPacket(rtp_info, payload, 0);
1672 }
1673
1674 // Get packet.
1675 if (packets_sent > kDelayInNumPackets) {
1676 neteq_->GetAudio(&out_frame_, &muted);
1677 packets_received++;
1678
1679 // The delay reported by the jitter buffer never exceeds
1680 // the number of samples previously fetched with GetAudio
1681 // (hence the min()).
1682 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1683
1684 // The increase of the expected delay is the product of
1685 // the current delay of the jitter buffer in ms * the
1686 // number of samples that are sent for play out.
1687 int current_delay_ms = packets_delay * kPacketLenMs;
1688 expected_delay += current_delay_ms * kSamples;
Chen Xing0acffb52019-01-15 15:46:29 +01001689 expected_emitted_count += kSamples;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001690 }
1691 }
1692
1693 if (apply_packet_loss) {
1694 // Extra call to GetAudio to cause concealment.
1695 neteq_->GetAudio(&out_frame_, &muted);
1696 }
1697
1698 // Check jitter buffer delay.
1699 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1700 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
Chen Xing0acffb52019-01-15 15:46:29 +01001701 EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001702}
1703
1704TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1705 TestJitterBufferDelay(false);
1706}
1707
1708TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1709 TestJitterBufferDelay(true);
1710}
1711
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001712TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithAcceleration) {
1713 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1714 const size_t kSamples = kPacketLenMs * 16;
1715 const size_t kPayloadBytes = kSamples * 2;
1716 RTPHeader rtp_info;
1717 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1718 rtp_info.payloadType = 94; // PCM16b WB codec.
1719 rtp_info.markerBit = 0;
1720 const uint8_t payload[kPayloadBytes] = {0};
1721
1722 neteq_->InsertPacket(rtp_info, payload, 0);
1723
1724 bool muted;
1725 neteq_->GetAudio(&out_frame_, &muted);
1726
1727 rtp_info.sequenceNumber += 1;
1728 rtp_info.timestamp += kSamples;
1729 neteq_->InsertPacket(rtp_info, payload, 0);
1730 rtp_info.sequenceNumber += 1;
1731 rtp_info.timestamp += kSamples;
1732 neteq_->InsertPacket(rtp_info, payload, 0);
1733
1734 // We have two packets in the buffer and kAccelerate operation will
1735 // extract 20 ms of data.
1736 neteq_->GetAudio(&out_frame_, &muted, Operations::kAccelerate);
1737
1738 // Check jitter buffer delay.
1739 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1740 EXPECT_EQ(10 * kSamples * 3, stats.jitter_buffer_delay_ms);
1741 EXPECT_EQ(kSamples * 3, stats.jitter_buffer_emitted_count);
1742}
1743
Henrik Lundin7687ad52018-07-02 10:14:46 +02001744namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001745TEST(NetEqNoTimeStretchingMode, RunTest) {
1746 NetEq::Config config;
1747 config.for_test_no_time_stretching = true;
1748 auto codecs = NetEqTest::StandardDecoderMap();
Henrik Lundin7687ad52018-07-02 10:14:46 +02001749 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1750 {1, kRtpExtensionAudioLevel},
1751 {3, kRtpExtensionAbsoluteSendTime},
1752 {5, kRtpExtensionTransportSequenceNumber},
1753 {7, kRtpExtensionVideoContentType},
1754 {8, kRtpExtensionVideoTiming}};
1755 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1756 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001757 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001758 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1759 new TimeLimitedNetEqInput(std::move(input), 20000));
1760 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1761 NetEqTest::Callbacks callbacks;
Niels Möllerbd6dee82019-01-02 09:39:47 +01001762 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, nullptr,
1763 std::move(input_time_limit), std::move(output), callbacks);
Henrik Lundin7687ad52018-07-02 10:14:46 +02001764 test.Run();
1765 const auto stats = test.SimulationStats();
1766 EXPECT_EQ(0, stats.accelerate_rate);
1767 EXPECT_EQ(0, stats.preemptive_rate);
1768}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001769
1770} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001771} // namespace webrtc