blob: 8be077a322bcfb1b514e84504020494f57267955 [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
Henrik Kjellander74640892015-10-29 11:31:02 +010011#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000023#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000024#include "testing/gtest/include/gtest/gtest.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000025#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000026#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010027#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080028#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029#include "webrtc/test/testsupport/fileutils.h"
30#include "webrtc/typedefs.h"
31
minyue5f026d02015-12-16 07:36:04 -080032#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
33#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
34#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
35#else
36#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
37#endif
38#endif
39
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000040DEFINE_bool(gen_ref, false, "Generate reference files.");
41
minyue5f026d02015-12-16 07:36:04 -080042namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
minyue5f026d02015-12-16 07:36:04 -080044bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000045 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000047 all_zero = buf[n] == 0;
48 return all_zero;
49}
50
minyue5f026d02015-12-16 07:36:04 -080051bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000052 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070053 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000054 all_non_zero = buf[n] != 0;
55 return all_non_zero;
56}
57
minyue5f026d02015-12-16 07:36:04 -080058#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
59void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
60 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
61 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
62 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
63 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
64 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
65 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
66 stats->set_expand_rate(stats_raw.expand_rate);
67 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
68 stats->set_preemptive_rate(stats_raw.preemptive_rate);
69 stats->set_accelerate_rate(stats_raw.accelerate_rate);
70 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
71 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
72 stats->set_added_zero_samples(stats_raw.added_zero_samples);
73 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
74 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
75 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
76 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
77}
78
79void Convert(const webrtc::RtcpStatistics& stats_raw,
80 webrtc::neteq_unittest::RtcpStatistics* stats) {
81 stats->set_fraction_lost(stats_raw.fraction_lost);
82 stats->set_cumulative_lost(stats_raw.cumulative_lost);
83 stats->set_extended_max_sequence_number(
84 stats_raw.extended_max_sequence_number);
85 stats->set_jitter(stats_raw.jitter);
86}
87
88void WriteMessage(FILE* file, const std::string& message) {
89 int32_t size = message.length();
90 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
91 if (size <= 0)
92 return;
93 ASSERT_EQ(static_cast<size_t>(size),
94 fwrite(message.data(), sizeof(char), size, file));
95}
96
97void ReadMessage(FILE* file, std::string* message) {
98 int32_t size;
99 ASSERT_EQ(1u, fread(&size, sizeof(size), 1, file));
100 if (size <= 0)
101 return;
kwiberg2d0c3322016-02-14 09:28:33 -0800102 std::unique_ptr<char[]> buffer(new char[size]);
minyue5f026d02015-12-16 07:36:04 -0800103 ASSERT_EQ(static_cast<size_t>(size),
104 fread(buffer.get(), sizeof(char), size, file));
105 message->assign(buffer.get(), size);
106}
107#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
108
109} // namespace
110
111namespace webrtc {
112
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113class RefFiles {
114 public:
115 RefFiles(const std::string& input_file, const std::string& output_file);
116 ~RefFiles();
117 template<class T> void ProcessReference(const T& test_results);
118 template<typename T, size_t n> void ProcessReference(
119 const T (&test_results)[n],
120 size_t length);
121 template<typename T, size_t n> void WriteToFile(
122 const T (&test_results)[n],
123 size_t length);
124 template<typename T, size_t n> void ReadFromFileAndCompare(
125 const T (&test_results)[n],
126 size_t length);
127 void WriteToFile(const NetEqNetworkStatistics& stats);
128 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
129 void WriteToFile(const RtcpStatistics& stats);
130 void ReadFromFileAndCompare(const RtcpStatistics& stats);
131
132 FILE* input_fp_;
133 FILE* output_fp_;
134};
135
136RefFiles::RefFiles(const std::string &input_file,
137 const std::string &output_file)
138 : input_fp_(NULL),
139 output_fp_(NULL) {
140 if (!input_file.empty()) {
141 input_fp_ = fopen(input_file.c_str(), "rb");
142 EXPECT_TRUE(input_fp_ != NULL);
143 }
144 if (!output_file.empty()) {
145 output_fp_ = fopen(output_file.c_str(), "wb");
146 EXPECT_TRUE(output_fp_ != NULL);
147 }
148}
149
150RefFiles::~RefFiles() {
151 if (input_fp_) {
152 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
153 fclose(input_fp_);
154 }
155 if (output_fp_) fclose(output_fp_);
156}
157
158template<class T>
159void RefFiles::ProcessReference(const T& test_results) {
160 WriteToFile(test_results);
161 ReadFromFileAndCompare(test_results);
162}
163
164template<typename T, size_t n>
165void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
166 WriteToFile(test_results, length);
167 ReadFromFileAndCompare(test_results, length);
168}
169
170template<typename T, size_t n>
171void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
172 if (output_fp_) {
173 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
174 }
175}
176
177template<typename T, size_t n>
178void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
179 size_t length) {
180 if (input_fp_) {
181 // Read from ref file.
182 T* ref = new T[length];
183 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
184 // Compare
185 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
186 delete [] ref;
187 }
188}
189
minyue5f026d02015-12-16 07:36:04 -0800190void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats_raw) {
191#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
192 if (!output_fp_)
193 return;
194 neteq_unittest::NetEqNetworkStatistics stats;
195 Convert(stats_raw, &stats);
196
197 std::string stats_string;
198 ASSERT_TRUE(stats.SerializeToString(&stats_string));
199 WriteMessage(output_fp_, stats_string);
200#else
201 FAIL() << "Writing to reference file requires Proto Buffer.";
202#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203}
204
205void RefFiles::ReadFromFileAndCompare(
206 const NetEqNetworkStatistics& stats) {
minyue5f026d02015-12-16 07:36:04 -0800207#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
208 if (!input_fp_)
209 return;
210
211 std::string stats_string;
212 ReadMessage(input_fp_, &stats_string);
213 neteq_unittest::NetEqNetworkStatistics ref_stats;
214 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
215
216 // Compare
217 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms());
218 ASSERT_EQ(stats.preferred_buffer_size_ms,
219 ref_stats.preferred_buffer_size_ms());
220 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found());
221 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate());
222 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate());
223 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate());
224 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate());
225 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate());
226 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm());
227 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples());
minyue93c08b72015-12-22 09:57:41 -0800228 ASSERT_EQ(stats.secondary_decoded_rate, ref_stats.secondary_decoded_rate());
minyue5f026d02015-12-16 07:36:04 -0800229 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate());
230#else
231 FAIL() << "Reading from reference file requires Proto Buffer.";
232#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233}
234
minyue5f026d02015-12-16 07:36:04 -0800235void RefFiles::WriteToFile(const RtcpStatistics& stats_raw) {
236#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
237 if (!output_fp_)
238 return;
239 neteq_unittest::RtcpStatistics stats;
240 Convert(stats_raw, &stats);
241
242 std::string stats_string;
243 ASSERT_TRUE(stats.SerializeToString(&stats_string));
244 WriteMessage(output_fp_, stats_string);
245#else
246 FAIL() << "Writing to reference file requires Proto Buffer.";
247#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248}
249
minyue5f026d02015-12-16 07:36:04 -0800250void RefFiles::ReadFromFileAndCompare(const RtcpStatistics& stats) {
251#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
252 if (!input_fp_)
253 return;
254 std::string stats_string;
255 ReadMessage(input_fp_, &stats_string);
256 neteq_unittest::RtcpStatistics ref_stats;
257 ASSERT_TRUE(ref_stats.ParseFromString(stats_string));
258
259 // Compare
260 ASSERT_EQ(stats.fraction_lost, ref_stats.fraction_lost());
261 ASSERT_EQ(stats.cumulative_lost, ref_stats.cumulative_lost());
262 ASSERT_EQ(stats.extended_max_sequence_number,
263 ref_stats.extended_max_sequence_number());
264 ASSERT_EQ(stats.jitter, ref_stats.jitter());
265#else
266 FAIL() << "Reading from reference file requires Proto Buffer.";
267#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268}
269
270class NetEqDecodingTest : public ::testing::Test {
271 protected:
272 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
273 // constants below can be changed.
274 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700275 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
276 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
277 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800278 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 static const int kInitSampleRateHz = 8000;
280
281 NetEqDecodingTest();
282 virtual void SetUp();
283 virtual void TearDown();
284 void SelectDecoders(NetEqDecoder* used_codec);
285 void LoadDecoders();
286 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800287 void Process();
minyue5f026d02015-12-16 07:36:04 -0800288
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000289 void DecodeAndCompare(const std::string& rtp_file,
290 const std::string& ref_file,
291 const std::string& stat_ref_file,
292 const std::string& rtcp_ref_file);
minyue5f026d02015-12-16 07:36:04 -0800293
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 static void PopulateRtpInfo(int frame_index,
295 int timestamp,
296 WebRtcRTPHeader* rtp_info);
297 static void PopulateCng(int frame_index,
298 int timestamp,
299 WebRtcRTPHeader* rtp_info,
300 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000301 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000302
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000303 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
304 const std::set<uint16_t>& drop_seq_numbers,
305 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
306
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000307 void LongCngWithClockDrift(double drift_factor,
308 double network_freeze_ms,
309 bool pull_audio_during_freeze,
310 int delay_tolerance_ms,
311 int max_time_to_speech_ms);
312
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000313 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000314
henrik.lundin0d96ab72016-04-06 12:28:26 -0700315 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000316
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000318 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800319 std::unique_ptr<test::RtpFileSource> rtp_source_;
320 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800322 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000324 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325};
326
327// Allocating the static const so that it can be passed by reference.
328const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700329const size_t NetEqDecodingTest::kBlockSize8kHz;
330const size_t NetEqDecodingTest::kBlockSize16kHz;
331const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332const int NetEqDecodingTest::kInitSampleRateHz;
333
334NetEqDecodingTest::NetEqDecodingTest()
335 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000336 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000338 output_sample_rate_(kInitSampleRateHz),
339 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000340 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341}
342
343void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000344 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000345 NetEqNetworkStatistics stat;
346 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
347 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 ASSERT_TRUE(neteq_);
349 LoadDecoders();
350}
351
352void NetEqDecodingTest::TearDown() {
353 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354}
355
356void NetEqDecodingTest::LoadDecoders() {
357 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800358 ASSERT_EQ(0,
359 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800361 ASSERT_EQ(0,
362 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700363#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800365 ASSERT_EQ(
366 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700367#endif
368#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000369 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800370 ASSERT_EQ(
371 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700372#endif
373#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800375 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
376 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700377#endif
minyue93c08b72015-12-22 09:57:41 -0800378#ifdef WEBRTC_CODEC_OPUS
379 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus,
380 "opus", 111));
381#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800383 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
384 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800386 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
387 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800389 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
390 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800392 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
393 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800395 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
396 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397}
398
399void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000400 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401}
402
henrik.lundin6d8e0112016-03-04 10:34:21 -0800403void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000404 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000405 while (packet_ && sim_clock_ >= packet_->time_ms()) {
406 if (packet_->payload_length_bytes() > 0) {
407 WebRtcRTPHeader rtp_header;
408 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800409#ifndef WEBRTC_CODEC_ISAC
410 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
411 if (rtp_header.header.payloadType != 104)
412#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800414 rtp_header,
415 rtc::ArrayView<const uint8_t>(
416 packet_->payload(), packet_->payload_length_bytes()),
417 static_cast<uint32_t>(packet_->time_ms() *
418 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000419 }
420 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000421 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000422 }
423
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000424 // Get audio from NetEq.
henrik.lundin55480f52016-03-08 02:37:57 -0800425 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800426 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
427 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
428 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
429 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
430 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800431 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432
433 // Increase time.
434 sim_clock_ += kTimeStepMs;
435}
436
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000437void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
438 const std::string& ref_file,
439 const std::string& stat_ref_file,
440 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441 OpenInputFile(rtp_file);
442
443 std::string ref_out_file = "";
444 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000445 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 }
447 RefFiles ref_files(ref_file, ref_out_file);
448
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000449 std::string stat_out_file = "";
450 if (stat_ref_file.empty()) {
451 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
452 }
453 RefFiles network_stat_files(stat_ref_file, stat_out_file);
454
455 std::string rtcp_out_file = "";
456 if (rtcp_ref_file.empty()) {
457 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
458 }
459 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
460
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000461 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000462 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000463 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000464 std::ostringstream ss;
465 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
466 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800467 ASSERT_NO_FATAL_FAILURE(Process());
468 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(
469 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000470
471 // Query the network statistics API once per second
472 if (sim_clock_ % 1000 == 0) {
473 // Process NetworkStatistics.
474 NetEqNetworkStatistics network_stats;
475 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000476 ASSERT_NO_FATAL_FAILURE(
477 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700478 // Compare with CurrentDelay, which should be identical.
479 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000480
481 // Process RTCPstat.
482 RtcpStatistics rtcp_stats;
483 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000484 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000485 }
486 }
487}
488
489void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
490 int timestamp,
491 WebRtcRTPHeader* rtp_info) {
492 rtp_info->header.sequenceNumber = frame_index;
493 rtp_info->header.timestamp = timestamp;
494 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
495 rtp_info->header.payloadType = 94; // PCM16b WB codec.
496 rtp_info->header.markerBit = 0;
497}
498
499void NetEqDecodingTest::PopulateCng(int frame_index,
500 int timestamp,
501 WebRtcRTPHeader* rtp_info,
502 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000503 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000504 rtp_info->header.sequenceNumber = frame_index;
505 rtp_info->header.timestamp = timestamp;
506 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
507 rtp_info->header.payloadType = 98; // WB CNG.
508 rtp_info->header.markerBit = 0;
509 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
510 *payload_len = 1; // Only noise level, no spectral parameters.
511}
512
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200513// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
ivoc72c08ed2016-01-20 07:26:24 -0800514#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
515 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
516 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200517 !defined(WEBRTC_ARCH_ARM64) && !defined(UNDEFINED_SANITIZER)
minyue5f026d02015-12-16 07:36:04 -0800518#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700519#else
minyue5f026d02015-12-16 07:36:04 -0800520#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700521#endif
minyue5f026d02015-12-16 07:36:04 -0800522TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800523 const std::string input_rtp_file =
524 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000525 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
526 // are identical. The latter could have been removed, but if clients still
527 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000528 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000529 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000530#if defined(_MSC_VER) && (_MSC_VER >= 1700)
531 // For Visual Studio 2012 and later, we will have to use the generic reference
532 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000533 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000534 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000535#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000536 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000537 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000538#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000539 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000540 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000541
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000542 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000543 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000544 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000545 DecodeAndCompare(input_rtp_file,
546 input_ref_file,
547 network_stat_ref_file,
548 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000549 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000550}
551
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200552// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
minyue93c08b72015-12-22 09:57:41 -0800553#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
554 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200555 defined(WEBRTC_CODEC_OPUS) && !defined(UNDEFINED_SANITIZER)
minyue93c08b72015-12-22 09:57:41 -0800556#define MAYBE_TestOpusBitExactness TestOpusBitExactness
557#else
558#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
559#endif
560TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
561 const std::string input_rtp_file =
562 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
563 const std::string input_ref_file =
kjellanderc3a09832016-02-02 13:18:33 -0800564 // The pcm files were generated by using Opus v1.1.2 to decode the RTC
565 // file generated by Opus v1.1
minyue93c08b72015-12-22 09:57:41 -0800566 webrtc::test::ResourcePath("audio_coding/neteq4_opus_ref", "pcm");
567 const std::string network_stat_ref_file =
kjellanderc3a09832016-02-02 13:18:33 -0800568 // The network stats file was generated when using Opus v1.1.2 to decode
569 // the RTC file generated by Opus v1.1
minyue93c08b72015-12-22 09:57:41 -0800570 webrtc::test::ResourcePath("audio_coding/neteq4_opus_network_stats",
571 "dat");
572 const std::string rtcp_stat_ref_file =
573 webrtc::test::ResourcePath("audio_coding/neteq4_opus_rtcp_stats", "dat");
574
575 if (FLAGS_gen_ref) {
576 DecodeAndCompare(input_rtp_file, "", "", "");
577 } else {
578 DecodeAndCompare(input_rtp_file,
579 input_ref_file,
580 network_stat_ref_file,
581 rtcp_stat_ref_file);
582 }
583}
584
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000585// Use fax mode to avoid time-scaling. This is to simplify the testing of
586// packet waiting times in the packet buffer.
587class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
588 protected:
589 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
590 config_.playout_mode = kPlayoutFax;
591 }
592};
593
594TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
596 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000597 const size_t kSamples = 10 * 16;
598 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000599 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800600 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000601 WebRtcRTPHeader rtp_info;
602 rtp_info.header.sequenceNumber = i;
603 rtp_info.header.timestamp = i * kSamples;
604 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
605 rtp_info.header.payloadType = 94; // PCM16b WB codec.
606 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800607 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000608 }
609 // Pull out all data.
610 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin55480f52016-03-08 02:37:57 -0800611 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800612 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000613 }
614
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200615 NetEqNetworkStatistics stats;
616 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000617 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
618 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200619 // each packet. Thus, we are calculating the statistics for a series from 10
620 // to 300, in steps of 10 ms.
621 EXPECT_EQ(155, stats.mean_waiting_time_ms);
622 EXPECT_EQ(155, stats.median_waiting_time_ms);
623 EXPECT_EQ(10, stats.min_waiting_time_ms);
624 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000625
626 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200627 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
628 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
629 EXPECT_EQ(-1, stats.median_waiting_time_ms);
630 EXPECT_EQ(-1, stats.min_waiting_time_ms);
631 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632}
633
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000634TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635 const int kNumFrames = 3000; // Needed for convergence.
636 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000637 const size_t kSamples = 10 * 16;
638 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639 while (frame_index < kNumFrames) {
640 // Insert one packet each time, except every 10th time where we insert two
641 // packets at once. This will create a negative clock-drift of approx. 10%.
642 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
643 for (int n = 0; n < num_packets; ++n) {
644 uint8_t payload[kPayloadBytes] = {0};
645 WebRtcRTPHeader rtp_info;
646 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800647 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000648 ++frame_index;
649 }
650
651 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800652 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800653 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000654 }
655
656 NetEqNetworkStatistics network_stats;
657 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
658 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
659}
660
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000661TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000662 const int kNumFrames = 5000; // Needed for convergence.
663 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000664 const size_t kSamples = 10 * 16;
665 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 for (int i = 0; i < kNumFrames; ++i) {
667 // Insert one packet each time, except every 10th time where we don't insert
668 // any packet. This will create a positive clock-drift of approx. 11%.
669 int num_packets = (i % 10 == 9 ? 0 : 1);
670 for (int n = 0; n < num_packets; ++n) {
671 uint8_t payload[kPayloadBytes] = {0};
672 WebRtcRTPHeader rtp_info;
673 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800674 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000675 ++frame_index;
676 }
677
678 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800679 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800680 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000681 }
682
683 NetEqNetworkStatistics network_stats;
684 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
685 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
686}
687
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000688void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
689 double network_freeze_ms,
690 bool pull_audio_during_freeze,
691 int delay_tolerance_ms,
692 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 uint16_t seq_no = 0;
694 uint32_t timestamp = 0;
695 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000696 const size_t kSamples = kFrameSizeMs * 16;
697 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000698 double next_input_time_ms = 0.0;
699 double t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700
701 // Insert speech for 5 seconds.
702 const int kSpeechDurationMs = 5000;
703 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
704 // Each turn in this for loop is 10 ms.
705 while (next_input_time_ms <= t_ms) {
706 // Insert one 30 ms speech frame.
707 uint8_t payload[kPayloadBytes] = {0};
708 WebRtcRTPHeader rtp_info;
709 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800710 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000711 ++seq_no;
712 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000713 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 }
715 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800716 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800717 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000718 }
719
henrik.lundin55480f52016-03-08 02:37:57 -0800720 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700721 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
722 ASSERT_TRUE(playout_timestamp);
723 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724
725 // Insert CNG for 1 minute (= 60000 ms).
726 const int kCngPeriodMs = 100;
727 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
728 const int kCngDurationMs = 60000;
729 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
730 // Each turn in this for loop is 10 ms.
731 while (next_input_time_ms <= t_ms) {
732 // Insert one CNG frame each 100 ms.
733 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000734 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000735 WebRtcRTPHeader rtp_info;
736 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800737 ASSERT_EQ(0, neteq_->InsertPacket(
738 rtp_info,
739 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000740 ++seq_no;
741 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000742 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 }
744 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800745 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800746 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747 }
748
henrik.lundin55480f52016-03-08 02:37:57 -0800749 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000750
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000751 if (network_freeze_ms > 0) {
752 // First keep pulling audio for |network_freeze_ms| without inserting
753 // any data, then insert CNG data corresponding to |network_freeze_ms|
754 // without pulling any output audio.
755 const double loop_end_time = t_ms + network_freeze_ms;
756 for (; t_ms < loop_end_time; t_ms += 10) {
757 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800758 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800759 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800760 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000761 }
762 bool pull_once = pull_audio_during_freeze;
763 // If |pull_once| is true, GetAudio will be called once half-way through
764 // the network recovery period.
765 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
766 while (next_input_time_ms <= t_ms) {
767 if (pull_once && next_input_time_ms >= pull_time_ms) {
768 pull_once = false;
769 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800770 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800771 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800772 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000773 t_ms += 10;
774 }
775 // Insert one CNG frame each 100 ms.
776 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000777 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000778 WebRtcRTPHeader rtp_info;
779 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800780 ASSERT_EQ(0, neteq_->InsertPacket(
781 rtp_info,
782 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000783 ++seq_no;
784 timestamp += kCngPeriodSamples;
785 next_input_time_ms += kCngPeriodMs * drift_factor;
786 }
787 }
788
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000789 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000790 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800791 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000792 // Each turn in this for loop is 10 ms.
793 while (next_input_time_ms <= t_ms) {
794 // Insert one 30 ms speech frame.
795 uint8_t payload[kPayloadBytes] = {0};
796 WebRtcRTPHeader rtp_info;
797 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800798 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000799 ++seq_no;
800 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000802 }
803 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800804 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800805 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000806 // Increase clock.
807 t_ms += 10;
808 }
809
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000810 // Check that the speech starts again within reasonable time.
811 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
812 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700813 playout_timestamp = PlayoutTimestamp();
814 ASSERT_TRUE(playout_timestamp);
815 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000816 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000817 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
818 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000819}
820
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000821TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000822 // Apply a clock drift of -25 ms / s (sender faster than receiver).
823 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000824 const double kNetworkFreezeTimeMs = 0.0;
825 const bool kGetAudioDuringFreezeRecovery = false;
826 const int kDelayToleranceMs = 20;
827 const int kMaxTimeToSpeechMs = 100;
828 LongCngWithClockDrift(kDriftFactor,
829 kNetworkFreezeTimeMs,
830 kGetAudioDuringFreezeRecovery,
831 kDelayToleranceMs,
832 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000833}
834
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000835TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000836 // Apply a clock drift of +25 ms / s (sender slower than receiver).
837 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000838 const double kNetworkFreezeTimeMs = 0.0;
839 const bool kGetAudioDuringFreezeRecovery = false;
840 const int kDelayToleranceMs = 20;
841 const int kMaxTimeToSpeechMs = 100;
842 LongCngWithClockDrift(kDriftFactor,
843 kNetworkFreezeTimeMs,
844 kGetAudioDuringFreezeRecovery,
845 kDelayToleranceMs,
846 kMaxTimeToSpeechMs);
847}
848
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000849TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000850 // Apply a clock drift of -25 ms / s (sender faster than receiver).
851 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
852 const double kNetworkFreezeTimeMs = 5000.0;
853 const bool kGetAudioDuringFreezeRecovery = false;
854 const int kDelayToleranceMs = 50;
855 const int kMaxTimeToSpeechMs = 200;
856 LongCngWithClockDrift(kDriftFactor,
857 kNetworkFreezeTimeMs,
858 kGetAudioDuringFreezeRecovery,
859 kDelayToleranceMs,
860 kMaxTimeToSpeechMs);
861}
862
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000863TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000864 // Apply a clock drift of +25 ms / s (sender slower than receiver).
865 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
866 const double kNetworkFreezeTimeMs = 5000.0;
867 const bool kGetAudioDuringFreezeRecovery = false;
868 const int kDelayToleranceMs = 20;
869 const int kMaxTimeToSpeechMs = 100;
870 LongCngWithClockDrift(kDriftFactor,
871 kNetworkFreezeTimeMs,
872 kGetAudioDuringFreezeRecovery,
873 kDelayToleranceMs,
874 kMaxTimeToSpeechMs);
875}
876
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000877TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000878 // Apply a clock drift of +25 ms / s (sender slower than receiver).
879 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
880 const double kNetworkFreezeTimeMs = 5000.0;
881 const bool kGetAudioDuringFreezeRecovery = true;
882 const int kDelayToleranceMs = 20;
883 const int kMaxTimeToSpeechMs = 100;
884 LongCngWithClockDrift(kDriftFactor,
885 kNetworkFreezeTimeMs,
886 kGetAudioDuringFreezeRecovery,
887 kDelayToleranceMs,
888 kMaxTimeToSpeechMs);
889}
890
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000891TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000892 const double kDriftFactor = 1.0; // No drift.
893 const double kNetworkFreezeTimeMs = 0.0;
894 const bool kGetAudioDuringFreezeRecovery = false;
895 const int kDelayToleranceMs = 10;
896 const int kMaxTimeToSpeechMs = 50;
897 LongCngWithClockDrift(kDriftFactor,
898 kNetworkFreezeTimeMs,
899 kGetAudioDuringFreezeRecovery,
900 kDelayToleranceMs,
901 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000902}
903
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000904TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000905 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 uint8_t payload[kPayloadBytes] = {0};
907 WebRtcRTPHeader rtp_info;
908 PopulateRtpInfo(0, 0, &rtp_info);
909 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800910 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000911 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
912}
913
Peter Boströme2976c82016-01-04 22:44:05 +0100914#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800915#define MAYBE_DecoderError DecoderError
916#else
917#define MAYBE_DecoderError DISABLED_DecoderError
918#endif
919
Peter Boströme2976c82016-01-04 22:44:05 +0100920TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000921 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000922 uint8_t payload[kPayloadBytes] = {0};
923 WebRtcRTPHeader rtp_info;
924 PopulateRtpInfo(0, 0, &rtp_info);
925 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800926 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000927 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
928 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800929 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
930 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000931 }
henrik.lundin55480f52016-03-08 02:37:57 -0800932 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000933 // Verify that there is a decoder error to check.
934 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800935
936 enum NetEqDecoderError {
937 ISAC_LENGTH_MISMATCH = 6730,
938 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
939 };
940#if defined(WEBRTC_CODEC_ISAC)
941 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
942#elif defined(WEBRTC_CODEC_ISACFX)
943 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
944#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000945 // Verify that the first 160 samples are set to 0, and that the remaining
946 // samples are left unmodified.
947 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
948 for (int i = 0; i < kExpectedOutputLength; ++i) {
949 std::ostringstream ss;
950 ss << "i = " << i;
951 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800952 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000953 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800954 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
955 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000956 std::ostringstream ss;
957 ss << "i = " << i;
958 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800959 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000960 }
961}
962
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000963TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000964 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
965 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800966 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
967 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000968 }
henrik.lundin55480f52016-03-08 02:37:57 -0800969 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000970 // Verify that the first block of samples is set to 0.
971 static const int kExpectedOutputLength =
972 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
973 for (int i = 0; i < kExpectedOutputLength; ++i) {
974 std::ostringstream ss;
975 ss << "i = " << i;
976 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800977 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000978 }
henrik.lundind89814b2015-11-23 06:49:25 -0800979 // Verify that the sample rate did not change from the initial configuration.
980 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000981}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000982
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000983class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000984 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000985 virtual void TestCondition(double sum_squared_noise,
986 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000987
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000988 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700989 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000990 uint8_t payload_type = 0xFF; // Invalid.
991 if (sampling_rate_hz == 8000) {
992 expected_samples_per_channel = kBlockSize8kHz;
993 payload_type = 93; // PCM 16, 8 kHz.
994 } else if (sampling_rate_hz == 16000) {
995 expected_samples_per_channel = kBlockSize16kHz;
996 payload_type = 94; // PCM 16, 16 kHZ.
997 } else if (sampling_rate_hz == 32000) {
998 expected_samples_per_channel = kBlockSize32kHz;
999 payload_type = 95; // PCM 16, 32 kHz.
1000 } else {
1001 ASSERT_TRUE(false); // Unsupported test case.
1002 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001003
henrik.lundin6d8e0112016-03-04 10:34:21 -08001004 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001005 test::AudioLoop input;
1006 // We are using the same 32 kHz input file for all tests, regardless of
1007 // |sampling_rate_hz|. The output may sound weird, but the test is still
1008 // valid.
1009 ASSERT_TRUE(input.Init(
1010 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
1011 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001012 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001013
1014 // Payload of 10 ms of PCM16 32 kHz.
1015 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001016 WebRtcRTPHeader rtp_info;
1017 PopulateRtpInfo(0, 0, &rtp_info);
1018 rtp_info.header.payloadType = payload_type;
1019
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001020 uint32_t receive_timestamp = 0;
1021 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -08001022 auto block = input.GetNextBlock();
1023 ASSERT_EQ(expected_samples_per_channel, block.size());
1024 size_t enc_len_bytes =
1025 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001026 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
1027
kwibergee2bac22015-11-11 10:34:00 -08001028 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
1029 payload, enc_len_bytes),
1030 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001031 output.Reset();
henrik.lundin55480f52016-03-08 02:37:57 -08001032 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001033 ASSERT_EQ(1u, output.num_channels_);
1034 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001035 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001036
1037 // Next packet.
1038 rtp_info.header.timestamp += expected_samples_per_channel;
1039 rtp_info.header.sequenceNumber++;
1040 receive_timestamp += expected_samples_per_channel;
1041 }
1042
henrik.lundin6d8e0112016-03-04 10:34:21 -08001043 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001044
1045 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
1046 // one frame without checking speech-type. This is the first frame pulled
1047 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin55480f52016-03-08 02:37:57 -08001048 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001049 ASSERT_EQ(1u, output.num_channels_);
1050 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001051
1052 // To be able to test the fading of background noise we need at lease to
1053 // pull 611 frames.
1054 const int kFadingThreshold = 611;
1055
1056 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1057 // is arbitrary, but sufficiently large to test enough number of frames.
1058 const int kNumPlcToCngTestFrames = 20;
1059 bool plc_to_cng = false;
1060 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001061 output.Reset();
1062 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin55480f52016-03-08 02:37:57 -08001063 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001064 ASSERT_EQ(1u, output.num_channels_);
1065 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001066 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001067 plc_to_cng = true;
1068 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001069 for (size_t k = 0;
1070 k < output.num_channels_ * output.samples_per_channel_; ++k)
1071 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001072 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001073 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001074 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001075 }
1076 }
1077 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1078 }
1079};
1080
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001081class NetEqBgnTestOn : public NetEqBgnTest {
1082 protected:
1083 NetEqBgnTestOn() : NetEqBgnTest() {
1084 config_.background_noise_mode = NetEq::kBgnOn;
1085 }
1086
1087 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1088 EXPECT_NE(0, sum_squared_noise);
1089 }
1090};
1091
1092class NetEqBgnTestOff : public NetEqBgnTest {
1093 protected:
1094 NetEqBgnTestOff() : NetEqBgnTest() {
1095 config_.background_noise_mode = NetEq::kBgnOff;
1096 }
1097
1098 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1099 EXPECT_EQ(0, sum_squared_noise);
1100 }
1101};
1102
1103class NetEqBgnTestFade : public NetEqBgnTest {
1104 protected:
1105 NetEqBgnTestFade() : NetEqBgnTest() {
1106 config_.background_noise_mode = NetEq::kBgnFade;
1107 }
1108
1109 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1110 if (should_be_faded)
1111 EXPECT_EQ(0, sum_squared_noise);
1112 }
1113};
1114
henrika1d34fe92015-06-16 10:04:20 +02001115TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001116 CheckBgn(8000);
1117 CheckBgn(16000);
1118 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001119}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001120
henrika1d34fe92015-06-16 10:04:20 +02001121TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001122 CheckBgn(8000);
1123 CheckBgn(16000);
1124 CheckBgn(32000);
1125}
1126
henrika1d34fe92015-06-16 10:04:20 +02001127TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001128 CheckBgn(8000);
1129 CheckBgn(16000);
1130 CheckBgn(32000);
1131}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001132
Peter Boströme2976c82016-01-04 22:44:05 +01001133#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001134#define MAYBE_SyncPacketInsert SyncPacketInsert
1135#else
1136#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1137#endif
1138TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001139 WebRtcRTPHeader rtp_info;
1140 uint32_t receive_timestamp = 0;
1141 // For the readability use the following payloads instead of the defaults of
1142 // this test.
1143 uint8_t kPcm16WbPayloadType = 1;
1144 uint8_t kCngNbPayloadType = 2;
1145 uint8_t kCngWbPayloadType = 3;
1146 uint8_t kCngSwb32PayloadType = 4;
1147 uint8_t kCngSwb48PayloadType = 5;
1148 uint8_t kAvtPayloadType = 6;
1149 uint8_t kRedPayloadType = 7;
1150 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1151
1152 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001153 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001154 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001155 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001156 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001157 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001158 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001159 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001160 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001161 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001162 "cng-swb48", kCngSwb48PayloadType));
1163 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001164 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001165 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001166 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001167 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001168 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001169
1170 PopulateRtpInfo(0, 0, &rtp_info);
1171 rtp_info.header.payloadType = kPcm16WbPayloadType;
1172
1173 // The first packet injected cannot be sync-packet.
1174 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1175
1176 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001177 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001178 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001179 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001180
1181 // Next packet. Last packet contained 10 ms audio.
1182 rtp_info.header.sequenceNumber++;
1183 rtp_info.header.timestamp += kBlockSize16kHz;
1184 receive_timestamp += kBlockSize16kHz;
1185
1186 // Unacceptable payload types CNG, AVT (DTMF), RED.
1187 rtp_info.header.payloadType = kCngNbPayloadType;
1188 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1189
1190 rtp_info.header.payloadType = kCngWbPayloadType;
1191 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1192
1193 rtp_info.header.payloadType = kCngSwb32PayloadType;
1194 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1195
1196 rtp_info.header.payloadType = kCngSwb48PayloadType;
1197 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1198
1199 rtp_info.header.payloadType = kAvtPayloadType;
1200 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1201
1202 rtp_info.header.payloadType = kRedPayloadType;
1203 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1204
1205 // Change of codec cannot be initiated with a sync packet.
1206 rtp_info.header.payloadType = kIsacPayloadType;
1207 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1208
1209 // Change of SSRC is not allowed with a sync packet.
1210 rtp_info.header.payloadType = kPcm16WbPayloadType;
1211 ++rtp_info.header.ssrc;
1212 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1213
1214 --rtp_info.header.ssrc;
1215 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1216}
1217
1218// First insert several noise like packets, then sync-packets. Decoding all
1219// packets should not produce error, statistics should not show any packet loss
1220// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001221// TODO(turajs) we will have a better test if we have a referece NetEq, and
1222// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1223// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001224TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001225 WebRtcRTPHeader rtp_info;
1226 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001227 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001228 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001230 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001231 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1233 }
1234 // Insert some packets which decode to noise. We are not interested in
1235 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001236 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001237 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001238 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001239 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001240 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1241 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001242
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001243 rtp_info.header.sequenceNumber++;
1244 rtp_info.header.timestamp += kBlockSize16kHz;
1245 receive_timestamp += kBlockSize16kHz;
1246 }
1247 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001248
1249 // Make sure sufficient number of sync packets are inserted that we can
1250 // conduct a test.
1251 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001252 // Insert sync-packets, the decoded sequence should be all-zero.
1253 for (int n = 0; n < kNumSyncPackets; ++n) {
1254 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001255 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001256 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1257 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001258 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001259 EXPECT_TRUE(IsAllZero(
1260 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001261 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001262 rtp_info.header.sequenceNumber++;
1263 rtp_info.header.timestamp += kBlockSize16kHz;
1264 receive_timestamp += kBlockSize16kHz;
1265 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001266
1267 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001268 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001269 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001270 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001271 ASSERT_EQ(0, neteq_->GetAudio(&output));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001272 if (n >= algorithmic_frame_delay + 1) {
1273 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001274 EXPECT_TRUE(IsAllNonZero(
1275 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001276 }
1277 rtp_info.header.sequenceNumber++;
1278 rtp_info.header.timestamp += kBlockSize16kHz;
1279 receive_timestamp += kBlockSize16kHz;
1280 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001281 NetEqNetworkStatistics network_stats;
1282 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1283 // Expecting a "clean" network.
1284 EXPECT_EQ(0, network_stats.packet_loss_rate);
1285 EXPECT_EQ(0, network_stats.expand_rate);
1286 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001287 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001288}
1289
1290// Test if the size of the packet buffer reported correctly when containing
1291// sync packets. Also, test if network packets override sync packets. That is to
1292// prefer decoding a network packet to a sync packet, if both have same sequence
1293// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001294TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001295 WebRtcRTPHeader rtp_info;
1296 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001297 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001298 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001299 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001300 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001301 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1302 }
1303 // Insert some packets which decode to noise. We are not interested in
1304 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001305 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001306 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1307 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001308 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001309 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001310 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1311 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001312 rtp_info.header.sequenceNumber++;
1313 rtp_info.header.timestamp += kBlockSize16kHz;
1314 receive_timestamp += kBlockSize16kHz;
1315 }
1316 const int kNumSyncPackets = 10;
1317
1318 WebRtcRTPHeader first_sync_packet_rtp_info;
1319 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1320
1321 // Insert sync-packets, but no decoding.
1322 for (int n = 0; n < kNumSyncPackets; ++n) {
1323 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1324 rtp_info.header.sequenceNumber++;
1325 rtp_info.header.timestamp += kBlockSize16kHz;
1326 receive_timestamp += kBlockSize16kHz;
1327 }
1328 NetEqNetworkStatistics network_stats;
1329 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001330 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1331 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001332
1333 // Rewind |rtp_info| to that of the first sync packet.
1334 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1335
1336 // Insert.
1337 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001338 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001339 rtp_info.header.sequenceNumber++;
1340 rtp_info.header.timestamp += kBlockSize16kHz;
1341 receive_timestamp += kBlockSize16kHz;
1342 }
1343
1344 // Decode.
1345 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin55480f52016-03-08 02:37:57 -08001346 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001347 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1348 ASSERT_EQ(1u, output.num_channels_);
1349 EXPECT_TRUE(IsAllNonZero(
1350 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001351 }
1352}
1353
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001354void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1355 uint32_t start_timestamp,
1356 const std::set<uint16_t>& drop_seq_numbers,
1357 bool expect_seq_no_wrap,
1358 bool expect_timestamp_wrap) {
1359 uint16_t seq_no = start_seq_no;
1360 uint32_t timestamp = start_timestamp;
1361 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1362 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1363 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001364 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001365 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001366 uint32_t receive_timestamp = 0;
1367
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001368 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001369 const int kSpeechDurationMs = 2000;
1370 int packets_inserted = 0;
1371 uint16_t last_seq_no;
1372 uint32_t last_timestamp;
1373 bool timestamp_wrapped = false;
1374 bool seq_no_wrapped = false;
1375 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1376 // Each turn in this for loop is 10 ms.
1377 while (next_input_time_ms <= t_ms) {
1378 // Insert one 30 ms speech frame.
1379 uint8_t payload[kPayloadBytes] = {0};
1380 WebRtcRTPHeader rtp_info;
1381 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1382 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1383 // This sequence number was not in the set to drop. Insert it.
1384 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001385 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001386 ++packets_inserted;
1387 }
1388 NetEqNetworkStatistics network_stats;
1389 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1390
1391 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1392 // packet size for first few packets. Therefore we refrain from checking
1393 // the criteria.
1394 if (packets_inserted > 4) {
1395 // Expect preferred and actual buffer size to be no more than 2 frames.
1396 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001397 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1398 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001399 }
1400 last_seq_no = seq_no;
1401 last_timestamp = timestamp;
1402
1403 ++seq_no;
1404 timestamp += kSamples;
1405 receive_timestamp += kSamples;
1406 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1407
1408 seq_no_wrapped |= seq_no < last_seq_no;
1409 timestamp_wrapped |= timestamp < last_timestamp;
1410 }
1411 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001412 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -08001413 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001414 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1415 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001416
1417 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001418 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1419 ASSERT_TRUE(playout_timestamp);
1420 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001421 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001422 }
1423 // Make sure we have actually tested wrap-around.
1424 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1425 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1426}
1427
1428TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1429 // Start with a sequence number that will soon wrap.
1430 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1431 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1432}
1433
1434TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1435 // Start with a sequence number that will soon wrap.
1436 std::set<uint16_t> drop_seq_numbers;
1437 drop_seq_numbers.insert(0xFFFF);
1438 drop_seq_numbers.insert(0x0);
1439 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1440}
1441
1442TEST_F(NetEqDecodingTest, TimestampWrap) {
1443 // Start with a timestamp that will soon wrap.
1444 std::set<uint16_t> drop_seq_numbers;
1445 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1446}
1447
1448TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1449 // Start with a timestamp and a sequence number that will wrap at the same
1450 // time.
1451 std::set<uint16_t> drop_seq_numbers;
1452 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1453}
1454
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001455void NetEqDecodingTest::DuplicateCng() {
1456 uint16_t seq_no = 0;
1457 uint32_t timestamp = 0;
1458 const int kFrameSizeMs = 10;
1459 const int kSampleRateKhz = 16;
1460 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001461 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001462
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001463 const int algorithmic_delay_samples = std::max(
1464 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001465 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001466 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001467 uint8_t payload[kPayloadBytes] = {0};
1468 WebRtcRTPHeader rtp_info;
1469 for (int i = 0; i < 3; ++i) {
1470 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001471 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001472 ++seq_no;
1473 timestamp += kSamples;
1474
1475 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001476 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001477 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001478 }
1479 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001480 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001481
1482 // Insert same CNG packet twice.
1483 const int kCngPeriodMs = 100;
1484 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001485 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001486 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1487 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001488 ASSERT_EQ(
1489 0, neteq_->InsertPacket(
1490 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001491
1492 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001493 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001494 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001495 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001496 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1497 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1498 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001499
1500 // Insert the same CNG packet again. Note that at this point it is old, since
1501 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001502 ASSERT_EQ(
1503 0, neteq_->InsertPacket(
1504 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001505
1506 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1507 // we have already pulled out CNG once.
1508 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin55480f52016-03-08 02:37:57 -08001509 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001510 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001511 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001512 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001513 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001514 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001515 }
1516
1517 // Insert speech again.
1518 ++seq_no;
1519 timestamp += kCngPeriodSamples;
1520 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001521 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001522
1523 // Pull audio once and verify that the output is speech again.
henrik.lundin55480f52016-03-08 02:37:57 -08001524 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001525 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001526 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001527 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1528 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001529 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001530 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001531}
1532
henrik.lundin0d96ab72016-04-06 12:28:26 -07001533rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1534 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001535}
1536
1537TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001538
1539TEST_F(NetEqDecodingTest, CngFirst) {
1540 uint16_t seq_no = 0;
1541 uint32_t timestamp = 0;
1542 const int kFrameSizeMs = 10;
1543 const int kSampleRateKhz = 16;
1544 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1545 const int kPayloadBytes = kSamples * 2;
1546 const int kCngPeriodMs = 100;
1547 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1548 size_t payload_len;
1549
1550 uint8_t payload[kPayloadBytes] = {0};
1551 WebRtcRTPHeader rtp_info;
1552
1553 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001554 ASSERT_EQ(
1555 NetEq::kOK,
1556 neteq_->InsertPacket(
1557 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001558 ++seq_no;
1559 timestamp += kCngPeriodSamples;
1560
1561 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001562 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001563 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001564 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001565
1566 // Insert some speech packets.
1567 for (int i = 0; i < 3; ++i) {
1568 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001569 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001570 ++seq_no;
1571 timestamp += kSamples;
1572
1573 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001574 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001575 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001576 }
1577 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001578 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001579}
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001580} // namespace webrtc