blob: 420e7bf481397dc193733e971827dd05f92d5a0b [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"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000028#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000029#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000030#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000031#include "webrtc/system_wrappers/interface/scoped_ptr.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
150 // speech inserted through expansion (in Q14).
151 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);
171 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate);
172 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate);
173 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm);
174 ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples);
175 ASSERT_EQ(stats.secondary_decoded_rate, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176 }
177}
178
179void RefFiles::WriteToFile(const RtcpStatistics& stats) {
180 if (output_fp_) {
181 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
182 output_fp_));
183 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
184 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000185 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
186 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 output_fp_));
188 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
189 output_fp_));
190 }
191}
192
193void RefFiles::ReadFromFileAndCompare(
194 const RtcpStatistics& stats) {
195 if (input_fp_) {
196 // Read from ref file.
197 RtcpStatistics ref_stats;
198 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
199 sizeof(ref_stats.fraction_lost), 1, input_fp_));
200 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
201 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000202 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
203 sizeof(ref_stats.extended_max_sequence_number), 1,
204 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
206 input_fp_));
207 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000208 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
209 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
210 ASSERT_EQ(ref_stats.extended_max_sequence_number,
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000211 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000212 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 }
214}
215
216class NetEqDecodingTest : public ::testing::Test {
217 protected:
218 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
219 // constants below can be changed.
220 static const int kTimeStepMs = 10;
221 static const int kBlockSize8kHz = kTimeStepMs * 8;
222 static const int kBlockSize16kHz = kTimeStepMs * 16;
223 static const int kBlockSize32kHz = kTimeStepMs * 32;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000224 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225 static const int kInitSampleRateHz = 8000;
226
227 NetEqDecodingTest();
228 virtual void SetUp();
229 virtual void TearDown();
230 void SelectDecoders(NetEqDecoder* used_codec);
231 void LoadDecoders();
232 void OpenInputFile(const std::string &rtp_file);
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000233 void Process(int* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000234 void DecodeAndCompare(const std::string& rtp_file,
235 const std::string& ref_file,
236 const std::string& stat_ref_file,
237 const std::string& rtcp_ref_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000238 static void PopulateRtpInfo(int frame_index,
239 int timestamp,
240 WebRtcRTPHeader* rtp_info);
241 static void PopulateCng(int frame_index,
242 int timestamp,
243 WebRtcRTPHeader* rtp_info,
244 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000245 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000247 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
248 const std::set<uint16_t>& drop_seq_numbers,
249 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
250
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000251 void LongCngWithClockDrift(double drift_factor,
252 double network_freeze_ms,
253 bool pull_audio_during_freeze,
254 int delay_tolerance_ms,
255 int max_time_to_speech_ms);
256
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000257 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000258
wu@webrtc.org94454b72014-06-05 20:34:08 +0000259 uint32_t PlayoutTimestamp();
260
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000262 NetEq::Config config_;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000263 scoped_ptr<test::RtpFileSource> rtp_source_;
264 scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 unsigned int sim_clock_;
266 int16_t out_data_[kMaxBlockSize];
267 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000268 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269};
270
271// Allocating the static const so that it can be passed by reference.
272const int NetEqDecodingTest::kTimeStepMs;
273const int NetEqDecodingTest::kBlockSize8kHz;
274const int NetEqDecodingTest::kBlockSize16kHz;
275const int NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000276const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277const int NetEqDecodingTest::kInitSampleRateHz;
278
279NetEqDecodingTest::NetEqDecodingTest()
280 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000281 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000283 output_sample_rate_(kInitSampleRateHz),
284 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000285 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286 memset(out_data_, 0, sizeof(out_data_));
287}
288
289void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000290 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000291 NetEqNetworkStatistics stat;
292 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
293 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 ASSERT_TRUE(neteq_);
295 LoadDecoders();
296}
297
298void NetEqDecodingTest::TearDown() {
299 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300}
301
302void NetEqDecodingTest::LoadDecoders() {
303 // Load PCMu.
304 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
305 // Load PCMa.
306 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000307#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 // Load iLBC.
309 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000310#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 // Load iSAC.
312 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000313#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314 // Load iSAC SWB.
315 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000316 // Load iSAC FB.
317 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000318#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319 // Load PCM16B nb.
320 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
321 // Load PCM16B wb.
322 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
323 // Load PCM16B swb32.
324 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
325 // Load CNG 8 kHz.
326 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
327 // Load CNG 16 kHz.
328 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
329}
330
331void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000332 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333}
334
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000335void NetEqDecodingTest::Process(int* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000337 while (packet_ && sim_clock_ >= packet_->time_ms()) {
338 if (packet_->payload_length_bytes() > 0) {
339 WebRtcRTPHeader rtp_header;
340 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000342 rtp_header, packet_->payload(),
343 packet_->payload_length_bytes(),
344 packet_->time_ms() * (output_sample_rate_ / 1000)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345 }
346 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000347 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 }
349
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000350 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 NetEqOutputType type;
352 int num_channels;
353 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
354 &num_channels, &type));
355 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
356 (*out_len == kBlockSize16kHz) ||
357 (*out_len == kBlockSize32kHz));
358 output_sample_rate_ = *out_len / 10 * 1000;
359
360 // Increase time.
361 sim_clock_ += kTimeStepMs;
362}
363
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000364void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
365 const std::string& ref_file,
366 const std::string& stat_ref_file,
367 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368 OpenInputFile(rtp_file);
369
370 std::string ref_out_file = "";
371 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000372 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 }
374 RefFiles ref_files(ref_file, ref_out_file);
375
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000376 std::string stat_out_file = "";
377 if (stat_ref_file.empty()) {
378 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
379 }
380 RefFiles network_stat_files(stat_ref_file, stat_out_file);
381
382 std::string rtcp_out_file = "";
383 if (rtcp_ref_file.empty()) {
384 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
385 }
386 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
387
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000388 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000390 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 std::ostringstream ss;
392 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
393 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000394 int out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000395 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397
398 // Query the network statistics API once per second
399 if (sim_clock_ % 1000 == 0) {
400 // Process NetworkStatistics.
401 NetEqNetworkStatistics network_stats;
402 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000403 ASSERT_NO_FATAL_FAILURE(
404 network_stat_files.ProcessReference(network_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000405
406 // Process RTCPstat.
407 RtcpStatistics rtcp_stats;
408 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000409 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410 }
411 }
412}
413
414void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
415 int timestamp,
416 WebRtcRTPHeader* rtp_info) {
417 rtp_info->header.sequenceNumber = frame_index;
418 rtp_info->header.timestamp = timestamp;
419 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
420 rtp_info->header.payloadType = 94; // PCM16b WB codec.
421 rtp_info->header.markerBit = 0;
422}
423
424void NetEqDecodingTest::PopulateCng(int frame_index,
425 int timestamp,
426 WebRtcRTPHeader* rtp_info,
427 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000428 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000429 rtp_info->header.sequenceNumber = frame_index;
430 rtp_info->header.timestamp = timestamp;
431 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
432 rtp_info->header.payloadType = 98; // WB CNG.
433 rtp_info->header.markerBit = 0;
434 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
435 *payload_len = 1; // Only noise level, no spectral parameters.
436}
437
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000438TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000439 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000440 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000441 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
442 // are identical. The latter could have been removed, but if clients still
443 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000444 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000445 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000446#if defined(_MSC_VER) && (_MSC_VER >= 1700)
447 // For Visual Studio 2012 and later, we will have to use the generic reference
448 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000449 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000450 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000451#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000452 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000453 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000454#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000455 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000456 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000457
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000458 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000459 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000460 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000461 DecodeAndCompare(input_rtp_file,
462 input_ref_file,
463 network_stat_ref_file,
464 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000465 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000466}
467
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000468// Use fax mode to avoid time-scaling. This is to simplify the testing of
469// packet waiting times in the packet buffer.
470class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
471 protected:
472 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
473 config_.playout_mode = kPlayoutFax;
474 }
475};
476
477TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000478 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
479 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000480 const size_t kSamples = 10 * 16;
481 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000482 for (size_t i = 0; i < num_frames; ++i) {
483 uint16_t payload[kSamples] = {0};
484 WebRtcRTPHeader rtp_info;
485 rtp_info.header.sequenceNumber = i;
486 rtp_info.header.timestamp = i * kSamples;
487 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
488 rtp_info.header.payloadType = 94; // PCM16b WB codec.
489 rtp_info.header.markerBit = 0;
490 ASSERT_EQ(0, neteq_->InsertPacket(
491 rtp_info,
492 reinterpret_cast<uint8_t*>(payload),
493 kPayloadBytes, 0));
494 }
495 // Pull out all data.
496 for (size_t i = 0; i < num_frames; ++i) {
497 int out_len;
498 int num_channels;
499 NetEqOutputType type;
500 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
501 &num_channels, &type));
502 ASSERT_EQ(kBlockSize16kHz, out_len);
503 }
504
505 std::vector<int> waiting_times;
506 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000507 EXPECT_EQ(num_frames, waiting_times.size());
508 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
509 // spacing (per definition), we expect the delay to increase with 10 ms for
510 // each packet.
511 for (size_t i = 0; i < waiting_times.size(); ++i) {
512 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
513 }
514
515 // Check statistics again and make sure it's been reset.
516 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000517 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000518 EXPECT_EQ(0, len);
519
520 // Process > 100 frames, and make sure that that we get statistics
521 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
522 num_frames = 110;
523 for (size_t i = 0; i < num_frames; ++i) {
524 uint16_t payload[kSamples] = {0};
525 WebRtcRTPHeader rtp_info;
526 rtp_info.header.sequenceNumber = i;
527 rtp_info.header.timestamp = i * kSamples;
528 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
529 rtp_info.header.payloadType = 94; // PCM16b WB codec.
530 rtp_info.header.markerBit = 0;
531 ASSERT_EQ(0, neteq_->InsertPacket(
532 rtp_info,
533 reinterpret_cast<uint8_t*>(payload),
534 kPayloadBytes, 0));
535 int out_len;
536 int num_channels;
537 NetEqOutputType type;
538 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
539 &num_channels, &type));
540 ASSERT_EQ(kBlockSize16kHz, out_len);
541 }
542
543 neteq_->WaitingTimes(&waiting_times);
544 EXPECT_EQ(100u, waiting_times.size());
545}
546
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000547TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000548 const int kNumFrames = 3000; // Needed for convergence.
549 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 const size_t kSamples = 10 * 16;
551 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000552 while (frame_index < kNumFrames) {
553 // Insert one packet each time, except every 10th time where we insert two
554 // packets at once. This will create a negative clock-drift of approx. 10%.
555 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
556 for (int n = 0; n < num_packets; ++n) {
557 uint8_t payload[kPayloadBytes] = {0};
558 WebRtcRTPHeader rtp_info;
559 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
560 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
561 ++frame_index;
562 }
563
564 // Pull out data once.
565 int out_len;
566 int num_channels;
567 NetEqOutputType type;
568 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
569 &num_channels, &type));
570 ASSERT_EQ(kBlockSize16kHz, out_len);
571 }
572
573 NetEqNetworkStatistics network_stats;
574 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
575 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
576}
577
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000578TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 const int kNumFrames = 5000; // Needed for convergence.
580 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000581 const size_t kSamples = 10 * 16;
582 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000583 for (int i = 0; i < kNumFrames; ++i) {
584 // Insert one packet each time, except every 10th time where we don't insert
585 // any packet. This will create a positive clock-drift of approx. 11%.
586 int num_packets = (i % 10 == 9 ? 0 : 1);
587 for (int n = 0; n < num_packets; ++n) {
588 uint8_t payload[kPayloadBytes] = {0};
589 WebRtcRTPHeader rtp_info;
590 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
591 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
592 ++frame_index;
593 }
594
595 // Pull out data once.
596 int out_len;
597 int num_channels;
598 NetEqOutputType type;
599 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
600 &num_channels, &type));
601 ASSERT_EQ(kBlockSize16kHz, out_len);
602 }
603
604 NetEqNetworkStatistics network_stats;
605 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
606 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
607}
608
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000609void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
610 double network_freeze_ms,
611 bool pull_audio_during_freeze,
612 int delay_tolerance_ms,
613 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 uint16_t seq_no = 0;
615 uint32_t timestamp = 0;
616 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000617 const size_t kSamples = kFrameSizeMs * 16;
618 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 double next_input_time_ms = 0.0;
620 double t_ms;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000621 int out_len;
622 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000623 NetEqOutputType type;
624
625 // Insert speech for 5 seconds.
626 const int kSpeechDurationMs = 5000;
627 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
628 // Each turn in this for loop is 10 ms.
629 while (next_input_time_ms <= t_ms) {
630 // Insert one 30 ms speech frame.
631 uint8_t payload[kPayloadBytes] = {0};
632 WebRtcRTPHeader rtp_info;
633 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
634 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
635 ++seq_no;
636 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000637 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 }
639 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
641 &num_channels, &type));
642 ASSERT_EQ(kBlockSize16kHz, out_len);
643 }
644
645 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000646 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000647
648 // Insert CNG for 1 minute (= 60000 ms).
649 const int kCngPeriodMs = 100;
650 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
651 const int kCngDurationMs = 60000;
652 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
653 // Each turn in this for loop is 10 ms.
654 while (next_input_time_ms <= t_ms) {
655 // Insert one CNG frame each 100 ms.
656 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000657 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000658 WebRtcRTPHeader rtp_info;
659 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
660 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
661 ++seq_no;
662 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000663 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 }
665 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
667 &num_channels, &type));
668 ASSERT_EQ(kBlockSize16kHz, out_len);
669 }
670
671 EXPECT_EQ(kOutputCNG, type);
672
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000673 if (network_freeze_ms > 0) {
674 // First keep pulling audio for |network_freeze_ms| without inserting
675 // any data, then insert CNG data corresponding to |network_freeze_ms|
676 // without pulling any output audio.
677 const double loop_end_time = t_ms + network_freeze_ms;
678 for (; t_ms < loop_end_time; t_ms += 10) {
679 // Pull out data once.
680 ASSERT_EQ(0,
681 neteq_->GetAudio(
682 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
683 ASSERT_EQ(kBlockSize16kHz, out_len);
684 EXPECT_EQ(kOutputCNG, type);
685 }
686 bool pull_once = pull_audio_during_freeze;
687 // If |pull_once| is true, GetAudio will be called once half-way through
688 // the network recovery period.
689 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
690 while (next_input_time_ms <= t_ms) {
691 if (pull_once && next_input_time_ms >= pull_time_ms) {
692 pull_once = false;
693 // Pull out data once.
694 ASSERT_EQ(
695 0,
696 neteq_->GetAudio(
697 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
698 ASSERT_EQ(kBlockSize16kHz, out_len);
699 EXPECT_EQ(kOutputCNG, type);
700 t_ms += 10;
701 }
702 // Insert one CNG frame each 100 ms.
703 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000704 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000705 WebRtcRTPHeader rtp_info;
706 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
707 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
708 ++seq_no;
709 timestamp += kCngPeriodSamples;
710 next_input_time_ms += kCngPeriodMs * drift_factor;
711 }
712 }
713
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000716 while (type != kOutputNormal) {
717 // Each turn in this for loop is 10 ms.
718 while (next_input_time_ms <= t_ms) {
719 // Insert one 30 ms speech frame.
720 uint8_t payload[kPayloadBytes] = {0};
721 WebRtcRTPHeader rtp_info;
722 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
723 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
724 ++seq_no;
725 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000726 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000727 }
728 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
730 &num_channels, &type));
731 ASSERT_EQ(kBlockSize16kHz, out_len);
732 // Increase clock.
733 t_ms += 10;
734 }
735
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000736 // Check that the speech starts again within reasonable time.
737 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
738 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000739 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000740 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000741 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
742 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743}
744
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000745TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000746 // Apply a clock drift of -25 ms / s (sender faster than receiver).
747 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000748 const double kNetworkFreezeTimeMs = 0.0;
749 const bool kGetAudioDuringFreezeRecovery = false;
750 const int kDelayToleranceMs = 20;
751 const int kMaxTimeToSpeechMs = 100;
752 LongCngWithClockDrift(kDriftFactor,
753 kNetworkFreezeTimeMs,
754 kGetAudioDuringFreezeRecovery,
755 kDelayToleranceMs,
756 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000757}
758
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000759TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000760 // Apply a clock drift of +25 ms / s (sender slower than receiver).
761 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000762 const double kNetworkFreezeTimeMs = 0.0;
763 const bool kGetAudioDuringFreezeRecovery = false;
764 const int kDelayToleranceMs = 20;
765 const int kMaxTimeToSpeechMs = 100;
766 LongCngWithClockDrift(kDriftFactor,
767 kNetworkFreezeTimeMs,
768 kGetAudioDuringFreezeRecovery,
769 kDelayToleranceMs,
770 kMaxTimeToSpeechMs);
771}
772
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000773TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000774 // Apply a clock drift of -25 ms / s (sender faster than receiver).
775 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
776 const double kNetworkFreezeTimeMs = 5000.0;
777 const bool kGetAudioDuringFreezeRecovery = false;
778 const int kDelayToleranceMs = 50;
779 const int kMaxTimeToSpeechMs = 200;
780 LongCngWithClockDrift(kDriftFactor,
781 kNetworkFreezeTimeMs,
782 kGetAudioDuringFreezeRecovery,
783 kDelayToleranceMs,
784 kMaxTimeToSpeechMs);
785}
786
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000787TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000788 // Apply a clock drift of +25 ms / s (sender slower than receiver).
789 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
790 const double kNetworkFreezeTimeMs = 5000.0;
791 const bool kGetAudioDuringFreezeRecovery = false;
792 const int kDelayToleranceMs = 20;
793 const int kMaxTimeToSpeechMs = 100;
794 LongCngWithClockDrift(kDriftFactor,
795 kNetworkFreezeTimeMs,
796 kGetAudioDuringFreezeRecovery,
797 kDelayToleranceMs,
798 kMaxTimeToSpeechMs);
799}
800
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000801TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000802 // Apply a clock drift of +25 ms / s (sender slower than receiver).
803 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
804 const double kNetworkFreezeTimeMs = 5000.0;
805 const bool kGetAudioDuringFreezeRecovery = true;
806 const int kDelayToleranceMs = 20;
807 const int kMaxTimeToSpeechMs = 100;
808 LongCngWithClockDrift(kDriftFactor,
809 kNetworkFreezeTimeMs,
810 kGetAudioDuringFreezeRecovery,
811 kDelayToleranceMs,
812 kMaxTimeToSpeechMs);
813}
814
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000815TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000816 const double kDriftFactor = 1.0; // No drift.
817 const double kNetworkFreezeTimeMs = 0.0;
818 const bool kGetAudioDuringFreezeRecovery = false;
819 const int kDelayToleranceMs = 10;
820 const int kMaxTimeToSpeechMs = 50;
821 LongCngWithClockDrift(kDriftFactor,
822 kNetworkFreezeTimeMs,
823 kGetAudioDuringFreezeRecovery,
824 kDelayToleranceMs,
825 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000826}
827
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000828TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000829 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000830 uint8_t payload[kPayloadBytes] = {0};
831 WebRtcRTPHeader rtp_info;
832 PopulateRtpInfo(0, 0, &rtp_info);
833 rtp_info.header.payloadType = 1; // Not registered as a decoder.
834 EXPECT_EQ(NetEq::kFail,
835 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
836 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
837}
838
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000839TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000840 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000841 uint8_t payload[kPayloadBytes] = {0};
842 WebRtcRTPHeader rtp_info;
843 PopulateRtpInfo(0, 0, &rtp_info);
844 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
845 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
846 NetEqOutputType type;
847 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
848 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000849 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000850 out_data_[i] = 1;
851 }
852 int num_channels;
853 int samples_per_channel;
854 EXPECT_EQ(NetEq::kFail,
855 neteq_->GetAudio(kMaxBlockSize, out_data_,
856 &samples_per_channel, &num_channels, &type));
857 // Verify that there is a decoder error to check.
858 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
859 // Code 6730 is an iSAC error code.
860 EXPECT_EQ(6730, neteq_->LastDecoderError());
861 // Verify that the first 160 samples are set to 0, and that the remaining
862 // samples are left unmodified.
863 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
864 for (int i = 0; i < kExpectedOutputLength; ++i) {
865 std::ostringstream ss;
866 ss << "i = " << i;
867 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
868 EXPECT_EQ(0, out_data_[i]);
869 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000870 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 std::ostringstream ss;
872 ss << "i = " << i;
873 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
874 EXPECT_EQ(1, out_data_[i]);
875 }
876}
877
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000878TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000879 NetEqOutputType type;
880 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
881 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000882 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000883 out_data_[i] = 1;
884 }
885 int num_channels;
886 int samples_per_channel;
887 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
888 &samples_per_channel,
889 &num_channels, &type));
890 // Verify that the first block of samples is set to 0.
891 static const int kExpectedOutputLength =
892 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
893 for (int i = 0; i < kExpectedOutputLength; ++i) {
894 std::ostringstream ss;
895 ss << "i = " << i;
896 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
897 EXPECT_EQ(0, out_data_[i]);
898 }
899}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000900
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000901class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000902 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000903 virtual void TestCondition(double sum_squared_noise,
904 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000905
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000906 void CheckBgn(int sampling_rate_hz) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000907 int16_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000908 uint8_t payload_type = 0xFF; // Invalid.
909 if (sampling_rate_hz == 8000) {
910 expected_samples_per_channel = kBlockSize8kHz;
911 payload_type = 93; // PCM 16, 8 kHz.
912 } else if (sampling_rate_hz == 16000) {
913 expected_samples_per_channel = kBlockSize16kHz;
914 payload_type = 94; // PCM 16, 16 kHZ.
915 } else if (sampling_rate_hz == 32000) {
916 expected_samples_per_channel = kBlockSize32kHz;
917 payload_type = 95; // PCM 16, 32 kHz.
918 } else {
919 ASSERT_TRUE(false); // Unsupported test case.
920 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000921
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000922 NetEqOutputType type;
923 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000924 test::AudioLoop input;
925 // We are using the same 32 kHz input file for all tests, regardless of
926 // |sampling_rate_hz|. The output may sound weird, but the test is still
927 // valid.
928 ASSERT_TRUE(input.Init(
929 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
930 10 * sampling_rate_hz, // Max 10 seconds loop length.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000931 static_cast<size_t>(expected_samples_per_channel)));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000932
933 // Payload of 10 ms of PCM16 32 kHz.
934 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000935 WebRtcRTPHeader rtp_info;
936 PopulateRtpInfo(0, 0, &rtp_info);
937 rtp_info.header.payloadType = payload_type;
938
939 int number_channels = 0;
940 int samples_per_channel = 0;
941
942 uint32_t receive_timestamp = 0;
943 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg@webrtc.org648f5d62015-02-10 09:18:28 +0000944 int16_t enc_len_bytes = WebRtcPcm16b_Encode(
945 input.GetNextBlock(), expected_samples_per_channel, payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000946 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
947
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000948 number_channels = 0;
949 samples_per_channel = 0;
950 ASSERT_EQ(0,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000951 neteq_->InsertPacket(rtp_info, payload,
952 static_cast<size_t>(enc_len_bytes),
953 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000954 ASSERT_EQ(0,
955 neteq_->GetAudio(kBlockSize32kHz,
956 output,
957 &samples_per_channel,
958 &number_channels,
959 &type));
960 ASSERT_EQ(1, number_channels);
961 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
962 ASSERT_EQ(kOutputNormal, type);
963
964 // Next packet.
965 rtp_info.header.timestamp += expected_samples_per_channel;
966 rtp_info.header.sequenceNumber++;
967 receive_timestamp += expected_samples_per_channel;
968 }
969
970 number_channels = 0;
971 samples_per_channel = 0;
972
973 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
974 // one frame without checking speech-type. This is the first frame pulled
975 // without inserting any packet, and might not be labeled as PLC.
976 ASSERT_EQ(0,
977 neteq_->GetAudio(kBlockSize32kHz,
978 output,
979 &samples_per_channel,
980 &number_channels,
981 &type));
982 ASSERT_EQ(1, number_channels);
983 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
984
985 // To be able to test the fading of background noise we need at lease to
986 // pull 611 frames.
987 const int kFadingThreshold = 611;
988
989 // Test several CNG-to-PLC packet for the expected behavior. The number 20
990 // is arbitrary, but sufficiently large to test enough number of frames.
991 const int kNumPlcToCngTestFrames = 20;
992 bool plc_to_cng = false;
993 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
994 number_channels = 0;
995 samples_per_channel = 0;
996 memset(output, 1, sizeof(output)); // Set to non-zero.
997 ASSERT_EQ(0,
998 neteq_->GetAudio(kBlockSize32kHz,
999 output,
1000 &samples_per_channel,
1001 &number_channels,
1002 &type));
1003 ASSERT_EQ(1, number_channels);
1004 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1005 if (type == kOutputPLCtoCNG) {
1006 plc_to_cng = true;
1007 double sum_squared = 0;
1008 for (int k = 0; k < number_channels * samples_per_channel; ++k)
1009 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001010 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001011 } else {
1012 EXPECT_EQ(kOutputPLC, type);
1013 }
1014 }
1015 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1016 }
1017};
1018
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001019class NetEqBgnTestOn : public NetEqBgnTest {
1020 protected:
1021 NetEqBgnTestOn() : NetEqBgnTest() {
1022 config_.background_noise_mode = NetEq::kBgnOn;
1023 }
1024
1025 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1026 EXPECT_NE(0, sum_squared_noise);
1027 }
1028};
1029
1030class NetEqBgnTestOff : public NetEqBgnTest {
1031 protected:
1032 NetEqBgnTestOff() : NetEqBgnTest() {
1033 config_.background_noise_mode = NetEq::kBgnOff;
1034 }
1035
1036 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1037 EXPECT_EQ(0, sum_squared_noise);
1038 }
1039};
1040
1041class NetEqBgnTestFade : public NetEqBgnTest {
1042 protected:
1043 NetEqBgnTestFade() : NetEqBgnTest() {
1044 config_.background_noise_mode = NetEq::kBgnFade;
1045 }
1046
1047 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1048 if (should_be_faded)
1049 EXPECT_EQ(0, sum_squared_noise);
1050 }
1051};
1052
1053TEST_F(NetEqBgnTestOn, RunTest) {
1054 CheckBgn(8000);
1055 CheckBgn(16000);
1056 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001057}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001058
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001059TEST_F(NetEqBgnTestOff, RunTest) {
1060 CheckBgn(8000);
1061 CheckBgn(16000);
1062 CheckBgn(32000);
1063}
1064
1065TEST_F(NetEqBgnTestFade, RunTest) {
1066 CheckBgn(8000);
1067 CheckBgn(16000);
1068 CheckBgn(32000);
1069}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001070
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001071TEST_F(NetEqDecodingTest, SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001072 WebRtcRTPHeader rtp_info;
1073 uint32_t receive_timestamp = 0;
1074 // For the readability use the following payloads instead of the defaults of
1075 // this test.
1076 uint8_t kPcm16WbPayloadType = 1;
1077 uint8_t kCngNbPayloadType = 2;
1078 uint8_t kCngWbPayloadType = 3;
1079 uint8_t kCngSwb32PayloadType = 4;
1080 uint8_t kCngSwb48PayloadType = 5;
1081 uint8_t kAvtPayloadType = 6;
1082 uint8_t kRedPayloadType = 7;
1083 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1084
1085 // Register decoders.
1086 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1087 kPcm16WbPayloadType));
1088 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1089 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1090 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1091 kCngSwb32PayloadType));
1092 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1093 kCngSwb48PayloadType));
1094 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1095 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1096 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1097
1098 PopulateRtpInfo(0, 0, &rtp_info);
1099 rtp_info.header.payloadType = kPcm16WbPayloadType;
1100
1101 // The first packet injected cannot be sync-packet.
1102 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1103
1104 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001105 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001106 uint8_t payload[kPayloadBytes] = {0};
1107 ASSERT_EQ(0, neteq_->InsertPacket(
1108 rtp_info, payload, kPayloadBytes, receive_timestamp));
1109
1110 // Next packet. Last packet contained 10 ms audio.
1111 rtp_info.header.sequenceNumber++;
1112 rtp_info.header.timestamp += kBlockSize16kHz;
1113 receive_timestamp += kBlockSize16kHz;
1114
1115 // Unacceptable payload types CNG, AVT (DTMF), RED.
1116 rtp_info.header.payloadType = kCngNbPayloadType;
1117 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1118
1119 rtp_info.header.payloadType = kCngWbPayloadType;
1120 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1121
1122 rtp_info.header.payloadType = kCngSwb32PayloadType;
1123 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1124
1125 rtp_info.header.payloadType = kCngSwb48PayloadType;
1126 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1127
1128 rtp_info.header.payloadType = kAvtPayloadType;
1129 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1130
1131 rtp_info.header.payloadType = kRedPayloadType;
1132 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1133
1134 // Change of codec cannot be initiated with a sync packet.
1135 rtp_info.header.payloadType = kIsacPayloadType;
1136 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1137
1138 // Change of SSRC is not allowed with a sync packet.
1139 rtp_info.header.payloadType = kPcm16WbPayloadType;
1140 ++rtp_info.header.ssrc;
1141 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1142
1143 --rtp_info.header.ssrc;
1144 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1145}
1146
1147// First insert several noise like packets, then sync-packets. Decoding all
1148// packets should not produce error, statistics should not show any packet loss
1149// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001150// TODO(turajs) we will have a better test if we have a referece NetEq, and
1151// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1152// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001153TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001154 WebRtcRTPHeader rtp_info;
1155 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001156 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001157 uint8_t payload[kPayloadBytes];
1158 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001159 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001160 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001161 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1162 }
1163 // Insert some packets which decode to noise. We are not interested in
1164 // actual decoded values.
1165 NetEqOutputType output_type;
1166 int num_channels;
1167 int samples_per_channel;
1168 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001169 for (int n = 0; n < 100; ++n) {
1170 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1171 receive_timestamp));
1172 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1173 &samples_per_channel, &num_channels,
1174 &output_type));
1175 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1176 ASSERT_EQ(1, num_channels);
1177
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001178 rtp_info.header.sequenceNumber++;
1179 rtp_info.header.timestamp += kBlockSize16kHz;
1180 receive_timestamp += kBlockSize16kHz;
1181 }
1182 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001183
1184 // Make sure sufficient number of sync packets are inserted that we can
1185 // conduct a test.
1186 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001187 // Insert sync-packets, the decoded sequence should be all-zero.
1188 for (int n = 0; n < kNumSyncPackets; ++n) {
1189 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1190 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1191 &samples_per_channel, &num_channels,
1192 &output_type));
1193 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1194 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001195 if (n > algorithmic_frame_delay) {
1196 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1197 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001198 rtp_info.header.sequenceNumber++;
1199 rtp_info.header.timestamp += kBlockSize16kHz;
1200 receive_timestamp += kBlockSize16kHz;
1201 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001202
1203 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001204 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001205 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1206 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1207 receive_timestamp));
1208 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1209 &samples_per_channel, &num_channels,
1210 &output_type));
1211 if (n >= algorithmic_frame_delay + 1) {
1212 // Expect that this frame contain samples from regular RTP.
1213 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1214 }
1215 rtp_info.header.sequenceNumber++;
1216 rtp_info.header.timestamp += kBlockSize16kHz;
1217 receive_timestamp += kBlockSize16kHz;
1218 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001219 NetEqNetworkStatistics network_stats;
1220 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1221 // Expecting a "clean" network.
1222 EXPECT_EQ(0, network_stats.packet_loss_rate);
1223 EXPECT_EQ(0, network_stats.expand_rate);
1224 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001225 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001226}
1227
1228// Test if the size of the packet buffer reported correctly when containing
1229// sync packets. Also, test if network packets override sync packets. That is to
1230// prefer decoding a network packet to a sync packet, if both have same sequence
1231// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001232TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001233 WebRtcRTPHeader rtp_info;
1234 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001235 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001236 uint8_t payload[kPayloadBytes];
1237 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001238 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001239 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1240 }
1241 // Insert some packets which decode to noise. We are not interested in
1242 // actual decoded values.
1243 NetEqOutputType output_type;
1244 int num_channels;
1245 int samples_per_channel;
1246 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001247 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1248 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001249 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1250 receive_timestamp));
1251 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1252 &samples_per_channel, &num_channels,
1253 &output_type));
1254 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1255 ASSERT_EQ(1, num_channels);
1256 rtp_info.header.sequenceNumber++;
1257 rtp_info.header.timestamp += kBlockSize16kHz;
1258 receive_timestamp += kBlockSize16kHz;
1259 }
1260 const int kNumSyncPackets = 10;
1261
1262 WebRtcRTPHeader first_sync_packet_rtp_info;
1263 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1264
1265 // Insert sync-packets, but no decoding.
1266 for (int n = 0; n < kNumSyncPackets; ++n) {
1267 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1268 rtp_info.header.sequenceNumber++;
1269 rtp_info.header.timestamp += kBlockSize16kHz;
1270 receive_timestamp += kBlockSize16kHz;
1271 }
1272 NetEqNetworkStatistics network_stats;
1273 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001274 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1275 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001276
1277 // Rewind |rtp_info| to that of the first sync packet.
1278 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1279
1280 // Insert.
1281 for (int n = 0; n < kNumSyncPackets; ++n) {
1282 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1283 receive_timestamp));
1284 rtp_info.header.sequenceNumber++;
1285 rtp_info.header.timestamp += kBlockSize16kHz;
1286 receive_timestamp += kBlockSize16kHz;
1287 }
1288
1289 // Decode.
1290 for (int n = 0; n < kNumSyncPackets; ++n) {
1291 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1292 &samples_per_channel, &num_channels,
1293 &output_type));
1294 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1295 ASSERT_EQ(1, num_channels);
1296 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1297 }
1298}
1299
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001300void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1301 uint32_t start_timestamp,
1302 const std::set<uint16_t>& drop_seq_numbers,
1303 bool expect_seq_no_wrap,
1304 bool expect_timestamp_wrap) {
1305 uint16_t seq_no = start_seq_no;
1306 uint32_t timestamp = start_timestamp;
1307 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1308 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1309 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001310 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001311 double next_input_time_ms = 0.0;
1312 int16_t decoded[kBlockSize16kHz];
1313 int num_channels;
1314 int samples_per_channel;
1315 NetEqOutputType output_type;
1316 uint32_t receive_timestamp = 0;
1317
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001318 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001319 const int kSpeechDurationMs = 2000;
1320 int packets_inserted = 0;
1321 uint16_t last_seq_no;
1322 uint32_t last_timestamp;
1323 bool timestamp_wrapped = false;
1324 bool seq_no_wrapped = false;
1325 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1326 // Each turn in this for loop is 10 ms.
1327 while (next_input_time_ms <= t_ms) {
1328 // Insert one 30 ms speech frame.
1329 uint8_t payload[kPayloadBytes] = {0};
1330 WebRtcRTPHeader rtp_info;
1331 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1332 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1333 // This sequence number was not in the set to drop. Insert it.
1334 ASSERT_EQ(0,
1335 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1336 receive_timestamp));
1337 ++packets_inserted;
1338 }
1339 NetEqNetworkStatistics network_stats;
1340 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1341
1342 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1343 // packet size for first few packets. Therefore we refrain from checking
1344 // the criteria.
1345 if (packets_inserted > 4) {
1346 // Expect preferred and actual buffer size to be no more than 2 frames.
1347 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001348 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1349 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001350 }
1351 last_seq_no = seq_no;
1352 last_timestamp = timestamp;
1353
1354 ++seq_no;
1355 timestamp += kSamples;
1356 receive_timestamp += kSamples;
1357 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1358
1359 seq_no_wrapped |= seq_no < last_seq_no;
1360 timestamp_wrapped |= timestamp < last_timestamp;
1361 }
1362 // Pull out data once.
1363 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1364 &samples_per_channel, &num_channels,
1365 &output_type));
1366 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1367 ASSERT_EQ(1, num_channels);
1368
1369 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001370 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001371 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001372 }
1373 // Make sure we have actually tested wrap-around.
1374 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1375 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1376}
1377
1378TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1379 // Start with a sequence number that will soon wrap.
1380 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1381 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1382}
1383
1384TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1385 // Start with a sequence number that will soon wrap.
1386 std::set<uint16_t> drop_seq_numbers;
1387 drop_seq_numbers.insert(0xFFFF);
1388 drop_seq_numbers.insert(0x0);
1389 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1390}
1391
1392TEST_F(NetEqDecodingTest, TimestampWrap) {
1393 // Start with a timestamp that will soon wrap.
1394 std::set<uint16_t> drop_seq_numbers;
1395 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1396}
1397
1398TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1399 // Start with a timestamp and a sequence number that will wrap at the same
1400 // time.
1401 std::set<uint16_t> drop_seq_numbers;
1402 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1403}
1404
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001405void NetEqDecodingTest::DuplicateCng() {
1406 uint16_t seq_no = 0;
1407 uint32_t timestamp = 0;
1408 const int kFrameSizeMs = 10;
1409 const int kSampleRateKhz = 16;
1410 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001411 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001412
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001413 const int algorithmic_delay_samples = std::max(
1414 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001415 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001416 // correct.
1417 int out_len;
1418 int num_channels;
1419 NetEqOutputType type;
1420 uint8_t payload[kPayloadBytes] = {0};
1421 WebRtcRTPHeader rtp_info;
1422 for (int i = 0; i < 3; ++i) {
1423 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1424 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1425 ++seq_no;
1426 timestamp += kSamples;
1427
1428 // Pull audio once.
1429 ASSERT_EQ(0,
1430 neteq_->GetAudio(
1431 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1432 ASSERT_EQ(kBlockSize16kHz, out_len);
1433 }
1434 // Verify speech output.
1435 EXPECT_EQ(kOutputNormal, type);
1436
1437 // Insert same CNG packet twice.
1438 const int kCngPeriodMs = 100;
1439 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001440 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001441 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1442 // This is the first time this CNG packet is inserted.
1443 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1444
1445 // Pull audio once and make sure CNG is played.
1446 ASSERT_EQ(0,
1447 neteq_->GetAudio(
1448 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1449 ASSERT_EQ(kBlockSize16kHz, out_len);
1450 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001451 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001452
1453 // Insert the same CNG packet again. Note that at this point it is old, since
1454 // we have already decoded the first copy of it.
1455 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1456
1457 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1458 // we have already pulled out CNG once.
1459 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1460 ASSERT_EQ(0,
1461 neteq_->GetAudio(
1462 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1463 ASSERT_EQ(kBlockSize16kHz, out_len);
1464 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001465 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001466 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001467 }
1468
1469 // Insert speech again.
1470 ++seq_no;
1471 timestamp += kCngPeriodSamples;
1472 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1473 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1474
1475 // Pull audio once and verify that the output is speech again.
1476 ASSERT_EQ(0,
1477 neteq_->GetAudio(
1478 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1479 ASSERT_EQ(kBlockSize16kHz, out_len);
1480 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001481 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001482 PlayoutTimestamp());
1483}
1484
1485uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1486 uint32_t playout_timestamp = 0;
1487 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1488 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001489}
1490
1491TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001492
1493TEST_F(NetEqDecodingTest, CngFirst) {
1494 uint16_t seq_no = 0;
1495 uint32_t timestamp = 0;
1496 const int kFrameSizeMs = 10;
1497 const int kSampleRateKhz = 16;
1498 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1499 const int kPayloadBytes = kSamples * 2;
1500 const int kCngPeriodMs = 100;
1501 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1502 size_t payload_len;
1503
1504 uint8_t payload[kPayloadBytes] = {0};
1505 WebRtcRTPHeader rtp_info;
1506
1507 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1508 ASSERT_EQ(NetEq::kOK,
1509 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1510 ++seq_no;
1511 timestamp += kCngPeriodSamples;
1512
1513 // Pull audio once and make sure CNG is played.
1514 int out_len;
1515 int num_channels;
1516 NetEqOutputType type;
1517 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1518 &num_channels, &type));
1519 ASSERT_EQ(kBlockSize16kHz, out_len);
1520 EXPECT_EQ(kOutputCNG, type);
1521
1522 // Insert some speech packets.
1523 for (int i = 0; i < 3; ++i) {
1524 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1525 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1526 ++seq_no;
1527 timestamp += kSamples;
1528
1529 // Pull audio once.
1530 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1531 &num_channels, &type));
1532 ASSERT_EQ(kBlockSize16kHz, out_len);
1533 }
1534 // Verify speech output.
1535 EXPECT_EQ(kOutputNormal, type);
1536}
1537
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001538} // namespace webrtc