blob: 64991550cdaea260e72b8ed9de60d5630e1be196 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * This file includes unit tests for NetEQ.
13 */
14
Henrik Kjellander74640892015-10-29 11:31:02 +010015#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000017#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include <stdlib.h>
19#include <string.h> // memset
20
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000021#include <algorithm>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000022#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023#include <string>
24#include <vector>
25
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000026#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000027#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000028#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000031#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000033#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/typedefs.h"
35
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000036DEFINE_bool(gen_ref, false, "Generate reference files.");
37
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038namespace webrtc {
39
Peter Kastingdce40cf2015-08-24 14:52:23 -070040static bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000041 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000043 all_zero = buf[n] == 0;
44 return all_zero;
45}
46
Peter Kastingdce40cf2015-08-24 14:52:23 -070047static bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000048 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000050 all_non_zero = buf[n] != 0;
51 return all_non_zero;
52}
53
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054class RefFiles {
55 public:
56 RefFiles(const std::string& input_file, const std::string& output_file);
57 ~RefFiles();
58 template<class T> void ProcessReference(const T& test_results);
59 template<typename T, size_t n> void ProcessReference(
60 const T (&test_results)[n],
61 size_t length);
62 template<typename T, size_t n> void WriteToFile(
63 const T (&test_results)[n],
64 size_t length);
65 template<typename T, size_t n> void ReadFromFileAndCompare(
66 const T (&test_results)[n],
67 size_t length);
68 void WriteToFile(const NetEqNetworkStatistics& stats);
69 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
70 void WriteToFile(const RtcpStatistics& stats);
71 void ReadFromFileAndCompare(const RtcpStatistics& stats);
72
73 FILE* input_fp_;
74 FILE* output_fp_;
75};
76
77RefFiles::RefFiles(const std::string &input_file,
78 const std::string &output_file)
79 : input_fp_(NULL),
80 output_fp_(NULL) {
81 if (!input_file.empty()) {
82 input_fp_ = fopen(input_file.c_str(), "rb");
83 EXPECT_TRUE(input_fp_ != NULL);
84 }
85 if (!output_file.empty()) {
86 output_fp_ = fopen(output_file.c_str(), "wb");
87 EXPECT_TRUE(output_fp_ != NULL);
88 }
89}
90
91RefFiles::~RefFiles() {
92 if (input_fp_) {
93 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
94 fclose(input_fp_);
95 }
96 if (output_fp_) fclose(output_fp_);
97}
98
99template<class T>
100void RefFiles::ProcessReference(const T& test_results) {
101 WriteToFile(test_results);
102 ReadFromFileAndCompare(test_results);
103}
104
105template<typename T, size_t n>
106void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
107 WriteToFile(test_results, length);
108 ReadFromFileAndCompare(test_results, length);
109}
110
111template<typename T, size_t n>
112void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
113 if (output_fp_) {
114 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
115 }
116}
117
118template<typename T, size_t n>
119void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
120 size_t length) {
121 if (input_fp_) {
122 // Read from ref file.
123 T* ref = new T[length];
124 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
125 // Compare
126 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
127 delete [] ref;
128 }
129}
130
131void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
132 if (output_fp_) {
133 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
134 output_fp_));
135 }
136}
137
138void RefFiles::ReadFromFileAndCompare(
139 const NetEqNetworkStatistics& stats) {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000140 // TODO(minyue): Update resource/audio_coding/neteq_network_stats.dat and
141 // resource/audio_coding/neteq_network_stats_win32.dat.
142 struct NetEqNetworkStatisticsOld {
143 uint16_t current_buffer_size_ms; // Current jitter buffer size in ms.
144 uint16_t preferred_buffer_size_ms; // Target buffer size in ms.
145 uint16_t jitter_peaks_found; // 1 if adding extra delay due to peaky
146 // jitter; 0 otherwise.
147 uint16_t packet_loss_rate; // Loss rate (network + late) in Q14.
148 uint16_t packet_discard_rate; // Late loss rate in Q14.
149 uint16_t expand_rate; // Fraction (of original stream) of synthesized
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000150 // audio inserted through expansion (in Q14).
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000151 uint16_t preemptive_rate; // Fraction of data inserted through pre-emptive
152 // expansion (in Q14).
153 uint16_t accelerate_rate; // Fraction of data removed through acceleration
154 // (in Q14).
155 int32_t clockdrift_ppm; // Average clock-drift in parts-per-million
156 // (positive or negative).
157 int added_zero_samples; // Number of zero samples added in "off" mode.
158 };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 if (input_fp_) {
160 // Read from ref file.
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000161 size_t stat_size = sizeof(NetEqNetworkStatisticsOld);
162 NetEqNetworkStatisticsOld ref_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
164 // Compare
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000165 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms);
166 ASSERT_EQ(stats.preferred_buffer_size_ms,
167 ref_stats.preferred_buffer_size_ms);
168 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found);
169 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate);
170 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate);
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000171 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate);
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000172 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate);
173 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate);
174 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700175 ASSERT_EQ(stats.added_zero_samples,
176 static_cast<size_t>(ref_stats.added_zero_samples));
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000177 ASSERT_EQ(stats.secondary_decoded_rate, 0);
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000178 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179 }
180}
181
182void RefFiles::WriteToFile(const RtcpStatistics& stats) {
183 if (output_fp_) {
184 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
185 output_fp_));
186 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
187 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000188 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
189 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 output_fp_));
191 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
192 output_fp_));
193 }
194}
195
196void RefFiles::ReadFromFileAndCompare(
197 const RtcpStatistics& stats) {
198 if (input_fp_) {
199 // Read from ref file.
200 RtcpStatistics ref_stats;
201 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
202 sizeof(ref_stats.fraction_lost), 1, input_fp_));
203 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
204 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000205 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
206 sizeof(ref_stats.extended_max_sequence_number), 1,
207 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
209 input_fp_));
210 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000211 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
212 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
213 ASSERT_EQ(ref_stats.extended_max_sequence_number,
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000214 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000215 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216 }
217}
218
219class NetEqDecodingTest : public ::testing::Test {
220 protected:
221 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
222 // constants below can be changed.
223 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700224 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
225 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
226 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000227 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228 static const int kInitSampleRateHz = 8000;
229
230 NetEqDecodingTest();
231 virtual void SetUp();
232 virtual void TearDown();
233 void SelectDecoders(NetEqDecoder* used_codec);
234 void LoadDecoders();
235 void OpenInputFile(const std::string &rtp_file);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236 void Process(size_t* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000237 void DecodeAndCompare(const std::string& rtp_file,
238 const std::string& ref_file,
239 const std::string& stat_ref_file,
240 const std::string& rtcp_ref_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 static void PopulateRtpInfo(int frame_index,
242 int timestamp,
243 WebRtcRTPHeader* rtp_info);
244 static void PopulateCng(int frame_index,
245 int timestamp,
246 WebRtcRTPHeader* rtp_info,
247 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000248 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000250 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
251 const std::set<uint16_t>& drop_seq_numbers,
252 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
253
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000254 void LongCngWithClockDrift(double drift_factor,
255 double network_freeze_ms,
256 bool pull_audio_during_freeze,
257 int delay_tolerance_ms,
258 int max_time_to_speech_ms);
259
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000260 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000261
wu@webrtc.org94454b72014-06-05 20:34:08 +0000262 uint32_t PlayoutTimestamp();
263
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000265 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000266 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
267 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 unsigned int sim_clock_;
269 int16_t out_data_[kMaxBlockSize];
270 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000271 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272};
273
274// Allocating the static const so that it can be passed by reference.
275const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700276const size_t NetEqDecodingTest::kBlockSize8kHz;
277const size_t NetEqDecodingTest::kBlockSize16kHz;
278const size_t NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000279const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280const int NetEqDecodingTest::kInitSampleRateHz;
281
282NetEqDecodingTest::NetEqDecodingTest()
283 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000284 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000286 output_sample_rate_(kInitSampleRateHz),
287 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000288 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 memset(out_data_, 0, sizeof(out_data_));
290}
291
292void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000293 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000294 NetEqNetworkStatistics stat;
295 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
296 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 ASSERT_TRUE(neteq_);
298 LoadDecoders();
299}
300
301void NetEqDecodingTest::TearDown() {
302 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303}
304
305void NetEqDecodingTest::LoadDecoders() {
306 // Load PCMu.
307 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
308 // Load PCMa.
309 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700310#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 // Load iLBC.
312 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700313#endif
314#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 // Load iSAC.
316 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700317#endif
318#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319 // Load iSAC SWB.
320 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700321#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 // Load PCM16B nb.
323 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
324 // Load PCM16B wb.
325 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
326 // Load PCM16B swb32.
327 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
328 // Load CNG 8 kHz.
329 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
330 // Load CNG 16 kHz.
331 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
332}
333
334void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000335 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336}
337
Peter Kastingdce40cf2015-08-24 14:52:23 -0700338void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000340 while (packet_ && sim_clock_ >= packet_->time_ms()) {
341 if (packet_->payload_length_bytes() > 0) {
342 WebRtcRTPHeader rtp_header;
343 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000345 rtp_header, packet_->payload(),
346 packet_->payload_length_bytes(),
Peter Kastingb7e50542015-06-11 12:55:50 -0700347 static_cast<uint32_t>(
348 packet_->time_ms() * (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 }
350 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000351 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 }
353
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000354 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 NetEqOutputType type;
356 int num_channels;
357 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
358 &num_channels, &type));
359 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
360 (*out_len == kBlockSize16kHz) ||
361 (*out_len == kBlockSize32kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700362 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363
364 // Increase time.
365 sim_clock_ += kTimeStepMs;
366}
367
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000368void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
369 const std::string& ref_file,
370 const std::string& stat_ref_file,
371 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 OpenInputFile(rtp_file);
373
374 std::string ref_out_file = "";
375 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000376 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 }
378 RefFiles ref_files(ref_file, ref_out_file);
379
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000380 std::string stat_out_file = "";
381 if (stat_ref_file.empty()) {
382 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
383 }
384 RefFiles network_stat_files(stat_ref_file, stat_out_file);
385
386 std::string rtcp_out_file = "";
387 if (rtcp_ref_file.empty()) {
388 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
389 }
390 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
391
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000392 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000394 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 std::ostringstream ss;
396 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
397 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700398 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000399 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401
402 // Query the network statistics API once per second
403 if (sim_clock_ % 1000 == 0) {
404 // Process NetworkStatistics.
405 NetEqNetworkStatistics network_stats;
406 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000407 ASSERT_NO_FATAL_FAILURE(
408 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700409 // Compare with CurrentDelay, which should be identical.
410 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000411
412 // Process RTCPstat.
413 RtcpStatistics rtcp_stats;
414 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000415 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000416 }
417 }
418}
419
420void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
421 int timestamp,
422 WebRtcRTPHeader* rtp_info) {
423 rtp_info->header.sequenceNumber = frame_index;
424 rtp_info->header.timestamp = timestamp;
425 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
426 rtp_info->header.payloadType = 94; // PCM16b WB codec.
427 rtp_info->header.markerBit = 0;
428}
429
430void NetEqDecodingTest::PopulateCng(int frame_index,
431 int timestamp,
432 WebRtcRTPHeader* rtp_info,
433 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000434 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000435 rtp_info->header.sequenceNumber = frame_index;
436 rtp_info->header.timestamp = timestamp;
437 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
438 rtp_info->header.payloadType = 98; // WB CNG.
439 rtp_info->header.markerBit = 0;
440 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
441 *payload_len = 1; // Only noise level, no spectral parameters.
442}
443
kwiberg98ab3a42015-09-30 21:54:21 -0700444#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISAC)) && \
445 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
446#define IF_ALL_CODECS(x) x
447#else
448#define IF_ALL_CODECS(x) DISABLED_##x
449#endif
450
henrikaa2c79402015-06-10 13:24:48 +0200451TEST_F(NetEqDecodingTest,
kwiberg98ab3a42015-09-30 21:54:21 -0700452 DISABLED_ON_IOS(DISABLED_ON_ANDROID(IF_ALL_CODECS(TestBitExactness)))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000453 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000454 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000455 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
456 // are identical. The latter could have been removed, but if clients still
457 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000458 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000459 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000460#if defined(_MSC_VER) && (_MSC_VER >= 1700)
461 // For Visual Studio 2012 and later, we will have to use the generic reference
462 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000463 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000464 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000465#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000466 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000467 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000468#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000469 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000470 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000471
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000472 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000473 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000474 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000475 DecodeAndCompare(input_rtp_file,
476 input_ref_file,
477 network_stat_ref_file,
478 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000479 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000480}
481
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000482// Use fax mode to avoid time-scaling. This is to simplify the testing of
483// packet waiting times in the packet buffer.
484class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
485 protected:
486 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
487 config_.playout_mode = kPlayoutFax;
488 }
489};
490
491TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000492 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
493 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000494 const size_t kSamples = 10 * 16;
495 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000496 for (size_t i = 0; i < num_frames; ++i) {
497 uint16_t payload[kSamples] = {0};
498 WebRtcRTPHeader rtp_info;
499 rtp_info.header.sequenceNumber = i;
500 rtp_info.header.timestamp = i * kSamples;
501 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
502 rtp_info.header.payloadType = 94; // PCM16b WB codec.
503 rtp_info.header.markerBit = 0;
504 ASSERT_EQ(0, neteq_->InsertPacket(
505 rtp_info,
506 reinterpret_cast<uint8_t*>(payload),
507 kPayloadBytes, 0));
508 }
509 // Pull out all data.
510 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700511 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000512 int num_channels;
513 NetEqOutputType type;
514 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
515 &num_channels, &type));
516 ASSERT_EQ(kBlockSize16kHz, out_len);
517 }
518
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200519 NetEqNetworkStatistics stats;
520 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000521 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
522 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200523 // each packet. Thus, we are calculating the statistics for a series from 10
524 // to 300, in steps of 10 ms.
525 EXPECT_EQ(155, stats.mean_waiting_time_ms);
526 EXPECT_EQ(155, stats.median_waiting_time_ms);
527 EXPECT_EQ(10, stats.min_waiting_time_ms);
528 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000529
530 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200531 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
532 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
533 EXPECT_EQ(-1, stats.median_waiting_time_ms);
534 EXPECT_EQ(-1, stats.min_waiting_time_ms);
535 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000536}
537
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000538TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000539 const int kNumFrames = 3000; // Needed for convergence.
540 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000541 const size_t kSamples = 10 * 16;
542 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000543 while (frame_index < kNumFrames) {
544 // Insert one packet each time, except every 10th time where we insert two
545 // packets at once. This will create a negative clock-drift of approx. 10%.
546 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
547 for (int n = 0; n < num_packets; ++n) {
548 uint8_t payload[kPayloadBytes] = {0};
549 WebRtcRTPHeader rtp_info;
550 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
551 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
552 ++frame_index;
553 }
554
555 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700556 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557 int num_channels;
558 NetEqOutputType type;
559 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
560 &num_channels, &type));
561 ASSERT_EQ(kBlockSize16kHz, out_len);
562 }
563
564 NetEqNetworkStatistics network_stats;
565 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
566 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
567}
568
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000569TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000570 const int kNumFrames = 5000; // Needed for convergence.
571 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000572 const size_t kSamples = 10 * 16;
573 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574 for (int i = 0; i < kNumFrames; ++i) {
575 // Insert one packet each time, except every 10th time where we don't insert
576 // any packet. This will create a positive clock-drift of approx. 11%.
577 int num_packets = (i % 10 == 9 ? 0 : 1);
578 for (int n = 0; n < num_packets; ++n) {
579 uint8_t payload[kPayloadBytes] = {0};
580 WebRtcRTPHeader rtp_info;
581 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
582 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
583 ++frame_index;
584 }
585
586 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700587 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000588 int num_channels;
589 NetEqOutputType type;
590 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
591 &num_channels, &type));
592 ASSERT_EQ(kBlockSize16kHz, out_len);
593 }
594
595 NetEqNetworkStatistics network_stats;
596 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
597 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
598}
599
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000600void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
601 double network_freeze_ms,
602 bool pull_audio_during_freeze,
603 int delay_tolerance_ms,
604 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 uint16_t seq_no = 0;
606 uint32_t timestamp = 0;
607 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000608 const size_t kSamples = kFrameSizeMs * 16;
609 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 double next_input_time_ms = 0.0;
611 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700612 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000613 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 NetEqOutputType type;
615
616 // Insert speech for 5 seconds.
617 const int kSpeechDurationMs = 5000;
618 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
619 // Each turn in this for loop is 10 ms.
620 while (next_input_time_ms <= t_ms) {
621 // Insert one 30 ms speech frame.
622 uint8_t payload[kPayloadBytes] = {0};
623 WebRtcRTPHeader rtp_info;
624 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
625 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
626 ++seq_no;
627 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000628 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629 }
630 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000631 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
632 &num_channels, &type));
633 ASSERT_EQ(kBlockSize16kHz, out_len);
634 }
635
636 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000637 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638
639 // Insert CNG for 1 minute (= 60000 ms).
640 const int kCngPeriodMs = 100;
641 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
642 const int kCngDurationMs = 60000;
643 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
644 // Each turn in this for loop is 10 ms.
645 while (next_input_time_ms <= t_ms) {
646 // Insert one CNG frame each 100 ms.
647 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000648 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649 WebRtcRTPHeader rtp_info;
650 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
651 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
652 ++seq_no;
653 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000654 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655 }
656 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
658 &num_channels, &type));
659 ASSERT_EQ(kBlockSize16kHz, out_len);
660 }
661
662 EXPECT_EQ(kOutputCNG, type);
663
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000664 if (network_freeze_ms > 0) {
665 // First keep pulling audio for |network_freeze_ms| without inserting
666 // any data, then insert CNG data corresponding to |network_freeze_ms|
667 // without pulling any output audio.
668 const double loop_end_time = t_ms + network_freeze_ms;
669 for (; t_ms < loop_end_time; t_ms += 10) {
670 // Pull out data once.
671 ASSERT_EQ(0,
672 neteq_->GetAudio(
673 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
674 ASSERT_EQ(kBlockSize16kHz, out_len);
675 EXPECT_EQ(kOutputCNG, type);
676 }
677 bool pull_once = pull_audio_during_freeze;
678 // If |pull_once| is true, GetAudio will be called once half-way through
679 // the network recovery period.
680 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
681 while (next_input_time_ms <= t_ms) {
682 if (pull_once && next_input_time_ms >= pull_time_ms) {
683 pull_once = false;
684 // Pull out data once.
685 ASSERT_EQ(
686 0,
687 neteq_->GetAudio(
688 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
689 ASSERT_EQ(kBlockSize16kHz, out_len);
690 EXPECT_EQ(kOutputCNG, type);
691 t_ms += 10;
692 }
693 // Insert one CNG frame each 100 ms.
694 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000695 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000696 WebRtcRTPHeader rtp_info;
697 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
698 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
699 ++seq_no;
700 timestamp += kCngPeriodSamples;
701 next_input_time_ms += kCngPeriodMs * drift_factor;
702 }
703 }
704
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000705 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000706 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000707 while (type != kOutputNormal) {
708 // Each turn in this for loop is 10 ms.
709 while (next_input_time_ms <= t_ms) {
710 // Insert one 30 ms speech frame.
711 uint8_t payload[kPayloadBytes] = {0};
712 WebRtcRTPHeader rtp_info;
713 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
714 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
715 ++seq_no;
716 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000717 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000718 }
719 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
721 &num_channels, &type));
722 ASSERT_EQ(kBlockSize16kHz, out_len);
723 // Increase clock.
724 t_ms += 10;
725 }
726
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000727 // Check that the speech starts again within reasonable time.
728 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
729 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000730 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
733 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734}
735
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000736TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000737 // Apply a clock drift of -25 ms / s (sender faster than receiver).
738 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000739 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);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000748}
749
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000750TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000751 // Apply a clock drift of +25 ms / s (sender slower than receiver).
752 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000753 const double kNetworkFreezeTimeMs = 0.0;
754 const bool kGetAudioDuringFreezeRecovery = false;
755 const int kDelayToleranceMs = 20;
756 const int kMaxTimeToSpeechMs = 100;
757 LongCngWithClockDrift(kDriftFactor,
758 kNetworkFreezeTimeMs,
759 kGetAudioDuringFreezeRecovery,
760 kDelayToleranceMs,
761 kMaxTimeToSpeechMs);
762}
763
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000764TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000765 // Apply a clock drift of -25 ms / s (sender faster 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 = 50;
770 const int kMaxTimeToSpeechMs = 200;
771 LongCngWithClockDrift(kDriftFactor,
772 kNetworkFreezeTimeMs,
773 kGetAudioDuringFreezeRecovery,
774 kDelayToleranceMs,
775 kMaxTimeToSpeechMs);
776}
777
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000778TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000779 // 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 = false;
783 const int kDelayToleranceMs = 20;
784 const int kMaxTimeToSpeechMs = 100;
785 LongCngWithClockDrift(kDriftFactor,
786 kNetworkFreezeTimeMs,
787 kGetAudioDuringFreezeRecovery,
788 kDelayToleranceMs,
789 kMaxTimeToSpeechMs);
790}
791
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000792TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000793 // Apply a clock drift of +25 ms / s (sender slower than receiver).
794 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
795 const double kNetworkFreezeTimeMs = 5000.0;
796 const bool kGetAudioDuringFreezeRecovery = true;
797 const int kDelayToleranceMs = 20;
798 const int kMaxTimeToSpeechMs = 100;
799 LongCngWithClockDrift(kDriftFactor,
800 kNetworkFreezeTimeMs,
801 kGetAudioDuringFreezeRecovery,
802 kDelayToleranceMs,
803 kMaxTimeToSpeechMs);
804}
805
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000806TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000807 const double kDriftFactor = 1.0; // No drift.
808 const double kNetworkFreezeTimeMs = 0.0;
809 const bool kGetAudioDuringFreezeRecovery = false;
810 const int kDelayToleranceMs = 10;
811 const int kMaxTimeToSpeechMs = 50;
812 LongCngWithClockDrift(kDriftFactor,
813 kNetworkFreezeTimeMs,
814 kGetAudioDuringFreezeRecovery,
815 kDelayToleranceMs,
816 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000817}
818
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000819TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000820 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000821 uint8_t payload[kPayloadBytes] = {0};
822 WebRtcRTPHeader rtp_info;
823 PopulateRtpInfo(0, 0, &rtp_info);
824 rtp_info.header.payloadType = 1; // Not registered as a decoder.
825 EXPECT_EQ(NetEq::kFail,
826 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
827 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
828}
829
kwiberg98ab3a42015-09-30 21:54:21 -0700830#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
831#define IF_ISAC(x) x
832#else
833#define IF_ISAC(x) DISABLED_##x
834#endif
835
836TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000837 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838 uint8_t payload[kPayloadBytes] = {0};
839 WebRtcRTPHeader rtp_info;
840 PopulateRtpInfo(0, 0, &rtp_info);
841 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
842 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
843 NetEqOutputType type;
844 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
845 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000846 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 out_data_[i] = 1;
848 }
849 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700850 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 EXPECT_EQ(NetEq::kFail,
852 neteq_->GetAudio(kMaxBlockSize, out_data_,
853 &samples_per_channel, &num_channels, &type));
854 // Verify that there is a decoder error to check.
855 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
856 // Code 6730 is an iSAC error code.
857 EXPECT_EQ(6730, neteq_->LastDecoderError());
858 // Verify that the first 160 samples are set to 0, and that the remaining
859 // samples are left unmodified.
860 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
861 for (int i = 0; i < kExpectedOutputLength; ++i) {
862 std::ostringstream ss;
863 ss << "i = " << i;
864 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
865 EXPECT_EQ(0, out_data_[i]);
866 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000867 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000868 std::ostringstream ss;
869 ss << "i = " << i;
870 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
871 EXPECT_EQ(1, out_data_[i]);
872 }
873}
874
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000875TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000876 NetEqOutputType type;
877 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
878 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000879 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 out_data_[i] = 1;
881 }
882 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700883 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000884 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
885 &samples_per_channel,
886 &num_channels, &type));
887 // Verify that the first block of samples is set to 0.
888 static const int kExpectedOutputLength =
889 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
890 for (int i = 0; i < kExpectedOutputLength; ++i) {
891 std::ostringstream ss;
892 ss << "i = " << i;
893 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
894 EXPECT_EQ(0, out_data_[i]);
895 }
896}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000897
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000898class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000899 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000900 virtual void TestCondition(double sum_squared_noise,
901 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000902
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000903 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700904 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000905 uint8_t payload_type = 0xFF; // Invalid.
906 if (sampling_rate_hz == 8000) {
907 expected_samples_per_channel = kBlockSize8kHz;
908 payload_type = 93; // PCM 16, 8 kHz.
909 } else if (sampling_rate_hz == 16000) {
910 expected_samples_per_channel = kBlockSize16kHz;
911 payload_type = 94; // PCM 16, 16 kHZ.
912 } else if (sampling_rate_hz == 32000) {
913 expected_samples_per_channel = kBlockSize32kHz;
914 payload_type = 95; // PCM 16, 32 kHz.
915 } else {
916 ASSERT_TRUE(false); // Unsupported test case.
917 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000918
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000919 NetEqOutputType type;
920 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000921 test::AudioLoop input;
922 // We are using the same 32 kHz input file for all tests, regardless of
923 // |sampling_rate_hz|. The output may sound weird, but the test is still
924 // valid.
925 ASSERT_TRUE(input.Init(
926 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
927 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700928 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000929
930 // Payload of 10 ms of PCM16 32 kHz.
931 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000932 WebRtcRTPHeader rtp_info;
933 PopulateRtpInfo(0, 0, &rtp_info);
934 rtp_info.header.payloadType = payload_type;
935
936 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700937 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000938
939 uint32_t receive_timestamp = 0;
940 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700941 size_t enc_len_bytes = WebRtcPcm16b_Encode(
kwiberg@webrtc.org648f5d62015-02-10 09:18:28 +0000942 input.GetNextBlock(), expected_samples_per_channel, payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000943 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
944
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000945 number_channels = 0;
946 samples_per_channel = 0;
947 ASSERT_EQ(0,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700948 neteq_->InsertPacket(rtp_info, payload, enc_len_bytes,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000949 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000950 ASSERT_EQ(0,
951 neteq_->GetAudio(kBlockSize32kHz,
952 output,
953 &samples_per_channel,
954 &number_channels,
955 &type));
956 ASSERT_EQ(1, number_channels);
957 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
958 ASSERT_EQ(kOutputNormal, type);
959
960 // Next packet.
961 rtp_info.header.timestamp += expected_samples_per_channel;
962 rtp_info.header.sequenceNumber++;
963 receive_timestamp += expected_samples_per_channel;
964 }
965
966 number_channels = 0;
967 samples_per_channel = 0;
968
969 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
970 // one frame without checking speech-type. This is the first frame pulled
971 // without inserting any packet, and might not be labeled as PLC.
972 ASSERT_EQ(0,
973 neteq_->GetAudio(kBlockSize32kHz,
974 output,
975 &samples_per_channel,
976 &number_channels,
977 &type));
978 ASSERT_EQ(1, number_channels);
979 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
980
981 // To be able to test the fading of background noise we need at lease to
982 // pull 611 frames.
983 const int kFadingThreshold = 611;
984
985 // Test several CNG-to-PLC packet for the expected behavior. The number 20
986 // is arbitrary, but sufficiently large to test enough number of frames.
987 const int kNumPlcToCngTestFrames = 20;
988 bool plc_to_cng = false;
989 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
990 number_channels = 0;
991 samples_per_channel = 0;
992 memset(output, 1, sizeof(output)); // Set to non-zero.
993 ASSERT_EQ(0,
994 neteq_->GetAudio(kBlockSize32kHz,
995 output,
996 &samples_per_channel,
997 &number_channels,
998 &type));
999 ASSERT_EQ(1, number_channels);
1000 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1001 if (type == kOutputPLCtoCNG) {
1002 plc_to_cng = true;
1003 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001004 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001005 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001006 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001007 } else {
1008 EXPECT_EQ(kOutputPLC, type);
1009 }
1010 }
1011 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1012 }
1013};
1014
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001015class NetEqBgnTestOn : public NetEqBgnTest {
1016 protected:
1017 NetEqBgnTestOn() : NetEqBgnTest() {
1018 config_.background_noise_mode = NetEq::kBgnOn;
1019 }
1020
1021 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1022 EXPECT_NE(0, sum_squared_noise);
1023 }
1024};
1025
1026class NetEqBgnTestOff : public NetEqBgnTest {
1027 protected:
1028 NetEqBgnTestOff() : NetEqBgnTest() {
1029 config_.background_noise_mode = NetEq::kBgnOff;
1030 }
1031
1032 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1033 EXPECT_EQ(0, sum_squared_noise);
1034 }
1035};
1036
1037class NetEqBgnTestFade : public NetEqBgnTest {
1038 protected:
1039 NetEqBgnTestFade() : NetEqBgnTest() {
1040 config_.background_noise_mode = NetEq::kBgnFade;
1041 }
1042
1043 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1044 if (should_be_faded)
1045 EXPECT_EQ(0, sum_squared_noise);
1046 }
1047};
1048
henrika1d34fe92015-06-16 10:04:20 +02001049TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001050 CheckBgn(8000);
1051 CheckBgn(16000);
1052 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001053}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001054
henrika1d34fe92015-06-16 10:04:20 +02001055TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001056 CheckBgn(8000);
1057 CheckBgn(16000);
1058 CheckBgn(32000);
1059}
1060
henrika1d34fe92015-06-16 10:04:20 +02001061TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001062 CheckBgn(8000);
1063 CheckBgn(16000);
1064 CheckBgn(32000);
1065}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001066
kwiberg98ab3a42015-09-30 21:54:21 -07001067TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001068 WebRtcRTPHeader rtp_info;
1069 uint32_t receive_timestamp = 0;
1070 // For the readability use the following payloads instead of the defaults of
1071 // this test.
1072 uint8_t kPcm16WbPayloadType = 1;
1073 uint8_t kCngNbPayloadType = 2;
1074 uint8_t kCngWbPayloadType = 3;
1075 uint8_t kCngSwb32PayloadType = 4;
1076 uint8_t kCngSwb48PayloadType = 5;
1077 uint8_t kAvtPayloadType = 6;
1078 uint8_t kRedPayloadType = 7;
1079 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1080
1081 // Register decoders.
1082 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1083 kPcm16WbPayloadType));
1084 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1085 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1086 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1087 kCngSwb32PayloadType));
1088 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1089 kCngSwb48PayloadType));
1090 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1091 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1092 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1093
1094 PopulateRtpInfo(0, 0, &rtp_info);
1095 rtp_info.header.payloadType = kPcm16WbPayloadType;
1096
1097 // The first packet injected cannot be sync-packet.
1098 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1099
1100 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001101 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001102 uint8_t payload[kPayloadBytes] = {0};
1103 ASSERT_EQ(0, neteq_->InsertPacket(
1104 rtp_info, payload, kPayloadBytes, receive_timestamp));
1105
1106 // Next packet. Last packet contained 10 ms audio.
1107 rtp_info.header.sequenceNumber++;
1108 rtp_info.header.timestamp += kBlockSize16kHz;
1109 receive_timestamp += kBlockSize16kHz;
1110
1111 // Unacceptable payload types CNG, AVT (DTMF), RED.
1112 rtp_info.header.payloadType = kCngNbPayloadType;
1113 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1114
1115 rtp_info.header.payloadType = kCngWbPayloadType;
1116 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1117
1118 rtp_info.header.payloadType = kCngSwb32PayloadType;
1119 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1120
1121 rtp_info.header.payloadType = kCngSwb48PayloadType;
1122 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1123
1124 rtp_info.header.payloadType = kAvtPayloadType;
1125 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1126
1127 rtp_info.header.payloadType = kRedPayloadType;
1128 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1129
1130 // Change of codec cannot be initiated with a sync packet.
1131 rtp_info.header.payloadType = kIsacPayloadType;
1132 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1133
1134 // Change of SSRC is not allowed with a sync packet.
1135 rtp_info.header.payloadType = kPcm16WbPayloadType;
1136 ++rtp_info.header.ssrc;
1137 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1138
1139 --rtp_info.header.ssrc;
1140 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1141}
1142
1143// First insert several noise like packets, then sync-packets. Decoding all
1144// packets should not produce error, statistics should not show any packet loss
1145// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001146// TODO(turajs) we will have a better test if we have a referece NetEq, and
1147// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1148// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001149TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001150 WebRtcRTPHeader rtp_info;
1151 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001152 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001153 uint8_t payload[kPayloadBytes];
1154 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001155 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001156 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001157 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1158 }
1159 // Insert some packets which decode to noise. We are not interested in
1160 // actual decoded values.
1161 NetEqOutputType output_type;
1162 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001163 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001164 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001165 for (int n = 0; n < 100; ++n) {
1166 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1167 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
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001174 rtp_info.header.sequenceNumber++;
1175 rtp_info.header.timestamp += kBlockSize16kHz;
1176 receive_timestamp += kBlockSize16kHz;
1177 }
1178 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001179
1180 // Make sure sufficient number of sync packets are inserted that we can
1181 // conduct a test.
1182 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001183 // Insert sync-packets, the decoded sequence should be all-zero.
1184 for (int n = 0; n < kNumSyncPackets; ++n) {
1185 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1186 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1187 &samples_per_channel, &num_channels,
1188 &output_type));
1189 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1190 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001191 if (n > algorithmic_frame_delay) {
1192 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1193 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001194 rtp_info.header.sequenceNumber++;
1195 rtp_info.header.timestamp += kBlockSize16kHz;
1196 receive_timestamp += kBlockSize16kHz;
1197 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001198
1199 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001200 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001201 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1202 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1203 receive_timestamp));
1204 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1205 &samples_per_channel, &num_channels,
1206 &output_type));
1207 if (n >= algorithmic_frame_delay + 1) {
1208 // Expect that this frame contain samples from regular RTP.
1209 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1210 }
1211 rtp_info.header.sequenceNumber++;
1212 rtp_info.header.timestamp += kBlockSize16kHz;
1213 receive_timestamp += kBlockSize16kHz;
1214 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001215 NetEqNetworkStatistics network_stats;
1216 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1217 // Expecting a "clean" network.
1218 EXPECT_EQ(0, network_stats.packet_loss_rate);
1219 EXPECT_EQ(0, network_stats.expand_rate);
1220 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001221 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001222}
1223
1224// Test if the size of the packet buffer reported correctly when containing
1225// sync packets. Also, test if network packets override sync packets. That is to
1226// prefer decoding a network packet to a sync packet, if both have same sequence
1227// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001228TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001229 WebRtcRTPHeader rtp_info;
1230 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001231 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232 uint8_t payload[kPayloadBytes];
1233 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001234 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001235 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1236 }
1237 // Insert some packets which decode to noise. We are not interested in
1238 // actual decoded values.
1239 NetEqOutputType output_type;
1240 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001241 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001242 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001243 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1244 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001245 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1246 receive_timestamp));
1247 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1248 &samples_per_channel, &num_channels,
1249 &output_type));
1250 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1251 ASSERT_EQ(1, num_channels);
1252 rtp_info.header.sequenceNumber++;
1253 rtp_info.header.timestamp += kBlockSize16kHz;
1254 receive_timestamp += kBlockSize16kHz;
1255 }
1256 const int kNumSyncPackets = 10;
1257
1258 WebRtcRTPHeader first_sync_packet_rtp_info;
1259 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1260
1261 // Insert sync-packets, but no decoding.
1262 for (int n = 0; n < kNumSyncPackets; ++n) {
1263 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1264 rtp_info.header.sequenceNumber++;
1265 rtp_info.header.timestamp += kBlockSize16kHz;
1266 receive_timestamp += kBlockSize16kHz;
1267 }
1268 NetEqNetworkStatistics network_stats;
1269 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001270 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1271 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001272
1273 // Rewind |rtp_info| to that of the first sync packet.
1274 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1275
1276 // Insert.
1277 for (int n = 0; n < kNumSyncPackets; ++n) {
1278 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1279 receive_timestamp));
1280 rtp_info.header.sequenceNumber++;
1281 rtp_info.header.timestamp += kBlockSize16kHz;
1282 receive_timestamp += kBlockSize16kHz;
1283 }
1284
1285 // Decode.
1286 for (int n = 0; n < kNumSyncPackets; ++n) {
1287 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1288 &samples_per_channel, &num_channels,
1289 &output_type));
1290 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1291 ASSERT_EQ(1, num_channels);
1292 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1293 }
1294}
1295
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001296void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1297 uint32_t start_timestamp,
1298 const std::set<uint16_t>& drop_seq_numbers,
1299 bool expect_seq_no_wrap,
1300 bool expect_timestamp_wrap) {
1301 uint16_t seq_no = start_seq_no;
1302 uint32_t timestamp = start_timestamp;
1303 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1304 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1305 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001306 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001307 double next_input_time_ms = 0.0;
1308 int16_t decoded[kBlockSize16kHz];
1309 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001310 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001311 NetEqOutputType output_type;
1312 uint32_t receive_timestamp = 0;
1313
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001314 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001315 const int kSpeechDurationMs = 2000;
1316 int packets_inserted = 0;
1317 uint16_t last_seq_no;
1318 uint32_t last_timestamp;
1319 bool timestamp_wrapped = false;
1320 bool seq_no_wrapped = false;
1321 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1322 // Each turn in this for loop is 10 ms.
1323 while (next_input_time_ms <= t_ms) {
1324 // Insert one 30 ms speech frame.
1325 uint8_t payload[kPayloadBytes] = {0};
1326 WebRtcRTPHeader rtp_info;
1327 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1328 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1329 // This sequence number was not in the set to drop. Insert it.
1330 ASSERT_EQ(0,
1331 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1332 receive_timestamp));
1333 ++packets_inserted;
1334 }
1335 NetEqNetworkStatistics network_stats;
1336 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1337
1338 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1339 // packet size for first few packets. Therefore we refrain from checking
1340 // the criteria.
1341 if (packets_inserted > 4) {
1342 // Expect preferred and actual buffer size to be no more than 2 frames.
1343 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001344 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1345 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001346 }
1347 last_seq_no = seq_no;
1348 last_timestamp = timestamp;
1349
1350 ++seq_no;
1351 timestamp += kSamples;
1352 receive_timestamp += kSamples;
1353 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1354
1355 seq_no_wrapped |= seq_no < last_seq_no;
1356 timestamp_wrapped |= timestamp < last_timestamp;
1357 }
1358 // Pull out data once.
1359 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1360 &samples_per_channel, &num_channels,
1361 &output_type));
1362 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1363 ASSERT_EQ(1, num_channels);
1364
1365 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001366 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001367 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001368 }
1369 // Make sure we have actually tested wrap-around.
1370 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1371 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1372}
1373
1374TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1375 // Start with a sequence number that will soon wrap.
1376 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1377 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1378}
1379
1380TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1381 // Start with a sequence number that will soon wrap.
1382 std::set<uint16_t> drop_seq_numbers;
1383 drop_seq_numbers.insert(0xFFFF);
1384 drop_seq_numbers.insert(0x0);
1385 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1386}
1387
1388TEST_F(NetEqDecodingTest, TimestampWrap) {
1389 // Start with a timestamp that will soon wrap.
1390 std::set<uint16_t> drop_seq_numbers;
1391 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1392}
1393
1394TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1395 // Start with a timestamp and a sequence number that will wrap at the same
1396 // time.
1397 std::set<uint16_t> drop_seq_numbers;
1398 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1399}
1400
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001401void NetEqDecodingTest::DuplicateCng() {
1402 uint16_t seq_no = 0;
1403 uint32_t timestamp = 0;
1404 const int kFrameSizeMs = 10;
1405 const int kSampleRateKhz = 16;
1406 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001407 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001408
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001409 const int algorithmic_delay_samples = std::max(
1410 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001411 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001412 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001413 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001414 int num_channels;
1415 NetEqOutputType type;
1416 uint8_t payload[kPayloadBytes] = {0};
1417 WebRtcRTPHeader rtp_info;
1418 for (int i = 0; i < 3; ++i) {
1419 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1420 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1421 ++seq_no;
1422 timestamp += kSamples;
1423
1424 // Pull audio once.
1425 ASSERT_EQ(0,
1426 neteq_->GetAudio(
1427 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1428 ASSERT_EQ(kBlockSize16kHz, out_len);
1429 }
1430 // Verify speech output.
1431 EXPECT_EQ(kOutputNormal, type);
1432
1433 // Insert same CNG packet twice.
1434 const int kCngPeriodMs = 100;
1435 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001436 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001437 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1438 // This is the first time this CNG packet is inserted.
1439 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1440
1441 // Pull audio once and make sure CNG is played.
1442 ASSERT_EQ(0,
1443 neteq_->GetAudio(
1444 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1445 ASSERT_EQ(kBlockSize16kHz, out_len);
1446 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001447 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001448
1449 // Insert the same CNG packet again. Note that at this point it is old, since
1450 // we have already decoded the first copy of it.
1451 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1452
1453 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1454 // we have already pulled out CNG once.
1455 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1456 ASSERT_EQ(0,
1457 neteq_->GetAudio(
1458 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1459 ASSERT_EQ(kBlockSize16kHz, out_len);
1460 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001461 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001462 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001463 }
1464
1465 // Insert speech again.
1466 ++seq_no;
1467 timestamp += kCngPeriodSamples;
1468 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1469 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1470
1471 // Pull audio once and verify that the output is speech again.
1472 ASSERT_EQ(0,
1473 neteq_->GetAudio(
1474 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1475 ASSERT_EQ(kBlockSize16kHz, out_len);
1476 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001477 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001478 PlayoutTimestamp());
1479}
1480
1481uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1482 uint32_t playout_timestamp = 0;
1483 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1484 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001485}
1486
1487TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001488
1489TEST_F(NetEqDecodingTest, CngFirst) {
1490 uint16_t seq_no = 0;
1491 uint32_t timestamp = 0;
1492 const int kFrameSizeMs = 10;
1493 const int kSampleRateKhz = 16;
1494 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1495 const int kPayloadBytes = kSamples * 2;
1496 const int kCngPeriodMs = 100;
1497 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1498 size_t payload_len;
1499
1500 uint8_t payload[kPayloadBytes] = {0};
1501 WebRtcRTPHeader rtp_info;
1502
1503 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1504 ASSERT_EQ(NetEq::kOK,
1505 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1506 ++seq_no;
1507 timestamp += kCngPeriodSamples;
1508
1509 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001510 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001511 int num_channels;
1512 NetEqOutputType type;
1513 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1514 &num_channels, &type));
1515 ASSERT_EQ(kBlockSize16kHz, out_len);
1516 EXPECT_EQ(kOutputCNG, type);
1517
1518 // Insert some speech packets.
1519 for (int i = 0; i < 3; ++i) {
1520 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1521 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1522 ++seq_no;
1523 timestamp += kSamples;
1524
1525 // Pull audio once.
1526 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1527 &num_channels, &type));
1528 ASSERT_EQ(kBlockSize16kHz, out_len);
1529 }
1530 // Verify speech output.
1531 EXPECT_EQ(kOutputNormal, type);
1532}
1533
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001534} // namespace webrtc