blob: 340cf581b6d80ec57516786b0b96ebaa62eb129b [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>
kwiberg2d0c3322016-02-14 09:28:33 -080022#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000023#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024#include <string>
25#include <vector>
26
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000027#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000028#include "testing/gtest/include/gtest/gtest.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.lundin6d8e0112016-03-04 10:34:21 -080032#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033#include "webrtc/test/testsupport/fileutils.h"
34#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;
kwiberg2d0c3322016-02-14 09:28:33 -0800106 std::unique_ptr<char[]> buffer(new char[size]);
minyue5f026d02015-12-16 07:36:04 -0800107 ASSERT_EQ(static_cast<size_t>(size),
108 fread(buffer.get(), sizeof(char), size, file));
109 message->assign(buffer.get(), size);
110}
111#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
112
113} // namespace
114
115namespace webrtc {
116
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117class RefFiles {
118 public:
119 RefFiles(const std::string& input_file, const std::string& output_file);
120 ~RefFiles();
121 template<class T> void ProcessReference(const T& test_results);
122 template<typename T, size_t n> void ProcessReference(
123 const T (&test_results)[n],
124 size_t length);
125 template<typename T, size_t n> void WriteToFile(
126 const T (&test_results)[n],
127 size_t length);
128 template<typename T, size_t n> void ReadFromFileAndCompare(
129 const T (&test_results)[n],
130 size_t length);
131 void WriteToFile(const NetEqNetworkStatistics& stats);
132 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
133 void WriteToFile(const RtcpStatistics& stats);
134 void ReadFromFileAndCompare(const RtcpStatistics& stats);
135
136 FILE* input_fp_;
137 FILE* output_fp_;
138};
139
140RefFiles::RefFiles(const std::string &input_file,
141 const std::string &output_file)
142 : input_fp_(NULL),
143 output_fp_(NULL) {
144 if (!input_file.empty()) {
145 input_fp_ = fopen(input_file.c_str(), "rb");
146 EXPECT_TRUE(input_fp_ != NULL);
147 }
148 if (!output_file.empty()) {
149 output_fp_ = fopen(output_file.c_str(), "wb");
150 EXPECT_TRUE(output_fp_ != NULL);
151 }
152}
153
154RefFiles::~RefFiles() {
155 if (input_fp_) {
156 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
157 fclose(input_fp_);
158 }
159 if (output_fp_) fclose(output_fp_);
160}
161
162template<class T>
163void RefFiles::ProcessReference(const T& test_results) {
164 WriteToFile(test_results);
165 ReadFromFileAndCompare(test_results);
166}
167
168template<typename T, size_t n>
169void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
170 WriteToFile(test_results, length);
171 ReadFromFileAndCompare(test_results, length);
172}
173
174template<typename T, size_t n>
175void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
176 if (output_fp_) {
177 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
178 }
179}
180
181template<typename T, size_t n>
182void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
183 size_t length) {
184 if (input_fp_) {
185 // Read from ref file.
186 T* ref = new T[length];
187 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
188 // Compare
189 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
190 delete [] ref;
191 }
192}
193
minyue5f026d02015-12-16 07:36:04 -0800194void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats_raw) {
195#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
196 if (!output_fp_)
197 return;
198 neteq_unittest::NetEqNetworkStatistics stats;
199 Convert(stats_raw, &stats);
200
201 std::string stats_string;
202 ASSERT_TRUE(stats.SerializeToString(&stats_string));
203 WriteMessage(output_fp_, stats_string);
204#else
205 FAIL() << "Writing to reference file requires Proto Buffer.";
206#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207}
208
209void RefFiles::ReadFromFileAndCompare(
210 const NetEqNetworkStatistics& stats) {
minyue5f026d02015-12-16 07:36:04 -0800211#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
212 if (!input_fp_)
213 return;
214
215 std::string stats_string;
216 ReadMessage(input_fp_, &stats_string);
217 neteq_unittest::NetEqNetworkStatistics ref_stats;
218 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
219
220 // Compare
221 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms());
222 ASSERT_EQ(stats.preferred_buffer_size_ms,
223 ref_stats.preferred_buffer_size_ms());
224 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found());
225 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate());
226 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate());
227 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate());
228 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate());
229 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate());
230 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm());
231 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples());
minyue93c08b72015-12-22 09:57:41 -0800232 ASSERT_EQ(stats.secondary_decoded_rate, ref_stats.secondary_decoded_rate());
minyue5f026d02015-12-16 07:36:04 -0800233 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate());
234#else
235 FAIL() << "Reading from reference file requires Proto Buffer.";
236#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237}
238
minyue5f026d02015-12-16 07:36:04 -0800239void RefFiles::WriteToFile(const RtcpStatistics& stats_raw) {
240#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
241 if (!output_fp_)
242 return;
243 neteq_unittest::RtcpStatistics stats;
244 Convert(stats_raw, &stats);
245
246 std::string stats_string;
247 ASSERT_TRUE(stats.SerializeToString(&stats_string));
248 WriteMessage(output_fp_, stats_string);
249#else
250 FAIL() << "Writing to reference file requires Proto Buffer.";
251#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000252}
253
minyue5f026d02015-12-16 07:36:04 -0800254void RefFiles::ReadFromFileAndCompare(const RtcpStatistics& stats) {
255#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
256 if (!input_fp_)
257 return;
258 std::string stats_string;
259 ReadMessage(input_fp_, &stats_string);
260 neteq_unittest::RtcpStatistics ref_stats;
261 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
262
263 // Compare
264 ASSERT_EQ(stats.fraction_lost, ref_stats.fraction_lost());
265 ASSERT_EQ(stats.cumulative_lost, ref_stats.cumulative_lost());
266 ASSERT_EQ(stats.extended_max_sequence_number,
267 ref_stats.extended_max_sequence_number());
268 ASSERT_EQ(stats.jitter, ref_stats.jitter());
269#else
270 FAIL() << "Reading from reference file requires Proto Buffer.";
271#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272}
273
274class NetEqDecodingTest : public ::testing::Test {
275 protected:
276 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
277 // constants below can be changed.
278 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700279 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
280 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
281 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800282 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
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);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800291 void Process();
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_;
kwiberg2d0c3322016-02-14 09:28:33 -0800323 std::unique_ptr<test::RtpFileSource> rtp_source_;
324 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800326 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327 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;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336const int NetEqDecodingTest::kInitSampleRateHz;
337
338NetEqDecodingTest::NetEqDecodingTest()
339 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000340 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000342 output_sample_rate_(kInitSampleRateHz),
343 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000344 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345}
346
347void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000348 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000349 NetEqNetworkStatistics stat;
350 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
351 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 ASSERT_TRUE(neteq_);
353 LoadDecoders();
354}
355
356void NetEqDecodingTest::TearDown() {
357 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358}
359
360void NetEqDecodingTest::LoadDecoders() {
361 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800362 ASSERT_EQ(0,
363 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800365 ASSERT_EQ(0,
366 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700367#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800369 ASSERT_EQ(
370 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700371#endif
372#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800374 ASSERT_EQ(
375 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700376#endif
377#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800379 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
380 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700381#endif
minyue93c08b72015-12-22 09:57:41 -0800382#ifdef WEBRTC_CODEC_OPUS
383 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus,
384 "opus", 111));
385#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800387 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
388 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800390 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
391 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800393 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
394 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800396 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
397 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800399 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
400 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401}
402
403void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000404 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000405}
406
henrik.lundin6d8e0112016-03-04 10:34:21 -0800407void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000408 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000409 while (packet_ && sim_clock_ >= packet_->time_ms()) {
410 if (packet_->payload_length_bytes() > 0) {
411 WebRtcRTPHeader rtp_header;
412 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800413#ifndef WEBRTC_CODEC_ISAC
414 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
415 if (rtp_header.header.payloadType != 104)
416#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000417 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800418 rtp_header,
419 rtc::ArrayView<const uint8_t>(
420 packet_->payload(), packet_->payload_length_bytes()),
421 static_cast<uint32_t>(packet_->time_ms() *
422 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423 }
424 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000425 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000426 }
427
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000428 // Get audio from NetEq.
henrik.lundin55480f52016-03-08 02:37:57 -0800429 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800430 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
431 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
432 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
433 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
434 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800435 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000436
437 // Increase time.
438 sim_clock_ += kTimeStepMs;
439}
440
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000441void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
442 const std::string& ref_file,
443 const std::string& stat_ref_file,
444 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000445 OpenInputFile(rtp_file);
446
447 std::string ref_out_file = "";
448 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000449 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000450 }
451 RefFiles ref_files(ref_file, ref_out_file);
452
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000453 std::string stat_out_file = "";
454 if (stat_ref_file.empty()) {
455 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
456 }
457 RefFiles network_stat_files(stat_ref_file, stat_out_file);
458
459 std::string rtcp_out_file = "";
460 if (rtcp_ref_file.empty()) {
461 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
462 }
463 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
464
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000465 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000466 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000467 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000468 std::ostringstream ss;
469 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
470 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800471 ASSERT_NO_FATAL_FAILURE(Process());
472 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(
473 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000474
475 // Query the network statistics API once per second
476 if (sim_clock_ % 1000 == 0) {
477 // Process NetworkStatistics.
478 NetEqNetworkStatistics network_stats;
479 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000480 ASSERT_NO_FATAL_FAILURE(
481 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700482 // Compare with CurrentDelay, which should be identical.
483 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000484
485 // Process RTCPstat.
486 RtcpStatistics rtcp_stats;
487 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000488 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000489 }
490 }
491}
492
493void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
494 int timestamp,
495 WebRtcRTPHeader* rtp_info) {
496 rtp_info->header.sequenceNumber = frame_index;
497 rtp_info->header.timestamp = timestamp;
498 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
499 rtp_info->header.payloadType = 94; // PCM16b WB codec.
500 rtp_info->header.markerBit = 0;
501}
502
503void NetEqDecodingTest::PopulateCng(int frame_index,
504 int timestamp,
505 WebRtcRTPHeader* rtp_info,
506 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000507 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000508 rtp_info->header.sequenceNumber = frame_index;
509 rtp_info->header.timestamp = timestamp;
510 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
511 rtp_info->header.payloadType = 98; // WB CNG.
512 rtp_info->header.markerBit = 0;
513 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
514 *payload_len = 1; // Only noise level, no spectral parameters.
515}
516
ivoc72c08ed2016-01-20 07:26:24 -0800517#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
518 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
519 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
520 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800521#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700522#else
minyue5f026d02015-12-16 07:36:04 -0800523#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700524#endif
minyue5f026d02015-12-16 07:36:04 -0800525TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800526 const std::string input_rtp_file =
527 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000528 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
529 // are identical. The latter could have been removed, but if clients still
530 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000531 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000532 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000533#if defined(_MSC_VER) && (_MSC_VER >= 1700)
534 // For Visual Studio 2012 and later, we will have to use the generic reference
535 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000536 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000537 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000538#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000539 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000540 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000541#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000542 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000543 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000544
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000545 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000546 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000547 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000548 DecodeAndCompare(input_rtp_file,
549 input_ref_file,
550 network_stat_ref_file,
551 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000552 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000553}
554
minyue93c08b72015-12-22 09:57:41 -0800555#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
556 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
557 defined(WEBRTC_CODEC_OPUS)
558#define MAYBE_TestOpusBitExactness TestOpusBitExactness
559#else
560#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
561#endif
562TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
563 const std::string input_rtp_file =
564 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
565 const std::string input_ref_file =
kjellanderc3a09832016-02-02 13:18:33 -0800566 // The pcm files were generated by using Opus v1.1.2 to decode the RTC
567 // file generated by Opus v1.1
minyue93c08b72015-12-22 09:57:41 -0800568 webrtc::test::ResourcePath("audio_coding/neteq4_opus_ref", "pcm");
569 const std::string network_stat_ref_file =
kjellanderc3a09832016-02-02 13:18:33 -0800570 // The network stats file was generated when using Opus v1.1.2 to decode
571 // the RTC file generated by Opus v1.1
minyue93c08b72015-12-22 09:57:41 -0800572 webrtc::test::ResourcePath("audio_coding/neteq4_opus_network_stats",
573 "dat");
574 const std::string rtcp_stat_ref_file =
575 webrtc::test::ResourcePath("audio_coding/neteq4_opus_rtcp_stats", "dat");
576
577 if (FLAGS_gen_ref) {
578 DecodeAndCompare(input_rtp_file, "", "", "");
579 } else {
580 DecodeAndCompare(input_rtp_file,
581 input_ref_file,
582 network_stat_ref_file,
583 rtcp_stat_ref_file);
584 }
585}
586
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000587// Use fax mode to avoid time-scaling. This is to simplify the testing of
588// packet waiting times in the packet buffer.
589class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
590 protected:
591 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
592 config_.playout_mode = kPlayoutFax;
593 }
594};
595
596TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000597 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
598 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000599 const size_t kSamples = 10 * 16;
600 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000601 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800602 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000603 WebRtcRTPHeader rtp_info;
604 rtp_info.header.sequenceNumber = i;
605 rtp_info.header.timestamp = i * kSamples;
606 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
607 rtp_info.header.payloadType = 94; // PCM16b WB codec.
608 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800609 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 }
611 // Pull out all data.
612 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin55480f52016-03-08 02:37:57 -0800613 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800614 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000615 }
616
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200617 NetEqNetworkStatistics stats;
618 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
620 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200621 // each packet. Thus, we are calculating the statistics for a series from 10
622 // to 300, in steps of 10 ms.
623 EXPECT_EQ(155, stats.mean_waiting_time_ms);
624 EXPECT_EQ(155, stats.median_waiting_time_ms);
625 EXPECT_EQ(10, stats.min_waiting_time_ms);
626 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000627
628 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200629 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
630 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
631 EXPECT_EQ(-1, stats.median_waiting_time_ms);
632 EXPECT_EQ(-1, stats.min_waiting_time_ms);
633 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000634}
635
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000636TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000637 const int kNumFrames = 3000; // Needed for convergence.
638 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000639 const size_t kSamples = 10 * 16;
640 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000641 while (frame_index < kNumFrames) {
642 // Insert one packet each time, except every 10th time where we insert two
643 // packets at once. This will create a negative clock-drift of approx. 10%.
644 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
645 for (int n = 0; n < num_packets; ++n) {
646 uint8_t payload[kPayloadBytes] = {0};
647 WebRtcRTPHeader rtp_info;
648 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800649 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650 ++frame_index;
651 }
652
653 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800654 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800655 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000656 }
657
658 NetEqNetworkStatistics network_stats;
659 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
660 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
661}
662
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000663TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 const int kNumFrames = 5000; // Needed for convergence.
665 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000666 const size_t kSamples = 10 * 16;
667 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 for (int i = 0; i < kNumFrames; ++i) {
669 // Insert one packet each time, except every 10th time where we don't insert
670 // any packet. This will create a positive clock-drift of approx. 11%.
671 int num_packets = (i % 10 == 9 ? 0 : 1);
672 for (int n = 0; n < num_packets; ++n) {
673 uint8_t payload[kPayloadBytes] = {0};
674 WebRtcRTPHeader rtp_info;
675 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800676 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677 ++frame_index;
678 }
679
680 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800681 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800682 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000683 }
684
685 NetEqNetworkStatistics network_stats;
686 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
687 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
688}
689
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000690void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
691 double network_freeze_ms,
692 bool pull_audio_during_freeze,
693 int delay_tolerance_ms,
694 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000695 uint16_t seq_no = 0;
696 uint32_t timestamp = 0;
697 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000698 const size_t kSamples = kFrameSizeMs * 16;
699 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700 double next_input_time_ms = 0.0;
701 double t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000702
703 // Insert speech for 5 seconds.
704 const int kSpeechDurationMs = 5000;
705 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
706 // Each turn in this for loop is 10 ms.
707 while (next_input_time_ms <= t_ms) {
708 // Insert one 30 ms speech frame.
709 uint8_t payload[kPayloadBytes] = {0};
710 WebRtcRTPHeader rtp_info;
711 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800712 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000713 ++seq_no;
714 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000715 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000716 }
717 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800718 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800719 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 }
721
henrik.lundin55480f52016-03-08 02:37:57 -0800722 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000723 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724
725 // Insert CNG for 1 minute (= 60000 ms).
726 const int kCngPeriodMs = 100;
727 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
728 const int kCngDurationMs = 60000;
729 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
730 // Each turn in this for loop is 10 ms.
731 while (next_input_time_ms <= t_ms) {
732 // Insert one CNG frame each 100 ms.
733 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000734 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000735 WebRtcRTPHeader rtp_info;
736 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800737 ASSERT_EQ(0, neteq_->InsertPacket(
738 rtp_info,
739 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000740 ++seq_no;
741 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000742 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 }
744 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800745 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800746 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747 }
748
henrik.lundin55480f52016-03-08 02:37:57 -0800749 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000750
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000751 if (network_freeze_ms > 0) {
752 // First keep pulling audio for |network_freeze_ms| without inserting
753 // any data, then insert CNG data corresponding to |network_freeze_ms|
754 // without pulling any output audio.
755 const double loop_end_time = t_ms + network_freeze_ms;
756 for (; t_ms < loop_end_time; t_ms += 10) {
757 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800758 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800759 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800760 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000761 }
762 bool pull_once = pull_audio_during_freeze;
763 // If |pull_once| is true, GetAudio will be called once half-way through
764 // the network recovery period.
765 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
766 while (next_input_time_ms <= t_ms) {
767 if (pull_once && next_input_time_ms >= pull_time_ms) {
768 pull_once = false;
769 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800770 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800771 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800772 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000773 t_ms += 10;
774 }
775 // Insert one CNG frame each 100 ms.
776 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000777 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000778 WebRtcRTPHeader rtp_info;
779 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800780 ASSERT_EQ(0, neteq_->InsertPacket(
781 rtp_info,
782 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000783 ++seq_no;
784 timestamp += kCngPeriodSamples;
785 next_input_time_ms += kCngPeriodMs * drift_factor;
786 }
787 }
788
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000789 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000790 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800791 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000792 // Each turn in this for loop is 10 ms.
793 while (next_input_time_ms <= t_ms) {
794 // Insert one 30 ms speech frame.
795 uint8_t payload[kPayloadBytes] = {0};
796 WebRtcRTPHeader rtp_info;
797 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800798 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000799 ++seq_no;
800 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000802 }
803 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800804 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800805 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000806 // Increase clock.
807 t_ms += 10;
808 }
809
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000810 // Check that the speech starts again within reasonable time.
811 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
812 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000813 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000814 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000815 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
816 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000817}
818
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000819TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000820 // Apply a clock drift of -25 ms / s (sender faster than receiver).
821 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000822 const double kNetworkFreezeTimeMs = 0.0;
823 const bool kGetAudioDuringFreezeRecovery = false;
824 const int kDelayToleranceMs = 20;
825 const int kMaxTimeToSpeechMs = 100;
826 LongCngWithClockDrift(kDriftFactor,
827 kNetworkFreezeTimeMs,
828 kGetAudioDuringFreezeRecovery,
829 kDelayToleranceMs,
830 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000831}
832
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000833TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000834 // Apply a clock drift of +25 ms / s (sender slower than receiver).
835 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000836 const double kNetworkFreezeTimeMs = 0.0;
837 const bool kGetAudioDuringFreezeRecovery = false;
838 const int kDelayToleranceMs = 20;
839 const int kMaxTimeToSpeechMs = 100;
840 LongCngWithClockDrift(kDriftFactor,
841 kNetworkFreezeTimeMs,
842 kGetAudioDuringFreezeRecovery,
843 kDelayToleranceMs,
844 kMaxTimeToSpeechMs);
845}
846
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000847TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000848 // Apply a clock drift of -25 ms / s (sender faster than receiver).
849 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
850 const double kNetworkFreezeTimeMs = 5000.0;
851 const bool kGetAudioDuringFreezeRecovery = false;
852 const int kDelayToleranceMs = 50;
853 const int kMaxTimeToSpeechMs = 200;
854 LongCngWithClockDrift(kDriftFactor,
855 kNetworkFreezeTimeMs,
856 kGetAudioDuringFreezeRecovery,
857 kDelayToleranceMs,
858 kMaxTimeToSpeechMs);
859}
860
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000861TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000862 // Apply a clock drift of +25 ms / s (sender slower than receiver).
863 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
864 const double kNetworkFreezeTimeMs = 5000.0;
865 const bool kGetAudioDuringFreezeRecovery = false;
866 const int kDelayToleranceMs = 20;
867 const int kMaxTimeToSpeechMs = 100;
868 LongCngWithClockDrift(kDriftFactor,
869 kNetworkFreezeTimeMs,
870 kGetAudioDuringFreezeRecovery,
871 kDelayToleranceMs,
872 kMaxTimeToSpeechMs);
873}
874
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000875TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000876 // Apply a clock drift of +25 ms / s (sender slower than receiver).
877 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
878 const double kNetworkFreezeTimeMs = 5000.0;
879 const bool kGetAudioDuringFreezeRecovery = true;
880 const int kDelayToleranceMs = 20;
881 const int kMaxTimeToSpeechMs = 100;
882 LongCngWithClockDrift(kDriftFactor,
883 kNetworkFreezeTimeMs,
884 kGetAudioDuringFreezeRecovery,
885 kDelayToleranceMs,
886 kMaxTimeToSpeechMs);
887}
888
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000889TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000890 const double kDriftFactor = 1.0; // No drift.
891 const double kNetworkFreezeTimeMs = 0.0;
892 const bool kGetAudioDuringFreezeRecovery = false;
893 const int kDelayToleranceMs = 10;
894 const int kMaxTimeToSpeechMs = 50;
895 LongCngWithClockDrift(kDriftFactor,
896 kNetworkFreezeTimeMs,
897 kGetAudioDuringFreezeRecovery,
898 kDelayToleranceMs,
899 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000900}
901
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000902TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000903 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000904 uint8_t payload[kPayloadBytes] = {0};
905 WebRtcRTPHeader rtp_info;
906 PopulateRtpInfo(0, 0, &rtp_info);
907 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800908 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
910}
911
Peter Boströme2976c82016-01-04 22:44:05 +0100912#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800913#define MAYBE_DecoderError DecoderError
914#else
915#define MAYBE_DecoderError DISABLED_DecoderError
916#endif
917
Peter Boströme2976c82016-01-04 22:44:05 +0100918TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000919 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920 uint8_t payload[kPayloadBytes] = {0};
921 WebRtcRTPHeader rtp_info;
922 PopulateRtpInfo(0, 0, &rtp_info);
923 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800924 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000925 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
926 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800927 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
928 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000929 }
henrik.lundin55480f52016-03-08 02:37:57 -0800930 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000931 // Verify that there is a decoder error to check.
932 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800933
934 enum NetEqDecoderError {
935 ISAC_LENGTH_MISMATCH = 6730,
936 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
937 };
938#if defined(WEBRTC_CODEC_ISAC)
939 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
940#elif defined(WEBRTC_CODEC_ISACFX)
941 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
942#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000943 // Verify that the first 160 samples are set to 0, and that the remaining
944 // samples are left unmodified.
945 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
946 for (int i = 0; i < kExpectedOutputLength; ++i) {
947 std::ostringstream ss;
948 ss << "i = " << i;
949 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800950 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000951 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800952 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
953 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000954 std::ostringstream ss;
955 ss << "i = " << i;
956 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800957 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000958 }
959}
960
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000961TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000962 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
963 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800964 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
965 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000966 }
henrik.lundin55480f52016-03-08 02:37:57 -0800967 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000968 // Verify that the first block of samples is set to 0.
969 static const int kExpectedOutputLength =
970 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
971 for (int i = 0; i < kExpectedOutputLength; ++i) {
972 std::ostringstream ss;
973 ss << "i = " << i;
974 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800975 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000976 }
henrik.lundind89814b2015-11-23 06:49:25 -0800977 // Verify that the sample rate did not change from the initial configuration.
978 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000979}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000980
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000981class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000982 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000983 virtual void TestCondition(double sum_squared_noise,
984 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000985
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000986 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700987 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000988 uint8_t payload_type = 0xFF; // Invalid.
989 if (sampling_rate_hz == 8000) {
990 expected_samples_per_channel = kBlockSize8kHz;
991 payload_type = 93; // PCM 16, 8 kHz.
992 } else if (sampling_rate_hz == 16000) {
993 expected_samples_per_channel = kBlockSize16kHz;
994 payload_type = 94; // PCM 16, 16 kHZ.
995 } else if (sampling_rate_hz == 32000) {
996 expected_samples_per_channel = kBlockSize32kHz;
997 payload_type = 95; // PCM 16, 32 kHz.
998 } else {
999 ASSERT_TRUE(false); // Unsupported test case.
1000 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001001
henrik.lundin6d8e0112016-03-04 10:34:21 -08001002 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001003 test::AudioLoop input;
1004 // We are using the same 32 kHz input file for all tests, regardless of
1005 // |sampling_rate_hz|. The output may sound weird, but the test is still
1006 // valid.
1007 ASSERT_TRUE(input.Init(
1008 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
1009 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001010 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001011
1012 // Payload of 10 ms of PCM16 32 kHz.
1013 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001014 WebRtcRTPHeader rtp_info;
1015 PopulateRtpInfo(0, 0, &rtp_info);
1016 rtp_info.header.payloadType = payload_type;
1017
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001018 uint32_t receive_timestamp = 0;
1019 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -08001020 auto block = input.GetNextBlock();
1021 ASSERT_EQ(expected_samples_per_channel, block.size());
1022 size_t enc_len_bytes =
1023 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001024 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
1025
kwibergee2bac22015-11-11 10:34:00 -08001026 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
1027 payload, enc_len_bytes),
1028 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001029 output.Reset();
henrik.lundin55480f52016-03-08 02:37:57 -08001030 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001031 ASSERT_EQ(1u, output.num_channels_);
1032 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001033 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001034
1035 // Next packet.
1036 rtp_info.header.timestamp += expected_samples_per_channel;
1037 rtp_info.header.sequenceNumber++;
1038 receive_timestamp += expected_samples_per_channel;
1039 }
1040
henrik.lundin6d8e0112016-03-04 10:34:21 -08001041 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001042
1043 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1044 // one frame without checking speech-type. This is the first frame pulled
1045 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin55480f52016-03-08 02:37:57 -08001046 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001047 ASSERT_EQ(1u, output.num_channels_);
1048 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001049
1050 // To be able to test the fading of background noise we need at lease to
1051 // pull 611 frames.
1052 const int kFadingThreshold = 611;
1053
1054 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1055 // is arbitrary, but sufficiently large to test enough number of frames.
1056 const int kNumPlcToCngTestFrames = 20;
1057 bool plc_to_cng = false;
1058 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001059 output.Reset();
1060 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin55480f52016-03-08 02:37:57 -08001061 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001062 ASSERT_EQ(1u, output.num_channels_);
1063 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001064 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001065 plc_to_cng = true;
1066 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001067 for (size_t k = 0;
1068 k < output.num_channels_ * output.samples_per_channel_; ++k)
1069 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001070 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001071 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001072 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001073 }
1074 }
1075 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1076 }
1077};
1078
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001079class NetEqBgnTestOn : public NetEqBgnTest {
1080 protected:
1081 NetEqBgnTestOn() : NetEqBgnTest() {
1082 config_.background_noise_mode = NetEq::kBgnOn;
1083 }
1084
1085 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1086 EXPECT_NE(0, sum_squared_noise);
1087 }
1088};
1089
1090class NetEqBgnTestOff : public NetEqBgnTest {
1091 protected:
1092 NetEqBgnTestOff() : NetEqBgnTest() {
1093 config_.background_noise_mode = NetEq::kBgnOff;
1094 }
1095
1096 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1097 EXPECT_EQ(0, sum_squared_noise);
1098 }
1099};
1100
1101class NetEqBgnTestFade : public NetEqBgnTest {
1102 protected:
1103 NetEqBgnTestFade() : NetEqBgnTest() {
1104 config_.background_noise_mode = NetEq::kBgnFade;
1105 }
1106
1107 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1108 if (should_be_faded)
1109 EXPECT_EQ(0, sum_squared_noise);
1110 }
1111};
1112
henrika1d34fe92015-06-16 10:04:20 +02001113TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001114 CheckBgn(8000);
1115 CheckBgn(16000);
1116 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001117}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001118
henrika1d34fe92015-06-16 10:04:20 +02001119TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001120 CheckBgn(8000);
1121 CheckBgn(16000);
1122 CheckBgn(32000);
1123}
1124
henrika1d34fe92015-06-16 10:04:20 +02001125TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001126 CheckBgn(8000);
1127 CheckBgn(16000);
1128 CheckBgn(32000);
1129}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001130
Peter Boströme2976c82016-01-04 22:44:05 +01001131#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001132#define MAYBE_SyncPacketInsert SyncPacketInsert
1133#else
1134#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1135#endif
1136TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001137 WebRtcRTPHeader rtp_info;
1138 uint32_t receive_timestamp = 0;
1139 // For the readability use the following payloads instead of the defaults of
1140 // this test.
1141 uint8_t kPcm16WbPayloadType = 1;
1142 uint8_t kCngNbPayloadType = 2;
1143 uint8_t kCngWbPayloadType = 3;
1144 uint8_t kCngSwb32PayloadType = 4;
1145 uint8_t kCngSwb48PayloadType = 5;
1146 uint8_t kAvtPayloadType = 6;
1147 uint8_t kRedPayloadType = 7;
1148 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1149
1150 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001151 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001152 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001153 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001154 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001155 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001156 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001157 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001158 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001159 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001160 "cng-swb48", kCngSwb48PayloadType));
1161 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001162 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001163 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001164 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001165 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001166 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001167
1168 PopulateRtpInfo(0, 0, &rtp_info);
1169 rtp_info.header.payloadType = kPcm16WbPayloadType;
1170
1171 // The first packet injected cannot be sync-packet.
1172 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1173
1174 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001175 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001176 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001177 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001178
1179 // Next packet. Last packet contained 10 ms audio.
1180 rtp_info.header.sequenceNumber++;
1181 rtp_info.header.timestamp += kBlockSize16kHz;
1182 receive_timestamp += kBlockSize16kHz;
1183
1184 // Unacceptable payload types CNG, AVT (DTMF), RED.
1185 rtp_info.header.payloadType = kCngNbPayloadType;
1186 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1187
1188 rtp_info.header.payloadType = kCngWbPayloadType;
1189 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1190
1191 rtp_info.header.payloadType = kCngSwb32PayloadType;
1192 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1193
1194 rtp_info.header.payloadType = kCngSwb48PayloadType;
1195 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1196
1197 rtp_info.header.payloadType = kAvtPayloadType;
1198 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1199
1200 rtp_info.header.payloadType = kRedPayloadType;
1201 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1202
1203 // Change of codec cannot be initiated with a sync packet.
1204 rtp_info.header.payloadType = kIsacPayloadType;
1205 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1206
1207 // Change of SSRC is not allowed with a sync packet.
1208 rtp_info.header.payloadType = kPcm16WbPayloadType;
1209 ++rtp_info.header.ssrc;
1210 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1211
1212 --rtp_info.header.ssrc;
1213 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1214}
1215
1216// First insert several noise like packets, then sync-packets. Decoding all
1217// packets should not produce error, statistics should not show any packet loss
1218// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001219// TODO(turajs) we will have a better test if we have a referece NetEq, and
1220// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1221// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001222TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001223 WebRtcRTPHeader rtp_info;
1224 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001225 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001226 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001227 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001228 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001229 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001230 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1231 }
1232 // Insert some packets which decode to noise. We are not interested in
1233 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001234 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001235 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001236 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001237 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001238 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1239 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001240
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001241 rtp_info.header.sequenceNumber++;
1242 rtp_info.header.timestamp += kBlockSize16kHz;
1243 receive_timestamp += kBlockSize16kHz;
1244 }
1245 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001246
1247 // Make sure sufficient number of sync packets are inserted that we can
1248 // conduct a test.
1249 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001250 // Insert sync-packets, the decoded sequence should be all-zero.
1251 for (int n = 0; n < kNumSyncPackets; ++n) {
1252 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001253 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001254 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1255 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001256 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001257 EXPECT_TRUE(IsAllZero(
1258 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001259 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001260 rtp_info.header.sequenceNumber++;
1261 rtp_info.header.timestamp += kBlockSize16kHz;
1262 receive_timestamp += kBlockSize16kHz;
1263 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001264
1265 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001266 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001267 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001268 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001269 ASSERT_EQ(0, neteq_->GetAudio(&output));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001270 if (n >= algorithmic_frame_delay + 1) {
1271 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001272 EXPECT_TRUE(IsAllNonZero(
1273 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001274 }
1275 rtp_info.header.sequenceNumber++;
1276 rtp_info.header.timestamp += kBlockSize16kHz;
1277 receive_timestamp += kBlockSize16kHz;
1278 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001279 NetEqNetworkStatistics network_stats;
1280 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1281 // Expecting a "clean" network.
1282 EXPECT_EQ(0, network_stats.packet_loss_rate);
1283 EXPECT_EQ(0, network_stats.expand_rate);
1284 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001285 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001286}
1287
1288// Test if the size of the packet buffer reported correctly when containing
1289// sync packets. Also, test if network packets override sync packets. That is to
1290// prefer decoding a network packet to a sync packet, if both have same sequence
1291// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001292TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001293 WebRtcRTPHeader rtp_info;
1294 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001295 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001296 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001297 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001298 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001299 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1300 }
1301 // Insert some packets which decode to noise. We are not interested in
1302 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001303 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001304 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1305 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001306 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001307 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001308 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1309 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001310 rtp_info.header.sequenceNumber++;
1311 rtp_info.header.timestamp += kBlockSize16kHz;
1312 receive_timestamp += kBlockSize16kHz;
1313 }
1314 const int kNumSyncPackets = 10;
1315
1316 WebRtcRTPHeader first_sync_packet_rtp_info;
1317 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1318
1319 // Insert sync-packets, but no decoding.
1320 for (int n = 0; n < kNumSyncPackets; ++n) {
1321 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1322 rtp_info.header.sequenceNumber++;
1323 rtp_info.header.timestamp += kBlockSize16kHz;
1324 receive_timestamp += kBlockSize16kHz;
1325 }
1326 NetEqNetworkStatistics network_stats;
1327 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001328 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1329 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001330
1331 // Rewind |rtp_info| to that of the first sync packet.
1332 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1333
1334 // Insert.
1335 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001336 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001337 rtp_info.header.sequenceNumber++;
1338 rtp_info.header.timestamp += kBlockSize16kHz;
1339 receive_timestamp += kBlockSize16kHz;
1340 }
1341
1342 // Decode.
1343 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin55480f52016-03-08 02:37:57 -08001344 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001345 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1346 ASSERT_EQ(1u, output.num_channels_);
1347 EXPECT_TRUE(IsAllNonZero(
1348 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001349 }
1350}
1351
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001352void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1353 uint32_t start_timestamp,
1354 const std::set<uint16_t>& drop_seq_numbers,
1355 bool expect_seq_no_wrap,
1356 bool expect_timestamp_wrap) {
1357 uint16_t seq_no = start_seq_no;
1358 uint32_t timestamp = start_timestamp;
1359 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1360 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1361 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001362 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001363 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001364 uint32_t receive_timestamp = 0;
1365
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001366 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001367 const int kSpeechDurationMs = 2000;
1368 int packets_inserted = 0;
1369 uint16_t last_seq_no;
1370 uint32_t last_timestamp;
1371 bool timestamp_wrapped = false;
1372 bool seq_no_wrapped = false;
1373 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1374 // Each turn in this for loop is 10 ms.
1375 while (next_input_time_ms <= t_ms) {
1376 // Insert one 30 ms speech frame.
1377 uint8_t payload[kPayloadBytes] = {0};
1378 WebRtcRTPHeader rtp_info;
1379 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1380 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1381 // This sequence number was not in the set to drop. Insert it.
1382 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001383 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001384 ++packets_inserted;
1385 }
1386 NetEqNetworkStatistics network_stats;
1387 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1388
1389 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1390 // packet size for first few packets. Therefore we refrain from checking
1391 // the criteria.
1392 if (packets_inserted > 4) {
1393 // Expect preferred and actual buffer size to be no more than 2 frames.
1394 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001395 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1396 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001397 }
1398 last_seq_no = seq_no;
1399 last_timestamp = timestamp;
1400
1401 ++seq_no;
1402 timestamp += kSamples;
1403 receive_timestamp += kSamples;
1404 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1405
1406 seq_no_wrapped |= seq_no < last_seq_no;
1407 timestamp_wrapped |= timestamp < last_timestamp;
1408 }
1409 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001410 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -08001411 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001412 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1413 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001414
1415 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001416 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001417 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001418 }
1419 // Make sure we have actually tested wrap-around.
1420 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1421 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1422}
1423
1424TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1425 // Start with a sequence number that will soon wrap.
1426 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1427 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1428}
1429
1430TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1431 // Start with a sequence number that will soon wrap.
1432 std::set<uint16_t> drop_seq_numbers;
1433 drop_seq_numbers.insert(0xFFFF);
1434 drop_seq_numbers.insert(0x0);
1435 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1436}
1437
1438TEST_F(NetEqDecodingTest, TimestampWrap) {
1439 // Start with a timestamp that will soon wrap.
1440 std::set<uint16_t> drop_seq_numbers;
1441 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1442}
1443
1444TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1445 // Start with a timestamp and a sequence number that will wrap at the same
1446 // time.
1447 std::set<uint16_t> drop_seq_numbers;
1448 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1449}
1450
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001451void NetEqDecodingTest::DuplicateCng() {
1452 uint16_t seq_no = 0;
1453 uint32_t timestamp = 0;
1454 const int kFrameSizeMs = 10;
1455 const int kSampleRateKhz = 16;
1456 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001457 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001458
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001459 const int algorithmic_delay_samples = std::max(
1460 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001461 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001462 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001463 uint8_t payload[kPayloadBytes] = {0};
1464 WebRtcRTPHeader rtp_info;
1465 for (int i = 0; i < 3; ++i) {
1466 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001467 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001468 ++seq_no;
1469 timestamp += kSamples;
1470
1471 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001472 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001473 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001474 }
1475 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001476 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001477
1478 // Insert same CNG packet twice.
1479 const int kCngPeriodMs = 100;
1480 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001481 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001482 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1483 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001484 ASSERT_EQ(
1485 0, neteq_->InsertPacket(
1486 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001487
1488 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001489 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001490 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001491 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001492 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001493
1494 // Insert the same CNG packet again. Note that at this point it is old, since
1495 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001496 ASSERT_EQ(
1497 0, neteq_->InsertPacket(
1498 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001499
1500 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1501 // we have already pulled out CNG once.
1502 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin55480f52016-03-08 02:37:57 -08001503 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001504 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001505 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001506 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001507 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001508 }
1509
1510 // Insert speech again.
1511 ++seq_no;
1512 timestamp += kCngPeriodSamples;
1513 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001514 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001515
1516 // Pull audio once and verify that the output is speech again.
henrik.lundin55480f52016-03-08 02:37:57 -08001517 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001518 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001519 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001520 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001521 PlayoutTimestamp());
1522}
1523
1524uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1525 uint32_t playout_timestamp = 0;
1526 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1527 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001528}
1529
1530TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001531
1532TEST_F(NetEqDecodingTest, CngFirst) {
1533 uint16_t seq_no = 0;
1534 uint32_t timestamp = 0;
1535 const int kFrameSizeMs = 10;
1536 const int kSampleRateKhz = 16;
1537 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1538 const int kPayloadBytes = kSamples * 2;
1539 const int kCngPeriodMs = 100;
1540 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1541 size_t payload_len;
1542
1543 uint8_t payload[kPayloadBytes] = {0};
1544 WebRtcRTPHeader rtp_info;
1545
1546 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001547 ASSERT_EQ(
1548 NetEq::kOK,
1549 neteq_->InsertPacket(
1550 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001551 ++seq_no;
1552 timestamp += kCngPeriodSamples;
1553
1554 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001555 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001556 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001557 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001558
1559 // Insert some speech packets.
1560 for (int i = 0; i < 3; ++i) {
1561 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001562 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001563 ++seq_no;
1564 timestamp += kSamples;
1565
1566 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001567 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001568 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001569 }
1570 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001571 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001572}
1573
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001574} // namespace webrtc