blob: 2e0f2d5255ef520b0c5efd7cde7a39a1e7b86729 [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.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);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000223 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000224
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225 NetEq* neteq_;
226 FILE* rtp_fp_;
227 unsigned int sim_clock_;
228 int16_t out_data_[kMaxBlockSize];
229 int output_sample_rate_;
230};
231
232// Allocating the static const so that it can be passed by reference.
233const int NetEqDecodingTest::kTimeStepMs;
234const int NetEqDecodingTest::kBlockSize8kHz;
235const int NetEqDecodingTest::kBlockSize16kHz;
236const int NetEqDecodingTest::kBlockSize32kHz;
237const int NetEqDecodingTest::kMaxBlockSize;
238const int NetEqDecodingTest::kInitSampleRateHz;
239
240NetEqDecodingTest::NetEqDecodingTest()
241 : neteq_(NULL),
242 rtp_fp_(NULL),
243 sim_clock_(0),
244 output_sample_rate_(kInitSampleRateHz) {
245 memset(out_data_, 0, sizeof(out_data_));
246}
247
248void NetEqDecodingTest::SetUp() {
249 neteq_ = NetEq::Create(kInitSampleRateHz);
250 ASSERT_TRUE(neteq_);
251 LoadDecoders();
252}
253
254void NetEqDecodingTest::TearDown() {
255 delete neteq_;
256 if (rtp_fp_)
257 fclose(rtp_fp_);
258}
259
260void NetEqDecodingTest::LoadDecoders() {
261 // Load PCMu.
262 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
263 // Load PCMa.
264 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000265#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 // Load iLBC.
267 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000268#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 // Load iSAC.
270 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000271#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 // Load iSAC SWB.
273 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000274 // Load iSAC FB.
275 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000276#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 // Load PCM16B nb.
278 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
279 // Load PCM16B wb.
280 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
281 // Load PCM16B swb32.
282 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
283 // Load CNG 8 kHz.
284 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
285 // Load CNG 16 kHz.
286 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
287}
288
289void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
290 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
291 ASSERT_TRUE(rtp_fp_ != NULL);
292 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
293}
294
295void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
296 // Check if time to receive.
297 while ((sim_clock_ >= rtp->time()) &&
298 (rtp->dataLen() >= 0)) {
299 if (rtp->dataLen() > 0) {
300 WebRtcRTPHeader rtpInfo;
301 rtp->parseHeader(&rtpInfo);
302 ASSERT_EQ(0, neteq_->InsertPacket(
303 rtpInfo,
304 rtp->payload(),
305 rtp->payloadLen(),
306 rtp->time() * (output_sample_rate_ / 1000)));
307 }
308 // Get next packet.
309 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
310 }
311
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000312 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313 NetEqOutputType type;
314 int num_channels;
315 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
316 &num_channels, &type));
317 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
318 (*out_len == kBlockSize16kHz) ||
319 (*out_len == kBlockSize32kHz));
320 output_sample_rate_ = *out_len / 10 * 1000;
321
322 // Increase time.
323 sim_clock_ += kTimeStepMs;
324}
325
326void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
327 const std::string &ref_file) {
328 OpenInputFile(rtp_file);
329
330 std::string ref_out_file = "";
331 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000332 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 }
334 RefFiles ref_files(ref_file, ref_out_file);
335
336 NETEQTEST_RTPpacket rtp;
337 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
338 int i = 0;
339 while (rtp.dataLen() >= 0) {
340 std::ostringstream ss;
341 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
342 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000343 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
345 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
346 }
347}
348
349void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
350 const std::string &stat_ref_file,
351 const std::string &rtcp_ref_file) {
352 OpenInputFile(rtp_file);
353 std::string stat_out_file = "";
354 if (stat_ref_file.empty()) {
355 stat_out_file = webrtc::test::OutputPath() +
356 "neteq_network_stats.dat";
357 }
358 RefFiles network_stat_files(stat_ref_file, stat_out_file);
359
360 std::string rtcp_out_file = "";
361 if (rtcp_ref_file.empty()) {
362 rtcp_out_file = webrtc::test::OutputPath() +
363 "neteq_rtcp_stats.dat";
364 }
365 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
366
367 NETEQTEST_RTPpacket rtp;
368 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
369 while (rtp.dataLen() >= 0) {
370 int out_len;
371 Process(&rtp, &out_len);
372
373 // Query the network statistics API once per second
374 if (sim_clock_ % 1000 == 0) {
375 // Process NetworkStatistics.
376 NetEqNetworkStatistics network_stats;
377 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
378 network_stat_files.ProcessReference(network_stats);
379
380 // Process RTCPstat.
381 RtcpStatistics rtcp_stats;
382 neteq_->GetRtcpStatistics(&rtcp_stats);
383 rtcp_stat_files.ProcessReference(rtcp_stats);
384 }
385 }
386}
387
388void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
389 int timestamp,
390 WebRtcRTPHeader* rtp_info) {
391 rtp_info->header.sequenceNumber = frame_index;
392 rtp_info->header.timestamp = timestamp;
393 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
394 rtp_info->header.payloadType = 94; // PCM16b WB codec.
395 rtp_info->header.markerBit = 0;
396}
397
398void NetEqDecodingTest::PopulateCng(int frame_index,
399 int timestamp,
400 WebRtcRTPHeader* rtp_info,
401 uint8_t* payload,
402 int* payload_len) {
403 rtp_info->header.sequenceNumber = frame_index;
404 rtp_info->header.timestamp = timestamp;
405 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
406 rtp_info->header.payloadType = 98; // WB CNG.
407 rtp_info->header.markerBit = 0;
408 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
409 *payload_len = 1; // Only noise level, no spectral parameters.
410}
411
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000412void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
413 NetEqBackgroundNoiseMode bgn_mode) {
414 int expected_samples_per_channel = 0;
415 uint8_t payload_type = 0xFF; // Invalid.
416 if (sampling_rate_hz == 8000) {
417 expected_samples_per_channel = kBlockSize8kHz;
418 payload_type = 93; // PCM 16, 8 kHz.
419 } else if (sampling_rate_hz == 16000) {
420 expected_samples_per_channel = kBlockSize16kHz;
421 payload_type = 94; // PCM 16, 16 kHZ.
422 } else if (sampling_rate_hz == 32000) {
423 expected_samples_per_channel = kBlockSize32kHz;
424 payload_type = 95; // PCM 16, 32 kHz.
425 } else {
426 ASSERT_TRUE(false); // Unsupported test case.
427 }
428
429 NetEqOutputType type;
430 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
431 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
432
433 // Payload of 10 ms of PCM16 32 kHz.
434 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
435
436 // Random payload.
437 for (int n = 0; n < expected_samples_per_channel; ++n) {
438 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
439 }
440 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
441 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
442 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
443
444 WebRtcRTPHeader rtp_info;
445 PopulateRtpInfo(0, 0, &rtp_info);
446 rtp_info.header.payloadType = payload_type;
447
448 int number_channels = 0;
449 int samples_per_channel = 0;
450
451 uint32_t receive_timestamp = 0;
452 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
453 number_channels = 0;
454 samples_per_channel = 0;
455 ASSERT_EQ(0, neteq_->InsertPacket(
456 rtp_info, payload, enc_len_bytes, receive_timestamp));
457 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
458 &number_channels, &type));
459 ASSERT_EQ(1, number_channels);
460 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
461 ASSERT_EQ(kOutputNormal, type);
462
463 // Next packet.
464 rtp_info.header.timestamp += expected_samples_per_channel;
465 rtp_info.header.sequenceNumber++;
466 receive_timestamp += expected_samples_per_channel;
467 }
468
469 number_channels = 0;
470 samples_per_channel = 0;
471
472 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
473 // frame without checking speech-type. This is the first frame pulled without
474 // inserting any packet, and might not be labeled as PCL.
475 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
476 &number_channels, &type));
477 ASSERT_EQ(1, number_channels);
478 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
479
480 // To be able to test the fading of background noise we need at lease to pull
481 // 610 frames.
482 const int kFadingThreshold = 610;
483
484 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
485 // arbitrary, but sufficiently large to test enough number of frames.
486 const int kNumPlcToCngTestFrames = 20;
487 bool plc_to_cng = false;
488 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
489 number_channels = 0;
490 samples_per_channel = 0;
491 memset(output, 1, sizeof(output)); // Set to non-zero.
492 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
493 &number_channels, &type));
494 ASSERT_EQ(1, number_channels);
495 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
496 if (type == kOutputPLCtoCNG) {
497 plc_to_cng = true;
498 double sum_squared = 0;
499 for (int k = 0; k < number_channels * samples_per_channel; ++k)
500 sum_squared += output[k] * output[k];
501 if (bgn_mode == kBgnOn) {
502 EXPECT_NE(0, sum_squared);
503 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
504 EXPECT_EQ(0, sum_squared);
505 }
506 } else {
507 EXPECT_EQ(kOutputPLC, type);
508 }
509 }
510 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
511}
512
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000513#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
514// Disabled for Windows 64-bit until webrtc:1458 is fixed.
515#define MAYBE_TestBitExactness DISABLED_TestBitExactness
516#else
517#define MAYBE_TestBitExactness TestBitExactness
518#endif
519
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000520TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000521 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000522 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000523#if defined(_MSC_VER) && (_MSC_VER >= 1700)
524 // For Visual Studio 2012 and later, we will have to use the generic reference
525 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000526 const std::string input_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000527 "resources/audio_coding/neteq4_universal_ref.pcm";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000528#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000529 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000530 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000531#endif
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000532
533 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000534 DecodeAndCompare(input_rtp_file, "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000535 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000536 DecodeAndCompare(input_rtp_file, input_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000537 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538}
539
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000540TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000541 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000542 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000543#if defined(_MSC_VER) && (_MSC_VER >= 1700)
544 // For Visual Studio 2012 and later, we will have to use the generic reference
545 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000546 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000547 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000548#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000549 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000550 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000551#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000552 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000553 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
554 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000555 DecodeAndCheckStats(input_rtp_file, "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000556 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000557 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
558 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000559 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000560}
561
562// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000563TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564 // Use fax mode to avoid time-scaling. This is to simplify the testing of
565 // packet waiting times in the packet buffer.
566 neteq_->SetPlayoutMode(kPlayoutFax);
567 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
568 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
569 size_t num_frames = 30;
570 const int kSamples = 10 * 16;
571 const int kPayloadBytes = kSamples * 2;
572 for (size_t i = 0; i < num_frames; ++i) {
573 uint16_t payload[kSamples] = {0};
574 WebRtcRTPHeader rtp_info;
575 rtp_info.header.sequenceNumber = i;
576 rtp_info.header.timestamp = i * kSamples;
577 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
578 rtp_info.header.payloadType = 94; // PCM16b WB codec.
579 rtp_info.header.markerBit = 0;
580 ASSERT_EQ(0, neteq_->InsertPacket(
581 rtp_info,
582 reinterpret_cast<uint8_t*>(payload),
583 kPayloadBytes, 0));
584 }
585 // Pull out all data.
586 for (size_t i = 0; i < num_frames; ++i) {
587 int out_len;
588 int num_channels;
589 NetEqOutputType type;
590 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
591 &num_channels, &type));
592 ASSERT_EQ(kBlockSize16kHz, out_len);
593 }
594
595 std::vector<int> waiting_times;
596 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000597 EXPECT_EQ(num_frames, waiting_times.size());
598 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
599 // spacing (per definition), we expect the delay to increase with 10 ms for
600 // each packet.
601 for (size_t i = 0; i < waiting_times.size(); ++i) {
602 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
603 }
604
605 // Check statistics again and make sure it's been reset.
606 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000607 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000608 EXPECT_EQ(0, len);
609
610 // Process > 100 frames, and make sure that that we get statistics
611 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
612 num_frames = 110;
613 for (size_t i = 0; i < num_frames; ++i) {
614 uint16_t payload[kSamples] = {0};
615 WebRtcRTPHeader rtp_info;
616 rtp_info.header.sequenceNumber = i;
617 rtp_info.header.timestamp = i * kSamples;
618 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
619 rtp_info.header.payloadType = 94; // PCM16b WB codec.
620 rtp_info.header.markerBit = 0;
621 ASSERT_EQ(0, neteq_->InsertPacket(
622 rtp_info,
623 reinterpret_cast<uint8_t*>(payload),
624 kPayloadBytes, 0));
625 int out_len;
626 int num_channels;
627 NetEqOutputType type;
628 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
629 &num_channels, &type));
630 ASSERT_EQ(kBlockSize16kHz, out_len);
631 }
632
633 neteq_->WaitingTimes(&waiting_times);
634 EXPECT_EQ(100u, waiting_times.size());
635}
636
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000637TEST_F(NetEqDecodingTest,
638 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639 const int kNumFrames = 3000; // Needed for convergence.
640 int frame_index = 0;
641 const int kSamples = 10 * 16;
642 const int kPayloadBytes = kSamples * 2;
643 while (frame_index < kNumFrames) {
644 // Insert one packet each time, except every 10th time where we insert two
645 // packets at once. This will create a negative clock-drift of approx. 10%.
646 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
647 for (int n = 0; n < num_packets; ++n) {
648 uint8_t payload[kPayloadBytes] = {0};
649 WebRtcRTPHeader rtp_info;
650 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
651 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
652 ++frame_index;
653 }
654
655 // Pull out data once.
656 int out_len;
657 int num_channels;
658 NetEqOutputType type;
659 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
660 &num_channels, &type));
661 ASSERT_EQ(kBlockSize16kHz, out_len);
662 }
663
664 NetEqNetworkStatistics network_stats;
665 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
666 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
667}
668
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000669TEST_F(NetEqDecodingTest,
670 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000671 const int kNumFrames = 5000; // Needed for convergence.
672 int frame_index = 0;
673 const int kSamples = 10 * 16;
674 const int kPayloadBytes = kSamples * 2;
675 for (int i = 0; i < kNumFrames; ++i) {
676 // Insert one packet each time, except every 10th time where we don't insert
677 // any packet. This will create a positive clock-drift of approx. 11%.
678 int num_packets = (i % 10 == 9 ? 0 : 1);
679 for (int n = 0; n < num_packets; ++n) {
680 uint8_t payload[kPayloadBytes] = {0};
681 WebRtcRTPHeader rtp_info;
682 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
683 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
684 ++frame_index;
685 }
686
687 // Pull out data once.
688 int out_len;
689 int num_channels;
690 NetEqOutputType type;
691 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
692 &num_channels, &type));
693 ASSERT_EQ(kBlockSize16kHz, out_len);
694 }
695
696 NetEqNetworkStatistics network_stats;
697 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
698 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
699}
700
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000701void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000702 uint16_t seq_no = 0;
703 uint32_t timestamp = 0;
704 const int kFrameSizeMs = 30;
705 const int kSamples = kFrameSizeMs * 16;
706 const int kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000707 double next_input_time_ms = 0.0;
708 double t_ms;
709 NetEqOutputType type;
710
711 // Insert speech for 5 seconds.
712 const int kSpeechDurationMs = 5000;
713 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
714 // Each turn in this for loop is 10 ms.
715 while (next_input_time_ms <= t_ms) {
716 // Insert one 30 ms speech frame.
717 uint8_t payload[kPayloadBytes] = {0};
718 WebRtcRTPHeader rtp_info;
719 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
720 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
721 ++seq_no;
722 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000723 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724 }
725 // Pull out data once.
726 int out_len;
727 int num_channels;
728 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
729 &num_channels, &type));
730 ASSERT_EQ(kBlockSize16kHz, out_len);
731 }
732
733 EXPECT_EQ(kOutputNormal, type);
734 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
735
736 // Insert CNG for 1 minute (= 60000 ms).
737 const int kCngPeriodMs = 100;
738 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
739 const int kCngDurationMs = 60000;
740 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
741 // Each turn in this for loop is 10 ms.
742 while (next_input_time_ms <= t_ms) {
743 // Insert one CNG frame each 100 ms.
744 uint8_t payload[kPayloadBytes];
745 int payload_len;
746 WebRtcRTPHeader rtp_info;
747 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
748 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
749 ++seq_no;
750 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000751 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000752 }
753 // Pull out data once.
754 int out_len;
755 int num_channels;
756 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
757 &num_channels, &type));
758 ASSERT_EQ(kBlockSize16kHz, out_len);
759 }
760
761 EXPECT_EQ(kOutputCNG, type);
762
763 // Insert speech again until output type is speech.
764 while (type != kOutputNormal) {
765 // Each turn in this for loop is 10 ms.
766 while (next_input_time_ms <= t_ms) {
767 // Insert one 30 ms speech frame.
768 uint8_t payload[kPayloadBytes] = {0};
769 WebRtcRTPHeader rtp_info;
770 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
771 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
772 ++seq_no;
773 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000774 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000775 }
776 // Pull out data once.
777 int out_len;
778 int num_channels;
779 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
780 &num_channels, &type));
781 ASSERT_EQ(kBlockSize16kHz, out_len);
782 // Increase clock.
783 t_ms += 10;
784 }
785
786 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
787 // Compare delay before and after, and make sure it differs less than 20 ms.
788 EXPECT_LE(delay_after, delay_before + 20 * 16);
789 EXPECT_GE(delay_after, delay_before - 20 * 16);
790}
791
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000792TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithNegativeClockDrift)) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000793 // Apply a clock drift of -25 ms / s (sender faster than receiver).
794 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
795 LongCngWithClockDrift(kDriftFactor);
796}
797
798// TODO(hlundin): Re-enable this test and fix the issues to make it pass.
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000799TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithPositiveClockDrift)) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000800 // 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
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001147 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001148 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.orgca8cb952014-03-12 10:26:52 +00001233void NetEqDecodingTest::DuplicateCng() {
1234 uint16_t seq_no = 0;
1235 uint32_t timestamp = 0;
1236 const int kFrameSizeMs = 10;
1237 const int kSampleRateKhz = 16;
1238 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1239 const int kPayloadBytes = kSamples * 2;
1240
1241 // Insert three speech packet. Three are needed to get the frame length
1242 // correct.
1243 int out_len;
1244 int num_channels;
1245 NetEqOutputType type;
1246 uint8_t payload[kPayloadBytes] = {0};
1247 WebRtcRTPHeader rtp_info;
1248 for (int i = 0; i < 3; ++i) {
1249 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1250 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1251 ++seq_no;
1252 timestamp += kSamples;
1253
1254 // Pull audio once.
1255 ASSERT_EQ(0,
1256 neteq_->GetAudio(
1257 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1258 ASSERT_EQ(kBlockSize16kHz, out_len);
1259 }
1260 // Verify speech output.
1261 EXPECT_EQ(kOutputNormal, type);
1262
1263 // Insert same CNG packet twice.
1264 const int kCngPeriodMs = 100;
1265 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1266 int payload_len;
1267 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1268 // This is the first time this CNG packet is inserted.
1269 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1270
1271 // Pull audio once and make sure CNG is played.
1272 ASSERT_EQ(0,
1273 neteq_->GetAudio(
1274 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1275 ASSERT_EQ(kBlockSize16kHz, out_len);
1276 EXPECT_EQ(kOutputCNG, type);
1277 EXPECT_EQ(timestamp - 10, neteq_->PlayoutTimestamp());
1278
1279 // Insert the same CNG packet again. Note that at this point it is old, since
1280 // we have already decoded the first copy of it.
1281 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1282
1283 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1284 // we have already pulled out CNG once.
1285 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1286 ASSERT_EQ(0,
1287 neteq_->GetAudio(
1288 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1289 ASSERT_EQ(kBlockSize16kHz, out_len);
1290 EXPECT_EQ(kOutputCNG, type);
1291 EXPECT_EQ(timestamp - 10, neteq_->PlayoutTimestamp());
1292 }
1293
1294 // Insert speech again.
1295 ++seq_no;
1296 timestamp += kCngPeriodSamples;
1297 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1298 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1299
1300 // Pull audio once and verify that the output is speech again.
1301 ASSERT_EQ(0,
1302 neteq_->GetAudio(
1303 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1304 ASSERT_EQ(kBlockSize16kHz, out_len);
1305 EXPECT_EQ(kOutputNormal, type);
1306 EXPECT_EQ(timestamp + kSamples - 10, neteq_->PlayoutTimestamp());
1307}
1308
1309TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001310} // namespace webrtc