blob: 3bdaa69b4f1a231959a8d1ff8e5117d0d9a3f91f [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:211/*
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:2815#include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2116
pbos@webrtc.org3ecc1622014-03-07 15:23:3417#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2118#include <stdlib.h>
19#include <string.h> // memset
20
turaj@webrtc.org8d1cdaa2014-04-11 18:47:5521#include <algorithm>
turaj@webrtc.org78b41a02013-11-22 20:27:0722#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2123#include <string>
24#include <vector>
25
turaj@webrtc.orga6101d72013-10-01 22:01:0926#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:4027#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:5528#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:4429#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:3830#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
turaj@webrtc.orgff43c852013-09-25 00:07:2731#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2132#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:5433#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2134#include "webrtc/typedefs.h"
35
turaj@webrtc.orga6101d72013-10-01 22:01:0936DEFINE_bool(gen_ref, false, "Generate reference files.");
37
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2138namespace webrtc {
39
turaj@webrtc.org7b75ac62013-09-26 00:27:5640static 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:2154class 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:09140 // 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:53150 // audio inserted through expansion (in Q14).
minyue@webrtc.org2c1bcf22015-02-17 10:17:09151 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:21159 if (input_fp_) {
160 // Read from ref file.
minyue@webrtc.org2c1bcf22015-02-17 10:17:09161 size_t stat_size = sizeof(NetEqNetworkStatisticsOld);
162 NetEqNetworkStatisticsOld ref_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21163 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
164 // Compare
minyue@webrtc.org2c1bcf22015-02-17 10:17:09165 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:53171 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate);
minyue@webrtc.org2c1bcf22015-02-17 10:17:09172 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:53177 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21178 }
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:07187 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:21189 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:07204 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:21207 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:00210 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:07213 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00214 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21215 }
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:14226 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21227 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:38235 void Process(int* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49236 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:21240 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:14247 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21248
turaj@webrtc.org78b41a02013-11-22 20:27:07249 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:05253 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:52259 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28260
wu@webrtc.org94454b72014-06-05 20:34:08261 uint32_t PlayoutTimestamp();
262
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21263 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37264 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55265 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
266 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21267 unsigned int sim_clock_;
268 int16_t out_data_[kMaxBlockSize];
269 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55270 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21271};
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:14278const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21279const int NetEqDecodingTest::kInitSampleRateHz;
280
281NetEqDecodingTest::NetEqDecodingTest()
282 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37283 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21284 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55285 output_sample_rate_(kInitSampleRateHz),
286 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37287 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21288 memset(out_data_, 0, sizeof(out_data_));
289}
290
291void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37292 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55293 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:21296 ASSERT_TRUE(neteq_);
297 LoadDecoders();
298}
299
300void NetEqDecodingTest::TearDown() {
301 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21302}
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:54309#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21310 // Load iLBC.
311 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54312#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21313 // Load iSAC.
314 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32315#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21316 // Load iSAC SWB.
317 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24318 // Load iSAC FB.
319 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32320#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21321 // 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:38334 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21335}
336
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38337void NetEqDecodingTest::Process(int* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21338 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38339 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:21343 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38344 rtp_header, packet_->payload(),
345 packet_->payload_length_bytes(),
346 packet_->time_ms() * (output_sample_rate_ / 1000)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21347 }
348 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38349 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21350 }
351
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20352 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21353 NetEqOutputType type;
354 int num_channels;
355 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
356 &num_channels, &type));
357 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
358 (*out_len == kBlockSize16kHz) ||
359 (*out_len == kBlockSize32kHz));
360 output_sample_rate_ = *out_len / 10 * 1000;
361
362 // Increase time.
363 sim_clock_ += kTimeStepMs;
364}
365
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49366void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
367 const std::string& ref_file,
368 const std::string& stat_ref_file,
369 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21370 OpenInputFile(rtp_file);
371
372 std::string ref_out_file = "";
373 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09374 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21375 }
376 RefFiles ref_files(ref_file, ref_out_file);
377
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49378 std::string stat_out_file = "";
379 if (stat_ref_file.empty()) {
380 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
381 }
382 RefFiles network_stat_files(stat_ref_file, stat_out_file);
383
384 std::string rtcp_out_file = "";
385 if (rtcp_ref_file.empty()) {
386 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
387 }
388 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
389
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38390 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21391 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38392 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21393 std::ostringstream ss;
394 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
395 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55396 int out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38397 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21398 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21399
400 // Query the network statistics API once per second
401 if (sim_clock_ % 1000 == 0) {
402 // Process NetworkStatistics.
403 NetEqNetworkStatistics network_stats;
404 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00405 ASSERT_NO_FATAL_FAILURE(
406 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21407
408 // Process RTCPstat.
409 RtcpStatistics rtcp_stats;
410 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00411 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21412 }
413 }
414}
415
416void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
417 int timestamp,
418 WebRtcRTPHeader* rtp_info) {
419 rtp_info->header.sequenceNumber = frame_index;
420 rtp_info->header.timestamp = timestamp;
421 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
422 rtp_info->header.payloadType = 94; // PCM16b WB codec.
423 rtp_info->header.markerBit = 0;
424}
425
426void NetEqDecodingTest::PopulateCng(int frame_index,
427 int timestamp,
428 WebRtcRTPHeader* rtp_info,
429 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14430 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21431 rtp_info->header.sequenceNumber = frame_index;
432 rtp_info->header.timestamp = timestamp;
433 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
434 rtp_info->header.payloadType = 98; // WB CNG.
435 rtp_info->header.markerBit = 0;
436 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
437 *payload_len = 1; // Only noise level, no spectral parameters.
438}
439
henrikaa2c79402015-06-10 11:24:48440// TODO(henrika): add support for IOS for all tests in this file.
441// See https://code.google.com/p/webrtc/issues/detail?id=4752 for details.
442TEST_F(NetEqDecodingTest,
443 DISABLED_ON_IOS(DISABLED_ON_ANDROID(TestBitExactness))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28444 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51445 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43446 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
447 // are identical. The latter could have been removed, but if clients still
448 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28449 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09450 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30451#if defined(_MSC_VER) && (_MSC_VER >= 1700)
452 // For Visual Studio 2012 and later, we will have to use the generic reference
453 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28454 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09455 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30456#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28457 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09458 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30459#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28460 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09461 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49462
turaj@webrtc.orga6101d72013-10-01 22:01:09463 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49464 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09465 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49466 DecodeAndCompare(input_rtp_file,
467 input_ref_file,
468 network_stat_ref_file,
469 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09470 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21471}
472
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39473// Use fax mode to avoid time-scaling. This is to simplify the testing of
474// packet waiting times in the packet buffer.
475class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
476 protected:
477 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
478 config_.playout_mode = kPlayoutFax;
479 }
480};
481
482TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21483 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
484 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14485 const size_t kSamples = 10 * 16;
486 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21487 for (size_t i = 0; i < num_frames; ++i) {
488 uint16_t payload[kSamples] = {0};
489 WebRtcRTPHeader rtp_info;
490 rtp_info.header.sequenceNumber = i;
491 rtp_info.header.timestamp = i * kSamples;
492 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
493 rtp_info.header.payloadType = 94; // PCM16b WB codec.
494 rtp_info.header.markerBit = 0;
495 ASSERT_EQ(0, neteq_->InsertPacket(
496 rtp_info,
497 reinterpret_cast<uint8_t*>(payload),
498 kPayloadBytes, 0));
499 }
500 // Pull out all data.
501 for (size_t i = 0; i < num_frames; ++i) {
502 int out_len;
503 int num_channels;
504 NetEqOutputType type;
505 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
506 &num_channels, &type));
507 ASSERT_EQ(kBlockSize16kHz, out_len);
508 }
509
510 std::vector<int> waiting_times;
511 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21512 EXPECT_EQ(num_frames, waiting_times.size());
513 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
514 // spacing (per definition), we expect the delay to increase with 10 ms for
515 // each packet.
516 for (size_t i = 0; i < waiting_times.size(); ++i) {
517 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
518 }
519
520 // Check statistics again and make sure it's been reset.
521 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55522 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21523 EXPECT_EQ(0, len);
524
525 // Process > 100 frames, and make sure that that we get statistics
526 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
527 num_frames = 110;
528 for (size_t i = 0; i < num_frames; ++i) {
529 uint16_t payload[kSamples] = {0};
530 WebRtcRTPHeader rtp_info;
531 rtp_info.header.sequenceNumber = i;
532 rtp_info.header.timestamp = i * kSamples;
533 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
534 rtp_info.header.payloadType = 94; // PCM16b WB codec.
535 rtp_info.header.markerBit = 0;
536 ASSERT_EQ(0, neteq_->InsertPacket(
537 rtp_info,
538 reinterpret_cast<uint8_t*>(payload),
539 kPayloadBytes, 0));
540 int out_len;
541 int num_channels;
542 NetEqOutputType type;
543 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
544 &num_channels, &type));
545 ASSERT_EQ(kBlockSize16kHz, out_len);
546 }
547
548 neteq_->WaitingTimes(&waiting_times);
549 EXPECT_EQ(100u, waiting_times.size());
550}
551
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00552TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21553 const int kNumFrames = 3000; // Needed for convergence.
554 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14555 const size_t kSamples = 10 * 16;
556 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21557 while (frame_index < kNumFrames) {
558 // Insert one packet each time, except every 10th time where we insert two
559 // packets at once. This will create a negative clock-drift of approx. 10%.
560 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
561 for (int n = 0; n < num_packets; ++n) {
562 uint8_t payload[kPayloadBytes] = {0};
563 WebRtcRTPHeader rtp_info;
564 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
565 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
566 ++frame_index;
567 }
568
569 // Pull out data once.
570 int out_len;
571 int num_channels;
572 NetEqOutputType type;
573 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
574 &num_channels, &type));
575 ASSERT_EQ(kBlockSize16kHz, out_len);
576 }
577
578 NetEqNetworkStatistics network_stats;
579 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
580 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
581}
582
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00583TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21584 const int kNumFrames = 5000; // Needed for convergence.
585 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14586 const size_t kSamples = 10 * 16;
587 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21588 for (int i = 0; i < kNumFrames; ++i) {
589 // Insert one packet each time, except every 10th time where we don't insert
590 // any packet. This will create a positive clock-drift of approx. 11%.
591 int num_packets = (i % 10 == 9 ? 0 : 1);
592 for (int n = 0; n < num_packets; ++n) {
593 uint8_t payload[kPayloadBytes] = {0};
594 WebRtcRTPHeader rtp_info;
595 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
596 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
597 ++frame_index;
598 }
599
600 // Pull out data once.
601 int out_len;
602 int num_channels;
603 NetEqOutputType type;
604 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
605 &num_channels, &type));
606 ASSERT_EQ(kBlockSize16kHz, out_len);
607 }
608
609 NetEqNetworkStatistics network_stats;
610 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
611 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
612}
613
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05614void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
615 double network_freeze_ms,
616 bool pull_audio_during_freeze,
617 int delay_tolerance_ms,
618 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21619 uint16_t seq_no = 0;
620 uint32_t timestamp = 0;
621 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14622 const size_t kSamples = kFrameSizeMs * 16;
623 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21624 double next_input_time_ms = 0.0;
625 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05626 int out_len;
627 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21628 NetEqOutputType type;
629
630 // Insert speech for 5 seconds.
631 const int kSpeechDurationMs = 5000;
632 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
633 // Each turn in this for loop is 10 ms.
634 while (next_input_time_ms <= t_ms) {
635 // Insert one 30 ms speech frame.
636 uint8_t payload[kPayloadBytes] = {0};
637 WebRtcRTPHeader rtp_info;
638 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
639 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
640 ++seq_no;
641 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28642 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21643 }
644 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21645 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
646 &num_channels, &type));
647 ASSERT_EQ(kBlockSize16kHz, out_len);
648 }
649
650 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08651 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21652
653 // Insert CNG for 1 minute (= 60000 ms).
654 const int kCngPeriodMs = 100;
655 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
656 const int kCngDurationMs = 60000;
657 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
658 // Each turn in this for loop is 10 ms.
659 while (next_input_time_ms <= t_ms) {
660 // Insert one CNG frame each 100 ms.
661 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14662 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21663 WebRtcRTPHeader rtp_info;
664 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
665 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
666 ++seq_no;
667 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28668 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21669 }
670 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21671 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
672 &num_channels, &type));
673 ASSERT_EQ(kBlockSize16kHz, out_len);
674 }
675
676 EXPECT_EQ(kOutputCNG, type);
677
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05678 if (network_freeze_ms > 0) {
679 // First keep pulling audio for |network_freeze_ms| without inserting
680 // any data, then insert CNG data corresponding to |network_freeze_ms|
681 // without pulling any output audio.
682 const double loop_end_time = t_ms + network_freeze_ms;
683 for (; t_ms < loop_end_time; t_ms += 10) {
684 // Pull out data once.
685 ASSERT_EQ(0,
686 neteq_->GetAudio(
687 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
688 ASSERT_EQ(kBlockSize16kHz, out_len);
689 EXPECT_EQ(kOutputCNG, type);
690 }
691 bool pull_once = pull_audio_during_freeze;
692 // If |pull_once| is true, GetAudio will be called once half-way through
693 // the network recovery period.
694 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
695 while (next_input_time_ms <= t_ms) {
696 if (pull_once && next_input_time_ms >= pull_time_ms) {
697 pull_once = false;
698 // Pull out data once.
699 ASSERT_EQ(
700 0,
701 neteq_->GetAudio(
702 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
703 ASSERT_EQ(kBlockSize16kHz, out_len);
704 EXPECT_EQ(kOutputCNG, type);
705 t_ms += 10;
706 }
707 // Insert one CNG frame each 100 ms.
708 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14709 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05710 WebRtcRTPHeader rtp_info;
711 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
712 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
713 ++seq_no;
714 timestamp += kCngPeriodSamples;
715 next_input_time_ms += kCngPeriodMs * drift_factor;
716 }
717 }
718
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21719 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05720 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21721 while (type != kOutputNormal) {
722 // Each turn in this for loop is 10 ms.
723 while (next_input_time_ms <= t_ms) {
724 // Insert one 30 ms speech frame.
725 uint8_t payload[kPayloadBytes] = {0};
726 WebRtcRTPHeader rtp_info;
727 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
728 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
729 ++seq_no;
730 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05731 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21732 }
733 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21734 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
735 &num_channels, &type));
736 ASSERT_EQ(kBlockSize16kHz, out_len);
737 // Increase clock.
738 t_ms += 10;
739 }
740
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05741 // Check that the speech starts again within reasonable time.
742 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
743 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08744 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21745 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05746 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
747 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21748}
749
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00750TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28751 // Apply a clock drift of -25 ms / s (sender faster than receiver).
752 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05753 const double kNetworkFreezeTimeMs = 0.0;
754 const bool kGetAudioDuringFreezeRecovery = false;
755 const int kDelayToleranceMs = 20;
756 const int kMaxTimeToSpeechMs = 100;
757 LongCngWithClockDrift(kDriftFactor,
758 kNetworkFreezeTimeMs,
759 kGetAudioDuringFreezeRecovery,
760 kDelayToleranceMs,
761 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28762}
763
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00764TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28765 // Apply a clock drift of +25 ms / s (sender slower than receiver).
766 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05767 const double kNetworkFreezeTimeMs = 0.0;
768 const bool kGetAudioDuringFreezeRecovery = false;
769 const int kDelayToleranceMs = 20;
770 const int kMaxTimeToSpeechMs = 100;
771 LongCngWithClockDrift(kDriftFactor,
772 kNetworkFreezeTimeMs,
773 kGetAudioDuringFreezeRecovery,
774 kDelayToleranceMs,
775 kMaxTimeToSpeechMs);
776}
777
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00778TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05779 // Apply a clock drift of -25 ms / s (sender faster than receiver).
780 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
781 const double kNetworkFreezeTimeMs = 5000.0;
782 const bool kGetAudioDuringFreezeRecovery = false;
783 const int kDelayToleranceMs = 50;
784 const int kMaxTimeToSpeechMs = 200;
785 LongCngWithClockDrift(kDriftFactor,
786 kNetworkFreezeTimeMs,
787 kGetAudioDuringFreezeRecovery,
788 kDelayToleranceMs,
789 kMaxTimeToSpeechMs);
790}
791
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00792TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05793 // Apply a clock drift of +25 ms / s (sender slower than receiver).
794 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
795 const double kNetworkFreezeTimeMs = 5000.0;
796 const bool kGetAudioDuringFreezeRecovery = false;
797 const int kDelayToleranceMs = 20;
798 const int kMaxTimeToSpeechMs = 100;
799 LongCngWithClockDrift(kDriftFactor,
800 kNetworkFreezeTimeMs,
801 kGetAudioDuringFreezeRecovery,
802 kDelayToleranceMs,
803 kMaxTimeToSpeechMs);
804}
805
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00806TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05807 // Apply a clock drift of +25 ms / s (sender slower than receiver).
808 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
809 const double kNetworkFreezeTimeMs = 5000.0;
810 const bool kGetAudioDuringFreezeRecovery = true;
811 const int kDelayToleranceMs = 20;
812 const int kMaxTimeToSpeechMs = 100;
813 LongCngWithClockDrift(kDriftFactor,
814 kNetworkFreezeTimeMs,
815 kGetAudioDuringFreezeRecovery,
816 kDelayToleranceMs,
817 kMaxTimeToSpeechMs);
818}
819
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00820TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05821 const double kDriftFactor = 1.0; // No drift.
822 const double kNetworkFreezeTimeMs = 0.0;
823 const bool kGetAudioDuringFreezeRecovery = false;
824 const int kDelayToleranceMs = 10;
825 const int kMaxTimeToSpeechMs = 50;
826 LongCngWithClockDrift(kDriftFactor,
827 kNetworkFreezeTimeMs,
828 kGetAudioDuringFreezeRecovery,
829 kDelayToleranceMs,
830 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28831}
832
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00833TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14834 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21835 uint8_t payload[kPayloadBytes] = {0};
836 WebRtcRTPHeader rtp_info;
837 PopulateRtpInfo(0, 0, &rtp_info);
838 rtp_info.header.payloadType = 1; // Not registered as a decoder.
839 EXPECT_EQ(NetEq::kFail,
840 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
841 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
842}
843
henrike@webrtc.orga950300b2013-07-08 18:53:54844TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14845 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21846 uint8_t payload[kPayloadBytes] = {0};
847 WebRtcRTPHeader rtp_info;
848 PopulateRtpInfo(0, 0, &rtp_info);
849 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
850 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
851 NetEqOutputType type;
852 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
853 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14854 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21855 out_data_[i] = 1;
856 }
857 int num_channels;
858 int samples_per_channel;
859 EXPECT_EQ(NetEq::kFail,
860 neteq_->GetAudio(kMaxBlockSize, out_data_,
861 &samples_per_channel, &num_channels, &type));
862 // Verify that there is a decoder error to check.
863 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
864 // Code 6730 is an iSAC error code.
865 EXPECT_EQ(6730, neteq_->LastDecoderError());
866 // Verify that the first 160 samples are set to 0, and that the remaining
867 // samples are left unmodified.
868 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
869 for (int i = 0; i < kExpectedOutputLength; ++i) {
870 std::ostringstream ss;
871 ss << "i = " << i;
872 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
873 EXPECT_EQ(0, out_data_[i]);
874 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14875 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21876 std::ostringstream ss;
877 ss << "i = " << i;
878 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
879 EXPECT_EQ(1, out_data_[i]);
880 }
881}
882
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00883TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21884 NetEqOutputType type;
885 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
886 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14887 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21888 out_data_[i] = 1;
889 }
890 int num_channels;
891 int samples_per_channel;
892 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
893 &samples_per_channel,
894 &num_channels, &type));
895 // Verify that the first block of samples is set to 0.
896 static const int kExpectedOutputLength =
897 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
898 for (int i = 0; i < kExpectedOutputLength; ++i) {
899 std::ostringstream ss;
900 ss << "i = " << i;
901 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
902 EXPECT_EQ(0, out_data_[i]);
903 }
904}
turaj@webrtc.orgff43c852013-09-25 00:07:27905
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44906class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37907 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44908 virtual void TestCondition(double sum_squared_noise,
909 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27910
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44911 void CheckBgn(int sampling_rate_hz) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14912 int16_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37913 uint8_t payload_type = 0xFF; // Invalid.
914 if (sampling_rate_hz == 8000) {
915 expected_samples_per_channel = kBlockSize8kHz;
916 payload_type = 93; // PCM 16, 8 kHz.
917 } else if (sampling_rate_hz == 16000) {
918 expected_samples_per_channel = kBlockSize16kHz;
919 payload_type = 94; // PCM 16, 16 kHZ.
920 } else if (sampling_rate_hz == 32000) {
921 expected_samples_per_channel = kBlockSize32kHz;
922 payload_type = 95; // PCM 16, 32 kHz.
923 } else {
924 ASSERT_TRUE(false); // Unsupported test case.
925 }
turaj@webrtc.orgff43c852013-09-25 00:07:27926
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37927 NetEqOutputType type;
928 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44929 test::AudioLoop input;
930 // We are using the same 32 kHz input file for all tests, regardless of
931 // |sampling_rate_hz|. The output may sound weird, but the test is still
932 // valid.
933 ASSERT_TRUE(input.Init(
934 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
935 10 * sampling_rate_hz, // Max 10 seconds loop length.
pkasting@chromium.org4591fbd2014-11-20 22:28:14936 static_cast<size_t>(expected_samples_per_channel)));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37937
938 // Payload of 10 ms of PCM16 32 kHz.
939 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37940 WebRtcRTPHeader rtp_info;
941 PopulateRtpInfo(0, 0, &rtp_info);
942 rtp_info.header.payloadType = payload_type;
943
944 int number_channels = 0;
945 int samples_per_channel = 0;
946
947 uint32_t receive_timestamp = 0;
948 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg@webrtc.org648f5d62015-02-10 09:18:28949 int16_t enc_len_bytes = WebRtcPcm16b_Encode(
950 input.GetNextBlock(), expected_samples_per_channel, payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44951 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
952
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37953 number_channels = 0;
954 samples_per_channel = 0;
955 ASSERT_EQ(0,
pkasting@chromium.org4591fbd2014-11-20 22:28:14956 neteq_->InsertPacket(rtp_info, payload,
957 static_cast<size_t>(enc_len_bytes),
958 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37959 ASSERT_EQ(0,
960 neteq_->GetAudio(kBlockSize32kHz,
961 output,
962 &samples_per_channel,
963 &number_channels,
964 &type));
965 ASSERT_EQ(1, number_channels);
966 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
967 ASSERT_EQ(kOutputNormal, type);
968
969 // Next packet.
970 rtp_info.header.timestamp += expected_samples_per_channel;
971 rtp_info.header.sequenceNumber++;
972 receive_timestamp += expected_samples_per_channel;
973 }
974
975 number_channels = 0;
976 samples_per_channel = 0;
977
978 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
979 // one frame without checking speech-type. This is the first frame pulled
980 // without inserting any packet, and might not be labeled as PLC.
981 ASSERT_EQ(0,
982 neteq_->GetAudio(kBlockSize32kHz,
983 output,
984 &samples_per_channel,
985 &number_channels,
986 &type));
987 ASSERT_EQ(1, number_channels);
988 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
989
990 // To be able to test the fading of background noise we need at lease to
991 // pull 611 frames.
992 const int kFadingThreshold = 611;
993
994 // Test several CNG-to-PLC packet for the expected behavior. The number 20
995 // is arbitrary, but sufficiently large to test enough number of frames.
996 const int kNumPlcToCngTestFrames = 20;
997 bool plc_to_cng = false;
998 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
999 number_channels = 0;
1000 samples_per_channel = 0;
1001 memset(output, 1, sizeof(output)); // Set to non-zero.
1002 ASSERT_EQ(0,
1003 neteq_->GetAudio(kBlockSize32kHz,
1004 output,
1005 &samples_per_channel,
1006 &number_channels,
1007 &type));
1008 ASSERT_EQ(1, number_channels);
1009 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1010 if (type == kOutputPLCtoCNG) {
1011 plc_to_cng = true;
1012 double sum_squared = 0;
1013 for (int k = 0; k < number_channels * samples_per_channel; ++k)
1014 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:441015 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:371016 } else {
1017 EXPECT_EQ(kOutputPLC, type);
1018 }
1019 }
1020 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1021 }
1022};
1023
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:441024class NetEqBgnTestOn : public NetEqBgnTest {
1025 protected:
1026 NetEqBgnTestOn() : NetEqBgnTest() {
1027 config_.background_noise_mode = NetEq::kBgnOn;
1028 }
1029
1030 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1031 EXPECT_NE(0, sum_squared_noise);
1032 }
1033};
1034
1035class NetEqBgnTestOff : public NetEqBgnTest {
1036 protected:
1037 NetEqBgnTestOff() : NetEqBgnTest() {
1038 config_.background_noise_mode = NetEq::kBgnOff;
1039 }
1040
1041 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1042 EXPECT_EQ(0, sum_squared_noise);
1043 }
1044};
1045
1046class NetEqBgnTestFade : public NetEqBgnTest {
1047 protected:
1048 NetEqBgnTestFade() : NetEqBgnTest() {
1049 config_.background_noise_mode = NetEq::kBgnFade;
1050 }
1051
1052 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1053 if (should_be_faded)
1054 EXPECT_EQ(0, sum_squared_noise);
1055 }
1056};
1057
henrikaa2c79402015-06-10 11:24:481058TEST_F(NetEqBgnTestOn, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:441059 CheckBgn(8000);
1060 CheckBgn(16000);
1061 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:271062}
turaj@webrtc.org7b75ac62013-09-26 00:27:561063
henrikaa2c79402015-06-10 11:24:481064TEST_F(NetEqBgnTestOff, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:441065 CheckBgn(8000);
1066 CheckBgn(16000);
1067 CheckBgn(32000);
1068}
1069
henrikaa2c79402015-06-10 11:24:481070TEST_F(NetEqBgnTestFade, DISABLED_ON_IOS(RunTest)) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:441071 CheckBgn(8000);
1072 CheckBgn(16000);
1073 CheckBgn(32000);
1074}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:371075
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:001076TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561077 WebRtcRTPHeader rtp_info;
1078 uint32_t receive_timestamp = 0;
1079 // For the readability use the following payloads instead of the defaults of
1080 // this test.
1081 uint8_t kPcm16WbPayloadType = 1;
1082 uint8_t kCngNbPayloadType = 2;
1083 uint8_t kCngWbPayloadType = 3;
1084 uint8_t kCngSwb32PayloadType = 4;
1085 uint8_t kCngSwb48PayloadType = 5;
1086 uint8_t kAvtPayloadType = 6;
1087 uint8_t kRedPayloadType = 7;
1088 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1089
1090 // Register decoders.
1091 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1092 kPcm16WbPayloadType));
1093 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1094 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1095 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1096 kCngSwb32PayloadType));
1097 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1098 kCngSwb48PayloadType));
1099 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1100 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1101 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1102
1103 PopulateRtpInfo(0, 0, &rtp_info);
1104 rtp_info.header.payloadType = kPcm16WbPayloadType;
1105
1106 // The first packet injected cannot be sync-packet.
1107 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1108
1109 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:141110 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:561111 uint8_t payload[kPayloadBytes] = {0};
1112 ASSERT_EQ(0, neteq_->InsertPacket(
1113 rtp_info, payload, kPayloadBytes, receive_timestamp));
1114
1115 // Next packet. Last packet contained 10 ms audio.
1116 rtp_info.header.sequenceNumber++;
1117 rtp_info.header.timestamp += kBlockSize16kHz;
1118 receive_timestamp += kBlockSize16kHz;
1119
1120 // Unacceptable payload types CNG, AVT (DTMF), RED.
1121 rtp_info.header.payloadType = kCngNbPayloadType;
1122 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1123
1124 rtp_info.header.payloadType = kCngWbPayloadType;
1125 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1126
1127 rtp_info.header.payloadType = kCngSwb32PayloadType;
1128 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1129
1130 rtp_info.header.payloadType = kCngSwb48PayloadType;
1131 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1132
1133 rtp_info.header.payloadType = kAvtPayloadType;
1134 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1135
1136 rtp_info.header.payloadType = kRedPayloadType;
1137 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1138
1139 // Change of codec cannot be initiated with a sync packet.
1140 rtp_info.header.payloadType = kIsacPayloadType;
1141 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1142
1143 // Change of SSRC is not allowed with a sync packet.
1144 rtp_info.header.payloadType = kPcm16WbPayloadType;
1145 ++rtp_info.header.ssrc;
1146 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1147
1148 --rtp_info.header.ssrc;
1149 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1150}
1151
1152// First insert several noise like packets, then sync-packets. Decoding all
1153// packets should not produce error, statistics should not show any packet loss
1154// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551155// TODO(turajs) we will have a better test if we have a referece NetEq, and
1156// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1157// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:001158TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561159 WebRtcRTPHeader rtp_info;
1160 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:141161 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:561162 uint8_t payload[kPayloadBytes];
1163 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551164 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:141165 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561166 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1167 }
1168 // Insert some packets which decode to noise. We are not interested in
1169 // actual decoded values.
1170 NetEqOutputType output_type;
1171 int num_channels;
1172 int samples_per_channel;
1173 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:561174 for (int n = 0; n < 100; ++n) {
1175 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1176 receive_timestamp));
1177 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1178 &samples_per_channel, &num_channels,
1179 &output_type));
1180 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1181 ASSERT_EQ(1, num_channels);
1182
turaj@webrtc.org7b75ac62013-09-26 00:27:561183 rtp_info.header.sequenceNumber++;
1184 rtp_info.header.timestamp += kBlockSize16kHz;
1185 receive_timestamp += kBlockSize16kHz;
1186 }
1187 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551188
1189 // Make sure sufficient number of sync packets are inserted that we can
1190 // conduct a test.
1191 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:561192 // Insert sync-packets, the decoded sequence should be all-zero.
1193 for (int n = 0; n < kNumSyncPackets; ++n) {
1194 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1195 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1196 &samples_per_channel, &num_channels,
1197 &output_type));
1198 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1199 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551200 if (n > algorithmic_frame_delay) {
1201 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1202 }
turaj@webrtc.org7b75ac62013-09-26 00:27:561203 rtp_info.header.sequenceNumber++;
1204 rtp_info.header.timestamp += kBlockSize16kHz;
1205 receive_timestamp += kBlockSize16kHz;
1206 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551207
1208 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:561209 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551210 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1211 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1212 receive_timestamp));
1213 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1214 &samples_per_channel, &num_channels,
1215 &output_type));
1216 if (n >= algorithmic_frame_delay + 1) {
1217 // Expect that this frame contain samples from regular RTP.
1218 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1219 }
1220 rtp_info.header.sequenceNumber++;
1221 rtp_info.header.timestamp += kBlockSize16kHz;
1222 receive_timestamp += kBlockSize16kHz;
1223 }
turaj@webrtc.org7b75ac62013-09-26 00:27:561224 NetEqNetworkStatistics network_stats;
1225 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1226 // Expecting a "clean" network.
1227 EXPECT_EQ(0, network_stats.packet_loss_rate);
1228 EXPECT_EQ(0, network_stats.expand_rate);
1229 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551230 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:561231}
1232
1233// Test if the size of the packet buffer reported correctly when containing
1234// sync packets. Also, test if network packets override sync packets. That is to
1235// prefer decoding a network packet to a sync packet, if both have same sequence
1236// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:001237TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561238 WebRtcRTPHeader rtp_info;
1239 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:141240 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:561241 uint8_t payload[kPayloadBytes];
1242 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:141243 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561244 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1245 }
1246 // Insert some packets which decode to noise. We are not interested in
1247 // actual decoded values.
1248 NetEqOutputType output_type;
1249 int num_channels;
1250 int samples_per_channel;
1251 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551252 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1253 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:561254 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1255 receive_timestamp));
1256 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1257 &samples_per_channel, &num_channels,
1258 &output_type));
1259 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1260 ASSERT_EQ(1, num_channels);
1261 rtp_info.header.sequenceNumber++;
1262 rtp_info.header.timestamp += kBlockSize16kHz;
1263 receive_timestamp += kBlockSize16kHz;
1264 }
1265 const int kNumSyncPackets = 10;
1266
1267 WebRtcRTPHeader first_sync_packet_rtp_info;
1268 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1269
1270 // Insert sync-packets, but no decoding.
1271 for (int n = 0; n < kNumSyncPackets; ++n) {
1272 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1273 rtp_info.header.sequenceNumber++;
1274 rtp_info.header.timestamp += kBlockSize16kHz;
1275 receive_timestamp += kBlockSize16kHz;
1276 }
1277 NetEqNetworkStatistics network_stats;
1278 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551279 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1280 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:561281
1282 // Rewind |rtp_info| to that of the first sync packet.
1283 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1284
1285 // Insert.
1286 for (int n = 0; n < kNumSyncPackets; ++n) {
1287 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1288 receive_timestamp));
1289 rtp_info.header.sequenceNumber++;
1290 rtp_info.header.timestamp += kBlockSize16kHz;
1291 receive_timestamp += kBlockSize16kHz;
1292 }
1293
1294 // Decode.
1295 for (int n = 0; n < kNumSyncPackets; ++n) {
1296 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1297 &samples_per_channel, &num_channels,
1298 &output_type));
1299 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1300 ASSERT_EQ(1, num_channels);
1301 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1302 }
1303}
1304
turaj@webrtc.org78b41a02013-11-22 20:27:071305void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1306 uint32_t start_timestamp,
1307 const std::set<uint16_t>& drop_seq_numbers,
1308 bool expect_seq_no_wrap,
1309 bool expect_timestamp_wrap) {
1310 uint16_t seq_no = start_seq_no;
1311 uint32_t timestamp = start_timestamp;
1312 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1313 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1314 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:141315 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:071316 double next_input_time_ms = 0.0;
1317 int16_t decoded[kBlockSize16kHz];
1318 int num_channels;
1319 int samples_per_channel;
1320 NetEqOutputType output_type;
1321 uint32_t receive_timestamp = 0;
1322
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521323 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:071324 const int kSpeechDurationMs = 2000;
1325 int packets_inserted = 0;
1326 uint16_t last_seq_no;
1327 uint32_t last_timestamp;
1328 bool timestamp_wrapped = false;
1329 bool seq_no_wrapped = false;
1330 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1331 // Each turn in this for loop is 10 ms.
1332 while (next_input_time_ms <= t_ms) {
1333 // Insert one 30 ms speech frame.
1334 uint8_t payload[kPayloadBytes] = {0};
1335 WebRtcRTPHeader rtp_info;
1336 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1337 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1338 // This sequence number was not in the set to drop. Insert it.
1339 ASSERT_EQ(0,
1340 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1341 receive_timestamp));
1342 ++packets_inserted;
1343 }
1344 NetEqNetworkStatistics network_stats;
1345 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1346
1347 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1348 // packet size for first few packets. Therefore we refrain from checking
1349 // the criteria.
1350 if (packets_inserted > 4) {
1351 // Expect preferred and actual buffer size to be no more than 2 frames.
1352 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551353 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1354 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:071355 }
1356 last_seq_no = seq_no;
1357 last_timestamp = timestamp;
1358
1359 ++seq_no;
1360 timestamp += kSamples;
1361 receive_timestamp += kSamples;
1362 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1363
1364 seq_no_wrapped |= seq_no < last_seq_no;
1365 timestamp_wrapped |= timestamp < last_timestamp;
1366 }
1367 // Pull out data once.
1368 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1369 &samples_per_channel, &num_channels,
1370 &output_type));
1371 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1372 ASSERT_EQ(1, num_channels);
1373
1374 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:081375 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:071376 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:071377 }
1378 // Make sure we have actually tested wrap-around.
1379 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1380 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1381}
1382
1383TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1384 // Start with a sequence number that will soon wrap.
1385 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1386 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1387}
1388
1389TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1390 // Start with a sequence number that will soon wrap.
1391 std::set<uint16_t> drop_seq_numbers;
1392 drop_seq_numbers.insert(0xFFFF);
1393 drop_seq_numbers.insert(0x0);
1394 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1395}
1396
1397TEST_F(NetEqDecodingTest, TimestampWrap) {
1398 // Start with a timestamp that will soon wrap.
1399 std::set<uint16_t> drop_seq_numbers;
1400 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1401}
1402
1403TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1404 // Start with a timestamp and a sequence number that will wrap at the same
1405 // time.
1406 std::set<uint16_t> drop_seq_numbers;
1407 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1408}
1409
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521410void NetEqDecodingTest::DuplicateCng() {
1411 uint16_t seq_no = 0;
1412 uint32_t timestamp = 0;
1413 const int kFrameSizeMs = 10;
1414 const int kSampleRateKhz = 16;
1415 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:141416 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521417
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551418 const int algorithmic_delay_samples = std::max(
1419 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:421420 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521421 // correct.
1422 int out_len;
1423 int num_channels;
1424 NetEqOutputType type;
1425 uint8_t payload[kPayloadBytes] = {0};
1426 WebRtcRTPHeader rtp_info;
1427 for (int i = 0; i < 3; ++i) {
1428 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1429 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1430 ++seq_no;
1431 timestamp += kSamples;
1432
1433 // Pull audio once.
1434 ASSERT_EQ(0,
1435 neteq_->GetAudio(
1436 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1437 ASSERT_EQ(kBlockSize16kHz, out_len);
1438 }
1439 // Verify speech output.
1440 EXPECT_EQ(kOutputNormal, type);
1441
1442 // Insert same CNG packet twice.
1443 const int kCngPeriodMs = 100;
1444 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:141445 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521446 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1447 // This is the first time this CNG packet is inserted.
1448 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1449
1450 // Pull audio once and make sure CNG is played.
1451 ASSERT_EQ(0,
1452 neteq_->GetAudio(
1453 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1454 ASSERT_EQ(kBlockSize16kHz, out_len);
1455 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:081456 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521457
1458 // Insert the same CNG packet again. Note that at this point it is old, since
1459 // we have already decoded the first copy of it.
1460 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1461
1462 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1463 // we have already pulled out CNG once.
1464 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1465 ASSERT_EQ(0,
1466 neteq_->GetAudio(
1467 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1468 ASSERT_EQ(kBlockSize16kHz, out_len);
1469 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551470 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:081471 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521472 }
1473
1474 // Insert speech again.
1475 ++seq_no;
1476 timestamp += kCngPeriodSamples;
1477 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1478 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1479
1480 // Pull audio once and verify that the output is speech again.
1481 ASSERT_EQ(0,
1482 neteq_->GetAudio(
1483 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1484 ASSERT_EQ(kBlockSize16kHz, out_len);
1485 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:551486 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:081487 PlayoutTimestamp());
1488}
1489
1490uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1491 uint32_t playout_timestamp = 0;
1492 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1493 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:521494}
1495
1496TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:421497
1498TEST_F(NetEqDecodingTest, CngFirst) {
1499 uint16_t seq_no = 0;
1500 uint32_t timestamp = 0;
1501 const int kFrameSizeMs = 10;
1502 const int kSampleRateKhz = 16;
1503 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1504 const int kPayloadBytes = kSamples * 2;
1505 const int kCngPeriodMs = 100;
1506 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1507 size_t payload_len;
1508
1509 uint8_t payload[kPayloadBytes] = {0};
1510 WebRtcRTPHeader rtp_info;
1511
1512 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1513 ASSERT_EQ(NetEq::kOK,
1514 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1515 ++seq_no;
1516 timestamp += kCngPeriodSamples;
1517
1518 // Pull audio once and make sure CNG is played.
1519 int out_len;
1520 int num_channels;
1521 NetEqOutputType type;
1522 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1523 &num_channels, &type));
1524 ASSERT_EQ(kBlockSize16kHz, out_len);
1525 EXPECT_EQ(kOutputCNG, type);
1526
1527 // Insert some speech packets.
1528 for (int i = 0; i < 3; ++i) {
1529 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1530 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1531 ++seq_no;
1532 timestamp += kSamples;
1533
1534 // Pull audio once.
1535 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1536 &num_channels, &type));
1537 ASSERT_EQ(kBlockSize16kHz, out_len);
1538 }
1539 // Verify speech output.
1540 EXPECT_EQ(kOutputNormal, type);
1541}
1542
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:551543} // namespace webrtc