blob: 04cef8aa99e7f1de8f2fbce8a7f3004aeaada010 [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());
232 ASSERT_EQ(stats.secondary_decoded_rate, 0);
233 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;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000282 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 static const int kInitSampleRateHz = 8000;
284
285 NetEqDecodingTest();
286 virtual void SetUp();
287 virtual void TearDown();
288 void SelectDecoders(NetEqDecoder* used_codec);
289 void LoadDecoders();
290 void OpenInputFile(const std::string &rtp_file);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700291 void Process(size_t* out_len);
minyue5f026d02015-12-16 07:36:04 -0800292
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000293 void DecodeAndCompare(const std::string& rtp_file,
294 const std::string& ref_file,
295 const std::string& stat_ref_file,
296 const std::string& rtcp_ref_file);
minyue5f026d02015-12-16 07:36:04 -0800297
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 static void PopulateRtpInfo(int frame_index,
299 int timestamp,
300 WebRtcRTPHeader* rtp_info);
301 static void PopulateCng(int frame_index,
302 int timestamp,
303 WebRtcRTPHeader* rtp_info,
304 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000305 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000307 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
308 const std::set<uint16_t>& drop_seq_numbers,
309 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
310
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000311 void LongCngWithClockDrift(double drift_factor,
312 double network_freeze_ms,
313 bool pull_audio_during_freeze,
314 int delay_tolerance_ms,
315 int max_time_to_speech_ms);
316
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000317 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000318
wu@webrtc.org94454b72014-06-05 20:34:08 +0000319 uint32_t PlayoutTimestamp();
320
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000322 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000323 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
324 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 unsigned int sim_clock_;
326 int16_t out_data_[kMaxBlockSize];
327 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000328 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329};
330
331// Allocating the static const so that it can be passed by reference.
332const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700333const size_t NetEqDecodingTest::kBlockSize8kHz;
334const size_t NetEqDecodingTest::kBlockSize16kHz;
335const size_t NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000336const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337const int NetEqDecodingTest::kInitSampleRateHz;
338
339NetEqDecodingTest::NetEqDecodingTest()
340 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000341 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000343 output_sample_rate_(kInitSampleRateHz),
344 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000345 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 memset(out_data_, 0, sizeof(out_data_));
347}
348
349void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000350 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000351 NetEqNetworkStatistics stat;
352 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
353 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 ASSERT_TRUE(neteq_);
355 LoadDecoders();
356}
357
358void NetEqDecodingTest::TearDown() {
359 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360}
361
362void NetEqDecodingTest::LoadDecoders() {
363 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800364 ASSERT_EQ(0,
365 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800367 ASSERT_EQ(0,
368 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700369#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800371 ASSERT_EQ(
372 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700373#endif
374#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800376 ASSERT_EQ(
377 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700378#endif
379#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800381 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
382 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700383#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800385 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
386 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800388 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
389 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800391 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
392 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800394 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
395 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800397 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
398 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399}
400
401void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000402 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403}
404
Peter Kastingdce40cf2015-08-24 14:52:23 -0700405void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000406 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000407 while (packet_ && sim_clock_ >= packet_->time_ms()) {
408 if (packet_->payload_length_bytes() > 0) {
409 WebRtcRTPHeader rtp_header;
410 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000411 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800412 rtp_header,
413 rtc::ArrayView<const uint8_t>(
414 packet_->payload(), packet_->payload_length_bytes()),
415 static_cast<uint32_t>(packet_->time_ms() *
416 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000417 }
418 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000419 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000420 }
421
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000422 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423 NetEqOutputType type;
424 int num_channels;
425 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
426 &num_channels, &type));
427 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
428 (*out_len == kBlockSize16kHz) ||
429 (*out_len == kBlockSize32kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700430 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundind89814b2015-11-23 06:49:25 -0800431 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432
433 // Increase time.
434 sim_clock_ += kTimeStepMs;
435}
436
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000437void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
438 const std::string& ref_file,
439 const std::string& stat_ref_file,
440 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441 OpenInputFile(rtp_file);
442
443 std::string ref_out_file = "";
444 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000445 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 }
447 RefFiles ref_files(ref_file, ref_out_file);
448
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000449 std::string stat_out_file = "";
450 if (stat_ref_file.empty()) {
451 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
452 }
453 RefFiles network_stat_files(stat_ref_file, stat_out_file);
454
455 std::string rtcp_out_file = "";
456 if (rtcp_ref_file.empty()) {
457 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
458 }
459 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
460
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000461 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000462 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000463 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000464 std::ostringstream ss;
465 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
466 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700467 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000468 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000469 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000470
471 // Query the network statistics API once per second
472 if (sim_clock_ % 1000 == 0) {
473 // Process NetworkStatistics.
474 NetEqNetworkStatistics network_stats;
475 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000476 ASSERT_NO_FATAL_FAILURE(
477 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700478 // Compare with CurrentDelay, which should be identical.
479 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000480
481 // Process RTCPstat.
482 RtcpStatistics rtcp_stats;
483 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000484 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000485 }
486 }
487}
488
489void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
490 int timestamp,
491 WebRtcRTPHeader* rtp_info) {
492 rtp_info->header.sequenceNumber = frame_index;
493 rtp_info->header.timestamp = timestamp;
494 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
495 rtp_info->header.payloadType = 94; // PCM16b WB codec.
496 rtp_info->header.markerBit = 0;
497}
498
499void NetEqDecodingTest::PopulateCng(int frame_index,
500 int timestamp,
501 WebRtcRTPHeader* rtp_info,
502 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000503 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000504 rtp_info->header.sequenceNumber = frame_index;
505 rtp_info->header.timestamp = timestamp;
506 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
507 rtp_info->header.payloadType = 98; // WB CNG.
508 rtp_info->header.markerBit = 0;
509 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
510 *payload_len = 1; // Only noise level, no spectral parameters.
511}
512
minyue5f026d02015-12-16 07:36:04 -0800513#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
514 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
515 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
kwiberg98ab3a42015-09-30 21:54:21 -0700516 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
minyue5f026d02015-12-16 07:36:04 -0800517#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700518#else
minyue5f026d02015-12-16 07:36:04 -0800519#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700520#endif
minyue5f026d02015-12-16 07:36:04 -0800521TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000522 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000523 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000524 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
525 // are identical. The latter could have been removed, but if clients still
526 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000527 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000528 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000529#if defined(_MSC_VER) && (_MSC_VER >= 1700)
530 // For Visual Studio 2012 and later, we will have to use the generic reference
531 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000532 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000533 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000534#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000535 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000536 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000537#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000538 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000539 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000540
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000541 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000542 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000543 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000544 DecodeAndCompare(input_rtp_file,
545 input_ref_file,
546 network_stat_ref_file,
547 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000548 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000549}
550
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000551// Use fax mode to avoid time-scaling. This is to simplify the testing of
552// packet waiting times in the packet buffer.
553class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
554 protected:
555 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
556 config_.playout_mode = kPlayoutFax;
557 }
558};
559
560TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
562 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000563 const size_t kSamples = 10 * 16;
564 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000565 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800566 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 WebRtcRTPHeader rtp_info;
568 rtp_info.header.sequenceNumber = i;
569 rtp_info.header.timestamp = i * kSamples;
570 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
571 rtp_info.header.payloadType = 94; // PCM16b WB codec.
572 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800573 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574 }
575 // Pull out all data.
576 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700577 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578 int num_channels;
579 NetEqOutputType type;
580 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
581 &num_channels, &type));
582 ASSERT_EQ(kBlockSize16kHz, out_len);
583 }
584
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200585 NetEqNetworkStatistics stats;
586 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
588 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200589 // each packet. Thus, we are calculating the statistics for a series from 10
590 // to 300, in steps of 10 ms.
591 EXPECT_EQ(155, stats.mean_waiting_time_ms);
592 EXPECT_EQ(155, stats.median_waiting_time_ms);
593 EXPECT_EQ(10, stats.min_waiting_time_ms);
594 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595
596 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200597 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
598 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
599 EXPECT_EQ(-1, stats.median_waiting_time_ms);
600 EXPECT_EQ(-1, stats.min_waiting_time_ms);
601 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602}
603
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000604TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 const int kNumFrames = 3000; // Needed for convergence.
606 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000607 const size_t kSamples = 10 * 16;
608 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609 while (frame_index < kNumFrames) {
610 // Insert one packet each time, except every 10th time where we insert two
611 // packets at once. This will create a negative clock-drift of approx. 10%.
612 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
613 for (int n = 0; n < num_packets; ++n) {
614 uint8_t payload[kPayloadBytes] = {0};
615 WebRtcRTPHeader rtp_info;
616 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800617 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000618 ++frame_index;
619 }
620
621 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700622 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000623 int num_channels;
624 NetEqOutputType type;
625 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
626 &num_channels, &type));
627 ASSERT_EQ(kBlockSize16kHz, out_len);
628 }
629
630 NetEqNetworkStatistics network_stats;
631 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
632 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
633}
634
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000635TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 const int kNumFrames = 5000; // Needed for convergence.
637 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000638 const size_t kSamples = 10 * 16;
639 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640 for (int i = 0; i < kNumFrames; ++i) {
641 // Insert one packet each time, except every 10th time where we don't insert
642 // any packet. This will create a positive clock-drift of approx. 11%.
643 int num_packets = (i % 10 == 9 ? 0 : 1);
644 for (int n = 0; n < num_packets; ++n) {
645 uint8_t payload[kPayloadBytes] = {0};
646 WebRtcRTPHeader rtp_info;
647 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800648 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649 ++frame_index;
650 }
651
652 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700653 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000654 int num_channels;
655 NetEqOutputType type;
656 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
657 &num_channels, &type));
658 ASSERT_EQ(kBlockSize16kHz, out_len);
659 }
660
661 NetEqNetworkStatistics network_stats;
662 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
663 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
664}
665
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000666void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
667 double network_freeze_ms,
668 bool pull_audio_during_freeze,
669 int delay_tolerance_ms,
670 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000671 uint16_t seq_no = 0;
672 uint32_t timestamp = 0;
673 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000674 const size_t kSamples = kFrameSizeMs * 16;
675 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000676 double next_input_time_ms = 0.0;
677 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000679 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000680 NetEqOutputType type;
681
682 // Insert speech for 5 seconds.
683 const int kSpeechDurationMs = 5000;
684 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
685 // Each turn in this for loop is 10 ms.
686 while (next_input_time_ms <= t_ms) {
687 // Insert one 30 ms speech frame.
688 uint8_t payload[kPayloadBytes] = {0};
689 WebRtcRTPHeader rtp_info;
690 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800691 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692 ++seq_no;
693 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000694 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000695 }
696 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000697 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
698 &num_channels, &type));
699 ASSERT_EQ(kBlockSize16kHz, out_len);
700 }
701
702 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000703 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000704
705 // Insert CNG for 1 minute (= 60000 ms).
706 const int kCngPeriodMs = 100;
707 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
708 const int kCngDurationMs = 60000;
709 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
710 // Each turn in this for loop is 10 ms.
711 while (next_input_time_ms <= t_ms) {
712 // Insert one CNG frame each 100 ms.
713 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000714 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000715 WebRtcRTPHeader rtp_info;
716 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800717 ASSERT_EQ(0, neteq_->InsertPacket(
718 rtp_info,
719 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 ++seq_no;
721 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000722 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000723 }
724 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000725 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
726 &num_channels, &type));
727 ASSERT_EQ(kBlockSize16kHz, out_len);
728 }
729
730 EXPECT_EQ(kOutputCNG, type);
731
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 if (network_freeze_ms > 0) {
733 // First keep pulling audio for |network_freeze_ms| without inserting
734 // any data, then insert CNG data corresponding to |network_freeze_ms|
735 // without pulling any output audio.
736 const double loop_end_time = t_ms + network_freeze_ms;
737 for (; t_ms < loop_end_time; t_ms += 10) {
738 // Pull out data once.
739 ASSERT_EQ(0,
740 neteq_->GetAudio(
741 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
742 ASSERT_EQ(kBlockSize16kHz, out_len);
743 EXPECT_EQ(kOutputCNG, type);
744 }
745 bool pull_once = pull_audio_during_freeze;
746 // If |pull_once| is true, GetAudio will be called once half-way through
747 // the network recovery period.
748 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
749 while (next_input_time_ms <= t_ms) {
750 if (pull_once && next_input_time_ms >= pull_time_ms) {
751 pull_once = false;
752 // Pull out data once.
753 ASSERT_EQ(
754 0,
755 neteq_->GetAudio(
756 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
757 ASSERT_EQ(kBlockSize16kHz, out_len);
758 EXPECT_EQ(kOutputCNG, type);
759 t_ms += 10;
760 }
761 // Insert one CNG frame each 100 ms.
762 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000763 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000764 WebRtcRTPHeader rtp_info;
765 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800766 ASSERT_EQ(0, neteq_->InsertPacket(
767 rtp_info,
768 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000769 ++seq_no;
770 timestamp += kCngPeriodSamples;
771 next_input_time_ms += kCngPeriodMs * drift_factor;
772 }
773 }
774
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000775 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000776 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000777 while (type != kOutputNormal) {
778 // Each turn in this for loop is 10 ms.
779 while (next_input_time_ms <= t_ms) {
780 // Insert one 30 ms speech frame.
781 uint8_t payload[kPayloadBytes] = {0};
782 WebRtcRTPHeader rtp_info;
783 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800784 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000785 ++seq_no;
786 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000787 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000788 }
789 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000790 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
791 &num_channels, &type));
792 ASSERT_EQ(kBlockSize16kHz, out_len);
793 // Increase clock.
794 t_ms += 10;
795 }
796
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000797 // Check that the speech starts again within reasonable time.
798 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
799 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000800 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000801 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000802 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
803 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000804}
805
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000806TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000807 // Apply a clock drift of -25 ms / s (sender faster than receiver).
808 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000809 const double kNetworkFreezeTimeMs = 0.0;
810 const bool kGetAudioDuringFreezeRecovery = false;
811 const int kDelayToleranceMs = 20;
812 const int kMaxTimeToSpeechMs = 100;
813 LongCngWithClockDrift(kDriftFactor,
814 kNetworkFreezeTimeMs,
815 kGetAudioDuringFreezeRecovery,
816 kDelayToleranceMs,
817 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000818}
819
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000820TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000821 // Apply a clock drift of +25 ms / s (sender slower than receiver).
822 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000823 const double kNetworkFreezeTimeMs = 0.0;
824 const bool kGetAudioDuringFreezeRecovery = false;
825 const int kDelayToleranceMs = 20;
826 const int kMaxTimeToSpeechMs = 100;
827 LongCngWithClockDrift(kDriftFactor,
828 kNetworkFreezeTimeMs,
829 kGetAudioDuringFreezeRecovery,
830 kDelayToleranceMs,
831 kMaxTimeToSpeechMs);
832}
833
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000834TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000835 // Apply a clock drift of -25 ms / s (sender faster than receiver).
836 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
837 const double kNetworkFreezeTimeMs = 5000.0;
838 const bool kGetAudioDuringFreezeRecovery = false;
839 const int kDelayToleranceMs = 50;
840 const int kMaxTimeToSpeechMs = 200;
841 LongCngWithClockDrift(kDriftFactor,
842 kNetworkFreezeTimeMs,
843 kGetAudioDuringFreezeRecovery,
844 kDelayToleranceMs,
845 kMaxTimeToSpeechMs);
846}
847
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000848TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000849 // Apply a clock drift of +25 ms / s (sender slower than receiver).
850 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
851 const double kNetworkFreezeTimeMs = 5000.0;
852 const bool kGetAudioDuringFreezeRecovery = false;
853 const int kDelayToleranceMs = 20;
854 const int kMaxTimeToSpeechMs = 100;
855 LongCngWithClockDrift(kDriftFactor,
856 kNetworkFreezeTimeMs,
857 kGetAudioDuringFreezeRecovery,
858 kDelayToleranceMs,
859 kMaxTimeToSpeechMs);
860}
861
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000862TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000863 // Apply a clock drift of +25 ms / s (sender slower than receiver).
864 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
865 const double kNetworkFreezeTimeMs = 5000.0;
866 const bool kGetAudioDuringFreezeRecovery = true;
867 const int kDelayToleranceMs = 20;
868 const int kMaxTimeToSpeechMs = 100;
869 LongCngWithClockDrift(kDriftFactor,
870 kNetworkFreezeTimeMs,
871 kGetAudioDuringFreezeRecovery,
872 kDelayToleranceMs,
873 kMaxTimeToSpeechMs);
874}
875
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000876TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000877 const double kDriftFactor = 1.0; // No drift.
878 const double kNetworkFreezeTimeMs = 0.0;
879 const bool kGetAudioDuringFreezeRecovery = false;
880 const int kDelayToleranceMs = 10;
881 const int kMaxTimeToSpeechMs = 50;
882 LongCngWithClockDrift(kDriftFactor,
883 kNetworkFreezeTimeMs,
884 kGetAudioDuringFreezeRecovery,
885 kDelayToleranceMs,
886 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000887}
888
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000889TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000890 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000891 uint8_t payload[kPayloadBytes] = {0};
892 WebRtcRTPHeader rtp_info;
893 PopulateRtpInfo(0, 0, &rtp_info);
894 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800895 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000896 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
897}
898
kwiberg98ab3a42015-09-30 21:54:21 -0700899#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
900#define IF_ISAC(x) x
901#else
902#define IF_ISAC(x) DISABLED_##x
903#endif
904
905TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000906 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000907 uint8_t payload[kPayloadBytes] = {0};
908 WebRtcRTPHeader rtp_info;
909 PopulateRtpInfo(0, 0, &rtp_info);
910 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800911 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912 NetEqOutputType type;
913 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
914 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000915 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 out_data_[i] = 1;
917 }
918 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700919 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 EXPECT_EQ(NetEq::kFail,
921 neteq_->GetAudio(kMaxBlockSize, out_data_,
922 &samples_per_channel, &num_channels, &type));
923 // Verify that there is a decoder error to check.
924 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
925 // Code 6730 is an iSAC error code.
926 EXPECT_EQ(6730, neteq_->LastDecoderError());
927 // Verify that the first 160 samples are set to 0, and that the remaining
928 // samples are left unmodified.
929 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
930 for (int i = 0; i < kExpectedOutputLength; ++i) {
931 std::ostringstream ss;
932 ss << "i = " << i;
933 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
934 EXPECT_EQ(0, out_data_[i]);
935 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000936 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000937 std::ostringstream ss;
938 ss << "i = " << i;
939 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
940 EXPECT_EQ(1, out_data_[i]);
941 }
942}
943
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000944TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000945 NetEqOutputType type;
946 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
947 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000948 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000949 out_data_[i] = 1;
950 }
951 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700952 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000953 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
954 &samples_per_channel,
955 &num_channels, &type));
956 // Verify that the first block of samples is set to 0.
957 static const int kExpectedOutputLength =
958 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
959 for (int i = 0; i < kExpectedOutputLength; ++i) {
960 std::ostringstream ss;
961 ss << "i = " << i;
962 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
963 EXPECT_EQ(0, out_data_[i]);
964 }
henrik.lundind89814b2015-11-23 06:49:25 -0800965 // Verify that the sample rate did not change from the initial configuration.
966 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000967}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000968
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000969class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000970 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000971 virtual void TestCondition(double sum_squared_noise,
972 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000973
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000974 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700975 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000976 uint8_t payload_type = 0xFF; // Invalid.
977 if (sampling_rate_hz == 8000) {
978 expected_samples_per_channel = kBlockSize8kHz;
979 payload_type = 93; // PCM 16, 8 kHz.
980 } else if (sampling_rate_hz == 16000) {
981 expected_samples_per_channel = kBlockSize16kHz;
982 payload_type = 94; // PCM 16, 16 kHZ.
983 } else if (sampling_rate_hz == 32000) {
984 expected_samples_per_channel = kBlockSize32kHz;
985 payload_type = 95; // PCM 16, 32 kHz.
986 } else {
987 ASSERT_TRUE(false); // Unsupported test case.
988 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000989
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000990 NetEqOutputType type;
991 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000992 test::AudioLoop input;
993 // We are using the same 32 kHz input file for all tests, regardless of
994 // |sampling_rate_hz|. The output may sound weird, but the test is still
995 // valid.
996 ASSERT_TRUE(input.Init(
997 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
998 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700999 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001000
1001 // Payload of 10 ms of PCM16 32 kHz.
1002 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001003 WebRtcRTPHeader rtp_info;
1004 PopulateRtpInfo(0, 0, &rtp_info);
1005 rtp_info.header.payloadType = payload_type;
1006
1007 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001008 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001009
1010 uint32_t receive_timestamp = 0;
1011 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -08001012 auto block = input.GetNextBlock();
1013 ASSERT_EQ(expected_samples_per_channel, block.size());
1014 size_t enc_len_bytes =
1015 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001016 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
1017
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001018 number_channels = 0;
1019 samples_per_channel = 0;
kwibergee2bac22015-11-11 10:34:00 -08001020 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
1021 payload, enc_len_bytes),
1022 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001023 ASSERT_EQ(0,
1024 neteq_->GetAudio(kBlockSize32kHz,
1025 output,
1026 &samples_per_channel,
1027 &number_channels,
1028 &type));
1029 ASSERT_EQ(1, number_channels);
1030 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1031 ASSERT_EQ(kOutputNormal, type);
1032
1033 // Next packet.
1034 rtp_info.header.timestamp += expected_samples_per_channel;
1035 rtp_info.header.sequenceNumber++;
1036 receive_timestamp += expected_samples_per_channel;
1037 }
1038
1039 number_channels = 0;
1040 samples_per_channel = 0;
1041
1042 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1043 // one frame without checking speech-type. This is the first frame pulled
1044 // without inserting any packet, and might not be labeled as PLC.
1045 ASSERT_EQ(0,
1046 neteq_->GetAudio(kBlockSize32kHz,
1047 output,
1048 &samples_per_channel,
1049 &number_channels,
1050 &type));
1051 ASSERT_EQ(1, number_channels);
1052 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1053
1054 // To be able to test the fading of background noise we need at lease to
1055 // pull 611 frames.
1056 const int kFadingThreshold = 611;
1057
1058 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1059 // is arbitrary, but sufficiently large to test enough number of frames.
1060 const int kNumPlcToCngTestFrames = 20;
1061 bool plc_to_cng = false;
1062 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
1063 number_channels = 0;
1064 samples_per_channel = 0;
1065 memset(output, 1, sizeof(output)); // Set to non-zero.
1066 ASSERT_EQ(0,
1067 neteq_->GetAudio(kBlockSize32kHz,
1068 output,
1069 &samples_per_channel,
1070 &number_channels,
1071 &type));
1072 ASSERT_EQ(1, number_channels);
1073 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1074 if (type == kOutputPLCtoCNG) {
1075 plc_to_cng = true;
1076 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001077 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001078 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001079 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001080 } else {
1081 EXPECT_EQ(kOutputPLC, type);
1082 }
1083 }
1084 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1085 }
1086};
1087
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001088class NetEqBgnTestOn : public NetEqBgnTest {
1089 protected:
1090 NetEqBgnTestOn() : NetEqBgnTest() {
1091 config_.background_noise_mode = NetEq::kBgnOn;
1092 }
1093
1094 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1095 EXPECT_NE(0, sum_squared_noise);
1096 }
1097};
1098
1099class NetEqBgnTestOff : public NetEqBgnTest {
1100 protected:
1101 NetEqBgnTestOff() : NetEqBgnTest() {
1102 config_.background_noise_mode = NetEq::kBgnOff;
1103 }
1104
1105 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1106 EXPECT_EQ(0, sum_squared_noise);
1107 }
1108};
1109
1110class NetEqBgnTestFade : public NetEqBgnTest {
1111 protected:
1112 NetEqBgnTestFade() : NetEqBgnTest() {
1113 config_.background_noise_mode = NetEq::kBgnFade;
1114 }
1115
1116 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1117 if (should_be_faded)
1118 EXPECT_EQ(0, sum_squared_noise);
1119 }
1120};
1121
henrika1d34fe92015-06-16 10:04:20 +02001122TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001123 CheckBgn(8000);
1124 CheckBgn(16000);
1125 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001126}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001127
henrika1d34fe92015-06-16 10:04:20 +02001128TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001129 CheckBgn(8000);
1130 CheckBgn(16000);
1131 CheckBgn(32000);
1132}
1133
henrika1d34fe92015-06-16 10:04:20 +02001134TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001135 CheckBgn(8000);
1136 CheckBgn(16000);
1137 CheckBgn(32000);
1138}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001139
kwiberg98ab3a42015-09-30 21:54:21 -07001140TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001141 WebRtcRTPHeader rtp_info;
1142 uint32_t receive_timestamp = 0;
1143 // For the readability use the following payloads instead of the defaults of
1144 // this test.
1145 uint8_t kPcm16WbPayloadType = 1;
1146 uint8_t kCngNbPayloadType = 2;
1147 uint8_t kCngWbPayloadType = 3;
1148 uint8_t kCngSwb32PayloadType = 4;
1149 uint8_t kCngSwb48PayloadType = 5;
1150 uint8_t kAvtPayloadType = 6;
1151 uint8_t kRedPayloadType = 7;
1152 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1153
1154 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001155 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001156 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001157 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001158 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001159 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001160 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001161 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001162 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001163 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001164 "cng-swb48", kCngSwb48PayloadType));
1165 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001166 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001167 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001168 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001169 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001170 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001171
1172 PopulateRtpInfo(0, 0, &rtp_info);
1173 rtp_info.header.payloadType = kPcm16WbPayloadType;
1174
1175 // The first packet injected cannot be sync-packet.
1176 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1177
1178 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001179 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001180 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001181 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001182
1183 // Next packet. Last packet contained 10 ms audio.
1184 rtp_info.header.sequenceNumber++;
1185 rtp_info.header.timestamp += kBlockSize16kHz;
1186 receive_timestamp += kBlockSize16kHz;
1187
1188 // Unacceptable payload types CNG, AVT (DTMF), RED.
1189 rtp_info.header.payloadType = kCngNbPayloadType;
1190 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1191
1192 rtp_info.header.payloadType = kCngWbPayloadType;
1193 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1194
1195 rtp_info.header.payloadType = kCngSwb32PayloadType;
1196 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1197
1198 rtp_info.header.payloadType = kCngSwb48PayloadType;
1199 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1200
1201 rtp_info.header.payloadType = kAvtPayloadType;
1202 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1203
1204 rtp_info.header.payloadType = kRedPayloadType;
1205 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1206
1207 // Change of codec cannot be initiated with a sync packet.
1208 rtp_info.header.payloadType = kIsacPayloadType;
1209 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1210
1211 // Change of SSRC is not allowed with a sync packet.
1212 rtp_info.header.payloadType = kPcm16WbPayloadType;
1213 ++rtp_info.header.ssrc;
1214 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1215
1216 --rtp_info.header.ssrc;
1217 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1218}
1219
1220// First insert several noise like packets, then sync-packets. Decoding all
1221// packets should not produce error, statistics should not show any packet loss
1222// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001223// TODO(turajs) we will have a better test if we have a referece NetEq, and
1224// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1225// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001226TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001227 WebRtcRTPHeader rtp_info;
1228 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001229 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001230 uint8_t payload[kPayloadBytes];
1231 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001232 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001233 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001234 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1235 }
1236 // Insert some packets which decode to noise. We are not interested in
1237 // actual decoded values.
1238 NetEqOutputType output_type;
1239 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001240 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001241 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001242 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001243 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001244 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1245 &samples_per_channel, &num_channels,
1246 &output_type));
1247 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1248 ASSERT_EQ(1, num_channels);
1249
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001250 rtp_info.header.sequenceNumber++;
1251 rtp_info.header.timestamp += kBlockSize16kHz;
1252 receive_timestamp += kBlockSize16kHz;
1253 }
1254 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001255
1256 // Make sure sufficient number of sync packets are inserted that we can
1257 // conduct a test.
1258 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001259 // Insert sync-packets, the decoded sequence should be all-zero.
1260 for (int n = 0; n < kNumSyncPackets; ++n) {
1261 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1262 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1263 &samples_per_channel, &num_channels,
1264 &output_type));
1265 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1266 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001267 if (n > algorithmic_frame_delay) {
1268 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1269 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001270 rtp_info.header.sequenceNumber++;
1271 rtp_info.header.timestamp += kBlockSize16kHz;
1272 receive_timestamp += kBlockSize16kHz;
1273 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001274
1275 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001276 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001277 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001278 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001279 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1280 &samples_per_channel, &num_channels,
1281 &output_type));
1282 if (n >= algorithmic_frame_delay + 1) {
1283 // Expect that this frame contain samples from regular RTP.
1284 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1285 }
1286 rtp_info.header.sequenceNumber++;
1287 rtp_info.header.timestamp += kBlockSize16kHz;
1288 receive_timestamp += kBlockSize16kHz;
1289 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001290 NetEqNetworkStatistics network_stats;
1291 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1292 // Expecting a "clean" network.
1293 EXPECT_EQ(0, network_stats.packet_loss_rate);
1294 EXPECT_EQ(0, network_stats.expand_rate);
1295 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001296 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001297}
1298
1299// Test if the size of the packet buffer reported correctly when containing
1300// sync packets. Also, test if network packets override sync packets. That is to
1301// prefer decoding a network packet to a sync packet, if both have same sequence
1302// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001303TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001304 WebRtcRTPHeader rtp_info;
1305 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001306 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001307 uint8_t payload[kPayloadBytes];
1308 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001309 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001310 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1311 }
1312 // Insert some packets which decode to noise. We are not interested in
1313 // actual decoded values.
1314 NetEqOutputType output_type;
1315 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001316 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001317 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001318 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1319 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001320 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001321 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1322 &samples_per_channel, &num_channels,
1323 &output_type));
1324 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1325 ASSERT_EQ(1, num_channels);
1326 rtp_info.header.sequenceNumber++;
1327 rtp_info.header.timestamp += kBlockSize16kHz;
1328 receive_timestamp += kBlockSize16kHz;
1329 }
1330 const int kNumSyncPackets = 10;
1331
1332 WebRtcRTPHeader first_sync_packet_rtp_info;
1333 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1334
1335 // Insert sync-packets, but no decoding.
1336 for (int n = 0; n < kNumSyncPackets; ++n) {
1337 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1338 rtp_info.header.sequenceNumber++;
1339 rtp_info.header.timestamp += kBlockSize16kHz;
1340 receive_timestamp += kBlockSize16kHz;
1341 }
1342 NetEqNetworkStatistics network_stats;
1343 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001344 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1345 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001346
1347 // Rewind |rtp_info| to that of the first sync packet.
1348 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1349
1350 // Insert.
1351 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001352 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001353 rtp_info.header.sequenceNumber++;
1354 rtp_info.header.timestamp += kBlockSize16kHz;
1355 receive_timestamp += kBlockSize16kHz;
1356 }
1357
1358 // Decode.
1359 for (int n = 0; n < kNumSyncPackets; ++n) {
1360 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1361 &samples_per_channel, &num_channels,
1362 &output_type));
1363 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1364 ASSERT_EQ(1, num_channels);
1365 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1366 }
1367}
1368
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001369void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1370 uint32_t start_timestamp,
1371 const std::set<uint16_t>& drop_seq_numbers,
1372 bool expect_seq_no_wrap,
1373 bool expect_timestamp_wrap) {
1374 uint16_t seq_no = start_seq_no;
1375 uint32_t timestamp = start_timestamp;
1376 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1377 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1378 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001379 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001380 double next_input_time_ms = 0.0;
1381 int16_t decoded[kBlockSize16kHz];
1382 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001383 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001384 NetEqOutputType output_type;
1385 uint32_t receive_timestamp = 0;
1386
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001387 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001388 const int kSpeechDurationMs = 2000;
1389 int packets_inserted = 0;
1390 uint16_t last_seq_no;
1391 uint32_t last_timestamp;
1392 bool timestamp_wrapped = false;
1393 bool seq_no_wrapped = false;
1394 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1395 // Each turn in this for loop is 10 ms.
1396 while (next_input_time_ms <= t_ms) {
1397 // Insert one 30 ms speech frame.
1398 uint8_t payload[kPayloadBytes] = {0};
1399 WebRtcRTPHeader rtp_info;
1400 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1401 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1402 // This sequence number was not in the set to drop. Insert it.
1403 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001404 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001405 ++packets_inserted;
1406 }
1407 NetEqNetworkStatistics network_stats;
1408 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1409
1410 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1411 // packet size for first few packets. Therefore we refrain from checking
1412 // the criteria.
1413 if (packets_inserted > 4) {
1414 // Expect preferred and actual buffer size to be no more than 2 frames.
1415 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001416 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1417 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001418 }
1419 last_seq_no = seq_no;
1420 last_timestamp = timestamp;
1421
1422 ++seq_no;
1423 timestamp += kSamples;
1424 receive_timestamp += kSamples;
1425 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1426
1427 seq_no_wrapped |= seq_no < last_seq_no;
1428 timestamp_wrapped |= timestamp < last_timestamp;
1429 }
1430 // Pull out data once.
1431 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1432 &samples_per_channel, &num_channels,
1433 &output_type));
1434 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1435 ASSERT_EQ(1, num_channels);
1436
1437 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001438 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001439 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001440 }
1441 // Make sure we have actually tested wrap-around.
1442 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1443 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1444}
1445
1446TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1447 // Start with a sequence number that will soon wrap.
1448 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1449 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1450}
1451
1452TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1453 // Start with a sequence number that will soon wrap.
1454 std::set<uint16_t> drop_seq_numbers;
1455 drop_seq_numbers.insert(0xFFFF);
1456 drop_seq_numbers.insert(0x0);
1457 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1458}
1459
1460TEST_F(NetEqDecodingTest, TimestampWrap) {
1461 // Start with a timestamp that will soon wrap.
1462 std::set<uint16_t> drop_seq_numbers;
1463 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1464}
1465
1466TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1467 // Start with a timestamp and a sequence number that will wrap at the same
1468 // time.
1469 std::set<uint16_t> drop_seq_numbers;
1470 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1471}
1472
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001473void NetEqDecodingTest::DuplicateCng() {
1474 uint16_t seq_no = 0;
1475 uint32_t timestamp = 0;
1476 const int kFrameSizeMs = 10;
1477 const int kSampleRateKhz = 16;
1478 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001479 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001480
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001481 const int algorithmic_delay_samples = std::max(
1482 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001483 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001484 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001485 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001486 int num_channels;
1487 NetEqOutputType type;
1488 uint8_t payload[kPayloadBytes] = {0};
1489 WebRtcRTPHeader rtp_info;
1490 for (int i = 0; i < 3; ++i) {
1491 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001492 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001493 ++seq_no;
1494 timestamp += kSamples;
1495
1496 // Pull audio once.
1497 ASSERT_EQ(0,
1498 neteq_->GetAudio(
1499 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1500 ASSERT_EQ(kBlockSize16kHz, out_len);
1501 }
1502 // Verify speech output.
1503 EXPECT_EQ(kOutputNormal, type);
1504
1505 // Insert same CNG packet twice.
1506 const int kCngPeriodMs = 100;
1507 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001508 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001509 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1510 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001511 ASSERT_EQ(
1512 0, neteq_->InsertPacket(
1513 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001514
1515 // Pull audio once and make sure CNG is played.
1516 ASSERT_EQ(0,
1517 neteq_->GetAudio(
1518 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1519 ASSERT_EQ(kBlockSize16kHz, out_len);
1520 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001521 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001522
1523 // Insert the same CNG packet again. Note that at this point it is old, since
1524 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001525 ASSERT_EQ(
1526 0, neteq_->InsertPacket(
1527 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001528
1529 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1530 // we have already pulled out CNG once.
1531 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1532 ASSERT_EQ(0,
1533 neteq_->GetAudio(
1534 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1535 ASSERT_EQ(kBlockSize16kHz, out_len);
1536 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001537 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001538 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001539 }
1540
1541 // Insert speech again.
1542 ++seq_no;
1543 timestamp += kCngPeriodSamples;
1544 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001545 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001546
1547 // Pull audio once and verify that the output is speech again.
1548 ASSERT_EQ(0,
1549 neteq_->GetAudio(
1550 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1551 ASSERT_EQ(kBlockSize16kHz, out_len);
1552 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001553 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001554 PlayoutTimestamp());
1555}
1556
1557uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1558 uint32_t playout_timestamp = 0;
1559 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1560 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001561}
1562
1563TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001564
1565TEST_F(NetEqDecodingTest, CngFirst) {
1566 uint16_t seq_no = 0;
1567 uint32_t timestamp = 0;
1568 const int kFrameSizeMs = 10;
1569 const int kSampleRateKhz = 16;
1570 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1571 const int kPayloadBytes = kSamples * 2;
1572 const int kCngPeriodMs = 100;
1573 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1574 size_t payload_len;
1575
1576 uint8_t payload[kPayloadBytes] = {0};
1577 WebRtcRTPHeader rtp_info;
1578
1579 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001580 ASSERT_EQ(
1581 NetEq::kOK,
1582 neteq_->InsertPacket(
1583 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001584 ++seq_no;
1585 timestamp += kCngPeriodSamples;
1586
1587 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001588 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001589 int num_channels;
1590 NetEqOutputType type;
1591 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1592 &num_channels, &type));
1593 ASSERT_EQ(kBlockSize16kHz, out_len);
1594 EXPECT_EQ(kOutputCNG, type);
1595
1596 // Insert some speech packets.
1597 for (int i = 0; i < 3; ++i) {
1598 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001599 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001600 ++seq_no;
1601 timestamp += kSamples;
1602
1603 // Pull audio once.
1604 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1605 &num_channels, &type));
1606 ASSERT_EQ(kBlockSize16kHz, out_len);
1607 }
1608 // Verify speech output.
1609 EXPECT_EQ(kOutputNormal, type);
1610}
1611
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001612} // namespace webrtc