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/neteq/interface/neteq.h"
19 #include <string.h> // memset
26 #include "gflags/gflags.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
29 #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
30 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
31 #include "webrtc/test/testsupport/fileutils.h"
32 #include "webrtc/test/testsupport/gtest_disable.h"
33 #include "webrtc/typedefs.h"
35 DEFINE_bool(gen_ref, false, "Generate reference files.");
39 static bool IsAllZero(const int16_t* buf, int buf_length) {
41 for (int n = 0; n < buf_length && all_zero; ++n)
42 all_zero = buf[n] == 0;
46 static bool IsAllNonZero(const int16_t* buf, int buf_length) {
47 bool all_non_zero = true;
48 for (int n = 0; n < buf_length && all_non_zero; ++n)
49 all_non_zero = buf[n] != 0;
55 RefFiles(const std::string& input_file, const std::string& output_file);
57 template<class T> void ProcessReference(const T& test_results);
58 template<typename T, size_t n> void ProcessReference(
59 const T (&test_results)[n],
61 template<typename T, size_t n> void WriteToFile(
62 const T (&test_results)[n],
64 template<typename T, size_t n> void ReadFromFileAndCompare(
65 const T (&test_results)[n],
67 void WriteToFile(const NetEqNetworkStatistics& stats);
68 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
69 void WriteToFile(const RtcpStatistics& stats);
70 void ReadFromFileAndCompare(const RtcpStatistics& stats);
76 RefFiles::RefFiles(const std::string &input_file,
77 const std::string &output_file)
80 if (!input_file.empty()) {
81 input_fp_ = fopen(input_file.c_str(), "rb");
82 EXPECT_TRUE(input_fp_ != NULL);
84 if (!output_file.empty()) {
85 output_fp_ = fopen(output_file.c_str(), "wb");
86 EXPECT_TRUE(output_fp_ != NULL);
90 RefFiles::~RefFiles() {
92 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
95 if (output_fp_) fclose(output_fp_);
99 void RefFiles::ProcessReference(const T& test_results) {
100 WriteToFile(test_results);
101 ReadFromFileAndCompare(test_results);
104 template<typename T, size_t n>
105 void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
106 WriteToFile(test_results, length);
107 ReadFromFileAndCompare(test_results, length);
110 template<typename T, size_t n>
111 void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
113 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
117 template<typename T, size_t n>
118 void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
121 // Read from ref file.
122 T* ref = new T[length];
123 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
125 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
130 void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
132 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
137 void RefFiles::ReadFromFileAndCompare(
138 const NetEqNetworkStatistics& stats) {
140 // Read from ref file.
141 size_t stat_size = sizeof(NetEqNetworkStatistics);
142 NetEqNetworkStatistics ref_stats;
143 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
145 ASSERT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
149 void RefFiles::WriteToFile(const RtcpStatistics& stats) {
151 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
153 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
154 sizeof(stats.cumulative_lost), 1, output_fp_));
155 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
156 sizeof(stats.extended_max_sequence_number), 1,
158 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
163 void RefFiles::ReadFromFileAndCompare(
164 const RtcpStatistics& stats) {
166 // Read from ref file.
167 RtcpStatistics ref_stats;
168 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
169 sizeof(ref_stats.fraction_lost), 1, input_fp_));
170 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
171 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
172 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
173 sizeof(ref_stats.extended_max_sequence_number), 1,
175 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
178 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
179 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
180 ASSERT_EQ(ref_stats.extended_max_sequence_number,
181 stats.extended_max_sequence_number);
182 ASSERT_EQ(ref_stats.jitter, stats.jitter);
186 class NetEqDecodingTest : public ::testing::Test {
188 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
189 // constants below can be changed.
190 static const int kTimeStepMs = 10;
191 static const int kBlockSize8kHz = kTimeStepMs * 8;
192 static const int kBlockSize16kHz = kTimeStepMs * 16;
193 static const int kBlockSize32kHz = kTimeStepMs * 32;
194 static const int kMaxBlockSize = kBlockSize32kHz;
195 static const int kInitSampleRateHz = 8000;
198 virtual void SetUp();
199 virtual void TearDown();
200 void SelectDecoders(NetEqDecoder* used_codec);
202 void OpenInputFile(const std::string &rtp_file);
203 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
204 void DecodeAndCompare(const std::string& rtp_file,
205 const std::string& ref_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 WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
218 const std::set<uint16_t>& drop_seq_numbers,
219 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
221 void LongCngWithClockDrift(double drift_factor,
222 double network_freeze_ms,
223 bool pull_audio_during_freeze,
224 int delay_tolerance_ms,
225 int max_time_to_speech_ms);
229 uint32_t PlayoutTimestamp();
232 NetEq::Config config_;
234 unsigned int sim_clock_;
235 int16_t out_data_[kMaxBlockSize];
236 int output_sample_rate_;
237 int algorithmic_delay_ms_;
240 // Allocating the static const so that it can be passed by reference.
241 const int NetEqDecodingTest::kTimeStepMs;
242 const int NetEqDecodingTest::kBlockSize8kHz;
243 const int NetEqDecodingTest::kBlockSize16kHz;
244 const int NetEqDecodingTest::kBlockSize32kHz;
245 const int NetEqDecodingTest::kMaxBlockSize;
246 const int NetEqDecodingTest::kInitSampleRateHz;
248 NetEqDecodingTest::NetEqDecodingTest()
253 output_sample_rate_(kInitSampleRateHz),
254 algorithmic_delay_ms_(0) {
255 config_.sample_rate_hz = kInitSampleRateHz;
256 memset(out_data_, 0, sizeof(out_data_));
259 void NetEqDecodingTest::SetUp() {
260 neteq_ = NetEq::Create(config_);
261 NetEqNetworkStatistics stat;
262 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
263 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
268 void NetEqDecodingTest::TearDown() {
274 void NetEqDecodingTest::LoadDecoders() {
276 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
278 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
279 #ifndef WEBRTC_ANDROID
281 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
282 #endif // WEBRTC_ANDROID
284 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
285 #ifndef WEBRTC_ANDROID
287 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
289 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
290 #endif // WEBRTC_ANDROID
292 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
294 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
295 // Load PCM16B swb32.
296 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
298 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
300 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
303 void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
304 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
305 ASSERT_TRUE(rtp_fp_ != NULL);
306 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
309 void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
310 // Check if time to receive.
311 while ((sim_clock_ >= rtp->time()) &&
312 (rtp->dataLen() >= 0)) {
313 if (rtp->dataLen() > 0) {
314 WebRtcRTPHeader rtpInfo;
315 rtp->parseHeader(&rtpInfo);
316 ASSERT_EQ(0, neteq_->InsertPacket(
320 rtp->time() * (output_sample_rate_ / 1000)));
323 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
326 // Get audio from NetEq.
327 NetEqOutputType type;
329 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
330 &num_channels, &type));
331 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
332 (*out_len == kBlockSize16kHz) ||
333 (*out_len == kBlockSize32kHz));
334 output_sample_rate_ = *out_len / 10 * 1000;
337 sim_clock_ += kTimeStepMs;
340 void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
341 const std::string& ref_file,
342 const std::string& stat_ref_file,
343 const std::string& rtcp_ref_file) {
344 OpenInputFile(rtp_file);
346 std::string ref_out_file = "";
347 if (ref_file.empty()) {
348 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
350 RefFiles ref_files(ref_file, ref_out_file);
352 std::string stat_out_file = "";
353 if (stat_ref_file.empty()) {
354 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
356 RefFiles network_stat_files(stat_ref_file, stat_out_file);
358 std::string rtcp_out_file = "";
359 if (rtcp_ref_file.empty()) {
360 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
362 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
364 NETEQTEST_RTPpacket rtp;
365 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
367 while (rtp.dataLen() >= 0) {
368 std::ostringstream ss;
369 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
370 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
372 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
373 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
375 // Query the network statistics API once per second
376 if (sim_clock_ % 1000 == 0) {
377 // Process NetworkStatistics.
378 NetEqNetworkStatistics network_stats;
379 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
380 ASSERT_NO_FATAL_FAILURE(
381 network_stat_files.ProcessReference(network_stats));
384 RtcpStatistics rtcp_stats;
385 neteq_->GetRtcpStatistics(&rtcp_stats);
386 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
391 void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
393 WebRtcRTPHeader* rtp_info) {
394 rtp_info->header.sequenceNumber = frame_index;
395 rtp_info->header.timestamp = timestamp;
396 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
397 rtp_info->header.payloadType = 94; // PCM16b WB codec.
398 rtp_info->header.markerBit = 0;
401 void NetEqDecodingTest::PopulateCng(int frame_index,
403 WebRtcRTPHeader* rtp_info,
406 rtp_info->header.sequenceNumber = frame_index;
407 rtp_info->header.timestamp = timestamp;
408 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
409 rtp_info->header.payloadType = 98; // WB CNG.
410 rtp_info->header.markerBit = 0;
411 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
412 *payload_len = 1; // Only noise level, no spectral parameters.
415 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestBitExactness)) {
416 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
417 "resources/audio_coding/neteq_universal_new.rtp";
418 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
419 // are identical. The latter could have been removed, but if clients still
420 // have a copy of the file, the test will fail.
421 const std::string input_ref_file =
422 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
423 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
424 // For Visual Studio 2012 and later, we will have to use the generic reference
425 // file, rather than the windows-specific one.
426 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
427 "resources/audio_coding/neteq4_network_stats.dat";
429 const std::string network_stat_ref_file =
430 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
432 const std::string rtcp_stat_ref_file =
433 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
436 DecodeAndCompare(input_rtp_file, "", "", "");
438 DecodeAndCompare(input_rtp_file,
440 network_stat_ref_file,
445 // Use fax mode to avoid time-scaling. This is to simplify the testing of
446 // packet waiting times in the packet buffer.
447 class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
449 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
450 config_.playout_mode = kPlayoutFax;
454 TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
455 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
456 size_t num_frames = 30;
457 const int kSamples = 10 * 16;
458 const int kPayloadBytes = kSamples * 2;
459 for (size_t i = 0; i < num_frames; ++i) {
460 uint16_t payload[kSamples] = {0};
461 WebRtcRTPHeader rtp_info;
462 rtp_info.header.sequenceNumber = i;
463 rtp_info.header.timestamp = i * kSamples;
464 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
465 rtp_info.header.payloadType = 94; // PCM16b WB codec.
466 rtp_info.header.markerBit = 0;
467 ASSERT_EQ(0, neteq_->InsertPacket(
469 reinterpret_cast<uint8_t*>(payload),
472 // Pull out all data.
473 for (size_t i = 0; i < num_frames; ++i) {
476 NetEqOutputType type;
477 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
478 &num_channels, &type));
479 ASSERT_EQ(kBlockSize16kHz, out_len);
482 std::vector<int> waiting_times;
483 neteq_->WaitingTimes(&waiting_times);
484 EXPECT_EQ(num_frames, waiting_times.size());
485 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
486 // spacing (per definition), we expect the delay to increase with 10 ms for
488 for (size_t i = 0; i < waiting_times.size(); ++i) {
489 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
492 // Check statistics again and make sure it's been reset.
493 neteq_->WaitingTimes(&waiting_times);
494 int len = waiting_times.size();
497 // Process > 100 frames, and make sure that that we get statistics
498 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
500 for (size_t i = 0; i < num_frames; ++i) {
501 uint16_t payload[kSamples] = {0};
502 WebRtcRTPHeader rtp_info;
503 rtp_info.header.sequenceNumber = i;
504 rtp_info.header.timestamp = i * kSamples;
505 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
506 rtp_info.header.payloadType = 94; // PCM16b WB codec.
507 rtp_info.header.markerBit = 0;
508 ASSERT_EQ(0, neteq_->InsertPacket(
510 reinterpret_cast<uint8_t*>(payload),
514 NetEqOutputType type;
515 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
516 &num_channels, &type));
517 ASSERT_EQ(kBlockSize16kHz, out_len);
520 neteq_->WaitingTimes(&waiting_times);
521 EXPECT_EQ(100u, waiting_times.size());
524 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
525 const int kNumFrames = 3000; // Needed for convergence.
527 const int kSamples = 10 * 16;
528 const int kPayloadBytes = kSamples * 2;
529 while (frame_index < kNumFrames) {
530 // Insert one packet each time, except every 10th time where we insert two
531 // packets at once. This will create a negative clock-drift of approx. 10%.
532 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
533 for (int n = 0; n < num_packets; ++n) {
534 uint8_t payload[kPayloadBytes] = {0};
535 WebRtcRTPHeader rtp_info;
536 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
537 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
541 // Pull out data once.
544 NetEqOutputType type;
545 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
546 &num_channels, &type));
547 ASSERT_EQ(kBlockSize16kHz, out_len);
550 NetEqNetworkStatistics network_stats;
551 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
552 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
555 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
556 const int kNumFrames = 5000; // Needed for convergence.
558 const int kSamples = 10 * 16;
559 const int kPayloadBytes = kSamples * 2;
560 for (int i = 0; i < kNumFrames; ++i) {
561 // Insert one packet each time, except every 10th time where we don't insert
562 // any packet. This will create a positive clock-drift of approx. 11%.
563 int num_packets = (i % 10 == 9 ? 0 : 1);
564 for (int n = 0; n < num_packets; ++n) {
565 uint8_t payload[kPayloadBytes] = {0};
566 WebRtcRTPHeader rtp_info;
567 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
568 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
572 // Pull out data once.
575 NetEqOutputType type;
576 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
577 &num_channels, &type));
578 ASSERT_EQ(kBlockSize16kHz, out_len);
581 NetEqNetworkStatistics network_stats;
582 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
583 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
586 void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
587 double network_freeze_ms,
588 bool pull_audio_during_freeze,
589 int delay_tolerance_ms,
590 int max_time_to_speech_ms) {
592 uint32_t timestamp = 0;
593 const int kFrameSizeMs = 30;
594 const int kSamples = kFrameSizeMs * 16;
595 const int kPayloadBytes = kSamples * 2;
596 double next_input_time_ms = 0.0;
600 NetEqOutputType type;
602 // Insert speech for 5 seconds.
603 const int kSpeechDurationMs = 5000;
604 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
605 // Each turn in this for loop is 10 ms.
606 while (next_input_time_ms <= t_ms) {
607 // Insert one 30 ms speech frame.
608 uint8_t payload[kPayloadBytes] = {0};
609 WebRtcRTPHeader rtp_info;
610 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
611 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
613 timestamp += kSamples;
614 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
616 // Pull out data once.
617 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
618 &num_channels, &type));
619 ASSERT_EQ(kBlockSize16kHz, out_len);
622 EXPECT_EQ(kOutputNormal, type);
623 int32_t delay_before = timestamp - PlayoutTimestamp();
625 // Insert CNG for 1 minute (= 60000 ms).
626 const int kCngPeriodMs = 100;
627 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
628 const int kCngDurationMs = 60000;
629 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
630 // Each turn in this for loop is 10 ms.
631 while (next_input_time_ms <= t_ms) {
632 // Insert one CNG frame each 100 ms.
633 uint8_t payload[kPayloadBytes];
635 WebRtcRTPHeader rtp_info;
636 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
637 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
639 timestamp += kCngPeriodSamples;
640 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
642 // Pull out data once.
643 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
644 &num_channels, &type));
645 ASSERT_EQ(kBlockSize16kHz, out_len);
648 EXPECT_EQ(kOutputCNG, type);
650 if (network_freeze_ms > 0) {
651 // First keep pulling audio for |network_freeze_ms| without inserting
652 // any data, then insert CNG data corresponding to |network_freeze_ms|
653 // without pulling any output audio.
654 const double loop_end_time = t_ms + network_freeze_ms;
655 for (; t_ms < loop_end_time; t_ms += 10) {
656 // Pull out data once.
659 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
660 ASSERT_EQ(kBlockSize16kHz, out_len);
661 EXPECT_EQ(kOutputCNG, type);
663 bool pull_once = pull_audio_during_freeze;
664 // If |pull_once| is true, GetAudio will be called once half-way through
665 // the network recovery period.
666 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
667 while (next_input_time_ms <= t_ms) {
668 if (pull_once && next_input_time_ms >= pull_time_ms) {
670 // Pull out data once.
674 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
675 ASSERT_EQ(kBlockSize16kHz, out_len);
676 EXPECT_EQ(kOutputCNG, type);
679 // Insert one CNG frame each 100 ms.
680 uint8_t payload[kPayloadBytes];
682 WebRtcRTPHeader rtp_info;
683 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
684 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
686 timestamp += kCngPeriodSamples;
687 next_input_time_ms += kCngPeriodMs * drift_factor;
691 // Insert speech again until output type is speech.
692 double speech_restart_time_ms = t_ms;
693 while (type != kOutputNormal) {
694 // Each turn in this for loop is 10 ms.
695 while (next_input_time_ms <= t_ms) {
696 // Insert one 30 ms speech frame.
697 uint8_t payload[kPayloadBytes] = {0};
698 WebRtcRTPHeader rtp_info;
699 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
700 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
702 timestamp += kSamples;
703 next_input_time_ms += kFrameSizeMs * drift_factor;
705 // Pull out data once.
706 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
707 &num_channels, &type));
708 ASSERT_EQ(kBlockSize16kHz, out_len);
713 // Check that the speech starts again within reasonable time.
714 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
715 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
716 int32_t delay_after = timestamp - PlayoutTimestamp();
717 // Compare delay before and after, and make sure it differs less than 20 ms.
718 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
719 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
722 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
723 // Apply a clock drift of -25 ms / s (sender faster than receiver).
724 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
725 const double kNetworkFreezeTimeMs = 0.0;
726 const bool kGetAudioDuringFreezeRecovery = false;
727 const int kDelayToleranceMs = 20;
728 const int kMaxTimeToSpeechMs = 100;
729 LongCngWithClockDrift(kDriftFactor,
730 kNetworkFreezeTimeMs,
731 kGetAudioDuringFreezeRecovery,
736 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
737 // Apply a clock drift of +25 ms / s (sender slower than receiver).
738 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
739 const double kNetworkFreezeTimeMs = 0.0;
740 const bool kGetAudioDuringFreezeRecovery = false;
741 const int kDelayToleranceMs = 20;
742 const int kMaxTimeToSpeechMs = 100;
743 LongCngWithClockDrift(kDriftFactor,
744 kNetworkFreezeTimeMs,
745 kGetAudioDuringFreezeRecovery,
750 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
751 // Apply a clock drift of -25 ms / s (sender faster than receiver).
752 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
753 const double kNetworkFreezeTimeMs = 5000.0;
754 const bool kGetAudioDuringFreezeRecovery = false;
755 const int kDelayToleranceMs = 50;
756 const int kMaxTimeToSpeechMs = 200;
757 LongCngWithClockDrift(kDriftFactor,
758 kNetworkFreezeTimeMs,
759 kGetAudioDuringFreezeRecovery,
764 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
765 // Apply a clock drift of +25 ms / s (sender slower than receiver).
766 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
767 const double kNetworkFreezeTimeMs = 5000.0;
768 const bool kGetAudioDuringFreezeRecovery = false;
769 const int kDelayToleranceMs = 20;
770 const int kMaxTimeToSpeechMs = 100;
771 LongCngWithClockDrift(kDriftFactor,
772 kNetworkFreezeTimeMs,
773 kGetAudioDuringFreezeRecovery,
778 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
779 // Apply a clock drift of +25 ms / s (sender slower than receiver).
780 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
781 const double kNetworkFreezeTimeMs = 5000.0;
782 const bool kGetAudioDuringFreezeRecovery = true;
783 const int kDelayToleranceMs = 20;
784 const int kMaxTimeToSpeechMs = 100;
785 LongCngWithClockDrift(kDriftFactor,
786 kNetworkFreezeTimeMs,
787 kGetAudioDuringFreezeRecovery,
792 TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
793 const double kDriftFactor = 1.0; // No drift.
794 const double kNetworkFreezeTimeMs = 0.0;
795 const bool kGetAudioDuringFreezeRecovery = false;
796 const int kDelayToleranceMs = 10;
797 const int kMaxTimeToSpeechMs = 50;
798 LongCngWithClockDrift(kDriftFactor,
799 kNetworkFreezeTimeMs,
800 kGetAudioDuringFreezeRecovery,
805 TEST_F(NetEqDecodingTest, UnknownPayloadType) {
806 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());
816 TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
817 const int kPayloadBytes = 100;
818 uint8_t payload[kPayloadBytes] = {0};
819 WebRtcRTPHeader rtp_info;
820 PopulateRtpInfo(0, 0, &rtp_info);
821 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
822 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
823 NetEqOutputType type;
824 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
826 for (int i = 0; i < kMaxBlockSize; ++i) {
830 int samples_per_channel;
831 EXPECT_EQ(NetEq::kFail,
832 neteq_->GetAudio(kMaxBlockSize, out_data_,
833 &samples_per_channel, &num_channels, &type));
834 // Verify that there is a decoder error to check.
835 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
836 // Code 6730 is an iSAC error code.
837 EXPECT_EQ(6730, neteq_->LastDecoderError());
838 // Verify that the first 160 samples are set to 0, and that the remaining
839 // samples are left unmodified.
840 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
841 for (int i = 0; i < kExpectedOutputLength; ++i) {
842 std::ostringstream ss;
844 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
845 EXPECT_EQ(0, out_data_[i]);
847 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
848 std::ostringstream ss;
850 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
851 EXPECT_EQ(1, out_data_[i]);
855 TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
856 NetEqOutputType type;
857 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
859 for (int i = 0; i < kMaxBlockSize; ++i) {
863 int samples_per_channel;
864 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
865 &samples_per_channel,
866 &num_channels, &type));
867 // Verify that the first block of samples is set to 0.
868 static const int kExpectedOutputLength =
869 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
870 for (int i = 0; i < kExpectedOutputLength; ++i) {
871 std::ostringstream ss;
873 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
874 EXPECT_EQ(0, out_data_[i]);
878 class NetEqBgnTest : public NetEqDecodingTest {
880 virtual void TestCondition(double sum_squared_noise,
881 bool should_be_faded) = 0;
883 void CheckBgn(int sampling_rate_hz) {
884 int expected_samples_per_channel = 0;
885 uint8_t payload_type = 0xFF; // Invalid.
886 if (sampling_rate_hz == 8000) {
887 expected_samples_per_channel = kBlockSize8kHz;
888 payload_type = 93; // PCM 16, 8 kHz.
889 } else if (sampling_rate_hz == 16000) {
890 expected_samples_per_channel = kBlockSize16kHz;
891 payload_type = 94; // PCM 16, 16 kHZ.
892 } else if (sampling_rate_hz == 32000) {
893 expected_samples_per_channel = kBlockSize32kHz;
894 payload_type = 95; // PCM 16, 32 kHz.
896 ASSERT_TRUE(false); // Unsupported test case.
899 NetEqOutputType type;
900 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
901 test::AudioLoop input;
902 // We are using the same 32 kHz input file for all tests, regardless of
903 // |sampling_rate_hz|. The output may sound weird, but the test is still
905 ASSERT_TRUE(input.Init(
906 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
907 10 * sampling_rate_hz, // Max 10 seconds loop length.
908 expected_samples_per_channel));
910 // Payload of 10 ms of PCM16 32 kHz.
911 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
912 WebRtcRTPHeader rtp_info;
913 PopulateRtpInfo(0, 0, &rtp_info);
914 rtp_info.header.payloadType = payload_type;
916 int number_channels = 0;
917 int samples_per_channel = 0;
919 uint32_t receive_timestamp = 0;
920 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
922 WebRtcPcm16b_EncodeW16(input.GetNextBlock(),
923 expected_samples_per_channel,
924 reinterpret_cast<int16_t*>(payload));
925 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
928 samples_per_channel = 0;
930 neteq_->InsertPacket(
931 rtp_info, payload, enc_len_bytes, receive_timestamp));
933 neteq_->GetAudio(kBlockSize32kHz,
935 &samples_per_channel,
938 ASSERT_EQ(1, number_channels);
939 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
940 ASSERT_EQ(kOutputNormal, type);
943 rtp_info.header.timestamp += expected_samples_per_channel;
944 rtp_info.header.sequenceNumber++;
945 receive_timestamp += expected_samples_per_channel;
949 samples_per_channel = 0;
951 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
952 // one frame without checking speech-type. This is the first frame pulled
953 // without inserting any packet, and might not be labeled as PLC.
955 neteq_->GetAudio(kBlockSize32kHz,
957 &samples_per_channel,
960 ASSERT_EQ(1, number_channels);
961 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
963 // To be able to test the fading of background noise we need at lease to
965 const int kFadingThreshold = 611;
967 // Test several CNG-to-PLC packet for the expected behavior. The number 20
968 // is arbitrary, but sufficiently large to test enough number of frames.
969 const int kNumPlcToCngTestFrames = 20;
970 bool plc_to_cng = false;
971 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
973 samples_per_channel = 0;
974 memset(output, 1, sizeof(output)); // Set to non-zero.
976 neteq_->GetAudio(kBlockSize32kHz,
978 &samples_per_channel,
981 ASSERT_EQ(1, number_channels);
982 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
983 if (type == kOutputPLCtoCNG) {
985 double sum_squared = 0;
986 for (int k = 0; k < number_channels * samples_per_channel; ++k)
987 sum_squared += output[k] * output[k];
988 TestCondition(sum_squared, n > kFadingThreshold);
990 EXPECT_EQ(kOutputPLC, type);
993 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
997 class NetEqBgnTestOn : public NetEqBgnTest {
999 NetEqBgnTestOn() : NetEqBgnTest() {
1000 config_.background_noise_mode = NetEq::kBgnOn;
1003 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1004 EXPECT_NE(0, sum_squared_noise);
1008 class NetEqBgnTestOff : public NetEqBgnTest {
1010 NetEqBgnTestOff() : NetEqBgnTest() {
1011 config_.background_noise_mode = NetEq::kBgnOff;
1014 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1015 EXPECT_EQ(0, sum_squared_noise);
1019 class NetEqBgnTestFade : public NetEqBgnTest {
1021 NetEqBgnTestFade() : NetEqBgnTest() {
1022 config_.background_noise_mode = NetEq::kBgnFade;
1025 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1026 if (should_be_faded)
1027 EXPECT_EQ(0, sum_squared_noise);
1031 TEST_F(NetEqBgnTestOn, RunTest) {
1037 TEST_F(NetEqBgnTestOff, RunTest) {
1043 TEST_F(NetEqBgnTestFade, RunTest) {
1049 TEST_F(NetEqDecodingTest, SyncPacketInsert) {
1050 WebRtcRTPHeader rtp_info;
1051 uint32_t receive_timestamp = 0;
1052 // For the readability use the following payloads instead of the defaults of
1054 uint8_t kPcm16WbPayloadType = 1;
1055 uint8_t kCngNbPayloadType = 2;
1056 uint8_t kCngWbPayloadType = 3;
1057 uint8_t kCngSwb32PayloadType = 4;
1058 uint8_t kCngSwb48PayloadType = 5;
1059 uint8_t kAvtPayloadType = 6;
1060 uint8_t kRedPayloadType = 7;
1061 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1063 // Register decoders.
1064 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1065 kPcm16WbPayloadType));
1066 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1067 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1068 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1069 kCngSwb32PayloadType));
1070 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1071 kCngSwb48PayloadType));
1072 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1073 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1074 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1076 PopulateRtpInfo(0, 0, &rtp_info);
1077 rtp_info.header.payloadType = kPcm16WbPayloadType;
1079 // The first packet injected cannot be sync-packet.
1080 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1082 // Payload length of 10 ms PCM16 16 kHz.
1083 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1084 uint8_t payload[kPayloadBytes] = {0};
1085 ASSERT_EQ(0, neteq_->InsertPacket(
1086 rtp_info, payload, kPayloadBytes, receive_timestamp));
1088 // Next packet. Last packet contained 10 ms audio.
1089 rtp_info.header.sequenceNumber++;
1090 rtp_info.header.timestamp += kBlockSize16kHz;
1091 receive_timestamp += kBlockSize16kHz;
1093 // Unacceptable payload types CNG, AVT (DTMF), RED.
1094 rtp_info.header.payloadType = kCngNbPayloadType;
1095 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1097 rtp_info.header.payloadType = kCngWbPayloadType;
1098 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1100 rtp_info.header.payloadType = kCngSwb32PayloadType;
1101 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1103 rtp_info.header.payloadType = kCngSwb48PayloadType;
1104 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1106 rtp_info.header.payloadType = kAvtPayloadType;
1107 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1109 rtp_info.header.payloadType = kRedPayloadType;
1110 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1112 // Change of codec cannot be initiated with a sync packet.
1113 rtp_info.header.payloadType = kIsacPayloadType;
1114 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1116 // Change of SSRC is not allowed with a sync packet.
1117 rtp_info.header.payloadType = kPcm16WbPayloadType;
1118 ++rtp_info.header.ssrc;
1119 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1121 --rtp_info.header.ssrc;
1122 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1125 // First insert several noise like packets, then sync-packets. Decoding all
1126 // packets should not produce error, statistics should not show any packet loss
1127 // and sync-packets should decode to zero.
1128 // TODO(turajs) we will have a better test if we have a referece NetEq, and
1129 // when Sync packets are inserted in "test" NetEq we insert all-zero payload
1130 // in reference NetEq and compare the output of those two.
1131 TEST_F(NetEqDecodingTest, SyncPacketDecode) {
1132 WebRtcRTPHeader rtp_info;
1133 PopulateRtpInfo(0, 0, &rtp_info);
1134 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1135 uint8_t payload[kPayloadBytes];
1136 int16_t decoded[kBlockSize16kHz];
1137 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1138 for (int n = 0; n < kPayloadBytes; ++n) {
1139 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1141 // Insert some packets which decode to noise. We are not interested in
1142 // actual decoded values.
1143 NetEqOutputType output_type;
1145 int samples_per_channel;
1146 uint32_t receive_timestamp = 0;
1147 for (int n = 0; n < 100; ++n) {
1148 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1149 receive_timestamp));
1150 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1151 &samples_per_channel, &num_channels,
1153 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1154 ASSERT_EQ(1, num_channels);
1156 rtp_info.header.sequenceNumber++;
1157 rtp_info.header.timestamp += kBlockSize16kHz;
1158 receive_timestamp += kBlockSize16kHz;
1160 const int kNumSyncPackets = 10;
1162 // Make sure sufficient number of sync packets are inserted that we can
1164 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
1165 // Insert sync-packets, the decoded sequence should be all-zero.
1166 for (int n = 0; n < kNumSyncPackets; ++n) {
1167 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1168 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1169 &samples_per_channel, &num_channels,
1171 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1172 ASSERT_EQ(1, num_channels);
1173 if (n > algorithmic_frame_delay) {
1174 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1176 rtp_info.header.sequenceNumber++;
1177 rtp_info.header.timestamp += kBlockSize16kHz;
1178 receive_timestamp += kBlockSize16kHz;
1181 // We insert regular packets, if sync packet are not correctly buffered then
1182 // network statistics would show some packet loss.
1183 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1184 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1185 receive_timestamp));
1186 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1187 &samples_per_channel, &num_channels,
1189 if (n >= algorithmic_frame_delay + 1) {
1190 // Expect that this frame contain samples from regular RTP.
1191 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1193 rtp_info.header.sequenceNumber++;
1194 rtp_info.header.timestamp += kBlockSize16kHz;
1195 receive_timestamp += kBlockSize16kHz;
1197 NetEqNetworkStatistics network_stats;
1198 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1199 // Expecting a "clean" network.
1200 EXPECT_EQ(0, network_stats.packet_loss_rate);
1201 EXPECT_EQ(0, network_stats.expand_rate);
1202 EXPECT_EQ(0, network_stats.accelerate_rate);
1203 EXPECT_LE(network_stats.preemptive_rate, 150);
1206 // Test if the size of the packet buffer reported correctly when containing
1207 // sync packets. Also, test if network packets override sync packets. That is to
1208 // prefer decoding a network packet to a sync packet, if both have same sequence
1209 // number and timestamp.
1210 TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
1211 WebRtcRTPHeader rtp_info;
1212 PopulateRtpInfo(0, 0, &rtp_info);
1213 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1214 uint8_t payload[kPayloadBytes];
1215 int16_t decoded[kBlockSize16kHz];
1216 for (int n = 0; n < kPayloadBytes; ++n) {
1217 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1219 // Insert some packets which decode to noise. We are not interested in
1220 // actual decoded values.
1221 NetEqOutputType output_type;
1223 int samples_per_channel;
1224 uint32_t receive_timestamp = 0;
1225 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1226 for (int n = 0; n < algorithmic_frame_delay; ++n) {
1227 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1228 receive_timestamp));
1229 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1230 &samples_per_channel, &num_channels,
1232 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1233 ASSERT_EQ(1, num_channels);
1234 rtp_info.header.sequenceNumber++;
1235 rtp_info.header.timestamp += kBlockSize16kHz;
1236 receive_timestamp += kBlockSize16kHz;
1238 const int kNumSyncPackets = 10;
1240 WebRtcRTPHeader first_sync_packet_rtp_info;
1241 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1243 // Insert sync-packets, but no decoding.
1244 for (int n = 0; n < kNumSyncPackets; ++n) {
1245 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1246 rtp_info.header.sequenceNumber++;
1247 rtp_info.header.timestamp += kBlockSize16kHz;
1248 receive_timestamp += kBlockSize16kHz;
1250 NetEqNetworkStatistics network_stats;
1251 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1252 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1253 network_stats.current_buffer_size_ms);
1255 // Rewind |rtp_info| to that of the first sync packet.
1256 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1259 for (int n = 0; n < kNumSyncPackets; ++n) {
1260 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1261 receive_timestamp));
1262 rtp_info.header.sequenceNumber++;
1263 rtp_info.header.timestamp += kBlockSize16kHz;
1264 receive_timestamp += kBlockSize16kHz;
1268 for (int n = 0; n < kNumSyncPackets; ++n) {
1269 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1270 &samples_per_channel, &num_channels,
1272 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1273 ASSERT_EQ(1, num_channels);
1274 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1278 void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1279 uint32_t start_timestamp,
1280 const std::set<uint16_t>& drop_seq_numbers,
1281 bool expect_seq_no_wrap,
1282 bool expect_timestamp_wrap) {
1283 uint16_t seq_no = start_seq_no;
1284 uint32_t timestamp = start_timestamp;
1285 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1286 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1287 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1288 const int kPayloadBytes = kSamples * sizeof(int16_t);
1289 double next_input_time_ms = 0.0;
1290 int16_t decoded[kBlockSize16kHz];
1292 int samples_per_channel;
1293 NetEqOutputType output_type;
1294 uint32_t receive_timestamp = 0;
1296 // Insert speech for 2 seconds.
1297 const int kSpeechDurationMs = 2000;
1298 int packets_inserted = 0;
1299 uint16_t last_seq_no;
1300 uint32_t last_timestamp;
1301 bool timestamp_wrapped = false;
1302 bool seq_no_wrapped = false;
1303 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1304 // Each turn in this for loop is 10 ms.
1305 while (next_input_time_ms <= t_ms) {
1306 // Insert one 30 ms speech frame.
1307 uint8_t payload[kPayloadBytes] = {0};
1308 WebRtcRTPHeader rtp_info;
1309 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1310 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1311 // This sequence number was not in the set to drop. Insert it.
1313 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1314 receive_timestamp));
1317 NetEqNetworkStatistics network_stats;
1318 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1320 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1321 // packet size for first few packets. Therefore we refrain from checking
1323 if (packets_inserted > 4) {
1324 // Expect preferred and actual buffer size to be no more than 2 frames.
1325 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
1326 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1327 algorithmic_delay_ms_);
1329 last_seq_no = seq_no;
1330 last_timestamp = timestamp;
1333 timestamp += kSamples;
1334 receive_timestamp += kSamples;
1335 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1337 seq_no_wrapped |= seq_no < last_seq_no;
1338 timestamp_wrapped |= timestamp < last_timestamp;
1340 // Pull out data once.
1341 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1342 &samples_per_channel, &num_channels,
1344 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1345 ASSERT_EQ(1, num_channels);
1347 // Expect delay (in samples) to be less than 2 packets.
1348 EXPECT_LE(timestamp - PlayoutTimestamp(),
1349 static_cast<uint32_t>(kSamples * 2));
1351 // Make sure we have actually tested wrap-around.
1352 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1353 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1356 TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1357 // Start with a sequence number that will soon wrap.
1358 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1359 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1362 TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1363 // Start with a sequence number that will soon wrap.
1364 std::set<uint16_t> drop_seq_numbers;
1365 drop_seq_numbers.insert(0xFFFF);
1366 drop_seq_numbers.insert(0x0);
1367 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1370 TEST_F(NetEqDecodingTest, TimestampWrap) {
1371 // Start with a timestamp that will soon wrap.
1372 std::set<uint16_t> drop_seq_numbers;
1373 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1376 TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1377 // Start with a timestamp and a sequence number that will wrap at the same
1379 std::set<uint16_t> drop_seq_numbers;
1380 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1383 void NetEqDecodingTest::DuplicateCng() {
1384 uint16_t seq_no = 0;
1385 uint32_t timestamp = 0;
1386 const int kFrameSizeMs = 10;
1387 const int kSampleRateKhz = 16;
1388 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1389 const int kPayloadBytes = kSamples * 2;
1391 const int algorithmic_delay_samples = std::max(
1392 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
1393 // Insert three speech packet. Three are needed to get the frame length
1397 NetEqOutputType type;
1398 uint8_t payload[kPayloadBytes] = {0};
1399 WebRtcRTPHeader rtp_info;
1400 for (int i = 0; i < 3; ++i) {
1401 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1402 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1404 timestamp += kSamples;
1409 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1410 ASSERT_EQ(kBlockSize16kHz, out_len);
1412 // Verify speech output.
1413 EXPECT_EQ(kOutputNormal, type);
1415 // Insert same CNG packet twice.
1416 const int kCngPeriodMs = 100;
1417 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1419 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1420 // This is the first time this CNG packet is inserted.
1421 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1423 // Pull audio once and make sure CNG is played.
1426 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1427 ASSERT_EQ(kBlockSize16kHz, out_len);
1428 EXPECT_EQ(kOutputCNG, type);
1429 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
1431 // Insert the same CNG packet again. Note that at this point it is old, since
1432 // we have already decoded the first copy of it.
1433 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1435 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1436 // we have already pulled out CNG once.
1437 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1440 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1441 ASSERT_EQ(kBlockSize16kHz, out_len);
1442 EXPECT_EQ(kOutputCNG, type);
1443 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1444 PlayoutTimestamp());
1447 // Insert speech again.
1449 timestamp += kCngPeriodSamples;
1450 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1451 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1453 // Pull audio once and verify that the output is speech again.
1456 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1457 ASSERT_EQ(kBlockSize16kHz, out_len);
1458 EXPECT_EQ(kOutputNormal, type);
1459 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
1460 PlayoutTimestamp());
1463 uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1464 uint32_t playout_timestamp = 0;
1465 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1466 return playout_timestamp;
1469 TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
1470 } // namespace webrtc