blob: 0db6fc7b73081a01f9b3b117a3f4f8f2e72ba3a4 [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_added_zero_samples(stats_raw.added_zero_samples);
97 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
98 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
99 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
100 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
101}
102
103void Convert(const webrtc::RtcpStatistics& stats_raw,
104 webrtc::neteq_unittest::RtcpStatistics* stats) {
105 stats->set_fraction_lost(stats_raw.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700106 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800107 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700108 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800109 stats->set_jitter(stats_raw.jitter);
110}
111
Yves Gerey665174f2018-06-19 15:03:05 +0200112void AddMessage(FILE* file,
113 rtc::MessageDigest* digest,
minyue4f906772016-04-29 11:05:14 -0700114 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800115 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700116 if (file)
117 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
118 digest->Update(&size, sizeof(size));
119
120 if (file)
121 ASSERT_EQ(static_cast<size_t>(size),
122 fwrite(message.data(), sizeof(char), size, file));
123 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800124}
125
minyue5f026d02015-12-16 07:36:04 -0800126#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
127
henrik.lundin7a926812016-05-12 13:51:28 -0700128void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700129 ASSERT_EQ(true,
130 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
Niels Möller05543682019-01-10 16:55:06 +0100131 ASSERT_EQ(true,
132 neteq->RegisterPayloadType(8, SdpAudioFormat("pcma", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700133#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700134 ASSERT_EQ(true,
135 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700136#endif
137#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700138 ASSERT_EQ(true,
139 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700140#endif
141#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700142 ASSERT_EQ(true,
143 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700144#endif
145#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700146 ASSERT_EQ(true,
147 neteq->RegisterPayloadType(
148 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700149#endif
kwiberg5adaf732016-10-04 09:33:27 -0700150 ASSERT_EQ(true,
151 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
152 ASSERT_EQ(true,
153 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
154 ASSERT_EQ(true,
155 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
156 ASSERT_EQ(true,
157 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
158 ASSERT_EQ(true,
159 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700160}
minyue5f026d02015-12-16 07:36:04 -0800161} // namespace
162
minyue4f906772016-04-29 11:05:14 -0700163class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164 public:
minyue4f906772016-04-29 11:05:14 -0700165 explicit ResultSink(const std::string& output_file);
166 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167
Yves Gerey665174f2018-06-19 15:03:05 +0200168 template <typename T>
169 void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700170
171 void AddResult(const NetEqNetworkStatistics& stats);
172 void AddResult(const RtcpStatistics& stats);
173
174 void VerifyChecksum(const std::string& ref_check_sum);
175
176 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000177 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700178 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179};
180
Joachim Bauch4e909192017-12-19 22:27:51 +0100181ResultSink::ResultSink(const std::string& output_file)
minyue4f906772016-04-29 11:05:14 -0700182 : output_fp_(nullptr),
Joachim Bauch4e909192017-12-19 22:27:51 +0100183 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 if (!output_file.empty()) {
185 output_fp_ = fopen(output_file.c_str(), "wb");
186 EXPECT_TRUE(output_fp_ != NULL);
187 }
188}
189
minyue4f906772016-04-29 11:05:14 -0700190ResultSink::~ResultSink() {
191 if (output_fp_)
192 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193}
194
Yves Gerey665174f2018-06-19 15:03:05 +0200195template <typename T>
yujo36b1a5f2017-06-12 12:45:32 -0700196void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000197 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700198 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199 }
yujo36b1a5f2017-06-12 12:45:32 -0700200 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201}
202
minyue4f906772016-04-29 11:05:14 -0700203void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800204#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800205 neteq_unittest::NetEqNetworkStatistics stats;
206 Convert(stats_raw, &stats);
207
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100208 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800209 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700210 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800211#else
212 FAIL() << "Writing to reference file requires Proto Buffer.";
213#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214}
215
minyue4f906772016-04-29 11:05:14 -0700216void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800217#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800218 neteq_unittest::RtcpStatistics stats;
219 Convert(stats_raw, &stats);
220
Mirko Bonadeie45c6882019-02-16 09:59:29 +0100221 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800222 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700223 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800224#else
225 FAIL() << "Writing to reference file requires Proto Buffer.";
226#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000227}
228
minyue4f906772016-04-29 11:05:14 -0700229void ResultSink::VerifyChecksum(const std::string& checksum) {
230 std::vector<char> buffer;
231 buffer.resize(digest_->Size());
232 digest_->Finish(&buffer[0], buffer.size());
233 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
Yves Gereya038e712018-11-14 10:45:50 +0100234 if (checksum.size() == result.size()) {
235 EXPECT_EQ(checksum, result);
236 } else {
237 // Check result is one the '|'-separated checksums.
238 EXPECT_NE(checksum.find(result), std::string::npos)
239 << result << " should be one of these:\n"
240 << checksum;
241 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242}
243
244class NetEqDecodingTest : public ::testing::Test {
245 protected:
246 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
247 // constants below can be changed.
248 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700249 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
250 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
251 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800252 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 static const int kInitSampleRateHz = 8000;
254
255 NetEqDecodingTest();
256 virtual void SetUp();
257 virtual void TearDown();
Yves Gerey665174f2018-06-19 15:03:05 +0200258 void OpenInputFile(const std::string& rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800259 void Process();
minyue5f026d02015-12-16 07:36:04 -0800260
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000261 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700262 const std::string& output_checksum,
263 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700264 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800265
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 static void PopulateRtpInfo(int frame_index,
267 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700268 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 static void PopulateCng(int frame_index,
270 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700271 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000273 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274
Yves Gerey665174f2018-06-19 15:03:05 +0200275 void WrapTest(uint16_t start_seq_no,
276 uint32_t start_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000277 const std::set<uint16_t>& drop_seq_numbers,
Yves Gerey665174f2018-06-19 15:03:05 +0200278 bool expect_seq_no_wrap,
279 bool expect_timestamp_wrap);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000280
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000281 void LongCngWithClockDrift(double drift_factor,
282 double network_freeze_ms,
283 bool pull_audio_during_freeze,
284 int delay_tolerance_ms,
285 int max_time_to_speech_ms);
286
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000287 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000288
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000289 SimulatedClock clock_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000291 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800292 std::unique_ptr<test::RtpFileSource> rtp_source_;
293 std::unique_ptr<test::Packet> packet_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800294 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000296 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297};
298
299// Allocating the static const so that it can be passed by reference.
300const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700301const size_t NetEqDecodingTest::kBlockSize8kHz;
302const size_t NetEqDecodingTest::kBlockSize16kHz;
303const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304const int NetEqDecodingTest::kInitSampleRateHz;
305
306NetEqDecodingTest::NetEqDecodingTest()
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000307 : clock_(0),
308 neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000309 config_(),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000310 output_sample_rate_(kInitSampleRateHz),
311 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000312 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313}
314
315void NetEqDecodingTest::SetUp() {
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000316 neteq_ = NetEq::Create(config_, &clock_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000317 NetEqNetworkStatistics stat;
318 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
319 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700321 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322}
323
324void NetEqDecodingTest::TearDown() {
325 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326}
327
Yves Gerey665174f2018-06-19 15:03:05 +0200328void NetEqDecodingTest::OpenInputFile(const std::string& rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000329 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330}
331
henrik.lundin6d8e0112016-03-04 10:34:21 -0800332void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 // Check if time to receive.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000334 while (packet_ && clock_.TimeInMilliseconds() >= packet_->time_ms()) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000335 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800336#ifndef WEBRTC_CODEC_ISAC
337 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700338 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800339#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200340 ASSERT_EQ(0,
341 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700342 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200343 rtc::ArrayView<const uint8_t>(
344 packet_->payload(), packet_->payload_length_bytes()),
345 static_cast<uint32_t>(packet_->time_ms() *
346 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 }
348 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700349 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 }
351
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000352 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700353 bool muted;
354 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
355 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800356 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
357 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
358 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
359 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
360 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800361 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362
363 // Increase time.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000364 clock_.AdvanceTimeMilliseconds(kTimeStepMs);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365}
366
minyue4f906772016-04-29 11:05:14 -0700367void NetEqDecodingTest::DecodeAndCompare(
368 const std::string& rtp_file,
369 const std::string& output_checksum,
370 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700371 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 OpenInputFile(rtp_file);
373
minyue4f906772016-04-29 11:05:14 -0700374 std::string ref_out_file =
375 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
376 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377
minyue4f906772016-04-29 11:05:14 -0700378 std::string stat_out_file =
379 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
380 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000381
henrik.lundin46ba49c2016-05-24 22:50:47 -0700382 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200384 uint64_t last_concealed_samples = 0;
385 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000386 while (packet_) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200387 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
389 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800390 ASSERT_NO_FATAL_FAILURE(Process());
Yves Gerey665174f2018-06-19 15:03:05 +0200391 ASSERT_NO_FATAL_FAILURE(
392 output.AddResult(out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393
394 // Query the network statistics API once per second
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000395 if (clock_.TimeInMilliseconds() % 1000 == 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700397 NetEqNetworkStatistics current_network_stats;
398 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
399 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
400
Henrik Lundinac0a5032017-09-25 12:22:46 +0200401 // Verify that liftime stats and network stats report similar loss
402 // concealment rates.
403 auto lifetime_stats = neteq_->GetLifetimeStatistics();
404 const uint64_t delta_concealed_samples =
405 lifetime_stats.concealed_samples - last_concealed_samples;
406 last_concealed_samples = lifetime_stats.concealed_samples;
407 const uint64_t delta_total_samples_received =
408 lifetime_stats.total_samples_received - last_total_samples_received;
409 last_total_samples_received = lifetime_stats.total_samples_received;
410 // The tolerance is 1% but expressed in Q14.
411 EXPECT_NEAR(
412 (delta_concealed_samples << 14) / delta_total_samples_received,
413 current_network_stats.expand_rate, (2 << 14) / 100.0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414 }
415 }
minyue4f906772016-04-29 11:05:14 -0700416
417 SCOPED_TRACE("Check output audio.");
418 output.VerifyChecksum(output_checksum);
419 SCOPED_TRACE("Check network stats.");
420 network_stats.VerifyChecksum(network_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000421}
422
423void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
424 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700425 RTPHeader* rtp_info) {
426 rtp_info->sequenceNumber = frame_index;
427 rtp_info->timestamp = timestamp;
428 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
429 rtp_info->payloadType = 94; // PCM16b WB codec.
430 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000431}
432
433void NetEqDecodingTest::PopulateCng(int frame_index,
434 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700435 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000436 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000437 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700438 rtp_info->sequenceNumber = frame_index;
439 rtp_info->timestamp = timestamp;
440 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
441 rtp_info->payloadType = 98; // WB CNG.
442 rtp_info->markerBit = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200443 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000444 *payload_len = 1; // Only noise level, no spectral parameters.
445}
446
ivoc72c08ed2016-01-20 07:26:24 -0800447#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
448 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +0100449 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800450#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700451#else
minyue5f026d02015-12-16 07:36:04 -0800452#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700453#endif
minyue5f026d02015-12-16 07:36:04 -0800454TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800455 const std::string input_rtp_file =
456 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000457
Yves Gerey665174f2018-06-19 15:03:05 +0200458 const std::string output_checksum =
Alessio Bazzica5b728cc2019-09-05 11:59:35 +0000459 PlatformChecksum("998be2e5a707e636af0b6298f54bedfabe72aae1",
460 "61e238ece4cd3b67d66a0b7047e06b20607dcb79", "not used",
461 "998be2e5a707e636af0b6298f54bedfabe72aae1",
462 "4116ac2a6e75baac3194b712d6fabe28b384275e");
minyue4f906772016-04-29 11:05:14 -0700463
henrik.lundin2979f552017-05-05 05:04:16 -0700464 const std::string network_stats_checksum =
Alessio Bazzica5b728cc2019-09-05 11:59:35 +0000465 PlatformChecksum("5e5230b2d5042eccd197dac29edade1cc233586c",
466 "2183564f11b53259ba7f86f48f4df3d7d653c678", "not used",
467 "5e5230b2d5042eccd197dac29edade1cc233586c",
468 "5e5230b2d5042eccd197dac29edade1cc233586c");
minyue4f906772016-04-29 11:05:14 -0700469
Yves Gerey665174f2018-06-19 15:03:05 +0200470 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200471 absl::GetFlag(FLAGS_gen_ref));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000472}
473
Yves Gerey665174f2018-06-19 15:03:05 +0200474#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200475 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800476#define MAYBE_TestOpusBitExactness TestOpusBitExactness
477#else
478#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
479#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200480TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800481 const std::string input_rtp_file =
482 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800483
Yves Gereya038e712018-11-14 10:45:50 +0100484 // Checksum depends on libopus being compiled with or without SSE.
485 const std::string maybe_sse =
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200486 "6b602683ca7285a98118b4824d72f4257952c18f|"
487 "eb0b68bddcac00fc85403df64f83126f8ea9bc93";
Yves Gerey110a4de2019-08-12 17:19:09 +0200488 // The neon implementation may differ.
489 const std::string maybe_neon =
490 "f95f2a220c9ca5d60b81c4653d46e0de2bee159f|"
491 "63651b8cc7711a66c9491d6b6ce94b774b64a0ce";
Yves Gereya038e712018-11-14 10:45:50 +0100492 const std::string output_checksum = PlatformChecksum(
Yves Gerey110a4de2019-08-12 17:19:09 +0200493 maybe_sse, maybe_neon, "6f288a03d34958f62496f18fa85655593eef4dbe",
494 maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700495
Yves Gerey110a4de2019-08-12 17:19:09 +0200496 // The neon implementation may differ.
497 const std::string stats_maybe_neon =
498 "a71dce66c7bea85ba22d4e29a5298f606f810444|"
Jakob Ivarsson65024d92019-08-30 15:37:07 +0200499 "6b8c29e39c82f5479f59726744d0cf3e88e725d3";
Yves Gerey110a4de2019-08-12 17:19:09 +0200500 const std::string network_stats_checksum = PlatformChecksum(
Jakob Ivarsson65024d92019-08-30 15:37:07 +0200501 "87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544", stats_maybe_neon,
502 "c876f2a04c4f0a91da7f084f80e87871b7c5a4a1",
503 "87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544",
504 "87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544");
minyue4f906772016-04-29 11:05:14 -0700505
Yves Gerey665174f2018-06-19 15:03:05 +0200506 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200507 absl::GetFlag(FLAGS_gen_ref));
minyue93c08b72015-12-22 09:57:41 -0800508}
509
Yves Gerey665174f2018-06-19 15:03:05 +0200510#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100511 defined(WEBRTC_CODEC_OPUS)
512#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
513#else
514#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
515#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100516TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100517 const std::string input_rtp_file =
518 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
519
Yves Gereya038e712018-11-14 10:45:50 +0100520 const std::string maybe_sse =
521 "713af6c92881f5aab1285765ee6680da9d1c06ce|"
522 "2ac10c4e79aeedd0df2863b079da5848b40f00b5";
523 const std::string output_checksum = PlatformChecksum(
524 maybe_sse, "3ec991b96872123f1554c03c543ca5d518431e46",
525 "da9f9a2d94e0c2d67342fad4965d7b91cda50b25", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100526
527 const std::string network_stats_checksum =
Jakob Ivarsson65024d92019-08-30 15:37:07 +0200528 "8caf49765f35b6862066d3f17531ce44d8e25f60";
Henrik Lundine9619f82017-11-27 14:05:27 +0100529
Henrik Lundine9619f82017-11-27 14:05:27 +0100530 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200531 absl::GetFlag(FLAGS_gen_ref));
Henrik Lundine9619f82017-11-27 14:05:27 +0100532}
533
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000534// Use fax mode to avoid time-scaling. This is to simplify the testing of
535// packet waiting times in the packet buffer.
536class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
537 protected:
538 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200539 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000540 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200541 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000542};
543
544TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
546 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000547 const size_t kSamples = 10 * 16;
548 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000549 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800550 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700551 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200552 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
553 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700554 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
555 rtp_info.payloadType = 94; // PCM16b WB codec.
556 rtp_info.markerBit = 0;
557 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 }
559 // Pull out all data.
560 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700561 bool muted;
562 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800563 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564 }
565
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200566 NetEqNetworkStatistics stats;
567 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000568 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
569 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200570 // each packet. Thus, we are calculating the statistics for a series from 10
571 // to 300, in steps of 10 ms.
572 EXPECT_EQ(155, stats.mean_waiting_time_ms);
573 EXPECT_EQ(155, stats.median_waiting_time_ms);
574 EXPECT_EQ(10, stats.min_waiting_time_ms);
575 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000576
577 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200578 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
579 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
580 EXPECT_EQ(-1, stats.median_waiting_time_ms);
581 EXPECT_EQ(-1, stats.min_waiting_time_ms);
582 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000583}
584
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000585void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
586 double network_freeze_ms,
587 bool pull_audio_during_freeze,
588 int delay_tolerance_ms,
589 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000590 uint16_t seq_no = 0;
591 uint32_t timestamp = 0;
592 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000593 const size_t kSamples = kFrameSizeMs * 16;
594 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 double next_input_time_ms = 0.0;
596 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700597 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598
599 // Insert speech for 5 seconds.
600 const int kSpeechDurationMs = 5000;
601 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
602 // Each turn in this for loop is 10 ms.
603 while (next_input_time_ms <= t_ms) {
604 // Insert one 30 ms speech frame.
605 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700606 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700608 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609 ++seq_no;
610 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000611 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 }
613 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700614 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800615 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 }
617
henrik.lundin55480f52016-03-08 02:37:57 -0800618 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200619 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700620 ASSERT_TRUE(playout_timestamp);
621 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622
623 // Insert CNG for 1 minute (= 60000 ms).
624 const int kCngPeriodMs = 100;
625 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
626 const int kCngDurationMs = 60000;
627 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
628 // Each turn in this for loop is 10 ms.
629 while (next_input_time_ms <= t_ms) {
630 // Insert one CNG frame each 100 ms.
631 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000632 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700633 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000634 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800635 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700636 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800637 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 ++seq_no;
639 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000640 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000641 }
642 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700643 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800644 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000645 }
646
henrik.lundin55480f52016-03-08 02:37:57 -0800647 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000648
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000649 if (network_freeze_ms > 0) {
650 // First keep pulling audio for |network_freeze_ms| without inserting
651 // any data, then insert CNG data corresponding to |network_freeze_ms|
652 // without pulling any output audio.
653 const double loop_end_time = t_ms + network_freeze_ms;
654 for (; t_ms < loop_end_time; t_ms += 10) {
655 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700656 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800657 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800658 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000659 }
660 bool pull_once = pull_audio_during_freeze;
661 // If |pull_once| is true, GetAudio will be called once half-way through
662 // the network recovery period.
663 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
664 while (next_input_time_ms <= t_ms) {
665 if (pull_once && next_input_time_ms >= pull_time_ms) {
666 pull_once = false;
667 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700668 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800669 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800670 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000671 t_ms += 10;
672 }
673 // Insert one CNG frame each 100 ms.
674 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000675 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700676 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000677 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800678 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700679 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800680 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000681 ++seq_no;
682 timestamp += kCngPeriodSamples;
683 next_input_time_ms += kCngPeriodMs * drift_factor;
684 }
685 }
686
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000687 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000688 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800689 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000690 // Each turn in this for loop is 10 ms.
691 while (next_input_time_ms <= t_ms) {
692 // Insert one 30 ms speech frame.
693 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700694 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000695 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700696 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000697 ++seq_no;
698 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000699 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700 }
701 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700702 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800703 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000704 // Increase clock.
705 t_ms += 10;
706 }
707
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 // Check that the speech starts again within reasonable time.
709 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
710 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700711 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700712 ASSERT_TRUE(playout_timestamp);
713 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
716 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000717}
718
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000719TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000720 // Apply a clock drift of -25 ms / s (sender faster than receiver).
721 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000722 const double kNetworkFreezeTimeMs = 0.0;
723 const bool kGetAudioDuringFreezeRecovery = false;
724 const int kDelayToleranceMs = 20;
725 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200726 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
727 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000728 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000729}
730
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000731TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000732 // Apply a clock drift of +25 ms / s (sender slower than receiver).
733 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000734 const double kNetworkFreezeTimeMs = 0.0;
735 const bool kGetAudioDuringFreezeRecovery = false;
Alessio Bazzica5b728cc2019-09-05 11:59:35 +0000736 const int kDelayToleranceMs = 20;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000737 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200738 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
739 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 kMaxTimeToSpeechMs);
741}
742
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000743TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000744 // Apply a clock drift of -25 ms / s (sender faster than receiver).
745 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
746 const double kNetworkFreezeTimeMs = 5000.0;
747 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200748 const int kDelayToleranceMs = 60;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000749 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200750 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
751 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000752 kMaxTimeToSpeechMs);
753}
754
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000755TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000756 // Apply a clock drift of +25 ms / s (sender slower than receiver).
757 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
758 const double kNetworkFreezeTimeMs = 5000.0;
759 const bool kGetAudioDuringFreezeRecovery = false;
Alessio Bazzica5b728cc2019-09-05 11:59:35 +0000760 const int kDelayToleranceMs = 20;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000761 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200762 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
763 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000764 kMaxTimeToSpeechMs);
765}
766
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000767TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000768 // Apply a clock drift of +25 ms / s (sender slower than receiver).
769 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
770 const double kNetworkFreezeTimeMs = 5000.0;
771 const bool kGetAudioDuringFreezeRecovery = true;
Alessio Bazzica5b728cc2019-09-05 11:59:35 +0000772 const int kDelayToleranceMs = 20;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000773 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200774 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
775 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000776 kMaxTimeToSpeechMs);
777}
778
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000779TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000780 const double kDriftFactor = 1.0; // No drift.
781 const double kNetworkFreezeTimeMs = 0.0;
782 const bool kGetAudioDuringFreezeRecovery = false;
783 const int kDelayToleranceMs = 10;
784 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200785 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
786 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000787 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000788}
789
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000790TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000791 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000792 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700793 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000794 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700795 rtp_info.payloadType = 1; // Not registered as a decoder.
796 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000797}
798
Peter Boströme2976c82016-01-04 22:44:05 +0100799#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800800#define MAYBE_DecoderError DecoderError
801#else
802#define MAYBE_DecoderError DISABLED_DecoderError
803#endif
804
Peter Boströme2976c82016-01-04 22:44:05 +0100805TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000806 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000807 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700808 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000809 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700810 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
811 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000812 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
813 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700814 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800815 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700816 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000817 }
henrik.lundin7a926812016-05-12 13:51:28 -0700818 bool muted;
819 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
820 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800821
yujo36b1a5f2017-06-12 12:45:32 -0700822 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000823 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700824 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000825 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200826 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000827 ss << "i = " << i;
828 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700829 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000830 }
831}
832
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000833TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000834 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
835 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700836 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800837 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700838 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000839 }
henrik.lundin7a926812016-05-12 13:51:28 -0700840 bool muted;
841 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
842 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000843 // Verify that the first block of samples is set to 0.
844 static const int kExpectedOutputLength =
845 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700846 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200848 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 ss << "i = " << i;
850 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700851 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000852 }
henrik.lundind89814b2015-11-23 06:49:25 -0800853 // Verify that the sample rate did not change from the initial configuration.
854 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000855}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000856
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000857class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000858 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000859 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700860 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000861 uint8_t payload_type = 0xFF; // Invalid.
862 if (sampling_rate_hz == 8000) {
863 expected_samples_per_channel = kBlockSize8kHz;
864 payload_type = 93; // PCM 16, 8 kHz.
865 } else if (sampling_rate_hz == 16000) {
866 expected_samples_per_channel = kBlockSize16kHz;
867 payload_type = 94; // PCM 16, 16 kHZ.
868 } else if (sampling_rate_hz == 32000) {
869 expected_samples_per_channel = kBlockSize32kHz;
870 payload_type = 95; // PCM 16, 32 kHz.
871 } else {
872 ASSERT_TRUE(false); // Unsupported test case.
873 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000874
henrik.lundin6d8e0112016-03-04 10:34:21 -0800875 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000876 test::AudioLoop input;
877 // We are using the same 32 kHz input file for all tests, regardless of
878 // |sampling_rate_hz|. The output may sound weird, but the test is still
879 // valid.
880 ASSERT_TRUE(input.Init(
881 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
882 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700883 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000884
885 // Payload of 10 ms of PCM16 32 kHz.
886 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700887 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000888 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700889 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000890
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000891 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700892 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000893 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800894 auto block = input.GetNextBlock();
895 ASSERT_EQ(expected_samples_per_channel, block.size());
896 size_t enc_len_bytes =
897 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000898 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
899
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200900 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700901 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200902 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
903 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800904 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700905 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800906 ASSERT_EQ(1u, output.num_channels_);
907 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800908 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000909
910 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200911 rtp_info.timestamp +=
912 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700913 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200914 receive_timestamp +=
915 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000916 }
917
henrik.lundin6d8e0112016-03-04 10:34:21 -0800918 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000919
920 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
921 // one frame without checking speech-type. This is the first frame pulled
922 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700923 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800924 ASSERT_EQ(1u, output.num_channels_);
925 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000926
927 // To be able to test the fading of background noise we need at lease to
928 // pull 611 frames.
929 const int kFadingThreshold = 611;
930
931 // Test several CNG-to-PLC packet for the expected behavior. The number 20
932 // is arbitrary, but sufficiently large to test enough number of frames.
933 const int kNumPlcToCngTestFrames = 20;
934 bool plc_to_cng = false;
935 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800936 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700937 // Set to non-zero.
938 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700939 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
940 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800941 ASSERT_EQ(1u, output.num_channels_);
942 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800943 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000944 plc_to_cng = true;
945 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700946 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800947 for (size_t k = 0;
948 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -0700949 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +0200950 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000951 } else {
henrik.lundin55480f52016-03-08 02:37:57 -0800952 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000953 }
954 }
955 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
956 }
957};
958
Henrik Lundin67190172018-04-20 15:34:48 +0200959TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000960 CheckBgn(8000);
961 CheckBgn(16000);
962 CheckBgn(32000);
963}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000964
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000965void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
966 uint32_t start_timestamp,
967 const std::set<uint16_t>& drop_seq_numbers,
968 bool expect_seq_no_wrap,
969 bool expect_timestamp_wrap) {
970 uint16_t seq_no = start_seq_no;
971 uint32_t timestamp = start_timestamp;
972 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
973 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
974 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000975 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000976 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000977 uint32_t receive_timestamp = 0;
978
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000979 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000980 const int kSpeechDurationMs = 2000;
981 int packets_inserted = 0;
982 uint16_t last_seq_no;
983 uint32_t last_timestamp;
984 bool timestamp_wrapped = false;
985 bool seq_no_wrapped = false;
986 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
987 // Each turn in this for loop is 10 ms.
988 while (next_input_time_ms <= t_ms) {
989 // Insert one 30 ms speech frame.
990 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700991 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000992 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
993 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
994 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700995 ASSERT_EQ(0,
996 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000997 ++packets_inserted;
998 }
999 NetEqNetworkStatistics network_stats;
1000 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1001
1002 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1003 // packet size for first few packets. Therefore we refrain from checking
1004 // the criteria.
1005 if (packets_inserted > 4) {
1006 // Expect preferred and actual buffer size to be no more than 2 frames.
1007 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
Yves Gerey665174f2018-06-19 15:03:05 +02001008 EXPECT_LE(network_stats.current_buffer_size_ms,
1009 kFrameSizeMs * 2 + algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001010 }
1011 last_seq_no = seq_no;
1012 last_timestamp = timestamp;
1013
1014 ++seq_no;
1015 timestamp += kSamples;
1016 receive_timestamp += kSamples;
1017 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1018
1019 seq_no_wrapped |= seq_no < last_seq_no;
1020 timestamp_wrapped |= timestamp < last_timestamp;
1021 }
1022 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001023 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001024 bool muted;
1025 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001026 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1027 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001028
1029 // Expect delay (in samples) to be less than 2 packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +02001030 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001031 ASSERT_TRUE(playout_timestamp);
1032 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001033 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001034 }
1035 // Make sure we have actually tested wrap-around.
1036 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1037 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1038}
1039
1040TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1041 // Start with a sequence number that will soon wrap.
1042 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1043 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1044}
1045
1046TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1047 // Start with a sequence number that will soon wrap.
1048 std::set<uint16_t> drop_seq_numbers;
1049 drop_seq_numbers.insert(0xFFFF);
1050 drop_seq_numbers.insert(0x0);
1051 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1052}
1053
1054TEST_F(NetEqDecodingTest, TimestampWrap) {
1055 // Start with a timestamp that will soon wrap.
1056 std::set<uint16_t> drop_seq_numbers;
1057 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1058}
1059
1060TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1061 // Start with a timestamp and a sequence number that will wrap at the same
1062 // time.
1063 std::set<uint16_t> drop_seq_numbers;
1064 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1065}
1066
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001067void NetEqDecodingTest::DuplicateCng() {
1068 uint16_t seq_no = 0;
1069 uint32_t timestamp = 0;
1070 const int kFrameSizeMs = 10;
1071 const int kSampleRateKhz = 16;
1072 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001073 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001074
Yves Gerey665174f2018-06-19 15:03:05 +02001075 const int algorithmic_delay_samples =
1076 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001077 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001078 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001079 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001080 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001081 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001082 for (int i = 0; i < 3; ++i) {
1083 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001084 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001085 ++seq_no;
1086 timestamp += kSamples;
1087
1088 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001089 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001090 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001091 }
1092 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001093 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001094
1095 // Insert same CNG packet twice.
1096 const int kCngPeriodMs = 100;
1097 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001098 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001099 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1100 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001101 ASSERT_EQ(
1102 0, neteq_->InsertPacket(
1103 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001104
1105 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001106 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001107 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001108 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001109 EXPECT_FALSE(
1110 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001111 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1112 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001113
1114 // Insert the same CNG packet again. Note that at this point it is old, since
1115 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001116 ASSERT_EQ(
1117 0, neteq_->InsertPacket(
1118 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001119
1120 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1121 // we have already pulled out CNG once.
1122 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001123 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001124 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001125 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001126 EXPECT_FALSE(
1127 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001128 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001129 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001130 }
1131
1132 // Insert speech again.
1133 ++seq_no;
1134 timestamp += kCngPeriodSamples;
1135 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001136 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001137
1138 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001139 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001140 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001141 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +02001142 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001143 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001144 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001145 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001146}
1147
Yves Gerey665174f2018-06-19 15:03:05 +02001148TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
1149 DuplicateCng();
1150}
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001151
1152TEST_F(NetEqDecodingTest, CngFirst) {
1153 uint16_t seq_no = 0;
1154 uint32_t timestamp = 0;
1155 const int kFrameSizeMs = 10;
1156 const int kSampleRateKhz = 16;
1157 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1158 const int kPayloadBytes = kSamples * 2;
1159 const int kCngPeriodMs = 100;
1160 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1161 size_t payload_len;
1162
1163 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001164 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001165
1166 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001167 ASSERT_EQ(
1168 NetEq::kOK,
1169 neteq_->InsertPacket(
1170 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001171 ++seq_no;
1172 timestamp += kCngPeriodSamples;
1173
1174 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001175 bool muted;
1176 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001177 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001178 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001179
1180 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001181 const uint32_t first_speech_timestamp = timestamp;
1182 int timeout_counter = 0;
1183 do {
1184 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001185 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001186 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001187 ++seq_no;
1188 timestamp += kSamples;
1189
1190 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001191 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001192 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001193 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001194 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001195 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001196}
henrik.lundin7a926812016-05-12 13:51:28 -07001197
1198class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1199 public:
1200 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1201 config_.enable_muted_state = true;
1202 }
1203
1204 protected:
1205 static constexpr size_t kSamples = 10 * 16;
1206 static constexpr size_t kPayloadBytes = kSamples * 2;
1207
1208 void InsertPacket(uint32_t rtp_timestamp) {
1209 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001210 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001211 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001212 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001213 }
1214
henrik.lundin42feb512016-09-20 06:51:40 -07001215 void InsertCngPacket(uint32_t rtp_timestamp) {
1216 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001217 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001218 size_t payload_len;
1219 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001220 EXPECT_EQ(
1221 NetEq::kOK,
1222 neteq_->InsertPacket(
1223 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001224 }
1225
henrik.lundin7a926812016-05-12 13:51:28 -07001226 bool GetAudioReturnMuted() {
1227 bool muted;
1228 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1229 return muted;
1230 }
1231
1232 void GetAudioUntilMuted() {
1233 while (!GetAudioReturnMuted()) {
1234 ASSERT_LT(counter_++, 1000) << "Test timed out";
1235 }
1236 }
1237
1238 void GetAudioUntilNormal() {
1239 bool muted = false;
1240 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1241 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1242 ASSERT_LT(counter_++, 1000) << "Test timed out";
1243 }
1244 EXPECT_FALSE(muted);
1245 }
1246
1247 int counter_ = 0;
1248};
1249
1250// Verifies that NetEq goes in and out of muted state as expected.
1251TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1252 // Insert one speech packet.
1253 InsertPacket(0);
1254 // Pull out audio once and expect it not to be muted.
1255 EXPECT_FALSE(GetAudioReturnMuted());
1256 // Pull data until faded out.
1257 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001258 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001259
1260 // Verify that output audio is not written during muted mode. Other parameters
1261 // should be correct, though.
1262 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001263 int16_t* frame_data = new_frame.mutable_data();
1264 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1265 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001266 }
1267 bool muted;
1268 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1269 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001270 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001271 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1272 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001273 }
1274 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1275 new_frame.timestamp_);
1276 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1277 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1278 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1279 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1280 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1281
1282 // Insert new data. Timestamp is corrected for the time elapsed since the last
1283 // packet. Verify that normal operation resumes.
1284 InsertPacket(kSamples * counter_);
1285 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001286 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001287
1288 NetEqNetworkStatistics stats;
1289 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1290 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1291 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1292 // concealment samples in this test.
1293 EXPECT_GT(stats.expand_rate, 14000);
1294 // And, it should be greater than the speech_expand_rate.
1295 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001296}
1297
1298// Verifies that NetEq goes out of muted state when given a delayed packet.
1299TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1300 // Insert one speech packet.
1301 InsertPacket(0);
1302 // Pull out audio once and expect it not to be muted.
1303 EXPECT_FALSE(GetAudioReturnMuted());
1304 // Pull data until faded out.
1305 GetAudioUntilMuted();
1306 // Insert new data. Timestamp is only corrected for the half of the time
1307 // elapsed since the last packet. That is, the new packet is delayed. Verify
1308 // that normal operation resumes.
1309 InsertPacket(kSamples * counter_ / 2);
1310 GetAudioUntilNormal();
1311}
1312
1313// Verifies that NetEq goes out of muted state when given a future packet.
1314TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1315 // Insert one speech packet.
1316 InsertPacket(0);
1317 // Pull out audio once and expect it not to be muted.
1318 EXPECT_FALSE(GetAudioReturnMuted());
1319 // Pull data until faded out.
1320 GetAudioUntilMuted();
1321 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1322 // last packet. That is, the new packet is too early. Verify that normal
1323 // operation resumes.
1324 InsertPacket(kSamples * counter_ * 2);
1325 GetAudioUntilNormal();
1326}
1327
1328// Verifies that NetEq goes out of muted state when given an old packet.
1329TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1330 // Insert one speech packet.
1331 InsertPacket(0);
1332 // Pull out audio once and expect it not to be muted.
1333 EXPECT_FALSE(GetAudioReturnMuted());
1334 // Pull data until faded out.
1335 GetAudioUntilMuted();
1336
1337 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1338 // Insert packet which is older than the first packet.
1339 InsertPacket(kSamples * (counter_ - 1000));
1340 EXPECT_FALSE(GetAudioReturnMuted());
1341 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1342}
1343
henrik.lundin42feb512016-09-20 06:51:40 -07001344// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1345// packet stream is suspended for a long time.
1346TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1347 // Insert one CNG packet.
1348 InsertCngPacket(0);
1349
1350 // Pull 10 seconds of audio (10 ms audio generated per lap).
1351 for (int i = 0; i < 1000; ++i) {
1352 bool muted;
1353 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1354 ASSERT_FALSE(muted);
1355 }
1356 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1357}
1358
1359// Verifies that NetEq goes back to normal after a long CNG period with the
1360// packet stream suspended.
1361TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1362 // Insert one CNG packet.
1363 InsertCngPacket(0);
1364
1365 // Pull 10 seconds of audio (10 ms audio generated per lap).
1366 for (int i = 0; i < 1000; ++i) {
1367 bool muted;
1368 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1369 }
1370
1371 // Insert new data. Timestamp is corrected for the time elapsed since the last
1372 // packet. Verify that normal operation resumes.
1373 InsertPacket(kSamples * counter_);
1374 GetAudioUntilNormal();
1375}
1376
henrik.lundin7a926812016-05-12 13:51:28 -07001377class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1378 public:
1379 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1380
1381 void SetUp() override {
1382 NetEqDecodingTest::SetUp();
1383 config2_ = config_;
1384 }
1385
1386 void CreateSecondInstance() {
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001387 neteq2_.reset(
1388 NetEq::Create(config2_, &clock_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001389 ASSERT_TRUE(neteq2_);
1390 LoadDecoders(neteq2_.get());
1391 }
1392
1393 protected:
1394 std::unique_ptr<NetEq> neteq2_;
1395 NetEq::Config config2_;
1396};
1397
1398namespace {
1399::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1400 const AudioFrame& b) {
1401 if (a.timestamp_ != b.timestamp_)
1402 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1403 << " != " << b.timestamp_ << ")";
1404 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +02001405 return ::testing::AssertionFailure()
1406 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
1407 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001408 if (a.samples_per_channel_ != b.samples_per_channel_)
1409 return ::testing::AssertionFailure()
1410 << "samples_per_channel_ diff (" << a.samples_per_channel_
1411 << " != " << b.samples_per_channel_ << ")";
1412 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +02001413 return ::testing::AssertionFailure()
1414 << "num_channels_ diff (" << a.num_channels_
1415 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001416 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +02001417 return ::testing::AssertionFailure()
1418 << "speech_type_ diff (" << a.speech_type_
1419 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001420 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +02001421 return ::testing::AssertionFailure()
1422 << "vad_activity_ diff (" << a.vad_activity_
1423 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001424 return ::testing::AssertionSuccess();
1425}
1426
1427::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1428 const AudioFrame& b) {
1429 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1430 if (!res)
1431 return res;
Yves Gerey665174f2018-06-19 15:03:05 +02001432 if (memcmp(a.data(), b.data(),
1433 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
1434 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001435 return ::testing::AssertionFailure() << "data_ diff";
1436 }
1437 return ::testing::AssertionSuccess();
1438}
1439
1440} // namespace
1441
1442TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1443 ASSERT_FALSE(config_.enable_muted_state);
1444 config2_.enable_muted_state = true;
1445 CreateSecondInstance();
1446
1447 // Insert one speech packet into both NetEqs.
1448 const size_t kSamples = 10 * 16;
1449 const size_t kPayloadBytes = kSamples * 2;
1450 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001451 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001452 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001453 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1454 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001455
1456 AudioFrame out_frame1, out_frame2;
1457 bool muted;
1458 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001459 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001460 ss << "i = " << i;
1461 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1462 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1463 EXPECT_FALSE(muted);
1464 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1465 if (muted) {
1466 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1467 } else {
1468 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1469 }
1470 }
1471 EXPECT_TRUE(muted);
1472
1473 // Insert new data. Timestamp is corrected for the time elapsed since the last
1474 // packet.
1475 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001476 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1477 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001478
1479 int counter = 0;
1480 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1481 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +02001482 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001483 ss << "counter = " << counter;
1484 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1485 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1486 EXPECT_FALSE(muted);
1487 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1488 if (muted) {
1489 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1490 } else {
1491 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1492 }
1493 }
1494 EXPECT_FALSE(muted);
1495}
1496
henrik.lundin114c1b32017-04-26 07:47:32 -07001497TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1498 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1499
1500 // Pull out data once.
1501 AudioFrame output;
1502 bool muted;
1503 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1504
1505 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1506}
1507
1508TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1509 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1510 // default). Make the length 10 ms.
1511 constexpr size_t kPayloadSamples = 16 * 10;
1512 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1513 uint8_t payload[kPayloadBytes] = {0};
1514
1515 RTPHeader rtp_info;
1516 constexpr uint32_t kRtpTimestamp = 0x1234;
1517 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1518 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1519
1520 // Pull out data once.
1521 AudioFrame output;
1522 bool muted;
1523 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1524
1525 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1526 neteq_->LastDecodedTimestamps());
1527
1528 // Nothing decoded on the second call.
1529 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1530 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1531}
1532
1533TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1534 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1535 // by default). Make the length 5 ms so that NetEq must decode them both in
1536 // the same GetAudio call.
1537 constexpr size_t kPayloadSamples = 16 * 5;
1538 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1539 uint8_t payload[kPayloadBytes] = {0};
1540
1541 RTPHeader rtp_info;
1542 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1543 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1544 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1545 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1546 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1547 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1548
1549 // Pull out data once.
1550 AudioFrame output;
1551 bool muted;
1552 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1553
1554 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1555 neteq_->LastDecodedTimestamps());
1556}
1557
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001558TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1559 const int kNumConcealmentEvents = 19;
1560 const size_t kSamples = 10 * 16;
1561 const size_t kPayloadBytes = kSamples * 2;
1562 int seq_no = 0;
1563 RTPHeader rtp_info;
1564 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1565 rtp_info.payloadType = 94; // PCM16b WB codec.
1566 rtp_info.markerBit = 0;
1567 const uint8_t payload[kPayloadBytes] = {0};
1568 bool muted;
1569
1570 for (int i = 0; i < kNumConcealmentEvents; i++) {
1571 // Insert some packets of 10 ms size.
1572 for (int j = 0; j < 10; j++) {
1573 rtp_info.sequenceNumber = seq_no++;
1574 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1575 neteq_->InsertPacket(rtp_info, payload, 0);
1576 neteq_->GetAudio(&out_frame_, &muted);
1577 }
1578
1579 // Lose a number of packets.
1580 int num_lost = 1 + i;
1581 for (int j = 0; j < num_lost; j++) {
1582 seq_no++;
1583 neteq_->GetAudio(&out_frame_, &muted);
1584 }
1585 }
1586
1587 // Check number of concealment events.
1588 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1589 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1590}
1591
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001592// Test that the jitter buffer delay stat is computed correctly.
1593void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1594 const int kNumPackets = 10;
1595 const int kDelayInNumPackets = 2;
1596 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1597 const size_t kSamples = kPacketLenMs * 16;
1598 const size_t kPayloadBytes = kSamples * 2;
1599 RTPHeader rtp_info;
1600 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1601 rtp_info.payloadType = 94; // PCM16b WB codec.
1602 rtp_info.markerBit = 0;
1603 const uint8_t payload[kPayloadBytes] = {0};
1604 bool muted;
1605 int packets_sent = 0;
1606 int packets_received = 0;
1607 int expected_delay = 0;
Chen Xing0acffb52019-01-15 15:46:29 +01001608 uint64_t expected_emitted_count = 0;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001609 while (packets_received < kNumPackets) {
1610 // Insert packet.
1611 if (packets_sent < kNumPackets) {
1612 rtp_info.sequenceNumber = packets_sent++;
1613 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1614 neteq_->InsertPacket(rtp_info, payload, 0);
1615 }
1616
1617 // Get packet.
1618 if (packets_sent > kDelayInNumPackets) {
1619 neteq_->GetAudio(&out_frame_, &muted);
1620 packets_received++;
1621
1622 // The delay reported by the jitter buffer never exceeds
1623 // the number of samples previously fetched with GetAudio
1624 // (hence the min()).
1625 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1626
1627 // The increase of the expected delay is the product of
1628 // the current delay of the jitter buffer in ms * the
1629 // number of samples that are sent for play out.
1630 int current_delay_ms = packets_delay * kPacketLenMs;
1631 expected_delay += current_delay_ms * kSamples;
Chen Xing0acffb52019-01-15 15:46:29 +01001632 expected_emitted_count += kSamples;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001633 }
1634 }
1635
1636 if (apply_packet_loss) {
1637 // Extra call to GetAudio to cause concealment.
1638 neteq_->GetAudio(&out_frame_, &muted);
1639 }
1640
1641 // Check jitter buffer delay.
1642 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1643 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
Chen Xing0acffb52019-01-15 15:46:29 +01001644 EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001645}
1646
1647TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1648 TestJitterBufferDelay(false);
1649}
1650
1651TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1652 TestJitterBufferDelay(true);
1653}
1654
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001655TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithAcceleration) {
1656 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1657 const size_t kSamples = kPacketLenMs * 16;
1658 const size_t kPayloadBytes = kSamples * 2;
1659 RTPHeader rtp_info;
1660 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1661 rtp_info.payloadType = 94; // PCM16b WB codec.
1662 rtp_info.markerBit = 0;
1663 const uint8_t payload[kPayloadBytes] = {0};
1664
1665 neteq_->InsertPacket(rtp_info, payload, 0);
1666
1667 bool muted;
1668 neteq_->GetAudio(&out_frame_, &muted);
1669
1670 rtp_info.sequenceNumber += 1;
1671 rtp_info.timestamp += kSamples;
1672 neteq_->InsertPacket(rtp_info, payload, 0);
1673 rtp_info.sequenceNumber += 1;
1674 rtp_info.timestamp += kSamples;
1675 neteq_->InsertPacket(rtp_info, payload, 0);
1676
1677 // We have two packets in the buffer and kAccelerate operation will
1678 // extract 20 ms of data.
1679 neteq_->GetAudio(&out_frame_, &muted, Operations::kAccelerate);
1680
1681 // Check jitter buffer delay.
1682 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1683 EXPECT_EQ(10 * kSamples * 3, stats.jitter_buffer_delay_ms);
1684 EXPECT_EQ(kSamples * 3, stats.jitter_buffer_emitted_count);
1685}
1686
Henrik Lundin7687ad52018-07-02 10:14:46 +02001687namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001688TEST(NetEqNoTimeStretchingMode, RunTest) {
1689 NetEq::Config config;
1690 config.for_test_no_time_stretching = true;
1691 auto codecs = NetEqTest::StandardDecoderMap();
Henrik Lundin7687ad52018-07-02 10:14:46 +02001692 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1693 {1, kRtpExtensionAudioLevel},
1694 {3, kRtpExtensionAbsoluteSendTime},
1695 {5, kRtpExtensionTransportSequenceNumber},
1696 {7, kRtpExtensionVideoContentType},
1697 {8, kRtpExtensionVideoTiming}};
1698 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1699 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001700 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001701 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1702 new TimeLimitedNetEqInput(std::move(input), 20000));
1703 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1704 NetEqTest::Callbacks callbacks;
Niels Möllerbd6dee82019-01-02 09:39:47 +01001705 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, nullptr,
1706 std::move(input_time_limit), std::move(output), callbacks);
Henrik Lundin7687ad52018-07-02 10:14:46 +02001707 test.Run();
1708 const auto stats = test.SimulationStats();
1709 EXPECT_EQ(0, stats.accelerate_rate);
1710 EXPECT_EQ(0, stats.preemptive_rate);
1711}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001712
1713} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001714} // namespace webrtc