2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
12 * This file includes unit tests for NetEQ.
15 #include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
19 #include <string.h> // memset
26 #include "gflags/gflags.h"
27 #include "gtest/gtest.h"
28 #include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_RTPpacket.h"
29 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
30 #include "webrtc/test/testsupport/fileutils.h"
31 #include "webrtc/test/testsupport/gtest_disable.h"
32 #include "webrtc/typedefs.h"
34 DEFINE_bool(gen_ref, false, "Generate reference files.");
38 static bool IsAllZero(const int16_t* buf, int buf_length) {
40 for (int n = 0; n < buf_length && all_zero; ++n)
41 all_zero = buf[n] == 0;
45 static bool IsAllNonZero(const int16_t* buf, int buf_length) {
46 bool all_non_zero = true;
47 for (int n = 0; n < buf_length && all_non_zero; ++n)
48 all_non_zero = buf[n] != 0;
54 RefFiles(const std::string& input_file, const std::string& output_file);
56 template<class T> void ProcessReference(const T& test_results);
57 template<typename T, size_t n> void ProcessReference(
58 const T (&test_results)[n],
60 template<typename T, size_t n> void WriteToFile(
61 const T (&test_results)[n],
63 template<typename T, size_t n> void ReadFromFileAndCompare(
64 const T (&test_results)[n],
66 void WriteToFile(const NetEqNetworkStatistics& stats);
67 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
68 void WriteToFile(const RtcpStatistics& stats);
69 void ReadFromFileAndCompare(const RtcpStatistics& stats);
75 RefFiles::RefFiles(const std::string &input_file,
76 const std::string &output_file)
79 if (!input_file.empty()) {
80 input_fp_ = fopen(input_file.c_str(), "rb");
81 EXPECT_TRUE(input_fp_ != NULL);
83 if (!output_file.empty()) {
84 output_fp_ = fopen(output_file.c_str(), "wb");
85 EXPECT_TRUE(output_fp_ != NULL);
89 RefFiles::~RefFiles() {
91 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
94 if (output_fp_) fclose(output_fp_);
98 void RefFiles::ProcessReference(const T& test_results) {
99 WriteToFile(test_results);
100 ReadFromFileAndCompare(test_results);
103 template<typename T, size_t n>
104 void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
105 WriteToFile(test_results, length);
106 ReadFromFileAndCompare(test_results, length);
109 template<typename T, size_t n>
110 void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
112 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
116 template<typename T, size_t n>
117 void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
120 // Read from ref file.
121 T* ref = new T[length];
122 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
124 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
129 void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
131 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
136 void RefFiles::ReadFromFileAndCompare(
137 const NetEqNetworkStatistics& stats) {
139 // Read from ref file.
140 size_t stat_size = sizeof(NetEqNetworkStatistics);
141 NetEqNetworkStatistics ref_stats;
142 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
144 EXPECT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
148 void RefFiles::WriteToFile(const RtcpStatistics& stats) {
150 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
152 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
153 sizeof(stats.cumulative_lost), 1, output_fp_));
154 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
155 sizeof(stats.extended_max_sequence_number), 1,
157 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
162 void RefFiles::ReadFromFileAndCompare(
163 const RtcpStatistics& stats) {
165 // Read from ref file.
166 RtcpStatistics ref_stats;
167 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
168 sizeof(ref_stats.fraction_lost), 1, input_fp_));
169 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
170 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
171 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
172 sizeof(ref_stats.extended_max_sequence_number), 1,
174 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
177 EXPECT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
178 EXPECT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
179 EXPECT_EQ(ref_stats.extended_max_sequence_number,
180 stats.extended_max_sequence_number);
181 EXPECT_EQ(ref_stats.jitter, stats.jitter);
185 class NetEqDecodingTest : public ::testing::Test {
187 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
188 // constants below can be changed.
189 static const int kTimeStepMs = 10;
190 static const int kBlockSize8kHz = kTimeStepMs * 8;
191 static const int kBlockSize16kHz = kTimeStepMs * 16;
192 static const int kBlockSize32kHz = kTimeStepMs * 32;
193 static const int kMaxBlockSize = kBlockSize32kHz;
194 static const int kInitSampleRateHz = 8000;
197 virtual void SetUp();
198 virtual void TearDown();
199 void SelectDecoders(NetEqDecoder* used_codec);
201 void OpenInputFile(const std::string &rtp_file);
202 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
203 void DecodeAndCompare(const std::string &rtp_file,
204 const std::string &ref_file);
205 void DecodeAndCheckStats(const std::string &rtp_file,
206 const std::string &stat_ref_file,
207 const std::string &rtcp_ref_file);
208 static void PopulateRtpInfo(int frame_index,
210 WebRtcRTPHeader* rtp_info);
211 static void PopulateCng(int frame_index,
213 WebRtcRTPHeader* rtp_info,
217 void CheckBgnOff(int sampling_rate, NetEqBackgroundNoiseMode bgn_mode);
219 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
220 const std::set<uint16_t>& drop_seq_numbers,
221 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
223 void LongCngWithClockDrift(double drift_factor,
224 double network_freeze_ms,
225 bool pull_audio_during_freeze,
226 int delay_tolerance_ms,
227 int max_time_to_speech_ms);
233 unsigned int sim_clock_;
234 int16_t out_data_[kMaxBlockSize];
235 int output_sample_rate_;
236 int algorithmic_delay_ms_;
239 // Allocating the static const so that it can be passed by reference.
240 const int NetEqDecodingTest::kTimeStepMs;
241 const int NetEqDecodingTest::kBlockSize8kHz;
242 const int NetEqDecodingTest::kBlockSize16kHz;
243 const int NetEqDecodingTest::kBlockSize32kHz;
244 const int NetEqDecodingTest::kMaxBlockSize;
245 const int NetEqDecodingTest::kInitSampleRateHz;
247 NetEqDecodingTest::NetEqDecodingTest()
251 output_sample_rate_(kInitSampleRateHz),
252 algorithmic_delay_ms_(0) {
253 memset(out_data_, 0, sizeof(out_data_));
256 void NetEqDecodingTest::SetUp() {
257 NetEq::Config config;
258 config.sample_rate_hz = kInitSampleRateHz;
259 neteq_ = NetEq::Create(config);
260 NetEqNetworkStatistics stat;
261 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
262 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
267 void NetEqDecodingTest::TearDown() {
273 void NetEqDecodingTest::LoadDecoders() {
275 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
277 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
278 #ifndef WEBRTC_ANDROID
280 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
281 #endif // WEBRTC_ANDROID
283 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
284 #ifndef WEBRTC_ANDROID
286 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
288 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
289 #endif // WEBRTC_ANDROID
291 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
293 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
294 // Load PCM16B swb32.
295 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
297 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
299 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
302 void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
303 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
304 ASSERT_TRUE(rtp_fp_ != NULL);
305 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
308 void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
309 // Check if time to receive.
310 while ((sim_clock_ >= rtp->time()) &&
311 (rtp->dataLen() >= 0)) {
312 if (rtp->dataLen() > 0) {
313 WebRtcRTPHeader rtpInfo;
314 rtp->parseHeader(&rtpInfo);
315 ASSERT_EQ(0, neteq_->InsertPacket(
319 rtp->time() * (output_sample_rate_ / 1000)));
322 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
325 // Get audio from NetEq.
326 NetEqOutputType type;
328 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
329 &num_channels, &type));
330 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
331 (*out_len == kBlockSize16kHz) ||
332 (*out_len == kBlockSize32kHz));
333 output_sample_rate_ = *out_len / 10 * 1000;
336 sim_clock_ += kTimeStepMs;
339 void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
340 const std::string &ref_file) {
341 OpenInputFile(rtp_file);
343 std::string ref_out_file = "";
344 if (ref_file.empty()) {
345 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
347 RefFiles ref_files(ref_file, ref_out_file);
349 NETEQTEST_RTPpacket rtp;
350 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
352 while (rtp.dataLen() >= 0) {
353 std::ostringstream ss;
354 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
355 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
357 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
358 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
362 void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
363 const std::string &stat_ref_file,
364 const std::string &rtcp_ref_file) {
365 OpenInputFile(rtp_file);
366 std::string stat_out_file = "";
367 if (stat_ref_file.empty()) {
368 stat_out_file = webrtc::test::OutputPath() +
369 "neteq_network_stats.dat";
371 RefFiles network_stat_files(stat_ref_file, stat_out_file);
373 std::string rtcp_out_file = "";
374 if (rtcp_ref_file.empty()) {
375 rtcp_out_file = webrtc::test::OutputPath() +
376 "neteq_rtcp_stats.dat";
378 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
380 NETEQTEST_RTPpacket rtp;
381 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
382 while (rtp.dataLen() >= 0) {
384 Process(&rtp, &out_len);
386 // Query the network statistics API once per second
387 if (sim_clock_ % 1000 == 0) {
388 // Process NetworkStatistics.
389 NetEqNetworkStatistics network_stats;
390 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
391 network_stat_files.ProcessReference(network_stats);
394 RtcpStatistics rtcp_stats;
395 neteq_->GetRtcpStatistics(&rtcp_stats);
396 rtcp_stat_files.ProcessReference(rtcp_stats);
401 void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
403 WebRtcRTPHeader* rtp_info) {
404 rtp_info->header.sequenceNumber = frame_index;
405 rtp_info->header.timestamp = timestamp;
406 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
407 rtp_info->header.payloadType = 94; // PCM16b WB codec.
408 rtp_info->header.markerBit = 0;
411 void NetEqDecodingTest::PopulateCng(int frame_index,
413 WebRtcRTPHeader* rtp_info,
416 rtp_info->header.sequenceNumber = frame_index;
417 rtp_info->header.timestamp = timestamp;
418 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
419 rtp_info->header.payloadType = 98; // WB CNG.
420 rtp_info->header.markerBit = 0;
421 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
422 *payload_len = 1; // Only noise level, no spectral parameters.
425 void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
426 NetEqBackgroundNoiseMode bgn_mode) {
427 int expected_samples_per_channel = 0;
428 uint8_t payload_type = 0xFF; // Invalid.
429 if (sampling_rate_hz == 8000) {
430 expected_samples_per_channel = kBlockSize8kHz;
431 payload_type = 93; // PCM 16, 8 kHz.
432 } else if (sampling_rate_hz == 16000) {
433 expected_samples_per_channel = kBlockSize16kHz;
434 payload_type = 94; // PCM 16, 16 kHZ.
435 } else if (sampling_rate_hz == 32000) {
436 expected_samples_per_channel = kBlockSize32kHz;
437 payload_type = 95; // PCM 16, 32 kHz.
439 ASSERT_TRUE(false); // Unsupported test case.
442 NetEqOutputType type;
443 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
444 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
446 // Payload of 10 ms of PCM16 32 kHz.
447 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
450 for (int n = 0; n < expected_samples_per_channel; ++n) {
451 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
453 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
454 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
455 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
457 WebRtcRTPHeader rtp_info;
458 PopulateRtpInfo(0, 0, &rtp_info);
459 rtp_info.header.payloadType = payload_type;
461 int number_channels = 0;
462 int samples_per_channel = 0;
464 uint32_t receive_timestamp = 0;
465 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
467 samples_per_channel = 0;
468 ASSERT_EQ(0, neteq_->InsertPacket(
469 rtp_info, payload, enc_len_bytes, receive_timestamp));
470 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
471 &number_channels, &type));
472 ASSERT_EQ(1, number_channels);
473 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
474 ASSERT_EQ(kOutputNormal, type);
477 rtp_info.header.timestamp += expected_samples_per_channel;
478 rtp_info.header.sequenceNumber++;
479 receive_timestamp += expected_samples_per_channel;
483 samples_per_channel = 0;
485 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
486 // frame without checking speech-type. This is the first frame pulled without
487 // inserting any packet, and might not be labeled as PCL.
488 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
489 &number_channels, &type));
490 ASSERT_EQ(1, number_channels);
491 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
493 // To be able to test the fading of background noise we need at lease to pull
495 const int kFadingThreshold = 611;
497 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
498 // arbitrary, but sufficiently large to test enough number of frames.
499 const int kNumPlcToCngTestFrames = 20;
500 bool plc_to_cng = false;
501 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
503 samples_per_channel = 0;
504 memset(output, 1, sizeof(output)); // Set to non-zero.
505 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
506 &number_channels, &type));
507 ASSERT_EQ(1, number_channels);
508 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
509 if (type == kOutputPLCtoCNG) {
511 double sum_squared = 0;
512 for (int k = 0; k < number_channels * samples_per_channel; ++k)
513 sum_squared += output[k] * output[k];
514 if (bgn_mode == kBgnOn) {
515 EXPECT_NE(0, sum_squared);
516 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
517 EXPECT_EQ(0, sum_squared);
520 EXPECT_EQ(kOutputPLC, type);
523 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
526 #if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
527 // Disabled for Windows 64-bit until webrtc:1458 is fixed.
528 #define MAYBE_TestBitExactness DISABLED_TestBitExactness
530 #define MAYBE_TestBitExactness TestBitExactness
533 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
534 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
535 "resources/audio_coding/neteq_universal_new.rtp";
536 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
537 // For Visual Studio 2012 and later, we will have to use the generic reference
538 // file, rather than the windows-specific one.
539 const std::string input_ref_file = webrtc::test::ProjectRootPath() +
540 "resources/audio_coding/neteq4_universal_ref.pcm";
542 const std::string input_ref_file =
543 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
547 DecodeAndCompare(input_rtp_file, "");
549 DecodeAndCompare(input_rtp_file, input_ref_file);
553 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
554 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
555 "resources/audio_coding/neteq_universal_new.rtp";
556 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
557 // For Visual Studio 2012 and later, we will have to use the generic reference
558 // file, rather than the windows-specific one.
559 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
560 "resources/audio_coding/neteq4_network_stats.dat";
562 const std::string network_stat_ref_file =
563 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
565 const std::string rtcp_stat_ref_file =
566 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
568 DecodeAndCheckStats(input_rtp_file, "", "");
570 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
575 // TODO(hlundin): Re-enable test once the statistics interface is up and again.
576 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
577 // Use fax mode to avoid time-scaling. This is to simplify the testing of
578 // packet waiting times in the packet buffer.
579 neteq_->SetPlayoutMode(kPlayoutFax);
580 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
581 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
582 size_t num_frames = 30;
583 const int kSamples = 10 * 16;
584 const int kPayloadBytes = kSamples * 2;
585 for (size_t i = 0; i < num_frames; ++i) {
586 uint16_t payload[kSamples] = {0};
587 WebRtcRTPHeader rtp_info;
588 rtp_info.header.sequenceNumber = i;
589 rtp_info.header.timestamp = i * kSamples;
590 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
591 rtp_info.header.payloadType = 94; // PCM16b WB codec.
592 rtp_info.header.markerBit = 0;
593 ASSERT_EQ(0, neteq_->InsertPacket(
595 reinterpret_cast<uint8_t*>(payload),
598 // Pull out all data.
599 for (size_t i = 0; i < num_frames; ++i) {
602 NetEqOutputType type;
603 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
604 &num_channels, &type));
605 ASSERT_EQ(kBlockSize16kHz, out_len);
608 std::vector<int> waiting_times;
609 neteq_->WaitingTimes(&waiting_times);
610 EXPECT_EQ(num_frames, waiting_times.size());
611 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
612 // spacing (per definition), we expect the delay to increase with 10 ms for
614 for (size_t i = 0; i < waiting_times.size(); ++i) {
615 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
618 // Check statistics again and make sure it's been reset.
619 neteq_->WaitingTimes(&waiting_times);
620 int len = waiting_times.size();
623 // Process > 100 frames, and make sure that that we get statistics
624 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
626 for (size_t i = 0; i < num_frames; ++i) {
627 uint16_t payload[kSamples] = {0};
628 WebRtcRTPHeader rtp_info;
629 rtp_info.header.sequenceNumber = i;
630 rtp_info.header.timestamp = i * kSamples;
631 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
632 rtp_info.header.payloadType = 94; // PCM16b WB codec.
633 rtp_info.header.markerBit = 0;
634 ASSERT_EQ(0, neteq_->InsertPacket(
636 reinterpret_cast<uint8_t*>(payload),
640 NetEqOutputType type;
641 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
642 &num_channels, &type));
643 ASSERT_EQ(kBlockSize16kHz, out_len);
646 neteq_->WaitingTimes(&waiting_times);
647 EXPECT_EQ(100u, waiting_times.size());
650 TEST_F(NetEqDecodingTest,
651 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
652 const int kNumFrames = 3000; // Needed for convergence.
654 const int kSamples = 10 * 16;
655 const int kPayloadBytes = kSamples * 2;
656 while (frame_index < kNumFrames) {
657 // Insert one packet each time, except every 10th time where we insert two
658 // packets at once. This will create a negative clock-drift of approx. 10%.
659 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
660 for (int n = 0; n < num_packets; ++n) {
661 uint8_t payload[kPayloadBytes] = {0};
662 WebRtcRTPHeader rtp_info;
663 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
664 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
668 // Pull out data once.
671 NetEqOutputType type;
672 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
673 &num_channels, &type));
674 ASSERT_EQ(kBlockSize16kHz, out_len);
677 NetEqNetworkStatistics network_stats;
678 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
679 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
682 TEST_F(NetEqDecodingTest,
683 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
684 const int kNumFrames = 5000; // Needed for convergence.
686 const int kSamples = 10 * 16;
687 const int kPayloadBytes = kSamples * 2;
688 for (int i = 0; i < kNumFrames; ++i) {
689 // Insert one packet each time, except every 10th time where we don't insert
690 // any packet. This will create a positive clock-drift of approx. 11%.
691 int num_packets = (i % 10 == 9 ? 0 : 1);
692 for (int n = 0; n < num_packets; ++n) {
693 uint8_t payload[kPayloadBytes] = {0};
694 WebRtcRTPHeader rtp_info;
695 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
696 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
700 // Pull out data once.
703 NetEqOutputType type;
704 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
705 &num_channels, &type));
706 ASSERT_EQ(kBlockSize16kHz, out_len);
709 NetEqNetworkStatistics network_stats;
710 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
711 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
714 void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
715 double network_freeze_ms,
716 bool pull_audio_during_freeze,
717 int delay_tolerance_ms,
718 int max_time_to_speech_ms) {
720 uint32_t timestamp = 0;
721 const int kFrameSizeMs = 30;
722 const int kSamples = kFrameSizeMs * 16;
723 const int kPayloadBytes = kSamples * 2;
724 double next_input_time_ms = 0.0;
728 NetEqOutputType type;
730 // Insert speech for 5 seconds.
731 const int kSpeechDurationMs = 5000;
732 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
733 // Each turn in this for loop is 10 ms.
734 while (next_input_time_ms <= t_ms) {
735 // Insert one 30 ms speech frame.
736 uint8_t payload[kPayloadBytes] = {0};
737 WebRtcRTPHeader rtp_info;
738 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
739 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
741 timestamp += kSamples;
742 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
744 // Pull out data once.
745 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
746 &num_channels, &type));
747 ASSERT_EQ(kBlockSize16kHz, out_len);
750 EXPECT_EQ(kOutputNormal, type);
751 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
753 // Insert CNG for 1 minute (= 60000 ms).
754 const int kCngPeriodMs = 100;
755 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
756 const int kCngDurationMs = 60000;
757 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
758 // Each turn in this for loop is 10 ms.
759 while (next_input_time_ms <= t_ms) {
760 // Insert one CNG frame each 100 ms.
761 uint8_t payload[kPayloadBytes];
763 WebRtcRTPHeader rtp_info;
764 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
765 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
767 timestamp += kCngPeriodSamples;
768 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
770 // Pull out data once.
771 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
772 &num_channels, &type));
773 ASSERT_EQ(kBlockSize16kHz, out_len);
776 EXPECT_EQ(kOutputCNG, type);
778 if (network_freeze_ms > 0) {
779 // First keep pulling audio for |network_freeze_ms| without inserting
780 // any data, then insert CNG data corresponding to |network_freeze_ms|
781 // without pulling any output audio.
782 const double loop_end_time = t_ms + network_freeze_ms;
783 for (; t_ms < loop_end_time; t_ms += 10) {
784 // Pull out data once.
787 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
788 ASSERT_EQ(kBlockSize16kHz, out_len);
789 EXPECT_EQ(kOutputCNG, type);
791 bool pull_once = pull_audio_during_freeze;
792 // If |pull_once| is true, GetAudio will be called once half-way through
793 // the network recovery period.
794 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
795 while (next_input_time_ms <= t_ms) {
796 if (pull_once && next_input_time_ms >= pull_time_ms) {
798 // Pull out data once.
802 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
803 ASSERT_EQ(kBlockSize16kHz, out_len);
804 EXPECT_EQ(kOutputCNG, type);
807 // Insert one CNG frame each 100 ms.
808 uint8_t payload[kPayloadBytes];
810 WebRtcRTPHeader rtp_info;
811 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
812 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
814 timestamp += kCngPeriodSamples;
815 next_input_time_ms += kCngPeriodMs * drift_factor;
819 // Insert speech again until output type is speech.
820 double speech_restart_time_ms = t_ms;
821 while (type != kOutputNormal) {
822 // Each turn in this for loop is 10 ms.
823 while (next_input_time_ms <= t_ms) {
824 // Insert one 30 ms speech frame.
825 uint8_t payload[kPayloadBytes] = {0};
826 WebRtcRTPHeader rtp_info;
827 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
828 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
830 timestamp += kSamples;
831 next_input_time_ms += kFrameSizeMs * drift_factor;
833 // Pull out data once.
834 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
835 &num_channels, &type));
836 ASSERT_EQ(kBlockSize16kHz, out_len);
841 // Check that the speech starts again within reasonable time.
842 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
843 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
844 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
845 // Compare delay before and after, and make sure it differs less than 20 ms.
846 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
847 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
850 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithNegativeClockDrift)) {
851 // Apply a clock drift of -25 ms / s (sender faster than receiver).
852 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
853 const double kNetworkFreezeTimeMs = 0.0;
854 const bool kGetAudioDuringFreezeRecovery = false;
855 const int kDelayToleranceMs = 20;
856 const int kMaxTimeToSpeechMs = 100;
857 LongCngWithClockDrift(kDriftFactor,
858 kNetworkFreezeTimeMs,
859 kGetAudioDuringFreezeRecovery,
864 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithPositiveClockDrift)) {
865 // Apply a clock drift of +25 ms / s (sender slower than receiver).
866 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
867 const double kNetworkFreezeTimeMs = 0.0;
868 const bool kGetAudioDuringFreezeRecovery = false;
869 const int kDelayToleranceMs = 20;
870 const int kMaxTimeToSpeechMs = 100;
871 LongCngWithClockDrift(kDriftFactor,
872 kNetworkFreezeTimeMs,
873 kGetAudioDuringFreezeRecovery,
878 TEST_F(NetEqDecodingTest,
879 DISABLED_ON_ANDROID(LongCngWithNegativeClockDriftNetworkFreeze)) {
880 // Apply a clock drift of -25 ms / s (sender faster than receiver).
881 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
882 const double kNetworkFreezeTimeMs = 5000.0;
883 const bool kGetAudioDuringFreezeRecovery = false;
884 const int kDelayToleranceMs = 50;
885 const int kMaxTimeToSpeechMs = 200;
886 LongCngWithClockDrift(kDriftFactor,
887 kNetworkFreezeTimeMs,
888 kGetAudioDuringFreezeRecovery,
893 TEST_F(NetEqDecodingTest,
894 DISABLED_ON_ANDROID(LongCngWithPositiveClockDriftNetworkFreeze)) {
895 // Apply a clock drift of +25 ms / s (sender slower than receiver).
896 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
897 const double kNetworkFreezeTimeMs = 5000.0;
898 const bool kGetAudioDuringFreezeRecovery = false;
899 const int kDelayToleranceMs = 20;
900 const int kMaxTimeToSpeechMs = 100;
901 LongCngWithClockDrift(kDriftFactor,
902 kNetworkFreezeTimeMs,
903 kGetAudioDuringFreezeRecovery,
910 DISABLED_ON_ANDROID(LongCngWithPositiveClockDriftNetworkFreezeExtraPull)) {
911 // Apply a clock drift of +25 ms / s (sender slower than receiver).
912 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
913 const double kNetworkFreezeTimeMs = 5000.0;
914 const bool kGetAudioDuringFreezeRecovery = true;
915 const int kDelayToleranceMs = 20;
916 const int kMaxTimeToSpeechMs = 100;
917 LongCngWithClockDrift(kDriftFactor,
918 kNetworkFreezeTimeMs,
919 kGetAudioDuringFreezeRecovery,
924 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithoutClockDrift)) {
925 const double kDriftFactor = 1.0; // No drift.
926 const double kNetworkFreezeTimeMs = 0.0;
927 const bool kGetAudioDuringFreezeRecovery = false;
928 const int kDelayToleranceMs = 10;
929 const int kMaxTimeToSpeechMs = 50;
930 LongCngWithClockDrift(kDriftFactor,
931 kNetworkFreezeTimeMs,
932 kGetAudioDuringFreezeRecovery,
937 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(UnknownPayloadType)) {
938 const int kPayloadBytes = 100;
939 uint8_t payload[kPayloadBytes] = {0};
940 WebRtcRTPHeader rtp_info;
941 PopulateRtpInfo(0, 0, &rtp_info);
942 rtp_info.header.payloadType = 1; // Not registered as a decoder.
943 EXPECT_EQ(NetEq::kFail,
944 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
945 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
948 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
949 const int kPayloadBytes = 100;
950 uint8_t payload[kPayloadBytes] = {0};
951 WebRtcRTPHeader rtp_info;
952 PopulateRtpInfo(0, 0, &rtp_info);
953 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
954 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
955 NetEqOutputType type;
956 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
958 for (int i = 0; i < kMaxBlockSize; ++i) {
962 int samples_per_channel;
963 EXPECT_EQ(NetEq::kFail,
964 neteq_->GetAudio(kMaxBlockSize, out_data_,
965 &samples_per_channel, &num_channels, &type));
966 // Verify that there is a decoder error to check.
967 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
968 // Code 6730 is an iSAC error code.
969 EXPECT_EQ(6730, neteq_->LastDecoderError());
970 // Verify that the first 160 samples are set to 0, and that the remaining
971 // samples are left unmodified.
972 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
973 for (int i = 0; i < kExpectedOutputLength; ++i) {
974 std::ostringstream ss;
976 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
977 EXPECT_EQ(0, out_data_[i]);
979 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
980 std::ostringstream ss;
982 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
983 EXPECT_EQ(1, out_data_[i]);
987 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(GetAudioBeforeInsertPacket)) {
988 NetEqOutputType type;
989 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
991 for (int i = 0; i < kMaxBlockSize; ++i) {
995 int samples_per_channel;
996 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
997 &samples_per_channel,
998 &num_channels, &type));
999 // Verify that the first block of samples is set to 0.
1000 static const int kExpectedOutputLength =
1001 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
1002 for (int i = 0; i < kExpectedOutputLength; ++i) {
1003 std::ostringstream ss;
1005 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
1006 EXPECT_EQ(0, out_data_[i]);
1010 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(BackgroundNoise)) {
1011 neteq_->SetBackgroundNoiseMode(kBgnOn);
1012 CheckBgnOff(8000, kBgnOn);
1013 CheckBgnOff(16000, kBgnOn);
1014 CheckBgnOff(32000, kBgnOn);
1015 EXPECT_EQ(kBgnOn, neteq_->BackgroundNoiseMode());
1017 neteq_->SetBackgroundNoiseMode(kBgnOff);
1018 CheckBgnOff(8000, kBgnOff);
1019 CheckBgnOff(16000, kBgnOff);
1020 CheckBgnOff(32000, kBgnOff);
1021 EXPECT_EQ(kBgnOff, neteq_->BackgroundNoiseMode());
1023 neteq_->SetBackgroundNoiseMode(kBgnFade);
1024 CheckBgnOff(8000, kBgnFade);
1025 CheckBgnOff(16000, kBgnFade);
1026 CheckBgnOff(32000, kBgnFade);
1027 EXPECT_EQ(kBgnFade, neteq_->BackgroundNoiseMode());
1030 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketInsert)) {
1031 WebRtcRTPHeader rtp_info;
1032 uint32_t receive_timestamp = 0;
1033 // For the readability use the following payloads instead of the defaults of
1035 uint8_t kPcm16WbPayloadType = 1;
1036 uint8_t kCngNbPayloadType = 2;
1037 uint8_t kCngWbPayloadType = 3;
1038 uint8_t kCngSwb32PayloadType = 4;
1039 uint8_t kCngSwb48PayloadType = 5;
1040 uint8_t kAvtPayloadType = 6;
1041 uint8_t kRedPayloadType = 7;
1042 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1044 // Register decoders.
1045 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1046 kPcm16WbPayloadType));
1047 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1048 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1049 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1050 kCngSwb32PayloadType));
1051 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1052 kCngSwb48PayloadType));
1053 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1054 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1055 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1057 PopulateRtpInfo(0, 0, &rtp_info);
1058 rtp_info.header.payloadType = kPcm16WbPayloadType;
1060 // The first packet injected cannot be sync-packet.
1061 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1063 // Payload length of 10 ms PCM16 16 kHz.
1064 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1065 uint8_t payload[kPayloadBytes] = {0};
1066 ASSERT_EQ(0, neteq_->InsertPacket(
1067 rtp_info, payload, kPayloadBytes, receive_timestamp));
1069 // Next packet. Last packet contained 10 ms audio.
1070 rtp_info.header.sequenceNumber++;
1071 rtp_info.header.timestamp += kBlockSize16kHz;
1072 receive_timestamp += kBlockSize16kHz;
1074 // Unacceptable payload types CNG, AVT (DTMF), RED.
1075 rtp_info.header.payloadType = kCngNbPayloadType;
1076 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1078 rtp_info.header.payloadType = kCngWbPayloadType;
1079 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081 rtp_info.header.payloadType = kCngSwb32PayloadType;
1082 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1084 rtp_info.header.payloadType = kCngSwb48PayloadType;
1085 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1087 rtp_info.header.payloadType = kAvtPayloadType;
1088 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1090 rtp_info.header.payloadType = kRedPayloadType;
1091 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1093 // Change of codec cannot be initiated with a sync packet.
1094 rtp_info.header.payloadType = kIsacPayloadType;
1095 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1097 // Change of SSRC is not allowed with a sync packet.
1098 rtp_info.header.payloadType = kPcm16WbPayloadType;
1099 ++rtp_info.header.ssrc;
1100 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1102 --rtp_info.header.ssrc;
1103 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1106 // First insert several noise like packets, then sync-packets. Decoding all
1107 // packets should not produce error, statistics should not show any packet loss
1108 // and sync-packets should decode to zero.
1109 // TODO(turajs) we will have a better test if we have a referece NetEq, and
1110 // when Sync packets are inserted in "test" NetEq we insert all-zero payload
1111 // in reference NetEq and compare the output of those two.
1112 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketDecode)) {
1113 WebRtcRTPHeader rtp_info;
1114 PopulateRtpInfo(0, 0, &rtp_info);
1115 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1116 uint8_t payload[kPayloadBytes];
1117 int16_t decoded[kBlockSize16kHz];
1118 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1119 for (int n = 0; n < kPayloadBytes; ++n) {
1120 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1122 // Insert some packets which decode to noise. We are not interested in
1123 // actual decoded values.
1124 NetEqOutputType output_type;
1126 int samples_per_channel;
1127 uint32_t receive_timestamp = 0;
1128 for (int n = 0; n < 100; ++n) {
1129 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1130 receive_timestamp));
1131 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1132 &samples_per_channel, &num_channels,
1134 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1135 ASSERT_EQ(1, num_channels);
1137 rtp_info.header.sequenceNumber++;
1138 rtp_info.header.timestamp += kBlockSize16kHz;
1139 receive_timestamp += kBlockSize16kHz;
1141 const int kNumSyncPackets = 10;
1143 // Make sure sufficient number of sync packets are inserted that we can
1145 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
1146 // Insert sync-packets, the decoded sequence should be all-zero.
1147 for (int n = 0; n < kNumSyncPackets; ++n) {
1148 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1149 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1150 &samples_per_channel, &num_channels,
1152 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1153 ASSERT_EQ(1, num_channels);
1154 if (n > algorithmic_frame_delay) {
1155 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1157 rtp_info.header.sequenceNumber++;
1158 rtp_info.header.timestamp += kBlockSize16kHz;
1159 receive_timestamp += kBlockSize16kHz;
1162 // We insert regular packets, if sync packet are not correctly buffered then
1163 // network statistics would show some packet loss.
1164 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1165 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1166 receive_timestamp));
1167 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1168 &samples_per_channel, &num_channels,
1170 if (n >= algorithmic_frame_delay + 1) {
1171 // Expect that this frame contain samples from regular RTP.
1172 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1174 rtp_info.header.sequenceNumber++;
1175 rtp_info.header.timestamp += kBlockSize16kHz;
1176 receive_timestamp += kBlockSize16kHz;
1178 NetEqNetworkStatistics network_stats;
1179 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1180 // Expecting a "clean" network.
1181 EXPECT_EQ(0, network_stats.packet_loss_rate);
1182 EXPECT_EQ(0, network_stats.expand_rate);
1183 EXPECT_EQ(0, network_stats.accelerate_rate);
1184 EXPECT_LE(network_stats.preemptive_rate, 150);
1187 // Test if the size of the packet buffer reported correctly when containing
1188 // sync packets. Also, test if network packets override sync packets. That is to
1189 // prefer decoding a network packet to a sync packet, if both have same sequence
1190 // number and timestamp.
1191 TEST_F(NetEqDecodingTest,
1192 DISABLED_ON_ANDROID(SyncPacketBufferSizeAndOverridenByNetworkPackets)) {
1193 WebRtcRTPHeader rtp_info;
1194 PopulateRtpInfo(0, 0, &rtp_info);
1195 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1196 uint8_t payload[kPayloadBytes];
1197 int16_t decoded[kBlockSize16kHz];
1198 for (int n = 0; n < kPayloadBytes; ++n) {
1199 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1201 // Insert some packets which decode to noise. We are not interested in
1202 // actual decoded values.
1203 NetEqOutputType output_type;
1205 int samples_per_channel;
1206 uint32_t receive_timestamp = 0;
1207 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1208 for (int n = 0; n < algorithmic_frame_delay; ++n) {
1209 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1210 receive_timestamp));
1211 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1212 &samples_per_channel, &num_channels,
1214 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1215 ASSERT_EQ(1, num_channels);
1216 rtp_info.header.sequenceNumber++;
1217 rtp_info.header.timestamp += kBlockSize16kHz;
1218 receive_timestamp += kBlockSize16kHz;
1220 const int kNumSyncPackets = 10;
1222 WebRtcRTPHeader first_sync_packet_rtp_info;
1223 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1225 // Insert sync-packets, but no decoding.
1226 for (int n = 0; n < kNumSyncPackets; ++n) {
1227 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1228 rtp_info.header.sequenceNumber++;
1229 rtp_info.header.timestamp += kBlockSize16kHz;
1230 receive_timestamp += kBlockSize16kHz;
1232 NetEqNetworkStatistics network_stats;
1233 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1234 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1235 network_stats.current_buffer_size_ms);
1237 // Rewind |rtp_info| to that of the first sync packet.
1238 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1241 for (int n = 0; n < kNumSyncPackets; ++n) {
1242 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1243 receive_timestamp));
1244 rtp_info.header.sequenceNumber++;
1245 rtp_info.header.timestamp += kBlockSize16kHz;
1246 receive_timestamp += kBlockSize16kHz;
1250 for (int n = 0; n < kNumSyncPackets; ++n) {
1251 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1252 &samples_per_channel, &num_channels,
1254 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1255 ASSERT_EQ(1, num_channels);
1256 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1260 void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1261 uint32_t start_timestamp,
1262 const std::set<uint16_t>& drop_seq_numbers,
1263 bool expect_seq_no_wrap,
1264 bool expect_timestamp_wrap) {
1265 uint16_t seq_no = start_seq_no;
1266 uint32_t timestamp = start_timestamp;
1267 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1268 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1269 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1270 const int kPayloadBytes = kSamples * sizeof(int16_t);
1271 double next_input_time_ms = 0.0;
1272 int16_t decoded[kBlockSize16kHz];
1274 int samples_per_channel;
1275 NetEqOutputType output_type;
1276 uint32_t receive_timestamp = 0;
1278 // Insert speech for 2 seconds.
1279 const int kSpeechDurationMs = 2000;
1280 int packets_inserted = 0;
1281 uint16_t last_seq_no;
1282 uint32_t last_timestamp;
1283 bool timestamp_wrapped = false;
1284 bool seq_no_wrapped = false;
1285 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1286 // Each turn in this for loop is 10 ms.
1287 while (next_input_time_ms <= t_ms) {
1288 // Insert one 30 ms speech frame.
1289 uint8_t payload[kPayloadBytes] = {0};
1290 WebRtcRTPHeader rtp_info;
1291 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1292 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1293 // This sequence number was not in the set to drop. Insert it.
1295 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1296 receive_timestamp));
1299 NetEqNetworkStatistics network_stats;
1300 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1302 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1303 // packet size for first few packets. Therefore we refrain from checking
1305 if (packets_inserted > 4) {
1306 // Expect preferred and actual buffer size to be no more than 2 frames.
1307 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
1308 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1309 algorithmic_delay_ms_);
1311 last_seq_no = seq_no;
1312 last_timestamp = timestamp;
1315 timestamp += kSamples;
1316 receive_timestamp += kSamples;
1317 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1319 seq_no_wrapped |= seq_no < last_seq_no;
1320 timestamp_wrapped |= timestamp < last_timestamp;
1322 // Pull out data once.
1323 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1324 &samples_per_channel, &num_channels,
1326 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1327 ASSERT_EQ(1, num_channels);
1329 // Expect delay (in samples) to be less than 2 packets.
1330 EXPECT_LE(timestamp - neteq_->PlayoutTimestamp(),
1331 static_cast<uint32_t>(kSamples * 2));
1333 // Make sure we have actually tested wrap-around.
1334 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1335 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1338 TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1339 // Start with a sequence number that will soon wrap.
1340 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1341 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1344 TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1345 // Start with a sequence number that will soon wrap.
1346 std::set<uint16_t> drop_seq_numbers;
1347 drop_seq_numbers.insert(0xFFFF);
1348 drop_seq_numbers.insert(0x0);
1349 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1352 TEST_F(NetEqDecodingTest, TimestampWrap) {
1353 // Start with a timestamp that will soon wrap.
1354 std::set<uint16_t> drop_seq_numbers;
1355 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1358 TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1359 // Start with a timestamp and a sequence number that will wrap at the same
1361 std::set<uint16_t> drop_seq_numbers;
1362 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1365 void NetEqDecodingTest::DuplicateCng() {
1366 uint16_t seq_no = 0;
1367 uint32_t timestamp = 0;
1368 const int kFrameSizeMs = 10;
1369 const int kSampleRateKhz = 16;
1370 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1371 const int kPayloadBytes = kSamples * 2;
1373 const int algorithmic_delay_samples = std::max(
1374 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
1375 // Insert three speech packet. Three are needed to get the frame length
1379 NetEqOutputType type;
1380 uint8_t payload[kPayloadBytes] = {0};
1381 WebRtcRTPHeader rtp_info;
1382 for (int i = 0; i < 3; ++i) {
1383 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1384 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1386 timestamp += kSamples;
1391 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1392 ASSERT_EQ(kBlockSize16kHz, out_len);
1394 // Verify speech output.
1395 EXPECT_EQ(kOutputNormal, type);
1397 // Insert same CNG packet twice.
1398 const int kCngPeriodMs = 100;
1399 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1401 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1402 // This is the first time this CNG packet is inserted.
1403 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1405 // Pull audio once and make sure CNG is played.
1408 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1409 ASSERT_EQ(kBlockSize16kHz, out_len);
1410 EXPECT_EQ(kOutputCNG, type);
1411 EXPECT_EQ(timestamp - algorithmic_delay_samples, neteq_->PlayoutTimestamp());
1413 // Insert the same CNG packet again. Note that at this point it is old, since
1414 // we have already decoded the first copy of it.
1415 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1417 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1418 // we have already pulled out CNG once.
1419 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1422 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1423 ASSERT_EQ(kBlockSize16kHz, out_len);
1424 EXPECT_EQ(kOutputCNG, type);
1425 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1426 neteq_->PlayoutTimestamp());
1429 // Insert speech again.
1431 timestamp += kCngPeriodSamples;
1432 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1433 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1435 // Pull audio once and verify that the output is speech again.
1438 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1439 ASSERT_EQ(kBlockSize16kHz, out_len);
1440 EXPECT_EQ(kOutputNormal, type);
1441 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
1442 neteq_->PlayoutTimestamp());
1445 TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
1446 } // namespace webrtc