blob: b3d6d8c7a7aabe390762ef6988265d830671a3ea [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
11/*
12 * This file includes unit tests for NetEQ.
13 */
14
Henrik Kjellander74640892015-10-29 11:31:02 +010015#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000017#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include <stdlib.h>
19#include <string.h> // memset
20
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000021#include <algorithm>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000022#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023#include <string>
24#include <vector>
25
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000026#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000027#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000028#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010031#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000033#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/typedefs.h"
35
minyue5f026d02015-12-16 07:36:04 -080036#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
37#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
38#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
39#else
40#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
41#endif
42#endif
43
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000044DEFINE_bool(gen_ref, false, "Generate reference files.");
45
minyue5f026d02015-12-16 07:36:04 -080046namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
minyue5f026d02015-12-16 07:36:04 -080048bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000049 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000051 all_zero = buf[n] == 0;
52 return all_zero;
53}
54
minyue5f026d02015-12-16 07:36:04 -080055bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000056 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070057 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000058 all_non_zero = buf[n] != 0;
59 return all_non_zero;
60}
61
minyue5f026d02015-12-16 07:36:04 -080062#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
63void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
64 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
65 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
66 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
67 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
68 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
69 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
70 stats->set_expand_rate(stats_raw.expand_rate);
71 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
72 stats->set_preemptive_rate(stats_raw.preemptive_rate);
73 stats->set_accelerate_rate(stats_raw.accelerate_rate);
74 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
75 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
76 stats->set_added_zero_samples(stats_raw.added_zero_samples);
77 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
78 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
79 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
80 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
81}
82
83void Convert(const webrtc::RtcpStatistics& stats_raw,
84 webrtc::neteq_unittest::RtcpStatistics* stats) {
85 stats->set_fraction_lost(stats_raw.fraction_lost);
86 stats->set_cumulative_lost(stats_raw.cumulative_lost);
87 stats->set_extended_max_sequence_number(
88 stats_raw.extended_max_sequence_number);
89 stats->set_jitter(stats_raw.jitter);
90}
91
92void WriteMessage(FILE* file, const std::string& message) {
93 int32_t size = message.length();
94 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
95 if (size <= 0)
96 return;
97 ASSERT_EQ(static_cast<size_t>(size),
98 fwrite(message.data(), sizeof(char), size, file));
99}
100
101void ReadMessage(FILE* file, std::string* message) {
102 int32_t size;
103 ASSERT_EQ(1u, fread(&size, sizeof(size), 1, file));
104 if (size <= 0)
105 return;
106 rtc::scoped_ptr<char[]> buffer(new char[size]);
107 ASSERT_EQ(static_cast<size_t>(size),
108 fread(buffer.get(), sizeof(char), size, file));
109 message->assign(buffer.get(), size);
110}
111#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
112
113} // namespace
114
115namespace webrtc {
116
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117class RefFiles {
118 public:
119 RefFiles(const std::string& input_file, const std::string& output_file);
120 ~RefFiles();
121 template<class T> void ProcessReference(const T& test_results);
122 template<typename T, size_t n> void ProcessReference(
123 const T (&test_results)[n],
124 size_t length);
125 template<typename T, size_t n> void WriteToFile(
126 const T (&test_results)[n],
127 size_t length);
128 template<typename T, size_t n> void ReadFromFileAndCompare(
129 const T (&test_results)[n],
130 size_t length);
131 void WriteToFile(const NetEqNetworkStatistics& stats);
132 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
133 void WriteToFile(const RtcpStatistics& stats);
134 void ReadFromFileAndCompare(const RtcpStatistics& stats);
135
136 FILE* input_fp_;
137 FILE* output_fp_;
138};
139
140RefFiles::RefFiles(const std::string &input_file,
141 const std::string &output_file)
142 : input_fp_(NULL),
143 output_fp_(NULL) {
144 if (!input_file.empty()) {
145 input_fp_ = fopen(input_file.c_str(), "rb");
146 EXPECT_TRUE(input_fp_ != NULL);
147 }
148 if (!output_file.empty()) {
149 output_fp_ = fopen(output_file.c_str(), "wb");
150 EXPECT_TRUE(output_fp_ != NULL);
151 }
152}
153
154RefFiles::~RefFiles() {
155 if (input_fp_) {
156 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
157 fclose(input_fp_);
158 }
159 if (output_fp_) fclose(output_fp_);
160}
161
162template<class T>
163void RefFiles::ProcessReference(const T& test_results) {
164 WriteToFile(test_results);
165 ReadFromFileAndCompare(test_results);
166}
167
168template<typename T, size_t n>
169void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
170 WriteToFile(test_results, length);
171 ReadFromFileAndCompare(test_results, length);
172}
173
174template<typename T, size_t n>
175void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
176 if (output_fp_) {
177 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
178 }
179}
180
181template<typename T, size_t n>
182void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
183 size_t length) {
184 if (input_fp_) {
185 // Read from ref file.
186 T* ref = new T[length];
187 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
188 // Compare
189 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
190 delete [] ref;
191 }
192}
193
minyue5f026d02015-12-16 07:36:04 -0800194void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats_raw) {
195#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
196 if (!output_fp_)
197 return;
198 neteq_unittest::NetEqNetworkStatistics stats;
199 Convert(stats_raw, &stats);
200
201 std::string stats_string;
202 ASSERT_TRUE(stats.SerializeToString(&stats_string));
203 WriteMessage(output_fp_, stats_string);
204#else
205 FAIL() << "Writing to reference file requires Proto Buffer.";
206#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207}
208
209void RefFiles::ReadFromFileAndCompare(
210 const NetEqNetworkStatistics& stats) {
minyue5f026d02015-12-16 07:36:04 -0800211#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
212 if (!input_fp_)
213 return;
214
215 std::string stats_string;
216 ReadMessage(input_fp_, &stats_string);
217 neteq_unittest::NetEqNetworkStatistics ref_stats;
218 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
219
220 // Compare
221 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms());
222 ASSERT_EQ(stats.preferred_buffer_size_ms,
223 ref_stats.preferred_buffer_size_ms());
224 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found());
225 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate());
226 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate());
227 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate());
228 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate());
229 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate());
230 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm());
231 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples());
minyue93c08b72015-12-22 09:57:41 -0800232 ASSERT_EQ(stats.secondary_decoded_rate, ref_stats.secondary_decoded_rate());
minyue5f026d02015-12-16 07:36:04 -0800233 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate());
234#else
235 FAIL() << "Reading from reference file requires Proto Buffer.";
236#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237}
238
minyue5f026d02015-12-16 07:36:04 -0800239void RefFiles::WriteToFile(const RtcpStatistics& stats_raw) {
240#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
241 if (!output_fp_)
242 return;
243 neteq_unittest::RtcpStatistics stats;
244 Convert(stats_raw, &stats);
245
246 std::string stats_string;
247 ASSERT_TRUE(stats.SerializeToString(&stats_string));
248 WriteMessage(output_fp_, stats_string);
249#else
250 FAIL() << "Writing to reference file requires Proto Buffer.";
251#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000252}
253
minyue5f026d02015-12-16 07:36:04 -0800254void RefFiles::ReadFromFileAndCompare(const RtcpStatistics& stats) {
255#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
256 if (!input_fp_)
257 return;
258 std::string stats_string;
259 ReadMessage(input_fp_, &stats_string);
260 neteq_unittest::RtcpStatistics ref_stats;
261 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
262
263 // Compare
264 ASSERT_EQ(stats.fraction_lost, ref_stats.fraction_lost());
265 ASSERT_EQ(stats.cumulative_lost, ref_stats.cumulative_lost());
266 ASSERT_EQ(stats.extended_max_sequence_number,
267 ref_stats.extended_max_sequence_number());
268 ASSERT_EQ(stats.jitter, ref_stats.jitter());
269#else
270 FAIL() << "Reading from reference file requires Proto Buffer.";
271#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272}
273
274class NetEqDecodingTest : public ::testing::Test {
275 protected:
276 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
277 // constants below can be changed.
278 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700279 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
280 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
281 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800282 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
283 static const size_t kMaxBlockSize = kBlockSize48kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 static const int kInitSampleRateHz = 8000;
285
286 NetEqDecodingTest();
287 virtual void SetUp();
288 virtual void TearDown();
289 void SelectDecoders(NetEqDecoder* used_codec);
290 void LoadDecoders();
291 void OpenInputFile(const std::string &rtp_file);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700292 void Process(size_t* out_len);
minyue5f026d02015-12-16 07:36:04 -0800293
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000294 void DecodeAndCompare(const std::string& rtp_file,
295 const std::string& ref_file,
296 const std::string& stat_ref_file,
297 const std::string& rtcp_ref_file);
minyue5f026d02015-12-16 07:36:04 -0800298
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 static void PopulateRtpInfo(int frame_index,
300 int timestamp,
301 WebRtcRTPHeader* rtp_info);
302 static void PopulateCng(int frame_index,
303 int timestamp,
304 WebRtcRTPHeader* rtp_info,
305 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000306 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000308 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
309 const std::set<uint16_t>& drop_seq_numbers,
310 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
311
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000312 void LongCngWithClockDrift(double drift_factor,
313 double network_freeze_ms,
314 bool pull_audio_during_freeze,
315 int delay_tolerance_ms,
316 int max_time_to_speech_ms);
317
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000318 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000319
wu@webrtc.org94454b72014-06-05 20:34:08 +0000320 uint32_t PlayoutTimestamp();
321
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000323 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000324 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
325 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 unsigned int sim_clock_;
327 int16_t out_data_[kMaxBlockSize];
328 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000329 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330};
331
332// Allocating the static const so that it can be passed by reference.
333const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700334const size_t NetEqDecodingTest::kBlockSize8kHz;
335const size_t NetEqDecodingTest::kBlockSize16kHz;
336const size_t NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000337const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338const int NetEqDecodingTest::kInitSampleRateHz;
339
340NetEqDecodingTest::NetEqDecodingTest()
341 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000342 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000344 output_sample_rate_(kInitSampleRateHz),
345 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000346 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 memset(out_data_, 0, sizeof(out_data_));
348}
349
350void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000351 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000352 NetEqNetworkStatistics stat;
353 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
354 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 ASSERT_TRUE(neteq_);
356 LoadDecoders();
357}
358
359void NetEqDecodingTest::TearDown() {
360 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361}
362
363void NetEqDecodingTest::LoadDecoders() {
364 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800365 ASSERT_EQ(0,
366 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800368 ASSERT_EQ(0,
369 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700370#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800372 ASSERT_EQ(
373 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700374#endif
375#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800377 ASSERT_EQ(
378 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700379#endif
380#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800382 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
383 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700384#endif
minyue93c08b72015-12-22 09:57:41 -0800385#ifdef WEBRTC_CODEC_OPUS
386 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus,
387 "opus", 111));
388#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800390 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
391 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800393 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
394 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800396 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
397 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800399 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
400 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800402 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
403 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000404}
405
406void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000407 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000408}
409
Peter Kastingdce40cf2015-08-24 14:52:23 -0700410void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000411 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000412 while (packet_ && sim_clock_ >= packet_->time_ms()) {
413 if (packet_->payload_length_bytes() > 0) {
414 WebRtcRTPHeader rtp_header;
415 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000416 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800417 rtp_header,
418 rtc::ArrayView<const uint8_t>(
419 packet_->payload(), packet_->payload_length_bytes()),
420 static_cast<uint32_t>(packet_->time_ms() *
421 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000422 }
423 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000424 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425 }
426
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000427 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000428 NetEqOutputType type;
429 int num_channels;
430 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
431 &num_channels, &type));
432 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
433 (*out_len == kBlockSize16kHz) ||
minyue93c08b72015-12-22 09:57:41 -0800434 (*out_len == kBlockSize32kHz) ||
435 (*out_len == kBlockSize48kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700436 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundind89814b2015-11-23 06:49:25 -0800437 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000438
439 // Increase time.
440 sim_clock_ += kTimeStepMs;
441}
442
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000443void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
444 const std::string& ref_file,
445 const std::string& stat_ref_file,
446 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000447 OpenInputFile(rtp_file);
448
449 std::string ref_out_file = "";
450 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000451 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000452 }
453 RefFiles ref_files(ref_file, ref_out_file);
454
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000455 std::string stat_out_file = "";
456 if (stat_ref_file.empty()) {
457 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
458 }
459 RefFiles network_stat_files(stat_ref_file, stat_out_file);
460
461 std::string rtcp_out_file = "";
462 if (rtcp_ref_file.empty()) {
463 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
464 }
465 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
466
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000467 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000468 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000469 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000470 std::ostringstream ss;
471 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
472 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700473 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000474 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000475 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000476
477 // Query the network statistics API once per second
478 if (sim_clock_ % 1000 == 0) {
479 // Process NetworkStatistics.
480 NetEqNetworkStatistics network_stats;
481 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000482 ASSERT_NO_FATAL_FAILURE(
483 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700484 // Compare with CurrentDelay, which should be identical.
485 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000486
487 // Process RTCPstat.
488 RtcpStatistics rtcp_stats;
489 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000490 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000491 }
492 }
493}
494
495void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
496 int timestamp,
497 WebRtcRTPHeader* rtp_info) {
498 rtp_info->header.sequenceNumber = frame_index;
499 rtp_info->header.timestamp = timestamp;
500 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
501 rtp_info->header.payloadType = 94; // PCM16b WB codec.
502 rtp_info->header.markerBit = 0;
503}
504
505void NetEqDecodingTest::PopulateCng(int frame_index,
506 int timestamp,
507 WebRtcRTPHeader* rtp_info,
508 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000509 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000510 rtp_info->header.sequenceNumber = frame_index;
511 rtp_info->header.timestamp = timestamp;
512 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
513 rtp_info->header.payloadType = 98; // WB CNG.
514 rtp_info->header.markerBit = 0;
515 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
516 *payload_len = 1; // Only noise level, no spectral parameters.
517}
518
minyue5f026d02015-12-16 07:36:04 -0800519#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
minyue93c08b72015-12-22 09:57:41 -0800520 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue5f026d02015-12-16 07:36:04 -0800521 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
kwiberg98ab3a42015-09-30 21:54:21 -0700522 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
minyue5f026d02015-12-16 07:36:04 -0800523#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700524#else
minyue5f026d02015-12-16 07:36:04 -0800525#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700526#endif
minyue5f026d02015-12-16 07:36:04 -0800527TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000528 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000529 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000530 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
531 // are identical. The latter could have been removed, but if clients still
532 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000533 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000534 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000535#if defined(_MSC_VER) && (_MSC_VER >= 1700)
536 // For Visual Studio 2012 and later, we will have to use the generic reference
537 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000538 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000539 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000540#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000541 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000542 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000543#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000544 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000545 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000546
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000547 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000548 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000549 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000550 DecodeAndCompare(input_rtp_file,
551 input_ref_file,
552 network_stat_ref_file,
553 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000554 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000555}
556
minyue93c08b72015-12-22 09:57:41 -0800557#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
558 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
559 defined(WEBRTC_CODEC_OPUS)
560#define MAYBE_TestOpusBitExactness TestOpusBitExactness
561#else
562#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
563#endif
564TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
565 const std::string input_rtp_file =
566 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
567 const std::string input_ref_file =
568 webrtc::test::ResourcePath("audio_coding/neteq4_opus_ref", "pcm");
569 const std::string network_stat_ref_file =
570 webrtc::test::ResourcePath("audio_coding/neteq4_opus_network_stats",
571 "dat");
572 const std::string rtcp_stat_ref_file =
573 webrtc::test::ResourcePath("audio_coding/neteq4_opus_rtcp_stats", "dat");
574
575 if (FLAGS_gen_ref) {
576 DecodeAndCompare(input_rtp_file, "", "", "");
577 } else {
578 DecodeAndCompare(input_rtp_file,
579 input_ref_file,
580 network_stat_ref_file,
581 rtcp_stat_ref_file);
582 }
583}
584
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000585// Use fax mode to avoid time-scaling. This is to simplify the testing of
586// packet waiting times in the packet buffer.
587class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
588 protected:
589 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
590 config_.playout_mode = kPlayoutFax;
591 }
592};
593
594TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
596 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000597 const size_t kSamples = 10 * 16;
598 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000599 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800600 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000601 WebRtcRTPHeader rtp_info;
602 rtp_info.header.sequenceNumber = i;
603 rtp_info.header.timestamp = i * kSamples;
604 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
605 rtp_info.header.payloadType = 94; // PCM16b WB codec.
606 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800607 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000608 }
609 // Pull out all data.
610 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700611 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 int num_channels;
613 NetEqOutputType type;
614 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
615 &num_channels, &type));
616 ASSERT_EQ(kBlockSize16kHz, out_len);
617 }
618
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200619 NetEqNetworkStatistics stats;
620 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000621 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
622 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200623 // each packet. Thus, we are calculating the statistics for a series from 10
624 // to 300, in steps of 10 ms.
625 EXPECT_EQ(155, stats.mean_waiting_time_ms);
626 EXPECT_EQ(155, stats.median_waiting_time_ms);
627 EXPECT_EQ(10, stats.min_waiting_time_ms);
628 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629
630 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200631 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
632 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
633 EXPECT_EQ(-1, stats.median_waiting_time_ms);
634 EXPECT_EQ(-1, stats.min_waiting_time_ms);
635 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636}
637
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000638TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639 const int kNumFrames = 3000; // Needed for convergence.
640 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000641 const size_t kSamples = 10 * 16;
642 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000643 while (frame_index < kNumFrames) {
644 // Insert one packet each time, except every 10th time where we insert two
645 // packets at once. This will create a negative clock-drift of approx. 10%.
646 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
647 for (int n = 0; n < num_packets; ++n) {
648 uint8_t payload[kPayloadBytes] = {0};
649 WebRtcRTPHeader rtp_info;
650 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800651 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000652 ++frame_index;
653 }
654
655 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700656 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 int num_channels;
658 NetEqOutputType type;
659 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
660 &num_channels, &type));
661 ASSERT_EQ(kBlockSize16kHz, out_len);
662 }
663
664 NetEqNetworkStatistics network_stats;
665 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
666 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
667}
668
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000669TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000670 const int kNumFrames = 5000; // Needed for convergence.
671 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000672 const size_t kSamples = 10 * 16;
673 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000674 for (int i = 0; i < kNumFrames; ++i) {
675 // Insert one packet each time, except every 10th time where we don't insert
676 // any packet. This will create a positive clock-drift of approx. 11%.
677 int num_packets = (i % 10 == 9 ? 0 : 1);
678 for (int n = 0; n < num_packets; ++n) {
679 uint8_t payload[kPayloadBytes] = {0};
680 WebRtcRTPHeader rtp_info;
681 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800682 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000683 ++frame_index;
684 }
685
686 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700687 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000688 int num_channels;
689 NetEqOutputType type;
690 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
691 &num_channels, &type));
692 ASSERT_EQ(kBlockSize16kHz, out_len);
693 }
694
695 NetEqNetworkStatistics network_stats;
696 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
697 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
698}
699
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000700void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
701 double network_freeze_ms,
702 bool pull_audio_during_freeze,
703 int delay_tolerance_ms,
704 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000705 uint16_t seq_no = 0;
706 uint32_t timestamp = 0;
707 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000708 const size_t kSamples = kFrameSizeMs * 16;
709 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000710 double next_input_time_ms = 0.0;
711 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700712 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000713 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 NetEqOutputType type;
715
716 // Insert speech for 5 seconds.
717 const int kSpeechDurationMs = 5000;
718 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
719 // Each turn in this for loop is 10 ms.
720 while (next_input_time_ms <= t_ms) {
721 // Insert one 30 ms speech frame.
722 uint8_t payload[kPayloadBytes] = {0};
723 WebRtcRTPHeader rtp_info;
724 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800725 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000726 ++seq_no;
727 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000728 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 }
730 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
732 &num_channels, &type));
733 ASSERT_EQ(kBlockSize16kHz, out_len);
734 }
735
736 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000737 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738
739 // Insert CNG for 1 minute (= 60000 ms).
740 const int kCngPeriodMs = 100;
741 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
742 const int kCngDurationMs = 60000;
743 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
744 // Each turn in this for loop is 10 ms.
745 while (next_input_time_ms <= t_ms) {
746 // Insert one CNG frame each 100 ms.
747 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000748 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749 WebRtcRTPHeader rtp_info;
750 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800751 ASSERT_EQ(0, neteq_->InsertPacket(
752 rtp_info,
753 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000754 ++seq_no;
755 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000756 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000757 }
758 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000759 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
760 &num_channels, &type));
761 ASSERT_EQ(kBlockSize16kHz, out_len);
762 }
763
764 EXPECT_EQ(kOutputCNG, type);
765
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000766 if (network_freeze_ms > 0) {
767 // First keep pulling audio for |network_freeze_ms| without inserting
768 // any data, then insert CNG data corresponding to |network_freeze_ms|
769 // without pulling any output audio.
770 const double loop_end_time = t_ms + network_freeze_ms;
771 for (; t_ms < loop_end_time; t_ms += 10) {
772 // Pull out data once.
773 ASSERT_EQ(0,
774 neteq_->GetAudio(
775 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
776 ASSERT_EQ(kBlockSize16kHz, out_len);
777 EXPECT_EQ(kOutputCNG, type);
778 }
779 bool pull_once = pull_audio_during_freeze;
780 // If |pull_once| is true, GetAudio will be called once half-way through
781 // the network recovery period.
782 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
783 while (next_input_time_ms <= t_ms) {
784 if (pull_once && next_input_time_ms >= pull_time_ms) {
785 pull_once = false;
786 // Pull out data once.
787 ASSERT_EQ(
788 0,
789 neteq_->GetAudio(
790 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
791 ASSERT_EQ(kBlockSize16kHz, out_len);
792 EXPECT_EQ(kOutputCNG, type);
793 t_ms += 10;
794 }
795 // Insert one CNG frame each 100 ms.
796 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000797 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000798 WebRtcRTPHeader rtp_info;
799 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800800 ASSERT_EQ(0, neteq_->InsertPacket(
801 rtp_info,
802 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000803 ++seq_no;
804 timestamp += kCngPeriodSamples;
805 next_input_time_ms += kCngPeriodMs * drift_factor;
806 }
807 }
808
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000809 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000810 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000811 while (type != kOutputNormal) {
812 // Each turn in this for loop is 10 ms.
813 while (next_input_time_ms <= t_ms) {
814 // Insert one 30 ms speech frame.
815 uint8_t payload[kPayloadBytes] = {0};
816 WebRtcRTPHeader rtp_info;
817 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800818 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000819 ++seq_no;
820 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000821 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000822 }
823 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000824 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
825 &num_channels, &type));
826 ASSERT_EQ(kBlockSize16kHz, out_len);
827 // Increase clock.
828 t_ms += 10;
829 }
830
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000831 // Check that the speech starts again within reasonable time.
832 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
833 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000834 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000835 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000836 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
837 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838}
839
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000840TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000841 // Apply a clock drift of -25 ms / s (sender faster than receiver).
842 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000843 const double kNetworkFreezeTimeMs = 0.0;
844 const bool kGetAudioDuringFreezeRecovery = false;
845 const int kDelayToleranceMs = 20;
846 const int kMaxTimeToSpeechMs = 100;
847 LongCngWithClockDrift(kDriftFactor,
848 kNetworkFreezeTimeMs,
849 kGetAudioDuringFreezeRecovery,
850 kDelayToleranceMs,
851 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000852}
853
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000854TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000855 // Apply a clock drift of +25 ms / s (sender slower than receiver).
856 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000857 const double kNetworkFreezeTimeMs = 0.0;
858 const bool kGetAudioDuringFreezeRecovery = false;
859 const int kDelayToleranceMs = 20;
860 const int kMaxTimeToSpeechMs = 100;
861 LongCngWithClockDrift(kDriftFactor,
862 kNetworkFreezeTimeMs,
863 kGetAudioDuringFreezeRecovery,
864 kDelayToleranceMs,
865 kMaxTimeToSpeechMs);
866}
867
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000868TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000869 // Apply a clock drift of -25 ms / s (sender faster than receiver).
870 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
871 const double kNetworkFreezeTimeMs = 5000.0;
872 const bool kGetAudioDuringFreezeRecovery = false;
873 const int kDelayToleranceMs = 50;
874 const int kMaxTimeToSpeechMs = 200;
875 LongCngWithClockDrift(kDriftFactor,
876 kNetworkFreezeTimeMs,
877 kGetAudioDuringFreezeRecovery,
878 kDelayToleranceMs,
879 kMaxTimeToSpeechMs);
880}
881
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000882TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000883 // Apply a clock drift of +25 ms / s (sender slower than receiver).
884 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
885 const double kNetworkFreezeTimeMs = 5000.0;
886 const bool kGetAudioDuringFreezeRecovery = false;
887 const int kDelayToleranceMs = 20;
888 const int kMaxTimeToSpeechMs = 100;
889 LongCngWithClockDrift(kDriftFactor,
890 kNetworkFreezeTimeMs,
891 kGetAudioDuringFreezeRecovery,
892 kDelayToleranceMs,
893 kMaxTimeToSpeechMs);
894}
895
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000896TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000897 // Apply a clock drift of +25 ms / s (sender slower than receiver).
898 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
899 const double kNetworkFreezeTimeMs = 5000.0;
900 const bool kGetAudioDuringFreezeRecovery = true;
901 const int kDelayToleranceMs = 20;
902 const int kMaxTimeToSpeechMs = 100;
903 LongCngWithClockDrift(kDriftFactor,
904 kNetworkFreezeTimeMs,
905 kGetAudioDuringFreezeRecovery,
906 kDelayToleranceMs,
907 kMaxTimeToSpeechMs);
908}
909
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000910TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000911 const double kDriftFactor = 1.0; // No drift.
912 const double kNetworkFreezeTimeMs = 0.0;
913 const bool kGetAudioDuringFreezeRecovery = false;
914 const int kDelayToleranceMs = 10;
915 const int kMaxTimeToSpeechMs = 50;
916 LongCngWithClockDrift(kDriftFactor,
917 kNetworkFreezeTimeMs,
918 kGetAudioDuringFreezeRecovery,
919 kDelayToleranceMs,
920 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000921}
922
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000923TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000924 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000925 uint8_t payload[kPayloadBytes] = {0};
926 WebRtcRTPHeader rtp_info;
927 PopulateRtpInfo(0, 0, &rtp_info);
928 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800929 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000930 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
931}
932
kwiberg98ab3a42015-09-30 21:54:21 -0700933#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
934#define IF_ISAC(x) x
935#else
936#define IF_ISAC(x) DISABLED_##x
937#endif
938
939TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000940 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000941 uint8_t payload[kPayloadBytes] = {0};
942 WebRtcRTPHeader rtp_info;
943 PopulateRtpInfo(0, 0, &rtp_info);
944 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800945 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000946 NetEqOutputType type;
947 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
948 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000949 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000950 out_data_[i] = 1;
951 }
952 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700953 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000954 EXPECT_EQ(NetEq::kFail,
955 neteq_->GetAudio(kMaxBlockSize, out_data_,
956 &samples_per_channel, &num_channels, &type));
957 // Verify that there is a decoder error to check.
958 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
959 // Code 6730 is an iSAC error code.
960 EXPECT_EQ(6730, neteq_->LastDecoderError());
961 // Verify that the first 160 samples are set to 0, and that the remaining
962 // samples are left unmodified.
963 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
964 for (int i = 0; i < kExpectedOutputLength; ++i) {
965 std::ostringstream ss;
966 ss << "i = " << i;
967 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
968 EXPECT_EQ(0, out_data_[i]);
969 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000970 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000971 std::ostringstream ss;
972 ss << "i = " << i;
973 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
974 EXPECT_EQ(1, out_data_[i]);
975 }
976}
977
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000978TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000979 NetEqOutputType type;
980 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
981 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000982 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000983 out_data_[i] = 1;
984 }
985 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700986 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000987 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
988 &samples_per_channel,
989 &num_channels, &type));
990 // Verify that the first block of samples is set to 0.
991 static const int kExpectedOutputLength =
992 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
993 for (int i = 0; i < kExpectedOutputLength; ++i) {
994 std::ostringstream ss;
995 ss << "i = " << i;
996 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
997 EXPECT_EQ(0, out_data_[i]);
998 }
henrik.lundind89814b2015-11-23 06:49:25 -0800999 // Verify that the sample rate did not change from the initial configuration.
1000 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001001}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001002
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001003class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001004 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001005 virtual void TestCondition(double sum_squared_noise,
1006 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001007
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001008 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07001009 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001010 uint8_t payload_type = 0xFF; // Invalid.
1011 if (sampling_rate_hz == 8000) {
1012 expected_samples_per_channel = kBlockSize8kHz;
1013 payload_type = 93; // PCM 16, 8 kHz.
1014 } else if (sampling_rate_hz == 16000) {
1015 expected_samples_per_channel = kBlockSize16kHz;
1016 payload_type = 94; // PCM 16, 16 kHZ.
1017 } else if (sampling_rate_hz == 32000) {
1018 expected_samples_per_channel = kBlockSize32kHz;
1019 payload_type = 95; // PCM 16, 32 kHz.
1020 } else {
1021 ASSERT_TRUE(false); // Unsupported test case.
1022 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001023
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001024 NetEqOutputType type;
1025 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001026 test::AudioLoop input;
1027 // We are using the same 32 kHz input file for all tests, regardless of
1028 // |sampling_rate_hz|. The output may sound weird, but the test is still
1029 // valid.
1030 ASSERT_TRUE(input.Init(
1031 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
1032 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001033 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001034
1035 // Payload of 10 ms of PCM16 32 kHz.
1036 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001037 WebRtcRTPHeader rtp_info;
1038 PopulateRtpInfo(0, 0, &rtp_info);
1039 rtp_info.header.payloadType = payload_type;
1040
1041 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001042 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001043
1044 uint32_t receive_timestamp = 0;
1045 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -08001046 auto block = input.GetNextBlock();
1047 ASSERT_EQ(expected_samples_per_channel, block.size());
1048 size_t enc_len_bytes =
1049 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001050 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
1051
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001052 number_channels = 0;
1053 samples_per_channel = 0;
kwibergee2bac22015-11-11 10:34:00 -08001054 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
1055 payload, enc_len_bytes),
1056 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001057 ASSERT_EQ(0,
1058 neteq_->GetAudio(kBlockSize32kHz,
1059 output,
1060 &samples_per_channel,
1061 &number_channels,
1062 &type));
1063 ASSERT_EQ(1, number_channels);
1064 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1065 ASSERT_EQ(kOutputNormal, type);
1066
1067 // Next packet.
1068 rtp_info.header.timestamp += expected_samples_per_channel;
1069 rtp_info.header.sequenceNumber++;
1070 receive_timestamp += expected_samples_per_channel;
1071 }
1072
1073 number_channels = 0;
1074 samples_per_channel = 0;
1075
1076 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1077 // one frame without checking speech-type. This is the first frame pulled
1078 // without inserting any packet, and might not be labeled as PLC.
1079 ASSERT_EQ(0,
1080 neteq_->GetAudio(kBlockSize32kHz,
1081 output,
1082 &samples_per_channel,
1083 &number_channels,
1084 &type));
1085 ASSERT_EQ(1, number_channels);
1086 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1087
1088 // To be able to test the fading of background noise we need at lease to
1089 // pull 611 frames.
1090 const int kFadingThreshold = 611;
1091
1092 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1093 // is arbitrary, but sufficiently large to test enough number of frames.
1094 const int kNumPlcToCngTestFrames = 20;
1095 bool plc_to_cng = false;
1096 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
1097 number_channels = 0;
1098 samples_per_channel = 0;
1099 memset(output, 1, sizeof(output)); // Set to non-zero.
1100 ASSERT_EQ(0,
1101 neteq_->GetAudio(kBlockSize32kHz,
1102 output,
1103 &samples_per_channel,
1104 &number_channels,
1105 &type));
1106 ASSERT_EQ(1, number_channels);
1107 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1108 if (type == kOutputPLCtoCNG) {
1109 plc_to_cng = true;
1110 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001111 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001112 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001113 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001114 } else {
1115 EXPECT_EQ(kOutputPLC, type);
1116 }
1117 }
1118 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1119 }
1120};
1121
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001122class NetEqBgnTestOn : public NetEqBgnTest {
1123 protected:
1124 NetEqBgnTestOn() : NetEqBgnTest() {
1125 config_.background_noise_mode = NetEq::kBgnOn;
1126 }
1127
1128 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1129 EXPECT_NE(0, sum_squared_noise);
1130 }
1131};
1132
1133class NetEqBgnTestOff : public NetEqBgnTest {
1134 protected:
1135 NetEqBgnTestOff() : NetEqBgnTest() {
1136 config_.background_noise_mode = NetEq::kBgnOff;
1137 }
1138
1139 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1140 EXPECT_EQ(0, sum_squared_noise);
1141 }
1142};
1143
1144class NetEqBgnTestFade : public NetEqBgnTest {
1145 protected:
1146 NetEqBgnTestFade() : NetEqBgnTest() {
1147 config_.background_noise_mode = NetEq::kBgnFade;
1148 }
1149
1150 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1151 if (should_be_faded)
1152 EXPECT_EQ(0, sum_squared_noise);
1153 }
1154};
1155
henrika1d34fe92015-06-16 10:04:20 +02001156TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001157 CheckBgn(8000);
1158 CheckBgn(16000);
1159 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001160}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001161
henrika1d34fe92015-06-16 10:04:20 +02001162TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001163 CheckBgn(8000);
1164 CheckBgn(16000);
1165 CheckBgn(32000);
1166}
1167
henrika1d34fe92015-06-16 10:04:20 +02001168TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001169 CheckBgn(8000);
1170 CheckBgn(16000);
1171 CheckBgn(32000);
1172}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001173
kwiberg98ab3a42015-09-30 21:54:21 -07001174TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001175 WebRtcRTPHeader rtp_info;
1176 uint32_t receive_timestamp = 0;
1177 // For the readability use the following payloads instead of the defaults of
1178 // this test.
1179 uint8_t kPcm16WbPayloadType = 1;
1180 uint8_t kCngNbPayloadType = 2;
1181 uint8_t kCngWbPayloadType = 3;
1182 uint8_t kCngSwb32PayloadType = 4;
1183 uint8_t kCngSwb48PayloadType = 5;
1184 uint8_t kAvtPayloadType = 6;
1185 uint8_t kRedPayloadType = 7;
1186 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1187
1188 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001189 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001190 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001191 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001192 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001193 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001194 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001195 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001196 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001197 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001198 "cng-swb48", kCngSwb48PayloadType));
1199 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001200 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001201 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001202 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001203 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001204 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001205
1206 PopulateRtpInfo(0, 0, &rtp_info);
1207 rtp_info.header.payloadType = kPcm16WbPayloadType;
1208
1209 // The first packet injected cannot be sync-packet.
1210 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1211
1212 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001213 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001214 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001215 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001216
1217 // Next packet. Last packet contained 10 ms audio.
1218 rtp_info.header.sequenceNumber++;
1219 rtp_info.header.timestamp += kBlockSize16kHz;
1220 receive_timestamp += kBlockSize16kHz;
1221
1222 // Unacceptable payload types CNG, AVT (DTMF), RED.
1223 rtp_info.header.payloadType = kCngNbPayloadType;
1224 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1225
1226 rtp_info.header.payloadType = kCngWbPayloadType;
1227 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1228
1229 rtp_info.header.payloadType = kCngSwb32PayloadType;
1230 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1231
1232 rtp_info.header.payloadType = kCngSwb48PayloadType;
1233 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1234
1235 rtp_info.header.payloadType = kAvtPayloadType;
1236 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1237
1238 rtp_info.header.payloadType = kRedPayloadType;
1239 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1240
1241 // Change of codec cannot be initiated with a sync packet.
1242 rtp_info.header.payloadType = kIsacPayloadType;
1243 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1244
1245 // Change of SSRC is not allowed with a sync packet.
1246 rtp_info.header.payloadType = kPcm16WbPayloadType;
1247 ++rtp_info.header.ssrc;
1248 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1249
1250 --rtp_info.header.ssrc;
1251 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1252}
1253
1254// First insert several noise like packets, then sync-packets. Decoding all
1255// packets should not produce error, statistics should not show any packet loss
1256// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001257// TODO(turajs) we will have a better test if we have a referece NetEq, and
1258// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1259// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001260TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001261 WebRtcRTPHeader rtp_info;
1262 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001263 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001264 uint8_t payload[kPayloadBytes];
1265 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001266 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001267 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001268 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1269 }
1270 // Insert some packets which decode to noise. We are not interested in
1271 // actual decoded values.
1272 NetEqOutputType output_type;
1273 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001274 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001275 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001276 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001277 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001278 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1279 &samples_per_channel, &num_channels,
1280 &output_type));
1281 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1282 ASSERT_EQ(1, num_channels);
1283
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001284 rtp_info.header.sequenceNumber++;
1285 rtp_info.header.timestamp += kBlockSize16kHz;
1286 receive_timestamp += kBlockSize16kHz;
1287 }
1288 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001289
1290 // Make sure sufficient number of sync packets are inserted that we can
1291 // conduct a test.
1292 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001293 // Insert sync-packets, the decoded sequence should be all-zero.
1294 for (int n = 0; n < kNumSyncPackets; ++n) {
1295 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1296 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1297 &samples_per_channel, &num_channels,
1298 &output_type));
1299 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1300 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001301 if (n > algorithmic_frame_delay) {
1302 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1303 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001304 rtp_info.header.sequenceNumber++;
1305 rtp_info.header.timestamp += kBlockSize16kHz;
1306 receive_timestamp += kBlockSize16kHz;
1307 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001308
1309 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001310 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001311 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001312 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001313 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1314 &samples_per_channel, &num_channels,
1315 &output_type));
1316 if (n >= algorithmic_frame_delay + 1) {
1317 // Expect that this frame contain samples from regular RTP.
1318 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1319 }
1320 rtp_info.header.sequenceNumber++;
1321 rtp_info.header.timestamp += kBlockSize16kHz;
1322 receive_timestamp += kBlockSize16kHz;
1323 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001324 NetEqNetworkStatistics network_stats;
1325 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1326 // Expecting a "clean" network.
1327 EXPECT_EQ(0, network_stats.packet_loss_rate);
1328 EXPECT_EQ(0, network_stats.expand_rate);
1329 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001330 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001331}
1332
1333// Test if the size of the packet buffer reported correctly when containing
1334// sync packets. Also, test if network packets override sync packets. That is to
1335// prefer decoding a network packet to a sync packet, if both have same sequence
1336// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001337TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001338 WebRtcRTPHeader rtp_info;
1339 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001340 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001341 uint8_t payload[kPayloadBytes];
1342 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001343 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001344 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1345 }
1346 // Insert some packets which decode to noise. We are not interested in
1347 // actual decoded values.
1348 NetEqOutputType output_type;
1349 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001350 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001351 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001352 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1353 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001354 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001355 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1356 &samples_per_channel, &num_channels,
1357 &output_type));
1358 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1359 ASSERT_EQ(1, num_channels);
1360 rtp_info.header.sequenceNumber++;
1361 rtp_info.header.timestamp += kBlockSize16kHz;
1362 receive_timestamp += kBlockSize16kHz;
1363 }
1364 const int kNumSyncPackets = 10;
1365
1366 WebRtcRTPHeader first_sync_packet_rtp_info;
1367 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1368
1369 // Insert sync-packets, but no decoding.
1370 for (int n = 0; n < kNumSyncPackets; ++n) {
1371 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1372 rtp_info.header.sequenceNumber++;
1373 rtp_info.header.timestamp += kBlockSize16kHz;
1374 receive_timestamp += kBlockSize16kHz;
1375 }
1376 NetEqNetworkStatistics network_stats;
1377 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001378 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1379 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001380
1381 // Rewind |rtp_info| to that of the first sync packet.
1382 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1383
1384 // Insert.
1385 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001386 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001387 rtp_info.header.sequenceNumber++;
1388 rtp_info.header.timestamp += kBlockSize16kHz;
1389 receive_timestamp += kBlockSize16kHz;
1390 }
1391
1392 // Decode.
1393 for (int n = 0; n < kNumSyncPackets; ++n) {
1394 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1395 &samples_per_channel, &num_channels,
1396 &output_type));
1397 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1398 ASSERT_EQ(1, num_channels);
1399 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1400 }
1401}
1402
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001403void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1404 uint32_t start_timestamp,
1405 const std::set<uint16_t>& drop_seq_numbers,
1406 bool expect_seq_no_wrap,
1407 bool expect_timestamp_wrap) {
1408 uint16_t seq_no = start_seq_no;
1409 uint32_t timestamp = start_timestamp;
1410 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1411 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1412 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001413 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001414 double next_input_time_ms = 0.0;
1415 int16_t decoded[kBlockSize16kHz];
1416 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001417 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001418 NetEqOutputType output_type;
1419 uint32_t receive_timestamp = 0;
1420
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001421 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001422 const int kSpeechDurationMs = 2000;
1423 int packets_inserted = 0;
1424 uint16_t last_seq_no;
1425 uint32_t last_timestamp;
1426 bool timestamp_wrapped = false;
1427 bool seq_no_wrapped = false;
1428 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1429 // Each turn in this for loop is 10 ms.
1430 while (next_input_time_ms <= t_ms) {
1431 // Insert one 30 ms speech frame.
1432 uint8_t payload[kPayloadBytes] = {0};
1433 WebRtcRTPHeader rtp_info;
1434 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1435 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1436 // This sequence number was not in the set to drop. Insert it.
1437 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001438 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001439 ++packets_inserted;
1440 }
1441 NetEqNetworkStatistics network_stats;
1442 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1443
1444 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1445 // packet size for first few packets. Therefore we refrain from checking
1446 // the criteria.
1447 if (packets_inserted > 4) {
1448 // Expect preferred and actual buffer size to be no more than 2 frames.
1449 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001450 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1451 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001452 }
1453 last_seq_no = seq_no;
1454 last_timestamp = timestamp;
1455
1456 ++seq_no;
1457 timestamp += kSamples;
1458 receive_timestamp += kSamples;
1459 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1460
1461 seq_no_wrapped |= seq_no < last_seq_no;
1462 timestamp_wrapped |= timestamp < last_timestamp;
1463 }
1464 // Pull out data once.
1465 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1466 &samples_per_channel, &num_channels,
1467 &output_type));
1468 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1469 ASSERT_EQ(1, num_channels);
1470
1471 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001472 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001473 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001474 }
1475 // Make sure we have actually tested wrap-around.
1476 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1477 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1478}
1479
1480TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1481 // Start with a sequence number that will soon wrap.
1482 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1483 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1484}
1485
1486TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1487 // Start with a sequence number that will soon wrap.
1488 std::set<uint16_t> drop_seq_numbers;
1489 drop_seq_numbers.insert(0xFFFF);
1490 drop_seq_numbers.insert(0x0);
1491 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1492}
1493
1494TEST_F(NetEqDecodingTest, TimestampWrap) {
1495 // Start with a timestamp that will soon wrap.
1496 std::set<uint16_t> drop_seq_numbers;
1497 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1498}
1499
1500TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1501 // Start with a timestamp and a sequence number that will wrap at the same
1502 // time.
1503 std::set<uint16_t> drop_seq_numbers;
1504 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1505}
1506
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001507void NetEqDecodingTest::DuplicateCng() {
1508 uint16_t seq_no = 0;
1509 uint32_t timestamp = 0;
1510 const int kFrameSizeMs = 10;
1511 const int kSampleRateKhz = 16;
1512 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001513 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001514
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001515 const int algorithmic_delay_samples = std::max(
1516 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001517 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001518 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001519 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001520 int num_channels;
1521 NetEqOutputType type;
1522 uint8_t payload[kPayloadBytes] = {0};
1523 WebRtcRTPHeader rtp_info;
1524 for (int i = 0; i < 3; ++i) {
1525 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001526 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001527 ++seq_no;
1528 timestamp += kSamples;
1529
1530 // Pull audio once.
1531 ASSERT_EQ(0,
1532 neteq_->GetAudio(
1533 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1534 ASSERT_EQ(kBlockSize16kHz, out_len);
1535 }
1536 // Verify speech output.
1537 EXPECT_EQ(kOutputNormal, type);
1538
1539 // Insert same CNG packet twice.
1540 const int kCngPeriodMs = 100;
1541 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001542 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001543 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1544 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001545 ASSERT_EQ(
1546 0, neteq_->InsertPacket(
1547 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001548
1549 // Pull audio once and make sure CNG is played.
1550 ASSERT_EQ(0,
1551 neteq_->GetAudio(
1552 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1553 ASSERT_EQ(kBlockSize16kHz, out_len);
1554 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001555 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001556
1557 // Insert the same CNG packet again. Note that at this point it is old, since
1558 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001559 ASSERT_EQ(
1560 0, neteq_->InsertPacket(
1561 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001562
1563 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1564 // we have already pulled out CNG once.
1565 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1566 ASSERT_EQ(0,
1567 neteq_->GetAudio(
1568 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1569 ASSERT_EQ(kBlockSize16kHz, out_len);
1570 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001571 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001572 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001573 }
1574
1575 // Insert speech again.
1576 ++seq_no;
1577 timestamp += kCngPeriodSamples;
1578 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001579 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001580
1581 // Pull audio once and verify that the output is speech again.
1582 ASSERT_EQ(0,
1583 neteq_->GetAudio(
1584 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1585 ASSERT_EQ(kBlockSize16kHz, out_len);
1586 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001587 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001588 PlayoutTimestamp());
1589}
1590
1591uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1592 uint32_t playout_timestamp = 0;
1593 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1594 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001595}
1596
1597TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001598
1599TEST_F(NetEqDecodingTest, CngFirst) {
1600 uint16_t seq_no = 0;
1601 uint32_t timestamp = 0;
1602 const int kFrameSizeMs = 10;
1603 const int kSampleRateKhz = 16;
1604 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1605 const int kPayloadBytes = kSamples * 2;
1606 const int kCngPeriodMs = 100;
1607 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1608 size_t payload_len;
1609
1610 uint8_t payload[kPayloadBytes] = {0};
1611 WebRtcRTPHeader rtp_info;
1612
1613 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001614 ASSERT_EQ(
1615 NetEq::kOK,
1616 neteq_->InsertPacket(
1617 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001618 ++seq_no;
1619 timestamp += kCngPeriodSamples;
1620
1621 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001622 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001623 int num_channels;
1624 NetEqOutputType type;
1625 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1626 &num_channels, &type));
1627 ASSERT_EQ(kBlockSize16kHz, out_len);
1628 EXPECT_EQ(kOutputCNG, type);
1629
1630 // Insert some speech packets.
1631 for (int i = 0; i < 3; ++i) {
1632 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001633 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001634 ++seq_no;
1635 timestamp += kSamples;
1636
1637 // Pull audio once.
1638 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1639 &num_channels, &type));
1640 ASSERT_EQ(kBlockSize16kHz, out_len);
1641 }
1642 // Verify speech output.
1643 EXPECT_EQ(kOutputNormal, type);
1644}
1645
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001646} // namespace webrtc