blob: 47e9e855e2a49a8ccd24e556e2c3b39f411872d3 [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
17#include <stdlib.h>
18#include <string.h> // memset
19
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000020#include <cmath>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000021#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022#include <string>
23#include <vector>
24
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000025#include "gflags/gflags.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026#include "gtest/gtest.h"
27#include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_RTPpacket.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000028#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000030#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031#include "webrtc/typedefs.h"
32
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000033DEFINE_bool(gen_ref, false, "Generate reference files.");
34
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035namespace webrtc {
36
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000037static bool IsAllZero(const int16_t* buf, int buf_length) {
38 bool all_zero = true;
39 for (int n = 0; n < buf_length && all_zero; ++n)
40 all_zero = buf[n] == 0;
41 return all_zero;
42}
43
44static bool IsAllNonZero(const int16_t* buf, int buf_length) {
45 bool all_non_zero = true;
46 for (int n = 0; n < buf_length && all_non_zero; ++n)
47 all_non_zero = buf[n] != 0;
48 return all_non_zero;
49}
50
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051class RefFiles {
52 public:
53 RefFiles(const std::string& input_file, const std::string& output_file);
54 ~RefFiles();
55 template<class T> void ProcessReference(const T& test_results);
56 template<typename T, size_t n> void ProcessReference(
57 const T (&test_results)[n],
58 size_t length);
59 template<typename T, size_t n> void WriteToFile(
60 const T (&test_results)[n],
61 size_t length);
62 template<typename T, size_t n> void ReadFromFileAndCompare(
63 const T (&test_results)[n],
64 size_t length);
65 void WriteToFile(const NetEqNetworkStatistics& stats);
66 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
67 void WriteToFile(const RtcpStatistics& stats);
68 void ReadFromFileAndCompare(const RtcpStatistics& stats);
69
70 FILE* input_fp_;
71 FILE* output_fp_;
72};
73
74RefFiles::RefFiles(const std::string &input_file,
75 const std::string &output_file)
76 : input_fp_(NULL),
77 output_fp_(NULL) {
78 if (!input_file.empty()) {
79 input_fp_ = fopen(input_file.c_str(), "rb");
80 EXPECT_TRUE(input_fp_ != NULL);
81 }
82 if (!output_file.empty()) {
83 output_fp_ = fopen(output_file.c_str(), "wb");
84 EXPECT_TRUE(output_fp_ != NULL);
85 }
86}
87
88RefFiles::~RefFiles() {
89 if (input_fp_) {
90 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
91 fclose(input_fp_);
92 }
93 if (output_fp_) fclose(output_fp_);
94}
95
96template<class T>
97void RefFiles::ProcessReference(const T& test_results) {
98 WriteToFile(test_results);
99 ReadFromFileAndCompare(test_results);
100}
101
102template<typename T, size_t n>
103void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
104 WriteToFile(test_results, length);
105 ReadFromFileAndCompare(test_results, length);
106}
107
108template<typename T, size_t n>
109void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
110 if (output_fp_) {
111 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
112 }
113}
114
115template<typename T, size_t n>
116void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
117 size_t length) {
118 if (input_fp_) {
119 // Read from ref file.
120 T* ref = new T[length];
121 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
122 // Compare
123 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
124 delete [] ref;
125 }
126}
127
128void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
129 if (output_fp_) {
130 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
131 output_fp_));
132 }
133}
134
135void RefFiles::ReadFromFileAndCompare(
136 const NetEqNetworkStatistics& stats) {
137 if (input_fp_) {
138 // Read from ref file.
139 size_t stat_size = sizeof(NetEqNetworkStatistics);
140 NetEqNetworkStatistics ref_stats;
141 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
142 // Compare
143 EXPECT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
144 }
145}
146
147void RefFiles::WriteToFile(const RtcpStatistics& stats) {
148 if (output_fp_) {
149 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
150 output_fp_));
151 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
152 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000153 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
154 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 output_fp_));
156 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
157 output_fp_));
158 }
159}
160
161void RefFiles::ReadFromFileAndCompare(
162 const RtcpStatistics& stats) {
163 if (input_fp_) {
164 // Read from ref file.
165 RtcpStatistics ref_stats;
166 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
167 sizeof(ref_stats.fraction_lost), 1, input_fp_));
168 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
169 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000170 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
171 sizeof(ref_stats.extended_max_sequence_number), 1,
172 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
174 input_fp_));
175 // Compare
176 EXPECT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
177 EXPECT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000178 EXPECT_EQ(ref_stats.extended_max_sequence_number,
179 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 EXPECT_EQ(ref_stats.jitter, stats.jitter);
181 }
182}
183
184class NetEqDecodingTest : public ::testing::Test {
185 protected:
186 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
187 // constants below can be changed.
188 static const int kTimeStepMs = 10;
189 static const int kBlockSize8kHz = kTimeStepMs * 8;
190 static const int kBlockSize16kHz = kTimeStepMs * 16;
191 static const int kBlockSize32kHz = kTimeStepMs * 32;
192 static const int kMaxBlockSize = kBlockSize32kHz;
193 static const int kInitSampleRateHz = 8000;
194
195 NetEqDecodingTest();
196 virtual void SetUp();
197 virtual void TearDown();
198 void SelectDecoders(NetEqDecoder* used_codec);
199 void LoadDecoders();
200 void OpenInputFile(const std::string &rtp_file);
201 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
202 void DecodeAndCompare(const std::string &rtp_file,
203 const std::string &ref_file);
204 void DecodeAndCheckStats(const std::string &rtp_file,
205 const std::string &stat_ref_file,
206 const std::string &rtcp_ref_file);
207 static void PopulateRtpInfo(int frame_index,
208 int timestamp,
209 WebRtcRTPHeader* rtp_info);
210 static void PopulateCng(int frame_index,
211 int timestamp,
212 WebRtcRTPHeader* rtp_info,
213 uint8_t* payload,
214 int* payload_len);
215
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000216 void CheckBgnOff(int sampling_rate, NetEqBackgroundNoiseMode bgn_mode);
217
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000218 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
219 const std::set<uint16_t>& drop_seq_numbers,
220 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
221
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000222 void LongCngWithClockDrift(double drift_factor);
223
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224 NetEq* neteq_;
225 FILE* rtp_fp_;
226 unsigned int sim_clock_;
227 int16_t out_data_[kMaxBlockSize];
228 int output_sample_rate_;
229};
230
231// Allocating the static const so that it can be passed by reference.
232const int NetEqDecodingTest::kTimeStepMs;
233const int NetEqDecodingTest::kBlockSize8kHz;
234const int NetEqDecodingTest::kBlockSize16kHz;
235const int NetEqDecodingTest::kBlockSize32kHz;
236const int NetEqDecodingTest::kMaxBlockSize;
237const int NetEqDecodingTest::kInitSampleRateHz;
238
239NetEqDecodingTest::NetEqDecodingTest()
240 : neteq_(NULL),
241 rtp_fp_(NULL),
242 sim_clock_(0),
243 output_sample_rate_(kInitSampleRateHz) {
244 memset(out_data_, 0, sizeof(out_data_));
245}
246
247void NetEqDecodingTest::SetUp() {
248 neteq_ = NetEq::Create(kInitSampleRateHz);
249 ASSERT_TRUE(neteq_);
250 LoadDecoders();
251}
252
253void NetEqDecodingTest::TearDown() {
254 delete neteq_;
255 if (rtp_fp_)
256 fclose(rtp_fp_);
257}
258
259void NetEqDecodingTest::LoadDecoders() {
260 // Load PCMu.
261 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
262 // Load PCMa.
263 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000264#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 // Load iLBC.
266 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000267#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 // Load iSAC.
269 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000270#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271 // Load iSAC SWB.
272 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000273 // Load iSAC FB.
274 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000275#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000276 // Load PCM16B nb.
277 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
278 // Load PCM16B wb.
279 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
280 // Load PCM16B swb32.
281 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
282 // Load CNG 8 kHz.
283 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
284 // Load CNG 16 kHz.
285 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
286}
287
288void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
289 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
290 ASSERT_TRUE(rtp_fp_ != NULL);
291 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
292}
293
294void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
295 // Check if time to receive.
296 while ((sim_clock_ >= rtp->time()) &&
297 (rtp->dataLen() >= 0)) {
298 if (rtp->dataLen() > 0) {
299 WebRtcRTPHeader rtpInfo;
300 rtp->parseHeader(&rtpInfo);
301 ASSERT_EQ(0, neteq_->InsertPacket(
302 rtpInfo,
303 rtp->payload(),
304 rtp->payloadLen(),
305 rtp->time() * (output_sample_rate_ / 1000)));
306 }
307 // Get next packet.
308 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
309 }
310
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000311 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 NetEqOutputType type;
313 int num_channels;
314 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
315 &num_channels, &type));
316 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
317 (*out_len == kBlockSize16kHz) ||
318 (*out_len == kBlockSize32kHz));
319 output_sample_rate_ = *out_len / 10 * 1000;
320
321 // Increase time.
322 sim_clock_ += kTimeStepMs;
323}
324
325void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
326 const std::string &ref_file) {
327 OpenInputFile(rtp_file);
328
329 std::string ref_out_file = "";
330 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000331 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 }
333 RefFiles ref_files(ref_file, ref_out_file);
334
335 NETEQTEST_RTPpacket rtp;
336 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
337 int i = 0;
338 while (rtp.dataLen() >= 0) {
339 std::ostringstream ss;
340 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
341 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000342 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
344 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
345 }
346}
347
348void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
349 const std::string &stat_ref_file,
350 const std::string &rtcp_ref_file) {
351 OpenInputFile(rtp_file);
352 std::string stat_out_file = "";
353 if (stat_ref_file.empty()) {
354 stat_out_file = webrtc::test::OutputPath() +
355 "neteq_network_stats.dat";
356 }
357 RefFiles network_stat_files(stat_ref_file, stat_out_file);
358
359 std::string rtcp_out_file = "";
360 if (rtcp_ref_file.empty()) {
361 rtcp_out_file = webrtc::test::OutputPath() +
362 "neteq_rtcp_stats.dat";
363 }
364 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
365
366 NETEQTEST_RTPpacket rtp;
367 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
368 while (rtp.dataLen() >= 0) {
369 int out_len;
370 Process(&rtp, &out_len);
371
372 // Query the network statistics API once per second
373 if (sim_clock_ % 1000 == 0) {
374 // Process NetworkStatistics.
375 NetEqNetworkStatistics network_stats;
376 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
377 network_stat_files.ProcessReference(network_stats);
378
379 // Process RTCPstat.
380 RtcpStatistics rtcp_stats;
381 neteq_->GetRtcpStatistics(&rtcp_stats);
382 rtcp_stat_files.ProcessReference(rtcp_stats);
383 }
384 }
385}
386
387void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
388 int timestamp,
389 WebRtcRTPHeader* rtp_info) {
390 rtp_info->header.sequenceNumber = frame_index;
391 rtp_info->header.timestamp = timestamp;
392 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
393 rtp_info->header.payloadType = 94; // PCM16b WB codec.
394 rtp_info->header.markerBit = 0;
395}
396
397void NetEqDecodingTest::PopulateCng(int frame_index,
398 int timestamp,
399 WebRtcRTPHeader* rtp_info,
400 uint8_t* payload,
401 int* payload_len) {
402 rtp_info->header.sequenceNumber = frame_index;
403 rtp_info->header.timestamp = timestamp;
404 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
405 rtp_info->header.payloadType = 98; // WB CNG.
406 rtp_info->header.markerBit = 0;
407 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
408 *payload_len = 1; // Only noise level, no spectral parameters.
409}
410
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000411void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
412 NetEqBackgroundNoiseMode bgn_mode) {
413 int expected_samples_per_channel = 0;
414 uint8_t payload_type = 0xFF; // Invalid.
415 if (sampling_rate_hz == 8000) {
416 expected_samples_per_channel = kBlockSize8kHz;
417 payload_type = 93; // PCM 16, 8 kHz.
418 } else if (sampling_rate_hz == 16000) {
419 expected_samples_per_channel = kBlockSize16kHz;
420 payload_type = 94; // PCM 16, 16 kHZ.
421 } else if (sampling_rate_hz == 32000) {
422 expected_samples_per_channel = kBlockSize32kHz;
423 payload_type = 95; // PCM 16, 32 kHz.
424 } else {
425 ASSERT_TRUE(false); // Unsupported test case.
426 }
427
428 NetEqOutputType type;
429 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
430 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
431
432 // Payload of 10 ms of PCM16 32 kHz.
433 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
434
435 // Random payload.
436 for (int n = 0; n < expected_samples_per_channel; ++n) {
437 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
438 }
439 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
440 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
441 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
442
443 WebRtcRTPHeader rtp_info;
444 PopulateRtpInfo(0, 0, &rtp_info);
445 rtp_info.header.payloadType = payload_type;
446
447 int number_channels = 0;
448 int samples_per_channel = 0;
449
450 uint32_t receive_timestamp = 0;
451 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
452 number_channels = 0;
453 samples_per_channel = 0;
454 ASSERT_EQ(0, neteq_->InsertPacket(
455 rtp_info, payload, enc_len_bytes, receive_timestamp));
456 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
457 &number_channels, &type));
458 ASSERT_EQ(1, number_channels);
459 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
460 ASSERT_EQ(kOutputNormal, type);
461
462 // Next packet.
463 rtp_info.header.timestamp += expected_samples_per_channel;
464 rtp_info.header.sequenceNumber++;
465 receive_timestamp += expected_samples_per_channel;
466 }
467
468 number_channels = 0;
469 samples_per_channel = 0;
470
471 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
472 // frame without checking speech-type. This is the first frame pulled without
473 // inserting any packet, and might not be labeled as PCL.
474 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
475 &number_channels, &type));
476 ASSERT_EQ(1, number_channels);
477 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
478
479 // To be able to test the fading of background noise we need at lease to pull
480 // 610 frames.
481 const int kFadingThreshold = 610;
482
483 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
484 // arbitrary, but sufficiently large to test enough number of frames.
485 const int kNumPlcToCngTestFrames = 20;
486 bool plc_to_cng = false;
487 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
488 number_channels = 0;
489 samples_per_channel = 0;
490 memset(output, 1, sizeof(output)); // Set to non-zero.
491 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
492 &number_channels, &type));
493 ASSERT_EQ(1, number_channels);
494 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
495 if (type == kOutputPLCtoCNG) {
496 plc_to_cng = true;
497 double sum_squared = 0;
498 for (int k = 0; k < number_channels * samples_per_channel; ++k)
499 sum_squared += output[k] * output[k];
500 if (bgn_mode == kBgnOn) {
501 EXPECT_NE(0, sum_squared);
502 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
503 EXPECT_EQ(0, sum_squared);
504 }
505 } else {
506 EXPECT_EQ(kOutputPLC, type);
507 }
508 }
509 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
510}
511
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000512#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
513// Disabled for Windows 64-bit until webrtc:1458 is fixed.
514#define MAYBE_TestBitExactness DISABLED_TestBitExactness
515#else
516#define MAYBE_TestBitExactness TestBitExactness
517#endif
518
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000519TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000520 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000521 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000522#if defined(_MSC_VER) && (_MSC_VER >= 1700)
523 // For Visual Studio 2012 and later, we will have to use the generic reference
524 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000525 const std::string input_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000526 "resources/audio_coding/neteq4_universal_ref.pcm";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000527#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000528 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000529 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000530#endif
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000531
532 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000533 DecodeAndCompare(input_rtp_file, "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000534 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000535 DecodeAndCompare(input_rtp_file, input_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000536 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000537}
538
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000539TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000540 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000541 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000542#if defined(_MSC_VER) && (_MSC_VER >= 1700)
543 // For Visual Studio 2012 and later, we will have to use the generic reference
544 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000545 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000546 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000547#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000548 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000549 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000550#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000551 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000552 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
553 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000554 DecodeAndCheckStats(input_rtp_file, "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000555 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000556 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
557 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000558 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559}
560
561// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000562TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000563 // Use fax mode to avoid time-scaling. This is to simplify the testing of
564 // packet waiting times in the packet buffer.
565 neteq_->SetPlayoutMode(kPlayoutFax);
566 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
567 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
568 size_t num_frames = 30;
569 const int kSamples = 10 * 16;
570 const int kPayloadBytes = kSamples * 2;
571 for (size_t i = 0; i < num_frames; ++i) {
572 uint16_t payload[kSamples] = {0};
573 WebRtcRTPHeader rtp_info;
574 rtp_info.header.sequenceNumber = i;
575 rtp_info.header.timestamp = i * kSamples;
576 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
577 rtp_info.header.payloadType = 94; // PCM16b WB codec.
578 rtp_info.header.markerBit = 0;
579 ASSERT_EQ(0, neteq_->InsertPacket(
580 rtp_info,
581 reinterpret_cast<uint8_t*>(payload),
582 kPayloadBytes, 0));
583 }
584 // Pull out all data.
585 for (size_t i = 0; i < num_frames; ++i) {
586 int out_len;
587 int num_channels;
588 NetEqOutputType type;
589 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
590 &num_channels, &type));
591 ASSERT_EQ(kBlockSize16kHz, out_len);
592 }
593
594 std::vector<int> waiting_times;
595 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000596 EXPECT_EQ(num_frames, waiting_times.size());
597 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
598 // spacing (per definition), we expect the delay to increase with 10 ms for
599 // each packet.
600 for (size_t i = 0; i < waiting_times.size(); ++i) {
601 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
602 }
603
604 // Check statistics again and make sure it's been reset.
605 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000606 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 EXPECT_EQ(0, len);
608
609 // Process > 100 frames, and make sure that that we get statistics
610 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
611 num_frames = 110;
612 for (size_t i = 0; i < num_frames; ++i) {
613 uint16_t payload[kSamples] = {0};
614 WebRtcRTPHeader rtp_info;
615 rtp_info.header.sequenceNumber = i;
616 rtp_info.header.timestamp = i * kSamples;
617 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
618 rtp_info.header.payloadType = 94; // PCM16b WB codec.
619 rtp_info.header.markerBit = 0;
620 ASSERT_EQ(0, neteq_->InsertPacket(
621 rtp_info,
622 reinterpret_cast<uint8_t*>(payload),
623 kPayloadBytes, 0));
624 int out_len;
625 int num_channels;
626 NetEqOutputType type;
627 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
628 &num_channels, &type));
629 ASSERT_EQ(kBlockSize16kHz, out_len);
630 }
631
632 neteq_->WaitingTimes(&waiting_times);
633 EXPECT_EQ(100u, waiting_times.size());
634}
635
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000636TEST_F(NetEqDecodingTest,
637 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 const int kNumFrames = 3000; // Needed for convergence.
639 int frame_index = 0;
640 const int kSamples = 10 * 16;
641 const int kPayloadBytes = kSamples * 2;
642 while (frame_index < kNumFrames) {
643 // Insert one packet each time, except every 10th time where we insert two
644 // packets at once. This will create a negative clock-drift of approx. 10%.
645 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
646 for (int n = 0; n < num_packets; ++n) {
647 uint8_t payload[kPayloadBytes] = {0};
648 WebRtcRTPHeader rtp_info;
649 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
650 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
651 ++frame_index;
652 }
653
654 // Pull out data once.
655 int out_len;
656 int num_channels;
657 NetEqOutputType type;
658 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
659 &num_channels, &type));
660 ASSERT_EQ(kBlockSize16kHz, out_len);
661 }
662
663 NetEqNetworkStatistics network_stats;
664 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
665 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
666}
667
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000668TEST_F(NetEqDecodingTest,
669 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000670 const int kNumFrames = 5000; // Needed for convergence.
671 int frame_index = 0;
672 const int kSamples = 10 * 16;
673 const int kPayloadBytes = kSamples * 2;
674 for (int i = 0; i < kNumFrames; ++i) {
675 // Insert one packet each time, except every 10th time where we don't insert
676 // any packet. This will create a positive clock-drift of approx. 11%.
677 int num_packets = (i % 10 == 9 ? 0 : 1);
678 for (int n = 0; n < num_packets; ++n) {
679 uint8_t payload[kPayloadBytes] = {0};
680 WebRtcRTPHeader rtp_info;
681 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
682 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
683 ++frame_index;
684 }
685
686 // Pull out data once.
687 int out_len;
688 int num_channels;
689 NetEqOutputType type;
690 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
691 &num_channels, &type));
692 ASSERT_EQ(kBlockSize16kHz, out_len);
693 }
694
695 NetEqNetworkStatistics network_stats;
696 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
697 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
698}
699
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000700void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000701 uint16_t seq_no = 0;
702 uint32_t timestamp = 0;
703 const int kFrameSizeMs = 30;
704 const int kSamples = kFrameSizeMs * 16;
705 const int kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000706 double next_input_time_ms = 0.0;
707 double t_ms;
708 NetEqOutputType type;
709
710 // Insert speech for 5 seconds.
711 const int kSpeechDurationMs = 5000;
712 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
713 // Each turn in this for loop is 10 ms.
714 while (next_input_time_ms <= t_ms) {
715 // Insert one 30 ms speech frame.
716 uint8_t payload[kPayloadBytes] = {0};
717 WebRtcRTPHeader rtp_info;
718 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
719 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
720 ++seq_no;
721 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000722 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000723 }
724 // Pull out data once.
725 int out_len;
726 int num_channels;
727 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
728 &num_channels, &type));
729 ASSERT_EQ(kBlockSize16kHz, out_len);
730 }
731
732 EXPECT_EQ(kOutputNormal, type);
733 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
734
735 // Insert CNG for 1 minute (= 60000 ms).
736 const int kCngPeriodMs = 100;
737 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
738 const int kCngDurationMs = 60000;
739 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
740 // Each turn in this for loop is 10 ms.
741 while (next_input_time_ms <= t_ms) {
742 // Insert one CNG frame each 100 ms.
743 uint8_t payload[kPayloadBytes];
744 int payload_len;
745 WebRtcRTPHeader rtp_info;
746 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
747 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
748 ++seq_no;
749 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000750 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000751 }
752 // Pull out data once.
753 int out_len;
754 int num_channels;
755 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
756 &num_channels, &type));
757 ASSERT_EQ(kBlockSize16kHz, out_len);
758 }
759
760 EXPECT_EQ(kOutputCNG, type);
761
762 // Insert speech again until output type is speech.
763 while (type != kOutputNormal) {
764 // Each turn in this for loop is 10 ms.
765 while (next_input_time_ms <= t_ms) {
766 // Insert one 30 ms speech frame.
767 uint8_t payload[kPayloadBytes] = {0};
768 WebRtcRTPHeader rtp_info;
769 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
770 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
771 ++seq_no;
772 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000773 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000774 }
775 // Pull out data once.
776 int out_len;
777 int num_channels;
778 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
779 &num_channels, &type));
780 ASSERT_EQ(kBlockSize16kHz, out_len);
781 // Increase clock.
782 t_ms += 10;
783 }
784
785 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
786 // Compare delay before and after, and make sure it differs less than 20 ms.
787 EXPECT_LE(delay_after, delay_before + 20 * 16);
788 EXPECT_GE(delay_after, delay_before - 20 * 16);
789}
790
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000791TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithClockNegativeDrift)) {
792 // Apply a clock drift of -25 ms / s (sender faster than receiver).
793 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
794 LongCngWithClockDrift(kDriftFactor);
795}
796
797// TODO(hlundin): Re-enable this test and fix the issues to make it pass.
798TEST_F(NetEqDecodingTest,
799 DISABLED_ON_ANDROID(DISABLED_LongCngWithClockPositiveDrift)) {
800 // Apply a clock drift of +25 ms / s (sender slower than receiver).
801 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
802 LongCngWithClockDrift(kDriftFactor);
803}
804
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000805TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(UnknownPayloadType)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000806 const int kPayloadBytes = 100;
807 uint8_t payload[kPayloadBytes] = {0};
808 WebRtcRTPHeader rtp_info;
809 PopulateRtpInfo(0, 0, &rtp_info);
810 rtp_info.header.payloadType = 1; // Not registered as a decoder.
811 EXPECT_EQ(NetEq::kFail,
812 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
813 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
814}
815
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000816TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(OversizePacket)) {
817 // Payload size is greater than packet buffer size
818 const int kPayloadBytes = NetEq::kMaxBytesInBuffer + 1;
819 uint8_t payload[kPayloadBytes] = {0};
820 WebRtcRTPHeader rtp_info;
821 PopulateRtpInfo(0, 0, &rtp_info);
822 rtp_info.header.payloadType = 103; // iSAC, no packet splitting.
823 EXPECT_EQ(NetEq::kFail,
824 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
825 EXPECT_EQ(NetEq::kOversizePacket, neteq_->LastError());
826}
827
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000828TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000829 const int kPayloadBytes = 100;
830 uint8_t payload[kPayloadBytes] = {0};
831 WebRtcRTPHeader rtp_info;
832 PopulateRtpInfo(0, 0, &rtp_info);
833 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
834 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
835 NetEqOutputType type;
836 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
837 // to GetAudio.
838 for (int i = 0; i < kMaxBlockSize; ++i) {
839 out_data_[i] = 1;
840 }
841 int num_channels;
842 int samples_per_channel;
843 EXPECT_EQ(NetEq::kFail,
844 neteq_->GetAudio(kMaxBlockSize, out_data_,
845 &samples_per_channel, &num_channels, &type));
846 // Verify that there is a decoder error to check.
847 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
848 // Code 6730 is an iSAC error code.
849 EXPECT_EQ(6730, neteq_->LastDecoderError());
850 // Verify that the first 160 samples are set to 0, and that the remaining
851 // samples are left unmodified.
852 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
853 for (int i = 0; i < kExpectedOutputLength; ++i) {
854 std::ostringstream ss;
855 ss << "i = " << i;
856 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
857 EXPECT_EQ(0, out_data_[i]);
858 }
859 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
860 std::ostringstream ss;
861 ss << "i = " << i;
862 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
863 EXPECT_EQ(1, out_data_[i]);
864 }
865}
866
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000867TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(GetAudioBeforeInsertPacket)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000868 NetEqOutputType type;
869 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
870 // to GetAudio.
871 for (int i = 0; i < kMaxBlockSize; ++i) {
872 out_data_[i] = 1;
873 }
874 int num_channels;
875 int samples_per_channel;
876 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
877 &samples_per_channel,
878 &num_channels, &type));
879 // Verify that the first block of samples is set to 0.
880 static const int kExpectedOutputLength =
881 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
882 for (int i = 0; i < kExpectedOutputLength; ++i) {
883 std::ostringstream ss;
884 ss << "i = " << i;
885 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
886 EXPECT_EQ(0, out_data_[i]);
887 }
888}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000889
turaj@webrtc.org3fdeddb2013-09-25 22:19:22 +0000890TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(BackgroundNoise)) {
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000891 neteq_->SetBackgroundNoiseMode(kBgnOn);
892 CheckBgnOff(8000, kBgnOn);
893 CheckBgnOff(16000, kBgnOn);
894 CheckBgnOff(32000, kBgnOn);
895 EXPECT_EQ(kBgnOn, neteq_->BackgroundNoiseMode());
896
897 neteq_->SetBackgroundNoiseMode(kBgnOff);
898 CheckBgnOff(8000, kBgnOff);
899 CheckBgnOff(16000, kBgnOff);
900 CheckBgnOff(32000, kBgnOff);
901 EXPECT_EQ(kBgnOff, neteq_->BackgroundNoiseMode());
902
903 neteq_->SetBackgroundNoiseMode(kBgnFade);
904 CheckBgnOff(8000, kBgnFade);
905 CheckBgnOff(16000, kBgnFade);
906 CheckBgnOff(32000, kBgnFade);
907 EXPECT_EQ(kBgnFade, neteq_->BackgroundNoiseMode());
908}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000909
910TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketInsert)) {
911 WebRtcRTPHeader rtp_info;
912 uint32_t receive_timestamp = 0;
913 // For the readability use the following payloads instead of the defaults of
914 // this test.
915 uint8_t kPcm16WbPayloadType = 1;
916 uint8_t kCngNbPayloadType = 2;
917 uint8_t kCngWbPayloadType = 3;
918 uint8_t kCngSwb32PayloadType = 4;
919 uint8_t kCngSwb48PayloadType = 5;
920 uint8_t kAvtPayloadType = 6;
921 uint8_t kRedPayloadType = 7;
922 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
923
924 // Register decoders.
925 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
926 kPcm16WbPayloadType));
927 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
928 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
929 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
930 kCngSwb32PayloadType));
931 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
932 kCngSwb48PayloadType));
933 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
934 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
935 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
936
937 PopulateRtpInfo(0, 0, &rtp_info);
938 rtp_info.header.payloadType = kPcm16WbPayloadType;
939
940 // The first packet injected cannot be sync-packet.
941 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
942
943 // Payload length of 10 ms PCM16 16 kHz.
944 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
945 uint8_t payload[kPayloadBytes] = {0};
946 ASSERT_EQ(0, neteq_->InsertPacket(
947 rtp_info, payload, kPayloadBytes, receive_timestamp));
948
949 // Next packet. Last packet contained 10 ms audio.
950 rtp_info.header.sequenceNumber++;
951 rtp_info.header.timestamp += kBlockSize16kHz;
952 receive_timestamp += kBlockSize16kHz;
953
954 // Unacceptable payload types CNG, AVT (DTMF), RED.
955 rtp_info.header.payloadType = kCngNbPayloadType;
956 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
957
958 rtp_info.header.payloadType = kCngWbPayloadType;
959 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
960
961 rtp_info.header.payloadType = kCngSwb32PayloadType;
962 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
963
964 rtp_info.header.payloadType = kCngSwb48PayloadType;
965 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
966
967 rtp_info.header.payloadType = kAvtPayloadType;
968 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
969
970 rtp_info.header.payloadType = kRedPayloadType;
971 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
972
973 // Change of codec cannot be initiated with a sync packet.
974 rtp_info.header.payloadType = kIsacPayloadType;
975 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
976
977 // Change of SSRC is not allowed with a sync packet.
978 rtp_info.header.payloadType = kPcm16WbPayloadType;
979 ++rtp_info.header.ssrc;
980 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
981
982 --rtp_info.header.ssrc;
983 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
984}
985
986// First insert several noise like packets, then sync-packets. Decoding all
987// packets should not produce error, statistics should not show any packet loss
988// and sync-packets should decode to zero.
989TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketDecode)) {
990 WebRtcRTPHeader rtp_info;
991 PopulateRtpInfo(0, 0, &rtp_info);
992 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
993 uint8_t payload[kPayloadBytes];
994 int16_t decoded[kBlockSize16kHz];
995 for (int n = 0; n < kPayloadBytes; ++n) {
996 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
997 }
998 // Insert some packets which decode to noise. We are not interested in
999 // actual decoded values.
1000 NetEqOutputType output_type;
1001 int num_channels;
1002 int samples_per_channel;
1003 uint32_t receive_timestamp = 0;
1004 int delay_samples = 0;
1005 for (int n = 0; n < 100; ++n) {
1006 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1007 receive_timestamp));
1008 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1009 &samples_per_channel, &num_channels,
1010 &output_type));
1011 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1012 ASSERT_EQ(1, num_channels);
1013
1014 // Even if there is RTP packet in NetEq's buffer, the first frame pulled
1015 // from NetEq starts with few zero samples. Here we measure this delay.
1016 if (n == 0) {
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001017 while (decoded[delay_samples] == 0) delay_samples++;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001018 }
1019 rtp_info.header.sequenceNumber++;
1020 rtp_info.header.timestamp += kBlockSize16kHz;
1021 receive_timestamp += kBlockSize16kHz;
1022 }
1023 const int kNumSyncPackets = 10;
1024 // Insert sync-packets, the decoded sequence should be all-zero.
1025 for (int n = 0; n < kNumSyncPackets; ++n) {
1026 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1027 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1028 &samples_per_channel, &num_channels,
1029 &output_type));
1030 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1031 ASSERT_EQ(1, num_channels);
1032 EXPECT_TRUE(IsAllZero(&decoded[delay_samples],
1033 samples_per_channel * num_channels - delay_samples));
1034 delay_samples = 0; // Delay only matters in the first frame.
1035 rtp_info.header.sequenceNumber++;
1036 rtp_info.header.timestamp += kBlockSize16kHz;
1037 receive_timestamp += kBlockSize16kHz;
1038 }
1039 // We insert a regular packet, if sync packet are not correctly buffered then
1040 // network statistics would show some packet loss.
1041 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1042 receive_timestamp));
1043 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1044 &samples_per_channel, &num_channels,
1045 &output_type));
1046 // Make sure the last inserted packet is decoded and there are non-zero
1047 // samples.
1048 EXPECT_FALSE(IsAllZero(decoded, samples_per_channel * num_channels));
1049 NetEqNetworkStatistics network_stats;
1050 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1051 // Expecting a "clean" network.
1052 EXPECT_EQ(0, network_stats.packet_loss_rate);
1053 EXPECT_EQ(0, network_stats.expand_rate);
1054 EXPECT_EQ(0, network_stats.accelerate_rate);
1055 EXPECT_EQ(0, network_stats.preemptive_rate);
1056}
1057
1058// Test if the size of the packet buffer reported correctly when containing
1059// sync packets. Also, test if network packets override sync packets. That is to
1060// prefer decoding a network packet to a sync packet, if both have same sequence
1061// number and timestamp.
1062TEST_F(NetEqDecodingTest,
1063 DISABLED_ON_ANDROID(SyncPacketBufferSizeAndOverridenByNetworkPackets)) {
1064 WebRtcRTPHeader rtp_info;
1065 PopulateRtpInfo(0, 0, &rtp_info);
1066 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1067 uint8_t payload[kPayloadBytes];
1068 int16_t decoded[kBlockSize16kHz];
1069 for (int n = 0; n < kPayloadBytes; ++n) {
1070 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1071 }
1072 // Insert some packets which decode to noise. We are not interested in
1073 // actual decoded values.
1074 NetEqOutputType output_type;
1075 int num_channels;
1076 int samples_per_channel;
1077 uint32_t receive_timestamp = 0;
1078 for (int n = 0; n < 1; ++n) {
1079 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1080 receive_timestamp));
1081 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1082 &samples_per_channel, &num_channels,
1083 &output_type));
1084 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1085 ASSERT_EQ(1, num_channels);
1086 rtp_info.header.sequenceNumber++;
1087 rtp_info.header.timestamp += kBlockSize16kHz;
1088 receive_timestamp += kBlockSize16kHz;
1089 }
1090 const int kNumSyncPackets = 10;
1091
1092 WebRtcRTPHeader first_sync_packet_rtp_info;
1093 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1094
1095 // Insert sync-packets, but no decoding.
1096 for (int n = 0; n < kNumSyncPackets; ++n) {
1097 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1098 rtp_info.header.sequenceNumber++;
1099 rtp_info.header.timestamp += kBlockSize16kHz;
1100 receive_timestamp += kBlockSize16kHz;
1101 }
1102 NetEqNetworkStatistics network_stats;
1103 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1104 EXPECT_EQ(kNumSyncPackets * 10, network_stats.current_buffer_size_ms);
1105
1106 // Rewind |rtp_info| to that of the first sync packet.
1107 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1108
1109 // Insert.
1110 for (int n = 0; n < kNumSyncPackets; ++n) {
1111 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1112 receive_timestamp));
1113 rtp_info.header.sequenceNumber++;
1114 rtp_info.header.timestamp += kBlockSize16kHz;
1115 receive_timestamp += kBlockSize16kHz;
1116 }
1117
1118 // Decode.
1119 for (int n = 0; n < kNumSyncPackets; ++n) {
1120 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1121 &samples_per_channel, &num_channels,
1122 &output_type));
1123 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1124 ASSERT_EQ(1, num_channels);
1125 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1126 }
1127}
1128
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001129void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1130 uint32_t start_timestamp,
1131 const std::set<uint16_t>& drop_seq_numbers,
1132 bool expect_seq_no_wrap,
1133 bool expect_timestamp_wrap) {
1134 uint16_t seq_no = start_seq_no;
1135 uint32_t timestamp = start_timestamp;
1136 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1137 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1138 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1139 const int kPayloadBytes = kSamples * sizeof(int16_t);
1140 double next_input_time_ms = 0.0;
1141 int16_t decoded[kBlockSize16kHz];
1142 int num_channels;
1143 int samples_per_channel;
1144 NetEqOutputType output_type;
1145 uint32_t receive_timestamp = 0;
1146
1147 // Insert speech for 1 second.
1148 const int kSpeechDurationMs = 2000;
1149 int packets_inserted = 0;
1150 uint16_t last_seq_no;
1151 uint32_t last_timestamp;
1152 bool timestamp_wrapped = false;
1153 bool seq_no_wrapped = false;
1154 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1155 // Each turn in this for loop is 10 ms.
1156 while (next_input_time_ms <= t_ms) {
1157 // Insert one 30 ms speech frame.
1158 uint8_t payload[kPayloadBytes] = {0};
1159 WebRtcRTPHeader rtp_info;
1160 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1161 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1162 // This sequence number was not in the set to drop. Insert it.
1163 ASSERT_EQ(0,
1164 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1165 receive_timestamp));
1166 ++packets_inserted;
1167 }
1168 NetEqNetworkStatistics network_stats;
1169 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1170
1171 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1172 // packet size for first few packets. Therefore we refrain from checking
1173 // the criteria.
1174 if (packets_inserted > 4) {
1175 // Expect preferred and actual buffer size to be no more than 2 frames.
1176 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
1177 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2);
1178 }
1179 last_seq_no = seq_no;
1180 last_timestamp = timestamp;
1181
1182 ++seq_no;
1183 timestamp += kSamples;
1184 receive_timestamp += kSamples;
1185 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1186
1187 seq_no_wrapped |= seq_no < last_seq_no;
1188 timestamp_wrapped |= timestamp < last_timestamp;
1189 }
1190 // Pull out data once.
1191 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1192 &samples_per_channel, &num_channels,
1193 &output_type));
1194 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1195 ASSERT_EQ(1, num_channels);
1196
1197 // Expect delay (in samples) to be less than 2 packets.
1198 EXPECT_LE(timestamp - neteq_->PlayoutTimestamp(),
1199 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001200 }
1201 // Make sure we have actually tested wrap-around.
1202 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1203 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1204}
1205
1206TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1207 // Start with a sequence number that will soon wrap.
1208 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1209 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1210}
1211
1212TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1213 // Start with a sequence number that will soon wrap.
1214 std::set<uint16_t> drop_seq_numbers;
1215 drop_seq_numbers.insert(0xFFFF);
1216 drop_seq_numbers.insert(0x0);
1217 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1218}
1219
1220TEST_F(NetEqDecodingTest, TimestampWrap) {
1221 // Start with a timestamp that will soon wrap.
1222 std::set<uint16_t> drop_seq_numbers;
1223 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1224}
1225
1226TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1227 // Start with a timestamp and a sequence number that will wrap at the same
1228 // time.
1229 std::set<uint16_t> drop_seq_numbers;
1230 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1231}
1232
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001233} // namespace webrtc