blob: e8ef1ad5ffaa212921debf68ca90493fa4b639aa [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * This file includes unit tests for NetEQ.
13 */
14
Henrik Kjellander74640892015-10-29 11:31:02 +010015#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000017#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include <stdlib.h>
19#include <string.h> // memset
20
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000021#include <algorithm>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000022#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023#include <string>
24#include <vector>
25
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000026#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000027#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000028#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010031#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000033#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/typedefs.h"
35
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000036DEFINE_bool(gen_ref, false, "Generate reference files.");
37
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038namespace webrtc {
39
Peter Kastingdce40cf2015-08-24 14:52:23 -070040static bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000041 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000043 all_zero = buf[n] == 0;
44 return all_zero;
45}
46
Peter Kastingdce40cf2015-08-24 14:52:23 -070047static bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000048 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000050 all_non_zero = buf[n] != 0;
51 return all_non_zero;
52}
53
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054class RefFiles {
55 public:
56 RefFiles(const std::string& input_file, const std::string& output_file);
57 ~RefFiles();
58 template<class T> void ProcessReference(const T& test_results);
59 template<typename T, size_t n> void ProcessReference(
60 const T (&test_results)[n],
61 size_t length);
62 template<typename T, size_t n> void WriteToFile(
63 const T (&test_results)[n],
64 size_t length);
65 template<typename T, size_t n> void ReadFromFileAndCompare(
66 const T (&test_results)[n],
67 size_t length);
68 void WriteToFile(const NetEqNetworkStatistics& stats);
69 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
70 void WriteToFile(const RtcpStatistics& stats);
71 void ReadFromFileAndCompare(const RtcpStatistics& stats);
72
73 FILE* input_fp_;
74 FILE* output_fp_;
75};
76
77RefFiles::RefFiles(const std::string &input_file,
78 const std::string &output_file)
79 : input_fp_(NULL),
80 output_fp_(NULL) {
81 if (!input_file.empty()) {
82 input_fp_ = fopen(input_file.c_str(), "rb");
83 EXPECT_TRUE(input_fp_ != NULL);
84 }
85 if (!output_file.empty()) {
86 output_fp_ = fopen(output_file.c_str(), "wb");
87 EXPECT_TRUE(output_fp_ != NULL);
88 }
89}
90
91RefFiles::~RefFiles() {
92 if (input_fp_) {
93 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
94 fclose(input_fp_);
95 }
96 if (output_fp_) fclose(output_fp_);
97}
98
99template<class T>
100void RefFiles::ProcessReference(const T& test_results) {
101 WriteToFile(test_results);
102 ReadFromFileAndCompare(test_results);
103}
104
105template<typename T, size_t n>
106void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
107 WriteToFile(test_results, length);
108 ReadFromFileAndCompare(test_results, length);
109}
110
111template<typename T, size_t n>
112void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
113 if (output_fp_) {
114 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
115 }
116}
117
118template<typename T, size_t n>
119void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
120 size_t length) {
121 if (input_fp_) {
122 // Read from ref file.
123 T* ref = new T[length];
124 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
125 // Compare
126 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
127 delete [] ref;
128 }
129}
130
131void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
132 if (output_fp_) {
133 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
134 output_fp_));
135 }
136}
137
138void RefFiles::ReadFromFileAndCompare(
139 const NetEqNetworkStatistics& stats) {
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000140 // TODO(minyue): Update resource/audio_coding/neteq_network_stats.dat and
141 // resource/audio_coding/neteq_network_stats_win32.dat.
142 struct NetEqNetworkStatisticsOld {
143 uint16_t current_buffer_size_ms; // Current jitter buffer size in ms.
144 uint16_t preferred_buffer_size_ms; // Target buffer size in ms.
145 uint16_t jitter_peaks_found; // 1 if adding extra delay due to peaky
146 // jitter; 0 otherwise.
147 uint16_t packet_loss_rate; // Loss rate (network + late) in Q14.
148 uint16_t packet_discard_rate; // Late loss rate in Q14.
149 uint16_t expand_rate; // Fraction (of original stream) of synthesized
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000150 // audio inserted through expansion (in Q14).
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000151 uint16_t preemptive_rate; // Fraction of data inserted through pre-emptive
152 // expansion (in Q14).
153 uint16_t accelerate_rate; // Fraction of data removed through acceleration
154 // (in Q14).
155 int32_t clockdrift_ppm; // Average clock-drift in parts-per-million
156 // (positive or negative).
157 int added_zero_samples; // Number of zero samples added in "off" mode.
158 };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 if (input_fp_) {
160 // Read from ref file.
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000161 size_t stat_size = sizeof(NetEqNetworkStatisticsOld);
162 NetEqNetworkStatisticsOld ref_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
164 // Compare
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000165 ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms);
166 ASSERT_EQ(stats.preferred_buffer_size_ms,
167 ref_stats.preferred_buffer_size_ms);
168 ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found);
169 ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate);
170 ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate);
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000171 ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate);
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000172 ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate);
173 ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate);
174 ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700175 ASSERT_EQ(stats.added_zero_samples,
176 static_cast<size_t>(ref_stats.added_zero_samples));
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000177 ASSERT_EQ(stats.secondary_decoded_rate, 0);
minyue@webrtc.org7d721ee2015-02-18 10:01:53 +0000178 ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179 }
180}
181
182void RefFiles::WriteToFile(const RtcpStatistics& stats) {
183 if (output_fp_) {
184 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
185 output_fp_));
186 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
187 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000188 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
189 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 output_fp_));
191 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
192 output_fp_));
193 }
194}
195
196void RefFiles::ReadFromFileAndCompare(
197 const RtcpStatistics& stats) {
198 if (input_fp_) {
199 // Read from ref file.
200 RtcpStatistics ref_stats;
201 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
202 sizeof(ref_stats.fraction_lost), 1, input_fp_));
203 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
204 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000205 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
206 sizeof(ref_stats.extended_max_sequence_number), 1,
207 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
209 input_fp_));
210 // Compare
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000211 ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
212 ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
213 ASSERT_EQ(ref_stats.extended_max_sequence_number,
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000214 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000215 ASSERT_EQ(ref_stats.jitter, stats.jitter);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216 }
217}
218
219class NetEqDecodingTest : public ::testing::Test {
220 protected:
221 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
222 // constants below can be changed.
223 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700224 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
225 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
226 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000227 static const size_t kMaxBlockSize = kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228 static const int kInitSampleRateHz = 8000;
229
230 NetEqDecodingTest();
231 virtual void SetUp();
232 virtual void TearDown();
233 void SelectDecoders(NetEqDecoder* used_codec);
234 void LoadDecoders();
235 void OpenInputFile(const std::string &rtp_file);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236 void Process(size_t* out_len);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000237 void DecodeAndCompare(const std::string& rtp_file,
238 const std::string& ref_file,
239 const std::string& stat_ref_file,
240 const std::string& rtcp_ref_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 static void PopulateRtpInfo(int frame_index,
242 int timestamp,
243 WebRtcRTPHeader* rtp_info);
244 static void PopulateCng(int frame_index,
245 int timestamp,
246 WebRtcRTPHeader* rtp_info,
247 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000248 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000250 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
251 const std::set<uint16_t>& drop_seq_numbers,
252 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
253
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000254 void LongCngWithClockDrift(double drift_factor,
255 double network_freeze_ms,
256 bool pull_audio_during_freeze,
257 int delay_tolerance_ms,
258 int max_time_to_speech_ms);
259
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000260 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000261
wu@webrtc.org94454b72014-06-05 20:34:08 +0000262 uint32_t PlayoutTimestamp();
263
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000265 NetEq::Config config_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000266 rtc::scoped_ptr<test::RtpFileSource> rtp_source_;
267 rtc::scoped_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 unsigned int sim_clock_;
269 int16_t out_data_[kMaxBlockSize];
270 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000271 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272};
273
274// Allocating the static const so that it can be passed by reference.
275const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700276const size_t NetEqDecodingTest::kBlockSize8kHz;
277const size_t NetEqDecodingTest::kBlockSize16kHz;
278const size_t NetEqDecodingTest::kBlockSize32kHz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000279const size_t NetEqDecodingTest::kMaxBlockSize;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280const int NetEqDecodingTest::kInitSampleRateHz;
281
282NetEqDecodingTest::NetEqDecodingTest()
283 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000284 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000286 output_sample_rate_(kInitSampleRateHz),
287 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000288 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 memset(out_data_, 0, sizeof(out_data_));
290}
291
292void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000293 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000294 NetEqNetworkStatistics stat;
295 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
296 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 ASSERT_TRUE(neteq_);
298 LoadDecoders();
299}
300
301void NetEqDecodingTest::TearDown() {
302 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303}
304
305void NetEqDecodingTest::LoadDecoders() {
306 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800307 ASSERT_EQ(0,
308 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800310 ASSERT_EQ(0,
311 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700312#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800314 ASSERT_EQ(
315 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700316#endif
317#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000318 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800319 ASSERT_EQ(
320 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700321#endif
322#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800324 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
325 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700326#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800328 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
329 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800331 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
332 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800334 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
335 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800337 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
338 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800340 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
341 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342}
343
344void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000345 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346}
347
Peter Kastingdce40cf2015-08-24 14:52:23 -0700348void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000350 while (packet_ && sim_clock_ >= packet_->time_ms()) {
351 if (packet_->payload_length_bytes() > 0) {
352 WebRtcRTPHeader rtp_header;
353 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800355 rtp_header,
356 rtc::ArrayView<const uint8_t>(
357 packet_->payload(), packet_->payload_length_bytes()),
358 static_cast<uint32_t>(packet_->time_ms() *
359 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 }
361 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000362 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 }
364
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000365 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366 NetEqOutputType type;
367 int num_channels;
368 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
369 &num_channels, &type));
370 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
371 (*out_len == kBlockSize16kHz) ||
372 (*out_len == kBlockSize32kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700373 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundind89814b2015-11-23 06:49:25 -0800374 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375
376 // Increase time.
377 sim_clock_ += kTimeStepMs;
378}
379
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000380void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
381 const std::string& ref_file,
382 const std::string& stat_ref_file,
383 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 OpenInputFile(rtp_file);
385
386 std::string ref_out_file = "";
387 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000388 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 }
390 RefFiles ref_files(ref_file, ref_out_file);
391
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000392 std::string stat_out_file = "";
393 if (stat_ref_file.empty()) {
394 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
395 }
396 RefFiles network_stat_files(stat_ref_file, stat_out_file);
397
398 std::string rtcp_out_file = "";
399 if (rtcp_ref_file.empty()) {
400 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
401 }
402 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
403
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000404 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000405 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000406 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000407 std::ostringstream ss;
408 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
409 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700410 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000411 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000412 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413
414 // Query the network statistics API once per second
415 if (sim_clock_ % 1000 == 0) {
416 // Process NetworkStatistics.
417 NetEqNetworkStatistics network_stats;
418 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000419 ASSERT_NO_FATAL_FAILURE(
420 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700421 // Compare with CurrentDelay, which should be identical.
422 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423
424 // Process RTCPstat.
425 RtcpStatistics rtcp_stats;
426 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000427 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000428 }
429 }
430}
431
432void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
433 int timestamp,
434 WebRtcRTPHeader* rtp_info) {
435 rtp_info->header.sequenceNumber = frame_index;
436 rtp_info->header.timestamp = timestamp;
437 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
438 rtp_info->header.payloadType = 94; // PCM16b WB codec.
439 rtp_info->header.markerBit = 0;
440}
441
442void NetEqDecodingTest::PopulateCng(int frame_index,
443 int timestamp,
444 WebRtcRTPHeader* rtp_info,
445 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000446 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000447 rtp_info->header.sequenceNumber = frame_index;
448 rtp_info->header.timestamp = timestamp;
449 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
450 rtp_info->header.payloadType = 98; // WB CNG.
451 rtp_info->header.markerBit = 0;
452 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
453 *payload_len = 1; // Only noise level, no spectral parameters.
454}
455
kwiberg98ab3a42015-09-30 21:54:21 -0700456#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISAC)) && \
457 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
458#define IF_ALL_CODECS(x) x
459#else
460#define IF_ALL_CODECS(x) DISABLED_##x
461#endif
462
henrikaa2c79402015-06-10 13:24:48 +0200463TEST_F(NetEqDecodingTest,
kwiberg98ab3a42015-09-30 21:54:21 -0700464 DISABLED_ON_IOS(DISABLED_ON_ANDROID(IF_ALL_CODECS(TestBitExactness)))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000465 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000466 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000467 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
468 // are identical. The latter could have been removed, but if clients still
469 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000470 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000471 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000472#if defined(_MSC_VER) && (_MSC_VER >= 1700)
473 // For Visual Studio 2012 and later, we will have to use the generic reference
474 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000475 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000476 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000477#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000478 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000479 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000480#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000481 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000482 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000483
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000484 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000485 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000486 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000487 DecodeAndCompare(input_rtp_file,
488 input_ref_file,
489 network_stat_ref_file,
490 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000491 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000492}
493
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000494// Use fax mode to avoid time-scaling. This is to simplify the testing of
495// packet waiting times in the packet buffer.
496class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
497 protected:
498 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
499 config_.playout_mode = kPlayoutFax;
500 }
501};
502
503TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000504 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
505 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000506 const size_t kSamples = 10 * 16;
507 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000508 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800509 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000510 WebRtcRTPHeader rtp_info;
511 rtp_info.header.sequenceNumber = i;
512 rtp_info.header.timestamp = i * kSamples;
513 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
514 rtp_info.header.payloadType = 94; // PCM16b WB codec.
515 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800516 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000517 }
518 // Pull out all data.
519 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700520 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000521 int num_channels;
522 NetEqOutputType type;
523 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
524 &num_channels, &type));
525 ASSERT_EQ(kBlockSize16kHz, out_len);
526 }
527
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200528 NetEqNetworkStatistics stats;
529 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000530 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
531 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200532 // each packet. Thus, we are calculating the statistics for a series from 10
533 // to 300, in steps of 10 ms.
534 EXPECT_EQ(155, stats.mean_waiting_time_ms);
535 EXPECT_EQ(155, stats.median_waiting_time_ms);
536 EXPECT_EQ(10, stats.min_waiting_time_ms);
537 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538
539 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200540 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
541 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
542 EXPECT_EQ(-1, stats.median_waiting_time_ms);
543 EXPECT_EQ(-1, stats.min_waiting_time_ms);
544 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545}
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);
kwibergee2bac22015-11-11 10:34:00 -0800560 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561 ++frame_index;
562 }
563
564 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700565 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000566 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);
kwibergee2bac22015-11-11 10:34:00 -0800591 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000592 ++frame_index;
593 }
594
595 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700596 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000597 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;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700621 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000622 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);
kwibergee2bac22015-11-11 10:34:00 -0800634 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635 ++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);
kwibergee2bac22015-11-11 10:34:00 -0800660 ASSERT_EQ(0, neteq_->InsertPacket(
661 rtp_info,
662 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000663 ++seq_no;
664 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000665 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666 }
667 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
669 &num_channels, &type));
670 ASSERT_EQ(kBlockSize16kHz, out_len);
671 }
672
673 EXPECT_EQ(kOutputCNG, type);
674
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000675 if (network_freeze_ms > 0) {
676 // First keep pulling audio for |network_freeze_ms| without inserting
677 // any data, then insert CNG data corresponding to |network_freeze_ms|
678 // without pulling any output audio.
679 const double loop_end_time = t_ms + network_freeze_ms;
680 for (; t_ms < loop_end_time; t_ms += 10) {
681 // Pull out data once.
682 ASSERT_EQ(0,
683 neteq_->GetAudio(
684 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
685 ASSERT_EQ(kBlockSize16kHz, out_len);
686 EXPECT_EQ(kOutputCNG, type);
687 }
688 bool pull_once = pull_audio_during_freeze;
689 // If |pull_once| is true, GetAudio will be called once half-way through
690 // the network recovery period.
691 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
692 while (next_input_time_ms <= t_ms) {
693 if (pull_once && next_input_time_ms >= pull_time_ms) {
694 pull_once = false;
695 // Pull out data once.
696 ASSERT_EQ(
697 0,
698 neteq_->GetAudio(
699 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
700 ASSERT_EQ(kBlockSize16kHz, out_len);
701 EXPECT_EQ(kOutputCNG, type);
702 t_ms += 10;
703 }
704 // Insert one CNG frame each 100 ms.
705 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000706 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000707 WebRtcRTPHeader rtp_info;
708 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800709 ASSERT_EQ(0, neteq_->InsertPacket(
710 rtp_info,
711 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000712 ++seq_no;
713 timestamp += kCngPeriodSamples;
714 next_input_time_ms += kCngPeriodMs * drift_factor;
715 }
716 }
717
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000718 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000719 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 while (type != kOutputNormal) {
721 // Each turn in this for loop is 10 ms.
722 while (next_input_time_ms <= t_ms) {
723 // Insert one 30 ms speech frame.
724 uint8_t payload[kPayloadBytes] = {0};
725 WebRtcRTPHeader rtp_info;
726 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800727 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000728 ++seq_no;
729 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000730 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 }
732 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
734 &num_channels, &type));
735 ASSERT_EQ(kBlockSize16kHz, out_len);
736 // Increase clock.
737 t_ms += 10;
738 }
739
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 // Check that the speech starts again within reasonable time.
741 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
742 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000743 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000745 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
746 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747}
748
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000749TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000750 // Apply a clock drift of -25 ms / s (sender faster than receiver).
751 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000752 const double kNetworkFreezeTimeMs = 0.0;
753 const bool kGetAudioDuringFreezeRecovery = false;
754 const int kDelayToleranceMs = 20;
755 const int kMaxTimeToSpeechMs = 100;
756 LongCngWithClockDrift(kDriftFactor,
757 kNetworkFreezeTimeMs,
758 kGetAudioDuringFreezeRecovery,
759 kDelayToleranceMs,
760 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000761}
762
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000763TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000764 // Apply a clock drift of +25 ms / s (sender slower than receiver).
765 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000766 const double kNetworkFreezeTimeMs = 0.0;
767 const bool kGetAudioDuringFreezeRecovery = false;
768 const int kDelayToleranceMs = 20;
769 const int kMaxTimeToSpeechMs = 100;
770 LongCngWithClockDrift(kDriftFactor,
771 kNetworkFreezeTimeMs,
772 kGetAudioDuringFreezeRecovery,
773 kDelayToleranceMs,
774 kMaxTimeToSpeechMs);
775}
776
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000777TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000778 // Apply a clock drift of -25 ms / s (sender faster than receiver).
779 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
780 const double kNetworkFreezeTimeMs = 5000.0;
781 const bool kGetAudioDuringFreezeRecovery = false;
782 const int kDelayToleranceMs = 50;
783 const int kMaxTimeToSpeechMs = 200;
784 LongCngWithClockDrift(kDriftFactor,
785 kNetworkFreezeTimeMs,
786 kGetAudioDuringFreezeRecovery,
787 kDelayToleranceMs,
788 kMaxTimeToSpeechMs);
789}
790
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000791TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000792 // Apply a clock drift of +25 ms / s (sender slower than receiver).
793 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
794 const double kNetworkFreezeTimeMs = 5000.0;
795 const bool kGetAudioDuringFreezeRecovery = false;
796 const int kDelayToleranceMs = 20;
797 const int kMaxTimeToSpeechMs = 100;
798 LongCngWithClockDrift(kDriftFactor,
799 kNetworkFreezeTimeMs,
800 kGetAudioDuringFreezeRecovery,
801 kDelayToleranceMs,
802 kMaxTimeToSpeechMs);
803}
804
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000805TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000806 // Apply a clock drift of +25 ms / s (sender slower than receiver).
807 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
808 const double kNetworkFreezeTimeMs = 5000.0;
809 const bool kGetAudioDuringFreezeRecovery = true;
810 const int kDelayToleranceMs = 20;
811 const int kMaxTimeToSpeechMs = 100;
812 LongCngWithClockDrift(kDriftFactor,
813 kNetworkFreezeTimeMs,
814 kGetAudioDuringFreezeRecovery,
815 kDelayToleranceMs,
816 kMaxTimeToSpeechMs);
817}
818
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000819TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000820 const double kDriftFactor = 1.0; // No drift.
821 const double kNetworkFreezeTimeMs = 0.0;
822 const bool kGetAudioDuringFreezeRecovery = false;
823 const int kDelayToleranceMs = 10;
824 const int kMaxTimeToSpeechMs = 50;
825 LongCngWithClockDrift(kDriftFactor,
826 kNetworkFreezeTimeMs,
827 kGetAudioDuringFreezeRecovery,
828 kDelayToleranceMs,
829 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000830}
831
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000832TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000833 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000834 uint8_t payload[kPayloadBytes] = {0};
835 WebRtcRTPHeader rtp_info;
836 PopulateRtpInfo(0, 0, &rtp_info);
837 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800838 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000839 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
840}
841
kwiberg98ab3a42015-09-30 21:54:21 -0700842#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
843#define IF_ISAC(x) x
844#else
845#define IF_ISAC(x) DISABLED_##x
846#endif
847
848TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000849 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000850 uint8_t payload[kPayloadBytes] = {0};
851 WebRtcRTPHeader rtp_info;
852 PopulateRtpInfo(0, 0, &rtp_info);
853 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800854 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000855 NetEqOutputType type;
856 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
857 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000858 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000859 out_data_[i] = 1;
860 }
861 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700862 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000863 EXPECT_EQ(NetEq::kFail,
864 neteq_->GetAudio(kMaxBlockSize, out_data_,
865 &samples_per_channel, &num_channels, &type));
866 // Verify that there is a decoder error to check.
867 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
868 // Code 6730 is an iSAC error code.
869 EXPECT_EQ(6730, neteq_->LastDecoderError());
870 // Verify that the first 160 samples are set to 0, and that the remaining
871 // samples are left unmodified.
872 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
873 for (int i = 0; i < kExpectedOutputLength; ++i) {
874 std::ostringstream ss;
875 ss << "i = " << i;
876 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
877 EXPECT_EQ(0, out_data_[i]);
878 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000879 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 std::ostringstream ss;
881 ss << "i = " << i;
882 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
883 EXPECT_EQ(1, out_data_[i]);
884 }
885}
886
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000887TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000888 NetEqOutputType type;
889 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
890 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000891 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000892 out_data_[i] = 1;
893 }
894 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700895 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000896 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
897 &samples_per_channel,
898 &num_channels, &type));
899 // Verify that the first block of samples is set to 0.
900 static const int kExpectedOutputLength =
901 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
902 for (int i = 0; i < kExpectedOutputLength; ++i) {
903 std::ostringstream ss;
904 ss << "i = " << i;
905 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
906 EXPECT_EQ(0, out_data_[i]);
907 }
henrik.lundind89814b2015-11-23 06:49:25 -0800908 // Verify that the sample rate did not change from the initial configuration.
909 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000910}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000911
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000912class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000913 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000914 virtual void TestCondition(double sum_squared_noise,
915 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000916
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000917 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700918 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000919 uint8_t payload_type = 0xFF; // Invalid.
920 if (sampling_rate_hz == 8000) {
921 expected_samples_per_channel = kBlockSize8kHz;
922 payload_type = 93; // PCM 16, 8 kHz.
923 } else if (sampling_rate_hz == 16000) {
924 expected_samples_per_channel = kBlockSize16kHz;
925 payload_type = 94; // PCM 16, 16 kHZ.
926 } else if (sampling_rate_hz == 32000) {
927 expected_samples_per_channel = kBlockSize32kHz;
928 payload_type = 95; // PCM 16, 32 kHz.
929 } else {
930 ASSERT_TRUE(false); // Unsupported test case.
931 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000932
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000933 NetEqOutputType type;
934 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000935 test::AudioLoop input;
936 // We are using the same 32 kHz input file for all tests, regardless of
937 // |sampling_rate_hz|. The output may sound weird, but the test is still
938 // valid.
939 ASSERT_TRUE(input.Init(
940 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
941 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700942 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000943
944 // Payload of 10 ms of PCM16 32 kHz.
945 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000946 WebRtcRTPHeader rtp_info;
947 PopulateRtpInfo(0, 0, &rtp_info);
948 rtp_info.header.payloadType = payload_type;
949
950 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700951 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000952
953 uint32_t receive_timestamp = 0;
954 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800955 auto block = input.GetNextBlock();
956 ASSERT_EQ(expected_samples_per_channel, block.size());
957 size_t enc_len_bytes =
958 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000959 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
960
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000961 number_channels = 0;
962 samples_per_channel = 0;
kwibergee2bac22015-11-11 10:34:00 -0800963 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
964 payload, enc_len_bytes),
965 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000966 ASSERT_EQ(0,
967 neteq_->GetAudio(kBlockSize32kHz,
968 output,
969 &samples_per_channel,
970 &number_channels,
971 &type));
972 ASSERT_EQ(1, number_channels);
973 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
974 ASSERT_EQ(kOutputNormal, type);
975
976 // Next packet.
977 rtp_info.header.timestamp += expected_samples_per_channel;
978 rtp_info.header.sequenceNumber++;
979 receive_timestamp += expected_samples_per_channel;
980 }
981
982 number_channels = 0;
983 samples_per_channel = 0;
984
985 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
986 // one frame without checking speech-type. This is the first frame pulled
987 // without inserting any packet, and might not be labeled as PLC.
988 ASSERT_EQ(0,
989 neteq_->GetAudio(kBlockSize32kHz,
990 output,
991 &samples_per_channel,
992 &number_channels,
993 &type));
994 ASSERT_EQ(1, number_channels);
995 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
996
997 // To be able to test the fading of background noise we need at lease to
998 // pull 611 frames.
999 const int kFadingThreshold = 611;
1000
1001 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1002 // is arbitrary, but sufficiently large to test enough number of frames.
1003 const int kNumPlcToCngTestFrames = 20;
1004 bool plc_to_cng = false;
1005 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
1006 number_channels = 0;
1007 samples_per_channel = 0;
1008 memset(output, 1, sizeof(output)); // Set to non-zero.
1009 ASSERT_EQ(0,
1010 neteq_->GetAudio(kBlockSize32kHz,
1011 output,
1012 &samples_per_channel,
1013 &number_channels,
1014 &type));
1015 ASSERT_EQ(1, number_channels);
1016 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1017 if (type == kOutputPLCtoCNG) {
1018 plc_to_cng = true;
1019 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001020 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001021 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001022 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001023 } else {
1024 EXPECT_EQ(kOutputPLC, type);
1025 }
1026 }
1027 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1028 }
1029};
1030
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001031class NetEqBgnTestOn : public NetEqBgnTest {
1032 protected:
1033 NetEqBgnTestOn() : NetEqBgnTest() {
1034 config_.background_noise_mode = NetEq::kBgnOn;
1035 }
1036
1037 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1038 EXPECT_NE(0, sum_squared_noise);
1039 }
1040};
1041
1042class NetEqBgnTestOff : public NetEqBgnTest {
1043 protected:
1044 NetEqBgnTestOff() : NetEqBgnTest() {
1045 config_.background_noise_mode = NetEq::kBgnOff;
1046 }
1047
1048 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1049 EXPECT_EQ(0, sum_squared_noise);
1050 }
1051};
1052
1053class NetEqBgnTestFade : public NetEqBgnTest {
1054 protected:
1055 NetEqBgnTestFade() : NetEqBgnTest() {
1056 config_.background_noise_mode = NetEq::kBgnFade;
1057 }
1058
1059 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1060 if (should_be_faded)
1061 EXPECT_EQ(0, sum_squared_noise);
1062 }
1063};
1064
henrika1d34fe92015-06-16 10:04:20 +02001065TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001066 CheckBgn(8000);
1067 CheckBgn(16000);
1068 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001069}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001070
henrika1d34fe92015-06-16 10:04:20 +02001071TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001072 CheckBgn(8000);
1073 CheckBgn(16000);
1074 CheckBgn(32000);
1075}
1076
henrika1d34fe92015-06-16 10:04:20 +02001077TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001078 CheckBgn(8000);
1079 CheckBgn(16000);
1080 CheckBgn(32000);
1081}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001082
kwiberg98ab3a42015-09-30 21:54:21 -07001083TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001084 WebRtcRTPHeader rtp_info;
1085 uint32_t receive_timestamp = 0;
1086 // For the readability use the following payloads instead of the defaults of
1087 // this test.
1088 uint8_t kPcm16WbPayloadType = 1;
1089 uint8_t kCngNbPayloadType = 2;
1090 uint8_t kCngWbPayloadType = 3;
1091 uint8_t kCngSwb32PayloadType = 4;
1092 uint8_t kCngSwb48PayloadType = 5;
1093 uint8_t kAvtPayloadType = 6;
1094 uint8_t kRedPayloadType = 7;
1095 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1096
1097 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001098 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001099 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001100 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001101 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001102 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001103 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001104 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001105 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001106 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001107 "cng-swb48", kCngSwb48PayloadType));
1108 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001109 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001110 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001111 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001112 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001113 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001114
1115 PopulateRtpInfo(0, 0, &rtp_info);
1116 rtp_info.header.payloadType = kPcm16WbPayloadType;
1117
1118 // The first packet injected cannot be sync-packet.
1119 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1120
1121 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001122 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001123 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001124 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001125
1126 // Next packet. Last packet contained 10 ms audio.
1127 rtp_info.header.sequenceNumber++;
1128 rtp_info.header.timestamp += kBlockSize16kHz;
1129 receive_timestamp += kBlockSize16kHz;
1130
1131 // Unacceptable payload types CNG, AVT (DTMF), RED.
1132 rtp_info.header.payloadType = kCngNbPayloadType;
1133 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1134
1135 rtp_info.header.payloadType = kCngWbPayloadType;
1136 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1137
1138 rtp_info.header.payloadType = kCngSwb32PayloadType;
1139 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1140
1141 rtp_info.header.payloadType = kCngSwb48PayloadType;
1142 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1143
1144 rtp_info.header.payloadType = kAvtPayloadType;
1145 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1146
1147 rtp_info.header.payloadType = kRedPayloadType;
1148 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1149
1150 // Change of codec cannot be initiated with a sync packet.
1151 rtp_info.header.payloadType = kIsacPayloadType;
1152 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1153
1154 // Change of SSRC is not allowed with a sync packet.
1155 rtp_info.header.payloadType = kPcm16WbPayloadType;
1156 ++rtp_info.header.ssrc;
1157 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1158
1159 --rtp_info.header.ssrc;
1160 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1161}
1162
1163// First insert several noise like packets, then sync-packets. Decoding all
1164// packets should not produce error, statistics should not show any packet loss
1165// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001166// TODO(turajs) we will have a better test if we have a referece NetEq, and
1167// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1168// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001169TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001170 WebRtcRTPHeader rtp_info;
1171 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001172 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001173 uint8_t payload[kPayloadBytes];
1174 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001175 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001176 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001177 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1178 }
1179 // Insert some packets which decode to noise. We are not interested in
1180 // actual decoded values.
1181 NetEqOutputType output_type;
1182 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001183 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001184 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001185 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001186 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001187 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1188 &samples_per_channel, &num_channels,
1189 &output_type));
1190 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1191 ASSERT_EQ(1, num_channels);
1192
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001193 rtp_info.header.sequenceNumber++;
1194 rtp_info.header.timestamp += kBlockSize16kHz;
1195 receive_timestamp += kBlockSize16kHz;
1196 }
1197 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001198
1199 // Make sure sufficient number of sync packets are inserted that we can
1200 // conduct a test.
1201 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001202 // Insert sync-packets, the decoded sequence should be all-zero.
1203 for (int n = 0; n < kNumSyncPackets; ++n) {
1204 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1205 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1206 &samples_per_channel, &num_channels,
1207 &output_type));
1208 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1209 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001210 if (n > algorithmic_frame_delay) {
1211 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1212 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001213 rtp_info.header.sequenceNumber++;
1214 rtp_info.header.timestamp += kBlockSize16kHz;
1215 receive_timestamp += kBlockSize16kHz;
1216 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001217
1218 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001219 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001220 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001221 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001222 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1223 &samples_per_channel, &num_channels,
1224 &output_type));
1225 if (n >= algorithmic_frame_delay + 1) {
1226 // Expect that this frame contain samples from regular RTP.
1227 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1228 }
1229 rtp_info.header.sequenceNumber++;
1230 rtp_info.header.timestamp += kBlockSize16kHz;
1231 receive_timestamp += kBlockSize16kHz;
1232 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001233 NetEqNetworkStatistics network_stats;
1234 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1235 // Expecting a "clean" network.
1236 EXPECT_EQ(0, network_stats.packet_loss_rate);
1237 EXPECT_EQ(0, network_stats.expand_rate);
1238 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001239 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001240}
1241
1242// Test if the size of the packet buffer reported correctly when containing
1243// sync packets. Also, test if network packets override sync packets. That is to
1244// prefer decoding a network packet to a sync packet, if both have same sequence
1245// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001246TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001247 WebRtcRTPHeader rtp_info;
1248 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001249 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001250 uint8_t payload[kPayloadBytes];
1251 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001252 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001253 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1254 }
1255 // Insert some packets which decode to noise. We are not interested in
1256 // actual decoded values.
1257 NetEqOutputType output_type;
1258 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001259 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001260 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001261 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1262 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001263 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001264 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1265 &samples_per_channel, &num_channels,
1266 &output_type));
1267 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1268 ASSERT_EQ(1, num_channels);
1269 rtp_info.header.sequenceNumber++;
1270 rtp_info.header.timestamp += kBlockSize16kHz;
1271 receive_timestamp += kBlockSize16kHz;
1272 }
1273 const int kNumSyncPackets = 10;
1274
1275 WebRtcRTPHeader first_sync_packet_rtp_info;
1276 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1277
1278 // Insert sync-packets, but no decoding.
1279 for (int n = 0; n < kNumSyncPackets; ++n) {
1280 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1281 rtp_info.header.sequenceNumber++;
1282 rtp_info.header.timestamp += kBlockSize16kHz;
1283 receive_timestamp += kBlockSize16kHz;
1284 }
1285 NetEqNetworkStatistics network_stats;
1286 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001287 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1288 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001289
1290 // Rewind |rtp_info| to that of the first sync packet.
1291 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1292
1293 // Insert.
1294 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001295 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001296 rtp_info.header.sequenceNumber++;
1297 rtp_info.header.timestamp += kBlockSize16kHz;
1298 receive_timestamp += kBlockSize16kHz;
1299 }
1300
1301 // Decode.
1302 for (int n = 0; n < kNumSyncPackets; ++n) {
1303 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1304 &samples_per_channel, &num_channels,
1305 &output_type));
1306 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1307 ASSERT_EQ(1, num_channels);
1308 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1309 }
1310}
1311
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001312void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1313 uint32_t start_timestamp,
1314 const std::set<uint16_t>& drop_seq_numbers,
1315 bool expect_seq_no_wrap,
1316 bool expect_timestamp_wrap) {
1317 uint16_t seq_no = start_seq_no;
1318 uint32_t timestamp = start_timestamp;
1319 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1320 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1321 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001322 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001323 double next_input_time_ms = 0.0;
1324 int16_t decoded[kBlockSize16kHz];
1325 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001326 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001327 NetEqOutputType output_type;
1328 uint32_t receive_timestamp = 0;
1329
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001330 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001331 const int kSpeechDurationMs = 2000;
1332 int packets_inserted = 0;
1333 uint16_t last_seq_no;
1334 uint32_t last_timestamp;
1335 bool timestamp_wrapped = false;
1336 bool seq_no_wrapped = false;
1337 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1338 // Each turn in this for loop is 10 ms.
1339 while (next_input_time_ms <= t_ms) {
1340 // Insert one 30 ms speech frame.
1341 uint8_t payload[kPayloadBytes] = {0};
1342 WebRtcRTPHeader rtp_info;
1343 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1344 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1345 // This sequence number was not in the set to drop. Insert it.
1346 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001347 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001348 ++packets_inserted;
1349 }
1350 NetEqNetworkStatistics network_stats;
1351 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1352
1353 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1354 // packet size for first few packets. Therefore we refrain from checking
1355 // the criteria.
1356 if (packets_inserted > 4) {
1357 // Expect preferred and actual buffer size to be no more than 2 frames.
1358 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001359 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1360 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001361 }
1362 last_seq_no = seq_no;
1363 last_timestamp = timestamp;
1364
1365 ++seq_no;
1366 timestamp += kSamples;
1367 receive_timestamp += kSamples;
1368 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1369
1370 seq_no_wrapped |= seq_no < last_seq_no;
1371 timestamp_wrapped |= timestamp < last_timestamp;
1372 }
1373 // Pull out data once.
1374 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1375 &samples_per_channel, &num_channels,
1376 &output_type));
1377 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1378 ASSERT_EQ(1, num_channels);
1379
1380 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001381 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001382 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001383 }
1384 // Make sure we have actually tested wrap-around.
1385 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1386 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1387}
1388
1389TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1390 // Start with a sequence number that will soon wrap.
1391 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1392 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1393}
1394
1395TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1396 // Start with a sequence number that will soon wrap.
1397 std::set<uint16_t> drop_seq_numbers;
1398 drop_seq_numbers.insert(0xFFFF);
1399 drop_seq_numbers.insert(0x0);
1400 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1401}
1402
1403TEST_F(NetEqDecodingTest, TimestampWrap) {
1404 // Start with a timestamp that will soon wrap.
1405 std::set<uint16_t> drop_seq_numbers;
1406 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1407}
1408
1409TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1410 // Start with a timestamp and a sequence number that will wrap at the same
1411 // time.
1412 std::set<uint16_t> drop_seq_numbers;
1413 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1414}
1415
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001416void NetEqDecodingTest::DuplicateCng() {
1417 uint16_t seq_no = 0;
1418 uint32_t timestamp = 0;
1419 const int kFrameSizeMs = 10;
1420 const int kSampleRateKhz = 16;
1421 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001422 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001423
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001424 const int algorithmic_delay_samples = std::max(
1425 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001426 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001427 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001428 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001429 int num_channels;
1430 NetEqOutputType type;
1431 uint8_t payload[kPayloadBytes] = {0};
1432 WebRtcRTPHeader rtp_info;
1433 for (int i = 0; i < 3; ++i) {
1434 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001435 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001436 ++seq_no;
1437 timestamp += kSamples;
1438
1439 // Pull audio once.
1440 ASSERT_EQ(0,
1441 neteq_->GetAudio(
1442 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1443 ASSERT_EQ(kBlockSize16kHz, out_len);
1444 }
1445 // Verify speech output.
1446 EXPECT_EQ(kOutputNormal, type);
1447
1448 // Insert same CNG packet twice.
1449 const int kCngPeriodMs = 100;
1450 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001451 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001452 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1453 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001454 ASSERT_EQ(
1455 0, neteq_->InsertPacket(
1456 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001457
1458 // Pull audio once and make sure CNG is played.
1459 ASSERT_EQ(0,
1460 neteq_->GetAudio(
1461 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1462 ASSERT_EQ(kBlockSize16kHz, out_len);
1463 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001464 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001465
1466 // Insert the same CNG packet again. Note that at this point it is old, since
1467 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001468 ASSERT_EQ(
1469 0, neteq_->InsertPacket(
1470 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001471
1472 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1473 // we have already pulled out CNG once.
1474 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1475 ASSERT_EQ(0,
1476 neteq_->GetAudio(
1477 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1478 ASSERT_EQ(kBlockSize16kHz, out_len);
1479 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001480 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001481 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001482 }
1483
1484 // Insert speech again.
1485 ++seq_no;
1486 timestamp += kCngPeriodSamples;
1487 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001488 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001489
1490 // Pull audio once and verify that the output is speech again.
1491 ASSERT_EQ(0,
1492 neteq_->GetAudio(
1493 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1494 ASSERT_EQ(kBlockSize16kHz, out_len);
1495 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001496 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001497 PlayoutTimestamp());
1498}
1499
1500uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1501 uint32_t playout_timestamp = 0;
1502 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1503 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001504}
1505
1506TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001507
1508TEST_F(NetEqDecodingTest, CngFirst) {
1509 uint16_t seq_no = 0;
1510 uint32_t timestamp = 0;
1511 const int kFrameSizeMs = 10;
1512 const int kSampleRateKhz = 16;
1513 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1514 const int kPayloadBytes = kSamples * 2;
1515 const int kCngPeriodMs = 100;
1516 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1517 size_t payload_len;
1518
1519 uint8_t payload[kPayloadBytes] = {0};
1520 WebRtcRTPHeader rtp_info;
1521
1522 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001523 ASSERT_EQ(
1524 NetEq::kOK,
1525 neteq_->InsertPacket(
1526 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001527 ++seq_no;
1528 timestamp += kCngPeriodSamples;
1529
1530 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001531 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001532 int num_channels;
1533 NetEqOutputType type;
1534 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1535 &num_channels, &type));
1536 ASSERT_EQ(kBlockSize16kHz, out_len);
1537 EXPECT_EQ(kOutputCNG, type);
1538
1539 // Insert some speech packets.
1540 for (int i = 0; i < 3; ++i) {
1541 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001542 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001543 ++seq_no;
1544 timestamp += kSamples;
1545
1546 // Pull audio once.
1547 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1548 &num_channels, &type));
1549 ASSERT_EQ(kBlockSize16kHz, out_len);
1550 }
1551 // Verify speech output.
1552 EXPECT_EQ(kOutputNormal, type);
1553}
1554
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001555} // namespace webrtc