blob: f218f721ae99ec4b179d290696b0a01a11dba2c8 [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"
33#include "webrtc/typedefs.h"
34
minyue5f026d02015-12-16 07:36:04 -080035#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
36#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
37#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
38#else
39#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
40#endif
41#endif
42
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000043DEFINE_bool(gen_ref, false, "Generate reference files.");
44
minyue5f026d02015-12-16 07:36:04 -080045namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
minyue5f026d02015-12-16 07:36:04 -080047bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000048 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000050 all_zero = buf[n] == 0;
51 return all_zero;
52}
53
minyue5f026d02015-12-16 07:36:04 -080054bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000055 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000057 all_non_zero = buf[n] != 0;
58 return all_non_zero;
59}
60
minyue5f026d02015-12-16 07:36:04 -080061#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
62void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
63 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
64 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
65 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
66 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
67 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
68 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
69 stats->set_expand_rate(stats_raw.expand_rate);
70 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
71 stats->set_preemptive_rate(stats_raw.preemptive_rate);
72 stats->set_accelerate_rate(stats_raw.accelerate_rate);
73 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
74 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
75 stats->set_added_zero_samples(stats_raw.added_zero_samples);
76 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
77 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
78 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
79 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
80}
81
82void Convert(const webrtc::RtcpStatistics& stats_raw,
83 webrtc::neteq_unittest::RtcpStatistics* stats) {
84 stats->set_fraction_lost(stats_raw.fraction_lost);
85 stats->set_cumulative_lost(stats_raw.cumulative_lost);
86 stats->set_extended_max_sequence_number(
87 stats_raw.extended_max_sequence_number);
88 stats->set_jitter(stats_raw.jitter);
89}
90
91void WriteMessage(FILE* file, const std::string& message) {
92 int32_t size = message.length();
93 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
94 if (size <= 0)
95 return;
96 ASSERT_EQ(static_cast<size_t>(size),
97 fwrite(message.data(), sizeof(char), size, file));
98}
99
100void ReadMessage(FILE* file, std::string* message) {
101 int32_t size;
102 ASSERT_EQ(1u, fread(&size, sizeof(size), 1, file));
103 if (size <= 0)
104 return;
105 rtc::scoped_ptr<char[]> buffer(new char[size]);
106 ASSERT_EQ(static_cast<size_t>(size),
107 fread(buffer.get(), sizeof(char), size, file));
108 message->assign(buffer.get(), size);
109}
110#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
111
112} // namespace
113
114namespace webrtc {
115
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116class RefFiles {
117 public:
118 RefFiles(const std::string& input_file, const std::string& output_file);
119 ~RefFiles();
120 template<class T> void ProcessReference(const T& test_results);
121 template<typename T, size_t n> void ProcessReference(
122 const T (&test_results)[n],
123 size_t length);
124 template<typename T, size_t n> void WriteToFile(
125 const T (&test_results)[n],
126 size_t length);
127 template<typename T, size_t n> void ReadFromFileAndCompare(
128 const T (&test_results)[n],
129 size_t length);
130 void WriteToFile(const NetEqNetworkStatistics& stats);
131 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
132 void WriteToFile(const RtcpStatistics& stats);
133 void ReadFromFileAndCompare(const RtcpStatistics& stats);
134
135 FILE* input_fp_;
136 FILE* output_fp_;
137};
138
139RefFiles::RefFiles(const std::string &input_file,
140 const std::string &output_file)
141 : input_fp_(NULL),
142 output_fp_(NULL) {
143 if (!input_file.empty()) {
144 input_fp_ = fopen(input_file.c_str(), "rb");
145 EXPECT_TRUE(input_fp_ != NULL);
146 }
147 if (!output_file.empty()) {
148 output_fp_ = fopen(output_file.c_str(), "wb");
149 EXPECT_TRUE(output_fp_ != NULL);
150 }
151}
152
153RefFiles::~RefFiles() {
154 if (input_fp_) {
155 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
156 fclose(input_fp_);
157 }
158 if (output_fp_) fclose(output_fp_);
159}
160
161template<class T>
162void RefFiles::ProcessReference(const T& test_results) {
163 WriteToFile(test_results);
164 ReadFromFileAndCompare(test_results);
165}
166
167template<typename T, size_t n>
168void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
169 WriteToFile(test_results, length);
170 ReadFromFileAndCompare(test_results, length);
171}
172
173template<typename T, size_t n>
174void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
175 if (output_fp_) {
176 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
177 }
178}
179
180template<typename T, size_t n>
181void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
182 size_t length) {
183 if (input_fp_) {
184 // Read from ref file.
185 T* ref = new T[length];
186 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
187 // Compare
188 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
189 delete [] ref;
190 }
191}
192
minyue5f026d02015-12-16 07:36:04 -0800193void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats_raw) {
194#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
195 if (!output_fp_)
196 return;
197 neteq_unittest::NetEqNetworkStatistics stats;
198 Convert(stats_raw, &stats);
199
200 std::string stats_string;
201 ASSERT_TRUE(stats.SerializeToString(&stats_string));
202 WriteMessage(output_fp_, stats_string);
203#else
204 FAIL() << "Writing to reference file requires Proto Buffer.";
205#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206}
207
208void RefFiles::ReadFromFileAndCompare(
209 const NetEqNetworkStatistics& stats) {
minyue5f026d02015-12-16 07:36:04 -0800210#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
211 if (!input_fp_)
212 return;
213
214 std::string stats_string;
215 ReadMessage(input_fp_, &stats_string);
216 neteq_unittest::NetEqNetworkStatistics ref_stats;
217 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
218
219 // Compare
220 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms());
221 ASSERT_EQ(stats.preferred_buffer_size_ms,
222 ref_stats.preferred_buffer_size_ms());
223 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found());
224 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate());
225 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate());
226 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate());
227 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate());
228 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate());
229 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm());
230 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples());
minyue93c08b72015-12-22 09:57:41 -0800231 ASSERT_EQ(stats.secondary_decoded_rate, ref_stats.secondary_decoded_rate());
minyue5f026d02015-12-16 07:36:04 -0800232 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate());
233#else
234 FAIL() << "Reading from reference file requires Proto Buffer.";
235#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236}
237
minyue5f026d02015-12-16 07:36:04 -0800238void RefFiles::WriteToFile(const RtcpStatistics& stats_raw) {
239#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
240 if (!output_fp_)
241 return;
242 neteq_unittest::RtcpStatistics stats;
243 Convert(stats_raw, &stats);
244
245 std::string stats_string;
246 ASSERT_TRUE(stats.SerializeToString(&stats_string));
247 WriteMessage(output_fp_, stats_string);
248#else
249 FAIL() << "Writing to reference file requires Proto Buffer.";
250#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251}
252
minyue5f026d02015-12-16 07:36:04 -0800253void RefFiles::ReadFromFileAndCompare(const RtcpStatistics& stats) {
254#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
255 if (!input_fp_)
256 return;
257 std::string stats_string;
258 ReadMessage(input_fp_, &stats_string);
259 neteq_unittest::RtcpStatistics ref_stats;
260 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
261
262 // Compare
263 ASSERT_EQ(stats.fraction_lost, ref_stats.fraction_lost());
264 ASSERT_EQ(stats.cumulative_lost, ref_stats.cumulative_lost());
265 ASSERT_EQ(stats.extended_max_sequence_number,
266 ref_stats.extended_max_sequence_number());
267 ASSERT_EQ(stats.jitter, ref_stats.jitter());
268#else
269 FAIL() << "Reading from reference file requires Proto Buffer.";
270#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271}
272
273class NetEqDecodingTest : public ::testing::Test {
274 protected:
275 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
276 // constants below can be changed.
277 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700278 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
279 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
280 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800281 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
282 static const size_t kMaxBlockSize = kBlockSize48kHz;
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
minyue93c08b72015-12-22 09:57:41 -0800384#ifdef WEBRTC_CODEC_OPUS
385 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus,
386 "opus", 111));
387#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800389 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
390 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800392 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
393 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800395 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
396 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800398 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
399 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800401 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
402 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403}
404
405void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000406 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000407}
408
Peter Kastingdce40cf2015-08-24 14:52:23 -0700409void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000411 while (packet_ && sim_clock_ >= packet_->time_ms()) {
412 if (packet_->payload_length_bytes() > 0) {
413 WebRtcRTPHeader rtp_header;
414 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800415#ifndef WEBRTC_CODEC_ISAC
416 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
417 if (rtp_header.header.payloadType != 104)
418#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000419 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800420 rtp_header,
421 rtc::ArrayView<const uint8_t>(
422 packet_->payload(), packet_->payload_length_bytes()),
423 static_cast<uint32_t>(packet_->time_ms() *
424 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425 }
426 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000427 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000428 }
429
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000430 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000431 NetEqOutputType type;
Peter Kasting69558702016-01-12 16:26:35 -0800432 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000433 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
434 &num_channels, &type));
435 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
436 (*out_len == kBlockSize16kHz) ||
minyue93c08b72015-12-22 09:57:41 -0800437 (*out_len == kBlockSize32kHz) ||
438 (*out_len == kBlockSize48kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700439 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundind89814b2015-11-23 06:49:25 -0800440 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441
442 // Increase time.
443 sim_clock_ += kTimeStepMs;
444}
445
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000446void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
447 const std::string& ref_file,
448 const std::string& stat_ref_file,
449 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000450 OpenInputFile(rtp_file);
451
452 std::string ref_out_file = "";
453 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000454 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000455 }
456 RefFiles ref_files(ref_file, ref_out_file);
457
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000458 std::string stat_out_file = "";
459 if (stat_ref_file.empty()) {
460 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
461 }
462 RefFiles network_stat_files(stat_ref_file, stat_out_file);
463
464 std::string rtcp_out_file = "";
465 if (rtcp_ref_file.empty()) {
466 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
467 }
468 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
469
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000470 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000471 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000472 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000473 std::ostringstream ss;
474 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
475 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700476 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000477 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000478 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000479
480 // Query the network statistics API once per second
481 if (sim_clock_ % 1000 == 0) {
482 // Process NetworkStatistics.
483 NetEqNetworkStatistics network_stats;
484 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000485 ASSERT_NO_FATAL_FAILURE(
486 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700487 // Compare with CurrentDelay, which should be identical.
488 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000489
490 // Process RTCPstat.
491 RtcpStatistics rtcp_stats;
492 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000493 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000494 }
495 }
496}
497
498void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
499 int timestamp,
500 WebRtcRTPHeader* rtp_info) {
501 rtp_info->header.sequenceNumber = frame_index;
502 rtp_info->header.timestamp = timestamp;
503 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
504 rtp_info->header.payloadType = 94; // PCM16b WB codec.
505 rtp_info->header.markerBit = 0;
506}
507
508void NetEqDecodingTest::PopulateCng(int frame_index,
509 int timestamp,
510 WebRtcRTPHeader* rtp_info,
511 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000512 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000513 rtp_info->header.sequenceNumber = frame_index;
514 rtp_info->header.timestamp = timestamp;
515 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
516 rtp_info->header.payloadType = 98; // WB CNG.
517 rtp_info->header.markerBit = 0;
518 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
519 *payload_len = 1; // Only noise level, no spectral parameters.
520}
521
ivoc72c08ed2016-01-20 07:26:24 -0800522#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
523 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
524 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
525 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800526#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700527#else
minyue5f026d02015-12-16 07:36:04 -0800528#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700529#endif
minyue5f026d02015-12-16 07:36:04 -0800530TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800531 const std::string input_rtp_file =
532 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000533 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
534 // are identical. The latter could have been removed, but if clients still
535 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000536 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000537 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000538#if defined(_MSC_VER) && (_MSC_VER >= 1700)
539 // For Visual Studio 2012 and later, we will have to use the generic reference
540 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000541 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000542 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000543#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000544 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000545 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000546#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000547 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000548 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000549
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000550 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000551 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000552 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000553 DecodeAndCompare(input_rtp_file,
554 input_ref_file,
555 network_stat_ref_file,
556 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000557 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558}
559
minyue93c08b72015-12-22 09:57:41 -0800560#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
561 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
562 defined(WEBRTC_CODEC_OPUS)
563#define MAYBE_TestOpusBitExactness TestOpusBitExactness
564#else
565#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
566#endif
567TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
568 const std::string input_rtp_file =
569 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
570 const std::string input_ref_file =
571 webrtc::test::ResourcePath("audio_coding/neteq4_opus_ref", "pcm");
572 const std::string network_stat_ref_file =
573 webrtc::test::ResourcePath("audio_coding/neteq4_opus_network_stats",
574 "dat");
575 const std::string rtcp_stat_ref_file =
576 webrtc::test::ResourcePath("audio_coding/neteq4_opus_rtcp_stats", "dat");
577
578 if (FLAGS_gen_ref) {
579 DecodeAndCompare(input_rtp_file, "", "", "");
580 } else {
581 DecodeAndCompare(input_rtp_file,
582 input_ref_file,
583 network_stat_ref_file,
584 rtcp_stat_ref_file);
585 }
586}
587
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000588// Use fax mode to avoid time-scaling. This is to simplify the testing of
589// packet waiting times in the packet buffer.
590class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
591 protected:
592 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
593 config_.playout_mode = kPlayoutFax;
594 }
595};
596
597TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
599 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000600 const size_t kSamples = 10 * 16;
601 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800603 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000604 WebRtcRTPHeader rtp_info;
605 rtp_info.header.sequenceNumber = i;
606 rtp_info.header.timestamp = i * kSamples;
607 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
608 rtp_info.header.payloadType = 94; // PCM16b WB codec.
609 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800610 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000611 }
612 // Pull out all data.
613 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700614 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -0800615 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 NetEqOutputType type;
617 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
618 &num_channels, &type));
619 ASSERT_EQ(kBlockSize16kHz, out_len);
620 }
621
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200622 NetEqNetworkStatistics stats;
623 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000624 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
625 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200626 // each packet. Thus, we are calculating the statistics for a series from 10
627 // to 300, in steps of 10 ms.
628 EXPECT_EQ(155, stats.mean_waiting_time_ms);
629 EXPECT_EQ(155, stats.median_waiting_time_ms);
630 EXPECT_EQ(10, stats.min_waiting_time_ms);
631 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632
633 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200634 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
635 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
636 EXPECT_EQ(-1, stats.median_waiting_time_ms);
637 EXPECT_EQ(-1, stats.min_waiting_time_ms);
638 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639}
640
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000641TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000642 const int kNumFrames = 3000; // Needed for convergence.
643 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000644 const size_t kSamples = 10 * 16;
645 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646 while (frame_index < kNumFrames) {
647 // Insert one packet each time, except every 10th time where we insert two
648 // packets at once. This will create a negative clock-drift of approx. 10%.
649 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
650 for (int n = 0; n < num_packets; ++n) {
651 uint8_t payload[kPayloadBytes] = {0};
652 WebRtcRTPHeader rtp_info;
653 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800654 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655 ++frame_index;
656 }
657
658 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700659 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -0800660 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661 NetEqOutputType type;
662 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
663 &num_channels, &type));
664 ASSERT_EQ(kBlockSize16kHz, out_len);
665 }
666
667 NetEqNetworkStatistics network_stats;
668 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
669 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
670}
671
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000672TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673 const int kNumFrames = 5000; // Needed for convergence.
674 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000675 const size_t kSamples = 10 * 16;
676 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677 for (int i = 0; i < kNumFrames; ++i) {
678 // Insert one packet each time, except every 10th time where we don't insert
679 // any packet. This will create a positive clock-drift of approx. 11%.
680 int num_packets = (i % 10 == 9 ? 0 : 1);
681 for (int n = 0; n < num_packets; ++n) {
682 uint8_t payload[kPayloadBytes] = {0};
683 WebRtcRTPHeader rtp_info;
684 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800685 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000686 ++frame_index;
687 }
688
689 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700690 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -0800691 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692 NetEqOutputType type;
693 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
694 &num_channels, &type));
695 ASSERT_EQ(kBlockSize16kHz, out_len);
696 }
697
698 NetEqNetworkStatistics network_stats;
699 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
700 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
701}
702
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000703void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
704 double network_freeze_ms,
705 bool pull_audio_during_freeze,
706 int delay_tolerance_ms,
707 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000708 uint16_t seq_no = 0;
709 uint32_t timestamp = 0;
710 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000711 const size_t kSamples = kFrameSizeMs * 16;
712 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000713 double next_input_time_ms = 0.0;
714 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700715 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -0800716 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000717 NetEqOutputType type;
718
719 // Insert speech for 5 seconds.
720 const int kSpeechDurationMs = 5000;
721 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
722 // Each turn in this for loop is 10 ms.
723 while (next_input_time_ms <= t_ms) {
724 // Insert one 30 ms speech frame.
725 uint8_t payload[kPayloadBytes] = {0};
726 WebRtcRTPHeader rtp_info;
727 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800728 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 ++seq_no;
730 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000731 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000732 }
733 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
735 &num_channels, &type));
736 ASSERT_EQ(kBlockSize16kHz, out_len);
737 }
738
739 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000740 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741
742 // Insert CNG for 1 minute (= 60000 ms).
743 const int kCngPeriodMs = 100;
744 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
745 const int kCngDurationMs = 60000;
746 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
747 // Each turn in this for loop is 10 ms.
748 while (next_input_time_ms <= t_ms) {
749 // Insert one CNG frame each 100 ms.
750 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000751 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000752 WebRtcRTPHeader rtp_info;
753 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800754 ASSERT_EQ(0, neteq_->InsertPacket(
755 rtp_info,
756 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000757 ++seq_no;
758 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000759 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000760 }
761 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000762 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
763 &num_channels, &type));
764 ASSERT_EQ(kBlockSize16kHz, out_len);
765 }
766
767 EXPECT_EQ(kOutputCNG, type);
768
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000769 if (network_freeze_ms > 0) {
770 // First keep pulling audio for |network_freeze_ms| without inserting
771 // any data, then insert CNG data corresponding to |network_freeze_ms|
772 // without pulling any output audio.
773 const double loop_end_time = t_ms + network_freeze_ms;
774 for (; t_ms < loop_end_time; t_ms += 10) {
775 // Pull out data once.
776 ASSERT_EQ(0,
777 neteq_->GetAudio(
778 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
779 ASSERT_EQ(kBlockSize16kHz, out_len);
780 EXPECT_EQ(kOutputCNG, type);
781 }
782 bool pull_once = pull_audio_during_freeze;
783 // If |pull_once| is true, GetAudio will be called once half-way through
784 // the network recovery period.
785 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
786 while (next_input_time_ms <= t_ms) {
787 if (pull_once && next_input_time_ms >= pull_time_ms) {
788 pull_once = false;
789 // Pull out data once.
790 ASSERT_EQ(
791 0,
792 neteq_->GetAudio(
793 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
794 ASSERT_EQ(kBlockSize16kHz, out_len);
795 EXPECT_EQ(kOutputCNG, type);
796 t_ms += 10;
797 }
798 // Insert one CNG frame each 100 ms.
799 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000800 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 WebRtcRTPHeader rtp_info;
802 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800803 ASSERT_EQ(0, neteq_->InsertPacket(
804 rtp_info,
805 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000806 ++seq_no;
807 timestamp += kCngPeriodSamples;
808 next_input_time_ms += kCngPeriodMs * drift_factor;
809 }
810 }
811
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000812 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000813 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000814 while (type != kOutputNormal) {
815 // Each turn in this for loop is 10 ms.
816 while (next_input_time_ms <= t_ms) {
817 // Insert one 30 ms speech frame.
818 uint8_t payload[kPayloadBytes] = {0};
819 WebRtcRTPHeader rtp_info;
820 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800821 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000822 ++seq_no;
823 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000824 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000825 }
826 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000827 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
828 &num_channels, &type));
829 ASSERT_EQ(kBlockSize16kHz, out_len);
830 // Increase clock.
831 t_ms += 10;
832 }
833
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000834 // Check that the speech starts again within reasonable time.
835 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
836 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000837 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000839 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
840 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000841}
842
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000843TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000844 // Apply a clock drift of -25 ms / s (sender faster than receiver).
845 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000846 const double kNetworkFreezeTimeMs = 0.0;
847 const bool kGetAudioDuringFreezeRecovery = false;
848 const int kDelayToleranceMs = 20;
849 const int kMaxTimeToSpeechMs = 100;
850 LongCngWithClockDrift(kDriftFactor,
851 kNetworkFreezeTimeMs,
852 kGetAudioDuringFreezeRecovery,
853 kDelayToleranceMs,
854 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000855}
856
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000857TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000858 // Apply a clock drift of +25 ms / s (sender slower than receiver).
859 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000860 const double kNetworkFreezeTimeMs = 0.0;
861 const bool kGetAudioDuringFreezeRecovery = false;
862 const int kDelayToleranceMs = 20;
863 const int kMaxTimeToSpeechMs = 100;
864 LongCngWithClockDrift(kDriftFactor,
865 kNetworkFreezeTimeMs,
866 kGetAudioDuringFreezeRecovery,
867 kDelayToleranceMs,
868 kMaxTimeToSpeechMs);
869}
870
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000871TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000872 // Apply a clock drift of -25 ms / s (sender faster than receiver).
873 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
874 const double kNetworkFreezeTimeMs = 5000.0;
875 const bool kGetAudioDuringFreezeRecovery = false;
876 const int kDelayToleranceMs = 50;
877 const int kMaxTimeToSpeechMs = 200;
878 LongCngWithClockDrift(kDriftFactor,
879 kNetworkFreezeTimeMs,
880 kGetAudioDuringFreezeRecovery,
881 kDelayToleranceMs,
882 kMaxTimeToSpeechMs);
883}
884
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000885TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000886 // Apply a clock drift of +25 ms / s (sender slower than receiver).
887 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
888 const double kNetworkFreezeTimeMs = 5000.0;
889 const bool kGetAudioDuringFreezeRecovery = false;
890 const int kDelayToleranceMs = 20;
891 const int kMaxTimeToSpeechMs = 100;
892 LongCngWithClockDrift(kDriftFactor,
893 kNetworkFreezeTimeMs,
894 kGetAudioDuringFreezeRecovery,
895 kDelayToleranceMs,
896 kMaxTimeToSpeechMs);
897}
898
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000899TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000900 // Apply a clock drift of +25 ms / s (sender slower than receiver).
901 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
902 const double kNetworkFreezeTimeMs = 5000.0;
903 const bool kGetAudioDuringFreezeRecovery = true;
904 const int kDelayToleranceMs = 20;
905 const int kMaxTimeToSpeechMs = 100;
906 LongCngWithClockDrift(kDriftFactor,
907 kNetworkFreezeTimeMs,
908 kGetAudioDuringFreezeRecovery,
909 kDelayToleranceMs,
910 kMaxTimeToSpeechMs);
911}
912
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000913TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000914 const double kDriftFactor = 1.0; // No drift.
915 const double kNetworkFreezeTimeMs = 0.0;
916 const bool kGetAudioDuringFreezeRecovery = false;
917 const int kDelayToleranceMs = 10;
918 const int kMaxTimeToSpeechMs = 50;
919 LongCngWithClockDrift(kDriftFactor,
920 kNetworkFreezeTimeMs,
921 kGetAudioDuringFreezeRecovery,
922 kDelayToleranceMs,
923 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000924}
925
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000926TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000927 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000928 uint8_t payload[kPayloadBytes] = {0};
929 WebRtcRTPHeader rtp_info;
930 PopulateRtpInfo(0, 0, &rtp_info);
931 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800932 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000933 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
934}
935
Peter Boströme2976c82016-01-04 22:44:05 +0100936#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800937#define MAYBE_DecoderError DecoderError
938#else
939#define MAYBE_DecoderError DISABLED_DecoderError
940#endif
941
Peter Boströme2976c82016-01-04 22:44:05 +0100942TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000943 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000944 uint8_t payload[kPayloadBytes] = {0};
945 WebRtcRTPHeader rtp_info;
946 PopulateRtpInfo(0, 0, &rtp_info);
947 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800948 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000949 NetEqOutputType type;
950 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
951 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000952 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000953 out_data_[i] = 1;
954 }
Peter Kasting69558702016-01-12 16:26:35 -0800955 size_t num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700956 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000957 EXPECT_EQ(NetEq::kFail,
958 neteq_->GetAudio(kMaxBlockSize, out_data_,
959 &samples_per_channel, &num_channels, &type));
960 // Verify that there is a decoder error to check.
961 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800962
963 enum NetEqDecoderError {
964 ISAC_LENGTH_MISMATCH = 6730,
965 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
966 };
967#if defined(WEBRTC_CODEC_ISAC)
968 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
969#elif defined(WEBRTC_CODEC_ISACFX)
970 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
971#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000972 // Verify that the first 160 samples are set to 0, and that the remaining
973 // samples are left unmodified.
974 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
975 for (int i = 0; i < kExpectedOutputLength; ++i) {
976 std::ostringstream ss;
977 ss << "i = " << i;
978 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
979 EXPECT_EQ(0, out_data_[i]);
980 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000981 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000982 std::ostringstream ss;
983 ss << "i = " << i;
984 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
985 EXPECT_EQ(1, out_data_[i]);
986 }
987}
988
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000989TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000990 NetEqOutputType type;
991 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
992 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000993 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000994 out_data_[i] = 1;
995 }
Peter Kasting69558702016-01-12 16:26:35 -0800996 size_t num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700997 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000998 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
999 &samples_per_channel,
1000 &num_channels, &type));
1001 // Verify that the first block of samples is set to 0.
1002 static const int kExpectedOutputLength =
1003 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
1004 for (int i = 0; i < kExpectedOutputLength; ++i) {
1005 std::ostringstream ss;
1006 ss << "i = " << i;
1007 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
1008 EXPECT_EQ(0, out_data_[i]);
1009 }
henrik.lundind89814b2015-11-23 06:49:25 -08001010 // Verify that the sample rate did not change from the initial configuration.
1011 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001012}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001013
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001014class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001015 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001016 virtual void TestCondition(double sum_squared_noise,
1017 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001018
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001019 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07001020 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001021 uint8_t payload_type = 0xFF; // Invalid.
1022 if (sampling_rate_hz == 8000) {
1023 expected_samples_per_channel = kBlockSize8kHz;
1024 payload_type = 93; // PCM 16, 8 kHz.
1025 } else if (sampling_rate_hz == 16000) {
1026 expected_samples_per_channel = kBlockSize16kHz;
1027 payload_type = 94; // PCM 16, 16 kHZ.
1028 } else if (sampling_rate_hz == 32000) {
1029 expected_samples_per_channel = kBlockSize32kHz;
1030 payload_type = 95; // PCM 16, 32 kHz.
1031 } else {
1032 ASSERT_TRUE(false); // Unsupported test case.
1033 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001034
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001035 NetEqOutputType type;
1036 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001037 test::AudioLoop input;
1038 // We are using the same 32 kHz input file for all tests, regardless of
1039 // |sampling_rate_hz|. The output may sound weird, but the test is still
1040 // valid.
1041 ASSERT_TRUE(input.Init(
1042 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
1043 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001044 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001045
1046 // Payload of 10 ms of PCM16 32 kHz.
1047 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001048 WebRtcRTPHeader rtp_info;
1049 PopulateRtpInfo(0, 0, &rtp_info);
1050 rtp_info.header.payloadType = payload_type;
1051
Peter Kasting69558702016-01-12 16:26:35 -08001052 size_t number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001053 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001054
1055 uint32_t receive_timestamp = 0;
1056 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -08001057 auto block = input.GetNextBlock();
1058 ASSERT_EQ(expected_samples_per_channel, block.size());
1059 size_t enc_len_bytes =
1060 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001061 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
1062
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001063 number_channels = 0;
1064 samples_per_channel = 0;
kwibergee2bac22015-11-11 10:34:00 -08001065 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
1066 payload, enc_len_bytes),
1067 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001068 ASSERT_EQ(0,
1069 neteq_->GetAudio(kBlockSize32kHz,
1070 output,
1071 &samples_per_channel,
1072 &number_channels,
1073 &type));
Peter Kasting69558702016-01-12 16:26:35 -08001074 ASSERT_EQ(1u, number_channels);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001075 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1076 ASSERT_EQ(kOutputNormal, type);
1077
1078 // Next packet.
1079 rtp_info.header.timestamp += expected_samples_per_channel;
1080 rtp_info.header.sequenceNumber++;
1081 receive_timestamp += expected_samples_per_channel;
1082 }
1083
1084 number_channels = 0;
1085 samples_per_channel = 0;
1086
1087 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1088 // one frame without checking speech-type. This is the first frame pulled
1089 // without inserting any packet, and might not be labeled as PLC.
1090 ASSERT_EQ(0,
1091 neteq_->GetAudio(kBlockSize32kHz,
1092 output,
1093 &samples_per_channel,
1094 &number_channels,
1095 &type));
Peter Kasting69558702016-01-12 16:26:35 -08001096 ASSERT_EQ(1u, number_channels);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001097 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1098
1099 // To be able to test the fading of background noise we need at lease to
1100 // pull 611 frames.
1101 const int kFadingThreshold = 611;
1102
1103 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1104 // is arbitrary, but sufficiently large to test enough number of frames.
1105 const int kNumPlcToCngTestFrames = 20;
1106 bool plc_to_cng = false;
1107 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
1108 number_channels = 0;
1109 samples_per_channel = 0;
1110 memset(output, 1, sizeof(output)); // Set to non-zero.
1111 ASSERT_EQ(0,
1112 neteq_->GetAudio(kBlockSize32kHz,
1113 output,
1114 &samples_per_channel,
1115 &number_channels,
1116 &type));
Peter Kasting69558702016-01-12 16:26:35 -08001117 ASSERT_EQ(1u, number_channels);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001118 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1119 if (type == kOutputPLCtoCNG) {
1120 plc_to_cng = true;
1121 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001122 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001123 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001124 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001125 } else {
1126 EXPECT_EQ(kOutputPLC, type);
1127 }
1128 }
1129 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1130 }
1131};
1132
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001133class NetEqBgnTestOn : public NetEqBgnTest {
1134 protected:
1135 NetEqBgnTestOn() : NetEqBgnTest() {
1136 config_.background_noise_mode = NetEq::kBgnOn;
1137 }
1138
1139 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1140 EXPECT_NE(0, sum_squared_noise);
1141 }
1142};
1143
1144class NetEqBgnTestOff : public NetEqBgnTest {
1145 protected:
1146 NetEqBgnTestOff() : NetEqBgnTest() {
1147 config_.background_noise_mode = NetEq::kBgnOff;
1148 }
1149
1150 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1151 EXPECT_EQ(0, sum_squared_noise);
1152 }
1153};
1154
1155class NetEqBgnTestFade : public NetEqBgnTest {
1156 protected:
1157 NetEqBgnTestFade() : NetEqBgnTest() {
1158 config_.background_noise_mode = NetEq::kBgnFade;
1159 }
1160
1161 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1162 if (should_be_faded)
1163 EXPECT_EQ(0, sum_squared_noise);
1164 }
1165};
1166
henrika1d34fe92015-06-16 10:04:20 +02001167TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001168 CheckBgn(8000);
1169 CheckBgn(16000);
1170 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001171}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001172
henrika1d34fe92015-06-16 10:04:20 +02001173TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001174 CheckBgn(8000);
1175 CheckBgn(16000);
1176 CheckBgn(32000);
1177}
1178
henrika1d34fe92015-06-16 10:04:20 +02001179TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001180 CheckBgn(8000);
1181 CheckBgn(16000);
1182 CheckBgn(32000);
1183}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001184
Peter Boströme2976c82016-01-04 22:44:05 +01001185#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001186#define MAYBE_SyncPacketInsert SyncPacketInsert
1187#else
1188#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1189#endif
1190TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001191 WebRtcRTPHeader rtp_info;
1192 uint32_t receive_timestamp = 0;
1193 // For the readability use the following payloads instead of the defaults of
1194 // this test.
1195 uint8_t kPcm16WbPayloadType = 1;
1196 uint8_t kCngNbPayloadType = 2;
1197 uint8_t kCngWbPayloadType = 3;
1198 uint8_t kCngSwb32PayloadType = 4;
1199 uint8_t kCngSwb48PayloadType = 5;
1200 uint8_t kAvtPayloadType = 6;
1201 uint8_t kRedPayloadType = 7;
1202 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1203
1204 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001205 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001206 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001207 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001208 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001209 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001210 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001211 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001212 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001213 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001214 "cng-swb48", kCngSwb48PayloadType));
1215 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001216 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001217 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001218 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001219 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001220 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001221
1222 PopulateRtpInfo(0, 0, &rtp_info);
1223 rtp_info.header.payloadType = kPcm16WbPayloadType;
1224
1225 // The first packet injected cannot be sync-packet.
1226 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1227
1228 // Payload length of 10 ms PCM16 16 kHz.
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] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001231 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232
1233 // Next packet. Last packet contained 10 ms audio.
1234 rtp_info.header.sequenceNumber++;
1235 rtp_info.header.timestamp += kBlockSize16kHz;
1236 receive_timestamp += kBlockSize16kHz;
1237
1238 // Unacceptable payload types CNG, AVT (DTMF), RED.
1239 rtp_info.header.payloadType = kCngNbPayloadType;
1240 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1241
1242 rtp_info.header.payloadType = kCngWbPayloadType;
1243 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1244
1245 rtp_info.header.payloadType = kCngSwb32PayloadType;
1246 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1247
1248 rtp_info.header.payloadType = kCngSwb48PayloadType;
1249 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1250
1251 rtp_info.header.payloadType = kAvtPayloadType;
1252 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1253
1254 rtp_info.header.payloadType = kRedPayloadType;
1255 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1256
1257 // Change of codec cannot be initiated with a sync packet.
1258 rtp_info.header.payloadType = kIsacPayloadType;
1259 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1260
1261 // Change of SSRC is not allowed with a sync packet.
1262 rtp_info.header.payloadType = kPcm16WbPayloadType;
1263 ++rtp_info.header.ssrc;
1264 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1265
1266 --rtp_info.header.ssrc;
1267 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1268}
1269
1270// First insert several noise like packets, then sync-packets. Decoding all
1271// packets should not produce error, statistics should not show any packet loss
1272// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001273// TODO(turajs) we will have a better test if we have a referece NetEq, and
1274// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1275// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001276TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001277 WebRtcRTPHeader rtp_info;
1278 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001279 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001280 uint8_t payload[kPayloadBytes];
1281 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001282 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001283 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001284 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1285 }
1286 // Insert some packets which decode to noise. We are not interested in
1287 // actual decoded values.
1288 NetEqOutputType output_type;
Peter Kasting69558702016-01-12 16:26:35 -08001289 size_t num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001290 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001291 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001292 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001293 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001294 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1295 &samples_per_channel, &num_channels,
1296 &output_type));
1297 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
Peter Kasting69558702016-01-12 16:26:35 -08001298 ASSERT_EQ(1u, num_channels);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001299
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001300 rtp_info.header.sequenceNumber++;
1301 rtp_info.header.timestamp += kBlockSize16kHz;
1302 receive_timestamp += kBlockSize16kHz;
1303 }
1304 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001305
1306 // Make sure sufficient number of sync packets are inserted that we can
1307 // conduct a test.
1308 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001309 // Insert sync-packets, the decoded sequence should be all-zero.
1310 for (int n = 0; n < kNumSyncPackets; ++n) {
1311 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1312 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1313 &samples_per_channel, &num_channels,
1314 &output_type));
1315 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
Peter Kasting69558702016-01-12 16:26:35 -08001316 ASSERT_EQ(1u, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001317 if (n > algorithmic_frame_delay) {
1318 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1319 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001320 rtp_info.header.sequenceNumber++;
1321 rtp_info.header.timestamp += kBlockSize16kHz;
1322 receive_timestamp += kBlockSize16kHz;
1323 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001324
1325 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001326 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001327 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001328 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001329 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1330 &samples_per_channel, &num_channels,
1331 &output_type));
1332 if (n >= algorithmic_frame_delay + 1) {
1333 // Expect that this frame contain samples from regular RTP.
1334 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1335 }
1336 rtp_info.header.sequenceNumber++;
1337 rtp_info.header.timestamp += kBlockSize16kHz;
1338 receive_timestamp += kBlockSize16kHz;
1339 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001340 NetEqNetworkStatistics network_stats;
1341 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1342 // Expecting a "clean" network.
1343 EXPECT_EQ(0, network_stats.packet_loss_rate);
1344 EXPECT_EQ(0, network_stats.expand_rate);
1345 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001346 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001347}
1348
1349// Test if the size of the packet buffer reported correctly when containing
1350// sync packets. Also, test if network packets override sync packets. That is to
1351// prefer decoding a network packet to a sync packet, if both have same sequence
1352// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001353TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001354 WebRtcRTPHeader rtp_info;
1355 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001356 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001357 uint8_t payload[kPayloadBytes];
1358 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001359 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001360 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1361 }
1362 // Insert some packets which decode to noise. We are not interested in
1363 // actual decoded values.
1364 NetEqOutputType output_type;
Peter Kasting69558702016-01-12 16:26:35 -08001365 size_t num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001366 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001367 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001368 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1369 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001370 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001371 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1372 &samples_per_channel, &num_channels,
1373 &output_type));
1374 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
Peter Kasting69558702016-01-12 16:26:35 -08001375 ASSERT_EQ(1u, num_channels);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001376 rtp_info.header.sequenceNumber++;
1377 rtp_info.header.timestamp += kBlockSize16kHz;
1378 receive_timestamp += kBlockSize16kHz;
1379 }
1380 const int kNumSyncPackets = 10;
1381
1382 WebRtcRTPHeader first_sync_packet_rtp_info;
1383 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1384
1385 // Insert sync-packets, but no decoding.
1386 for (int n = 0; n < kNumSyncPackets; ++n) {
1387 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1388 rtp_info.header.sequenceNumber++;
1389 rtp_info.header.timestamp += kBlockSize16kHz;
1390 receive_timestamp += kBlockSize16kHz;
1391 }
1392 NetEqNetworkStatistics network_stats;
1393 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001394 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1395 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001396
1397 // Rewind |rtp_info| to that of the first sync packet.
1398 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1399
1400 // Insert.
1401 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001402 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001403 rtp_info.header.sequenceNumber++;
1404 rtp_info.header.timestamp += kBlockSize16kHz;
1405 receive_timestamp += kBlockSize16kHz;
1406 }
1407
1408 // Decode.
1409 for (int n = 0; n < kNumSyncPackets; ++n) {
1410 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1411 &samples_per_channel, &num_channels,
1412 &output_type));
1413 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
Peter Kasting69558702016-01-12 16:26:35 -08001414 ASSERT_EQ(1u, num_channels);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001415 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1416 }
1417}
1418
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001419void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1420 uint32_t start_timestamp,
1421 const std::set<uint16_t>& drop_seq_numbers,
1422 bool expect_seq_no_wrap,
1423 bool expect_timestamp_wrap) {
1424 uint16_t seq_no = start_seq_no;
1425 uint32_t timestamp = start_timestamp;
1426 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1427 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1428 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001429 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001430 double next_input_time_ms = 0.0;
1431 int16_t decoded[kBlockSize16kHz];
Peter Kasting69558702016-01-12 16:26:35 -08001432 size_t num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001433 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001434 NetEqOutputType output_type;
1435 uint32_t receive_timestamp = 0;
1436
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001437 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001438 const int kSpeechDurationMs = 2000;
1439 int packets_inserted = 0;
1440 uint16_t last_seq_no;
1441 uint32_t last_timestamp;
1442 bool timestamp_wrapped = false;
1443 bool seq_no_wrapped = false;
1444 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1445 // Each turn in this for loop is 10 ms.
1446 while (next_input_time_ms <= t_ms) {
1447 // Insert one 30 ms speech frame.
1448 uint8_t payload[kPayloadBytes] = {0};
1449 WebRtcRTPHeader rtp_info;
1450 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1451 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1452 // This sequence number was not in the set to drop. Insert it.
1453 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001454 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001455 ++packets_inserted;
1456 }
1457 NetEqNetworkStatistics network_stats;
1458 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1459
1460 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1461 // packet size for first few packets. Therefore we refrain from checking
1462 // the criteria.
1463 if (packets_inserted > 4) {
1464 // Expect preferred and actual buffer size to be no more than 2 frames.
1465 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001466 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1467 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001468 }
1469 last_seq_no = seq_no;
1470 last_timestamp = timestamp;
1471
1472 ++seq_no;
1473 timestamp += kSamples;
1474 receive_timestamp += kSamples;
1475 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1476
1477 seq_no_wrapped |= seq_no < last_seq_no;
1478 timestamp_wrapped |= timestamp < last_timestamp;
1479 }
1480 // Pull out data once.
1481 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1482 &samples_per_channel, &num_channels,
1483 &output_type));
1484 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
Peter Kasting69558702016-01-12 16:26:35 -08001485 ASSERT_EQ(1u, num_channels);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001486
1487 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001488 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001489 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001490 }
1491 // Make sure we have actually tested wrap-around.
1492 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1493 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1494}
1495
1496TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1497 // Start with a sequence number that will soon wrap.
1498 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1499 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1500}
1501
1502TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1503 // Start with a sequence number that will soon wrap.
1504 std::set<uint16_t> drop_seq_numbers;
1505 drop_seq_numbers.insert(0xFFFF);
1506 drop_seq_numbers.insert(0x0);
1507 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1508}
1509
1510TEST_F(NetEqDecodingTest, TimestampWrap) {
1511 // Start with a timestamp that will soon wrap.
1512 std::set<uint16_t> drop_seq_numbers;
1513 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1514}
1515
1516TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1517 // Start with a timestamp and a sequence number that will wrap at the same
1518 // time.
1519 std::set<uint16_t> drop_seq_numbers;
1520 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1521}
1522
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001523void NetEqDecodingTest::DuplicateCng() {
1524 uint16_t seq_no = 0;
1525 uint32_t timestamp = 0;
1526 const int kFrameSizeMs = 10;
1527 const int kSampleRateKhz = 16;
1528 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001529 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001530
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001531 const int algorithmic_delay_samples = std::max(
1532 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001533 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001534 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001535 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -08001536 size_t num_channels;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001537 NetEqOutputType type;
1538 uint8_t payload[kPayloadBytes] = {0};
1539 WebRtcRTPHeader rtp_info;
1540 for (int i = 0; i < 3; ++i) {
1541 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001542 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001543 ++seq_no;
1544 timestamp += kSamples;
1545
1546 // Pull audio once.
1547 ASSERT_EQ(0,
1548 neteq_->GetAudio(
1549 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1550 ASSERT_EQ(kBlockSize16kHz, out_len);
1551 }
1552 // Verify speech output.
1553 EXPECT_EQ(kOutputNormal, type);
1554
1555 // Insert same CNG packet twice.
1556 const int kCngPeriodMs = 100;
1557 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001558 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001559 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1560 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001561 ASSERT_EQ(
1562 0, neteq_->InsertPacket(
1563 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001564
1565 // Pull audio once and make sure CNG is played.
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);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001571 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001572
1573 // Insert the same CNG packet again. Note that at this point it is old, since
1574 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001575 ASSERT_EQ(
1576 0, neteq_->InsertPacket(
1577 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001578
1579 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1580 // we have already pulled out CNG once.
1581 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
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(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001587 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001588 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001589 }
1590
1591 // Insert speech again.
1592 ++seq_no;
1593 timestamp += kCngPeriodSamples;
1594 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001595 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001596
1597 // Pull audio once and verify that the output is speech again.
1598 ASSERT_EQ(0,
1599 neteq_->GetAudio(
1600 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1601 ASSERT_EQ(kBlockSize16kHz, out_len);
1602 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001603 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001604 PlayoutTimestamp());
1605}
1606
1607uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1608 uint32_t playout_timestamp = 0;
1609 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1610 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001611}
1612
1613TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001614
1615TEST_F(NetEqDecodingTest, CngFirst) {
1616 uint16_t seq_no = 0;
1617 uint32_t timestamp = 0;
1618 const int kFrameSizeMs = 10;
1619 const int kSampleRateKhz = 16;
1620 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1621 const int kPayloadBytes = kSamples * 2;
1622 const int kCngPeriodMs = 100;
1623 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1624 size_t payload_len;
1625
1626 uint8_t payload[kPayloadBytes] = {0};
1627 WebRtcRTPHeader rtp_info;
1628
1629 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001630 ASSERT_EQ(
1631 NetEq::kOK,
1632 neteq_->InsertPacket(
1633 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001634 ++seq_no;
1635 timestamp += kCngPeriodSamples;
1636
1637 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001638 size_t out_len;
Peter Kasting69558702016-01-12 16:26:35 -08001639 size_t num_channels;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001640 NetEqOutputType type;
1641 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1642 &num_channels, &type));
1643 ASSERT_EQ(kBlockSize16kHz, out_len);
1644 EXPECT_EQ(kOutputCNG, type);
1645
1646 // Insert some speech packets.
1647 for (int i = 0; i < 3; ++i) {
1648 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001649 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001650 ++seq_no;
1651 timestamp += kSamples;
1652
1653 // Pull audio once.
1654 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1655 &num_channels, &type));
1656 ASSERT_EQ(kBlockSize16kHz, out_len);
1657 }
1658 // Verify speech output.
1659 EXPECT_EQ(kOutputNormal, type);
1660}
1661
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001662} // namespace webrtc