blob: 3acb865ceefed0d607c04dcd64820deb7a401ae6 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020023#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
26#include "modules/audio_coding/neteq/tools/audio_loop.h"
Henrik Lundin7687ad52018-07-02 10:14:46 +020027#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
28#include "modules/audio_coding/neteq/tools/neteq_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "modules/include/module_common_types_public.h"
Niels Möller53382cb2018-11-27 14:05:08 +010031#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/ignore_wundef.h"
Joachim Bauch4e909192017-12-19 22:27:51 +010034#include "rtc_base/messagedigest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/stringencode.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"
Henrik Lundine9619f82017-11-27 14:05:27 +010040#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "test/gtest.h"
42#include "test/testsupport/fileutils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Mirko Bonadei81ca3bf2018-01-09 09:40:39 +010044// This must come after test/gtest.h
45#include "rtc_base/flags.h" // NOLINT(build/include)
46
minyue5f026d02015-12-16 07:36:04 -080047#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070048RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080049#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
50#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
51#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080053#endif
kwiberg77eab702016-09-28 17:42:01 -070054RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080055#endif
56
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020057WEBRTC_DEFINE_bool(gen_ref, false, "Generate reference files.");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000058
kwiberg5adaf732016-10-04 09:33:27 -070059namespace webrtc {
60
minyue5f026d02015-12-16 07:36:04 -080061namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
minyue4f906772016-04-29 11:05:14 -070063const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020064 const std::string& checksum_android_32,
65 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070066 const std::string& checksum_win_32,
67 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070068#if defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +020069#ifdef WEBRTC_ARCH_64_BITS
70 return checksum_android_64;
71#else
72 return checksum_android_32;
73#endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070074#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020075#ifdef WEBRTC_ARCH_64_BITS
76 return checksum_win_64;
77#else
78 return checksum_win_32;
79#endif // WEBRTC_ARCH_64_BITS
minyue4f906772016-04-29 11:05:14 -070080#else
81 return checksum_general;
82#endif // WEBRTC_WIN
83}
84
minyue5f026d02015-12-16 07:36:04 -080085#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
86void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
87 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
88 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
89 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
90 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
91 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
minyue5f026d02015-12-16 07:36:04 -080092 stats->set_expand_rate(stats_raw.expand_rate);
93 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
94 stats->set_preemptive_rate(stats_raw.preemptive_rate);
95 stats->set_accelerate_rate(stats_raw.accelerate_rate);
96 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020097 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
minyue5f026d02015-12-16 07:36:04 -080098 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
99 stats->set_added_zero_samples(stats_raw.added_zero_samples);
100 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
101 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
102 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
103 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
104}
105
106void Convert(const webrtc::RtcpStatistics& stats_raw,
107 webrtc::neteq_unittest::RtcpStatistics* stats) {
108 stats->set_fraction_lost(stats_raw.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700109 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800110 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700111 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800112 stats->set_jitter(stats_raw.jitter);
113}
114
Yves Gerey665174f2018-06-19 15:03:05 +0200115void AddMessage(FILE* file,
116 rtc::MessageDigest* digest,
minyue4f906772016-04-29 11:05:14 -0700117 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800118 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700119 if (file)
120 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
121 digest->Update(&size, sizeof(size));
122
123 if (file)
124 ASSERT_EQ(static_cast<size_t>(size),
125 fwrite(message.data(), sizeof(char), size, file));
126 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800127}
128
minyue5f026d02015-12-16 07:36:04 -0800129#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
130
henrik.lundin7a926812016-05-12 13:51:28 -0700131void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700132 ASSERT_EQ(true,
133 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
134 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
135 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700136 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
137 "pcma", 8));
138#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700139 ASSERT_EQ(true,
140 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700141#endif
142#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700143 ASSERT_EQ(true,
144 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700145#endif
146#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700147 ASSERT_EQ(true,
148 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700149#endif
150#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700151 ASSERT_EQ(true,
152 neteq->RegisterPayloadType(
153 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700154#endif
kwiberg5adaf732016-10-04 09:33:27 -0700155 ASSERT_EQ(true,
156 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
157 ASSERT_EQ(true,
158 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
159 ASSERT_EQ(true,
160 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
161 ASSERT_EQ(true,
162 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
163 ASSERT_EQ(true,
164 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700165}
minyue5f026d02015-12-16 07:36:04 -0800166} // namespace
167
minyue4f906772016-04-29 11:05:14 -0700168class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 public:
minyue4f906772016-04-29 11:05:14 -0700170 explicit ResultSink(const std::string& output_file);
171 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000172
Yves Gerey665174f2018-06-19 15:03:05 +0200173 template <typename T>
174 void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700175
176 void AddResult(const NetEqNetworkStatistics& stats);
177 void AddResult(const RtcpStatistics& stats);
178
179 void VerifyChecksum(const std::string& ref_check_sum);
180
181 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700183 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184};
185
Joachim Bauch4e909192017-12-19 22:27:51 +0100186ResultSink::ResultSink(const std::string& output_file)
minyue4f906772016-04-29 11:05:14 -0700187 : output_fp_(nullptr),
Joachim Bauch4e909192017-12-19 22:27:51 +0100188 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 if (!output_file.empty()) {
190 output_fp_ = fopen(output_file.c_str(), "wb");
191 EXPECT_TRUE(output_fp_ != NULL);
192 }
193}
194
minyue4f906772016-04-29 11:05:14 -0700195ResultSink::~ResultSink() {
196 if (output_fp_)
197 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198}
199
Yves Gerey665174f2018-06-19 15:03:05 +0200200template <typename T>
yujo36b1a5f2017-06-12 12:45:32 -0700201void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700203 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 }
yujo36b1a5f2017-06-12 12:45:32 -0700205 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206}
207
minyue4f906772016-04-29 11:05:14 -0700208void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800209#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800210 neteq_unittest::NetEqNetworkStatistics stats;
211 Convert(stats_raw, &stats);
212
mbonadei7c2c8432017-04-07 00:59:12 -0700213 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800214 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700215 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800216#else
217 FAIL() << "Writing to reference file requires Proto Buffer.";
218#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000219}
220
minyue4f906772016-04-29 11:05:14 -0700221void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800222#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800223 neteq_unittest::RtcpStatistics stats;
224 Convert(stats_raw, &stats);
225
mbonadei7c2c8432017-04-07 00:59:12 -0700226 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800227 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700228 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800229#else
230 FAIL() << "Writing to reference file requires Proto Buffer.";
231#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232}
233
minyue4f906772016-04-29 11:05:14 -0700234void ResultSink::VerifyChecksum(const std::string& checksum) {
235 std::vector<char> buffer;
236 buffer.resize(digest_->Size());
237 digest_->Finish(&buffer[0], buffer.size());
238 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
Yves Gereya038e712018-11-14 10:45:50 +0100239 if (checksum.size() == result.size()) {
240 EXPECT_EQ(checksum, result);
241 } else {
242 // Check result is one the '|'-separated checksums.
243 EXPECT_NE(checksum.find(result), std::string::npos)
244 << result << " should be one of these:\n"
245 << checksum;
246 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000247}
248
249class NetEqDecodingTest : public ::testing::Test {
250 protected:
251 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
252 // constants below can be changed.
253 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700254 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
255 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
256 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800257 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258 static const int kInitSampleRateHz = 8000;
259
260 NetEqDecodingTest();
261 virtual void SetUp();
262 virtual void TearDown();
263 void SelectDecoders(NetEqDecoder* used_codec);
Yves Gerey665174f2018-06-19 15:03:05 +0200264 void OpenInputFile(const std::string& rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800265 void Process();
minyue5f026d02015-12-16 07:36:04 -0800266
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000267 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700268 const std::string& output_checksum,
269 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700270 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800271
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 static void PopulateRtpInfo(int frame_index,
273 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700274 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275 static void PopulateCng(int frame_index,
276 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700277 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000279 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280
Yves Gerey665174f2018-06-19 15:03:05 +0200281 void WrapTest(uint16_t start_seq_no,
282 uint32_t start_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000283 const std::set<uint16_t>& drop_seq_numbers,
Yves Gerey665174f2018-06-19 15:03:05 +0200284 bool expect_seq_no_wrap,
285 bool expect_timestamp_wrap);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000286
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000287 void LongCngWithClockDrift(double drift_factor,
288 double network_freeze_ms,
289 bool pull_audio_during_freeze,
290 int delay_tolerance_ms,
291 int max_time_to_speech_ms);
292
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000293 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000294
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000296 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800297 std::unique_ptr<test::RtpFileSource> rtp_source_;
298 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800300 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000302 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303};
304
305// Allocating the static const so that it can be passed by reference.
306const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700307const size_t NetEqDecodingTest::kBlockSize8kHz;
308const size_t NetEqDecodingTest::kBlockSize16kHz;
309const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310const int NetEqDecodingTest::kInitSampleRateHz;
311
312NetEqDecodingTest::NetEqDecodingTest()
313 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000314 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000316 output_sample_rate_(kInitSampleRateHz),
317 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000318 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319}
320
321void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700322 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000323 NetEqNetworkStatistics stat;
324 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
325 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700327 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328}
329
330void NetEqDecodingTest::TearDown() {
331 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332}
333
Yves Gerey665174f2018-06-19 15:03:05 +0200334void NetEqDecodingTest::OpenInputFile(const std::string& rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000335 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336}
337
henrik.lundin6d8e0112016-03-04 10:34:21 -0800338void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000340 while (packet_ && sim_clock_ >= packet_->time_ms()) {
341 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800342#ifndef WEBRTC_CODEC_ISAC
343 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700344 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800345#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200346 ASSERT_EQ(0,
347 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700348 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200349 rtc::ArrayView<const uint8_t>(
350 packet_->payload(), packet_->payload_length_bytes()),
351 static_cast<uint32_t>(packet_->time_ms() *
352 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 }
354 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700355 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 }
357
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000358 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700359 bool muted;
360 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
361 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800362 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
363 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
364 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
365 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
366 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800367 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368
369 // Increase time.
370 sim_clock_ += kTimeStepMs;
371}
372
minyue4f906772016-04-29 11:05:14 -0700373void NetEqDecodingTest::DecodeAndCompare(
374 const std::string& rtp_file,
375 const std::string& output_checksum,
376 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700377 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 OpenInputFile(rtp_file);
379
minyue4f906772016-04-29 11:05:14 -0700380 std::string ref_out_file =
381 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
382 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383
minyue4f906772016-04-29 11:05:14 -0700384 std::string stat_out_file =
385 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
386 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000387
henrik.lundin46ba49c2016-05-24 22:50:47 -0700388 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200390 uint64_t last_concealed_samples = 0;
391 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000392 while (packet_) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200393 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
395 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800396 ASSERT_NO_FATAL_FAILURE(Process());
Yves Gerey665174f2018-06-19 15:03:05 +0200397 ASSERT_NO_FATAL_FAILURE(
398 output.AddResult(out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399
400 // Query the network statistics API once per second
401 if (sim_clock_ % 1000 == 0) {
402 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700403 NetEqNetworkStatistics current_network_stats;
404 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
405 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
406
henrik.lundin9c3efd02015-08-27 13:12:22 -0700407 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700408 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
409 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410
Henrik Lundinac0a5032017-09-25 12:22:46 +0200411 // Verify that liftime stats and network stats report similar loss
412 // concealment rates.
413 auto lifetime_stats = neteq_->GetLifetimeStatistics();
414 const uint64_t delta_concealed_samples =
415 lifetime_stats.concealed_samples - last_concealed_samples;
416 last_concealed_samples = lifetime_stats.concealed_samples;
417 const uint64_t delta_total_samples_received =
418 lifetime_stats.total_samples_received - last_total_samples_received;
419 last_total_samples_received = lifetime_stats.total_samples_received;
420 // The tolerance is 1% but expressed in Q14.
421 EXPECT_NEAR(
422 (delta_concealed_samples << 14) / delta_total_samples_received,
423 current_network_stats.expand_rate, (2 << 14) / 100.0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000424 }
425 }
minyue4f906772016-04-29 11:05:14 -0700426
427 SCOPED_TRACE("Check output audio.");
428 output.VerifyChecksum(output_checksum);
429 SCOPED_TRACE("Check network stats.");
430 network_stats.VerifyChecksum(network_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000431}
432
433void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
434 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700435 RTPHeader* rtp_info) {
436 rtp_info->sequenceNumber = frame_index;
437 rtp_info->timestamp = timestamp;
438 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
439 rtp_info->payloadType = 94; // PCM16b WB codec.
440 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441}
442
443void NetEqDecodingTest::PopulateCng(int frame_index,
444 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700445 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000447 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700448 rtp_info->sequenceNumber = frame_index;
449 rtp_info->timestamp = timestamp;
450 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
451 rtp_info->payloadType = 98; // WB CNG.
452 rtp_info->markerBit = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200453 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000454 *payload_len = 1; // Only noise level, no spectral parameters.
455}
456
ivoc72c08ed2016-01-20 07:26:24 -0800457#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
458 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +0100459 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800460#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700461#else
minyue5f026d02015-12-16 07:36:04 -0800462#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700463#endif
minyue5f026d02015-12-16 07:36:04 -0800464TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800465 const std::string input_rtp_file =
466 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000467
Yves Gerey665174f2018-06-19 15:03:05 +0200468 const std::string output_checksum =
469 PlatformChecksum("0c6dc227f781c81a229970f8fceda1a012498cba",
470 "15c4a2202877a414515e218bdb7992f0ad53e5af", "not used",
471 "0c6dc227f781c81a229970f8fceda1a012498cba",
472 "25fc4c863caa499aa447a5b8d059f5452cbcc500");
minyue4f906772016-04-29 11:05:14 -0700473
henrik.lundin2979f552017-05-05 05:04:16 -0700474 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200475 PlatformChecksum("4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
Yves Gerey665174f2018-06-19 15:03:05 +0200476 "e339cb2adf5ab3dfc21cb7205d670a34751e8336", "not used",
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200477 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
478 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9");
minyue4f906772016-04-29 11:05:14 -0700479
Yves Gerey665174f2018-06-19 15:03:05 +0200480 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100481 FLAG_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000482}
483
Yves Gerey665174f2018-06-19 15:03:05 +0200484#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200485 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800486#define MAYBE_TestOpusBitExactness TestOpusBitExactness
487#else
488#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
489#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200490TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800491 const std::string input_rtp_file =
492 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800493
Yves Gereya038e712018-11-14 10:45:50 +0100494 // Checksum depends on libopus being compiled with or without SSE.
495 const std::string maybe_sse =
496 "14a63b3c7b925c82296be4bafc71bec85f2915c2|"
497 "2c05677daa968d6c68b92adf4affb7cd9bb4d363";
498 const std::string output_checksum = PlatformChecksum(
499 maybe_sse, "b7b7ed802b0e18ee416973bf3b9ae98599b0181d",
500 "5876e52dda90d5ca433c3726555b907b97c86374", maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700501
henrik.lundin2979f552017-05-05 05:04:16 -0700502 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200503 PlatformChecksum("adb3272498e436d1c019cbfd71610e9510c54497",
504 "fa935a91abc7291db47428a2d7c5361b98713a92",
505 "42106aa5267300f709f63737707ef07afd9dac61",
506 "adb3272498e436d1c019cbfd71610e9510c54497",
507 "adb3272498e436d1c019cbfd71610e9510c54497");
minyue4f906772016-04-29 11:05:14 -0700508
Yves Gerey665174f2018-06-19 15:03:05 +0200509 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100510 FLAG_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800511}
512
Yves Gerey665174f2018-06-19 15:03:05 +0200513#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100514 defined(WEBRTC_CODEC_OPUS)
515#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
516#else
517#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
518#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100519TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100520 const std::string input_rtp_file =
521 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
522
Yves Gereya038e712018-11-14 10:45:50 +0100523 const std::string maybe_sse =
524 "713af6c92881f5aab1285765ee6680da9d1c06ce|"
525 "2ac10c4e79aeedd0df2863b079da5848b40f00b5";
526 const std::string output_checksum = PlatformChecksum(
527 maybe_sse, "3ec991b96872123f1554c03c543ca5d518431e46",
528 "da9f9a2d94e0c2d67342fad4965d7b91cda50b25", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100529
530 const std::string network_stats_checksum =
531 "bab58dc587d956f326056d7340c96eb9d2d3cc21";
532
Henrik Lundine9619f82017-11-27 14:05:27 +0100533 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100534 FLAG_gen_ref);
Henrik Lundine9619f82017-11-27 14:05:27 +0100535}
536
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000537// Use fax mode to avoid time-scaling. This is to simplify the testing of
538// packet waiting times in the packet buffer.
539class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
540 protected:
541 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200542 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000543 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200544 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000545};
546
547TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000548 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
549 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 const size_t kSamples = 10 * 16;
551 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000552 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800553 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700554 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200555 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
556 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700557 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
558 rtp_info.payloadType = 94; // PCM16b WB codec.
559 rtp_info.markerBit = 0;
560 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561 }
562 // Pull out all data.
563 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700564 bool muted;
565 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800566 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 }
568
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200569 NetEqNetworkStatistics stats;
570 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000571 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
572 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200573 // each packet. Thus, we are calculating the statistics for a series from 10
574 // to 300, in steps of 10 ms.
575 EXPECT_EQ(155, stats.mean_waiting_time_ms);
576 EXPECT_EQ(155, stats.median_waiting_time_ms);
577 EXPECT_EQ(10, stats.min_waiting_time_ms);
578 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579
580 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200581 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
582 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
583 EXPECT_EQ(-1, stats.median_waiting_time_ms);
584 EXPECT_EQ(-1, stats.min_waiting_time_ms);
585 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000586}
587
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000588TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 const int kNumFrames = 3000; // Needed for convergence.
590 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000591 const size_t kSamples = 10 * 16;
592 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000593 while (frame_index < kNumFrames) {
594 // Insert one packet each time, except every 10th time where we insert two
595 // packets at once. This will create a negative clock-drift of approx. 10%.
596 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
597 for (int n = 0; n < num_packets; ++n) {
598 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700599 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000600 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700601 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 ++frame_index;
603 }
604
605 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700606 bool muted;
607 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800608 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609 }
610
611 NetEqNetworkStatistics network_stats;
612 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700613 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614}
615
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000616TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000617 const int kNumFrames = 5000; // Needed for convergence.
618 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000619 const size_t kSamples = 10 * 16;
620 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000621 for (int i = 0; i < kNumFrames; ++i) {
622 // Insert one packet each time, except every 10th time where we don't insert
623 // any packet. This will create a positive clock-drift of approx. 11%.
624 int num_packets = (i % 10 == 9 ? 0 : 1);
625 for (int n = 0; n < num_packets; ++n) {
626 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700627 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000628 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700629 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000630 ++frame_index;
631 }
632
633 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700634 bool muted;
635 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800636 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000637 }
638
639 NetEqNetworkStatistics network_stats;
640 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700641 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000642}
643
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000644void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
645 double network_freeze_ms,
646 bool pull_audio_during_freeze,
647 int delay_tolerance_ms,
648 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649 uint16_t seq_no = 0;
650 uint32_t timestamp = 0;
651 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000652 const size_t kSamples = kFrameSizeMs * 16;
653 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000654 double next_input_time_ms = 0.0;
655 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700656 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657
658 // Insert speech for 5 seconds.
659 const int kSpeechDurationMs = 5000;
660 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
661 // Each turn in this for loop is 10 ms.
662 while (next_input_time_ms <= t_ms) {
663 // Insert one 30 ms speech frame.
664 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700665 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700667 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 ++seq_no;
669 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000670 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000671 }
672 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700673 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800674 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000675 }
676
henrik.lundin55480f52016-03-08 02:37:57 -0800677 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200678 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700679 ASSERT_TRUE(playout_timestamp);
680 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000681
682 // Insert CNG for 1 minute (= 60000 ms).
683 const int kCngPeriodMs = 100;
684 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
685 const int kCngDurationMs = 60000;
686 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
687 // Each turn in this for loop is 10 ms.
688 while (next_input_time_ms <= t_ms) {
689 // Insert one CNG frame each 100 ms.
690 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000691 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700692 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800694 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700695 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800696 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000697 ++seq_no;
698 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000699 next_input_time_ms += static_cast<double>(kCngPeriodMs) * 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 }
705
henrik.lundin55480f52016-03-08 02:37:57 -0800706 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000707
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 if (network_freeze_ms > 0) {
709 // First keep pulling audio for |network_freeze_ms| without inserting
710 // any data, then insert CNG data corresponding to |network_freeze_ms|
711 // without pulling any output audio.
712 const double loop_end_time = t_ms + network_freeze_ms;
713 for (; t_ms < loop_end_time; t_ms += 10) {
714 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700715 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800716 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800717 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000718 }
719 bool pull_once = pull_audio_during_freeze;
720 // If |pull_once| is true, GetAudio will be called once half-way through
721 // the network recovery period.
722 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
723 while (next_input_time_ms <= t_ms) {
724 if (pull_once && next_input_time_ms >= pull_time_ms) {
725 pull_once = false;
726 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700727 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800728 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800729 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000730 t_ms += 10;
731 }
732 // Insert one CNG frame each 100 ms.
733 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000734 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700735 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000736 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800737 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700738 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800739 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 ++seq_no;
741 timestamp += kCngPeriodSamples;
742 next_input_time_ms += kCngPeriodMs * drift_factor;
743 }
744 }
745
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000746 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000747 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800748 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749 // Each turn in this for loop is 10 ms.
750 while (next_input_time_ms <= t_ms) {
751 // Insert one 30 ms speech frame.
752 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700753 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000754 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700755 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000756 ++seq_no;
757 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000758 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000759 }
760 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700761 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800762 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000763 // Increase clock.
764 t_ms += 10;
765 }
766
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 // Check that the speech starts again within reasonable time.
768 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
769 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700770 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700771 ASSERT_TRUE(playout_timestamp);
772 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000773 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000774 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
775 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000776}
777
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000778TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000779 // Apply a clock drift of -25 ms / s (sender faster than receiver).
780 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000781 const double kNetworkFreezeTimeMs = 0.0;
782 const bool kGetAudioDuringFreezeRecovery = false;
783 const int kDelayToleranceMs = 20;
784 const int kMaxTimeToSpeechMs = 100;
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, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000791 // Apply a clock drift of +25 ms / s (sender slower than receiver).
792 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000793 const double kNetworkFreezeTimeMs = 0.0;
794 const bool kGetAudioDuringFreezeRecovery = false;
795 const int kDelayToleranceMs = 20;
796 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200797 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
798 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000799 kMaxTimeToSpeechMs);
800}
801
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000802TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000803 // Apply a clock drift of -25 ms / s (sender faster than receiver).
804 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
805 const double kNetworkFreezeTimeMs = 5000.0;
806 const bool kGetAudioDuringFreezeRecovery = false;
807 const int kDelayToleranceMs = 50;
808 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200809 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
810 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000811 kMaxTimeToSpeechMs);
812}
813
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000814TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000815 // Apply a clock drift of +25 ms / s (sender slower than receiver).
816 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
817 const double kNetworkFreezeTimeMs = 5000.0;
818 const bool kGetAudioDuringFreezeRecovery = false;
819 const int kDelayToleranceMs = 20;
820 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200821 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
822 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000823 kMaxTimeToSpeechMs);
824}
825
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000826TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000827 // Apply a clock drift of +25 ms / s (sender slower than receiver).
828 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
829 const double kNetworkFreezeTimeMs = 5000.0;
830 const bool kGetAudioDuringFreezeRecovery = true;
831 const int kDelayToleranceMs = 20;
832 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200833 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
834 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000835 kMaxTimeToSpeechMs);
836}
837
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000838TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000839 const double kDriftFactor = 1.0; // No drift.
840 const double kNetworkFreezeTimeMs = 0.0;
841 const bool kGetAudioDuringFreezeRecovery = false;
842 const int kDelayToleranceMs = 10;
843 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200844 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
845 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000846 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000847}
848
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000849TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000850 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700852 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000853 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700854 rtp_info.payloadType = 1; // Not registered as a decoder.
855 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000856}
857
Peter Boströme2976c82016-01-04 22:44:05 +0100858#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800859#define MAYBE_DecoderError DecoderError
860#else
861#define MAYBE_DecoderError DISABLED_DecoderError
862#endif
863
Peter Boströme2976c82016-01-04 22:44:05 +0100864TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000865 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000866 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700867 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000868 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700869 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
870 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
872 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700873 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800874 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700875 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000876 }
henrik.lundin7a926812016-05-12 13:51:28 -0700877 bool muted;
878 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
879 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800880
yujo36b1a5f2017-06-12 12:45:32 -0700881 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700883 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000884 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200885 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000886 ss << "i = " << i;
887 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700888 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 }
890}
891
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000892TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000893 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
894 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700895 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800896 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700897 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 }
henrik.lundin7a926812016-05-12 13:51:28 -0700899 bool muted;
900 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
901 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000902 // Verify that the first block of samples is set to 0.
903 static const int kExpectedOutputLength =
904 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700905 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200907 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000908 ss << "i = " << i;
909 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700910 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000911 }
henrik.lundind89814b2015-11-23 06:49:25 -0800912 // Verify that the sample rate did not change from the initial configuration.
913 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000914}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000915
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000916class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000917 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000918 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700919 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000920 uint8_t payload_type = 0xFF; // Invalid.
921 if (sampling_rate_hz == 8000) {
922 expected_samples_per_channel = kBlockSize8kHz;
923 payload_type = 93; // PCM 16, 8 kHz.
924 } else if (sampling_rate_hz == 16000) {
925 expected_samples_per_channel = kBlockSize16kHz;
926 payload_type = 94; // PCM 16, 16 kHZ.
927 } else if (sampling_rate_hz == 32000) {
928 expected_samples_per_channel = kBlockSize32kHz;
929 payload_type = 95; // PCM 16, 32 kHz.
930 } else {
931 ASSERT_TRUE(false); // Unsupported test case.
932 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000933
henrik.lundin6d8e0112016-03-04 10:34:21 -0800934 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000935 test::AudioLoop input;
936 // We are using the same 32 kHz input file for all tests, regardless of
937 // |sampling_rate_hz|. The output may sound weird, but the test is still
938 // valid.
939 ASSERT_TRUE(input.Init(
940 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
941 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700942 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000943
944 // Payload of 10 ms of PCM16 32 kHz.
945 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700946 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000947 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700948 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000949
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000950 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700951 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000952 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800953 auto block = input.GetNextBlock();
954 ASSERT_EQ(expected_samples_per_channel, block.size());
955 size_t enc_len_bytes =
956 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000957 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
958
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200959 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700960 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200961 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
962 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800963 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700964 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800965 ASSERT_EQ(1u, output.num_channels_);
966 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800967 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000968
969 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200970 rtp_info.timestamp +=
971 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700972 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200973 receive_timestamp +=
974 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000975 }
976
henrik.lundin6d8e0112016-03-04 10:34:21 -0800977 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000978
979 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
980 // one frame without checking speech-type. This is the first frame pulled
981 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700982 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800983 ASSERT_EQ(1u, output.num_channels_);
984 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000985
986 // To be able to test the fading of background noise we need at lease to
987 // pull 611 frames.
988 const int kFadingThreshold = 611;
989
990 // Test several CNG-to-PLC packet for the expected behavior. The number 20
991 // is arbitrary, but sufficiently large to test enough number of frames.
992 const int kNumPlcToCngTestFrames = 20;
993 bool plc_to_cng = false;
994 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800995 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700996 // Set to non-zero.
997 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700998 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
999 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001000 ASSERT_EQ(1u, output.num_channels_);
1001 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001002 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001003 plc_to_cng = true;
1004 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001005 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001006 for (size_t k = 0;
1007 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001008 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +02001009 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001010 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001011 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001012 }
1013 }
1014 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1015 }
1016};
1017
Henrik Lundin67190172018-04-20 15:34:48 +02001018TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001019 CheckBgn(8000);
1020 CheckBgn(16000);
1021 CheckBgn(32000);
1022}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001023
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001024void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1025 uint32_t start_timestamp,
1026 const std::set<uint16_t>& drop_seq_numbers,
1027 bool expect_seq_no_wrap,
1028 bool expect_timestamp_wrap) {
1029 uint16_t seq_no = start_seq_no;
1030 uint32_t timestamp = start_timestamp;
1031 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1032 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1033 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001034 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001035 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001036 uint32_t receive_timestamp = 0;
1037
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001038 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001039 const int kSpeechDurationMs = 2000;
1040 int packets_inserted = 0;
1041 uint16_t last_seq_no;
1042 uint32_t last_timestamp;
1043 bool timestamp_wrapped = false;
1044 bool seq_no_wrapped = false;
1045 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1046 // Each turn in this for loop is 10 ms.
1047 while (next_input_time_ms <= t_ms) {
1048 // Insert one 30 ms speech frame.
1049 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001050 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001051 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1052 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1053 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001054 ASSERT_EQ(0,
1055 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001056 ++packets_inserted;
1057 }
1058 NetEqNetworkStatistics network_stats;
1059 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1060
1061 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1062 // packet size for first few packets. Therefore we refrain from checking
1063 // the criteria.
1064 if (packets_inserted > 4) {
1065 // Expect preferred and actual buffer size to be no more than 2 frames.
1066 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
Yves Gerey665174f2018-06-19 15:03:05 +02001067 EXPECT_LE(network_stats.current_buffer_size_ms,
1068 kFrameSizeMs * 2 + algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001069 }
1070 last_seq_no = seq_no;
1071 last_timestamp = timestamp;
1072
1073 ++seq_no;
1074 timestamp += kSamples;
1075 receive_timestamp += kSamples;
1076 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1077
1078 seq_no_wrapped |= seq_no < last_seq_no;
1079 timestamp_wrapped |= timestamp < last_timestamp;
1080 }
1081 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001082 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001083 bool muted;
1084 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001085 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1086 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001087
1088 // Expect delay (in samples) to be less than 2 packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +02001089 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001090 ASSERT_TRUE(playout_timestamp);
1091 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001092 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001093 }
1094 // Make sure we have actually tested wrap-around.
1095 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1096 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1097}
1098
1099TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1100 // Start with a sequence number that will soon wrap.
1101 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1102 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1103}
1104
1105TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1106 // Start with a sequence number that will soon wrap.
1107 std::set<uint16_t> drop_seq_numbers;
1108 drop_seq_numbers.insert(0xFFFF);
1109 drop_seq_numbers.insert(0x0);
1110 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1111}
1112
1113TEST_F(NetEqDecodingTest, TimestampWrap) {
1114 // Start with a timestamp that will soon wrap.
1115 std::set<uint16_t> drop_seq_numbers;
1116 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1117}
1118
1119TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1120 // Start with a timestamp and a sequence number that will wrap at the same
1121 // time.
1122 std::set<uint16_t> drop_seq_numbers;
1123 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1124}
1125
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001126void NetEqDecodingTest::DuplicateCng() {
1127 uint16_t seq_no = 0;
1128 uint32_t timestamp = 0;
1129 const int kFrameSizeMs = 10;
1130 const int kSampleRateKhz = 16;
1131 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001132 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001133
Yves Gerey665174f2018-06-19 15:03:05 +02001134 const int algorithmic_delay_samples =
1135 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001136 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001137 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001138 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001139 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001140 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001141 for (int i = 0; i < 3; ++i) {
1142 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001143 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001144 ++seq_no;
1145 timestamp += kSamples;
1146
1147 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001148 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001149 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001150 }
1151 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001152 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001153
1154 // Insert same CNG packet twice.
1155 const int kCngPeriodMs = 100;
1156 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001157 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001158 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1159 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001160 ASSERT_EQ(
1161 0, neteq_->InsertPacket(
1162 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001163
1164 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001165 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001166 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001167 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001168 EXPECT_FALSE(
1169 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001170 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1171 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001172
1173 // Insert the same CNG packet again. Note that at this point it is old, since
1174 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001175 ASSERT_EQ(
1176 0, neteq_->InsertPacket(
1177 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001178
1179 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1180 // we have already pulled out CNG once.
1181 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001182 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001183 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001184 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001185 EXPECT_FALSE(
1186 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001187 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001188 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001189 }
1190
1191 // Insert speech again.
1192 ++seq_no;
1193 timestamp += kCngPeriodSamples;
1194 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001195 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001196
1197 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001198 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001199 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001200 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +02001201 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001202 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001203 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001204 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001205}
1206
Yves Gerey665174f2018-06-19 15:03:05 +02001207TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
1208 DuplicateCng();
1209}
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001210
1211TEST_F(NetEqDecodingTest, CngFirst) {
1212 uint16_t seq_no = 0;
1213 uint32_t timestamp = 0;
1214 const int kFrameSizeMs = 10;
1215 const int kSampleRateKhz = 16;
1216 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1217 const int kPayloadBytes = kSamples * 2;
1218 const int kCngPeriodMs = 100;
1219 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1220 size_t payload_len;
1221
1222 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001223 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001224
1225 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001226 ASSERT_EQ(
1227 NetEq::kOK,
1228 neteq_->InsertPacket(
1229 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001230 ++seq_no;
1231 timestamp += kCngPeriodSamples;
1232
1233 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001234 bool muted;
1235 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001237 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001238
1239 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001240 const uint32_t first_speech_timestamp = timestamp;
1241 int timeout_counter = 0;
1242 do {
1243 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001244 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001245 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001246 ++seq_no;
1247 timestamp += kSamples;
1248
1249 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001250 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001251 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001252 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001253 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001254 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001255}
henrik.lundin7a926812016-05-12 13:51:28 -07001256
1257class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1258 public:
1259 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1260 config_.enable_muted_state = true;
1261 }
1262
1263 protected:
1264 static constexpr size_t kSamples = 10 * 16;
1265 static constexpr size_t kPayloadBytes = kSamples * 2;
1266
1267 void InsertPacket(uint32_t rtp_timestamp) {
1268 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001269 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001270 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001271 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001272 }
1273
henrik.lundin42feb512016-09-20 06:51:40 -07001274 void InsertCngPacket(uint32_t rtp_timestamp) {
1275 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001276 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001277 size_t payload_len;
1278 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001279 EXPECT_EQ(
1280 NetEq::kOK,
1281 neteq_->InsertPacket(
1282 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001283 }
1284
henrik.lundin7a926812016-05-12 13:51:28 -07001285 bool GetAudioReturnMuted() {
1286 bool muted;
1287 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1288 return muted;
1289 }
1290
1291 void GetAudioUntilMuted() {
1292 while (!GetAudioReturnMuted()) {
1293 ASSERT_LT(counter_++, 1000) << "Test timed out";
1294 }
1295 }
1296
1297 void GetAudioUntilNormal() {
1298 bool muted = false;
1299 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1300 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1301 ASSERT_LT(counter_++, 1000) << "Test timed out";
1302 }
1303 EXPECT_FALSE(muted);
1304 }
1305
1306 int counter_ = 0;
1307};
1308
1309// Verifies that NetEq goes in and out of muted state as expected.
1310TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1311 // Insert one speech packet.
1312 InsertPacket(0);
1313 // Pull out audio once and expect it not to be muted.
1314 EXPECT_FALSE(GetAudioReturnMuted());
1315 // Pull data until faded out.
1316 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001317 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001318
1319 // Verify that output audio is not written during muted mode. Other parameters
1320 // should be correct, though.
1321 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001322 int16_t* frame_data = new_frame.mutable_data();
1323 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1324 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001325 }
1326 bool muted;
1327 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1328 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001329 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001330 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1331 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001332 }
1333 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1334 new_frame.timestamp_);
1335 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1336 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1337 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1338 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1339 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1340
1341 // Insert new data. Timestamp is corrected for the time elapsed since the last
1342 // packet. Verify that normal operation resumes.
1343 InsertPacket(kSamples * counter_);
1344 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001345 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001346
1347 NetEqNetworkStatistics stats;
1348 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1349 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1350 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1351 // concealment samples in this test.
1352 EXPECT_GT(stats.expand_rate, 14000);
1353 // And, it should be greater than the speech_expand_rate.
1354 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001355}
1356
1357// Verifies that NetEq goes out of muted state when given a delayed packet.
1358TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1359 // Insert one speech packet.
1360 InsertPacket(0);
1361 // Pull out audio once and expect it not to be muted.
1362 EXPECT_FALSE(GetAudioReturnMuted());
1363 // Pull data until faded out.
1364 GetAudioUntilMuted();
1365 // Insert new data. Timestamp is only corrected for the half of the time
1366 // elapsed since the last packet. That is, the new packet is delayed. Verify
1367 // that normal operation resumes.
1368 InsertPacket(kSamples * counter_ / 2);
1369 GetAudioUntilNormal();
1370}
1371
1372// Verifies that NetEq goes out of muted state when given a future packet.
1373TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1374 // Insert one speech packet.
1375 InsertPacket(0);
1376 // Pull out audio once and expect it not to be muted.
1377 EXPECT_FALSE(GetAudioReturnMuted());
1378 // Pull data until faded out.
1379 GetAudioUntilMuted();
1380 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1381 // last packet. That is, the new packet is too early. Verify that normal
1382 // operation resumes.
1383 InsertPacket(kSamples * counter_ * 2);
1384 GetAudioUntilNormal();
1385}
1386
1387// Verifies that NetEq goes out of muted state when given an old packet.
1388TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1389 // Insert one speech packet.
1390 InsertPacket(0);
1391 // Pull out audio once and expect it not to be muted.
1392 EXPECT_FALSE(GetAudioReturnMuted());
1393 // Pull data until faded out.
1394 GetAudioUntilMuted();
1395
1396 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1397 // Insert packet which is older than the first packet.
1398 InsertPacket(kSamples * (counter_ - 1000));
1399 EXPECT_FALSE(GetAudioReturnMuted());
1400 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1401}
1402
henrik.lundin42feb512016-09-20 06:51:40 -07001403// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1404// packet stream is suspended for a long time.
1405TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1406 // Insert one CNG packet.
1407 InsertCngPacket(0);
1408
1409 // Pull 10 seconds of audio (10 ms audio generated per lap).
1410 for (int i = 0; i < 1000; ++i) {
1411 bool muted;
1412 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1413 ASSERT_FALSE(muted);
1414 }
1415 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1416}
1417
1418// Verifies that NetEq goes back to normal after a long CNG period with the
1419// packet stream suspended.
1420TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1421 // Insert one CNG packet.
1422 InsertCngPacket(0);
1423
1424 // Pull 10 seconds of audio (10 ms audio generated per lap).
1425 for (int i = 0; i < 1000; ++i) {
1426 bool muted;
1427 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1428 }
1429
1430 // Insert new data. Timestamp is corrected for the time elapsed since the last
1431 // packet. Verify that normal operation resumes.
1432 InsertPacket(kSamples * counter_);
1433 GetAudioUntilNormal();
1434}
1435
henrik.lundin7a926812016-05-12 13:51:28 -07001436class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1437 public:
1438 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1439
1440 void SetUp() override {
1441 NetEqDecodingTest::SetUp();
1442 config2_ = config_;
1443 }
1444
1445 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001446 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001447 ASSERT_TRUE(neteq2_);
1448 LoadDecoders(neteq2_.get());
1449 }
1450
1451 protected:
1452 std::unique_ptr<NetEq> neteq2_;
1453 NetEq::Config config2_;
1454};
1455
1456namespace {
1457::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1458 const AudioFrame& b) {
1459 if (a.timestamp_ != b.timestamp_)
1460 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1461 << " != " << b.timestamp_ << ")";
1462 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +02001463 return ::testing::AssertionFailure()
1464 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
1465 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001466 if (a.samples_per_channel_ != b.samples_per_channel_)
1467 return ::testing::AssertionFailure()
1468 << "samples_per_channel_ diff (" << a.samples_per_channel_
1469 << " != " << b.samples_per_channel_ << ")";
1470 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +02001471 return ::testing::AssertionFailure()
1472 << "num_channels_ diff (" << a.num_channels_
1473 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001474 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +02001475 return ::testing::AssertionFailure()
1476 << "speech_type_ diff (" << a.speech_type_
1477 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001478 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +02001479 return ::testing::AssertionFailure()
1480 << "vad_activity_ diff (" << a.vad_activity_
1481 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001482 return ::testing::AssertionSuccess();
1483}
1484
1485::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1486 const AudioFrame& b) {
1487 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1488 if (!res)
1489 return res;
Yves Gerey665174f2018-06-19 15:03:05 +02001490 if (memcmp(a.data(), b.data(),
1491 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
1492 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001493 return ::testing::AssertionFailure() << "data_ diff";
1494 }
1495 return ::testing::AssertionSuccess();
1496}
1497
1498} // namespace
1499
1500TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1501 ASSERT_FALSE(config_.enable_muted_state);
1502 config2_.enable_muted_state = true;
1503 CreateSecondInstance();
1504
1505 // Insert one speech packet into both NetEqs.
1506 const size_t kSamples = 10 * 16;
1507 const size_t kPayloadBytes = kSamples * 2;
1508 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001509 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001510 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001511 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1512 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001513
1514 AudioFrame out_frame1, out_frame2;
1515 bool muted;
1516 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001517 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001518 ss << "i = " << i;
1519 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1520 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1521 EXPECT_FALSE(muted);
1522 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1523 if (muted) {
1524 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1525 } else {
1526 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1527 }
1528 }
1529 EXPECT_TRUE(muted);
1530
1531 // Insert new data. Timestamp is corrected for the time elapsed since the last
1532 // packet.
1533 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001534 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1535 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001536
1537 int counter = 0;
1538 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1539 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +02001540 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001541 ss << "counter = " << counter;
1542 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1543 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1544 EXPECT_FALSE(muted);
1545 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1546 if (muted) {
1547 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1548 } else {
1549 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1550 }
1551 }
1552 EXPECT_FALSE(muted);
1553}
1554
henrik.lundin114c1b32017-04-26 07:47:32 -07001555TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1556 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1557
1558 // Pull out data once.
1559 AudioFrame output;
1560 bool muted;
1561 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1562
1563 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1564}
1565
1566TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1567 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1568 // default). Make the length 10 ms.
1569 constexpr size_t kPayloadSamples = 16 * 10;
1570 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1571 uint8_t payload[kPayloadBytes] = {0};
1572
1573 RTPHeader rtp_info;
1574 constexpr uint32_t kRtpTimestamp = 0x1234;
1575 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1576 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1577
1578 // Pull out data once.
1579 AudioFrame output;
1580 bool muted;
1581 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1582
1583 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1584 neteq_->LastDecodedTimestamps());
1585
1586 // Nothing decoded on the second call.
1587 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1588 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1589}
1590
1591TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1592 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1593 // by default). Make the length 5 ms so that NetEq must decode them both in
1594 // the same GetAudio call.
1595 constexpr size_t kPayloadSamples = 16 * 5;
1596 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1597 uint8_t payload[kPayloadBytes] = {0};
1598
1599 RTPHeader rtp_info;
1600 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1601 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1602 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1603 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1604 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1605 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1606
1607 // Pull out data once.
1608 AudioFrame output;
1609 bool muted;
1610 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1611
1612 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1613 neteq_->LastDecodedTimestamps());
1614}
1615
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001616TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1617 const int kNumConcealmentEvents = 19;
1618 const size_t kSamples = 10 * 16;
1619 const size_t kPayloadBytes = kSamples * 2;
1620 int seq_no = 0;
1621 RTPHeader rtp_info;
1622 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1623 rtp_info.payloadType = 94; // PCM16b WB codec.
1624 rtp_info.markerBit = 0;
1625 const uint8_t payload[kPayloadBytes] = {0};
1626 bool muted;
1627
1628 for (int i = 0; i < kNumConcealmentEvents; i++) {
1629 // Insert some packets of 10 ms size.
1630 for (int j = 0; j < 10; j++) {
1631 rtp_info.sequenceNumber = seq_no++;
1632 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1633 neteq_->InsertPacket(rtp_info, payload, 0);
1634 neteq_->GetAudio(&out_frame_, &muted);
1635 }
1636
1637 // Lose a number of packets.
1638 int num_lost = 1 + i;
1639 for (int j = 0; j < num_lost; j++) {
1640 seq_no++;
1641 neteq_->GetAudio(&out_frame_, &muted);
1642 }
1643 }
1644
1645 // Check number of concealment events.
1646 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1647 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1648}
1649
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001650// Test that the jitter buffer delay stat is computed correctly.
1651void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1652 const int kNumPackets = 10;
1653 const int kDelayInNumPackets = 2;
1654 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1655 const size_t kSamples = kPacketLenMs * 16;
1656 const size_t kPayloadBytes = kSamples * 2;
1657 RTPHeader rtp_info;
1658 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1659 rtp_info.payloadType = 94; // PCM16b WB codec.
1660 rtp_info.markerBit = 0;
1661 const uint8_t payload[kPayloadBytes] = {0};
1662 bool muted;
1663 int packets_sent = 0;
1664 int packets_received = 0;
1665 int expected_delay = 0;
1666 while (packets_received < kNumPackets) {
1667 // Insert packet.
1668 if (packets_sent < kNumPackets) {
1669 rtp_info.sequenceNumber = packets_sent++;
1670 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1671 neteq_->InsertPacket(rtp_info, payload, 0);
1672 }
1673
1674 // Get packet.
1675 if (packets_sent > kDelayInNumPackets) {
1676 neteq_->GetAudio(&out_frame_, &muted);
1677 packets_received++;
1678
1679 // The delay reported by the jitter buffer never exceeds
1680 // the number of samples previously fetched with GetAudio
1681 // (hence the min()).
1682 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1683
1684 // The increase of the expected delay is the product of
1685 // the current delay of the jitter buffer in ms * the
1686 // number of samples that are sent for play out.
1687 int current_delay_ms = packets_delay * kPacketLenMs;
1688 expected_delay += current_delay_ms * kSamples;
1689 }
1690 }
1691
1692 if (apply_packet_loss) {
1693 // Extra call to GetAudio to cause concealment.
1694 neteq_->GetAudio(&out_frame_, &muted);
1695 }
1696
1697 // Check jitter buffer delay.
1698 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1699 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
1700}
1701
1702TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1703 TestJitterBufferDelay(false);
1704}
1705
1706TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1707 TestJitterBufferDelay(true);
1708}
1709
Henrik Lundin7687ad52018-07-02 10:14:46 +02001710namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001711TEST(NetEqNoTimeStretchingMode, RunTest) {
1712 NetEq::Config config;
1713 config.for_test_no_time_stretching = true;
1714 auto codecs = NetEqTest::StandardDecoderMap();
1715 NetEqTest::ExtDecoderMap ext_codecs;
1716 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1717 {1, kRtpExtensionAudioLevel},
1718 {3, kRtpExtensionAbsoluteSendTime},
1719 {5, kRtpExtensionTransportSequenceNumber},
1720 {7, kRtpExtensionVideoContentType},
1721 {8, kRtpExtensionVideoTiming}};
1722 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1723 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001724 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001725 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1726 new TimeLimitedNetEqInput(std::move(input), 20000));
1727 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1728 NetEqTest::Callbacks callbacks;
1729 NetEqTest test(config, codecs, ext_codecs, std::move(input_time_limit),
1730 std::move(output), callbacks);
1731 test.Run();
1732 const auto stats = test.SimulationStats();
1733 EXPECT_EQ(0, stats.accelerate_rate);
1734 EXPECT_EQ(0, stats.preemptive_rate);
1735}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001736
1737} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001738} // namespace webrtc