1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 /* 12 * This file includes unit tests for NetEQ. 13 */ 14 15 #include "webrtc/modules/audio_coding/neteq/include/neteq.h" 16 17 #include <math.h> 18 #include <stdlib.h> 19 #include <string.h> // memset 20 21 #include <algorithm> 22 #include <set> 23 #include <string> 24 #include <vector> 25 26 #include "gflags/gflags.h" 27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "webrtc/base/scoped_ptr.h" 29 #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h" 30 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" 31 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" 32 #include "webrtc/test/testsupport/fileutils.h" 33 #include "webrtc/typedefs.h" 34 35 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 36 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD 37 #include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h" 38 #else 39 #include "webrtc/audio_coding/neteq/neteq_unittest.pb.h" 40 #endif 41 #endif 42 43 DEFINE_bool(gen_ref, false, "Generate reference files."); 44 45 namespace { 46 47 bool IsAllZero(const int16_t* buf, size_t buf_length) { 48 bool all_zero = true; 49 for (size_t n = 0; n < buf_length && all_zero; ++n) 50 all_zero = buf[n] == 0; 51 return all_zero; 52 } 53 54 bool IsAllNonZero(const int16_t* buf, size_t buf_length) { 55 bool all_non_zero = true; 56 for (size_t n = 0; n < buf_length && all_non_zero; ++n) 57 all_non_zero = buf[n] != 0; 58 return all_non_zero; 59 } 60 61 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 62 void Convert(const webrtc::NetEqNetworkStatistics& stats_raw, 63 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) { 64 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms); 65 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms); 66 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found); 67 stats->set_packet_loss_rate(stats_raw.packet_loss_rate); 68 stats->set_packet_discard_rate(stats_raw.packet_discard_rate); 69 stats->set_expand_rate(stats_raw.expand_rate); 70 stats->set_speech_expand_rate(stats_raw.speech_expand_rate); 71 stats->set_preemptive_rate(stats_raw.preemptive_rate); 72 stats->set_accelerate_rate(stats_raw.accelerate_rate); 73 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate); 74 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm); 75 stats->set_added_zero_samples(stats_raw.added_zero_samples); 76 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms); 77 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms); 78 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms); 79 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms); 80 } 81 82 void Convert(const webrtc::RtcpStatistics& stats_raw, 83 webrtc::neteq_unittest::RtcpStatistics* stats) { 84 stats->set_fraction_lost(stats_raw.fraction_lost); 85 stats->set_cumulative_lost(stats_raw.cumulative_lost); 86 stats->set_extended_max_sequence_number( 87 stats_raw.extended_max_sequence_number); 88 stats->set_jitter(stats_raw.jitter); 89 } 90 91 void WriteMessage(FILE* file, const std::string& message) { 92 int32_t size = message.length(); 93 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file)); 94 if (size <= 0) 95 return; 96 ASSERT_EQ(static_cast<size_t>(size), 97 fwrite(message.data(), sizeof(char), size, file)); 98 } 99 100 void ReadMessage(FILE* file, std::string* message) { 101 int32_t size; 102 ASSERT_EQ(1u, fread(&size, sizeof(size), 1, file)); 103 if (size <= 0) 104 return; 105 rtc::scoped_ptr<char[]> buffer(new char[size]); 106 ASSERT_EQ(static_cast<size_t>(size), 107 fread(buffer.get(), sizeof(char), size, file)); 108 message->assign(buffer.get(), size); 109 } 110 #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT 111 112 } // namespace 113 114 namespace webrtc { 115 116 class RefFiles { 117 public: 118 RefFiles(const std::string& input_file, const std::string& output_file); 119 ~RefFiles(); 120 template<class T> void ProcessReference(const T& test_results); 121 template<typename T, size_t n> void ProcessReference( 122 const T (&test_results)[n], 123 size_t length); 124 template<typename T, size_t n> void WriteToFile( 125 const T (&test_results)[n], 126 size_t length); 127 template<typename T, size_t n> void ReadFromFileAndCompare( 128 const T (&test_results)[n], 129 size_t length); 130 void WriteToFile(const NetEqNetworkStatistics& stats); 131 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats); 132 void WriteToFile(const RtcpStatistics& stats); 133 void ReadFromFileAndCompare(const RtcpStatistics& stats); 134 135 FILE* input_fp_; 136 FILE* output_fp_; 137 }; 138 139 RefFiles::RefFiles(const std::string &input_file, 140 const std::string &output_file) 141 : input_fp_(NULL), 142 output_fp_(NULL) { 143 if (!input_file.empty()) { 144 input_fp_ = fopen(input_file.c_str(), "rb"); 145 EXPECT_TRUE(input_fp_ != NULL); 146 } 147 if (!output_file.empty()) { 148 output_fp_ = fopen(output_file.c_str(), "wb"); 149 EXPECT_TRUE(output_fp_ != NULL); 150 } 151 } 152 153 RefFiles::~RefFiles() { 154 if (input_fp_) { 155 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end. 156 fclose(input_fp_); 157 } 158 if (output_fp_) fclose(output_fp_); 159 } 160 161 template<class T> 162 void RefFiles::ProcessReference(const T& test_results) { 163 WriteToFile(test_results); 164 ReadFromFileAndCompare(test_results); 165 } 166 167 template<typename T, size_t n> 168 void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) { 169 WriteToFile(test_results, length); 170 ReadFromFileAndCompare(test_results, length); 171 } 172 173 template<typename T, size_t n> 174 void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) { 175 if (output_fp_) { 176 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_)); 177 } 178 } 179 180 template<typename T, size_t n> 181 void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n], 182 size_t length) { 183 if (input_fp_) { 184 // Read from ref file. 185 T* ref = new T[length]; 186 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_)); 187 // Compare 188 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length)); 189 delete [] ref; 190 } 191 } 192 193 void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats_raw) { 194 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 195 if (!output_fp_) 196 return; 197 neteq_unittest::NetEqNetworkStatistics stats; 198 Convert(stats_raw, &stats); 199 200 std::string stats_string; 201 ASSERT_TRUE(stats.SerializeToString(&stats_string)); 202 WriteMessage(output_fp_, stats_string); 203 #else 204 FAIL() << "Writing to reference file requires Proto Buffer."; 205 #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT 206 } 207 208 void RefFiles::ReadFromFileAndCompare( 209 const NetEqNetworkStatistics& stats) { 210 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 211 if (!input_fp_) 212 return; 213 214 std::string stats_string; 215 ReadMessage(input_fp_, &stats_string); 216 neteq_unittest::NetEqNetworkStatistics ref_stats; 217 ASSERT_TRUE(ref_stats.ParseFromString(stats_string)); 218 219 // Compare 220 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms()); 221 ASSERT_EQ(stats.preferred_buffer_size_ms, 222 ref_stats.preferred_buffer_size_ms()); 223 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found()); 224 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate()); 225 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate()); 226 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate()); 227 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate()); 228 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate()); 229 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm()); 230 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples()); 231 ASSERT_EQ(stats.secondary_decoded_rate, ref_stats.secondary_decoded_rate()); 232 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate()); 233 #else 234 FAIL() << "Reading from reference file requires Proto Buffer."; 235 #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT 236 } 237 238 void RefFiles::WriteToFile(const RtcpStatistics& stats_raw) { 239 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 240 if (!output_fp_) 241 return; 242 neteq_unittest::RtcpStatistics stats; 243 Convert(stats_raw, &stats); 244 245 std::string stats_string; 246 ASSERT_TRUE(stats.SerializeToString(&stats_string)); 247 WriteMessage(output_fp_, stats_string); 248 #else 249 FAIL() << "Writing to reference file requires Proto Buffer."; 250 #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT 251 } 252 253 void RefFiles::ReadFromFileAndCompare(const RtcpStatistics& stats) { 254 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT 255 if (!input_fp_) 256 return; 257 std::string stats_string; 258 ReadMessage(input_fp_, &stats_string); 259 neteq_unittest::RtcpStatistics ref_stats; 260 ASSERT_TRUE(ref_stats.ParseFromString(stats_string)); 261 262 // Compare 263 ASSERT_EQ(stats.fraction_lost, ref_stats.fraction_lost()); 264 ASSERT_EQ(stats.cumulative_lost, ref_stats.cumulative_lost()); 265 ASSERT_EQ(stats.extended_max_sequence_number, 266 ref_stats.extended_max_sequence_number()); 267 ASSERT_EQ(stats.jitter, ref_stats.jitter()); 268 #else 269 FAIL() << "Reading from reference file requires Proto Buffer."; 270 #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT 271 } 272 273 class NetEqDecodingTest : public ::testing::Test { 274 protected: 275 // NetEQ must be polled for data once every 10 ms. Thus, neither of the 276 // constants below can be changed. 277 static const int kTimeStepMs = 10; 278 static const size_t kBlockSize8kHz = kTimeStepMs * 8; 279 static const size_t kBlockSize16kHz = kTimeStepMs * 16; 280 static const size_t kBlockSize32kHz = kTimeStepMs * 32; 281 static const size_t kBlockSize48kHz = kTimeStepMs * 48; 282 static const size_t kMaxBlockSize = kBlockSize48kHz; 283 static const int kInitSampleRateHz = 8000; 284 285 NetEqDecodingTest(); 286 virtual void SetUp(); 287 virtual void TearDown(); 288 void SelectDecoders(NetEqDecoder* used_codec); 289 void LoadDecoders(); 290 void OpenInputFile(const std::string &rtp_file); 291 void Process(size_t* out_len); 292 293 void DecodeAndCompare(const std::string& rtp_file, 294 const std::string& ref_file, 295 const std::string& stat_ref_file, 296 const std::string& rtcp_ref_file); 297 298 static void PopulateRtpInfo(int frame_index, 299 int timestamp, 300 WebRtcRTPHeader* rtp_info); 301 static void PopulateCng(int frame_index, 302 int timestamp, 303 WebRtcRTPHeader* rtp_info, 304 uint8_t* payload, 305 size_t* payload_len); 306 307 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp, 308 const std::set<uint16_t>& drop_seq_numbers, 309 bool expect_seq_no_wrap, bool expect_timestamp_wrap); 310 311 void LongCngWithClockDrift(double drift_factor, 312 double network_freeze_ms, 313 bool pull_audio_during_freeze, 314 int delay_tolerance_ms, 315 int max_time_to_speech_ms); 316 317 void DuplicateCng(); 318 319 uint32_t PlayoutTimestamp(); 320 321 NetEq* neteq_; 322 NetEq::Config config_; 323 rtc::scoped_ptr<test::RtpFileSource> rtp_source_; 324 rtc::scoped_ptr<test::Packet> packet_; 325 unsigned int sim_clock_; 326 int16_t out_data_[kMaxBlockSize]; 327 int output_sample_rate_; 328 int algorithmic_delay_ms_; 329 }; 330 331 // Allocating the static const so that it can be passed by reference. 332 const int NetEqDecodingTest::kTimeStepMs; 333 const size_t NetEqDecodingTest::kBlockSize8kHz; 334 const size_t NetEqDecodingTest::kBlockSize16kHz; 335 const size_t NetEqDecodingTest::kBlockSize32kHz; 336 const size_t NetEqDecodingTest::kMaxBlockSize; 337 const int NetEqDecodingTest::kInitSampleRateHz; 338 339 NetEqDecodingTest::NetEqDecodingTest() 340 : neteq_(NULL), 341 config_(), 342 sim_clock_(0), 343 output_sample_rate_(kInitSampleRateHz), 344 algorithmic_delay_ms_(0) { 345 config_.sample_rate_hz = kInitSampleRateHz; 346 memset(out_data_, 0, sizeof(out_data_)); 347 } 348 349 void NetEqDecodingTest::SetUp() { 350 neteq_ = NetEq::Create(config_); 351 NetEqNetworkStatistics stat; 352 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat)); 353 algorithmic_delay_ms_ = stat.current_buffer_size_ms; 354 ASSERT_TRUE(neteq_); 355 LoadDecoders(); 356 } 357 358 void NetEqDecodingTest::TearDown() { 359 delete neteq_; 360 } 361 362 void NetEqDecodingTest::LoadDecoders() { 363 // Load PCMu. 364 ASSERT_EQ(0, 365 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0)); 366 // Load PCMa. 367 ASSERT_EQ(0, 368 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8)); 369 #ifdef WEBRTC_CODEC_ILBC 370 // Load iLBC. 371 ASSERT_EQ( 372 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102)); 373 #endif 374 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) 375 // Load iSAC. 376 ASSERT_EQ( 377 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103)); 378 #endif 379 #ifdef WEBRTC_CODEC_ISAC 380 // Load iSAC SWB. 381 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb, 382 "isac-swb", 104)); 383 #endif 384 #ifdef WEBRTC_CODEC_OPUS 385 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus, 386 "opus", 111)); 387 #endif 388 // Load PCM16B nb. 389 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B, 390 "pcm16-nb", 93)); 391 // Load PCM16B wb. 392 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb, 393 "pcm16-wb", 94)); 394 // Load PCM16B swb32. 395 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz, 396 "pcm16-swb32", 95)); 397 // Load CNG 8 kHz. 398 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb, 399 "cng-nb", 13)); 400 // Load CNG 16 kHz. 401 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb, 402 "cng-wb", 98)); 403 } 404 405 void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) { 406 rtp_source_.reset(test::RtpFileSource::Create(rtp_file)); 407 } 408 409 void NetEqDecodingTest::Process(size_t* out_len) { 410 // Check if time to receive. 411 while (packet_ && sim_clock_ >= packet_->time_ms()) { 412 if (packet_->payload_length_bytes() > 0) { 413 WebRtcRTPHeader rtp_header; 414 packet_->ConvertHeader(&rtp_header); 415 ASSERT_EQ(0, neteq_->InsertPacket( 416 rtp_header, 417 rtc::ArrayView<const uint8_t>( 418 packet_->payload(), packet_->payload_length_bytes()), 419 static_cast<uint32_t>(packet_->time_ms() * 420 (output_sample_rate_ / 1000)))); 421 } 422 // Get next packet. 423 packet_.reset(rtp_source_->NextPacket()); 424 } 425 426 // Get audio from NetEq. 427 NetEqOutputType type; 428 size_t num_channels; 429 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len, 430 &num_channels, &type)); 431 ASSERT_TRUE((*out_len == kBlockSize8kHz) || 432 (*out_len == kBlockSize16kHz) || 433 (*out_len == kBlockSize32kHz) || 434 (*out_len == kBlockSize48kHz)); 435 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000); 436 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz()); 437 438 // Increase time. 439 sim_clock_ += kTimeStepMs; 440 } 441 442 void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file, 443 const std::string& ref_file, 444 const std::string& stat_ref_file, 445 const std::string& rtcp_ref_file) { 446 OpenInputFile(rtp_file); 447 448 std::string ref_out_file = ""; 449 if (ref_file.empty()) { 450 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm"; 451 } 452 RefFiles ref_files(ref_file, ref_out_file); 453 454 std::string stat_out_file = ""; 455 if (stat_ref_file.empty()) { 456 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat"; 457 } 458 RefFiles network_stat_files(stat_ref_file, stat_out_file); 459 460 std::string rtcp_out_file = ""; 461 if (rtcp_ref_file.empty()) { 462 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat"; 463 } 464 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file); 465 466 packet_.reset(rtp_source_->NextPacket()); 467 int i = 0; 468 while (packet_) { 469 std::ostringstream ss; 470 ss << "Lap number " << i++ << " in DecodeAndCompare while loop"; 471 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. 472 size_t out_len = 0; 473 ASSERT_NO_FATAL_FAILURE(Process(&out_len)); 474 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len)); 475 476 // Query the network statistics API once per second 477 if (sim_clock_ % 1000 == 0) { 478 // Process NetworkStatistics. 479 NetEqNetworkStatistics network_stats; 480 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 481 ASSERT_NO_FATAL_FAILURE( 482 network_stat_files.ProcessReference(network_stats)); 483 // Compare with CurrentDelay, which should be identical. 484 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs()); 485 486 // Process RTCPstat. 487 RtcpStatistics rtcp_stats; 488 neteq_->GetRtcpStatistics(&rtcp_stats); 489 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats)); 490 } 491 } 492 } 493 494 void NetEqDecodingTest::PopulateRtpInfo(int frame_index, 495 int timestamp, 496 WebRtcRTPHeader* rtp_info) { 497 rtp_info->header.sequenceNumber = frame_index; 498 rtp_info->header.timestamp = timestamp; 499 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. 500 rtp_info->header.payloadType = 94; // PCM16b WB codec. 501 rtp_info->header.markerBit = 0; 502 } 503 504 void NetEqDecodingTest::PopulateCng(int frame_index, 505 int timestamp, 506 WebRtcRTPHeader* rtp_info, 507 uint8_t* payload, 508 size_t* payload_len) { 509 rtp_info->header.sequenceNumber = frame_index; 510 rtp_info->header.timestamp = timestamp; 511 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. 512 rtp_info->header.payloadType = 98; // WB CNG. 513 rtp_info->header.markerBit = 0; 514 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen. 515 *payload_len = 1; // Only noise level, no spectral parameters. 516 } 517 518 #if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \ 519 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \ 520 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \ 521 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) 522 #define MAYBE_TestBitExactness TestBitExactness 523 #else 524 #define MAYBE_TestBitExactness DISABLED_TestBitExactness 525 #endif 526 TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) { 527 const std::string input_rtp_file = 528 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"); 529 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm 530 // are identical. The latter could have been removed, but if clients still 531 // have a copy of the file, the test will fail. 532 const std::string input_ref_file = 533 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm"); 534 #if defined(_MSC_VER) && (_MSC_VER >= 1700) 535 // For Visual Studio 2012 and later, we will have to use the generic reference 536 // file, rather than the windows-specific one. 537 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() + 538 "resources/audio_coding/neteq4_network_stats.dat"; 539 #else 540 const std::string network_stat_ref_file = 541 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat"); 542 #endif 543 const std::string rtcp_stat_ref_file = 544 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat"); 545 546 if (FLAGS_gen_ref) { 547 DecodeAndCompare(input_rtp_file, "", "", ""); 548 } else { 549 DecodeAndCompare(input_rtp_file, 550 input_ref_file, 551 network_stat_ref_file, 552 rtcp_stat_ref_file); 553 } 554 } 555 556 #if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \ 557 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \ 558 defined(WEBRTC_CODEC_OPUS) 559 #define MAYBE_TestOpusBitExactness TestOpusBitExactness 560 #else 561 #define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness 562 #endif 563 TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) { 564 const std::string input_rtp_file = 565 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp"); 566 const std::string input_ref_file = 567 webrtc::test::ResourcePath("audio_coding/neteq4_opus_ref", "pcm"); 568 const std::string network_stat_ref_file = 569 webrtc::test::ResourcePath("audio_coding/neteq4_opus_network_stats", 570 "dat"); 571 const std::string rtcp_stat_ref_file = 572 webrtc::test::ResourcePath("audio_coding/neteq4_opus_rtcp_stats", "dat"); 573 574 if (FLAGS_gen_ref) { 575 DecodeAndCompare(input_rtp_file, "", "", ""); 576 } else { 577 DecodeAndCompare(input_rtp_file, 578 input_ref_file, 579 network_stat_ref_file, 580 rtcp_stat_ref_file); 581 } 582 } 583 584 // Use fax mode to avoid time-scaling. This is to simplify the testing of 585 // packet waiting times in the packet buffer. 586 class NetEqDecodingTestFaxMode : public NetEqDecodingTest { 587 protected: 588 NetEqDecodingTestFaxMode() : NetEqDecodingTest() { 589 config_.playout_mode = kPlayoutFax; 590 } 591 }; 592 593 TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) { 594 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio. 595 size_t num_frames = 30; 596 const size_t kSamples = 10 * 16; 597 const size_t kPayloadBytes = kSamples * 2; 598 for (size_t i = 0; i < num_frames; ++i) { 599 const uint8_t payload[kPayloadBytes] = {0}; 600 WebRtcRTPHeader rtp_info; 601 rtp_info.header.sequenceNumber = i; 602 rtp_info.header.timestamp = i * kSamples; 603 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC. 604 rtp_info.header.payloadType = 94; // PCM16b WB codec. 605 rtp_info.header.markerBit = 0; 606 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 607 } 608 // Pull out all data. 609 for (size_t i = 0; i < num_frames; ++i) { 610 size_t out_len; 611 size_t num_channels; 612 NetEqOutputType type; 613 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 614 &num_channels, &type)); 615 ASSERT_EQ(kBlockSize16kHz, out_len); 616 } 617 618 NetEqNetworkStatistics stats; 619 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); 620 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms 621 // spacing (per definition), we expect the delay to increase with 10 ms for 622 // each packet. Thus, we are calculating the statistics for a series from 10 623 // to 300, in steps of 10 ms. 624 EXPECT_EQ(155, stats.mean_waiting_time_ms); 625 EXPECT_EQ(155, stats.median_waiting_time_ms); 626 EXPECT_EQ(10, stats.min_waiting_time_ms); 627 EXPECT_EQ(300, stats.max_waiting_time_ms); 628 629 // Check statistics again and make sure it's been reset. 630 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); 631 EXPECT_EQ(-1, stats.mean_waiting_time_ms); 632 EXPECT_EQ(-1, stats.median_waiting_time_ms); 633 EXPECT_EQ(-1, stats.min_waiting_time_ms); 634 EXPECT_EQ(-1, stats.max_waiting_time_ms); 635 } 636 637 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) { 638 const int kNumFrames = 3000; // Needed for convergence. 639 int frame_index = 0; 640 const size_t kSamples = 10 * 16; 641 const size_t kPayloadBytes = kSamples * 2; 642 while (frame_index < kNumFrames) { 643 // Insert one packet each time, except every 10th time where we insert two 644 // packets at once. This will create a negative clock-drift of approx. 10%. 645 int num_packets = (frame_index % 10 == 0 ? 2 : 1); 646 for (int n = 0; n < num_packets; ++n) { 647 uint8_t payload[kPayloadBytes] = {0}; 648 WebRtcRTPHeader rtp_info; 649 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); 650 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 651 ++frame_index; 652 } 653 654 // Pull out data once. 655 size_t out_len; 656 size_t num_channels; 657 NetEqOutputType type; 658 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 659 &num_channels, &type)); 660 ASSERT_EQ(kBlockSize16kHz, out_len); 661 } 662 663 NetEqNetworkStatistics network_stats; 664 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 665 EXPECT_EQ(-103196, network_stats.clockdrift_ppm); 666 } 667 668 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) { 669 const int kNumFrames = 5000; // Needed for convergence. 670 int frame_index = 0; 671 const size_t kSamples = 10 * 16; 672 const size_t kPayloadBytes = kSamples * 2; 673 for (int i = 0; i < kNumFrames; ++i) { 674 // Insert one packet each time, except every 10th time where we don't insert 675 // any packet. This will create a positive clock-drift of approx. 11%. 676 int num_packets = (i % 10 == 9 ? 0 : 1); 677 for (int n = 0; n < num_packets; ++n) { 678 uint8_t payload[kPayloadBytes] = {0}; 679 WebRtcRTPHeader rtp_info; 680 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); 681 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 682 ++frame_index; 683 } 684 685 // Pull out data once. 686 size_t out_len; 687 size_t num_channels; 688 NetEqOutputType type; 689 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 690 &num_channels, &type)); 691 ASSERT_EQ(kBlockSize16kHz, out_len); 692 } 693 694 NetEqNetworkStatistics network_stats; 695 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 696 EXPECT_EQ(110946, network_stats.clockdrift_ppm); 697 } 698 699 void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor, 700 double network_freeze_ms, 701 bool pull_audio_during_freeze, 702 int delay_tolerance_ms, 703 int max_time_to_speech_ms) { 704 uint16_t seq_no = 0; 705 uint32_t timestamp = 0; 706 const int kFrameSizeMs = 30; 707 const size_t kSamples = kFrameSizeMs * 16; 708 const size_t kPayloadBytes = kSamples * 2; 709 double next_input_time_ms = 0.0; 710 double t_ms; 711 size_t out_len; 712 size_t num_channels; 713 NetEqOutputType type; 714 715 // Insert speech for 5 seconds. 716 const int kSpeechDurationMs = 5000; 717 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { 718 // Each turn in this for loop is 10 ms. 719 while (next_input_time_ms <= t_ms) { 720 // Insert one 30 ms speech frame. 721 uint8_t payload[kPayloadBytes] = {0}; 722 WebRtcRTPHeader rtp_info; 723 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 724 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 725 ++seq_no; 726 timestamp += kSamples; 727 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor; 728 } 729 // Pull out data once. 730 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 731 &num_channels, &type)); 732 ASSERT_EQ(kBlockSize16kHz, out_len); 733 } 734 735 EXPECT_EQ(kOutputNormal, type); 736 int32_t delay_before = timestamp - PlayoutTimestamp(); 737 738 // Insert CNG for 1 minute (= 60000 ms). 739 const int kCngPeriodMs = 100; 740 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples. 741 const int kCngDurationMs = 60000; 742 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) { 743 // Each turn in this for loop is 10 ms. 744 while (next_input_time_ms <= t_ms) { 745 // Insert one CNG frame each 100 ms. 746 uint8_t payload[kPayloadBytes]; 747 size_t payload_len; 748 WebRtcRTPHeader rtp_info; 749 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); 750 ASSERT_EQ(0, neteq_->InsertPacket( 751 rtp_info, 752 rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); 753 ++seq_no; 754 timestamp += kCngPeriodSamples; 755 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor; 756 } 757 // Pull out data once. 758 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 759 &num_channels, &type)); 760 ASSERT_EQ(kBlockSize16kHz, out_len); 761 } 762 763 EXPECT_EQ(kOutputCNG, type); 764 765 if (network_freeze_ms > 0) { 766 // First keep pulling audio for |network_freeze_ms| without inserting 767 // any data, then insert CNG data corresponding to |network_freeze_ms| 768 // without pulling any output audio. 769 const double loop_end_time = t_ms + network_freeze_ms; 770 for (; t_ms < loop_end_time; t_ms += 10) { 771 // Pull out data once. 772 ASSERT_EQ(0, 773 neteq_->GetAudio( 774 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 775 ASSERT_EQ(kBlockSize16kHz, out_len); 776 EXPECT_EQ(kOutputCNG, type); 777 } 778 bool pull_once = pull_audio_during_freeze; 779 // If |pull_once| is true, GetAudio will be called once half-way through 780 // the network recovery period. 781 double pull_time_ms = (t_ms + next_input_time_ms) / 2; 782 while (next_input_time_ms <= t_ms) { 783 if (pull_once && next_input_time_ms >= pull_time_ms) { 784 pull_once = false; 785 // Pull out data once. 786 ASSERT_EQ( 787 0, 788 neteq_->GetAudio( 789 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 790 ASSERT_EQ(kBlockSize16kHz, out_len); 791 EXPECT_EQ(kOutputCNG, type); 792 t_ms += 10; 793 } 794 // Insert one CNG frame each 100 ms. 795 uint8_t payload[kPayloadBytes]; 796 size_t payload_len; 797 WebRtcRTPHeader rtp_info; 798 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); 799 ASSERT_EQ(0, neteq_->InsertPacket( 800 rtp_info, 801 rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); 802 ++seq_no; 803 timestamp += kCngPeriodSamples; 804 next_input_time_ms += kCngPeriodMs * drift_factor; 805 } 806 } 807 808 // Insert speech again until output type is speech. 809 double speech_restart_time_ms = t_ms; 810 while (type != kOutputNormal) { 811 // Each turn in this for loop is 10 ms. 812 while (next_input_time_ms <= t_ms) { 813 // Insert one 30 ms speech frame. 814 uint8_t payload[kPayloadBytes] = {0}; 815 WebRtcRTPHeader rtp_info; 816 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 817 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 818 ++seq_no; 819 timestamp += kSamples; 820 next_input_time_ms += kFrameSizeMs * drift_factor; 821 } 822 // Pull out data once. 823 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 824 &num_channels, &type)); 825 ASSERT_EQ(kBlockSize16kHz, out_len); 826 // Increase clock. 827 t_ms += 10; 828 } 829 830 // Check that the speech starts again within reasonable time. 831 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms; 832 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms); 833 int32_t delay_after = timestamp - PlayoutTimestamp(); 834 // Compare delay before and after, and make sure it differs less than 20 ms. 835 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16); 836 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16); 837 } 838 839 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) { 840 // Apply a clock drift of -25 ms / s (sender faster than receiver). 841 const double kDriftFactor = 1000.0 / (1000.0 + 25.0); 842 const double kNetworkFreezeTimeMs = 0.0; 843 const bool kGetAudioDuringFreezeRecovery = false; 844 const int kDelayToleranceMs = 20; 845 const int kMaxTimeToSpeechMs = 100; 846 LongCngWithClockDrift(kDriftFactor, 847 kNetworkFreezeTimeMs, 848 kGetAudioDuringFreezeRecovery, 849 kDelayToleranceMs, 850 kMaxTimeToSpeechMs); 851 } 852 853 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) { 854 // Apply a clock drift of +25 ms / s (sender slower than receiver). 855 const double kDriftFactor = 1000.0 / (1000.0 - 25.0); 856 const double kNetworkFreezeTimeMs = 0.0; 857 const bool kGetAudioDuringFreezeRecovery = false; 858 const int kDelayToleranceMs = 20; 859 const int kMaxTimeToSpeechMs = 100; 860 LongCngWithClockDrift(kDriftFactor, 861 kNetworkFreezeTimeMs, 862 kGetAudioDuringFreezeRecovery, 863 kDelayToleranceMs, 864 kMaxTimeToSpeechMs); 865 } 866 867 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) { 868 // Apply a clock drift of -25 ms / s (sender faster than receiver). 869 const double kDriftFactor = 1000.0 / (1000.0 + 25.0); 870 const double kNetworkFreezeTimeMs = 5000.0; 871 const bool kGetAudioDuringFreezeRecovery = false; 872 const int kDelayToleranceMs = 50; 873 const int kMaxTimeToSpeechMs = 200; 874 LongCngWithClockDrift(kDriftFactor, 875 kNetworkFreezeTimeMs, 876 kGetAudioDuringFreezeRecovery, 877 kDelayToleranceMs, 878 kMaxTimeToSpeechMs); 879 } 880 881 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) { 882 // Apply a clock drift of +25 ms / s (sender slower than receiver). 883 const double kDriftFactor = 1000.0 / (1000.0 - 25.0); 884 const double kNetworkFreezeTimeMs = 5000.0; 885 const bool kGetAudioDuringFreezeRecovery = false; 886 const int kDelayToleranceMs = 20; 887 const int kMaxTimeToSpeechMs = 100; 888 LongCngWithClockDrift(kDriftFactor, 889 kNetworkFreezeTimeMs, 890 kGetAudioDuringFreezeRecovery, 891 kDelayToleranceMs, 892 kMaxTimeToSpeechMs); 893 } 894 895 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) { 896 // Apply a clock drift of +25 ms / s (sender slower than receiver). 897 const double kDriftFactor = 1000.0 / (1000.0 - 25.0); 898 const double kNetworkFreezeTimeMs = 5000.0; 899 const bool kGetAudioDuringFreezeRecovery = true; 900 const int kDelayToleranceMs = 20; 901 const int kMaxTimeToSpeechMs = 100; 902 LongCngWithClockDrift(kDriftFactor, 903 kNetworkFreezeTimeMs, 904 kGetAudioDuringFreezeRecovery, 905 kDelayToleranceMs, 906 kMaxTimeToSpeechMs); 907 } 908 909 TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) { 910 const double kDriftFactor = 1.0; // No drift. 911 const double kNetworkFreezeTimeMs = 0.0; 912 const bool kGetAudioDuringFreezeRecovery = false; 913 const int kDelayToleranceMs = 10; 914 const int kMaxTimeToSpeechMs = 50; 915 LongCngWithClockDrift(kDriftFactor, 916 kNetworkFreezeTimeMs, 917 kGetAudioDuringFreezeRecovery, 918 kDelayToleranceMs, 919 kMaxTimeToSpeechMs); 920 } 921 922 TEST_F(NetEqDecodingTest, UnknownPayloadType) { 923 const size_t kPayloadBytes = 100; 924 uint8_t payload[kPayloadBytes] = {0}; 925 WebRtcRTPHeader rtp_info; 926 PopulateRtpInfo(0, 0, &rtp_info); 927 rtp_info.header.payloadType = 1; // Not registered as a decoder. 928 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0)); 929 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError()); 930 } 931 932 #if defined(WEBRTC_ANDROID) 933 #define MAYBE_DecoderError DISABLED_DecoderError 934 #else 935 #define MAYBE_DecoderError DecoderError 936 #endif 937 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) 938 TEST_F(NetEqDecodingTest, MAYBE_DecoderError) { 939 const size_t kPayloadBytes = 100; 940 uint8_t payload[kPayloadBytes] = {0}; 941 WebRtcRTPHeader rtp_info; 942 PopulateRtpInfo(0, 0, &rtp_info); 943 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid. 944 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 945 NetEqOutputType type; 946 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call 947 // to GetAudio. 948 for (size_t i = 0; i < kMaxBlockSize; ++i) { 949 out_data_[i] = 1; 950 } 951 size_t num_channels; 952 size_t samples_per_channel; 953 EXPECT_EQ(NetEq::kFail, 954 neteq_->GetAudio(kMaxBlockSize, out_data_, 955 &samples_per_channel, &num_channels, &type)); 956 // Verify that there is a decoder error to check. 957 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError()); 958 // Code 6730 is an iSAC error code. 959 EXPECT_EQ(6730, neteq_->LastDecoderError()); 960 // Verify that the first 160 samples are set to 0, and that the remaining 961 // samples are left unmodified. 962 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate. 963 for (int i = 0; i < kExpectedOutputLength; ++i) { 964 std::ostringstream ss; 965 ss << "i = " << i; 966 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. 967 EXPECT_EQ(0, out_data_[i]); 968 } 969 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) { 970 std::ostringstream ss; 971 ss << "i = " << i; 972 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. 973 EXPECT_EQ(1, out_data_[i]); 974 } 975 } 976 #endif 977 978 TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) { 979 NetEqOutputType type; 980 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call 981 // to GetAudio. 982 for (size_t i = 0; i < kMaxBlockSize; ++i) { 983 out_data_[i] = 1; 984 } 985 size_t num_channels; 986 size_t samples_per_channel; 987 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, 988 &samples_per_channel, 989 &num_channels, &type)); 990 // Verify that the first block of samples is set to 0. 991 static const int kExpectedOutputLength = 992 kInitSampleRateHz / 100; // 10 ms at initial sample rate. 993 for (int i = 0; i < kExpectedOutputLength; ++i) { 994 std::ostringstream ss; 995 ss << "i = " << i; 996 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. 997 EXPECT_EQ(0, out_data_[i]); 998 } 999 // Verify that the sample rate did not change from the initial configuration. 1000 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz()); 1001 } 1002 1003 class NetEqBgnTest : public NetEqDecodingTest { 1004 protected: 1005 virtual void TestCondition(double sum_squared_noise, 1006 bool should_be_faded) = 0; 1007 1008 void CheckBgn(int sampling_rate_hz) { 1009 size_t expected_samples_per_channel = 0; 1010 uint8_t payload_type = 0xFF; // Invalid. 1011 if (sampling_rate_hz == 8000) { 1012 expected_samples_per_channel = kBlockSize8kHz; 1013 payload_type = 93; // PCM 16, 8 kHz. 1014 } else if (sampling_rate_hz == 16000) { 1015 expected_samples_per_channel = kBlockSize16kHz; 1016 payload_type = 94; // PCM 16, 16 kHZ. 1017 } else if (sampling_rate_hz == 32000) { 1018 expected_samples_per_channel = kBlockSize32kHz; 1019 payload_type = 95; // PCM 16, 32 kHz. 1020 } else { 1021 ASSERT_TRUE(false); // Unsupported test case. 1022 } 1023 1024 NetEqOutputType type; 1025 int16_t output[kBlockSize32kHz]; // Maximum size is chosen. 1026 test::AudioLoop input; 1027 // We are using the same 32 kHz input file for all tests, regardless of 1028 // |sampling_rate_hz|. The output may sound weird, but the test is still 1029 // valid. 1030 ASSERT_TRUE(input.Init( 1031 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 1032 10 * sampling_rate_hz, // Max 10 seconds loop length. 1033 expected_samples_per_channel)); 1034 1035 // Payload of 10 ms of PCM16 32 kHz. 1036 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)]; 1037 WebRtcRTPHeader rtp_info; 1038 PopulateRtpInfo(0, 0, &rtp_info); 1039 rtp_info.header.payloadType = payload_type; 1040 1041 size_t number_channels = 0; 1042 size_t samples_per_channel = 0; 1043 1044 uint32_t receive_timestamp = 0; 1045 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio. 1046 auto block = input.GetNextBlock(); 1047 ASSERT_EQ(expected_samples_per_channel, block.size()); 1048 size_t enc_len_bytes = 1049 WebRtcPcm16b_Encode(block.data(), block.size(), payload); 1050 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2); 1051 1052 number_channels = 0; 1053 samples_per_channel = 0; 1054 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( 1055 payload, enc_len_bytes), 1056 receive_timestamp)); 1057 ASSERT_EQ(0, 1058 neteq_->GetAudio(kBlockSize32kHz, 1059 output, 1060 &samples_per_channel, 1061 &number_channels, 1062 &type)); 1063 ASSERT_EQ(1u, number_channels); 1064 ASSERT_EQ(expected_samples_per_channel, samples_per_channel); 1065 ASSERT_EQ(kOutputNormal, type); 1066 1067 // Next packet. 1068 rtp_info.header.timestamp += expected_samples_per_channel; 1069 rtp_info.header.sequenceNumber++; 1070 receive_timestamp += expected_samples_per_channel; 1071 } 1072 1073 number_channels = 0; 1074 samples_per_channel = 0; 1075 1076 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull 1077 // one frame without checking speech-type. This is the first frame pulled 1078 // without inserting any packet, and might not be labeled as PLC. 1079 ASSERT_EQ(0, 1080 neteq_->GetAudio(kBlockSize32kHz, 1081 output, 1082 &samples_per_channel, 1083 &number_channels, 1084 &type)); 1085 ASSERT_EQ(1u, number_channels); 1086 ASSERT_EQ(expected_samples_per_channel, samples_per_channel); 1087 1088 // To be able to test the fading of background noise we need at lease to 1089 // pull 611 frames. 1090 const int kFadingThreshold = 611; 1091 1092 // Test several CNG-to-PLC packet for the expected behavior. The number 20 1093 // is arbitrary, but sufficiently large to test enough number of frames. 1094 const int kNumPlcToCngTestFrames = 20; 1095 bool plc_to_cng = false; 1096 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) { 1097 number_channels = 0; 1098 samples_per_channel = 0; 1099 memset(output, 1, sizeof(output)); // Set to non-zero. 1100 ASSERT_EQ(0, 1101 neteq_->GetAudio(kBlockSize32kHz, 1102 output, 1103 &samples_per_channel, 1104 &number_channels, 1105 &type)); 1106 ASSERT_EQ(1u, number_channels); 1107 ASSERT_EQ(expected_samples_per_channel, samples_per_channel); 1108 if (type == kOutputPLCtoCNG) { 1109 plc_to_cng = true; 1110 double sum_squared = 0; 1111 for (size_t k = 0; k < number_channels * samples_per_channel; ++k) 1112 sum_squared += output[k] * output[k]; 1113 TestCondition(sum_squared, n > kFadingThreshold); 1114 } else { 1115 EXPECT_EQ(kOutputPLC, type); 1116 } 1117 } 1118 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred. 1119 } 1120 }; 1121 1122 class NetEqBgnTestOn : public NetEqBgnTest { 1123 protected: 1124 NetEqBgnTestOn() : NetEqBgnTest() { 1125 config_.background_noise_mode = NetEq::kBgnOn; 1126 } 1127 1128 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { 1129 EXPECT_NE(0, sum_squared_noise); 1130 } 1131 }; 1132 1133 class NetEqBgnTestOff : public NetEqBgnTest { 1134 protected: 1135 NetEqBgnTestOff() : NetEqBgnTest() { 1136 config_.background_noise_mode = NetEq::kBgnOff; 1137 } 1138 1139 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { 1140 EXPECT_EQ(0, sum_squared_noise); 1141 } 1142 }; 1143 1144 class NetEqBgnTestFade : public NetEqBgnTest { 1145 protected: 1146 NetEqBgnTestFade() : NetEqBgnTest() { 1147 config_.background_noise_mode = NetEq::kBgnFade; 1148 } 1149 1150 void TestCondition(double sum_squared_noise, bool should_be_faded) { 1151 if (should_be_faded) 1152 EXPECT_EQ(0, sum_squared_noise); 1153 } 1154 }; 1155 1156 TEST_F(NetEqBgnTestOn, RunTest) { 1157 CheckBgn(8000); 1158 CheckBgn(16000); 1159 CheckBgn(32000); 1160 } 1161 1162 TEST_F(NetEqBgnTestOff, RunTest) { 1163 CheckBgn(8000); 1164 CheckBgn(16000); 1165 CheckBgn(32000); 1166 } 1167 1168 TEST_F(NetEqBgnTestFade, RunTest) { 1169 CheckBgn(8000); 1170 CheckBgn(16000); 1171 CheckBgn(32000); 1172 } 1173 1174 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) 1175 TEST_F(NetEqDecodingTest, SyncPacketInsert) { 1176 WebRtcRTPHeader rtp_info; 1177 uint32_t receive_timestamp = 0; 1178 // For the readability use the following payloads instead of the defaults of 1179 // this test. 1180 uint8_t kPcm16WbPayloadType = 1; 1181 uint8_t kCngNbPayloadType = 2; 1182 uint8_t kCngWbPayloadType = 3; 1183 uint8_t kCngSwb32PayloadType = 4; 1184 uint8_t kCngSwb48PayloadType = 5; 1185 uint8_t kAvtPayloadType = 6; 1186 uint8_t kRedPayloadType = 7; 1187 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered. 1188 1189 // Register decoders. 1190 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb, 1191 "pcm16-wb", kPcm16WbPayloadType)); 1192 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb, 1193 "cng-nb", kCngNbPayloadType)); 1194 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb, 1195 "cng-wb", kCngWbPayloadType)); 1196 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz, 1197 "cng-swb32", kCngSwb32PayloadType)); 1198 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz, 1199 "cng-swb48", kCngSwb48PayloadType)); 1200 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt", 1201 kAvtPayloadType)); 1202 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red", 1203 kRedPayloadType)); 1204 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 1205 kIsacPayloadType)); 1206 1207 PopulateRtpInfo(0, 0, &rtp_info); 1208 rtp_info.header.payloadType = kPcm16WbPayloadType; 1209 1210 // The first packet injected cannot be sync-packet. 1211 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1212 1213 // Payload length of 10 ms PCM16 16 kHz. 1214 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); 1215 uint8_t payload[kPayloadBytes] = {0}; 1216 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1217 1218 // Next packet. Last packet contained 10 ms audio. 1219 rtp_info.header.sequenceNumber++; 1220 rtp_info.header.timestamp += kBlockSize16kHz; 1221 receive_timestamp += kBlockSize16kHz; 1222 1223 // Unacceptable payload types CNG, AVT (DTMF), RED. 1224 rtp_info.header.payloadType = kCngNbPayloadType; 1225 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1226 1227 rtp_info.header.payloadType = kCngWbPayloadType; 1228 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1229 1230 rtp_info.header.payloadType = kCngSwb32PayloadType; 1231 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1232 1233 rtp_info.header.payloadType = kCngSwb48PayloadType; 1234 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1235 1236 rtp_info.header.payloadType = kAvtPayloadType; 1237 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1238 1239 rtp_info.header.payloadType = kRedPayloadType; 1240 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1241 1242 // Change of codec cannot be initiated with a sync packet. 1243 rtp_info.header.payloadType = kIsacPayloadType; 1244 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1245 1246 // Change of SSRC is not allowed with a sync packet. 1247 rtp_info.header.payloadType = kPcm16WbPayloadType; 1248 ++rtp_info.header.ssrc; 1249 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1250 1251 --rtp_info.header.ssrc; 1252 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1253 } 1254 #endif 1255 1256 // First insert several noise like packets, then sync-packets. Decoding all 1257 // packets should not produce error, statistics should not show any packet loss 1258 // and sync-packets should decode to zero. 1259 // TODO(turajs) we will have a better test if we have a referece NetEq, and 1260 // when Sync packets are inserted in "test" NetEq we insert all-zero payload 1261 // in reference NetEq and compare the output of those two. 1262 TEST_F(NetEqDecodingTest, SyncPacketDecode) { 1263 WebRtcRTPHeader rtp_info; 1264 PopulateRtpInfo(0, 0, &rtp_info); 1265 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); 1266 uint8_t payload[kPayloadBytes]; 1267 int16_t decoded[kBlockSize16kHz]; 1268 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; 1269 for (size_t n = 0; n < kPayloadBytes; ++n) { 1270 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. 1271 } 1272 // Insert some packets which decode to noise. We are not interested in 1273 // actual decoded values. 1274 NetEqOutputType output_type; 1275 size_t num_channels; 1276 size_t samples_per_channel; 1277 uint32_t receive_timestamp = 0; 1278 for (int n = 0; n < 100; ++n) { 1279 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1280 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1281 &samples_per_channel, &num_channels, 1282 &output_type)); 1283 ASSERT_EQ(kBlockSize16kHz, samples_per_channel); 1284 ASSERT_EQ(1u, num_channels); 1285 1286 rtp_info.header.sequenceNumber++; 1287 rtp_info.header.timestamp += kBlockSize16kHz; 1288 receive_timestamp += kBlockSize16kHz; 1289 } 1290 const int kNumSyncPackets = 10; 1291 1292 // Make sure sufficient number of sync packets are inserted that we can 1293 // conduct a test. 1294 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay); 1295 // Insert sync-packets, the decoded sequence should be all-zero. 1296 for (int n = 0; n < kNumSyncPackets; ++n) { 1297 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1298 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1299 &samples_per_channel, &num_channels, 1300 &output_type)); 1301 ASSERT_EQ(kBlockSize16kHz, samples_per_channel); 1302 ASSERT_EQ(1u, num_channels); 1303 if (n > algorithmic_frame_delay) { 1304 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels)); 1305 } 1306 rtp_info.header.sequenceNumber++; 1307 rtp_info.header.timestamp += kBlockSize16kHz; 1308 receive_timestamp += kBlockSize16kHz; 1309 } 1310 1311 // We insert regular packets, if sync packet are not correctly buffered then 1312 // network statistics would show some packet loss. 1313 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) { 1314 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1315 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1316 &samples_per_channel, &num_channels, 1317 &output_type)); 1318 if (n >= algorithmic_frame_delay + 1) { 1319 // Expect that this frame contain samples from regular RTP. 1320 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels)); 1321 } 1322 rtp_info.header.sequenceNumber++; 1323 rtp_info.header.timestamp += kBlockSize16kHz; 1324 receive_timestamp += kBlockSize16kHz; 1325 } 1326 NetEqNetworkStatistics network_stats; 1327 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 1328 // Expecting a "clean" network. 1329 EXPECT_EQ(0, network_stats.packet_loss_rate); 1330 EXPECT_EQ(0, network_stats.expand_rate); 1331 EXPECT_EQ(0, network_stats.accelerate_rate); 1332 EXPECT_LE(network_stats.preemptive_rate, 150); 1333 } 1334 1335 // Test if the size of the packet buffer reported correctly when containing 1336 // sync packets. Also, test if network packets override sync packets. That is to 1337 // prefer decoding a network packet to a sync packet, if both have same sequence 1338 // number and timestamp. 1339 TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) { 1340 WebRtcRTPHeader rtp_info; 1341 PopulateRtpInfo(0, 0, &rtp_info); 1342 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); 1343 uint8_t payload[kPayloadBytes]; 1344 int16_t decoded[kBlockSize16kHz]; 1345 for (size_t n = 0; n < kPayloadBytes; ++n) { 1346 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. 1347 } 1348 // Insert some packets which decode to noise. We are not interested in 1349 // actual decoded values. 1350 NetEqOutputType output_type; 1351 size_t num_channels; 1352 size_t samples_per_channel; 1353 uint32_t receive_timestamp = 0; 1354 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; 1355 for (int n = 0; n < algorithmic_frame_delay; ++n) { 1356 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1357 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1358 &samples_per_channel, &num_channels, 1359 &output_type)); 1360 ASSERT_EQ(kBlockSize16kHz, samples_per_channel); 1361 ASSERT_EQ(1u, num_channels); 1362 rtp_info.header.sequenceNumber++; 1363 rtp_info.header.timestamp += kBlockSize16kHz; 1364 receive_timestamp += kBlockSize16kHz; 1365 } 1366 const int kNumSyncPackets = 10; 1367 1368 WebRtcRTPHeader first_sync_packet_rtp_info; 1369 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info)); 1370 1371 // Insert sync-packets, but no decoding. 1372 for (int n = 0; n < kNumSyncPackets; ++n) { 1373 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); 1374 rtp_info.header.sequenceNumber++; 1375 rtp_info.header.timestamp += kBlockSize16kHz; 1376 receive_timestamp += kBlockSize16kHz; 1377 } 1378 NetEqNetworkStatistics network_stats; 1379 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 1380 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_, 1381 network_stats.current_buffer_size_ms); 1382 1383 // Rewind |rtp_info| to that of the first sync packet. 1384 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info)); 1385 1386 // Insert. 1387 for (int n = 0; n < kNumSyncPackets; ++n) { 1388 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1389 rtp_info.header.sequenceNumber++; 1390 rtp_info.header.timestamp += kBlockSize16kHz; 1391 receive_timestamp += kBlockSize16kHz; 1392 } 1393 1394 // Decode. 1395 for (int n = 0; n < kNumSyncPackets; ++n) { 1396 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1397 &samples_per_channel, &num_channels, 1398 &output_type)); 1399 ASSERT_EQ(kBlockSize16kHz, samples_per_channel); 1400 ASSERT_EQ(1u, num_channels); 1401 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels)); 1402 } 1403 } 1404 1405 void NetEqDecodingTest::WrapTest(uint16_t start_seq_no, 1406 uint32_t start_timestamp, 1407 const std::set<uint16_t>& drop_seq_numbers, 1408 bool expect_seq_no_wrap, 1409 bool expect_timestamp_wrap) { 1410 uint16_t seq_no = start_seq_no; 1411 uint32_t timestamp = start_timestamp; 1412 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame. 1413 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs; 1414 const int kSamples = kBlockSize16kHz * kBlocksPerFrame; 1415 const size_t kPayloadBytes = kSamples * sizeof(int16_t); 1416 double next_input_time_ms = 0.0; 1417 int16_t decoded[kBlockSize16kHz]; 1418 size_t num_channels; 1419 size_t samples_per_channel; 1420 NetEqOutputType output_type; 1421 uint32_t receive_timestamp = 0; 1422 1423 // Insert speech for 2 seconds. 1424 const int kSpeechDurationMs = 2000; 1425 int packets_inserted = 0; 1426 uint16_t last_seq_no; 1427 uint32_t last_timestamp; 1428 bool timestamp_wrapped = false; 1429 bool seq_no_wrapped = false; 1430 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { 1431 // Each turn in this for loop is 10 ms. 1432 while (next_input_time_ms <= t_ms) { 1433 // Insert one 30 ms speech frame. 1434 uint8_t payload[kPayloadBytes] = {0}; 1435 WebRtcRTPHeader rtp_info; 1436 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 1437 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) { 1438 // This sequence number was not in the set to drop. Insert it. 1439 ASSERT_EQ(0, 1440 neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); 1441 ++packets_inserted; 1442 } 1443 NetEqNetworkStatistics network_stats; 1444 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); 1445 1446 // Due to internal NetEq logic, preferred buffer-size is about 4 times the 1447 // packet size for first few packets. Therefore we refrain from checking 1448 // the criteria. 1449 if (packets_inserted > 4) { 1450 // Expect preferred and actual buffer size to be no more than 2 frames. 1451 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2); 1452 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 + 1453 algorithmic_delay_ms_); 1454 } 1455 last_seq_no = seq_no; 1456 last_timestamp = timestamp; 1457 1458 ++seq_no; 1459 timestamp += kSamples; 1460 receive_timestamp += kSamples; 1461 next_input_time_ms += static_cast<double>(kFrameSizeMs); 1462 1463 seq_no_wrapped |= seq_no < last_seq_no; 1464 timestamp_wrapped |= timestamp < last_timestamp; 1465 } 1466 // Pull out data once. 1467 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, 1468 &samples_per_channel, &num_channels, 1469 &output_type)); 1470 ASSERT_EQ(kBlockSize16kHz, samples_per_channel); 1471 ASSERT_EQ(1u, num_channels); 1472 1473 // Expect delay (in samples) to be less than 2 packets. 1474 EXPECT_LE(timestamp - PlayoutTimestamp(), 1475 static_cast<uint32_t>(kSamples * 2)); 1476 } 1477 // Make sure we have actually tested wrap-around. 1478 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped); 1479 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped); 1480 } 1481 1482 TEST_F(NetEqDecodingTest, SequenceNumberWrap) { 1483 // Start with a sequence number that will soon wrap. 1484 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets. 1485 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); 1486 } 1487 1488 TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) { 1489 // Start with a sequence number that will soon wrap. 1490 std::set<uint16_t> drop_seq_numbers; 1491 drop_seq_numbers.insert(0xFFFF); 1492 drop_seq_numbers.insert(0x0); 1493 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); 1494 } 1495 1496 TEST_F(NetEqDecodingTest, TimestampWrap) { 1497 // Start with a timestamp that will soon wrap. 1498 std::set<uint16_t> drop_seq_numbers; 1499 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true); 1500 } 1501 1502 TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) { 1503 // Start with a timestamp and a sequence number that will wrap at the same 1504 // time. 1505 std::set<uint16_t> drop_seq_numbers; 1506 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true); 1507 } 1508 1509 void NetEqDecodingTest::DuplicateCng() { 1510 uint16_t seq_no = 0; 1511 uint32_t timestamp = 0; 1512 const int kFrameSizeMs = 10; 1513 const int kSampleRateKhz = 16; 1514 const int kSamples = kFrameSizeMs * kSampleRateKhz; 1515 const size_t kPayloadBytes = kSamples * 2; 1516 1517 const int algorithmic_delay_samples = std::max( 1518 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8); 1519 // Insert three speech packets. Three are needed to get the frame length 1520 // correct. 1521 size_t out_len; 1522 size_t num_channels; 1523 NetEqOutputType type; 1524 uint8_t payload[kPayloadBytes] = {0}; 1525 WebRtcRTPHeader rtp_info; 1526 for (int i = 0; i < 3; ++i) { 1527 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 1528 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 1529 ++seq_no; 1530 timestamp += kSamples; 1531 1532 // Pull audio once. 1533 ASSERT_EQ(0, 1534 neteq_->GetAudio( 1535 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 1536 ASSERT_EQ(kBlockSize16kHz, out_len); 1537 } 1538 // Verify speech output. 1539 EXPECT_EQ(kOutputNormal, type); 1540 1541 // Insert same CNG packet twice. 1542 const int kCngPeriodMs = 100; 1543 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; 1544 size_t payload_len; 1545 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); 1546 // This is the first time this CNG packet is inserted. 1547 ASSERT_EQ( 1548 0, neteq_->InsertPacket( 1549 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); 1550 1551 // Pull audio once and make sure CNG is played. 1552 ASSERT_EQ(0, 1553 neteq_->GetAudio( 1554 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 1555 ASSERT_EQ(kBlockSize16kHz, out_len); 1556 EXPECT_EQ(kOutputCNG, type); 1557 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp()); 1558 1559 // Insert the same CNG packet again. Note that at this point it is old, since 1560 // we have already decoded the first copy of it. 1561 ASSERT_EQ( 1562 0, neteq_->InsertPacket( 1563 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); 1564 1565 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since 1566 // we have already pulled out CNG once. 1567 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) { 1568 ASSERT_EQ(0, 1569 neteq_->GetAudio( 1570 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 1571 ASSERT_EQ(kBlockSize16kHz, out_len); 1572 EXPECT_EQ(kOutputCNG, type); 1573 EXPECT_EQ(timestamp - algorithmic_delay_samples, 1574 PlayoutTimestamp()); 1575 } 1576 1577 // Insert speech again. 1578 ++seq_no; 1579 timestamp += kCngPeriodSamples; 1580 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 1581 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 1582 1583 // Pull audio once and verify that the output is speech again. 1584 ASSERT_EQ(0, 1585 neteq_->GetAudio( 1586 kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); 1587 ASSERT_EQ(kBlockSize16kHz, out_len); 1588 EXPECT_EQ(kOutputNormal, type); 1589 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples, 1590 PlayoutTimestamp()); 1591 } 1592 1593 uint32_t NetEqDecodingTest::PlayoutTimestamp() { 1594 uint32_t playout_timestamp = 0; 1595 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp)); 1596 return playout_timestamp; 1597 } 1598 1599 TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); } 1600 1601 TEST_F(NetEqDecodingTest, CngFirst) { 1602 uint16_t seq_no = 0; 1603 uint32_t timestamp = 0; 1604 const int kFrameSizeMs = 10; 1605 const int kSampleRateKhz = 16; 1606 const int kSamples = kFrameSizeMs * kSampleRateKhz; 1607 const int kPayloadBytes = kSamples * 2; 1608 const int kCngPeriodMs = 100; 1609 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; 1610 size_t payload_len; 1611 1612 uint8_t payload[kPayloadBytes] = {0}; 1613 WebRtcRTPHeader rtp_info; 1614 1615 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); 1616 ASSERT_EQ( 1617 NetEq::kOK, 1618 neteq_->InsertPacket( 1619 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); 1620 ++seq_no; 1621 timestamp += kCngPeriodSamples; 1622 1623 // Pull audio once and make sure CNG is played. 1624 size_t out_len; 1625 size_t num_channels; 1626 NetEqOutputType type; 1627 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 1628 &num_channels, &type)); 1629 ASSERT_EQ(kBlockSize16kHz, out_len); 1630 EXPECT_EQ(kOutputCNG, type); 1631 1632 // Insert some speech packets. 1633 for (int i = 0; i < 3; ++i) { 1634 PopulateRtpInfo(seq_no, timestamp, &rtp_info); 1635 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); 1636 ++seq_no; 1637 timestamp += kSamples; 1638 1639 // Pull audio once. 1640 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, 1641 &num_channels, &type)); 1642 ASSERT_EQ(kBlockSize16kHz, out_len); 1643 } 1644 // Verify speech output. 1645 EXPECT_EQ(kOutputNormal, type); 1646 } 1647 1648 } // namespace webrtc 1649