blob: 6f47dd1c06135486de67d52bb683e73eec922bf2 [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"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000031#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000033#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/typedefs.h"
35
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000036DEFINE_bool(gen_ref, false, "Generate reference files.");
37
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038namespace webrtc {
39
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.
kwibergee1879c2015-10-29 06:20:28 -0700307 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 // Load PCMa.
kwibergee1879c2015-10-29 06:20:28 -0700309 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700310#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 // Load iLBC.
kwibergee1879c2015-10-29 06:20:28 -0700312 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700313#endif
314#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 // Load iSAC.
kwibergee1879c2015-10-29 06:20:28 -0700316 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700317#endif
318#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319 // Load iSAC SWB.
kwibergee1879c2015-10-29 06:20:28 -0700320 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb, 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700321#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 // Load PCM16B nb.
kwibergee1879c2015-10-29 06:20:28 -0700323 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B, 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324 // Load PCM16B wb.
kwibergee1879c2015-10-29 06:20:28 -0700325 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb, 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 // Load PCM16B swb32.
kwibergee1879c2015-10-29 06:20:28 -0700327 ASSERT_EQ(
328 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz, 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329 // Load CNG 8 kHz.
kwibergee1879c2015-10-29 06:20:28 -0700330 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb, 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 // Load CNG 16 kHz.
kwibergee1879c2015-10-29 06:20:28 -0700332 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb, 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333}
334
335void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000336 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337}
338
Peter Kastingdce40cf2015-08-24 14:52:23 -0700339void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000341 while (packet_ && sim_clock_ >= packet_->time_ms()) {
342 if (packet_->payload_length_bytes() > 0) {
343 WebRtcRTPHeader rtp_header;
344 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000346 rtp_header, packet_->payload(),
347 packet_->payload_length_bytes(),
Peter Kastingb7e50542015-06-11 12:55:50 -0700348 static_cast<uint32_t>(
349 packet_->time_ms() * (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 }
351 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000352 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 }
354
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000355 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 NetEqOutputType type;
357 int num_channels;
358 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
359 &num_channels, &type));
360 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
361 (*out_len == kBlockSize16kHz) ||
362 (*out_len == kBlockSize32kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700363 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
365 // Increase time.
366 sim_clock_ += kTimeStepMs;
367}
368
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000369void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
370 const std::string& ref_file,
371 const std::string& stat_ref_file,
372 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 OpenInputFile(rtp_file);
374
375 std::string ref_out_file = "";
376 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000377 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 }
379 RefFiles ref_files(ref_file, ref_out_file);
380
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000381 std::string stat_out_file = "";
382 if (stat_ref_file.empty()) {
383 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
384 }
385 RefFiles network_stat_files(stat_ref_file, stat_out_file);
386
387 std::string rtcp_out_file = "";
388 if (rtcp_ref_file.empty()) {
389 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
390 }
391 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
392
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000393 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000395 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396 std::ostringstream ss;
397 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
398 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700399 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000400 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000402
403 // Query the network statistics API once per second
404 if (sim_clock_ % 1000 == 0) {
405 // Process NetworkStatistics.
406 NetEqNetworkStatistics network_stats;
407 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000408 ASSERT_NO_FATAL_FAILURE(
409 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700410 // Compare with CurrentDelay, which should be identical.
411 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000412
413 // Process RTCPstat.
414 RtcpStatistics rtcp_stats;
415 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000416 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000417 }
418 }
419}
420
421void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
422 int timestamp,
423 WebRtcRTPHeader* rtp_info) {
424 rtp_info->header.sequenceNumber = frame_index;
425 rtp_info->header.timestamp = timestamp;
426 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
427 rtp_info->header.payloadType = 94; // PCM16b WB codec.
428 rtp_info->header.markerBit = 0;
429}
430
431void NetEqDecodingTest::PopulateCng(int frame_index,
432 int timestamp,
433 WebRtcRTPHeader* rtp_info,
434 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000435 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000436 rtp_info->header.sequenceNumber = frame_index;
437 rtp_info->header.timestamp = timestamp;
438 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
439 rtp_info->header.payloadType = 98; // WB CNG.
440 rtp_info->header.markerBit = 0;
441 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
442 *payload_len = 1; // Only noise level, no spectral parameters.
443}
444
kwiberg98ab3a42015-09-30 21:54:21 -0700445#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISAC)) && \
446 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
447#define IF_ALL_CODECS(x) x
448#else
449#define IF_ALL_CODECS(x) DISABLED_##x
450#endif
451
henrikaa2c79402015-06-10 13:24:48 +0200452TEST_F(NetEqDecodingTest,
kwiberg98ab3a42015-09-30 21:54:21 -0700453 DISABLED_ON_IOS(DISABLED_ON_ANDROID(IF_ALL_CODECS(TestBitExactness)))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000454 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000455 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000456 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
457 // are identical. The latter could have been removed, but if clients still
458 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000459 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000460 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000461#if defined(_MSC_VER) && (_MSC_VER >= 1700)
462 // For Visual Studio 2012 and later, we will have to use the generic reference
463 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000464 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000465 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000466#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000467 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000468 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000469#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000470 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000471 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000472
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000473 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000474 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000475 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000476 DecodeAndCompare(input_rtp_file,
477 input_ref_file,
478 network_stat_ref_file,
479 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000480 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000481}
482
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000483// Use fax mode to avoid time-scaling. This is to simplify the testing of
484// packet waiting times in the packet buffer.
485class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
486 protected:
487 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
488 config_.playout_mode = kPlayoutFax;
489 }
490};
491
492TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000493 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
494 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000495 const size_t kSamples = 10 * 16;
496 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000497 for (size_t i = 0; i < num_frames; ++i) {
498 uint16_t payload[kSamples] = {0};
499 WebRtcRTPHeader rtp_info;
500 rtp_info.header.sequenceNumber = i;
501 rtp_info.header.timestamp = i * kSamples;
502 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
503 rtp_info.header.payloadType = 94; // PCM16b WB codec.
504 rtp_info.header.markerBit = 0;
505 ASSERT_EQ(0, neteq_->InsertPacket(
506 rtp_info,
507 reinterpret_cast<uint8_t*>(payload),
508 kPayloadBytes, 0));
509 }
510 // Pull out all data.
511 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700512 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000513 int num_channels;
514 NetEqOutputType type;
515 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
516 &num_channels, &type));
517 ASSERT_EQ(kBlockSize16kHz, out_len);
518 }
519
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200520 NetEqNetworkStatistics stats;
521 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000522 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
523 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200524 // each packet. Thus, we are calculating the statistics for a series from 10
525 // to 300, in steps of 10 ms.
526 EXPECT_EQ(155, stats.mean_waiting_time_ms);
527 EXPECT_EQ(155, stats.median_waiting_time_ms);
528 EXPECT_EQ(10, stats.min_waiting_time_ms);
529 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000530
531 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200532 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
533 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
534 EXPECT_EQ(-1, stats.median_waiting_time_ms);
535 EXPECT_EQ(-1, stats.min_waiting_time_ms);
536 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000537}
538
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000539TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000540 const int kNumFrames = 3000; // Needed for convergence.
541 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000542 const size_t kSamples = 10 * 16;
543 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000544 while (frame_index < kNumFrames) {
545 // Insert one packet each time, except every 10th time where we insert two
546 // packets at once. This will create a negative clock-drift of approx. 10%.
547 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
548 for (int n = 0; n < num_packets; ++n) {
549 uint8_t payload[kPayloadBytes] = {0};
550 WebRtcRTPHeader rtp_info;
551 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
552 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
553 ++frame_index;
554 }
555
556 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700557 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 int num_channels;
559 NetEqOutputType type;
560 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
561 &num_channels, &type));
562 ASSERT_EQ(kBlockSize16kHz, out_len);
563 }
564
565 NetEqNetworkStatistics network_stats;
566 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
567 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
568}
569
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000570TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000571 const int kNumFrames = 5000; // Needed for convergence.
572 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000573 const size_t kSamples = 10 * 16;
574 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000575 for (int i = 0; i < kNumFrames; ++i) {
576 // Insert one packet each time, except every 10th time where we don't insert
577 // any packet. This will create a positive clock-drift of approx. 11%.
578 int num_packets = (i % 10 == 9 ? 0 : 1);
579 for (int n = 0; n < num_packets; ++n) {
580 uint8_t payload[kPayloadBytes] = {0};
581 WebRtcRTPHeader rtp_info;
582 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
583 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
584 ++frame_index;
585 }
586
587 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700588 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 int num_channels;
590 NetEqOutputType type;
591 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
592 &num_channels, &type));
593 ASSERT_EQ(kBlockSize16kHz, out_len);
594 }
595
596 NetEqNetworkStatistics network_stats;
597 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
598 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
599}
600
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000601void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
602 double network_freeze_ms,
603 bool pull_audio_during_freeze,
604 int delay_tolerance_ms,
605 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606 uint16_t seq_no = 0;
607 uint32_t timestamp = 0;
608 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000609 const size_t kSamples = kFrameSizeMs * 16;
610 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000611 double next_input_time_ms = 0.0;
612 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700613 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000614 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000615 NetEqOutputType type;
616
617 // Insert speech for 5 seconds.
618 const int kSpeechDurationMs = 5000;
619 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
620 // Each turn in this for loop is 10 ms.
621 while (next_input_time_ms <= t_ms) {
622 // Insert one 30 ms speech frame.
623 uint8_t payload[kPayloadBytes] = {0};
624 WebRtcRTPHeader rtp_info;
625 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
626 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
627 ++seq_no;
628 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000629 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000630 }
631 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
633 &num_channels, &type));
634 ASSERT_EQ(kBlockSize16kHz, out_len);
635 }
636
637 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000638 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639
640 // Insert CNG for 1 minute (= 60000 ms).
641 const int kCngPeriodMs = 100;
642 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
643 const int kCngDurationMs = 60000;
644 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
645 // Each turn in this for loop is 10 ms.
646 while (next_input_time_ms <= t_ms) {
647 // Insert one CNG frame each 100 ms.
648 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000649 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650 WebRtcRTPHeader rtp_info;
651 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
652 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
653 ++seq_no;
654 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000655 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000656 }
657 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000658 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
659 &num_channels, &type));
660 ASSERT_EQ(kBlockSize16kHz, out_len);
661 }
662
663 EXPECT_EQ(kOutputCNG, type);
664
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000665 if (network_freeze_ms > 0) {
666 // First keep pulling audio for |network_freeze_ms| without inserting
667 // any data, then insert CNG data corresponding to |network_freeze_ms|
668 // without pulling any output audio.
669 const double loop_end_time = t_ms + network_freeze_ms;
670 for (; t_ms < loop_end_time; t_ms += 10) {
671 // Pull out data once.
672 ASSERT_EQ(0,
673 neteq_->GetAudio(
674 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
675 ASSERT_EQ(kBlockSize16kHz, out_len);
676 EXPECT_EQ(kOutputCNG, type);
677 }
678 bool pull_once = pull_audio_during_freeze;
679 // If |pull_once| is true, GetAudio will be called once half-way through
680 // the network recovery period.
681 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
682 while (next_input_time_ms <= t_ms) {
683 if (pull_once && next_input_time_ms >= pull_time_ms) {
684 pull_once = false;
685 // Pull out data once.
686 ASSERT_EQ(
687 0,
688 neteq_->GetAudio(
689 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
690 ASSERT_EQ(kBlockSize16kHz, out_len);
691 EXPECT_EQ(kOutputCNG, type);
692 t_ms += 10;
693 }
694 // Insert one CNG frame each 100 ms.
695 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000696 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000697 WebRtcRTPHeader rtp_info;
698 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
699 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
700 ++seq_no;
701 timestamp += kCngPeriodSamples;
702 next_input_time_ms += kCngPeriodMs * drift_factor;
703 }
704 }
705
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000706 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000707 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000708 while (type != kOutputNormal) {
709 // Each turn in this for loop is 10 ms.
710 while (next_input_time_ms <= t_ms) {
711 // Insert one 30 ms speech frame.
712 uint8_t payload[kPayloadBytes] = {0};
713 WebRtcRTPHeader rtp_info;
714 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
715 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
716 ++seq_no;
717 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000718 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000719 }
720 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000721 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
722 &num_channels, &type));
723 ASSERT_EQ(kBlockSize16kHz, out_len);
724 // Increase clock.
725 t_ms += 10;
726 }
727
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000728 // Check that the speech starts again within reasonable time.
729 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
730 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000731 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000732 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000733 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
734 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000735}
736
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000737TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000738 // Apply a clock drift of -25 ms / s (sender faster than receiver).
739 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 const double kNetworkFreezeTimeMs = 0.0;
741 const bool kGetAudioDuringFreezeRecovery = false;
742 const int kDelayToleranceMs = 20;
743 const int kMaxTimeToSpeechMs = 100;
744 LongCngWithClockDrift(kDriftFactor,
745 kNetworkFreezeTimeMs,
746 kGetAudioDuringFreezeRecovery,
747 kDelayToleranceMs,
748 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000749}
750
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000751TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000752 // Apply a clock drift of +25 ms / s (sender slower than receiver).
753 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000754 const double kNetworkFreezeTimeMs = 0.0;
755 const bool kGetAudioDuringFreezeRecovery = false;
756 const int kDelayToleranceMs = 20;
757 const int kMaxTimeToSpeechMs = 100;
758 LongCngWithClockDrift(kDriftFactor,
759 kNetworkFreezeTimeMs,
760 kGetAudioDuringFreezeRecovery,
761 kDelayToleranceMs,
762 kMaxTimeToSpeechMs);
763}
764
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000765TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000766 // Apply a clock drift of -25 ms / s (sender faster than receiver).
767 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
768 const double kNetworkFreezeTimeMs = 5000.0;
769 const bool kGetAudioDuringFreezeRecovery = false;
770 const int kDelayToleranceMs = 50;
771 const int kMaxTimeToSpeechMs = 200;
772 LongCngWithClockDrift(kDriftFactor,
773 kNetworkFreezeTimeMs,
774 kGetAudioDuringFreezeRecovery,
775 kDelayToleranceMs,
776 kMaxTimeToSpeechMs);
777}
778
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000779TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000780 // Apply a clock drift of +25 ms / s (sender slower than receiver).
781 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
782 const double kNetworkFreezeTimeMs = 5000.0;
783 const bool kGetAudioDuringFreezeRecovery = false;
784 const int kDelayToleranceMs = 20;
785 const int kMaxTimeToSpeechMs = 100;
786 LongCngWithClockDrift(kDriftFactor,
787 kNetworkFreezeTimeMs,
788 kGetAudioDuringFreezeRecovery,
789 kDelayToleranceMs,
790 kMaxTimeToSpeechMs);
791}
792
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000793TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000794 // Apply a clock drift of +25 ms / s (sender slower than receiver).
795 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
796 const double kNetworkFreezeTimeMs = 5000.0;
797 const bool kGetAudioDuringFreezeRecovery = true;
798 const int kDelayToleranceMs = 20;
799 const int kMaxTimeToSpeechMs = 100;
800 LongCngWithClockDrift(kDriftFactor,
801 kNetworkFreezeTimeMs,
802 kGetAudioDuringFreezeRecovery,
803 kDelayToleranceMs,
804 kMaxTimeToSpeechMs);
805}
806
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000807TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000808 const double kDriftFactor = 1.0; // No drift.
809 const double kNetworkFreezeTimeMs = 0.0;
810 const bool kGetAudioDuringFreezeRecovery = false;
811 const int kDelayToleranceMs = 10;
812 const int kMaxTimeToSpeechMs = 50;
813 LongCngWithClockDrift(kDriftFactor,
814 kNetworkFreezeTimeMs,
815 kGetAudioDuringFreezeRecovery,
816 kDelayToleranceMs,
817 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000818}
819
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000820TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000821 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000822 uint8_t payload[kPayloadBytes] = {0};
823 WebRtcRTPHeader rtp_info;
824 PopulateRtpInfo(0, 0, &rtp_info);
825 rtp_info.header.payloadType = 1; // Not registered as a decoder.
826 EXPECT_EQ(NetEq::kFail,
827 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
828 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
829}
830
kwiberg98ab3a42015-09-30 21:54:21 -0700831#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
832#define IF_ISAC(x) x
833#else
834#define IF_ISAC(x) DISABLED_##x
835#endif
836
837TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000838 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000839 uint8_t payload[kPayloadBytes] = {0};
840 WebRtcRTPHeader rtp_info;
841 PopulateRtpInfo(0, 0, &rtp_info);
842 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
843 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
844 NetEqOutputType type;
845 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
846 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000847 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000848 out_data_[i] = 1;
849 }
850 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700851 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000852 EXPECT_EQ(NetEq::kFail,
853 neteq_->GetAudio(kMaxBlockSize, out_data_,
854 &samples_per_channel, &num_channels, &type));
855 // Verify that there is a decoder error to check.
856 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
857 // Code 6730 is an iSAC error code.
858 EXPECT_EQ(6730, neteq_->LastDecoderError());
859 // Verify that the first 160 samples are set to 0, and that the remaining
860 // samples are left unmodified.
861 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
862 for (int i = 0; i < kExpectedOutputLength; ++i) {
863 std::ostringstream ss;
864 ss << "i = " << i;
865 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
866 EXPECT_EQ(0, out_data_[i]);
867 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000868 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000869 std::ostringstream ss;
870 ss << "i = " << i;
871 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
872 EXPECT_EQ(1, out_data_[i]);
873 }
874}
875
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000876TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000877 NetEqOutputType type;
878 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
879 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000880 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000881 out_data_[i] = 1;
882 }
883 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700884 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000885 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
886 &samples_per_channel,
887 &num_channels, &type));
888 // Verify that the first block of samples is set to 0.
889 static const int kExpectedOutputLength =
890 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
891 for (int i = 0; i < kExpectedOutputLength; ++i) {
892 std::ostringstream ss;
893 ss << "i = " << i;
894 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
895 EXPECT_EQ(0, out_data_[i]);
896 }
897}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000898
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000899class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000900 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000901 virtual void TestCondition(double sum_squared_noise,
902 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000903
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000904 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700905 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000906 uint8_t payload_type = 0xFF; // Invalid.
907 if (sampling_rate_hz == 8000) {
908 expected_samples_per_channel = kBlockSize8kHz;
909 payload_type = 93; // PCM 16, 8 kHz.
910 } else if (sampling_rate_hz == 16000) {
911 expected_samples_per_channel = kBlockSize16kHz;
912 payload_type = 94; // PCM 16, 16 kHZ.
913 } else if (sampling_rate_hz == 32000) {
914 expected_samples_per_channel = kBlockSize32kHz;
915 payload_type = 95; // PCM 16, 32 kHz.
916 } else {
917 ASSERT_TRUE(false); // Unsupported test case.
918 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000919
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000920 NetEqOutputType type;
921 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000922 test::AudioLoop input;
923 // We are using the same 32 kHz input file for all tests, regardless of
924 // |sampling_rate_hz|. The output may sound weird, but the test is still
925 // valid.
926 ASSERT_TRUE(input.Init(
927 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
928 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700929 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000930
931 // Payload of 10 ms of PCM16 32 kHz.
932 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000933 WebRtcRTPHeader rtp_info;
934 PopulateRtpInfo(0, 0, &rtp_info);
935 rtp_info.header.payloadType = payload_type;
936
937 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700938 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000939
940 uint32_t receive_timestamp = 0;
941 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800942 auto block = input.GetNextBlock();
943 ASSERT_EQ(expected_samples_per_channel, block.size());
944 size_t enc_len_bytes =
945 WebRtcPcm16b_Encode(block.data(), block.size(), 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,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700951 neteq_->InsertPacket(rtp_info, payload, enc_len_bytes,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000952 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000953 ASSERT_EQ(0,
954 neteq_->GetAudio(kBlockSize32kHz,
955 output,
956 &samples_per_channel,
957 &number_channels,
958 &type));
959 ASSERT_EQ(1, number_channels);
960 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
961 ASSERT_EQ(kOutputNormal, type);
962
963 // Next packet.
964 rtp_info.header.timestamp += expected_samples_per_channel;
965 rtp_info.header.sequenceNumber++;
966 receive_timestamp += expected_samples_per_channel;
967 }
968
969 number_channels = 0;
970 samples_per_channel = 0;
971
972 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
973 // one frame without checking speech-type. This is the first frame pulled
974 // without inserting any packet, and might not be labeled as PLC.
975 ASSERT_EQ(0,
976 neteq_->GetAudio(kBlockSize32kHz,
977 output,
978 &samples_per_channel,
979 &number_channels,
980 &type));
981 ASSERT_EQ(1, number_channels);
982 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
983
984 // To be able to test the fading of background noise we need at lease to
985 // pull 611 frames.
986 const int kFadingThreshold = 611;
987
988 // Test several CNG-to-PLC packet for the expected behavior. The number 20
989 // is arbitrary, but sufficiently large to test enough number of frames.
990 const int kNumPlcToCngTestFrames = 20;
991 bool plc_to_cng = false;
992 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
993 number_channels = 0;
994 samples_per_channel = 0;
995 memset(output, 1, sizeof(output)); // Set to non-zero.
996 ASSERT_EQ(0,
997 neteq_->GetAudio(kBlockSize32kHz,
998 output,
999 &samples_per_channel,
1000 &number_channels,
1001 &type));
1002 ASSERT_EQ(1, number_channels);
1003 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1004 if (type == kOutputPLCtoCNG) {
1005 plc_to_cng = true;
1006 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001007 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001008 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001009 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001010 } else {
1011 EXPECT_EQ(kOutputPLC, type);
1012 }
1013 }
1014 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1015 }
1016};
1017
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001018class NetEqBgnTestOn : public NetEqBgnTest {
1019 protected:
1020 NetEqBgnTestOn() : NetEqBgnTest() {
1021 config_.background_noise_mode = NetEq::kBgnOn;
1022 }
1023
1024 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1025 EXPECT_NE(0, sum_squared_noise);
1026 }
1027};
1028
1029class NetEqBgnTestOff : public NetEqBgnTest {
1030 protected:
1031 NetEqBgnTestOff() : NetEqBgnTest() {
1032 config_.background_noise_mode = NetEq::kBgnOff;
1033 }
1034
1035 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1036 EXPECT_EQ(0, sum_squared_noise);
1037 }
1038};
1039
1040class NetEqBgnTestFade : public NetEqBgnTest {
1041 protected:
1042 NetEqBgnTestFade() : NetEqBgnTest() {
1043 config_.background_noise_mode = NetEq::kBgnFade;
1044 }
1045
1046 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1047 if (should_be_faded)
1048 EXPECT_EQ(0, sum_squared_noise);
1049 }
1050};
1051
henrika1d34fe92015-06-16 10:04:20 +02001052TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001053 CheckBgn(8000);
1054 CheckBgn(16000);
1055 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001056}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001057
henrika1d34fe92015-06-16 10:04:20 +02001058TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001059 CheckBgn(8000);
1060 CheckBgn(16000);
1061 CheckBgn(32000);
1062}
1063
henrika1d34fe92015-06-16 10:04:20 +02001064TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001065 CheckBgn(8000);
1066 CheckBgn(16000);
1067 CheckBgn(32000);
1068}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001069
kwiberg98ab3a42015-09-30 21:54:21 -07001070TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001071 WebRtcRTPHeader rtp_info;
1072 uint32_t receive_timestamp = 0;
1073 // For the readability use the following payloads instead of the defaults of
1074 // this test.
1075 uint8_t kPcm16WbPayloadType = 1;
1076 uint8_t kCngNbPayloadType = 2;
1077 uint8_t kCngWbPayloadType = 3;
1078 uint8_t kCngSwb32PayloadType = 4;
1079 uint8_t kCngSwb48PayloadType = 5;
1080 uint8_t kAvtPayloadType = 6;
1081 uint8_t kRedPayloadType = 7;
1082 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1083
1084 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001085 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001086 kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001087 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
1088 kCngNbPayloadType));
1089 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
1090 kCngWbPayloadType));
1091 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001092 kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001093 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001094 kCngSwb48PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001095 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT,
1096 kAvtPayloadType));
1097 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED,
1098 kRedPayloadType));
1099 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC,
1100 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001101
1102 PopulateRtpInfo(0, 0, &rtp_info);
1103 rtp_info.header.payloadType = kPcm16WbPayloadType;
1104
1105 // The first packet injected cannot be sync-packet.
1106 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1107
1108 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001109 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001110 uint8_t payload[kPayloadBytes] = {0};
1111 ASSERT_EQ(0, neteq_->InsertPacket(
1112 rtp_info, payload, kPayloadBytes, receive_timestamp));
1113
1114 // Next packet. Last packet contained 10 ms audio.
1115 rtp_info.header.sequenceNumber++;
1116 rtp_info.header.timestamp += kBlockSize16kHz;
1117 receive_timestamp += kBlockSize16kHz;
1118
1119 // Unacceptable payload types CNG, AVT (DTMF), RED.
1120 rtp_info.header.payloadType = kCngNbPayloadType;
1121 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1122
1123 rtp_info.header.payloadType = kCngWbPayloadType;
1124 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1125
1126 rtp_info.header.payloadType = kCngSwb32PayloadType;
1127 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1128
1129 rtp_info.header.payloadType = kCngSwb48PayloadType;
1130 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1131
1132 rtp_info.header.payloadType = kAvtPayloadType;
1133 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1134
1135 rtp_info.header.payloadType = kRedPayloadType;
1136 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1137
1138 // Change of codec cannot be initiated with a sync packet.
1139 rtp_info.header.payloadType = kIsacPayloadType;
1140 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1141
1142 // Change of SSRC is not allowed with a sync packet.
1143 rtp_info.header.payloadType = kPcm16WbPayloadType;
1144 ++rtp_info.header.ssrc;
1145 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1146
1147 --rtp_info.header.ssrc;
1148 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1149}
1150
1151// First insert several noise like packets, then sync-packets. Decoding all
1152// packets should not produce error, statistics should not show any packet loss
1153// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001154// TODO(turajs) we will have a better test if we have a referece NetEq, and
1155// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1156// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001157TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001158 WebRtcRTPHeader rtp_info;
1159 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001160 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001161 uint8_t payload[kPayloadBytes];
1162 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001163 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001164 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001165 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1166 }
1167 // Insert some packets which decode to noise. We are not interested in
1168 // actual decoded values.
1169 NetEqOutputType output_type;
1170 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001171 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001172 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001173 for (int n = 0; n < 100; ++n) {
1174 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1175 receive_timestamp));
1176 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1177 &samples_per_channel, &num_channels,
1178 &output_type));
1179 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1180 ASSERT_EQ(1, num_channels);
1181
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001182 rtp_info.header.sequenceNumber++;
1183 rtp_info.header.timestamp += kBlockSize16kHz;
1184 receive_timestamp += kBlockSize16kHz;
1185 }
1186 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001187
1188 // Make sure sufficient number of sync packets are inserted that we can
1189 // conduct a test.
1190 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001191 // Insert sync-packets, the decoded sequence should be all-zero.
1192 for (int n = 0; n < kNumSyncPackets; ++n) {
1193 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1194 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1195 &samples_per_channel, &num_channels,
1196 &output_type));
1197 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1198 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001199 if (n > algorithmic_frame_delay) {
1200 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1201 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001202 rtp_info.header.sequenceNumber++;
1203 rtp_info.header.timestamp += kBlockSize16kHz;
1204 receive_timestamp += kBlockSize16kHz;
1205 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001206
1207 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001208 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001209 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1210 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1211 receive_timestamp));
1212 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1213 &samples_per_channel, &num_channels,
1214 &output_type));
1215 if (n >= algorithmic_frame_delay + 1) {
1216 // Expect that this frame contain samples from regular RTP.
1217 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1218 }
1219 rtp_info.header.sequenceNumber++;
1220 rtp_info.header.timestamp += kBlockSize16kHz;
1221 receive_timestamp += kBlockSize16kHz;
1222 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001223 NetEqNetworkStatistics network_stats;
1224 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1225 // Expecting a "clean" network.
1226 EXPECT_EQ(0, network_stats.packet_loss_rate);
1227 EXPECT_EQ(0, network_stats.expand_rate);
1228 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001229 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001230}
1231
1232// Test if the size of the packet buffer reported correctly when containing
1233// sync packets. Also, test if network packets override sync packets. That is to
1234// prefer decoding a network packet to a sync packet, if both have same sequence
1235// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001236TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001237 WebRtcRTPHeader rtp_info;
1238 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001239 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001240 uint8_t payload[kPayloadBytes];
1241 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001242 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001243 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1244 }
1245 // Insert some packets which decode to noise. We are not interested in
1246 // actual decoded values.
1247 NetEqOutputType output_type;
1248 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001249 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001250 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001251 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1252 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001253 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1254 receive_timestamp));
1255 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1256 &samples_per_channel, &num_channels,
1257 &output_type));
1258 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1259 ASSERT_EQ(1, num_channels);
1260 rtp_info.header.sequenceNumber++;
1261 rtp_info.header.timestamp += kBlockSize16kHz;
1262 receive_timestamp += kBlockSize16kHz;
1263 }
1264 const int kNumSyncPackets = 10;
1265
1266 WebRtcRTPHeader first_sync_packet_rtp_info;
1267 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1268
1269 // Insert sync-packets, but no decoding.
1270 for (int n = 0; n < kNumSyncPackets; ++n) {
1271 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1272 rtp_info.header.sequenceNumber++;
1273 rtp_info.header.timestamp += kBlockSize16kHz;
1274 receive_timestamp += kBlockSize16kHz;
1275 }
1276 NetEqNetworkStatistics network_stats;
1277 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001278 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1279 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001280
1281 // Rewind |rtp_info| to that of the first sync packet.
1282 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1283
1284 // Insert.
1285 for (int n = 0; n < kNumSyncPackets; ++n) {
1286 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1287 receive_timestamp));
1288 rtp_info.header.sequenceNumber++;
1289 rtp_info.header.timestamp += kBlockSize16kHz;
1290 receive_timestamp += kBlockSize16kHz;
1291 }
1292
1293 // Decode.
1294 for (int n = 0; n < kNumSyncPackets; ++n) {
1295 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1296 &samples_per_channel, &num_channels,
1297 &output_type));
1298 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1299 ASSERT_EQ(1, num_channels);
1300 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1301 }
1302}
1303
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001304void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1305 uint32_t start_timestamp,
1306 const std::set<uint16_t>& drop_seq_numbers,
1307 bool expect_seq_no_wrap,
1308 bool expect_timestamp_wrap) {
1309 uint16_t seq_no = start_seq_no;
1310 uint32_t timestamp = start_timestamp;
1311 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1312 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1313 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001314 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001315 double next_input_time_ms = 0.0;
1316 int16_t decoded[kBlockSize16kHz];
1317 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001318 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001319 NetEqOutputType output_type;
1320 uint32_t receive_timestamp = 0;
1321
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001322 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001323 const int kSpeechDurationMs = 2000;
1324 int packets_inserted = 0;
1325 uint16_t last_seq_no;
1326 uint32_t last_timestamp;
1327 bool timestamp_wrapped = false;
1328 bool seq_no_wrapped = false;
1329 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1330 // Each turn in this for loop is 10 ms.
1331 while (next_input_time_ms <= t_ms) {
1332 // Insert one 30 ms speech frame.
1333 uint8_t payload[kPayloadBytes] = {0};
1334 WebRtcRTPHeader rtp_info;
1335 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1336 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1337 // This sequence number was not in the set to drop. Insert it.
1338 ASSERT_EQ(0,
1339 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1340 receive_timestamp));
1341 ++packets_inserted;
1342 }
1343 NetEqNetworkStatistics network_stats;
1344 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1345
1346 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1347 // packet size for first few packets. Therefore we refrain from checking
1348 // the criteria.
1349 if (packets_inserted > 4) {
1350 // Expect preferred and actual buffer size to be no more than 2 frames.
1351 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001352 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1353 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001354 }
1355 last_seq_no = seq_no;
1356 last_timestamp = timestamp;
1357
1358 ++seq_no;
1359 timestamp += kSamples;
1360 receive_timestamp += kSamples;
1361 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1362
1363 seq_no_wrapped |= seq_no < last_seq_no;
1364 timestamp_wrapped |= timestamp < last_timestamp;
1365 }
1366 // Pull out data once.
1367 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1368 &samples_per_channel, &num_channels,
1369 &output_type));
1370 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1371 ASSERT_EQ(1, num_channels);
1372
1373 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001374 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001375 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001376 }
1377 // Make sure we have actually tested wrap-around.
1378 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1379 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1380}
1381
1382TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1383 // Start with a sequence number that will soon wrap.
1384 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1385 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1386}
1387
1388TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1389 // Start with a sequence number that will soon wrap.
1390 std::set<uint16_t> drop_seq_numbers;
1391 drop_seq_numbers.insert(0xFFFF);
1392 drop_seq_numbers.insert(0x0);
1393 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1394}
1395
1396TEST_F(NetEqDecodingTest, TimestampWrap) {
1397 // Start with a timestamp that will soon wrap.
1398 std::set<uint16_t> drop_seq_numbers;
1399 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1400}
1401
1402TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1403 // Start with a timestamp and a sequence number that will wrap at the same
1404 // time.
1405 std::set<uint16_t> drop_seq_numbers;
1406 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1407}
1408
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001409void NetEqDecodingTest::DuplicateCng() {
1410 uint16_t seq_no = 0;
1411 uint32_t timestamp = 0;
1412 const int kFrameSizeMs = 10;
1413 const int kSampleRateKhz = 16;
1414 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001415 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001416
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001417 const int algorithmic_delay_samples = std::max(
1418 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001419 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001420 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001421 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001422 int num_channels;
1423 NetEqOutputType type;
1424 uint8_t payload[kPayloadBytes] = {0};
1425 WebRtcRTPHeader rtp_info;
1426 for (int i = 0; i < 3; ++i) {
1427 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1428 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1429 ++seq_no;
1430 timestamp += kSamples;
1431
1432 // Pull audio once.
1433 ASSERT_EQ(0,
1434 neteq_->GetAudio(
1435 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1436 ASSERT_EQ(kBlockSize16kHz, out_len);
1437 }
1438 // Verify speech output.
1439 EXPECT_EQ(kOutputNormal, type);
1440
1441 // Insert same CNG packet twice.
1442 const int kCngPeriodMs = 100;
1443 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001444 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001445 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1446 // This is the first time this CNG packet is inserted.
1447 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1448
1449 // Pull audio once and make sure CNG is played.
1450 ASSERT_EQ(0,
1451 neteq_->GetAudio(
1452 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1453 ASSERT_EQ(kBlockSize16kHz, out_len);
1454 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001455 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001456
1457 // Insert the same CNG packet again. Note that at this point it is old, since
1458 // we have already decoded the first copy of it.
1459 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1460
1461 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1462 // we have already pulled out CNG once.
1463 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1464 ASSERT_EQ(0,
1465 neteq_->GetAudio(
1466 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1467 ASSERT_EQ(kBlockSize16kHz, out_len);
1468 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001469 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001470 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001471 }
1472
1473 // Insert speech again.
1474 ++seq_no;
1475 timestamp += kCngPeriodSamples;
1476 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1477 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1478
1479 // Pull audio once and verify that the output is speech again.
1480 ASSERT_EQ(0,
1481 neteq_->GetAudio(
1482 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1483 ASSERT_EQ(kBlockSize16kHz, out_len);
1484 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001485 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001486 PlayoutTimestamp());
1487}
1488
1489uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1490 uint32_t playout_timestamp = 0;
1491 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1492 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001493}
1494
1495TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001496
1497TEST_F(NetEqDecodingTest, CngFirst) {
1498 uint16_t seq_no = 0;
1499 uint32_t timestamp = 0;
1500 const int kFrameSizeMs = 10;
1501 const int kSampleRateKhz = 16;
1502 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1503 const int kPayloadBytes = kSamples * 2;
1504 const int kCngPeriodMs = 100;
1505 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1506 size_t payload_len;
1507
1508 uint8_t payload[kPayloadBytes] = {0};
1509 WebRtcRTPHeader rtp_info;
1510
1511 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1512 ASSERT_EQ(NetEq::kOK,
1513 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1514 ++seq_no;
1515 timestamp += kCngPeriodSamples;
1516
1517 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001518 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001519 int num_channels;
1520 NetEqOutputType type;
1521 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1522 &num_channels, &type));
1523 ASSERT_EQ(kBlockSize16kHz, out_len);
1524 EXPECT_EQ(kOutputCNG, type);
1525
1526 // Insert some speech packets.
1527 for (int i = 0; i < 3; ++i) {
1528 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1529 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1530 ++seq_no;
1531 timestamp += kSamples;
1532
1533 // Pull audio once.
1534 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1535 &num_channels, &type));
1536 ASSERT_EQ(kBlockSize16kHz, out_len);
1537 }
1538 // Verify speech output.
1539 EXPECT_EQ(kOutputNormal, type);
1540}
1541
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001542} // namespace webrtc