blob: 1fd53455d301cba79eba21774479aec6f3058433 [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.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/interface/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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027#include "gtest/gtest.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000028#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000029#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000031#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/typedefs.h"
33
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000034DEFINE_bool(gen_ref, false, "Generate reference files.");
35
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036namespace webrtc {
37
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000038static bool IsAllZero(const int16_t* buf, int buf_length) {
39 bool all_zero = true;
40 for (int n = 0; n < buf_length && all_zero; ++n)
41 all_zero = buf[n] == 0;
42 return all_zero;
43}
44
45static bool IsAllNonZero(const int16_t* buf, int buf_length) {
46 bool all_non_zero = true;
47 for (int n = 0; n < buf_length && all_non_zero; ++n)
48 all_non_zero = buf[n] != 0;
49 return all_non_zero;
50}
51
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052class RefFiles {
53 public:
54 RefFiles(const std::string& input_file, const std::string& output_file);
55 ~RefFiles();
56 template<class T> void ProcessReference(const T& test_results);
57 template<typename T, size_t n> void ProcessReference(
58 const T (&test_results)[n],
59 size_t length);
60 template<typename T, size_t n> void WriteToFile(
61 const T (&test_results)[n],
62 size_t length);
63 template<typename T, size_t n> void ReadFromFileAndCompare(
64 const T (&test_results)[n],
65 size_t length);
66 void WriteToFile(const NetEqNetworkStatistics& stats);
67 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
68 void WriteToFile(const RtcpStatistics& stats);
69 void ReadFromFileAndCompare(const RtcpStatistics& stats);
70
71 FILE* input_fp_;
72 FILE* output_fp_;
73};
74
75RefFiles::RefFiles(const std::string &input_file,
76 const std::string &output_file)
77 : input_fp_(NULL),
78 output_fp_(NULL) {
79 if (!input_file.empty()) {
80 input_fp_ = fopen(input_file.c_str(), "rb");
81 EXPECT_TRUE(input_fp_ != NULL);
82 }
83 if (!output_file.empty()) {
84 output_fp_ = fopen(output_file.c_str(), "wb");
85 EXPECT_TRUE(output_fp_ != NULL);
86 }
87}
88
89RefFiles::~RefFiles() {
90 if (input_fp_) {
91 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
92 fclose(input_fp_);
93 }
94 if (output_fp_) fclose(output_fp_);
95}
96
97template<class T>
98void RefFiles::ProcessReference(const T& test_results) {
99 WriteToFile(test_results);
100 ReadFromFileAndCompare(test_results);
101}
102
103template<typename T, size_t n>
104void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
105 WriteToFile(test_results, length);
106 ReadFromFileAndCompare(test_results, length);
107}
108
109template<typename T, size_t n>
110void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
111 if (output_fp_) {
112 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
113 }
114}
115
116template<typename T, size_t n>
117void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
118 size_t length) {
119 if (input_fp_) {
120 // Read from ref file.
121 T* ref = new T[length];
122 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
123 // Compare
124 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
125 delete [] ref;
126 }
127}
128
129void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
130 if (output_fp_) {
131 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
132 output_fp_));
133 }
134}
135
136void RefFiles::ReadFromFileAndCompare(
137 const NetEqNetworkStatistics& stats) {
138 if (input_fp_) {
139 // Read from ref file.
140 size_t stat_size = sizeof(NetEqNetworkStatistics);
141 NetEqNetworkStatistics ref_stats;
142 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
143 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000144 ASSERT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145 }
146}
147
148void RefFiles::WriteToFile(const RtcpStatistics& stats) {
149 if (output_fp_) {
150 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
151 output_fp_));
152 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
153 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000154 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
155 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 output_fp_));
157 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
158 output_fp_));
159 }
160}
161
162void RefFiles::ReadFromFileAndCompare(
163 const RtcpStatistics& stats) {
164 if (input_fp_) {
165 // Read from ref file.
166 RtcpStatistics ref_stats;
167 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
168 sizeof(ref_stats.fraction_lost), 1, input_fp_));
169 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
170 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000171 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
172 sizeof(ref_stats.extended_max_sequence_number), 1,
173 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
175 input_fp_));
176 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000177 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
178 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
179 ASSERT_EQ(ref_stats.extended_max_sequence_number,
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000180 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000181 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 }
183}
184
185class NetEqDecodingTest : public ::testing::Test {
186 protected:
187 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
188 // constants below can be changed.
189 static const int kTimeStepMs = 10;
190 static const int kBlockSize8kHz = kTimeStepMs * 8;
191 static const int kBlockSize16kHz = kTimeStepMs * 16;
192 static const int kBlockSize32kHz = kTimeStepMs * 32;
193 static const int kMaxBlockSize = kBlockSize32kHz;
194 static const int kInitSampleRateHz = 8000;
195
196 NetEqDecodingTest();
197 virtual void SetUp();
198 virtual void TearDown();
199 void SelectDecoders(NetEqDecoder* used_codec);
200 void LoadDecoders();
201 void OpenInputFile(const std::string &rtp_file);
202 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000203 void DecodeAndCompare(const std::string& rtp_file,
204 const std::string& ref_file,
205 const std::string& stat_ref_file,
206 const std::string& rtcp_ref_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207 static void PopulateRtpInfo(int frame_index,
208 int timestamp,
209 WebRtcRTPHeader* rtp_info);
210 static void PopulateCng(int frame_index,
211 int timestamp,
212 WebRtcRTPHeader* rtp_info,
213 uint8_t* payload,
214 int* payload_len);
215
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000216 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
217 const std::set<uint16_t>& drop_seq_numbers,
218 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
219
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000220 void LongCngWithClockDrift(double drift_factor,
221 double network_freeze_ms,
222 bool pull_audio_during_freeze,
223 int delay_tolerance_ms,
224 int max_time_to_speech_ms);
225
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000226 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000227
wu@webrtc.org94454b72014-06-05 20:34:08 +0000228 uint32_t PlayoutTimestamp();
229
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000230 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000231 NetEq::Config config_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232 FILE* rtp_fp_;
233 unsigned int sim_clock_;
234 int16_t out_data_[kMaxBlockSize];
235 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000236 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237};
238
239// Allocating the static const so that it can be passed by reference.
240const int NetEqDecodingTest::kTimeStepMs;
241const int NetEqDecodingTest::kBlockSize8kHz;
242const int NetEqDecodingTest::kBlockSize16kHz;
243const int NetEqDecodingTest::kBlockSize32kHz;
244const int NetEqDecodingTest::kMaxBlockSize;
245const int NetEqDecodingTest::kInitSampleRateHz;
246
247NetEqDecodingTest::NetEqDecodingTest()
248 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000249 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250 rtp_fp_(NULL),
251 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000252 output_sample_rate_(kInitSampleRateHz),
253 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000254 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255 memset(out_data_, 0, sizeof(out_data_));
256}
257
258void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000259 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000260 NetEqNetworkStatistics stat;
261 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
262 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 ASSERT_TRUE(neteq_);
264 LoadDecoders();
265}
266
267void NetEqDecodingTest::TearDown() {
268 delete neteq_;
269 if (rtp_fp_)
270 fclose(rtp_fp_);
271}
272
273void NetEqDecodingTest::LoadDecoders() {
274 // Load PCMu.
275 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
276 // Load PCMa.
277 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000278#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 // Load iLBC.
280 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000281#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 // Load iSAC.
283 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000284#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 // Load iSAC SWB.
286 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000287 // Load iSAC FB.
288 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000289#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 // Load PCM16B nb.
291 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
292 // Load PCM16B wb.
293 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
294 // Load PCM16B swb32.
295 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
296 // Load CNG 8 kHz.
297 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
298 // Load CNG 16 kHz.
299 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
300}
301
302void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
303 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
304 ASSERT_TRUE(rtp_fp_ != NULL);
305 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
306}
307
308void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
309 // Check if time to receive.
310 while ((sim_clock_ >= rtp->time()) &&
311 (rtp->dataLen() >= 0)) {
312 if (rtp->dataLen() > 0) {
313 WebRtcRTPHeader rtpInfo;
314 rtp->parseHeader(&rtpInfo);
315 ASSERT_EQ(0, neteq_->InsertPacket(
316 rtpInfo,
317 rtp->payload(),
318 rtp->payloadLen(),
319 rtp->time() * (output_sample_rate_ / 1000)));
320 }
321 // Get next packet.
322 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
323 }
324
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000325 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 NetEqOutputType type;
327 int num_channels;
328 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
329 &num_channels, &type));
330 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
331 (*out_len == kBlockSize16kHz) ||
332 (*out_len == kBlockSize32kHz));
333 output_sample_rate_ = *out_len / 10 * 1000;
334
335 // Increase time.
336 sim_clock_ += kTimeStepMs;
337}
338
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000339void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
340 const std::string& ref_file,
341 const std::string& stat_ref_file,
342 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 OpenInputFile(rtp_file);
344
345 std::string ref_out_file = "";
346 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000347 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 }
349 RefFiles ref_files(ref_file, ref_out_file);
350
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000351 std::string stat_out_file = "";
352 if (stat_ref_file.empty()) {
353 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
354 }
355 RefFiles network_stat_files(stat_ref_file, stat_out_file);
356
357 std::string rtcp_out_file = "";
358 if (rtcp_ref_file.empty()) {
359 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
360 }
361 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
362
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 NETEQTEST_RTPpacket rtp;
364 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
365 int i = 0;
366 while (rtp.dataLen() >= 0) {
367 std::ostringstream ss;
368 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
369 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000370 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
372 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373
374 // Query the network statistics API once per second
375 if (sim_clock_ % 1000 == 0) {
376 // Process NetworkStatistics.
377 NetEqNetworkStatistics network_stats;
378 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000379 ASSERT_NO_FATAL_FAILURE(
380 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381
382 // Process RTCPstat.
383 RtcpStatistics rtcp_stats;
384 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000385 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 }
387 }
388}
389
390void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
391 int timestamp,
392 WebRtcRTPHeader* rtp_info) {
393 rtp_info->header.sequenceNumber = frame_index;
394 rtp_info->header.timestamp = timestamp;
395 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
396 rtp_info->header.payloadType = 94; // PCM16b WB codec.
397 rtp_info->header.markerBit = 0;
398}
399
400void NetEqDecodingTest::PopulateCng(int frame_index,
401 int timestamp,
402 WebRtcRTPHeader* rtp_info,
403 uint8_t* payload,
404 int* payload_len) {
405 rtp_info->header.sequenceNumber = frame_index;
406 rtp_info->header.timestamp = timestamp;
407 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
408 rtp_info->header.payloadType = 98; // WB CNG.
409 rtp_info->header.markerBit = 0;
410 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
411 *payload_len = 1; // Only noise level, no spectral parameters.
412}
413
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000414TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000415 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000416 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000417 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
418 // are identical. The latter could have been removed, but if clients still
419 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000420 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000421 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000422#if defined(_MSC_VER) && (_MSC_VER >= 1700)
423 // For Visual Studio 2012 and later, we will have to use the generic reference
424 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000425 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000426 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000427#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000428 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000429 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000430#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000431 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000432 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000433
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000434 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000435 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000436 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000437 DecodeAndCompare(input_rtp_file,
438 input_ref_file,
439 network_stat_ref_file,
440 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000441 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442}
443
444// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000445TEST_F(NetEqDecodingTest, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 // Use fax mode to avoid time-scaling. This is to simplify the testing of
447 // packet waiting times in the packet buffer.
448 neteq_->SetPlayoutMode(kPlayoutFax);
449 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
450 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
451 size_t num_frames = 30;
452 const int kSamples = 10 * 16;
453 const int kPayloadBytes = kSamples * 2;
454 for (size_t i = 0; i < num_frames; ++i) {
455 uint16_t payload[kSamples] = {0};
456 WebRtcRTPHeader rtp_info;
457 rtp_info.header.sequenceNumber = i;
458 rtp_info.header.timestamp = i * kSamples;
459 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
460 rtp_info.header.payloadType = 94; // PCM16b WB codec.
461 rtp_info.header.markerBit = 0;
462 ASSERT_EQ(0, neteq_->InsertPacket(
463 rtp_info,
464 reinterpret_cast<uint8_t*>(payload),
465 kPayloadBytes, 0));
466 }
467 // Pull out all data.
468 for (size_t i = 0; i < num_frames; ++i) {
469 int out_len;
470 int num_channels;
471 NetEqOutputType type;
472 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
473 &num_channels, &type));
474 ASSERT_EQ(kBlockSize16kHz, out_len);
475 }
476
477 std::vector<int> waiting_times;
478 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000479 EXPECT_EQ(num_frames, waiting_times.size());
480 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
481 // spacing (per definition), we expect the delay to increase with 10 ms for
482 // each packet.
483 for (size_t i = 0; i < waiting_times.size(); ++i) {
484 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
485 }
486
487 // Check statistics again and make sure it's been reset.
488 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000489 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000490 EXPECT_EQ(0, len);
491
492 // Process > 100 frames, and make sure that that we get statistics
493 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
494 num_frames = 110;
495 for (size_t i = 0; i < num_frames; ++i) {
496 uint16_t payload[kSamples] = {0};
497 WebRtcRTPHeader rtp_info;
498 rtp_info.header.sequenceNumber = i;
499 rtp_info.header.timestamp = i * kSamples;
500 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
501 rtp_info.header.payloadType = 94; // PCM16b WB codec.
502 rtp_info.header.markerBit = 0;
503 ASSERT_EQ(0, neteq_->InsertPacket(
504 rtp_info,
505 reinterpret_cast<uint8_t*>(payload),
506 kPayloadBytes, 0));
507 int out_len;
508 int num_channels;
509 NetEqOutputType type;
510 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
511 &num_channels, &type));
512 ASSERT_EQ(kBlockSize16kHz, out_len);
513 }
514
515 neteq_->WaitingTimes(&waiting_times);
516 EXPECT_EQ(100u, waiting_times.size());
517}
518
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000519TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000520 const int kNumFrames = 3000; // Needed for convergence.
521 int frame_index = 0;
522 const int kSamples = 10 * 16;
523 const int kPayloadBytes = kSamples * 2;
524 while (frame_index < kNumFrames) {
525 // Insert one packet each time, except every 10th time where we insert two
526 // packets at once. This will create a negative clock-drift of approx. 10%.
527 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
528 for (int n = 0; n < num_packets; ++n) {
529 uint8_t payload[kPayloadBytes] = {0};
530 WebRtcRTPHeader rtp_info;
531 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
532 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
533 ++frame_index;
534 }
535
536 // Pull out data once.
537 int out_len;
538 int num_channels;
539 NetEqOutputType type;
540 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
541 &num_channels, &type));
542 ASSERT_EQ(kBlockSize16kHz, out_len);
543 }
544
545 NetEqNetworkStatistics network_stats;
546 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
547 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
548}
549
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000550TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000551 const int kNumFrames = 5000; // Needed for convergence.
552 int frame_index = 0;
553 const int kSamples = 10 * 16;
554 const int kPayloadBytes = kSamples * 2;
555 for (int i = 0; i < kNumFrames; ++i) {
556 // Insert one packet each time, except every 10th time where we don't insert
557 // any packet. This will create a positive clock-drift of approx. 11%.
558 int num_packets = (i % 10 == 9 ? 0 : 1);
559 for (int n = 0; n < num_packets; ++n) {
560 uint8_t payload[kPayloadBytes] = {0};
561 WebRtcRTPHeader rtp_info;
562 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
563 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
564 ++frame_index;
565 }
566
567 // Pull out data once.
568 int out_len;
569 int num_channels;
570 NetEqOutputType type;
571 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
572 &num_channels, &type));
573 ASSERT_EQ(kBlockSize16kHz, out_len);
574 }
575
576 NetEqNetworkStatistics network_stats;
577 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
578 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
579}
580
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000581void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
582 double network_freeze_ms,
583 bool pull_audio_during_freeze,
584 int delay_tolerance_ms,
585 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000586 uint16_t seq_no = 0;
587 uint32_t timestamp = 0;
588 const int kFrameSizeMs = 30;
589 const int kSamples = kFrameSizeMs * 16;
590 const int kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 double next_input_time_ms = 0.0;
592 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000593 int out_len;
594 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 NetEqOutputType type;
596
597 // Insert speech for 5 seconds.
598 const int kSpeechDurationMs = 5000;
599 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
600 // Each turn in this for loop is 10 ms.
601 while (next_input_time_ms <= t_ms) {
602 // Insert one 30 ms speech frame.
603 uint8_t payload[kPayloadBytes] = {0};
604 WebRtcRTPHeader rtp_info;
605 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
606 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
607 ++seq_no;
608 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000609 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 }
611 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
613 &num_channels, &type));
614 ASSERT_EQ(kBlockSize16kHz, out_len);
615 }
616
617 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000618 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619
620 // Insert CNG for 1 minute (= 60000 ms).
621 const int kCngPeriodMs = 100;
622 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
623 const int kCngDurationMs = 60000;
624 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
625 // Each turn in this for loop is 10 ms.
626 while (next_input_time_ms <= t_ms) {
627 // Insert one CNG frame each 100 ms.
628 uint8_t payload[kPayloadBytes];
629 int payload_len;
630 WebRtcRTPHeader rtp_info;
631 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
632 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
633 ++seq_no;
634 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000635 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 }
637 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
639 &num_channels, &type));
640 ASSERT_EQ(kBlockSize16kHz, out_len);
641 }
642
643 EXPECT_EQ(kOutputCNG, type);
644
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000645 if (network_freeze_ms > 0) {
646 // First keep pulling audio for |network_freeze_ms| without inserting
647 // any data, then insert CNG data corresponding to |network_freeze_ms|
648 // without pulling any output audio.
649 const double loop_end_time = t_ms + network_freeze_ms;
650 for (; t_ms < loop_end_time; t_ms += 10) {
651 // Pull out data once.
652 ASSERT_EQ(0,
653 neteq_->GetAudio(
654 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
655 ASSERT_EQ(kBlockSize16kHz, out_len);
656 EXPECT_EQ(kOutputCNG, type);
657 }
658 bool pull_once = pull_audio_during_freeze;
659 // If |pull_once| is true, GetAudio will be called once half-way through
660 // the network recovery period.
661 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
662 while (next_input_time_ms <= t_ms) {
663 if (pull_once && next_input_time_ms >= pull_time_ms) {
664 pull_once = false;
665 // Pull out data once.
666 ASSERT_EQ(
667 0,
668 neteq_->GetAudio(
669 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
670 ASSERT_EQ(kBlockSize16kHz, out_len);
671 EXPECT_EQ(kOutputCNG, type);
672 t_ms += 10;
673 }
674 // Insert one CNG frame each 100 ms.
675 uint8_t payload[kPayloadBytes];
676 int payload_len;
677 WebRtcRTPHeader rtp_info;
678 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
679 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
680 ++seq_no;
681 timestamp += kCngPeriodSamples;
682 next_input_time_ms += kCngPeriodMs * drift_factor;
683 }
684 }
685
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000686 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000687 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000688 while (type != kOutputNormal) {
689 // Each turn in this for loop is 10 ms.
690 while (next_input_time_ms <= t_ms) {
691 // Insert one 30 ms speech frame.
692 uint8_t payload[kPayloadBytes] = {0};
693 WebRtcRTPHeader rtp_info;
694 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
695 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
696 ++seq_no;
697 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000698 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000699 }
700 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000701 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
702 &num_channels, &type));
703 ASSERT_EQ(kBlockSize16kHz, out_len);
704 // Increase clock.
705 t_ms += 10;
706 }
707
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 // Check that the speech starts again within reasonable time.
709 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
710 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000711 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000712 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000713 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
714 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000715}
716
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000717TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000718 // Apply a clock drift of -25 ms / s (sender faster than receiver).
719 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000720 const double kNetworkFreezeTimeMs = 0.0;
721 const bool kGetAudioDuringFreezeRecovery = false;
722 const int kDelayToleranceMs = 20;
723 const int kMaxTimeToSpeechMs = 100;
724 LongCngWithClockDrift(kDriftFactor,
725 kNetworkFreezeTimeMs,
726 kGetAudioDuringFreezeRecovery,
727 kDelayToleranceMs,
728 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000729}
730
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000731TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000732 // Apply a clock drift of +25 ms / s (sender slower than receiver).
733 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000734 const double kNetworkFreezeTimeMs = 0.0;
735 const bool kGetAudioDuringFreezeRecovery = false;
736 const int kDelayToleranceMs = 20;
737 const int kMaxTimeToSpeechMs = 100;
738 LongCngWithClockDrift(kDriftFactor,
739 kNetworkFreezeTimeMs,
740 kGetAudioDuringFreezeRecovery,
741 kDelayToleranceMs,
742 kMaxTimeToSpeechMs);
743}
744
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000745TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000746 // Apply a clock drift of -25 ms / s (sender faster than receiver).
747 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
748 const double kNetworkFreezeTimeMs = 5000.0;
749 const bool kGetAudioDuringFreezeRecovery = false;
750 const int kDelayToleranceMs = 50;
751 const int kMaxTimeToSpeechMs = 200;
752 LongCngWithClockDrift(kDriftFactor,
753 kNetworkFreezeTimeMs,
754 kGetAudioDuringFreezeRecovery,
755 kDelayToleranceMs,
756 kMaxTimeToSpeechMs);
757}
758
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000759TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000760 // Apply a clock drift of +25 ms / s (sender slower than receiver).
761 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
762 const double kNetworkFreezeTimeMs = 5000.0;
763 const bool kGetAudioDuringFreezeRecovery = false;
764 const int kDelayToleranceMs = 20;
765 const int kMaxTimeToSpeechMs = 100;
766 LongCngWithClockDrift(kDriftFactor,
767 kNetworkFreezeTimeMs,
768 kGetAudioDuringFreezeRecovery,
769 kDelayToleranceMs,
770 kMaxTimeToSpeechMs);
771}
772
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000773TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000774 // Apply a clock drift of +25 ms / s (sender slower than receiver).
775 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
776 const double kNetworkFreezeTimeMs = 5000.0;
777 const bool kGetAudioDuringFreezeRecovery = true;
778 const int kDelayToleranceMs = 20;
779 const int kMaxTimeToSpeechMs = 100;
780 LongCngWithClockDrift(kDriftFactor,
781 kNetworkFreezeTimeMs,
782 kGetAudioDuringFreezeRecovery,
783 kDelayToleranceMs,
784 kMaxTimeToSpeechMs);
785}
786
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000787TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000788 const double kDriftFactor = 1.0; // No drift.
789 const double kNetworkFreezeTimeMs = 0.0;
790 const bool kGetAudioDuringFreezeRecovery = false;
791 const int kDelayToleranceMs = 10;
792 const int kMaxTimeToSpeechMs = 50;
793 LongCngWithClockDrift(kDriftFactor,
794 kNetworkFreezeTimeMs,
795 kGetAudioDuringFreezeRecovery,
796 kDelayToleranceMs,
797 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000798}
799
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000800TEST_F(NetEqDecodingTest, UnknownPayloadType) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000801 const int kPayloadBytes = 100;
802 uint8_t payload[kPayloadBytes] = {0};
803 WebRtcRTPHeader rtp_info;
804 PopulateRtpInfo(0, 0, &rtp_info);
805 rtp_info.header.payloadType = 1; // Not registered as a decoder.
806 EXPECT_EQ(NetEq::kFail,
807 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
808 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
809}
810
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000811TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000812 const int kPayloadBytes = 100;
813 uint8_t payload[kPayloadBytes] = {0};
814 WebRtcRTPHeader rtp_info;
815 PopulateRtpInfo(0, 0, &rtp_info);
816 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
817 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
818 NetEqOutputType type;
819 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
820 // to GetAudio.
821 for (int i = 0; i < kMaxBlockSize; ++i) {
822 out_data_[i] = 1;
823 }
824 int num_channels;
825 int samples_per_channel;
826 EXPECT_EQ(NetEq::kFail,
827 neteq_->GetAudio(kMaxBlockSize, out_data_,
828 &samples_per_channel, &num_channels, &type));
829 // Verify that there is a decoder error to check.
830 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
831 // Code 6730 is an iSAC error code.
832 EXPECT_EQ(6730, neteq_->LastDecoderError());
833 // Verify that the first 160 samples are set to 0, and that the remaining
834 // samples are left unmodified.
835 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
836 for (int i = 0; i < kExpectedOutputLength; ++i) {
837 std::ostringstream ss;
838 ss << "i = " << i;
839 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
840 EXPECT_EQ(0, out_data_[i]);
841 }
842 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
843 std::ostringstream ss;
844 ss << "i = " << i;
845 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
846 EXPECT_EQ(1, out_data_[i]);
847 }
848}
849
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000850TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 NetEqOutputType type;
852 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
853 // to GetAudio.
854 for (int i = 0; i < kMaxBlockSize; ++i) {
855 out_data_[i] = 1;
856 }
857 int num_channels;
858 int samples_per_channel;
859 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
860 &samples_per_channel,
861 &num_channels, &type));
862 // Verify that the first block of samples is set to 0.
863 static const int kExpectedOutputLength =
864 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
865 for (int i = 0; i < kExpectedOutputLength; ++i) {
866 std::ostringstream ss;
867 ss << "i = " << i;
868 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
869 EXPECT_EQ(0, out_data_[i]);
870 }
871}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000872
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000873class NetEqBgnTest
874 : public NetEqDecodingTest,
875 public ::testing::WithParamInterface<NetEq::BackgroundNoiseMode> {
876 protected:
877 NetEqBgnTest() : NetEqDecodingTest() {
878 config_.background_noise_mode = GetParam();
879 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000880
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000881 void CheckBgnOff(int sampling_rate_hz) {
882 int expected_samples_per_channel = 0;
883 uint8_t payload_type = 0xFF; // Invalid.
884 if (sampling_rate_hz == 8000) {
885 expected_samples_per_channel = kBlockSize8kHz;
886 payload_type = 93; // PCM 16, 8 kHz.
887 } else if (sampling_rate_hz == 16000) {
888 expected_samples_per_channel = kBlockSize16kHz;
889 payload_type = 94; // PCM 16, 16 kHZ.
890 } else if (sampling_rate_hz == 32000) {
891 expected_samples_per_channel = kBlockSize32kHz;
892 payload_type = 95; // PCM 16, 32 kHz.
893 } else {
894 ASSERT_TRUE(false); // Unsupported test case.
895 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000896
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000897 NetEqOutputType type;
898 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
899 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
900
901 // Payload of 10 ms of PCM16 32 kHz.
902 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
903
904 // Random payload.
905 for (int n = 0; n < expected_samples_per_channel; ++n) {
906 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
907 }
908 int enc_len_bytes =
909 WebRtcPcm16b_EncodeW16(input,
910 expected_samples_per_channel,
911 reinterpret_cast<int16_t*>(payload));
912 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
913
914 WebRtcRTPHeader rtp_info;
915 PopulateRtpInfo(0, 0, &rtp_info);
916 rtp_info.header.payloadType = payload_type;
917
918 int number_channels = 0;
919 int samples_per_channel = 0;
920
921 uint32_t receive_timestamp = 0;
922 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
923 number_channels = 0;
924 samples_per_channel = 0;
925 ASSERT_EQ(0,
926 neteq_->InsertPacket(
927 rtp_info, payload, enc_len_bytes, receive_timestamp));
928 ASSERT_EQ(0,
929 neteq_->GetAudio(kBlockSize32kHz,
930 output,
931 &samples_per_channel,
932 &number_channels,
933 &type));
934 ASSERT_EQ(1, number_channels);
935 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
936 ASSERT_EQ(kOutputNormal, type);
937
938 // Next packet.
939 rtp_info.header.timestamp += expected_samples_per_channel;
940 rtp_info.header.sequenceNumber++;
941 receive_timestamp += expected_samples_per_channel;
942 }
943
944 number_channels = 0;
945 samples_per_channel = 0;
946
947 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
948 // one frame without checking speech-type. This is the first frame pulled
949 // without inserting any packet, and might not be labeled as PLC.
950 ASSERT_EQ(0,
951 neteq_->GetAudio(kBlockSize32kHz,
952 output,
953 &samples_per_channel,
954 &number_channels,
955 &type));
956 ASSERT_EQ(1, number_channels);
957 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
958
959 // To be able to test the fading of background noise we need at lease to
960 // pull 611 frames.
961 const int kFadingThreshold = 611;
962
963 // Test several CNG-to-PLC packet for the expected behavior. The number 20
964 // is arbitrary, but sufficiently large to test enough number of frames.
965 const int kNumPlcToCngTestFrames = 20;
966 bool plc_to_cng = false;
967 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
968 number_channels = 0;
969 samples_per_channel = 0;
970 memset(output, 1, sizeof(output)); // Set to non-zero.
971 ASSERT_EQ(0,
972 neteq_->GetAudio(kBlockSize32kHz,
973 output,
974 &samples_per_channel,
975 &number_channels,
976 &type));
977 ASSERT_EQ(1, number_channels);
978 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
979 if (type == kOutputPLCtoCNG) {
980 plc_to_cng = true;
981 double sum_squared = 0;
982 for (int k = 0; k < number_channels * samples_per_channel; ++k)
983 sum_squared += output[k] * output[k];
984 if (config_.background_noise_mode == NetEq::kBgnOn) {
985 EXPECT_NE(0, sum_squared);
986 } else if (config_.background_noise_mode == NetEq::kBgnOff ||
987 n > kFadingThreshold) {
988 EXPECT_EQ(0, sum_squared);
989 }
990 } else {
991 EXPECT_EQ(kOutputPLC, type);
992 }
993 }
994 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
995 }
996};
997
998TEST_P(NetEqBgnTest, BackgroundNoise) {
999 CheckBgnOff(8000);
1000 CheckBgnOff(16000);
1001 CheckBgnOff(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001002}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001003
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001004INSTANTIATE_TEST_CASE_P(BgnModes,
1005 NetEqBgnTest,
1006 ::testing::Values(NetEq::kBgnOn,
1007 NetEq::kBgnOff,
1008 NetEq::kBgnFade));
1009
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001010TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001011 WebRtcRTPHeader rtp_info;
1012 uint32_t receive_timestamp = 0;
1013 // For the readability use the following payloads instead of the defaults of
1014 // this test.
1015 uint8_t kPcm16WbPayloadType = 1;
1016 uint8_t kCngNbPayloadType = 2;
1017 uint8_t kCngWbPayloadType = 3;
1018 uint8_t kCngSwb32PayloadType = 4;
1019 uint8_t kCngSwb48PayloadType = 5;
1020 uint8_t kAvtPayloadType = 6;
1021 uint8_t kRedPayloadType = 7;
1022 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1023
1024 // Register decoders.
1025 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1026 kPcm16WbPayloadType));
1027 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1028 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1029 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1030 kCngSwb32PayloadType));
1031 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1032 kCngSwb48PayloadType));
1033 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1034 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1035 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1036
1037 PopulateRtpInfo(0, 0, &rtp_info);
1038 rtp_info.header.payloadType = kPcm16WbPayloadType;
1039
1040 // The first packet injected cannot be sync-packet.
1041 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1042
1043 // Payload length of 10 ms PCM16 16 kHz.
1044 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1045 uint8_t payload[kPayloadBytes] = {0};
1046 ASSERT_EQ(0, neteq_->InsertPacket(
1047 rtp_info, payload, kPayloadBytes, receive_timestamp));
1048
1049 // Next packet. Last packet contained 10 ms audio.
1050 rtp_info.header.sequenceNumber++;
1051 rtp_info.header.timestamp += kBlockSize16kHz;
1052 receive_timestamp += kBlockSize16kHz;
1053
1054 // Unacceptable payload types CNG, AVT (DTMF), RED.
1055 rtp_info.header.payloadType = kCngNbPayloadType;
1056 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1057
1058 rtp_info.header.payloadType = kCngWbPayloadType;
1059 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1060
1061 rtp_info.header.payloadType = kCngSwb32PayloadType;
1062 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1063
1064 rtp_info.header.payloadType = kCngSwb48PayloadType;
1065 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1066
1067 rtp_info.header.payloadType = kAvtPayloadType;
1068 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1069
1070 rtp_info.header.payloadType = kRedPayloadType;
1071 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1072
1073 // Change of codec cannot be initiated with a sync packet.
1074 rtp_info.header.payloadType = kIsacPayloadType;
1075 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1076
1077 // Change of SSRC is not allowed with a sync packet.
1078 rtp_info.header.payloadType = kPcm16WbPayloadType;
1079 ++rtp_info.header.ssrc;
1080 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081
1082 --rtp_info.header.ssrc;
1083 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1084}
1085
1086// First insert several noise like packets, then sync-packets. Decoding all
1087// packets should not produce error, statistics should not show any packet loss
1088// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001089// TODO(turajs) we will have a better test if we have a referece NetEq, and
1090// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1091// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001092TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001093 WebRtcRTPHeader rtp_info;
1094 PopulateRtpInfo(0, 0, &rtp_info);
1095 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1096 uint8_t payload[kPayloadBytes];
1097 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001098 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001099 for (int n = 0; n < kPayloadBytes; ++n) {
1100 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1101 }
1102 // Insert some packets which decode to noise. We are not interested in
1103 // actual decoded values.
1104 NetEqOutputType output_type;
1105 int num_channels;
1106 int samples_per_channel;
1107 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001108 for (int n = 0; n < 100; ++n) {
1109 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1110 receive_timestamp));
1111 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1112 &samples_per_channel, &num_channels,
1113 &output_type));
1114 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1115 ASSERT_EQ(1, num_channels);
1116
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001117 rtp_info.header.sequenceNumber++;
1118 rtp_info.header.timestamp += kBlockSize16kHz;
1119 receive_timestamp += kBlockSize16kHz;
1120 }
1121 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001122
1123 // Make sure sufficient number of sync packets are inserted that we can
1124 // conduct a test.
1125 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001126 // Insert sync-packets, the decoded sequence should be all-zero.
1127 for (int n = 0; n < kNumSyncPackets; ++n) {
1128 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1129 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1130 &samples_per_channel, &num_channels,
1131 &output_type));
1132 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1133 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001134 if (n > algorithmic_frame_delay) {
1135 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1136 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001137 rtp_info.header.sequenceNumber++;
1138 rtp_info.header.timestamp += kBlockSize16kHz;
1139 receive_timestamp += kBlockSize16kHz;
1140 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001141
1142 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001143 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001144 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1145 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1146 receive_timestamp));
1147 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1148 &samples_per_channel, &num_channels,
1149 &output_type));
1150 if (n >= algorithmic_frame_delay + 1) {
1151 // Expect that this frame contain samples from regular RTP.
1152 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1153 }
1154 rtp_info.header.sequenceNumber++;
1155 rtp_info.header.timestamp += kBlockSize16kHz;
1156 receive_timestamp += kBlockSize16kHz;
1157 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001158 NetEqNetworkStatistics network_stats;
1159 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1160 // Expecting a "clean" network.
1161 EXPECT_EQ(0, network_stats.packet_loss_rate);
1162 EXPECT_EQ(0, network_stats.expand_rate);
1163 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001164 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001165}
1166
1167// Test if the size of the packet buffer reported correctly when containing
1168// sync packets. Also, test if network packets override sync packets. That is to
1169// prefer decoding a network packet to a sync packet, if both have same sequence
1170// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001171TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001172 WebRtcRTPHeader rtp_info;
1173 PopulateRtpInfo(0, 0, &rtp_info);
1174 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1175 uint8_t payload[kPayloadBytes];
1176 int16_t decoded[kBlockSize16kHz];
1177 for (int n = 0; n < kPayloadBytes; ++n) {
1178 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1179 }
1180 // Insert some packets which decode to noise. We are not interested in
1181 // actual decoded values.
1182 NetEqOutputType output_type;
1183 int num_channels;
1184 int samples_per_channel;
1185 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001186 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1187 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001188 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1189 receive_timestamp));
1190 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1191 &samples_per_channel, &num_channels,
1192 &output_type));
1193 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1194 ASSERT_EQ(1, num_channels);
1195 rtp_info.header.sequenceNumber++;
1196 rtp_info.header.timestamp += kBlockSize16kHz;
1197 receive_timestamp += kBlockSize16kHz;
1198 }
1199 const int kNumSyncPackets = 10;
1200
1201 WebRtcRTPHeader first_sync_packet_rtp_info;
1202 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1203
1204 // Insert sync-packets, but no decoding.
1205 for (int n = 0; n < kNumSyncPackets; ++n) {
1206 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1207 rtp_info.header.sequenceNumber++;
1208 rtp_info.header.timestamp += kBlockSize16kHz;
1209 receive_timestamp += kBlockSize16kHz;
1210 }
1211 NetEqNetworkStatistics network_stats;
1212 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001213 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1214 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001215
1216 // Rewind |rtp_info| to that of the first sync packet.
1217 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1218
1219 // Insert.
1220 for (int n = 0; n < kNumSyncPackets; ++n) {
1221 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1222 receive_timestamp));
1223 rtp_info.header.sequenceNumber++;
1224 rtp_info.header.timestamp += kBlockSize16kHz;
1225 receive_timestamp += kBlockSize16kHz;
1226 }
1227
1228 // Decode.
1229 for (int n = 0; n < kNumSyncPackets; ++n) {
1230 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1231 &samples_per_channel, &num_channels,
1232 &output_type));
1233 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1234 ASSERT_EQ(1, num_channels);
1235 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1236 }
1237}
1238
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001239void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1240 uint32_t start_timestamp,
1241 const std::set<uint16_t>& drop_seq_numbers,
1242 bool expect_seq_no_wrap,
1243 bool expect_timestamp_wrap) {
1244 uint16_t seq_no = start_seq_no;
1245 uint32_t timestamp = start_timestamp;
1246 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1247 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1248 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1249 const int kPayloadBytes = kSamples * sizeof(int16_t);
1250 double next_input_time_ms = 0.0;
1251 int16_t decoded[kBlockSize16kHz];
1252 int num_channels;
1253 int samples_per_channel;
1254 NetEqOutputType output_type;
1255 uint32_t receive_timestamp = 0;
1256
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001257 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001258 const int kSpeechDurationMs = 2000;
1259 int packets_inserted = 0;
1260 uint16_t last_seq_no;
1261 uint32_t last_timestamp;
1262 bool timestamp_wrapped = false;
1263 bool seq_no_wrapped = false;
1264 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1265 // Each turn in this for loop is 10 ms.
1266 while (next_input_time_ms <= t_ms) {
1267 // Insert one 30 ms speech frame.
1268 uint8_t payload[kPayloadBytes] = {0};
1269 WebRtcRTPHeader rtp_info;
1270 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1271 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1272 // This sequence number was not in the set to drop. Insert it.
1273 ASSERT_EQ(0,
1274 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1275 receive_timestamp));
1276 ++packets_inserted;
1277 }
1278 NetEqNetworkStatistics network_stats;
1279 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1280
1281 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1282 // packet size for first few packets. Therefore we refrain from checking
1283 // the criteria.
1284 if (packets_inserted > 4) {
1285 // Expect preferred and actual buffer size to be no more than 2 frames.
1286 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001287 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1288 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001289 }
1290 last_seq_no = seq_no;
1291 last_timestamp = timestamp;
1292
1293 ++seq_no;
1294 timestamp += kSamples;
1295 receive_timestamp += kSamples;
1296 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1297
1298 seq_no_wrapped |= seq_no < last_seq_no;
1299 timestamp_wrapped |= timestamp < last_timestamp;
1300 }
1301 // Pull out data once.
1302 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1303 &samples_per_channel, &num_channels,
1304 &output_type));
1305 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1306 ASSERT_EQ(1, num_channels);
1307
1308 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001309 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001310 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001311 }
1312 // Make sure we have actually tested wrap-around.
1313 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1314 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1315}
1316
1317TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1318 // Start with a sequence number that will soon wrap.
1319 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1320 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1321}
1322
1323TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1324 // Start with a sequence number that will soon wrap.
1325 std::set<uint16_t> drop_seq_numbers;
1326 drop_seq_numbers.insert(0xFFFF);
1327 drop_seq_numbers.insert(0x0);
1328 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1329}
1330
1331TEST_F(NetEqDecodingTest, TimestampWrap) {
1332 // Start with a timestamp that will soon wrap.
1333 std::set<uint16_t> drop_seq_numbers;
1334 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1335}
1336
1337TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1338 // Start with a timestamp and a sequence number that will wrap at the same
1339 // time.
1340 std::set<uint16_t> drop_seq_numbers;
1341 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1342}
1343
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001344void NetEqDecodingTest::DuplicateCng() {
1345 uint16_t seq_no = 0;
1346 uint32_t timestamp = 0;
1347 const int kFrameSizeMs = 10;
1348 const int kSampleRateKhz = 16;
1349 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1350 const int kPayloadBytes = kSamples * 2;
1351
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001352 const int algorithmic_delay_samples = std::max(
1353 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001354 // Insert three speech packet. Three are needed to get the frame length
1355 // correct.
1356 int out_len;
1357 int num_channels;
1358 NetEqOutputType type;
1359 uint8_t payload[kPayloadBytes] = {0};
1360 WebRtcRTPHeader rtp_info;
1361 for (int i = 0; i < 3; ++i) {
1362 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1363 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1364 ++seq_no;
1365 timestamp += kSamples;
1366
1367 // Pull audio once.
1368 ASSERT_EQ(0,
1369 neteq_->GetAudio(
1370 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1371 ASSERT_EQ(kBlockSize16kHz, out_len);
1372 }
1373 // Verify speech output.
1374 EXPECT_EQ(kOutputNormal, type);
1375
1376 // Insert same CNG packet twice.
1377 const int kCngPeriodMs = 100;
1378 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1379 int payload_len;
1380 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1381 // This is the first time this CNG packet is inserted.
1382 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1383
1384 // Pull audio once and make sure CNG is played.
1385 ASSERT_EQ(0,
1386 neteq_->GetAudio(
1387 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1388 ASSERT_EQ(kBlockSize16kHz, out_len);
1389 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001390 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001391
1392 // Insert the same CNG packet again. Note that at this point it is old, since
1393 // we have already decoded the first copy of it.
1394 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1395
1396 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1397 // we have already pulled out CNG once.
1398 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1399 ASSERT_EQ(0,
1400 neteq_->GetAudio(
1401 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1402 ASSERT_EQ(kBlockSize16kHz, out_len);
1403 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001404 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001405 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001406 }
1407
1408 // Insert speech again.
1409 ++seq_no;
1410 timestamp += kCngPeriodSamples;
1411 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1412 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1413
1414 // Pull audio once and verify that the output is speech again.
1415 ASSERT_EQ(0,
1416 neteq_->GetAudio(
1417 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1418 ASSERT_EQ(kBlockSize16kHz, out_len);
1419 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001420 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001421 PlayoutTimestamp());
1422}
1423
1424uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1425 uint32_t playout_timestamp = 0;
1426 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1427 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001428}
1429
1430TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001431} // namespace webrtc