blob: 8a662622534def0f37245f40f8ea87057ea67803 [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.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/interface/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
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000040static bool IsAllZero(const int16_t* buf, int buf_length) {
41 bool all_zero = true;
42 for (int n = 0; n < buf_length && all_zero; ++n)
43 all_zero = buf[n] == 0;
44 return all_zero;
45}
46
47static bool IsAllNonZero(const int16_t* buf, int buf_length) {
48 bool all_non_zero = true;
49 for (int n = 0; n < buf_length && all_non_zero; ++n)
50 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);
175 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples);
176 ASSERT_EQ(stats.secondary_decoded_rate, 0);
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000177 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178 }
179}
180
181void RefFiles::WriteToFile(const RtcpStatistics& stats) {
182 if (output_fp_) {
183 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
184 output_fp_));
185 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
186 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000187 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
188 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 output_fp_));
190 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
191 output_fp_));
192 }
193}
194
195void RefFiles::ReadFromFileAndCompare(
196 const RtcpStatistics& stats) {
197 if (input_fp_) {
198 // Read from ref file.
199 RtcpStatistics ref_stats;
200 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
201 sizeof(ref_stats.fraction_lost), 1, input_fp_));
202 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
203 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000204 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
205 sizeof(ref_stats.extended_max_sequence_number), 1,
206 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
208 input_fp_));
209 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000210 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
211 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
212 ASSERT_EQ(ref_stats.extended_max_sequence_number,
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000213 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000214 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000215 }
216}
217
218class NetEqDecodingTest : public ::testing::Test {
219 protected:
220 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
221 // constants below can be changed.
222 static const int kTimeStepMs = 10;
223 static const int kBlockSize8kHz = kTimeStepMs * 8;
224 static const int kBlockSize16kHz = kTimeStepMs * 16;
225 static const int kBlockSize32kHz = kTimeStepMs * 32;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000226 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000227 static const int kInitSampleRateHz = 8000;
228
229 NetEqDecodingTest();
230 virtual void SetUp();
231 virtual void TearDown();
232 void SelectDecoders(NetEqDecoder* used_codec);
233 void LoadDecoders();
234 void OpenInputFile(const std::string &rtp_file);
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000235 void Process(int* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000236 void DecodeAndCompare(const std::string& rtp_file,
237 const std::string& ref_file,
238 const std::string& stat_ref_file,
239 const std::string& rtcp_ref_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000240 static void PopulateRtpInfo(int frame_index,
241 int timestamp,
242 WebRtcRTPHeader* rtp_info);
243 static void PopulateCng(int frame_index,
244 int timestamp,
245 WebRtcRTPHeader* rtp_info,
246 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000247 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000249 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
250 const std::set<uint16_t>& drop_seq_numbers,
251 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
252
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000253 void LongCngWithClockDrift(double drift_factor,
254 double network_freeze_ms,
255 bool pull_audio_during_freeze,
256 int delay_tolerance_ms,
257 int max_time_to_speech_ms);
258
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000259 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000260
wu@webrtc.org94454b72014-06-05 20:34:08 +0000261 uint32_t PlayoutTimestamp();
262
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000264 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000265 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
266 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 unsigned int sim_clock_;
268 int16_t out_data_[kMaxBlockSize];
269 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000270 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271};
272
273// Allocating the static const so that it can be passed by reference.
274const int NetEqDecodingTest::kTimeStepMs;
275const int NetEqDecodingTest::kBlockSize8kHz;
276const int NetEqDecodingTest::kBlockSize16kHz;
277const int NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000278const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279const int NetEqDecodingTest::kInitSampleRateHz;
280
281NetEqDecodingTest::NetEqDecodingTest()
282 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000283 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000285 output_sample_rate_(kInitSampleRateHz),
286 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000287 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 memset(out_data_, 0, sizeof(out_data_));
289}
290
291void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000292 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000293 NetEqNetworkStatistics stat;
294 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
295 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 ASSERT_TRUE(neteq_);
297 LoadDecoders();
298}
299
300void NetEqDecodingTest::TearDown() {
301 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000302}
303
304void NetEqDecodingTest::LoadDecoders() {
305 // Load PCMu.
306 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
307 // Load PCMa.
308 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000309#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310 // Load iLBC.
311 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000312#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313 // Load iSAC.
314 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000315#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000316 // Load iSAC SWB.
317 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000318 // Load iSAC FB.
319 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000320#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 // Load PCM16B nb.
322 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
323 // Load PCM16B wb.
324 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
325 // Load PCM16B swb32.
326 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
327 // Load CNG 8 kHz.
328 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
329 // Load CNG 16 kHz.
330 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
331}
332
333void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000334 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335}
336
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000337void NetEqDecodingTest::Process(int* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000339 while (packet_ && sim_clock_ >= packet_->time_ms()) {
340 if (packet_->payload_length_bytes() > 0) {
341 WebRtcRTPHeader rtp_header;
342 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000344 rtp_header, packet_->payload(),
345 packet_->payload_length_bytes(),
Peter Kastingb7e50542015-06-11 12:55:50 -0700346 static_cast<uint32_t>(
347 packet_->time_ms() * (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 }
349 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000350 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 }
352
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000353 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 NetEqOutputType type;
355 int num_channels;
356 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
357 &num_channels, &type));
358 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
359 (*out_len == kBlockSize16kHz) ||
360 (*out_len == kBlockSize32kHz));
361 output_sample_rate_ = *out_len / 10 * 1000;
362
363 // Increase time.
364 sim_clock_ += kTimeStepMs;
365}
366
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000367void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
368 const std::string& ref_file,
369 const std::string& stat_ref_file,
370 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 OpenInputFile(rtp_file);
372
373 std::string ref_out_file = "";
374 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000375 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 }
377 RefFiles ref_files(ref_file, ref_out_file);
378
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000379 std::string stat_out_file = "";
380 if (stat_ref_file.empty()) {
381 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
382 }
383 RefFiles network_stat_files(stat_ref_file, stat_out_file);
384
385 std::string rtcp_out_file = "";
386 if (rtcp_ref_file.empty()) {
387 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
388 }
389 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
390
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000391 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000393 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 std::ostringstream ss;
395 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
396 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000397 int out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000398 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400
401 // Query the network statistics API once per second
402 if (sim_clock_ % 1000 == 0) {
403 // Process NetworkStatistics.
404 NetEqNetworkStatistics network_stats;
405 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000406 ASSERT_NO_FATAL_FAILURE(
407 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000408
409 // Process RTCPstat.
410 RtcpStatistics rtcp_stats;
411 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000412 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413 }
414 }
415}
416
417void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
418 int timestamp,
419 WebRtcRTPHeader* rtp_info) {
420 rtp_info->header.sequenceNumber = frame_index;
421 rtp_info->header.timestamp = timestamp;
422 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
423 rtp_info->header.payloadType = 94; // PCM16b WB codec.
424 rtp_info->header.markerBit = 0;
425}
426
427void NetEqDecodingTest::PopulateCng(int frame_index,
428 int timestamp,
429 WebRtcRTPHeader* rtp_info,
430 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000431 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000432 rtp_info->header.sequenceNumber = frame_index;
433 rtp_info->header.timestamp = timestamp;
434 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
435 rtp_info->header.payloadType = 98; // WB CNG.
436 rtp_info->header.markerBit = 0;
437 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
438 *payload_len = 1; // Only noise level, no spectral parameters.
439}
440
henrikaa2c79402015-06-10 13:24:48 +0200441// TODO(henrika): add support for IOS for all tests in this file.
442// See https://code.google.com/p/webrtc/issues/detail?id=4752 for details.
443TEST_F(NetEqDecodingTest,
444 DISABLED_ON_IOS(DISABLED_ON_ANDROID(TestBitExactness))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000445 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000446 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000447 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
448 // are identical. The latter could have been removed, but if clients still
449 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000450 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000451 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000452#if defined(_MSC_VER) && (_MSC_VER >= 1700)
453 // For Visual Studio 2012 and later, we will have to use the generic reference
454 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000455 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000456 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000457#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000458 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000459 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000460#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000461 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000462 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000463
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000464 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000465 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000466 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000467 DecodeAndCompare(input_rtp_file,
468 input_ref_file,
469 network_stat_ref_file,
470 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000471 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000472}
473
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000474// Use fax mode to avoid time-scaling. This is to simplify the testing of
475// packet waiting times in the packet buffer.
476class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
477 protected:
478 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
479 config_.playout_mode = kPlayoutFax;
480 }
481};
482
483TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000484 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
485 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000486 const size_t kSamples = 10 * 16;
487 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000488 for (size_t i = 0; i < num_frames; ++i) {
489 uint16_t payload[kSamples] = {0};
490 WebRtcRTPHeader rtp_info;
491 rtp_info.header.sequenceNumber = i;
492 rtp_info.header.timestamp = i * kSamples;
493 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
494 rtp_info.header.payloadType = 94; // PCM16b WB codec.
495 rtp_info.header.markerBit = 0;
496 ASSERT_EQ(0, neteq_->InsertPacket(
497 rtp_info,
498 reinterpret_cast<uint8_t*>(payload),
499 kPayloadBytes, 0));
500 }
501 // Pull out all data.
502 for (size_t i = 0; i < num_frames; ++i) {
503 int out_len;
504 int num_channels;
505 NetEqOutputType type;
506 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
507 &num_channels, &type));
508 ASSERT_EQ(kBlockSize16kHz, out_len);
509 }
510
511 std::vector<int> waiting_times;
512 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000513 EXPECT_EQ(num_frames, waiting_times.size());
514 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
515 // spacing (per definition), we expect the delay to increase with 10 ms for
516 // each packet.
517 for (size_t i = 0; i < waiting_times.size(); ++i) {
518 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
519 }
520
521 // Check statistics again and make sure it's been reset.
522 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000523 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000524 EXPECT_EQ(0, len);
525
526 // Process > 100 frames, and make sure that that we get statistics
527 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
528 num_frames = 110;
529 for (size_t i = 0; i < num_frames; ++i) {
530 uint16_t payload[kSamples] = {0};
531 WebRtcRTPHeader rtp_info;
532 rtp_info.header.sequenceNumber = i;
533 rtp_info.header.timestamp = i * kSamples;
534 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
535 rtp_info.header.payloadType = 94; // PCM16b WB codec.
536 rtp_info.header.markerBit = 0;
537 ASSERT_EQ(0, neteq_->InsertPacket(
538 rtp_info,
539 reinterpret_cast<uint8_t*>(payload),
540 kPayloadBytes, 0));
541 int out_len;
542 int num_channels;
543 NetEqOutputType type;
544 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
545 &num_channels, &type));
546 ASSERT_EQ(kBlockSize16kHz, out_len);
547 }
548
549 neteq_->WaitingTimes(&waiting_times);
550 EXPECT_EQ(100u, waiting_times.size());
551}
552
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000553TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554 const int kNumFrames = 3000; // Needed for convergence.
555 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000556 const size_t kSamples = 10 * 16;
557 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 while (frame_index < kNumFrames) {
559 // Insert one packet each time, except every 10th time where we insert two
560 // packets at once. This will create a negative clock-drift of approx. 10%.
561 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
562 for (int n = 0; n < num_packets; ++n) {
563 uint8_t payload[kPayloadBytes] = {0};
564 WebRtcRTPHeader rtp_info;
565 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
566 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
567 ++frame_index;
568 }
569
570 // Pull out data once.
571 int out_len;
572 int num_channels;
573 NetEqOutputType type;
574 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
575 &num_channels, &type));
576 ASSERT_EQ(kBlockSize16kHz, out_len);
577 }
578
579 NetEqNetworkStatistics network_stats;
580 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
581 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
582}
583
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000584TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585 const int kNumFrames = 5000; // Needed for convergence.
586 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000587 const size_t kSamples = 10 * 16;
588 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 for (int i = 0; i < kNumFrames; ++i) {
590 // Insert one packet each time, except every 10th time where we don't insert
591 // any packet. This will create a positive clock-drift of approx. 11%.
592 int num_packets = (i % 10 == 9 ? 0 : 1);
593 for (int n = 0; n < num_packets; ++n) {
594 uint8_t payload[kPayloadBytes] = {0};
595 WebRtcRTPHeader rtp_info;
596 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
597 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
598 ++frame_index;
599 }
600
601 // Pull out data once.
602 int out_len;
603 int num_channels;
604 NetEqOutputType type;
605 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
606 &num_channels, &type));
607 ASSERT_EQ(kBlockSize16kHz, out_len);
608 }
609
610 NetEqNetworkStatistics network_stats;
611 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
612 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
613}
614
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000615void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
616 double network_freeze_ms,
617 bool pull_audio_during_freeze,
618 int delay_tolerance_ms,
619 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000620 uint16_t seq_no = 0;
621 uint32_t timestamp = 0;
622 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000623 const size_t kSamples = kFrameSizeMs * 16;
624 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000625 double next_input_time_ms = 0.0;
626 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000627 int out_len;
628 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629 NetEqOutputType type;
630
631 // Insert speech for 5 seconds.
632 const int kSpeechDurationMs = 5000;
633 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
634 // Each turn in this for loop is 10 ms.
635 while (next_input_time_ms <= t_ms) {
636 // Insert one 30 ms speech frame.
637 uint8_t payload[kPayloadBytes] = {0};
638 WebRtcRTPHeader rtp_info;
639 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
640 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
641 ++seq_no;
642 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000643 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000644 }
645 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
647 &num_channels, &type));
648 ASSERT_EQ(kBlockSize16kHz, out_len);
649 }
650
651 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000652 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000653
654 // Insert CNG for 1 minute (= 60000 ms).
655 const int kCngPeriodMs = 100;
656 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
657 const int kCngDurationMs = 60000;
658 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
659 // Each turn in this for loop is 10 ms.
660 while (next_input_time_ms <= t_ms) {
661 // Insert one CNG frame each 100 ms.
662 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000663 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 WebRtcRTPHeader rtp_info;
665 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
666 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
667 ++seq_no;
668 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000669 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000670 }
671 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000672 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
673 &num_channels, &type));
674 ASSERT_EQ(kBlockSize16kHz, out_len);
675 }
676
677 EXPECT_EQ(kOutputCNG, type);
678
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000679 if (network_freeze_ms > 0) {
680 // First keep pulling audio for |network_freeze_ms| without inserting
681 // any data, then insert CNG data corresponding to |network_freeze_ms|
682 // without pulling any output audio.
683 const double loop_end_time = t_ms + network_freeze_ms;
684 for (; t_ms < loop_end_time; t_ms += 10) {
685 // Pull out data once.
686 ASSERT_EQ(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 }
692 bool pull_once = pull_audio_during_freeze;
693 // If |pull_once| is true, GetAudio will be called once half-way through
694 // the network recovery period.
695 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
696 while (next_input_time_ms <= t_ms) {
697 if (pull_once && next_input_time_ms >= pull_time_ms) {
698 pull_once = false;
699 // Pull out data once.
700 ASSERT_EQ(
701 0,
702 neteq_->GetAudio(
703 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
704 ASSERT_EQ(kBlockSize16kHz, out_len);
705 EXPECT_EQ(kOutputCNG, type);
706 t_ms += 10;
707 }
708 // Insert one CNG frame each 100 ms.
709 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000710 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000711 WebRtcRTPHeader rtp_info;
712 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
713 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
714 ++seq_no;
715 timestamp += kCngPeriodSamples;
716 next_input_time_ms += kCngPeriodMs * drift_factor;
717 }
718 }
719
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000721 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 while (type != kOutputNormal) {
723 // Each turn in this for loop is 10 ms.
724 while (next_input_time_ms <= t_ms) {
725 // Insert one 30 ms speech frame.
726 uint8_t payload[kPayloadBytes] = {0};
727 WebRtcRTPHeader rtp_info;
728 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
729 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
730 ++seq_no;
731 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733 }
734 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000735 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
736 &num_channels, &type));
737 ASSERT_EQ(kBlockSize16kHz, out_len);
738 // Increase clock.
739 t_ms += 10;
740 }
741
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000742 // Check that the speech starts again within reasonable time.
743 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
744 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000745 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000746 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000747 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
748 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749}
750
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000751TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000752 // Apply a clock drift of -25 ms / s (sender faster than receiver).
753 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000754 const double kNetworkFreezeTimeMs = 0.0;
755 const bool kGetAudioDuringFreezeRecovery = false;
756 const int kDelayToleranceMs = 20;
757 const int kMaxTimeToSpeechMs = 100;
758 LongCngWithClockDrift(kDriftFactor,
759 kNetworkFreezeTimeMs,
760 kGetAudioDuringFreezeRecovery,
761 kDelayToleranceMs,
762 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000763}
764
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000765TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000766 // Apply a clock drift of +25 ms / s (sender slower than receiver).
767 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000768 const double kNetworkFreezeTimeMs = 0.0;
769 const bool kGetAudioDuringFreezeRecovery = false;
770 const int kDelayToleranceMs = 20;
771 const int kMaxTimeToSpeechMs = 100;
772 LongCngWithClockDrift(kDriftFactor,
773 kNetworkFreezeTimeMs,
774 kGetAudioDuringFreezeRecovery,
775 kDelayToleranceMs,
776 kMaxTimeToSpeechMs);
777}
778
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000779TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000780 // Apply a clock drift of -25 ms / s (sender faster than receiver).
781 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
782 const double kNetworkFreezeTimeMs = 5000.0;
783 const bool kGetAudioDuringFreezeRecovery = false;
784 const int kDelayToleranceMs = 50;
785 const int kMaxTimeToSpeechMs = 200;
786 LongCngWithClockDrift(kDriftFactor,
787 kNetworkFreezeTimeMs,
788 kGetAudioDuringFreezeRecovery,
789 kDelayToleranceMs,
790 kMaxTimeToSpeechMs);
791}
792
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000793TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000794 // Apply a clock drift of +25 ms / s (sender slower than receiver).
795 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
796 const double kNetworkFreezeTimeMs = 5000.0;
797 const bool kGetAudioDuringFreezeRecovery = false;
798 const int kDelayToleranceMs = 20;
799 const int kMaxTimeToSpeechMs = 100;
800 LongCngWithClockDrift(kDriftFactor,
801 kNetworkFreezeTimeMs,
802 kGetAudioDuringFreezeRecovery,
803 kDelayToleranceMs,
804 kMaxTimeToSpeechMs);
805}
806
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000807TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000808 // Apply a clock drift of +25 ms / s (sender slower than receiver).
809 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
810 const double kNetworkFreezeTimeMs = 5000.0;
811 const bool kGetAudioDuringFreezeRecovery = true;
812 const int kDelayToleranceMs = 20;
813 const int kMaxTimeToSpeechMs = 100;
814 LongCngWithClockDrift(kDriftFactor,
815 kNetworkFreezeTimeMs,
816 kGetAudioDuringFreezeRecovery,
817 kDelayToleranceMs,
818 kMaxTimeToSpeechMs);
819}
820
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000821TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000822 const double kDriftFactor = 1.0; // No drift.
823 const double kNetworkFreezeTimeMs = 0.0;
824 const bool kGetAudioDuringFreezeRecovery = false;
825 const int kDelayToleranceMs = 10;
826 const int kMaxTimeToSpeechMs = 50;
827 LongCngWithClockDrift(kDriftFactor,
828 kNetworkFreezeTimeMs,
829 kGetAudioDuringFreezeRecovery,
830 kDelayToleranceMs,
831 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000832}
833
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000834TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000835 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000836 uint8_t payload[kPayloadBytes] = {0};
837 WebRtcRTPHeader rtp_info;
838 PopulateRtpInfo(0, 0, &rtp_info);
839 rtp_info.header.payloadType = 1; // Not registered as a decoder.
840 EXPECT_EQ(NetEq::kFail,
841 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
842 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
843}
844
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000845TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000846 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 uint8_t payload[kPayloadBytes] = {0};
848 WebRtcRTPHeader rtp_info;
849 PopulateRtpInfo(0, 0, &rtp_info);
850 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
851 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
852 NetEqOutputType type;
853 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
854 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000855 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000856 out_data_[i] = 1;
857 }
858 int num_channels;
859 int samples_per_channel;
860 EXPECT_EQ(NetEq::kFail,
861 neteq_->GetAudio(kMaxBlockSize, out_data_,
862 &samples_per_channel, &num_channels, &type));
863 // Verify that there is a decoder error to check.
864 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
865 // Code 6730 is an iSAC error code.
866 EXPECT_EQ(6730, neteq_->LastDecoderError());
867 // Verify that the first 160 samples are set to 0, and that the remaining
868 // samples are left unmodified.
869 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
870 for (int i = 0; i < kExpectedOutputLength; ++i) {
871 std::ostringstream ss;
872 ss << "i = " << i;
873 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
874 EXPECT_EQ(0, out_data_[i]);
875 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000876 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000877 std::ostringstream ss;
878 ss << "i = " << i;
879 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
880 EXPECT_EQ(1, out_data_[i]);
881 }
882}
883
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000884TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000885 NetEqOutputType type;
886 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
887 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000888 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 out_data_[i] = 1;
890 }
891 int num_channels;
892 int samples_per_channel;
893 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
894 &samples_per_channel,
895 &num_channels, &type));
896 // Verify that the first block of samples is set to 0.
897 static const int kExpectedOutputLength =
898 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
899 for (int i = 0; i < kExpectedOutputLength; ++i) {
900 std::ostringstream ss;
901 ss << "i = " << i;
902 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
903 EXPECT_EQ(0, out_data_[i]);
904 }
905}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000906
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000907class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000908 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000909 virtual void TestCondition(double sum_squared_noise,
910 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000911
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000912 void CheckBgn(int sampling_rate_hz) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000913 int16_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000914 uint8_t payload_type = 0xFF; // Invalid.
915 if (sampling_rate_hz == 8000) {
916 expected_samples_per_channel = kBlockSize8kHz;
917 payload_type = 93; // PCM 16, 8 kHz.
918 } else if (sampling_rate_hz == 16000) {
919 expected_samples_per_channel = kBlockSize16kHz;
920 payload_type = 94; // PCM 16, 16 kHZ.
921 } else if (sampling_rate_hz == 32000) {
922 expected_samples_per_channel = kBlockSize32kHz;
923 payload_type = 95; // PCM 16, 32 kHz.
924 } else {
925 ASSERT_TRUE(false); // Unsupported test case.
926 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000927
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000928 NetEqOutputType type;
929 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000930 test::AudioLoop input;
931 // We are using the same 32 kHz input file for all tests, regardless of
932 // |sampling_rate_hz|. The output may sound weird, but the test is still
933 // valid.
934 ASSERT_TRUE(input.Init(
935 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
936 10 * sampling_rate_hz, // Max 10 seconds loop length.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000937 static_cast<size_t>(expected_samples_per_channel)));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000938
939 // Payload of 10 ms of PCM16 32 kHz.
940 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000941 WebRtcRTPHeader rtp_info;
942 PopulateRtpInfo(0, 0, &rtp_info);
943 rtp_info.header.payloadType = payload_type;
944
945 int number_channels = 0;
946 int samples_per_channel = 0;
947
948 uint32_t receive_timestamp = 0;
949 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg@webrtc.org648f5d62015-02-10 09:18:28 +0000950 int16_t enc_len_bytes = WebRtcPcm16b_Encode(
951 input.GetNextBlock(), expected_samples_per_channel, payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000952 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
953
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000954 number_channels = 0;
955 samples_per_channel = 0;
956 ASSERT_EQ(0,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000957 neteq_->InsertPacket(rtp_info, payload,
958 static_cast<size_t>(enc_len_bytes),
959 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000960 ASSERT_EQ(0,
961 neteq_->GetAudio(kBlockSize32kHz,
962 output,
963 &samples_per_channel,
964 &number_channels,
965 &type));
966 ASSERT_EQ(1, number_channels);
967 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
968 ASSERT_EQ(kOutputNormal, type);
969
970 // Next packet.
971 rtp_info.header.timestamp += expected_samples_per_channel;
972 rtp_info.header.sequenceNumber++;
973 receive_timestamp += expected_samples_per_channel;
974 }
975
976 number_channels = 0;
977 samples_per_channel = 0;
978
979 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
980 // one frame without checking speech-type. This is the first frame pulled
981 // without inserting any packet, and might not be labeled as PLC.
982 ASSERT_EQ(0,
983 neteq_->GetAudio(kBlockSize32kHz,
984 output,
985 &samples_per_channel,
986 &number_channels,
987 &type));
988 ASSERT_EQ(1, number_channels);
989 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
990
991 // To be able to test the fading of background noise we need at lease to
992 // pull 611 frames.
993 const int kFadingThreshold = 611;
994
995 // Test several CNG-to-PLC packet for the expected behavior. The number 20
996 // is arbitrary, but sufficiently large to test enough number of frames.
997 const int kNumPlcToCngTestFrames = 20;
998 bool plc_to_cng = false;
999 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
1000 number_channels = 0;
1001 samples_per_channel = 0;
1002 memset(output, 1, sizeof(output)); // Set to non-zero.
1003 ASSERT_EQ(0,
1004 neteq_->GetAudio(kBlockSize32kHz,
1005 output,
1006 &samples_per_channel,
1007 &number_channels,
1008 &type));
1009 ASSERT_EQ(1, number_channels);
1010 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1011 if (type == kOutputPLCtoCNG) {
1012 plc_to_cng = true;
1013 double sum_squared = 0;
1014 for (int k = 0; k < number_channels * samples_per_channel; ++k)
1015 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001016 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001017 } else {
1018 EXPECT_EQ(kOutputPLC, type);
1019 }
1020 }
1021 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1022 }
1023};
1024
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001025class NetEqBgnTestOn : public NetEqBgnTest {
1026 protected:
1027 NetEqBgnTestOn() : NetEqBgnTest() {
1028 config_.background_noise_mode = NetEq::kBgnOn;
1029 }
1030
1031 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1032 EXPECT_NE(0, sum_squared_noise);
1033 }
1034};
1035
1036class NetEqBgnTestOff : public NetEqBgnTest {
1037 protected:
1038 NetEqBgnTestOff() : NetEqBgnTest() {
1039 config_.background_noise_mode = NetEq::kBgnOff;
1040 }
1041
1042 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1043 EXPECT_EQ(0, sum_squared_noise);
1044 }
1045};
1046
1047class NetEqBgnTestFade : public NetEqBgnTest {
1048 protected:
1049 NetEqBgnTestFade() : NetEqBgnTest() {
1050 config_.background_noise_mode = NetEq::kBgnFade;
1051 }
1052
1053 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1054 if (should_be_faded)
1055 EXPECT_EQ(0, sum_squared_noise);
1056 }
1057};
1058
henrikaa2c79402015-06-10 13:24:48 +02001059TEST_F(NetEqBgnTestOn, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001060 CheckBgn(8000);
1061 CheckBgn(16000);
1062 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001063}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001064
henrikaa2c79402015-06-10 13:24:48 +02001065TEST_F(NetEqBgnTestOff, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001066 CheckBgn(8000);
1067 CheckBgn(16000);
1068 CheckBgn(32000);
1069}
1070
henrikaa2c79402015-06-10 13:24:48 +02001071TEST_F(NetEqBgnTestFade, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001072 CheckBgn(8000);
1073 CheckBgn(16000);
1074 CheckBgn(32000);
1075}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001076
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001077TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001078 WebRtcRTPHeader rtp_info;
1079 uint32_t receive_timestamp = 0;
1080 // For the readability use the following payloads instead of the defaults of
1081 // this test.
1082 uint8_t kPcm16WbPayloadType = 1;
1083 uint8_t kCngNbPayloadType = 2;
1084 uint8_t kCngWbPayloadType = 3;
1085 uint8_t kCngSwb32PayloadType = 4;
1086 uint8_t kCngSwb48PayloadType = 5;
1087 uint8_t kAvtPayloadType = 6;
1088 uint8_t kRedPayloadType = 7;
1089 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1090
1091 // Register decoders.
1092 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1093 kPcm16WbPayloadType));
1094 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1095 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1096 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1097 kCngSwb32PayloadType));
1098 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1099 kCngSwb48PayloadType));
1100 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1101 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1102 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1103
1104 PopulateRtpInfo(0, 0, &rtp_info);
1105 rtp_info.header.payloadType = kPcm16WbPayloadType;
1106
1107 // The first packet injected cannot be sync-packet.
1108 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1109
1110 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001111 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001112 uint8_t payload[kPayloadBytes] = {0};
1113 ASSERT_EQ(0, neteq_->InsertPacket(
1114 rtp_info, payload, kPayloadBytes, receive_timestamp));
1115
1116 // Next packet. Last packet contained 10 ms audio.
1117 rtp_info.header.sequenceNumber++;
1118 rtp_info.header.timestamp += kBlockSize16kHz;
1119 receive_timestamp += kBlockSize16kHz;
1120
1121 // Unacceptable payload types CNG, AVT (DTMF), RED.
1122 rtp_info.header.payloadType = kCngNbPayloadType;
1123 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1124
1125 rtp_info.header.payloadType = kCngWbPayloadType;
1126 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1127
1128 rtp_info.header.payloadType = kCngSwb32PayloadType;
1129 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1130
1131 rtp_info.header.payloadType = kCngSwb48PayloadType;
1132 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1133
1134 rtp_info.header.payloadType = kAvtPayloadType;
1135 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1136
1137 rtp_info.header.payloadType = kRedPayloadType;
1138 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1139
1140 // Change of codec cannot be initiated with a sync packet.
1141 rtp_info.header.payloadType = kIsacPayloadType;
1142 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1143
1144 // Change of SSRC is not allowed with a sync packet.
1145 rtp_info.header.payloadType = kPcm16WbPayloadType;
1146 ++rtp_info.header.ssrc;
1147 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1148
1149 --rtp_info.header.ssrc;
1150 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1151}
1152
1153// First insert several noise like packets, then sync-packets. Decoding all
1154// packets should not produce error, statistics should not show any packet loss
1155// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001156// TODO(turajs) we will have a better test if we have a referece NetEq, and
1157// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1158// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001159TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001160 WebRtcRTPHeader rtp_info;
1161 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001162 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001163 uint8_t payload[kPayloadBytes];
1164 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001165 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001166 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001167 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1168 }
1169 // Insert some packets which decode to noise. We are not interested in
1170 // actual decoded values.
1171 NetEqOutputType output_type;
1172 int num_channels;
1173 int samples_per_channel;
1174 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001175 for (int n = 0; n < 100; ++n) {
1176 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1177 receive_timestamp));
1178 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1179 &samples_per_channel, &num_channels,
1180 &output_type));
1181 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1182 ASSERT_EQ(1, num_channels);
1183
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001184 rtp_info.header.sequenceNumber++;
1185 rtp_info.header.timestamp += kBlockSize16kHz;
1186 receive_timestamp += kBlockSize16kHz;
1187 }
1188 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001189
1190 // Make sure sufficient number of sync packets are inserted that we can
1191 // conduct a test.
1192 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001193 // Insert sync-packets, the decoded sequence should be all-zero.
1194 for (int n = 0; n < kNumSyncPackets; ++n) {
1195 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1196 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1197 &samples_per_channel, &num_channels,
1198 &output_type));
1199 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1200 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001201 if (n > algorithmic_frame_delay) {
1202 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1203 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001204 rtp_info.header.sequenceNumber++;
1205 rtp_info.header.timestamp += kBlockSize16kHz;
1206 receive_timestamp += kBlockSize16kHz;
1207 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001208
1209 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001210 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001211 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1212 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1213 receive_timestamp));
1214 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1215 &samples_per_channel, &num_channels,
1216 &output_type));
1217 if (n >= algorithmic_frame_delay + 1) {
1218 // Expect that this frame contain samples from regular RTP.
1219 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1220 }
1221 rtp_info.header.sequenceNumber++;
1222 rtp_info.header.timestamp += kBlockSize16kHz;
1223 receive_timestamp += kBlockSize16kHz;
1224 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001225 NetEqNetworkStatistics network_stats;
1226 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1227 // Expecting a "clean" network.
1228 EXPECT_EQ(0, network_stats.packet_loss_rate);
1229 EXPECT_EQ(0, network_stats.expand_rate);
1230 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001231 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232}
1233
1234// Test if the size of the packet buffer reported correctly when containing
1235// sync packets. Also, test if network packets override sync packets. That is to
1236// prefer decoding a network packet to a sync packet, if both have same sequence
1237// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001238TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001239 WebRtcRTPHeader rtp_info;
1240 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001241 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001242 uint8_t payload[kPayloadBytes];
1243 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001244 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001245 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1246 }
1247 // Insert some packets which decode to noise. We are not interested in
1248 // actual decoded values.
1249 NetEqOutputType output_type;
1250 int num_channels;
1251 int samples_per_channel;
1252 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001253 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1254 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001255 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1256 receive_timestamp));
1257 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1258 &samples_per_channel, &num_channels,
1259 &output_type));
1260 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1261 ASSERT_EQ(1, num_channels);
1262 rtp_info.header.sequenceNumber++;
1263 rtp_info.header.timestamp += kBlockSize16kHz;
1264 receive_timestamp += kBlockSize16kHz;
1265 }
1266 const int kNumSyncPackets = 10;
1267
1268 WebRtcRTPHeader first_sync_packet_rtp_info;
1269 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1270
1271 // Insert sync-packets, but no decoding.
1272 for (int n = 0; n < kNumSyncPackets; ++n) {
1273 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1274 rtp_info.header.sequenceNumber++;
1275 rtp_info.header.timestamp += kBlockSize16kHz;
1276 receive_timestamp += kBlockSize16kHz;
1277 }
1278 NetEqNetworkStatistics network_stats;
1279 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001280 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1281 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001282
1283 // Rewind |rtp_info| to that of the first sync packet.
1284 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1285
1286 // Insert.
1287 for (int n = 0; n < kNumSyncPackets; ++n) {
1288 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1289 receive_timestamp));
1290 rtp_info.header.sequenceNumber++;
1291 rtp_info.header.timestamp += kBlockSize16kHz;
1292 receive_timestamp += kBlockSize16kHz;
1293 }
1294
1295 // Decode.
1296 for (int n = 0; n < kNumSyncPackets; ++n) {
1297 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1298 &samples_per_channel, &num_channels,
1299 &output_type));
1300 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1301 ASSERT_EQ(1, num_channels);
1302 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1303 }
1304}
1305
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001306void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1307 uint32_t start_timestamp,
1308 const std::set<uint16_t>& drop_seq_numbers,
1309 bool expect_seq_no_wrap,
1310 bool expect_timestamp_wrap) {
1311 uint16_t seq_no = start_seq_no;
1312 uint32_t timestamp = start_timestamp;
1313 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1314 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1315 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001316 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001317 double next_input_time_ms = 0.0;
1318 int16_t decoded[kBlockSize16kHz];
1319 int num_channels;
1320 int samples_per_channel;
1321 NetEqOutputType output_type;
1322 uint32_t receive_timestamp = 0;
1323
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001324 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001325 const int kSpeechDurationMs = 2000;
1326 int packets_inserted = 0;
1327 uint16_t last_seq_no;
1328 uint32_t last_timestamp;
1329 bool timestamp_wrapped = false;
1330 bool seq_no_wrapped = false;
1331 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1332 // Each turn in this for loop is 10 ms.
1333 while (next_input_time_ms <= t_ms) {
1334 // Insert one 30 ms speech frame.
1335 uint8_t payload[kPayloadBytes] = {0};
1336 WebRtcRTPHeader rtp_info;
1337 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1338 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1339 // This sequence number was not in the set to drop. Insert it.
1340 ASSERT_EQ(0,
1341 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1342 receive_timestamp));
1343 ++packets_inserted;
1344 }
1345 NetEqNetworkStatistics network_stats;
1346 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1347
1348 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1349 // packet size for first few packets. Therefore we refrain from checking
1350 // the criteria.
1351 if (packets_inserted > 4) {
1352 // Expect preferred and actual buffer size to be no more than 2 frames.
1353 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001354 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1355 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001356 }
1357 last_seq_no = seq_no;
1358 last_timestamp = timestamp;
1359
1360 ++seq_no;
1361 timestamp += kSamples;
1362 receive_timestamp += kSamples;
1363 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1364
1365 seq_no_wrapped |= seq_no < last_seq_no;
1366 timestamp_wrapped |= timestamp < last_timestamp;
1367 }
1368 // Pull out data once.
1369 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1370 &samples_per_channel, &num_channels,
1371 &output_type));
1372 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1373 ASSERT_EQ(1, num_channels);
1374
1375 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001376 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001377 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001378 }
1379 // Make sure we have actually tested wrap-around.
1380 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1381 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1382}
1383
1384TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1385 // Start with a sequence number that will soon wrap.
1386 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1387 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1388}
1389
1390TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1391 // Start with a sequence number that will soon wrap.
1392 std::set<uint16_t> drop_seq_numbers;
1393 drop_seq_numbers.insert(0xFFFF);
1394 drop_seq_numbers.insert(0x0);
1395 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1396}
1397
1398TEST_F(NetEqDecodingTest, TimestampWrap) {
1399 // Start with a timestamp that will soon wrap.
1400 std::set<uint16_t> drop_seq_numbers;
1401 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1402}
1403
1404TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1405 // Start with a timestamp and a sequence number that will wrap at the same
1406 // time.
1407 std::set<uint16_t> drop_seq_numbers;
1408 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1409}
1410
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001411void NetEqDecodingTest::DuplicateCng() {
1412 uint16_t seq_no = 0;
1413 uint32_t timestamp = 0;
1414 const int kFrameSizeMs = 10;
1415 const int kSampleRateKhz = 16;
1416 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001417 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001418
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001419 const int algorithmic_delay_samples = std::max(
1420 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001421 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001422 // correct.
1423 int out_len;
1424 int num_channels;
1425 NetEqOutputType type;
1426 uint8_t payload[kPayloadBytes] = {0};
1427 WebRtcRTPHeader rtp_info;
1428 for (int i = 0; i < 3; ++i) {
1429 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1430 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1431 ++seq_no;
1432 timestamp += kSamples;
1433
1434 // Pull audio once.
1435 ASSERT_EQ(0,
1436 neteq_->GetAudio(
1437 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1438 ASSERT_EQ(kBlockSize16kHz, out_len);
1439 }
1440 // Verify speech output.
1441 EXPECT_EQ(kOutputNormal, type);
1442
1443 // Insert same CNG packet twice.
1444 const int kCngPeriodMs = 100;
1445 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001446 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001447 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1448 // This is the first time this CNG packet is inserted.
1449 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1450
1451 // Pull audio once and make sure CNG is played.
1452 ASSERT_EQ(0,
1453 neteq_->GetAudio(
1454 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1455 ASSERT_EQ(kBlockSize16kHz, out_len);
1456 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001457 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001458
1459 // Insert the same CNG packet again. Note that at this point it is old, since
1460 // we have already decoded the first copy of it.
1461 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1462
1463 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1464 // we have already pulled out CNG once.
1465 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1466 ASSERT_EQ(0,
1467 neteq_->GetAudio(
1468 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1469 ASSERT_EQ(kBlockSize16kHz, out_len);
1470 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001471 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001472 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001473 }
1474
1475 // Insert speech again.
1476 ++seq_no;
1477 timestamp += kCngPeriodSamples;
1478 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1479 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1480
1481 // Pull audio once and verify that the output is speech again.
1482 ASSERT_EQ(0,
1483 neteq_->GetAudio(
1484 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1485 ASSERT_EQ(kBlockSize16kHz, out_len);
1486 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001487 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001488 PlayoutTimestamp());
1489}
1490
1491uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1492 uint32_t playout_timestamp = 0;
1493 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1494 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001495}
1496
1497TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001498
1499TEST_F(NetEqDecodingTest, CngFirst) {
1500 uint16_t seq_no = 0;
1501 uint32_t timestamp = 0;
1502 const int kFrameSizeMs = 10;
1503 const int kSampleRateKhz = 16;
1504 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1505 const int kPayloadBytes = kSamples * 2;
1506 const int kCngPeriodMs = 100;
1507 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1508 size_t payload_len;
1509
1510 uint8_t payload[kPayloadBytes] = {0};
1511 WebRtcRTPHeader rtp_info;
1512
1513 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1514 ASSERT_EQ(NetEq::kOK,
1515 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1516 ++seq_no;
1517 timestamp += kCngPeriodSamples;
1518
1519 // Pull audio once and make sure CNG is played.
1520 int out_len;
1521 int num_channels;
1522 NetEqOutputType type;
1523 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1524 &num_channels, &type));
1525 ASSERT_EQ(kBlockSize16kHz, out_len);
1526 EXPECT_EQ(kOutputCNG, type);
1527
1528 // Insert some speech packets.
1529 for (int i = 0; i < 3; ++i) {
1530 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1531 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1532 ++seq_no;
1533 timestamp += kSamples;
1534
1535 // Pull audio once.
1536 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1537 &num_channels, &type));
1538 ASSERT_EQ(kBlockSize16kHz, out_len);
1539 }
1540 // Verify speech output.
1541 EXPECT_EQ(kOutputNormal, type);
1542}
1543
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001544} // namespace webrtc