Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq / neteq_unittest.cc
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/interface/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/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"
34
35 DEFINE_bool(gen_ref, false, "Generate reference files.");
36
37 namespace webrtc {
38
39 static bool IsAllZero(const int16_t* buf, int buf_length) {
40   bool all_zero = true;
41   for (int n = 0; n < buf_length && all_zero; ++n)
42     all_zero = buf[n] == 0;
43   return all_zero;
44 }
45
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;
50   return all_non_zero;
51 }
52
53 class RefFiles {
54  public:
55   RefFiles(const std::string& input_file, const std::string& output_file);
56   ~RefFiles();
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],
60       size_t length);
61   template<typename T, size_t n> void WriteToFile(
62       const T (&test_results)[n],
63       size_t length);
64   template<typename T, size_t n> void ReadFromFileAndCompare(
65       const T (&test_results)[n],
66       size_t length);
67   void WriteToFile(const NetEqNetworkStatistics& stats);
68   void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
69   void WriteToFile(const RtcpStatistics& stats);
70   void ReadFromFileAndCompare(const RtcpStatistics& stats);
71
72   FILE* input_fp_;
73   FILE* output_fp_;
74 };
75
76 RefFiles::RefFiles(const std::string &input_file,
77                    const std::string &output_file)
78     : input_fp_(NULL),
79       output_fp_(NULL) {
80   if (!input_file.empty()) {
81     input_fp_ = fopen(input_file.c_str(), "rb");
82     EXPECT_TRUE(input_fp_ != NULL);
83   }
84   if (!output_file.empty()) {
85     output_fp_ = fopen(output_file.c_str(), "wb");
86     EXPECT_TRUE(output_fp_ != NULL);
87   }
88 }
89
90 RefFiles::~RefFiles() {
91   if (input_fp_) {
92     EXPECT_EQ(EOF, fgetc(input_fp_));  // Make sure that we reached the end.
93     fclose(input_fp_);
94   }
95   if (output_fp_) fclose(output_fp_);
96 }
97
98 template<class T>
99 void RefFiles::ProcessReference(const T& test_results) {
100   WriteToFile(test_results);
101   ReadFromFileAndCompare(test_results);
102 }
103
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);
108 }
109
110 template<typename T, size_t n>
111 void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
112   if (output_fp_) {
113     ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
114   }
115 }
116
117 template<typename T, size_t n>
118 void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
119                                       size_t length) {
120   if (input_fp_) {
121     // Read from ref file.
122     T* ref = new T[length];
123     ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
124     // Compare
125     ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
126     delete [] ref;
127   }
128 }
129
130 void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
131   if (output_fp_) {
132     ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
133                          output_fp_));
134   }
135 }
136
137 void RefFiles::ReadFromFileAndCompare(
138     const NetEqNetworkStatistics& stats) {
139   if (input_fp_) {
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_));
144     // Compare
145     ASSERT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
146   }
147 }
148
149 void RefFiles::WriteToFile(const RtcpStatistics& stats) {
150   if (output_fp_) {
151     ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
152                          output_fp_));
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,
157                          output_fp_));
158     ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
159                          output_fp_));
160   }
161 }
162
163 void RefFiles::ReadFromFileAndCompare(
164     const RtcpStatistics& stats) {
165   if (input_fp_) {
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,
174                         input_fp_));
175     ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
176                         input_fp_));
177     // Compare
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);
183   }
184 }
185
186 class NetEqDecodingTest : public ::testing::Test {
187  protected:
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;
196
197   NetEqDecodingTest();
198   virtual void SetUp();
199   virtual void TearDown();
200   void SelectDecoders(NetEqDecoder* used_codec);
201   void LoadDecoders();
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,
209                               int timestamp,
210                               WebRtcRTPHeader* rtp_info);
211   static void PopulateCng(int frame_index,
212                           int timestamp,
213                           WebRtcRTPHeader* rtp_info,
214                           uint8_t* payload,
215                           int* payload_len);
216
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);
220
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);
226
227   void DuplicateCng();
228
229   uint32_t PlayoutTimestamp();
230
231   NetEq* neteq_;
232   NetEq::Config config_;
233   FILE* rtp_fp_;
234   unsigned int sim_clock_;
235   int16_t out_data_[kMaxBlockSize];
236   int output_sample_rate_;
237   int algorithmic_delay_ms_;
238 };
239
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;
247
248 NetEqDecodingTest::NetEqDecodingTest()
249     : neteq_(NULL),
250       config_(),
251       rtp_fp_(NULL),
252       sim_clock_(0),
253       output_sample_rate_(kInitSampleRateHz),
254       algorithmic_delay_ms_(0) {
255   config_.sample_rate_hz = kInitSampleRateHz;
256   memset(out_data_, 0, sizeof(out_data_));
257 }
258
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;
264   ASSERT_TRUE(neteq_);
265   LoadDecoders();
266 }
267
268 void NetEqDecodingTest::TearDown() {
269   delete neteq_;
270   if (rtp_fp_)
271     fclose(rtp_fp_);
272 }
273
274 void NetEqDecodingTest::LoadDecoders() {
275   // Load PCMu.
276   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
277   // Load PCMa.
278   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
279 #ifndef WEBRTC_ANDROID
280   // Load iLBC.
281   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
282 #endif  // WEBRTC_ANDROID
283   // Load iSAC.
284   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
285 #ifndef WEBRTC_ANDROID
286   // Load iSAC SWB.
287   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
288   // Load iSAC FB.
289   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
290 #endif  // WEBRTC_ANDROID
291   // Load PCM16B nb.
292   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
293   // Load PCM16B wb.
294   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
295   // Load PCM16B swb32.
296   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
297   // Load CNG 8 kHz.
298   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
299   // Load CNG 16 kHz.
300   ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
301 }
302
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_));
307 }
308
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(
317           rtpInfo,
318           rtp->payload(),
319           rtp->payloadLen(),
320           rtp->time() * (output_sample_rate_ / 1000)));
321     }
322     // Get next packet.
323     ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
324   }
325
326   // Get audio from NetEq.
327   NetEqOutputType type;
328   int num_channels;
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;
335
336   // Increase time.
337   sim_clock_ += kTimeStepMs;
338 }
339
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);
345
346   std::string ref_out_file = "";
347   if (ref_file.empty()) {
348     ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
349   }
350   RefFiles ref_files(ref_file, ref_out_file);
351
352   std::string stat_out_file = "";
353   if (stat_ref_file.empty()) {
354     stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
355   }
356   RefFiles network_stat_files(stat_ref_file, stat_out_file);
357
358   std::string rtcp_out_file = "";
359   if (rtcp_ref_file.empty()) {
360     rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
361   }
362   RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
363
364   NETEQTEST_RTPpacket rtp;
365   ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
366   int i = 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.
371     int out_len = 0;
372     ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
373     ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
374
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));
382
383       // Process RTCPstat.
384       RtcpStatistics rtcp_stats;
385       neteq_->GetRtcpStatistics(&rtcp_stats);
386       ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
387     }
388   }
389 }
390
391 void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
392                                         int timestamp,
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;
399 }
400
401 void NetEqDecodingTest::PopulateCng(int frame_index,
402                                     int timestamp,
403                                     WebRtcRTPHeader* rtp_info,
404                                     uint8_t* payload,
405                                     int* payload_len) {
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.
413 }
414
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";
428 #else
429   const std::string network_stat_ref_file =
430       webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
431 #endif
432   const std::string rtcp_stat_ref_file =
433       webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
434
435   if (FLAGS_gen_ref) {
436     DecodeAndCompare(input_rtp_file, "", "", "");
437   } else {
438     DecodeAndCompare(input_rtp_file,
439                      input_ref_file,
440                      network_stat_ref_file,
441                      rtcp_stat_ref_file);
442   }
443 }
444
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 {
448  protected:
449   NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
450     config_.playout_mode = kPlayoutFax;
451   }
452 };
453
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(
468         rtp_info,
469         reinterpret_cast<uint8_t*>(payload),
470         kPayloadBytes, 0));
471   }
472   // Pull out all data.
473   for (size_t i = 0; i < num_frames; ++i) {
474     int out_len;
475     int num_channels;
476     NetEqOutputType type;
477     ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
478                                   &num_channels, &type));
479     ASSERT_EQ(kBlockSize16kHz, out_len);
480   }
481
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
487   // each packet.
488   for (size_t i = 0; i < waiting_times.size(); ++i) {
489     EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
490   }
491
492   // Check statistics again and make sure it's been reset.
493   neteq_->WaitingTimes(&waiting_times);
494   int len = waiting_times.size();
495   EXPECT_EQ(0, len);
496
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.
499   num_frames = 110;
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(
509         rtp_info,
510         reinterpret_cast<uint8_t*>(payload),
511         kPayloadBytes, 0));
512     int out_len;
513     int num_channels;
514     NetEqOutputType type;
515     ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
516                                   &num_channels, &type));
517     ASSERT_EQ(kBlockSize16kHz, out_len);
518   }
519
520   neteq_->WaitingTimes(&waiting_times);
521   EXPECT_EQ(100u, waiting_times.size());
522 }
523
524 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
525   const int kNumFrames = 3000;  // Needed for convergence.
526   int frame_index = 0;
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));
538       ++frame_index;
539     }
540
541     // Pull out data once.
542     int out_len;
543     int num_channels;
544     NetEqOutputType type;
545     ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
546                                   &num_channels, &type));
547     ASSERT_EQ(kBlockSize16kHz, out_len);
548   }
549
550   NetEqNetworkStatistics network_stats;
551   ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
552   EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
553 }
554
555 TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
556   const int kNumFrames = 5000;  // Needed for convergence.
557   int frame_index = 0;
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));
569       ++frame_index;
570     }
571
572     // Pull out data once.
573     int out_len;
574     int num_channels;
575     NetEqOutputType type;
576     ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
577                                   &num_channels, &type));
578     ASSERT_EQ(kBlockSize16kHz, out_len);
579   }
580
581   NetEqNetworkStatistics network_stats;
582   ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
583   EXPECT_EQ(110946, network_stats.clockdrift_ppm);
584 }
585
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) {
591   uint16_t seq_no = 0;
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;
597   double t_ms;
598   int out_len;
599   int num_channels;
600   NetEqOutputType type;
601
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));
612       ++seq_no;
613       timestamp += kSamples;
614       next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
615     }
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);
620   }
621
622   EXPECT_EQ(kOutputNormal, type);
623   int32_t delay_before = timestamp - PlayoutTimestamp();
624
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];
634       int payload_len;
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));
638       ++seq_no;
639       timestamp += kCngPeriodSamples;
640       next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
641     }
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);
646   }
647
648   EXPECT_EQ(kOutputCNG, type);
649
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.
657       ASSERT_EQ(0,
658                 neteq_->GetAudio(
659                     kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
660       ASSERT_EQ(kBlockSize16kHz, out_len);
661       EXPECT_EQ(kOutputCNG, type);
662     }
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) {
669         pull_once = false;
670         // Pull out data once.
671         ASSERT_EQ(
672             0,
673             neteq_->GetAudio(
674                 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
675         ASSERT_EQ(kBlockSize16kHz, out_len);
676         EXPECT_EQ(kOutputCNG, type);
677         t_ms += 10;
678       }
679       // Insert one CNG frame each 100 ms.
680       uint8_t payload[kPayloadBytes];
681       int payload_len;
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));
685       ++seq_no;
686       timestamp += kCngPeriodSamples;
687       next_input_time_ms += kCngPeriodMs * drift_factor;
688     }
689   }
690
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));
701       ++seq_no;
702       timestamp += kSamples;
703       next_input_time_ms += kFrameSizeMs * drift_factor;
704     }
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);
709     // Increase clock.
710     t_ms += 10;
711   }
712
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);
720 }
721
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,
732                         kDelayToleranceMs,
733                         kMaxTimeToSpeechMs);
734 }
735
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,
746                         kDelayToleranceMs,
747                         kMaxTimeToSpeechMs);
748 }
749
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,
760                         kDelayToleranceMs,
761                         kMaxTimeToSpeechMs);
762 }
763
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,
774                         kDelayToleranceMs,
775                         kMaxTimeToSpeechMs);
776 }
777
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,
788                         kDelayToleranceMs,
789                         kMaxTimeToSpeechMs);
790 }
791
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,
801                         kDelayToleranceMs,
802                         kMaxTimeToSpeechMs);
803 }
804
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());
814 }
815
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
825   // to GetAudio.
826   for (int i = 0; i < kMaxBlockSize; ++i) {
827     out_data_[i] = 1;
828   }
829   int num_channels;
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;
843     ss << "i = " << i;
844     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
845     EXPECT_EQ(0, out_data_[i]);
846   }
847   for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
848     std::ostringstream ss;
849     ss << "i = " << i;
850     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
851     EXPECT_EQ(1, out_data_[i]);
852   }
853 }
854
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
858   // to GetAudio.
859   for (int i = 0; i < kMaxBlockSize; ++i) {
860     out_data_[i] = 1;
861   }
862   int num_channels;
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;
872     ss << "i = " << i;
873     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
874     EXPECT_EQ(0, out_data_[i]);
875   }
876 }
877
878 class NetEqBgnTest : public NetEqDecodingTest {
879  protected:
880   virtual void TestCondition(double sum_squared_noise,
881                              bool should_be_faded) = 0;
882
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.
895     } else {
896       ASSERT_TRUE(false);  // Unsupported test case.
897     }
898
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
904     // valid.
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));
909
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;
915
916     int number_channels = 0;
917     int samples_per_channel = 0;
918
919     uint32_t receive_timestamp = 0;
920     for (int n = 0; n < 10; ++n) {  // Insert few packets and get audio.
921       int enc_len_bytes =
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);
926
927       number_channels = 0;
928       samples_per_channel = 0;
929       ASSERT_EQ(0,
930                 neteq_->InsertPacket(
931                     rtp_info, payload, enc_len_bytes, receive_timestamp));
932       ASSERT_EQ(0,
933                 neteq_->GetAudio(kBlockSize32kHz,
934                                  output,
935                                  &samples_per_channel,
936                                  &number_channels,
937                                  &type));
938       ASSERT_EQ(1, number_channels);
939       ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
940       ASSERT_EQ(kOutputNormal, type);
941
942       // Next packet.
943       rtp_info.header.timestamp += expected_samples_per_channel;
944       rtp_info.header.sequenceNumber++;
945       receive_timestamp += expected_samples_per_channel;
946     }
947
948     number_channels = 0;
949     samples_per_channel = 0;
950
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.
954     ASSERT_EQ(0,
955               neteq_->GetAudio(kBlockSize32kHz,
956                                output,
957                                &samples_per_channel,
958                                &number_channels,
959                                &type));
960     ASSERT_EQ(1, number_channels);
961     ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
962
963     // To be able to test the fading of background noise we need at lease to
964     // pull 611 frames.
965     const int kFadingThreshold = 611;
966
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) {
972       number_channels = 0;
973       samples_per_channel = 0;
974       memset(output, 1, sizeof(output));  // Set to non-zero.
975       ASSERT_EQ(0,
976                 neteq_->GetAudio(kBlockSize32kHz,
977                                  output,
978                                  &samples_per_channel,
979                                  &number_channels,
980                                  &type));
981       ASSERT_EQ(1, number_channels);
982       ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
983       if (type == kOutputPLCtoCNG) {
984         plc_to_cng = true;
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);
989       } else {
990         EXPECT_EQ(kOutputPLC, type);
991       }
992     }
993     EXPECT_TRUE(plc_to_cng);  // Just to be sure that PLC-to-CNG has occurred.
994   }
995 };
996
997 class NetEqBgnTestOn : public NetEqBgnTest {
998  protected:
999   NetEqBgnTestOn() : NetEqBgnTest() {
1000     config_.background_noise_mode = NetEq::kBgnOn;
1001   }
1002
1003   void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1004     EXPECT_NE(0, sum_squared_noise);
1005   }
1006 };
1007
1008 class NetEqBgnTestOff : public NetEqBgnTest {
1009  protected:
1010   NetEqBgnTestOff() : NetEqBgnTest() {
1011     config_.background_noise_mode = NetEq::kBgnOff;
1012   }
1013
1014   void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1015     EXPECT_EQ(0, sum_squared_noise);
1016   }
1017 };
1018
1019 class NetEqBgnTestFade : public NetEqBgnTest {
1020  protected:
1021   NetEqBgnTestFade() : NetEqBgnTest() {
1022     config_.background_noise_mode = NetEq::kBgnFade;
1023   }
1024
1025   void TestCondition(double sum_squared_noise, bool should_be_faded) {
1026     if (should_be_faded)
1027       EXPECT_EQ(0, sum_squared_noise);
1028   }
1029 };
1030
1031 TEST_F(NetEqBgnTestOn, RunTest) {
1032   CheckBgn(8000);
1033   CheckBgn(16000);
1034   CheckBgn(32000);
1035 }
1036
1037 TEST_F(NetEqBgnTestOff, RunTest) {
1038   CheckBgn(8000);
1039   CheckBgn(16000);
1040   CheckBgn(32000);
1041 }
1042
1043 TEST_F(NetEqBgnTestFade, RunTest) {
1044   CheckBgn(8000);
1045   CheckBgn(16000);
1046   CheckBgn(32000);
1047 }
1048
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
1053   // this test.
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.
1062
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));
1075
1076   PopulateRtpInfo(0, 0, &rtp_info);
1077   rtp_info.header.payloadType = kPcm16WbPayloadType;
1078
1079   // The first packet injected cannot be sync-packet.
1080   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081
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));
1087
1088   // Next packet. Last packet contained 10 ms audio.
1089   rtp_info.header.sequenceNumber++;
1090   rtp_info.header.timestamp += kBlockSize16kHz;
1091   receive_timestamp += kBlockSize16kHz;
1092
1093   // Unacceptable payload types CNG, AVT (DTMF), RED.
1094   rtp_info.header.payloadType = kCngNbPayloadType;
1095   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1096
1097   rtp_info.header.payloadType = kCngWbPayloadType;
1098   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1099
1100   rtp_info.header.payloadType = kCngSwb32PayloadType;
1101   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1102
1103   rtp_info.header.payloadType = kCngSwb48PayloadType;
1104   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1105
1106   rtp_info.header.payloadType = kAvtPayloadType;
1107   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1108
1109   rtp_info.header.payloadType = kRedPayloadType;
1110   EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1111
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));
1115
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));
1120
1121   --rtp_info.header.ssrc;
1122   EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1123 }
1124
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.
1140   }
1141   // Insert some packets which decode to noise. We are not interested in
1142   // actual decoded values.
1143   NetEqOutputType output_type;
1144   int num_channels;
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,
1152                                   &output_type));
1153     ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1154     ASSERT_EQ(1, num_channels);
1155
1156     rtp_info.header.sequenceNumber++;
1157     rtp_info.header.timestamp += kBlockSize16kHz;
1158     receive_timestamp += kBlockSize16kHz;
1159   }
1160   const int kNumSyncPackets = 10;
1161
1162   // Make sure sufficient number of sync packets are inserted that we can
1163   // conduct a test.
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,
1170                                   &output_type));
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));
1175     }
1176     rtp_info.header.sequenceNumber++;
1177     rtp_info.header.timestamp += kBlockSize16kHz;
1178     receive_timestamp += kBlockSize16kHz;
1179   }
1180
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,
1188                                   &output_type));
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));
1192     }
1193     rtp_info.header.sequenceNumber++;
1194     rtp_info.header.timestamp += kBlockSize16kHz;
1195     receive_timestamp += kBlockSize16kHz;
1196   }
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);
1204 }
1205
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.
1218   }
1219   // Insert some packets which decode to noise. We are not interested in
1220   // actual decoded values.
1221   NetEqOutputType output_type;
1222   int num_channels;
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,
1231                                   &output_type));
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;
1237   }
1238   const int kNumSyncPackets = 10;
1239
1240   WebRtcRTPHeader first_sync_packet_rtp_info;
1241   memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1242
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;
1249   }
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);
1254
1255   // Rewind |rtp_info| to that of the first sync packet.
1256   memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1257
1258   // Insert.
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;
1265   }
1266
1267   // Decode.
1268   for (int n = 0; n < kNumSyncPackets; ++n) {
1269     ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1270                                   &samples_per_channel, &num_channels,
1271                                   &output_type));
1272     ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1273     ASSERT_EQ(1, num_channels);
1274     EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1275   }
1276 }
1277
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];
1291   int num_channels;
1292   int samples_per_channel;
1293   NetEqOutputType output_type;
1294   uint32_t receive_timestamp = 0;
1295
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.
1312         ASSERT_EQ(0,
1313                   neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1314                                        receive_timestamp));
1315         ++packets_inserted;
1316       }
1317       NetEqNetworkStatistics network_stats;
1318       ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1319
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
1322       // the criteria.
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_);
1328       }
1329       last_seq_no = seq_no;
1330       last_timestamp = timestamp;
1331
1332       ++seq_no;
1333       timestamp += kSamples;
1334       receive_timestamp += kSamples;
1335       next_input_time_ms += static_cast<double>(kFrameSizeMs);
1336
1337       seq_no_wrapped |= seq_no < last_seq_no;
1338       timestamp_wrapped |= timestamp < last_timestamp;
1339     }
1340     // Pull out data once.
1341     ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1342                                   &samples_per_channel, &num_channels,
1343                                   &output_type));
1344     ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1345     ASSERT_EQ(1, num_channels);
1346
1347     // Expect delay (in samples) to be less than 2 packets.
1348     EXPECT_LE(timestamp - PlayoutTimestamp(),
1349               static_cast<uint32_t>(kSamples * 2));
1350   }
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);
1354 }
1355
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);
1360 }
1361
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);
1368 }
1369
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);
1374 }
1375
1376 TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1377   // Start with a timestamp and a sequence number that will wrap at the same
1378   // time.
1379   std::set<uint16_t> drop_seq_numbers;
1380   WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1381 }
1382
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;
1390
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
1394   // correct.
1395   int out_len;
1396   int num_channels;
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));
1403     ++seq_no;
1404     timestamp += kSamples;
1405
1406     // Pull audio once.
1407     ASSERT_EQ(0,
1408               neteq_->GetAudio(
1409                   kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1410     ASSERT_EQ(kBlockSize16kHz, out_len);
1411   }
1412   // Verify speech output.
1413   EXPECT_EQ(kOutputNormal, type);
1414
1415   // Insert same CNG packet twice.
1416   const int kCngPeriodMs = 100;
1417   const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1418   int payload_len;
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));
1422
1423   // Pull audio once and make sure CNG is played.
1424   ASSERT_EQ(0,
1425             neteq_->GetAudio(
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());
1430
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));
1434
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) {
1438     ASSERT_EQ(0,
1439               neteq_->GetAudio(
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());
1445   }
1446
1447   // Insert speech again.
1448   ++seq_no;
1449   timestamp += kCngPeriodSamples;
1450   PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1451   ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1452
1453   // Pull audio once and verify that the output is speech again.
1454   ASSERT_EQ(0,
1455             neteq_->GetAudio(
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());
1461 }
1462
1463 uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1464   uint32_t playout_timestamp = 0;
1465   EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1466   return playout_timestamp;
1467 }
1468
1469 TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
1470 }  // namespace webrtc