blob: 9103436190d78ee03b5f7319e49e35eab74603a5 [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
15#include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
16
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"
28#include "webrtc/modules/audio_coding/neteq4/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.orgff43c852013-09-25 00:07:27 +0000217 void CheckBgnOff(int sampling_rate, NetEqBackgroundNoiseMode bgn_mode);
218
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000219 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
220 const std::set<uint16_t>& drop_seq_numbers,
221 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
222
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000223 void LongCngWithClockDrift(double drift_factor,
224 double network_freeze_ms,
225 bool pull_audio_during_freeze,
226 int delay_tolerance_ms,
227 int max_time_to_speech_ms);
228
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000229 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000230
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231 NetEq* neteq_;
232 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),
249 rtp_fp_(NULL),
250 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000251 output_sample_rate_(kInitSampleRateHz),
252 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 memset(out_data_, 0, sizeof(out_data_));
254}
255
256void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000257 NetEq::Config config;
258 config.sample_rate_hz = kInitSampleRateHz;
259 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
339void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
340 const std::string &ref_file) {
341 OpenInputFile(rtp_file);
342
343 std::string ref_out_file = "";
344 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000345 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 }
347 RefFiles ref_files(ref_file, ref_out_file);
348
349 NETEQTEST_RTPpacket rtp;
350 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
351 int i = 0;
352 while (rtp.dataLen() >= 0) {
353 std::ostringstream ss;
354 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
355 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000356 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
358 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
359 }
360}
361
362void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
363 const std::string &stat_ref_file,
364 const std::string &rtcp_ref_file) {
365 OpenInputFile(rtp_file);
366 std::string stat_out_file = "";
367 if (stat_ref_file.empty()) {
368 stat_out_file = webrtc::test::OutputPath() +
369 "neteq_network_stats.dat";
370 }
371 RefFiles network_stat_files(stat_ref_file, stat_out_file);
372
373 std::string rtcp_out_file = "";
374 if (rtcp_ref_file.empty()) {
375 rtcp_out_file = webrtc::test::OutputPath() +
376 "neteq_rtcp_stats.dat";
377 }
378 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
379
380 NETEQTEST_RTPpacket rtp;
381 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
382 while (rtp.dataLen() >= 0) {
383 int out_len;
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000384 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385
386 // Query the network statistics API once per second
387 if (sim_clock_ % 1000 == 0) {
388 // Process NetworkStatistics.
389 NetEqNetworkStatistics network_stats;
390 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000391 ASSERT_NO_FATAL_FAILURE(
392 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393
394 // Process RTCPstat.
395 RtcpStatistics rtcp_stats;
396 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000397 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 }
399 }
400}
401
402void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
403 int timestamp,
404 WebRtcRTPHeader* rtp_info) {
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 = 94; // PCM16b WB codec.
409 rtp_info->header.markerBit = 0;
410}
411
412void NetEqDecodingTest::PopulateCng(int frame_index,
413 int timestamp,
414 WebRtcRTPHeader* rtp_info,
415 uint8_t* payload,
416 int* payload_len) {
417 rtp_info->header.sequenceNumber = frame_index;
418 rtp_info->header.timestamp = timestamp;
419 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
420 rtp_info->header.payloadType = 98; // WB CNG.
421 rtp_info->header.markerBit = 0;
422 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
423 *payload_len = 1; // Only noise level, no spectral parameters.
424}
425
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000426void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
427 NetEqBackgroundNoiseMode bgn_mode) {
428 int expected_samples_per_channel = 0;
429 uint8_t payload_type = 0xFF; // Invalid.
430 if (sampling_rate_hz == 8000) {
431 expected_samples_per_channel = kBlockSize8kHz;
432 payload_type = 93; // PCM 16, 8 kHz.
433 } else if (sampling_rate_hz == 16000) {
434 expected_samples_per_channel = kBlockSize16kHz;
435 payload_type = 94; // PCM 16, 16 kHZ.
436 } else if (sampling_rate_hz == 32000) {
437 expected_samples_per_channel = kBlockSize32kHz;
438 payload_type = 95; // PCM 16, 32 kHz.
439 } else {
440 ASSERT_TRUE(false); // Unsupported test case.
441 }
442
443 NetEqOutputType type;
444 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
445 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
446
447 // Payload of 10 ms of PCM16 32 kHz.
448 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
449
450 // Random payload.
451 for (int n = 0; n < expected_samples_per_channel; ++n) {
452 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
453 }
454 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
455 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
456 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
457
458 WebRtcRTPHeader rtp_info;
459 PopulateRtpInfo(0, 0, &rtp_info);
460 rtp_info.header.payloadType = payload_type;
461
462 int number_channels = 0;
463 int samples_per_channel = 0;
464
465 uint32_t receive_timestamp = 0;
466 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
467 number_channels = 0;
468 samples_per_channel = 0;
469 ASSERT_EQ(0, neteq_->InsertPacket(
470 rtp_info, payload, enc_len_bytes, receive_timestamp));
471 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
472 &number_channels, &type));
473 ASSERT_EQ(1, number_channels);
474 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
475 ASSERT_EQ(kOutputNormal, type);
476
477 // Next packet.
478 rtp_info.header.timestamp += expected_samples_per_channel;
479 rtp_info.header.sequenceNumber++;
480 receive_timestamp += expected_samples_per_channel;
481 }
482
483 number_channels = 0;
484 samples_per_channel = 0;
485
486 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
487 // frame without checking speech-type. This is the first frame pulled without
488 // inserting any packet, and might not be labeled as PCL.
489 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
490 &number_channels, &type));
491 ASSERT_EQ(1, number_channels);
492 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
493
494 // To be able to test the fading of background noise we need at lease to pull
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000495 // 611 frames.
496 const int kFadingThreshold = 611;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000497
498 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
499 // arbitrary, but sufficiently large to test enough number of frames.
500 const int kNumPlcToCngTestFrames = 20;
501 bool plc_to_cng = false;
502 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
503 number_channels = 0;
504 samples_per_channel = 0;
505 memset(output, 1, sizeof(output)); // Set to non-zero.
506 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
507 &number_channels, &type));
508 ASSERT_EQ(1, number_channels);
509 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
510 if (type == kOutputPLCtoCNG) {
511 plc_to_cng = true;
512 double sum_squared = 0;
513 for (int k = 0; k < number_channels * samples_per_channel; ++k)
514 sum_squared += output[k] * output[k];
515 if (bgn_mode == kBgnOn) {
516 EXPECT_NE(0, sum_squared);
517 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
518 EXPECT_EQ(0, sum_squared);
519 }
520 } else {
521 EXPECT_EQ(kOutputPLC, type);
522 }
523 }
524 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
525}
526
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000527#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
528// Disabled for Windows 64-bit until webrtc:1458 is fixed.
529#define MAYBE_TestBitExactness DISABLED_TestBitExactness
530#else
531#define MAYBE_TestBitExactness TestBitExactness
532#endif
533
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000534TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000535 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000536 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000537#if defined(_MSC_VER) && (_MSC_VER >= 1700)
538 // For Visual Studio 2012 and later, we will have to use the generic reference
539 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000540 const std::string input_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000541 "resources/audio_coding/neteq4_universal_ref.pcm";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000542#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000543 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000544 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000545#endif
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000546
547 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000548 DecodeAndCompare(input_rtp_file, "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000549 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000550 DecodeAndCompare(input_rtp_file, input_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000551 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000552}
553
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000554TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000555 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000556 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000557#if defined(_MSC_VER) && (_MSC_VER >= 1700)
558 // For Visual Studio 2012 and later, we will have to use the generic reference
559 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000560 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000561 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000562#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000563 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000564 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000565#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000566 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000567 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
568 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000569 DecodeAndCheckStats(input_rtp_file, "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000570 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000571 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
572 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000573 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574}
575
576// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000577TEST_F(NetEqDecodingTest, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578 // Use fax mode to avoid time-scaling. This is to simplify the testing of
579 // packet waiting times in the packet buffer.
580 neteq_->SetPlayoutMode(kPlayoutFax);
581 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
582 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
583 size_t num_frames = 30;
584 const int kSamples = 10 * 16;
585 const int kPayloadBytes = kSamples * 2;
586 for (size_t i = 0; i < num_frames; ++i) {
587 uint16_t payload[kSamples] = {0};
588 WebRtcRTPHeader rtp_info;
589 rtp_info.header.sequenceNumber = i;
590 rtp_info.header.timestamp = i * kSamples;
591 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
592 rtp_info.header.payloadType = 94; // PCM16b WB codec.
593 rtp_info.header.markerBit = 0;
594 ASSERT_EQ(0, neteq_->InsertPacket(
595 rtp_info,
596 reinterpret_cast<uint8_t*>(payload),
597 kPayloadBytes, 0));
598 }
599 // Pull out all data.
600 for (size_t i = 0; i < num_frames; ++i) {
601 int out_len;
602 int num_channels;
603 NetEqOutputType type;
604 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
605 &num_channels, &type));
606 ASSERT_EQ(kBlockSize16kHz, out_len);
607 }
608
609 std::vector<int> waiting_times;
610 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000611 EXPECT_EQ(num_frames, waiting_times.size());
612 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
613 // spacing (per definition), we expect the delay to increase with 10 ms for
614 // each packet.
615 for (size_t i = 0; i < waiting_times.size(); ++i) {
616 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
617 }
618
619 // Check statistics again and make sure it's been reset.
620 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000621 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622 EXPECT_EQ(0, len);
623
624 // Process > 100 frames, and make sure that that we get statistics
625 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
626 num_frames = 110;
627 for (size_t i = 0; i < num_frames; ++i) {
628 uint16_t payload[kSamples] = {0};
629 WebRtcRTPHeader rtp_info;
630 rtp_info.header.sequenceNumber = i;
631 rtp_info.header.timestamp = i * kSamples;
632 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
633 rtp_info.header.payloadType = 94; // PCM16b WB codec.
634 rtp_info.header.markerBit = 0;
635 ASSERT_EQ(0, neteq_->InsertPacket(
636 rtp_info,
637 reinterpret_cast<uint8_t*>(payload),
638 kPayloadBytes, 0));
639 int out_len;
640 int num_channels;
641 NetEqOutputType type;
642 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
643 &num_channels, &type));
644 ASSERT_EQ(kBlockSize16kHz, out_len);
645 }
646
647 neteq_->WaitingTimes(&waiting_times);
648 EXPECT_EQ(100u, waiting_times.size());
649}
650
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000651TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000652 const int kNumFrames = 3000; // Needed for convergence.
653 int frame_index = 0;
654 const int kSamples = 10 * 16;
655 const int kPayloadBytes = kSamples * 2;
656 while (frame_index < kNumFrames) {
657 // Insert one packet each time, except every 10th time where we insert two
658 // packets at once. This will create a negative clock-drift of approx. 10%.
659 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
660 for (int n = 0; n < num_packets; ++n) {
661 uint8_t payload[kPayloadBytes] = {0};
662 WebRtcRTPHeader rtp_info;
663 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
664 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
665 ++frame_index;
666 }
667
668 // Pull out data once.
669 int out_len;
670 int num_channels;
671 NetEqOutputType type;
672 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
673 &num_channels, &type));
674 ASSERT_EQ(kBlockSize16kHz, out_len);
675 }
676
677 NetEqNetworkStatistics network_stats;
678 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
679 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
680}
681
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000682TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000683 const int kNumFrames = 5000; // Needed for convergence.
684 int frame_index = 0;
685 const int kSamples = 10 * 16;
686 const int kPayloadBytes = kSamples * 2;
687 for (int i = 0; i < kNumFrames; ++i) {
688 // Insert one packet each time, except every 10th time where we don't insert
689 // any packet. This will create a positive clock-drift of approx. 11%.
690 int num_packets = (i % 10 == 9 ? 0 : 1);
691 for (int n = 0; n < num_packets; ++n) {
692 uint8_t payload[kPayloadBytes] = {0};
693 WebRtcRTPHeader rtp_info;
694 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
695 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
696 ++frame_index;
697 }
698
699 // Pull out data once.
700 int out_len;
701 int num_channels;
702 NetEqOutputType type;
703 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
704 &num_channels, &type));
705 ASSERT_EQ(kBlockSize16kHz, out_len);
706 }
707
708 NetEqNetworkStatistics network_stats;
709 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
710 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
711}
712
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000713void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
714 double network_freeze_ms,
715 bool pull_audio_during_freeze,
716 int delay_tolerance_ms,
717 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000718 uint16_t seq_no = 0;
719 uint32_t timestamp = 0;
720 const int kFrameSizeMs = 30;
721 const int kSamples = kFrameSizeMs * 16;
722 const int kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000723 double next_input_time_ms = 0.0;
724 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000725 int out_len;
726 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000727 NetEqOutputType type;
728
729 // Insert speech for 5 seconds.
730 const int kSpeechDurationMs = 5000;
731 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
732 // Each turn in this for loop is 10 ms.
733 while (next_input_time_ms <= t_ms) {
734 // Insert one 30 ms speech frame.
735 uint8_t payload[kPayloadBytes] = {0};
736 WebRtcRTPHeader rtp_info;
737 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
738 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
739 ++seq_no;
740 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000741 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000742 }
743 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
745 &num_channels, &type));
746 ASSERT_EQ(kBlockSize16kHz, out_len);
747 }
748
749 EXPECT_EQ(kOutputNormal, type);
750 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
751
752 // Insert CNG for 1 minute (= 60000 ms).
753 const int kCngPeriodMs = 100;
754 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
755 const int kCngDurationMs = 60000;
756 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
757 // Each turn in this for loop is 10 ms.
758 while (next_input_time_ms <= t_ms) {
759 // Insert one CNG frame each 100 ms.
760 uint8_t payload[kPayloadBytes];
761 int payload_len;
762 WebRtcRTPHeader rtp_info;
763 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
764 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
765 ++seq_no;
766 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000767 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000768 }
769 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000770 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
771 &num_channels, &type));
772 ASSERT_EQ(kBlockSize16kHz, out_len);
773 }
774
775 EXPECT_EQ(kOutputCNG, type);
776
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000777 if (network_freeze_ms > 0) {
778 // First keep pulling audio for |network_freeze_ms| without inserting
779 // any data, then insert CNG data corresponding to |network_freeze_ms|
780 // without pulling any output audio.
781 const double loop_end_time = t_ms + network_freeze_ms;
782 for (; t_ms < loop_end_time; t_ms += 10) {
783 // Pull out data once.
784 ASSERT_EQ(0,
785 neteq_->GetAudio(
786 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
787 ASSERT_EQ(kBlockSize16kHz, out_len);
788 EXPECT_EQ(kOutputCNG, type);
789 }
790 bool pull_once = pull_audio_during_freeze;
791 // If |pull_once| is true, GetAudio will be called once half-way through
792 // the network recovery period.
793 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
794 while (next_input_time_ms <= t_ms) {
795 if (pull_once && next_input_time_ms >= pull_time_ms) {
796 pull_once = false;
797 // Pull out data once.
798 ASSERT_EQ(
799 0,
800 neteq_->GetAudio(
801 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
802 ASSERT_EQ(kBlockSize16kHz, out_len);
803 EXPECT_EQ(kOutputCNG, type);
804 t_ms += 10;
805 }
806 // Insert one CNG frame each 100 ms.
807 uint8_t payload[kPayloadBytes];
808 int payload_len;
809 WebRtcRTPHeader rtp_info;
810 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
811 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
812 ++seq_no;
813 timestamp += kCngPeriodSamples;
814 next_input_time_ms += kCngPeriodMs * drift_factor;
815 }
816 }
817
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000818 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000819 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000820 while (type != kOutputNormal) {
821 // Each turn in this for loop is 10 ms.
822 while (next_input_time_ms <= t_ms) {
823 // Insert one 30 ms speech frame.
824 uint8_t payload[kPayloadBytes] = {0};
825 WebRtcRTPHeader rtp_info;
826 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
827 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
828 ++seq_no;
829 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000830 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000831 }
832 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000833 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
834 &num_channels, &type));
835 ASSERT_EQ(kBlockSize16kHz, out_len);
836 // Increase clock.
837 t_ms += 10;
838 }
839
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000840 // Check that the speech starts again within reasonable time.
841 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
842 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000843 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
844 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000845 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
846 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847}
848
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000849TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000850 // Apply a clock drift of -25 ms / s (sender faster than receiver).
851 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000852 const double kNetworkFreezeTimeMs = 0.0;
853 const bool kGetAudioDuringFreezeRecovery = false;
854 const int kDelayToleranceMs = 20;
855 const int kMaxTimeToSpeechMs = 100;
856 LongCngWithClockDrift(kDriftFactor,
857 kNetworkFreezeTimeMs,
858 kGetAudioDuringFreezeRecovery,
859 kDelayToleranceMs,
860 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000861}
862
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000863TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000864 // Apply a clock drift of +25 ms / s (sender slower than receiver).
865 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000866 const double kNetworkFreezeTimeMs = 0.0;
867 const bool kGetAudioDuringFreezeRecovery = false;
868 const int kDelayToleranceMs = 20;
869 const int kMaxTimeToSpeechMs = 100;
870 LongCngWithClockDrift(kDriftFactor,
871 kNetworkFreezeTimeMs,
872 kGetAudioDuringFreezeRecovery,
873 kDelayToleranceMs,
874 kMaxTimeToSpeechMs);
875}
876
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000877TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000878 // Apply a clock drift of -25 ms / s (sender faster than receiver).
879 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
880 const double kNetworkFreezeTimeMs = 5000.0;
881 const bool kGetAudioDuringFreezeRecovery = false;
882 const int kDelayToleranceMs = 50;
883 const int kMaxTimeToSpeechMs = 200;
884 LongCngWithClockDrift(kDriftFactor,
885 kNetworkFreezeTimeMs,
886 kGetAudioDuringFreezeRecovery,
887 kDelayToleranceMs,
888 kMaxTimeToSpeechMs);
889}
890
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000891TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000892 // Apply a clock drift of +25 ms / s (sender slower than receiver).
893 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
894 const double kNetworkFreezeTimeMs = 5000.0;
895 const bool kGetAudioDuringFreezeRecovery = false;
896 const int kDelayToleranceMs = 20;
897 const int kMaxTimeToSpeechMs = 100;
898 LongCngWithClockDrift(kDriftFactor,
899 kNetworkFreezeTimeMs,
900 kGetAudioDuringFreezeRecovery,
901 kDelayToleranceMs,
902 kMaxTimeToSpeechMs);
903}
904
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000905TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000906 // Apply a clock drift of +25 ms / s (sender slower than receiver).
907 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
908 const double kNetworkFreezeTimeMs = 5000.0;
909 const bool kGetAudioDuringFreezeRecovery = true;
910 const int kDelayToleranceMs = 20;
911 const int kMaxTimeToSpeechMs = 100;
912 LongCngWithClockDrift(kDriftFactor,
913 kNetworkFreezeTimeMs,
914 kGetAudioDuringFreezeRecovery,
915 kDelayToleranceMs,
916 kMaxTimeToSpeechMs);
917}
918
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000919TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000920 const double kDriftFactor = 1.0; // No drift.
921 const double kNetworkFreezeTimeMs = 0.0;
922 const bool kGetAudioDuringFreezeRecovery = false;
923 const int kDelayToleranceMs = 10;
924 const int kMaxTimeToSpeechMs = 50;
925 LongCngWithClockDrift(kDriftFactor,
926 kNetworkFreezeTimeMs,
927 kGetAudioDuringFreezeRecovery,
928 kDelayToleranceMs,
929 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000930}
931
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000932TEST_F(NetEqDecodingTest, UnknownPayloadType) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000933 const int kPayloadBytes = 100;
934 uint8_t payload[kPayloadBytes] = {0};
935 WebRtcRTPHeader rtp_info;
936 PopulateRtpInfo(0, 0, &rtp_info);
937 rtp_info.header.payloadType = 1; // Not registered as a decoder.
938 EXPECT_EQ(NetEq::kFail,
939 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
940 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
941}
942
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000943TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000944 const int kPayloadBytes = 100;
945 uint8_t payload[kPayloadBytes] = {0};
946 WebRtcRTPHeader rtp_info;
947 PopulateRtpInfo(0, 0, &rtp_info);
948 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
949 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
950 NetEqOutputType type;
951 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
952 // to GetAudio.
953 for (int i = 0; i < kMaxBlockSize; ++i) {
954 out_data_[i] = 1;
955 }
956 int num_channels;
957 int samples_per_channel;
958 EXPECT_EQ(NetEq::kFail,
959 neteq_->GetAudio(kMaxBlockSize, out_data_,
960 &samples_per_channel, &num_channels, &type));
961 // Verify that there is a decoder error to check.
962 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
963 // Code 6730 is an iSAC error code.
964 EXPECT_EQ(6730, neteq_->LastDecoderError());
965 // Verify that the first 160 samples are set to 0, and that the remaining
966 // samples are left unmodified.
967 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
968 for (int i = 0; i < kExpectedOutputLength; ++i) {
969 std::ostringstream ss;
970 ss << "i = " << i;
971 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
972 EXPECT_EQ(0, out_data_[i]);
973 }
974 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
975 std::ostringstream ss;
976 ss << "i = " << i;
977 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
978 EXPECT_EQ(1, out_data_[i]);
979 }
980}
981
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000982TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000983 NetEqOutputType type;
984 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
985 // to GetAudio.
986 for (int i = 0; i < kMaxBlockSize; ++i) {
987 out_data_[i] = 1;
988 }
989 int num_channels;
990 int samples_per_channel;
991 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
992 &samples_per_channel,
993 &num_channels, &type));
994 // Verify that the first block of samples is set to 0.
995 static const int kExpectedOutputLength =
996 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
997 for (int i = 0; i < kExpectedOutputLength; ++i) {
998 std::ostringstream ss;
999 ss << "i = " << i;
1000 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
1001 EXPECT_EQ(0, out_data_[i]);
1002 }
1003}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001004
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001005TEST_F(NetEqDecodingTest, BackgroundNoise) {
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001006 neteq_->SetBackgroundNoiseMode(kBgnOn);
1007 CheckBgnOff(8000, kBgnOn);
1008 CheckBgnOff(16000, kBgnOn);
1009 CheckBgnOff(32000, kBgnOn);
1010 EXPECT_EQ(kBgnOn, neteq_->BackgroundNoiseMode());
1011
1012 neteq_->SetBackgroundNoiseMode(kBgnOff);
1013 CheckBgnOff(8000, kBgnOff);
1014 CheckBgnOff(16000, kBgnOff);
1015 CheckBgnOff(32000, kBgnOff);
1016 EXPECT_EQ(kBgnOff, neteq_->BackgroundNoiseMode());
1017
1018 neteq_->SetBackgroundNoiseMode(kBgnFade);
1019 CheckBgnOff(8000, kBgnFade);
1020 CheckBgnOff(16000, kBgnFade);
1021 CheckBgnOff(32000, kBgnFade);
1022 EXPECT_EQ(kBgnFade, neteq_->BackgroundNoiseMode());
1023}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001024
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001025TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001026 WebRtcRTPHeader rtp_info;
1027 uint32_t receive_timestamp = 0;
1028 // For the readability use the following payloads instead of the defaults of
1029 // this test.
1030 uint8_t kPcm16WbPayloadType = 1;
1031 uint8_t kCngNbPayloadType = 2;
1032 uint8_t kCngWbPayloadType = 3;
1033 uint8_t kCngSwb32PayloadType = 4;
1034 uint8_t kCngSwb48PayloadType = 5;
1035 uint8_t kAvtPayloadType = 6;
1036 uint8_t kRedPayloadType = 7;
1037 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1038
1039 // Register decoders.
1040 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1041 kPcm16WbPayloadType));
1042 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1043 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1044 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1045 kCngSwb32PayloadType));
1046 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1047 kCngSwb48PayloadType));
1048 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1049 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1050 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1051
1052 PopulateRtpInfo(0, 0, &rtp_info);
1053 rtp_info.header.payloadType = kPcm16WbPayloadType;
1054
1055 // The first packet injected cannot be sync-packet.
1056 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1057
1058 // Payload length of 10 ms PCM16 16 kHz.
1059 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1060 uint8_t payload[kPayloadBytes] = {0};
1061 ASSERT_EQ(0, neteq_->InsertPacket(
1062 rtp_info, payload, kPayloadBytes, receive_timestamp));
1063
1064 // Next packet. Last packet contained 10 ms audio.
1065 rtp_info.header.sequenceNumber++;
1066 rtp_info.header.timestamp += kBlockSize16kHz;
1067 receive_timestamp += kBlockSize16kHz;
1068
1069 // Unacceptable payload types CNG, AVT (DTMF), RED.
1070 rtp_info.header.payloadType = kCngNbPayloadType;
1071 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1072
1073 rtp_info.header.payloadType = kCngWbPayloadType;
1074 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1075
1076 rtp_info.header.payloadType = kCngSwb32PayloadType;
1077 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1078
1079 rtp_info.header.payloadType = kCngSwb48PayloadType;
1080 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081
1082 rtp_info.header.payloadType = kAvtPayloadType;
1083 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1084
1085 rtp_info.header.payloadType = kRedPayloadType;
1086 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1087
1088 // Change of codec cannot be initiated with a sync packet.
1089 rtp_info.header.payloadType = kIsacPayloadType;
1090 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1091
1092 // Change of SSRC is not allowed with a sync packet.
1093 rtp_info.header.payloadType = kPcm16WbPayloadType;
1094 ++rtp_info.header.ssrc;
1095 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1096
1097 --rtp_info.header.ssrc;
1098 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1099}
1100
1101// First insert several noise like packets, then sync-packets. Decoding all
1102// packets should not produce error, statistics should not show any packet loss
1103// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001104// TODO(turajs) we will have a better test if we have a referece NetEq, and
1105// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1106// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001107TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001108 WebRtcRTPHeader rtp_info;
1109 PopulateRtpInfo(0, 0, &rtp_info);
1110 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1111 uint8_t payload[kPayloadBytes];
1112 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001113 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001114 for (int n = 0; n < kPayloadBytes; ++n) {
1115 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1116 }
1117 // Insert some packets which decode to noise. We are not interested in
1118 // actual decoded values.
1119 NetEqOutputType output_type;
1120 int num_channels;
1121 int samples_per_channel;
1122 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001123 for (int n = 0; n < 100; ++n) {
1124 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1125 receive_timestamp));
1126 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1127 &samples_per_channel, &num_channels,
1128 &output_type));
1129 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1130 ASSERT_EQ(1, num_channels);
1131
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001132 rtp_info.header.sequenceNumber++;
1133 rtp_info.header.timestamp += kBlockSize16kHz;
1134 receive_timestamp += kBlockSize16kHz;
1135 }
1136 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001137
1138 // Make sure sufficient number of sync packets are inserted that we can
1139 // conduct a test.
1140 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001141 // Insert sync-packets, the decoded sequence should be all-zero.
1142 for (int n = 0; n < kNumSyncPackets; ++n) {
1143 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1144 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1145 &samples_per_channel, &num_channels,
1146 &output_type));
1147 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1148 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001149 if (n > algorithmic_frame_delay) {
1150 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1151 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001152 rtp_info.header.sequenceNumber++;
1153 rtp_info.header.timestamp += kBlockSize16kHz;
1154 receive_timestamp += kBlockSize16kHz;
1155 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001156
1157 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001158 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001159 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1160 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1161 receive_timestamp));
1162 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1163 &samples_per_channel, &num_channels,
1164 &output_type));
1165 if (n >= algorithmic_frame_delay + 1) {
1166 // Expect that this frame contain samples from regular RTP.
1167 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1168 }
1169 rtp_info.header.sequenceNumber++;
1170 rtp_info.header.timestamp += kBlockSize16kHz;
1171 receive_timestamp += kBlockSize16kHz;
1172 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001173 NetEqNetworkStatistics network_stats;
1174 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1175 // Expecting a "clean" network.
1176 EXPECT_EQ(0, network_stats.packet_loss_rate);
1177 EXPECT_EQ(0, network_stats.expand_rate);
1178 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001179 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001180}
1181
1182// Test if the size of the packet buffer reported correctly when containing
1183// sync packets. Also, test if network packets override sync packets. That is to
1184// prefer decoding a network packet to a sync packet, if both have same sequence
1185// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001186TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001187 WebRtcRTPHeader rtp_info;
1188 PopulateRtpInfo(0, 0, &rtp_info);
1189 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1190 uint8_t payload[kPayloadBytes];
1191 int16_t decoded[kBlockSize16kHz];
1192 for (int n = 0; n < kPayloadBytes; ++n) {
1193 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1194 }
1195 // Insert some packets which decode to noise. We are not interested in
1196 // actual decoded values.
1197 NetEqOutputType output_type;
1198 int num_channels;
1199 int samples_per_channel;
1200 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001201 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1202 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001203 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1204 receive_timestamp));
1205 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1206 &samples_per_channel, &num_channels,
1207 &output_type));
1208 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1209 ASSERT_EQ(1, num_channels);
1210 rtp_info.header.sequenceNumber++;
1211 rtp_info.header.timestamp += kBlockSize16kHz;
1212 receive_timestamp += kBlockSize16kHz;
1213 }
1214 const int kNumSyncPackets = 10;
1215
1216 WebRtcRTPHeader first_sync_packet_rtp_info;
1217 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1218
1219 // Insert sync-packets, but no decoding.
1220 for (int n = 0; n < kNumSyncPackets; ++n) {
1221 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1222 rtp_info.header.sequenceNumber++;
1223 rtp_info.header.timestamp += kBlockSize16kHz;
1224 receive_timestamp += kBlockSize16kHz;
1225 }
1226 NetEqNetworkStatistics network_stats;
1227 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001228 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1229 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001230
1231 // Rewind |rtp_info| to that of the first sync packet.
1232 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1233
1234 // Insert.
1235 for (int n = 0; n < kNumSyncPackets; ++n) {
1236 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1237 receive_timestamp));
1238 rtp_info.header.sequenceNumber++;
1239 rtp_info.header.timestamp += kBlockSize16kHz;
1240 receive_timestamp += kBlockSize16kHz;
1241 }
1242
1243 // Decode.
1244 for (int n = 0; n < kNumSyncPackets; ++n) {
1245 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1246 &samples_per_channel, &num_channels,
1247 &output_type));
1248 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1249 ASSERT_EQ(1, num_channels);
1250 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1251 }
1252}
1253
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001254void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1255 uint32_t start_timestamp,
1256 const std::set<uint16_t>& drop_seq_numbers,
1257 bool expect_seq_no_wrap,
1258 bool expect_timestamp_wrap) {
1259 uint16_t seq_no = start_seq_no;
1260 uint32_t timestamp = start_timestamp;
1261 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1262 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1263 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1264 const int kPayloadBytes = kSamples * sizeof(int16_t);
1265 double next_input_time_ms = 0.0;
1266 int16_t decoded[kBlockSize16kHz];
1267 int num_channels;
1268 int samples_per_channel;
1269 NetEqOutputType output_type;
1270 uint32_t receive_timestamp = 0;
1271
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001272 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001273 const int kSpeechDurationMs = 2000;
1274 int packets_inserted = 0;
1275 uint16_t last_seq_no;
1276 uint32_t last_timestamp;
1277 bool timestamp_wrapped = false;
1278 bool seq_no_wrapped = false;
1279 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1280 // Each turn in this for loop is 10 ms.
1281 while (next_input_time_ms <= t_ms) {
1282 // Insert one 30 ms speech frame.
1283 uint8_t payload[kPayloadBytes] = {0};
1284 WebRtcRTPHeader rtp_info;
1285 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1286 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1287 // This sequence number was not in the set to drop. Insert it.
1288 ASSERT_EQ(0,
1289 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1290 receive_timestamp));
1291 ++packets_inserted;
1292 }
1293 NetEqNetworkStatistics network_stats;
1294 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1295
1296 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1297 // packet size for first few packets. Therefore we refrain from checking
1298 // the criteria.
1299 if (packets_inserted > 4) {
1300 // Expect preferred and actual buffer size to be no more than 2 frames.
1301 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001302 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1303 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001304 }
1305 last_seq_no = seq_no;
1306 last_timestamp = timestamp;
1307
1308 ++seq_no;
1309 timestamp += kSamples;
1310 receive_timestamp += kSamples;
1311 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1312
1313 seq_no_wrapped |= seq_no < last_seq_no;
1314 timestamp_wrapped |= timestamp < last_timestamp;
1315 }
1316 // Pull out data once.
1317 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1318 &samples_per_channel, &num_channels,
1319 &output_type));
1320 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1321 ASSERT_EQ(1, num_channels);
1322
1323 // Expect delay (in samples) to be less than 2 packets.
1324 EXPECT_LE(timestamp - neteq_->PlayoutTimestamp(),
1325 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001326 }
1327 // Make sure we have actually tested wrap-around.
1328 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1329 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1330}
1331
1332TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1333 // Start with a sequence number that will soon wrap.
1334 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1335 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1336}
1337
1338TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1339 // Start with a sequence number that will soon wrap.
1340 std::set<uint16_t> drop_seq_numbers;
1341 drop_seq_numbers.insert(0xFFFF);
1342 drop_seq_numbers.insert(0x0);
1343 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1344}
1345
1346TEST_F(NetEqDecodingTest, TimestampWrap) {
1347 // Start with a timestamp that will soon wrap.
1348 std::set<uint16_t> drop_seq_numbers;
1349 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1350}
1351
1352TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1353 // Start with a timestamp and a sequence number that will wrap at the same
1354 // time.
1355 std::set<uint16_t> drop_seq_numbers;
1356 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1357}
1358
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001359void NetEqDecodingTest::DuplicateCng() {
1360 uint16_t seq_no = 0;
1361 uint32_t timestamp = 0;
1362 const int kFrameSizeMs = 10;
1363 const int kSampleRateKhz = 16;
1364 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1365 const int kPayloadBytes = kSamples * 2;
1366
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001367 const int algorithmic_delay_samples = std::max(
1368 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001369 // Insert three speech packet. Three are needed to get the frame length
1370 // correct.
1371 int out_len;
1372 int num_channels;
1373 NetEqOutputType type;
1374 uint8_t payload[kPayloadBytes] = {0};
1375 WebRtcRTPHeader rtp_info;
1376 for (int i = 0; i < 3; ++i) {
1377 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1378 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1379 ++seq_no;
1380 timestamp += kSamples;
1381
1382 // Pull audio once.
1383 ASSERT_EQ(0,
1384 neteq_->GetAudio(
1385 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1386 ASSERT_EQ(kBlockSize16kHz, out_len);
1387 }
1388 // Verify speech output.
1389 EXPECT_EQ(kOutputNormal, type);
1390
1391 // Insert same CNG packet twice.
1392 const int kCngPeriodMs = 100;
1393 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1394 int payload_len;
1395 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1396 // This is the first time this CNG packet is inserted.
1397 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1398
1399 // Pull audio once and make sure CNG is played.
1400 ASSERT_EQ(0,
1401 neteq_->GetAudio(
1402 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1403 ASSERT_EQ(kBlockSize16kHz, out_len);
1404 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001405 EXPECT_EQ(timestamp - algorithmic_delay_samples, neteq_->PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001406
1407 // Insert the same CNG packet again. Note that at this point it is old, since
1408 // we have already decoded the first copy of it.
1409 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1410
1411 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1412 // we have already pulled out CNG once.
1413 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1414 ASSERT_EQ(0,
1415 neteq_->GetAudio(
1416 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1417 ASSERT_EQ(kBlockSize16kHz, out_len);
1418 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001419 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1420 neteq_->PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001421 }
1422
1423 // Insert speech again.
1424 ++seq_no;
1425 timestamp += kCngPeriodSamples;
1426 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1427 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1428
1429 // Pull audio once and verify that the output is speech again.
1430 ASSERT_EQ(0,
1431 neteq_->GetAudio(
1432 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1433 ASSERT_EQ(kBlockSize16kHz, out_len);
1434 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001435 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
1436 neteq_->PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001437}
1438
1439TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001440} // namespace webrtc