blob: 317c7b575827b912482093185c509aedba41f1e2 [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);
203 void DecodeAndCompare(const std::string &rtp_file,
204 const std::string &ref_file);
205 void DecodeAndCheckStats(const std::string &rtp_file,
206 const std::string &stat_ref_file,
207 const std::string &rtcp_ref_file);
208 static void PopulateRtpInfo(int frame_index,
209 int timestamp,
210 WebRtcRTPHeader* rtp_info);
211 static void PopulateCng(int frame_index,
212 int timestamp,
213 WebRtcRTPHeader* rtp_info,
214 uint8_t* payload,
215 int* payload_len);
216
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000217 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
218 const std::set<uint16_t>& drop_seq_numbers,
219 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
220
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000221 void LongCngWithClockDrift(double drift_factor,
222 double network_freeze_ms,
223 bool pull_audio_during_freeze,
224 int delay_tolerance_ms,
225 int max_time_to_speech_ms);
226
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000227 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000228
wu@webrtc.org94454b72014-06-05 20:34:08 +0000229 uint32_t PlayoutTimestamp();
230
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000232 NetEq::Config config_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233 FILE* rtp_fp_;
234 unsigned int sim_clock_;
235 int16_t out_data_[kMaxBlockSize];
236 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000237 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000238};
239
240// Allocating the static const so that it can be passed by reference.
241const int NetEqDecodingTest::kTimeStepMs;
242const int NetEqDecodingTest::kBlockSize8kHz;
243const int NetEqDecodingTest::kBlockSize16kHz;
244const int NetEqDecodingTest::kBlockSize32kHz;
245const int NetEqDecodingTest::kMaxBlockSize;
246const int NetEqDecodingTest::kInitSampleRateHz;
247
248NetEqDecodingTest::NetEqDecodingTest()
249 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000250 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251 rtp_fp_(NULL),
252 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000253 output_sample_rate_(kInitSampleRateHz),
254 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000255 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 memset(out_data_, 0, sizeof(out_data_));
257}
258
259void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000260 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000261 NetEqNetworkStatistics stat;
262 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
263 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264 ASSERT_TRUE(neteq_);
265 LoadDecoders();
266}
267
268void NetEqDecodingTest::TearDown() {
269 delete neteq_;
270 if (rtp_fp_)
271 fclose(rtp_fp_);
272}
273
274void NetEqDecodingTest::LoadDecoders() {
275 // Load PCMu.
276 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
277 // Load PCMa.
278 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000279#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 // Load iLBC.
281 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000282#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 // Load iSAC.
284 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000285#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286 // Load iSAC SWB.
287 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000288 // Load iSAC FB.
289 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000290#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 // Load PCM16B nb.
292 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
293 // Load PCM16B wb.
294 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
295 // Load PCM16B swb32.
296 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
297 // Load CNG 8 kHz.
298 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
299 // Load CNG 16 kHz.
300 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
301}
302
303void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
304 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
305 ASSERT_TRUE(rtp_fp_ != NULL);
306 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
307}
308
309void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
310 // Check if time to receive.
311 while ((sim_clock_ >= rtp->time()) &&
312 (rtp->dataLen() >= 0)) {
313 if (rtp->dataLen() > 0) {
314 WebRtcRTPHeader rtpInfo;
315 rtp->parseHeader(&rtpInfo);
316 ASSERT_EQ(0, neteq_->InsertPacket(
317 rtpInfo,
318 rtp->payload(),
319 rtp->payloadLen(),
320 rtp->time() * (output_sample_rate_ / 1000)));
321 }
322 // Get next packet.
323 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
324 }
325
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000326 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327 NetEqOutputType type;
328 int num_channels;
329 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
330 &num_channels, &type));
331 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
332 (*out_len == kBlockSize16kHz) ||
333 (*out_len == kBlockSize32kHz));
334 output_sample_rate_ = *out_len / 10 * 1000;
335
336 // Increase time.
337 sim_clock_ += kTimeStepMs;
338}
339
340void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
341 const std::string &ref_file) {
342 OpenInputFile(rtp_file);
343
344 std::string ref_out_file = "";
345 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000346 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 }
348 RefFiles ref_files(ref_file, ref_out_file);
349
350 NETEQTEST_RTPpacket rtp;
351 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
352 int i = 0;
353 while (rtp.dataLen() >= 0) {
354 std::ostringstream ss;
355 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
356 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000357 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
359 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
360 }
361}
362
363void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
364 const std::string &stat_ref_file,
365 const std::string &rtcp_ref_file) {
366 OpenInputFile(rtp_file);
367 std::string stat_out_file = "";
368 if (stat_ref_file.empty()) {
369 stat_out_file = webrtc::test::OutputPath() +
370 "neteq_network_stats.dat";
371 }
372 RefFiles network_stat_files(stat_ref_file, stat_out_file);
373
374 std::string rtcp_out_file = "";
375 if (rtcp_ref_file.empty()) {
376 rtcp_out_file = webrtc::test::OutputPath() +
377 "neteq_rtcp_stats.dat";
378 }
379 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
380
381 NETEQTEST_RTPpacket rtp;
382 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
383 while (rtp.dataLen() >= 0) {
384 int out_len;
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000385 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386
387 // Query the network statistics API once per second
388 if (sim_clock_ % 1000 == 0) {
389 // Process NetworkStatistics.
390 NetEqNetworkStatistics network_stats;
391 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000392 ASSERT_NO_FATAL_FAILURE(
393 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394
395 // Process RTCPstat.
396 RtcpStatistics rtcp_stats;
397 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000398 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399 }
400 }
401}
402
403void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
404 int timestamp,
405 WebRtcRTPHeader* rtp_info) {
406 rtp_info->header.sequenceNumber = frame_index;
407 rtp_info->header.timestamp = timestamp;
408 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
409 rtp_info->header.payloadType = 94; // PCM16b WB codec.
410 rtp_info->header.markerBit = 0;
411}
412
413void NetEqDecodingTest::PopulateCng(int frame_index,
414 int timestamp,
415 WebRtcRTPHeader* rtp_info,
416 uint8_t* payload,
417 int* payload_len) {
418 rtp_info->header.sequenceNumber = frame_index;
419 rtp_info->header.timestamp = timestamp;
420 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
421 rtp_info->header.payloadType = 98; // WB CNG.
422 rtp_info->header.markerBit = 0;
423 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
424 *payload_len = 1; // Only noise level, no spectral parameters.
425}
426
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000427TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000428 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000429 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000430 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
431 // are identical. The latter could have been removed, but if clients still
432 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000433 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000434 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000435
436 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000437 DecodeAndCompare(input_rtp_file, "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000438 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000439 DecodeAndCompare(input_rtp_file, input_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000440 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441}
442
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000443TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000444 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000445 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000446#if defined(_MSC_VER) && (_MSC_VER >= 1700)
447 // For Visual Studio 2012 and later, we will have to use the generic reference
448 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000449 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000450 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000451#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000452 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000453 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000454#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000455 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000456 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
457 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000458 DecodeAndCheckStats(input_rtp_file, "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000459 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000460 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
461 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000462 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000463}
464
465// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000466TEST_F(NetEqDecodingTest, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000467 // Use fax mode to avoid time-scaling. This is to simplify the testing of
468 // packet waiting times in the packet buffer.
469 neteq_->SetPlayoutMode(kPlayoutFax);
470 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
471 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
472 size_t num_frames = 30;
473 const int kSamples = 10 * 16;
474 const int kPayloadBytes = kSamples * 2;
475 for (size_t i = 0; i < num_frames; ++i) {
476 uint16_t payload[kSamples] = {0};
477 WebRtcRTPHeader rtp_info;
478 rtp_info.header.sequenceNumber = i;
479 rtp_info.header.timestamp = i * kSamples;
480 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
481 rtp_info.header.payloadType = 94; // PCM16b WB codec.
482 rtp_info.header.markerBit = 0;
483 ASSERT_EQ(0, neteq_->InsertPacket(
484 rtp_info,
485 reinterpret_cast<uint8_t*>(payload),
486 kPayloadBytes, 0));
487 }
488 // Pull out all data.
489 for (size_t i = 0; i < num_frames; ++i) {
490 int out_len;
491 int num_channels;
492 NetEqOutputType type;
493 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
494 &num_channels, &type));
495 ASSERT_EQ(kBlockSize16kHz, out_len);
496 }
497
498 std::vector<int> waiting_times;
499 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000500 EXPECT_EQ(num_frames, waiting_times.size());
501 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
502 // spacing (per definition), we expect the delay to increase with 10 ms for
503 // each packet.
504 for (size_t i = 0; i < waiting_times.size(); ++i) {
505 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
506 }
507
508 // Check statistics again and make sure it's been reset.
509 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000510 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000511 EXPECT_EQ(0, len);
512
513 // Process > 100 frames, and make sure that that we get statistics
514 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
515 num_frames = 110;
516 for (size_t i = 0; i < num_frames; ++i) {
517 uint16_t payload[kSamples] = {0};
518 WebRtcRTPHeader rtp_info;
519 rtp_info.header.sequenceNumber = i;
520 rtp_info.header.timestamp = i * kSamples;
521 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
522 rtp_info.header.payloadType = 94; // PCM16b WB codec.
523 rtp_info.header.markerBit = 0;
524 ASSERT_EQ(0, neteq_->InsertPacket(
525 rtp_info,
526 reinterpret_cast<uint8_t*>(payload),
527 kPayloadBytes, 0));
528 int out_len;
529 int num_channels;
530 NetEqOutputType type;
531 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
532 &num_channels, &type));
533 ASSERT_EQ(kBlockSize16kHz, out_len);
534 }
535
536 neteq_->WaitingTimes(&waiting_times);
537 EXPECT_EQ(100u, waiting_times.size());
538}
539
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000540TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000541 const int kNumFrames = 3000; // Needed for convergence.
542 int frame_index = 0;
543 const int kSamples = 10 * 16;
544 const int kPayloadBytes = kSamples * 2;
545 while (frame_index < kNumFrames) {
546 // Insert one packet each time, except every 10th time where we insert two
547 // packets at once. This will create a negative clock-drift of approx. 10%.
548 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
549 for (int n = 0; n < num_packets; ++n) {
550 uint8_t payload[kPayloadBytes] = {0};
551 WebRtcRTPHeader rtp_info;
552 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
553 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
554 ++frame_index;
555 }
556
557 // Pull out data once.
558 int out_len;
559 int num_channels;
560 NetEqOutputType type;
561 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
562 &num_channels, &type));
563 ASSERT_EQ(kBlockSize16kHz, out_len);
564 }
565
566 NetEqNetworkStatistics network_stats;
567 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
568 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
569}
570
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000571TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572 const int kNumFrames = 5000; // Needed for convergence.
573 int frame_index = 0;
574 const int kSamples = 10 * 16;
575 const int kPayloadBytes = kSamples * 2;
576 for (int i = 0; i < kNumFrames; ++i) {
577 // Insert one packet each time, except every 10th time where we don't insert
578 // any packet. This will create a positive clock-drift of approx. 11%.
579 int num_packets = (i % 10 == 9 ? 0 : 1);
580 for (int n = 0; n < num_packets; ++n) {
581 uint8_t payload[kPayloadBytes] = {0};
582 WebRtcRTPHeader rtp_info;
583 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
584 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
585 ++frame_index;
586 }
587
588 // Pull out data once.
589 int out_len;
590 int num_channels;
591 NetEqOutputType type;
592 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
593 &num_channels, &type));
594 ASSERT_EQ(kBlockSize16kHz, out_len);
595 }
596
597 NetEqNetworkStatistics network_stats;
598 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
599 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
600}
601
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000602void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
603 double network_freeze_ms,
604 bool pull_audio_during_freeze,
605 int delay_tolerance_ms,
606 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 uint16_t seq_no = 0;
608 uint32_t timestamp = 0;
609 const int kFrameSizeMs = 30;
610 const int kSamples = kFrameSizeMs * 16;
611 const int kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 double next_input_time_ms = 0.0;
613 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000614 int out_len;
615 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 NetEqOutputType type;
617
618 // Insert speech for 5 seconds.
619 const int kSpeechDurationMs = 5000;
620 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
621 // Each turn in this for loop is 10 ms.
622 while (next_input_time_ms <= t_ms) {
623 // Insert one 30 ms speech frame.
624 uint8_t payload[kPayloadBytes] = {0};
625 WebRtcRTPHeader rtp_info;
626 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
627 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
628 ++seq_no;
629 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000630 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000631 }
632 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
634 &num_channels, &type));
635 ASSERT_EQ(kBlockSize16kHz, out_len);
636 }
637
638 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000639 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640
641 // Insert CNG for 1 minute (= 60000 ms).
642 const int kCngPeriodMs = 100;
643 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
644 const int kCngDurationMs = 60000;
645 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
646 // Each turn in this for loop is 10 ms.
647 while (next_input_time_ms <= t_ms) {
648 // Insert one CNG frame each 100 ms.
649 uint8_t payload[kPayloadBytes];
650 int payload_len;
651 WebRtcRTPHeader rtp_info;
652 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
653 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
654 ++seq_no;
655 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000656 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 }
658 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000659 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
660 &num_channels, &type));
661 ASSERT_EQ(kBlockSize16kHz, out_len);
662 }
663
664 EXPECT_EQ(kOutputCNG, type);
665
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000666 if (network_freeze_ms > 0) {
667 // First keep pulling audio for |network_freeze_ms| without inserting
668 // any data, then insert CNG data corresponding to |network_freeze_ms|
669 // without pulling any output audio.
670 const double loop_end_time = t_ms + network_freeze_ms;
671 for (; t_ms < loop_end_time; t_ms += 10) {
672 // Pull out data once.
673 ASSERT_EQ(0,
674 neteq_->GetAudio(
675 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
676 ASSERT_EQ(kBlockSize16kHz, out_len);
677 EXPECT_EQ(kOutputCNG, type);
678 }
679 bool pull_once = pull_audio_during_freeze;
680 // If |pull_once| is true, GetAudio will be called once half-way through
681 // the network recovery period.
682 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
683 while (next_input_time_ms <= t_ms) {
684 if (pull_once && next_input_time_ms >= pull_time_ms) {
685 pull_once = false;
686 // Pull out data once.
687 ASSERT_EQ(
688 0,
689 neteq_->GetAudio(
690 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
691 ASSERT_EQ(kBlockSize16kHz, out_len);
692 EXPECT_EQ(kOutputCNG, type);
693 t_ms += 10;
694 }
695 // Insert one CNG frame each 100 ms.
696 uint8_t payload[kPayloadBytes];
697 int payload_len;
698 WebRtcRTPHeader rtp_info;
699 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
700 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
701 ++seq_no;
702 timestamp += kCngPeriodSamples;
703 next_input_time_ms += kCngPeriodMs * drift_factor;
704 }
705 }
706
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000707 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000709 while (type != kOutputNormal) {
710 // Each turn in this for loop is 10 ms.
711 while (next_input_time_ms <= t_ms) {
712 // Insert one 30 ms speech frame.
713 uint8_t payload[kPayloadBytes] = {0};
714 WebRtcRTPHeader rtp_info;
715 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
716 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
717 ++seq_no;
718 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000719 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 }
721 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
723 &num_channels, &type));
724 ASSERT_EQ(kBlockSize16kHz, out_len);
725 // Increase clock.
726 t_ms += 10;
727 }
728
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000729 // Check that the speech starts again within reasonable time.
730 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
731 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000732 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000734 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
735 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000736}
737
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000738TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000739 // Apply a clock drift of -25 ms / s (sender faster than receiver).
740 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000741 const double kNetworkFreezeTimeMs = 0.0;
742 const bool kGetAudioDuringFreezeRecovery = false;
743 const int kDelayToleranceMs = 20;
744 const int kMaxTimeToSpeechMs = 100;
745 LongCngWithClockDrift(kDriftFactor,
746 kNetworkFreezeTimeMs,
747 kGetAudioDuringFreezeRecovery,
748 kDelayToleranceMs,
749 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000750}
751
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000752TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000753 // Apply a clock drift of +25 ms / s (sender slower than receiver).
754 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000755 const double kNetworkFreezeTimeMs = 0.0;
756 const bool kGetAudioDuringFreezeRecovery = false;
757 const int kDelayToleranceMs = 20;
758 const int kMaxTimeToSpeechMs = 100;
759 LongCngWithClockDrift(kDriftFactor,
760 kNetworkFreezeTimeMs,
761 kGetAudioDuringFreezeRecovery,
762 kDelayToleranceMs,
763 kMaxTimeToSpeechMs);
764}
765
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000766TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 // Apply a clock drift of -25 ms / s (sender faster than receiver).
768 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
769 const double kNetworkFreezeTimeMs = 5000.0;
770 const bool kGetAudioDuringFreezeRecovery = false;
771 const int kDelayToleranceMs = 50;
772 const int kMaxTimeToSpeechMs = 200;
773 LongCngWithClockDrift(kDriftFactor,
774 kNetworkFreezeTimeMs,
775 kGetAudioDuringFreezeRecovery,
776 kDelayToleranceMs,
777 kMaxTimeToSpeechMs);
778}
779
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000780TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000781 // Apply a clock drift of +25 ms / s (sender slower than receiver).
782 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
783 const double kNetworkFreezeTimeMs = 5000.0;
784 const bool kGetAudioDuringFreezeRecovery = false;
785 const int kDelayToleranceMs = 20;
786 const int kMaxTimeToSpeechMs = 100;
787 LongCngWithClockDrift(kDriftFactor,
788 kNetworkFreezeTimeMs,
789 kGetAudioDuringFreezeRecovery,
790 kDelayToleranceMs,
791 kMaxTimeToSpeechMs);
792}
793
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000794TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000795 // Apply a clock drift of +25 ms / s (sender slower than receiver).
796 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
797 const double kNetworkFreezeTimeMs = 5000.0;
798 const bool kGetAudioDuringFreezeRecovery = true;
799 const int kDelayToleranceMs = 20;
800 const int kMaxTimeToSpeechMs = 100;
801 LongCngWithClockDrift(kDriftFactor,
802 kNetworkFreezeTimeMs,
803 kGetAudioDuringFreezeRecovery,
804 kDelayToleranceMs,
805 kMaxTimeToSpeechMs);
806}
807
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000808TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000809 const double kDriftFactor = 1.0; // No drift.
810 const double kNetworkFreezeTimeMs = 0.0;
811 const bool kGetAudioDuringFreezeRecovery = false;
812 const int kDelayToleranceMs = 10;
813 const int kMaxTimeToSpeechMs = 50;
814 LongCngWithClockDrift(kDriftFactor,
815 kNetworkFreezeTimeMs,
816 kGetAudioDuringFreezeRecovery,
817 kDelayToleranceMs,
818 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000819}
820
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000821TEST_F(NetEqDecodingTest, UnknownPayloadType) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000822 const int kPayloadBytes = 100;
823 uint8_t payload[kPayloadBytes] = {0};
824 WebRtcRTPHeader rtp_info;
825 PopulateRtpInfo(0, 0, &rtp_info);
826 rtp_info.header.payloadType = 1; // Not registered as a decoder.
827 EXPECT_EQ(NetEq::kFail,
828 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
829 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
830}
831
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000832TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000833 const int kPayloadBytes = 100;
834 uint8_t payload[kPayloadBytes] = {0};
835 WebRtcRTPHeader rtp_info;
836 PopulateRtpInfo(0, 0, &rtp_info);
837 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
838 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
839 NetEqOutputType type;
840 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
841 // to GetAudio.
842 for (int i = 0; i < kMaxBlockSize; ++i) {
843 out_data_[i] = 1;
844 }
845 int num_channels;
846 int samples_per_channel;
847 EXPECT_EQ(NetEq::kFail,
848 neteq_->GetAudio(kMaxBlockSize, out_data_,
849 &samples_per_channel, &num_channels, &type));
850 // Verify that there is a decoder error to check.
851 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
852 // Code 6730 is an iSAC error code.
853 EXPECT_EQ(6730, neteq_->LastDecoderError());
854 // Verify that the first 160 samples are set to 0, and that the remaining
855 // samples are left unmodified.
856 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
857 for (int i = 0; i < kExpectedOutputLength; ++i) {
858 std::ostringstream ss;
859 ss << "i = " << i;
860 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
861 EXPECT_EQ(0, out_data_[i]);
862 }
863 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
864 std::ostringstream ss;
865 ss << "i = " << i;
866 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
867 EXPECT_EQ(1, out_data_[i]);
868 }
869}
870
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000871TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000872 NetEqOutputType type;
873 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
874 // to GetAudio.
875 for (int i = 0; i < kMaxBlockSize; ++i) {
876 out_data_[i] = 1;
877 }
878 int num_channels;
879 int samples_per_channel;
880 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
881 &samples_per_channel,
882 &num_channels, &type));
883 // Verify that the first block of samples is set to 0.
884 static const int kExpectedOutputLength =
885 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
886 for (int i = 0; i < kExpectedOutputLength; ++i) {
887 std::ostringstream ss;
888 ss << "i = " << i;
889 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
890 EXPECT_EQ(0, out_data_[i]);
891 }
892}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000893
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000894class NetEqBgnTest
895 : public NetEqDecodingTest,
896 public ::testing::WithParamInterface<NetEq::BackgroundNoiseMode> {
897 protected:
898 NetEqBgnTest() : NetEqDecodingTest() {
899 config_.background_noise_mode = GetParam();
900 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000901
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000902 void CheckBgnOff(int sampling_rate_hz) {
903 int expected_samples_per_channel = 0;
904 uint8_t payload_type = 0xFF; // Invalid.
905 if (sampling_rate_hz == 8000) {
906 expected_samples_per_channel = kBlockSize8kHz;
907 payload_type = 93; // PCM 16, 8 kHz.
908 } else if (sampling_rate_hz == 16000) {
909 expected_samples_per_channel = kBlockSize16kHz;
910 payload_type = 94; // PCM 16, 16 kHZ.
911 } else if (sampling_rate_hz == 32000) {
912 expected_samples_per_channel = kBlockSize32kHz;
913 payload_type = 95; // PCM 16, 32 kHz.
914 } else {
915 ASSERT_TRUE(false); // Unsupported test case.
916 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000917
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000918 NetEqOutputType type;
919 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
920 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
921
922 // Payload of 10 ms of PCM16 32 kHz.
923 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
924
925 // Random payload.
926 for (int n = 0; n < expected_samples_per_channel; ++n) {
927 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
928 }
929 int enc_len_bytes =
930 WebRtcPcm16b_EncodeW16(input,
931 expected_samples_per_channel,
932 reinterpret_cast<int16_t*>(payload));
933 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
934
935 WebRtcRTPHeader rtp_info;
936 PopulateRtpInfo(0, 0, &rtp_info);
937 rtp_info.header.payloadType = payload_type;
938
939 int number_channels = 0;
940 int samples_per_channel = 0;
941
942 uint32_t receive_timestamp = 0;
943 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
944 number_channels = 0;
945 samples_per_channel = 0;
946 ASSERT_EQ(0,
947 neteq_->InsertPacket(
948 rtp_info, payload, enc_len_bytes, receive_timestamp));
949 ASSERT_EQ(0,
950 neteq_->GetAudio(kBlockSize32kHz,
951 output,
952 &samples_per_channel,
953 &number_channels,
954 &type));
955 ASSERT_EQ(1, number_channels);
956 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
957 ASSERT_EQ(kOutputNormal, type);
958
959 // Next packet.
960 rtp_info.header.timestamp += expected_samples_per_channel;
961 rtp_info.header.sequenceNumber++;
962 receive_timestamp += expected_samples_per_channel;
963 }
964
965 number_channels = 0;
966 samples_per_channel = 0;
967
968 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
969 // one frame without checking speech-type. This is the first frame pulled
970 // without inserting any packet, and might not be labeled as PLC.
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
980 // To be able to test the fading of background noise we need at lease to
981 // pull 611 frames.
982 const int kFadingThreshold = 611;
983
984 // Test several CNG-to-PLC packet for the expected behavior. The number 20
985 // is arbitrary, but sufficiently large to test enough number of frames.
986 const int kNumPlcToCngTestFrames = 20;
987 bool plc_to_cng = false;
988 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
989 number_channels = 0;
990 samples_per_channel = 0;
991 memset(output, 1, sizeof(output)); // Set to non-zero.
992 ASSERT_EQ(0,
993 neteq_->GetAudio(kBlockSize32kHz,
994 output,
995 &samples_per_channel,
996 &number_channels,
997 &type));
998 ASSERT_EQ(1, number_channels);
999 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1000 if (type == kOutputPLCtoCNG) {
1001 plc_to_cng = true;
1002 double sum_squared = 0;
1003 for (int k = 0; k < number_channels * samples_per_channel; ++k)
1004 sum_squared += output[k] * output[k];
1005 if (config_.background_noise_mode == NetEq::kBgnOn) {
1006 EXPECT_NE(0, sum_squared);
1007 } else if (config_.background_noise_mode == NetEq::kBgnOff ||
1008 n > kFadingThreshold) {
1009 EXPECT_EQ(0, sum_squared);
1010 }
1011 } else {
1012 EXPECT_EQ(kOutputPLC, type);
1013 }
1014 }
1015 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1016 }
1017};
1018
1019TEST_P(NetEqBgnTest, BackgroundNoise) {
1020 CheckBgnOff(8000);
1021 CheckBgnOff(16000);
1022 CheckBgnOff(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001023}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001024
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001025INSTANTIATE_TEST_CASE_P(BgnModes,
1026 NetEqBgnTest,
1027 ::testing::Values(NetEq::kBgnOn,
1028 NetEq::kBgnOff,
1029 NetEq::kBgnFade));
1030
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001031TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001032 WebRtcRTPHeader rtp_info;
1033 uint32_t receive_timestamp = 0;
1034 // For the readability use the following payloads instead of the defaults of
1035 // this test.
1036 uint8_t kPcm16WbPayloadType = 1;
1037 uint8_t kCngNbPayloadType = 2;
1038 uint8_t kCngWbPayloadType = 3;
1039 uint8_t kCngSwb32PayloadType = 4;
1040 uint8_t kCngSwb48PayloadType = 5;
1041 uint8_t kAvtPayloadType = 6;
1042 uint8_t kRedPayloadType = 7;
1043 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1044
1045 // Register decoders.
1046 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1047 kPcm16WbPayloadType));
1048 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1049 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1050 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1051 kCngSwb32PayloadType));
1052 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1053 kCngSwb48PayloadType));
1054 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1055 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1056 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1057
1058 PopulateRtpInfo(0, 0, &rtp_info);
1059 rtp_info.header.payloadType = kPcm16WbPayloadType;
1060
1061 // The first packet injected cannot be sync-packet.
1062 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1063
1064 // Payload length of 10 ms PCM16 16 kHz.
1065 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1066 uint8_t payload[kPayloadBytes] = {0};
1067 ASSERT_EQ(0, neteq_->InsertPacket(
1068 rtp_info, payload, kPayloadBytes, receive_timestamp));
1069
1070 // Next packet. Last packet contained 10 ms audio.
1071 rtp_info.header.sequenceNumber++;
1072 rtp_info.header.timestamp += kBlockSize16kHz;
1073 receive_timestamp += kBlockSize16kHz;
1074
1075 // Unacceptable payload types CNG, AVT (DTMF), RED.
1076 rtp_info.header.payloadType = kCngNbPayloadType;
1077 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1078
1079 rtp_info.header.payloadType = kCngWbPayloadType;
1080 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081
1082 rtp_info.header.payloadType = kCngSwb32PayloadType;
1083 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1084
1085 rtp_info.header.payloadType = kCngSwb48PayloadType;
1086 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1087
1088 rtp_info.header.payloadType = kAvtPayloadType;
1089 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1090
1091 rtp_info.header.payloadType = kRedPayloadType;
1092 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1093
1094 // Change of codec cannot be initiated with a sync packet.
1095 rtp_info.header.payloadType = kIsacPayloadType;
1096 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1097
1098 // Change of SSRC is not allowed with a sync packet.
1099 rtp_info.header.payloadType = kPcm16WbPayloadType;
1100 ++rtp_info.header.ssrc;
1101 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1102
1103 --rtp_info.header.ssrc;
1104 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1105}
1106
1107// First insert several noise like packets, then sync-packets. Decoding all
1108// packets should not produce error, statistics should not show any packet loss
1109// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001110// TODO(turajs) we will have a better test if we have a referece NetEq, and
1111// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1112// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001113TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001114 WebRtcRTPHeader rtp_info;
1115 PopulateRtpInfo(0, 0, &rtp_info);
1116 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1117 uint8_t payload[kPayloadBytes];
1118 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001119 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001120 for (int n = 0; n < kPayloadBytes; ++n) {
1121 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1122 }
1123 // Insert some packets which decode to noise. We are not interested in
1124 // actual decoded values.
1125 NetEqOutputType output_type;
1126 int num_channels;
1127 int samples_per_channel;
1128 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001129 for (int n = 0; n < 100; ++n) {
1130 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1131 receive_timestamp));
1132 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1133 &samples_per_channel, &num_channels,
1134 &output_type));
1135 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1136 ASSERT_EQ(1, num_channels);
1137
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001138 rtp_info.header.sequenceNumber++;
1139 rtp_info.header.timestamp += kBlockSize16kHz;
1140 receive_timestamp += kBlockSize16kHz;
1141 }
1142 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001143
1144 // Make sure sufficient number of sync packets are inserted that we can
1145 // conduct a test.
1146 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001147 // Insert sync-packets, the decoded sequence should be all-zero.
1148 for (int n = 0; n < kNumSyncPackets; ++n) {
1149 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1150 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1151 &samples_per_channel, &num_channels,
1152 &output_type));
1153 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1154 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001155 if (n > algorithmic_frame_delay) {
1156 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1157 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001158 rtp_info.header.sequenceNumber++;
1159 rtp_info.header.timestamp += kBlockSize16kHz;
1160 receive_timestamp += kBlockSize16kHz;
1161 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001162
1163 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001164 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001165 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1166 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1167 receive_timestamp));
1168 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1169 &samples_per_channel, &num_channels,
1170 &output_type));
1171 if (n >= algorithmic_frame_delay + 1) {
1172 // Expect that this frame contain samples from regular RTP.
1173 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1174 }
1175 rtp_info.header.sequenceNumber++;
1176 rtp_info.header.timestamp += kBlockSize16kHz;
1177 receive_timestamp += kBlockSize16kHz;
1178 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001179 NetEqNetworkStatistics network_stats;
1180 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1181 // Expecting a "clean" network.
1182 EXPECT_EQ(0, network_stats.packet_loss_rate);
1183 EXPECT_EQ(0, network_stats.expand_rate);
1184 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001185 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001186}
1187
1188// Test if the size of the packet buffer reported correctly when containing
1189// sync packets. Also, test if network packets override sync packets. That is to
1190// prefer decoding a network packet to a sync packet, if both have same sequence
1191// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001192TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001193 WebRtcRTPHeader rtp_info;
1194 PopulateRtpInfo(0, 0, &rtp_info);
1195 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1196 uint8_t payload[kPayloadBytes];
1197 int16_t decoded[kBlockSize16kHz];
1198 for (int n = 0; n < kPayloadBytes; ++n) {
1199 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1200 }
1201 // Insert some packets which decode to noise. We are not interested in
1202 // actual decoded values.
1203 NetEqOutputType output_type;
1204 int num_channels;
1205 int samples_per_channel;
1206 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001207 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1208 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001209 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1210 receive_timestamp));
1211 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1212 &samples_per_channel, &num_channels,
1213 &output_type));
1214 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1215 ASSERT_EQ(1, num_channels);
1216 rtp_info.header.sequenceNumber++;
1217 rtp_info.header.timestamp += kBlockSize16kHz;
1218 receive_timestamp += kBlockSize16kHz;
1219 }
1220 const int kNumSyncPackets = 10;
1221
1222 WebRtcRTPHeader first_sync_packet_rtp_info;
1223 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1224
1225 // Insert sync-packets, but no decoding.
1226 for (int n = 0; n < kNumSyncPackets; ++n) {
1227 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1228 rtp_info.header.sequenceNumber++;
1229 rtp_info.header.timestamp += kBlockSize16kHz;
1230 receive_timestamp += kBlockSize16kHz;
1231 }
1232 NetEqNetworkStatistics network_stats;
1233 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001234 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1235 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001236
1237 // Rewind |rtp_info| to that of the first sync packet.
1238 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1239
1240 // Insert.
1241 for (int n = 0; n < kNumSyncPackets; ++n) {
1242 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1243 receive_timestamp));
1244 rtp_info.header.sequenceNumber++;
1245 rtp_info.header.timestamp += kBlockSize16kHz;
1246 receive_timestamp += kBlockSize16kHz;
1247 }
1248
1249 // Decode.
1250 for (int n = 0; n < kNumSyncPackets; ++n) {
1251 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1252 &samples_per_channel, &num_channels,
1253 &output_type));
1254 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1255 ASSERT_EQ(1, num_channels);
1256 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1257 }
1258}
1259
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001260void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1261 uint32_t start_timestamp,
1262 const std::set<uint16_t>& drop_seq_numbers,
1263 bool expect_seq_no_wrap,
1264 bool expect_timestamp_wrap) {
1265 uint16_t seq_no = start_seq_no;
1266 uint32_t timestamp = start_timestamp;
1267 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1268 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1269 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1270 const int kPayloadBytes = kSamples * sizeof(int16_t);
1271 double next_input_time_ms = 0.0;
1272 int16_t decoded[kBlockSize16kHz];
1273 int num_channels;
1274 int samples_per_channel;
1275 NetEqOutputType output_type;
1276 uint32_t receive_timestamp = 0;
1277
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001278 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001279 const int kSpeechDurationMs = 2000;
1280 int packets_inserted = 0;
1281 uint16_t last_seq_no;
1282 uint32_t last_timestamp;
1283 bool timestamp_wrapped = false;
1284 bool seq_no_wrapped = false;
1285 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1286 // Each turn in this for loop is 10 ms.
1287 while (next_input_time_ms <= t_ms) {
1288 // Insert one 30 ms speech frame.
1289 uint8_t payload[kPayloadBytes] = {0};
1290 WebRtcRTPHeader rtp_info;
1291 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1292 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1293 // This sequence number was not in the set to drop. Insert it.
1294 ASSERT_EQ(0,
1295 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1296 receive_timestamp));
1297 ++packets_inserted;
1298 }
1299 NetEqNetworkStatistics network_stats;
1300 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1301
1302 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1303 // packet size for first few packets. Therefore we refrain from checking
1304 // the criteria.
1305 if (packets_inserted > 4) {
1306 // Expect preferred and actual buffer size to be no more than 2 frames.
1307 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001308 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1309 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001310 }
1311 last_seq_no = seq_no;
1312 last_timestamp = timestamp;
1313
1314 ++seq_no;
1315 timestamp += kSamples;
1316 receive_timestamp += kSamples;
1317 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1318
1319 seq_no_wrapped |= seq_no < last_seq_no;
1320 timestamp_wrapped |= timestamp < last_timestamp;
1321 }
1322 // Pull out data once.
1323 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1324 &samples_per_channel, &num_channels,
1325 &output_type));
1326 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1327 ASSERT_EQ(1, num_channels);
1328
1329 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001330 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001331 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001332 }
1333 // Make sure we have actually tested wrap-around.
1334 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1335 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1336}
1337
1338TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1339 // Start with a sequence number that will soon wrap.
1340 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1341 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1342}
1343
1344TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1345 // Start with a sequence number that will soon wrap.
1346 std::set<uint16_t> drop_seq_numbers;
1347 drop_seq_numbers.insert(0xFFFF);
1348 drop_seq_numbers.insert(0x0);
1349 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1350}
1351
1352TEST_F(NetEqDecodingTest, TimestampWrap) {
1353 // Start with a timestamp that will soon wrap.
1354 std::set<uint16_t> drop_seq_numbers;
1355 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1356}
1357
1358TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1359 // Start with a timestamp and a sequence number that will wrap at the same
1360 // time.
1361 std::set<uint16_t> drop_seq_numbers;
1362 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1363}
1364
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001365void NetEqDecodingTest::DuplicateCng() {
1366 uint16_t seq_no = 0;
1367 uint32_t timestamp = 0;
1368 const int kFrameSizeMs = 10;
1369 const int kSampleRateKhz = 16;
1370 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1371 const int kPayloadBytes = kSamples * 2;
1372
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001373 const int algorithmic_delay_samples = std::max(
1374 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001375 // Insert three speech packet. Three are needed to get the frame length
1376 // correct.
1377 int out_len;
1378 int num_channels;
1379 NetEqOutputType type;
1380 uint8_t payload[kPayloadBytes] = {0};
1381 WebRtcRTPHeader rtp_info;
1382 for (int i = 0; i < 3; ++i) {
1383 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1384 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1385 ++seq_no;
1386 timestamp += kSamples;
1387
1388 // Pull audio once.
1389 ASSERT_EQ(0,
1390 neteq_->GetAudio(
1391 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1392 ASSERT_EQ(kBlockSize16kHz, out_len);
1393 }
1394 // Verify speech output.
1395 EXPECT_EQ(kOutputNormal, type);
1396
1397 // Insert same CNG packet twice.
1398 const int kCngPeriodMs = 100;
1399 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1400 int payload_len;
1401 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1402 // This is the first time this CNG packet is inserted.
1403 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1404
1405 // Pull audio once and make sure CNG is played.
1406 ASSERT_EQ(0,
1407 neteq_->GetAudio(
1408 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1409 ASSERT_EQ(kBlockSize16kHz, out_len);
1410 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001411 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001412
1413 // Insert the same CNG packet again. Note that at this point it is old, since
1414 // we have already decoded the first copy of it.
1415 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1416
1417 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1418 // we have already pulled out CNG once.
1419 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1420 ASSERT_EQ(0,
1421 neteq_->GetAudio(
1422 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1423 ASSERT_EQ(kBlockSize16kHz, out_len);
1424 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001425 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001426 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001427 }
1428
1429 // Insert speech again.
1430 ++seq_no;
1431 timestamp += kCngPeriodSamples;
1432 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1433 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1434
1435 // Pull audio once and verify that the output is speech again.
1436 ASSERT_EQ(0,
1437 neteq_->GetAudio(
1438 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1439 ASSERT_EQ(kBlockSize16kHz, out_len);
1440 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001441 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001442 PlayoutTimestamp());
1443}
1444
1445uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1446 uint32_t playout_timestamp = 0;
1447 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1448 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001449}
1450
1451TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001452} // namespace webrtc