blob: 6dfcac0d3a3f07ee910dec403a133e9fd5990af4 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * This file includes unit tests for NetEQ.
13 */
14
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000017#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include <stdlib.h>
19#include <string.h> // memset
20
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000021#include <algorithm>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000022#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023#include <string>
24#include <vector>
25
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000026#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000027#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000028#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000031#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000033#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/typedefs.h"
35
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000036DEFINE_bool(gen_ref, false, "Generate reference files.");
37
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038namespace webrtc {
39
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.
307 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
308 // Load PCMa.
309 ASSERT_EQ(0, neteq_->RegisterPayloadType(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.
312 ASSERT_EQ(0, neteq_->RegisterPayloadType(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.
316 ASSERT_EQ(0, neteq_->RegisterPayloadType(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.
320 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000321 // Load iSAC FB.
322 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
kwiberg98ab3a42015-09-30 21:54:21 -0700323#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324 // Load PCM16B nb.
325 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
326 // Load PCM16B wb.
327 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
328 // Load PCM16B swb32.
329 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
330 // Load CNG 8 kHz.
331 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
332 // Load CNG 16 kHz.
333 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
334}
335
336void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000337 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338}
339
Peter Kastingdce40cf2015-08-24 14:52:23 -0700340void NetEqDecodingTest::Process(size_t* out_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000342 while (packet_ && sim_clock_ >= packet_->time_ms()) {
343 if (packet_->payload_length_bytes() > 0) {
344 WebRtcRTPHeader rtp_header;
345 packet_->ConvertHeader(&rtp_header);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000347 rtp_header, packet_->payload(),
348 packet_->payload_length_bytes(),
Peter Kastingb7e50542015-06-11 12:55:50 -0700349 static_cast<uint32_t>(
350 packet_->time_ms() * (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 }
352 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000353 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 }
355
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000356 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 NetEqOutputType type;
358 int num_channels;
359 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
360 &num_channels, &type));
361 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
362 (*out_len == kBlockSize16kHz) ||
363 (*out_len == kBlockSize32kHz));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700364 output_sample_rate_ = static_cast<int>(*out_len / 10 * 1000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365
366 // Increase time.
367 sim_clock_ += kTimeStepMs;
368}
369
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000370void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file,
371 const std::string& ref_file,
372 const std::string& stat_ref_file,
373 const std::string& rtcp_ref_file) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374 OpenInputFile(rtp_file);
375
376 std::string ref_out_file = "";
377 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000378 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 }
380 RefFiles ref_files(ref_file, ref_out_file);
381
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000382 std::string stat_out_file = "";
383 if (stat_ref_file.empty()) {
384 stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat";
385 }
386 RefFiles network_stat_files(stat_ref_file, stat_out_file);
387
388 std::string rtcp_out_file = "";
389 if (rtcp_ref_file.empty()) {
390 rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat";
391 }
392 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
393
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000394 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000396 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 std::ostringstream ss;
398 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
399 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700400 size_t out_len = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000401 ASSERT_NO_FATAL_FAILURE(Process(&out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000402 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403
404 // Query the network statistics API once per second
405 if (sim_clock_ % 1000 == 0) {
406 // Process NetworkStatistics.
407 NetEqNetworkStatistics network_stats;
408 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000409 ASSERT_NO_FATAL_FAILURE(
410 network_stat_files.ProcessReference(network_stats));
henrik.lundin9c3efd02015-08-27 13:12:22 -0700411 // Compare with CurrentDelay, which should be identical.
412 EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413
414 // Process RTCPstat.
415 RtcpStatistics rtcp_stats;
416 neteq_->GetRtcpStatistics(&rtcp_stats);
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000417 ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000418 }
419 }
420}
421
422void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
423 int timestamp,
424 WebRtcRTPHeader* rtp_info) {
425 rtp_info->header.sequenceNumber = frame_index;
426 rtp_info->header.timestamp = timestamp;
427 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
428 rtp_info->header.payloadType = 94; // PCM16b WB codec.
429 rtp_info->header.markerBit = 0;
430}
431
432void NetEqDecodingTest::PopulateCng(int frame_index,
433 int timestamp,
434 WebRtcRTPHeader* rtp_info,
435 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000436 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000437 rtp_info->header.sequenceNumber = frame_index;
438 rtp_info->header.timestamp = timestamp;
439 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
440 rtp_info->header.payloadType = 98; // WB CNG.
441 rtp_info->header.markerBit = 0;
442 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
443 *payload_len = 1; // Only noise level, no spectral parameters.
444}
445
kwiberg98ab3a42015-09-30 21:54:21 -0700446#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISAC)) && \
447 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722)
448#define IF_ALL_CODECS(x) x
449#else
450#define IF_ALL_CODECS(x) DISABLED_##x
451#endif
452
henrikaa2c79402015-06-10 13:24:48 +0200453TEST_F(NetEqDecodingTest,
kwiberg98ab3a42015-09-30 21:54:21 -0700454 DISABLED_ON_IOS(DISABLED_ON_ANDROID(IF_ALL_CODECS(TestBitExactness)))) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000455 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000456 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org48438c22014-05-20 16:07:43 +0000457 // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm
458 // are identical. The latter could have been removed, but if clients still
459 // have a copy of the file, the test will fail.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000460 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000461 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000462#if defined(_MSC_VER) && (_MSC_VER >= 1700)
463 // For Visual Studio 2012 and later, we will have to use the generic reference
464 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000465 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000466 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000467#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000468 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000469 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000470#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000471 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000472 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000473
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000474 if (FLAGS_gen_ref) {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000475 DecodeAndCompare(input_rtp_file, "", "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000476 } else {
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000477 DecodeAndCompare(input_rtp_file,
478 input_ref_file,
479 network_stat_ref_file,
480 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000481 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000482}
483
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000484// Use fax mode to avoid time-scaling. This is to simplify the testing of
485// packet waiting times in the packet buffer.
486class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
487 protected:
488 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
489 config_.playout_mode = kPlayoutFax;
490 }
491};
492
493TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000494 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
495 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000496 const size_t kSamples = 10 * 16;
497 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000498 for (size_t i = 0; i < num_frames; ++i) {
499 uint16_t payload[kSamples] = {0};
500 WebRtcRTPHeader rtp_info;
501 rtp_info.header.sequenceNumber = i;
502 rtp_info.header.timestamp = i * kSamples;
503 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
504 rtp_info.header.payloadType = 94; // PCM16b WB codec.
505 rtp_info.header.markerBit = 0;
506 ASSERT_EQ(0, neteq_->InsertPacket(
507 rtp_info,
508 reinterpret_cast<uint8_t*>(payload),
509 kPayloadBytes, 0));
510 }
511 // Pull out all data.
512 for (size_t i = 0; i < num_frames; ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700513 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000514 int num_channels;
515 NetEqOutputType type;
516 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
517 &num_channels, &type));
518 ASSERT_EQ(kBlockSize16kHz, out_len);
519 }
520
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200521 NetEqNetworkStatistics stats;
522 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000523 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
524 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200525 // each packet. Thus, we are calculating the statistics for a series from 10
526 // to 300, in steps of 10 ms.
527 EXPECT_EQ(155, stats.mean_waiting_time_ms);
528 EXPECT_EQ(155, stats.median_waiting_time_ms);
529 EXPECT_EQ(10, stats.min_waiting_time_ms);
530 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000531
532 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200533 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
534 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
535 EXPECT_EQ(-1, stats.median_waiting_time_ms);
536 EXPECT_EQ(-1, stats.min_waiting_time_ms);
537 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538}
539
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000540TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000541 const int kNumFrames = 3000; // Needed for convergence.
542 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000543 const size_t kSamples = 10 * 16;
544 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545 while (frame_index < kNumFrames) {
546 // Insert one packet each time, except every 10th time where we insert two
547 // packets at once. This will create a negative clock-drift of approx. 10%.
548 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
549 for (int n = 0; n < num_packets; ++n) {
550 uint8_t payload[kPayloadBytes] = {0};
551 WebRtcRTPHeader rtp_info;
552 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
553 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
554 ++frame_index;
555 }
556
557 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700558 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559 int num_channels;
560 NetEqOutputType type;
561 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
562 &num_channels, &type));
563 ASSERT_EQ(kBlockSize16kHz, out_len);
564 }
565
566 NetEqNetworkStatistics network_stats;
567 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
568 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
569}
570
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000571TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572 const int kNumFrames = 5000; // Needed for convergence.
573 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000574 const size_t kSamples = 10 * 16;
575 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000576 for (int i = 0; i < kNumFrames; ++i) {
577 // Insert one packet each time, except every 10th time where we don't insert
578 // any packet. This will create a positive clock-drift of approx. 11%.
579 int num_packets = (i % 10 == 9 ? 0 : 1);
580 for (int n = 0; n < num_packets; ++n) {
581 uint8_t payload[kPayloadBytes] = {0};
582 WebRtcRTPHeader rtp_info;
583 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
584 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
585 ++frame_index;
586 }
587
588 // Pull out data once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700589 size_t out_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000590 int num_channels;
591 NetEqOutputType type;
592 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
593 &num_channels, &type));
594 ASSERT_EQ(kBlockSize16kHz, out_len);
595 }
596
597 NetEqNetworkStatistics network_stats;
598 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
599 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
600}
601
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000602void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
603 double network_freeze_ms,
604 bool pull_audio_during_freeze,
605 int delay_tolerance_ms,
606 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 uint16_t seq_no = 0;
608 uint32_t timestamp = 0;
609 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000610 const size_t kSamples = kFrameSizeMs * 16;
611 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 double next_input_time_ms = 0.0;
613 double t_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700614 size_t out_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000615 int num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 NetEqOutputType type;
617
618 // Insert speech for 5 seconds.
619 const int kSpeechDurationMs = 5000;
620 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
621 // Each turn in this for loop is 10 ms.
622 while (next_input_time_ms <= t_ms) {
623 // Insert one 30 ms speech frame.
624 uint8_t payload[kPayloadBytes] = {0};
625 WebRtcRTPHeader rtp_info;
626 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
627 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
628 ++seq_no;
629 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000630 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000631 }
632 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
634 &num_channels, &type));
635 ASSERT_EQ(kBlockSize16kHz, out_len);
636 }
637
638 EXPECT_EQ(kOutputNormal, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000639 int32_t delay_before = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640
641 // Insert CNG for 1 minute (= 60000 ms).
642 const int kCngPeriodMs = 100;
643 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
644 const int kCngDurationMs = 60000;
645 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
646 // Each turn in this for loop is 10 ms.
647 while (next_input_time_ms <= t_ms) {
648 // Insert one CNG frame each 100 ms.
649 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000650 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000651 WebRtcRTPHeader rtp_info;
652 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
653 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
654 ++seq_no;
655 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000656 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 }
658 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000659 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
660 &num_channels, &type));
661 ASSERT_EQ(kBlockSize16kHz, out_len);
662 }
663
664 EXPECT_EQ(kOutputCNG, type);
665
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000666 if (network_freeze_ms > 0) {
667 // First keep pulling audio for |network_freeze_ms| without inserting
668 // any data, then insert CNG data corresponding to |network_freeze_ms|
669 // without pulling any output audio.
670 const double loop_end_time = t_ms + network_freeze_ms;
671 for (; t_ms < loop_end_time; t_ms += 10) {
672 // Pull out data once.
673 ASSERT_EQ(0,
674 neteq_->GetAudio(
675 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
676 ASSERT_EQ(kBlockSize16kHz, out_len);
677 EXPECT_EQ(kOutputCNG, type);
678 }
679 bool pull_once = pull_audio_during_freeze;
680 // If |pull_once| is true, GetAudio will be called once half-way through
681 // the network recovery period.
682 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
683 while (next_input_time_ms <= t_ms) {
684 if (pull_once && next_input_time_ms >= pull_time_ms) {
685 pull_once = false;
686 // Pull out data once.
687 ASSERT_EQ(
688 0,
689 neteq_->GetAudio(
690 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
691 ASSERT_EQ(kBlockSize16kHz, out_len);
692 EXPECT_EQ(kOutputCNG, type);
693 t_ms += 10;
694 }
695 // Insert one CNG frame each 100 ms.
696 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000697 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000698 WebRtcRTPHeader rtp_info;
699 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
700 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
701 ++seq_no;
702 timestamp += kCngPeriodSamples;
703 next_input_time_ms += kCngPeriodMs * drift_factor;
704 }
705 }
706
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000707 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 double speech_restart_time_ms = t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000709 while (type != kOutputNormal) {
710 // Each turn in this for loop is 10 ms.
711 while (next_input_time_ms <= t_ms) {
712 // Insert one 30 ms speech frame.
713 uint8_t payload[kPayloadBytes] = {0};
714 WebRtcRTPHeader rtp_info;
715 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
716 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
717 ++seq_no;
718 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000719 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000720 }
721 // Pull out data once.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000722 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
723 &num_channels, &type));
724 ASSERT_EQ(kBlockSize16kHz, out_len);
725 // Increase clock.
726 t_ms += 10;
727 }
728
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000729 // Check that the speech starts again within reasonable time.
730 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
731 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000732 int32_t delay_after = timestamp - PlayoutTimestamp();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000734 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
735 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000736}
737
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000738TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000739 // Apply a clock drift of -25 ms / s (sender faster than receiver).
740 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000741 const double kNetworkFreezeTimeMs = 0.0;
742 const bool kGetAudioDuringFreezeRecovery = false;
743 const int kDelayToleranceMs = 20;
744 const int kMaxTimeToSpeechMs = 100;
745 LongCngWithClockDrift(kDriftFactor,
746 kNetworkFreezeTimeMs,
747 kGetAudioDuringFreezeRecovery,
748 kDelayToleranceMs,
749 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000750}
751
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000752TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000753 // Apply a clock drift of +25 ms / s (sender slower than receiver).
754 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000755 const double kNetworkFreezeTimeMs = 0.0;
756 const bool kGetAudioDuringFreezeRecovery = false;
757 const int kDelayToleranceMs = 20;
758 const int kMaxTimeToSpeechMs = 100;
759 LongCngWithClockDrift(kDriftFactor,
760 kNetworkFreezeTimeMs,
761 kGetAudioDuringFreezeRecovery,
762 kDelayToleranceMs,
763 kMaxTimeToSpeechMs);
764}
765
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000766TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 // Apply a clock drift of -25 ms / s (sender faster than receiver).
768 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
769 const double kNetworkFreezeTimeMs = 5000.0;
770 const bool kGetAudioDuringFreezeRecovery = false;
771 const int kDelayToleranceMs = 50;
772 const int kMaxTimeToSpeechMs = 200;
773 LongCngWithClockDrift(kDriftFactor,
774 kNetworkFreezeTimeMs,
775 kGetAudioDuringFreezeRecovery,
776 kDelayToleranceMs,
777 kMaxTimeToSpeechMs);
778}
779
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000780TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000781 // Apply a clock drift of +25 ms / s (sender slower than receiver).
782 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
783 const double kNetworkFreezeTimeMs = 5000.0;
784 const bool kGetAudioDuringFreezeRecovery = false;
785 const int kDelayToleranceMs = 20;
786 const int kMaxTimeToSpeechMs = 100;
787 LongCngWithClockDrift(kDriftFactor,
788 kNetworkFreezeTimeMs,
789 kGetAudioDuringFreezeRecovery,
790 kDelayToleranceMs,
791 kMaxTimeToSpeechMs);
792}
793
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000794TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000795 // Apply a clock drift of +25 ms / s (sender slower than receiver).
796 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
797 const double kNetworkFreezeTimeMs = 5000.0;
798 const bool kGetAudioDuringFreezeRecovery = true;
799 const int kDelayToleranceMs = 20;
800 const int kMaxTimeToSpeechMs = 100;
801 LongCngWithClockDrift(kDriftFactor,
802 kNetworkFreezeTimeMs,
803 kGetAudioDuringFreezeRecovery,
804 kDelayToleranceMs,
805 kMaxTimeToSpeechMs);
806}
807
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000808TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000809 const double kDriftFactor = 1.0; // No drift.
810 const double kNetworkFreezeTimeMs = 0.0;
811 const bool kGetAudioDuringFreezeRecovery = false;
812 const int kDelayToleranceMs = 10;
813 const int kMaxTimeToSpeechMs = 50;
814 LongCngWithClockDrift(kDriftFactor,
815 kNetworkFreezeTimeMs,
816 kGetAudioDuringFreezeRecovery,
817 kDelayToleranceMs,
818 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000819}
820
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000821TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000822 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000823 uint8_t payload[kPayloadBytes] = {0};
824 WebRtcRTPHeader rtp_info;
825 PopulateRtpInfo(0, 0, &rtp_info);
826 rtp_info.header.payloadType = 1; // Not registered as a decoder.
827 EXPECT_EQ(NetEq::kFail,
828 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
829 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
830}
831
kwiberg98ab3a42015-09-30 21:54:21 -0700832#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
833#define IF_ISAC(x) x
834#else
835#define IF_ISAC(x) DISABLED_##x
836#endif
837
838TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(IF_ISAC(DecoderError))) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000839 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000840 uint8_t payload[kPayloadBytes] = {0};
841 WebRtcRTPHeader rtp_info;
842 PopulateRtpInfo(0, 0, &rtp_info);
843 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
844 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
845 NetEqOutputType type;
846 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
847 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000848 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 out_data_[i] = 1;
850 }
851 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700852 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000853 EXPECT_EQ(NetEq::kFail,
854 neteq_->GetAudio(kMaxBlockSize, out_data_,
855 &samples_per_channel, &num_channels, &type));
856 // Verify that there is a decoder error to check.
857 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
858 // Code 6730 is an iSAC error code.
859 EXPECT_EQ(6730, neteq_->LastDecoderError());
860 // Verify that the first 160 samples are set to 0, and that the remaining
861 // samples are left unmodified.
862 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
863 for (int i = 0; i < kExpectedOutputLength; ++i) {
864 std::ostringstream ss;
865 ss << "i = " << i;
866 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
867 EXPECT_EQ(0, out_data_[i]);
868 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000869 for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 std::ostringstream ss;
871 ss << "i = " << i;
872 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
873 EXPECT_EQ(1, out_data_[i]);
874 }
875}
876
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000877TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 NetEqOutputType type;
879 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
880 // to GetAudio.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000881 for (size_t i = 0; i < kMaxBlockSize; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 out_data_[i] = 1;
883 }
884 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700885 size_t samples_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000886 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
887 &samples_per_channel,
888 &num_channels, &type));
889 // Verify that the first block of samples is set to 0.
890 static const int kExpectedOutputLength =
891 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
892 for (int i = 0; i < kExpectedOutputLength; ++i) {
893 std::ostringstream ss;
894 ss << "i = " << i;
895 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
896 EXPECT_EQ(0, out_data_[i]);
897 }
898}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000899
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000900class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000901 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000902 virtual void TestCondition(double sum_squared_noise,
903 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000904
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000905 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700906 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000907 uint8_t payload_type = 0xFF; // Invalid.
908 if (sampling_rate_hz == 8000) {
909 expected_samples_per_channel = kBlockSize8kHz;
910 payload_type = 93; // PCM 16, 8 kHz.
911 } else if (sampling_rate_hz == 16000) {
912 expected_samples_per_channel = kBlockSize16kHz;
913 payload_type = 94; // PCM 16, 16 kHZ.
914 } else if (sampling_rate_hz == 32000) {
915 expected_samples_per_channel = kBlockSize32kHz;
916 payload_type = 95; // PCM 16, 32 kHz.
917 } else {
918 ASSERT_TRUE(false); // Unsupported test case.
919 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000920
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000921 NetEqOutputType type;
922 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000923 test::AudioLoop input;
924 // We are using the same 32 kHz input file for all tests, regardless of
925 // |sampling_rate_hz|. The output may sound weird, but the test is still
926 // valid.
927 ASSERT_TRUE(input.Init(
928 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
929 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700930 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000931
932 // Payload of 10 ms of PCM16 32 kHz.
933 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000934 WebRtcRTPHeader rtp_info;
935 PopulateRtpInfo(0, 0, &rtp_info);
936 rtp_info.header.payloadType = payload_type;
937
938 int number_channels = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700939 size_t samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000940
941 uint32_t receive_timestamp = 0;
942 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700943 size_t enc_len_bytes = WebRtcPcm16b_Encode(
kwiberg@webrtc.org648f5d62015-02-10 09:18:28 +0000944 input.GetNextBlock(), expected_samples_per_channel, payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000945 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
946
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000947 number_channels = 0;
948 samples_per_channel = 0;
949 ASSERT_EQ(0,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700950 neteq_->InsertPacket(rtp_info, payload, enc_len_bytes,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000951 receive_timestamp));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000952 ASSERT_EQ(0,
953 neteq_->GetAudio(kBlockSize32kHz,
954 output,
955 &samples_per_channel,
956 &number_channels,
957 &type));
958 ASSERT_EQ(1, number_channels);
959 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
960 ASSERT_EQ(kOutputNormal, type);
961
962 // Next packet.
963 rtp_info.header.timestamp += expected_samples_per_channel;
964 rtp_info.header.sequenceNumber++;
965 receive_timestamp += expected_samples_per_channel;
966 }
967
968 number_channels = 0;
969 samples_per_channel = 0;
970
971 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
972 // one frame without checking speech-type. This is the first frame pulled
973 // without inserting any packet, and might not be labeled as PLC.
974 ASSERT_EQ(0,
975 neteq_->GetAudio(kBlockSize32kHz,
976 output,
977 &samples_per_channel,
978 &number_channels,
979 &type));
980 ASSERT_EQ(1, number_channels);
981 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
982
983 // To be able to test the fading of background noise we need at lease to
984 // pull 611 frames.
985 const int kFadingThreshold = 611;
986
987 // Test several CNG-to-PLC packet for the expected behavior. The number 20
988 // is arbitrary, but sufficiently large to test enough number of frames.
989 const int kNumPlcToCngTestFrames = 20;
990 bool plc_to_cng = false;
991 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
992 number_channels = 0;
993 samples_per_channel = 0;
994 memset(output, 1, sizeof(output)); // Set to non-zero.
995 ASSERT_EQ(0,
996 neteq_->GetAudio(kBlockSize32kHz,
997 output,
998 &samples_per_channel,
999 &number_channels,
1000 &type));
1001 ASSERT_EQ(1, number_channels);
1002 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
1003 if (type == kOutputPLCtoCNG) {
1004 plc_to_cng = true;
1005 double sum_squared = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001006 for (size_t k = 0; k < number_channels * samples_per_channel; ++k)
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001007 sum_squared += output[k] * output[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001008 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001009 } else {
1010 EXPECT_EQ(kOutputPLC, type);
1011 }
1012 }
1013 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1014 }
1015};
1016
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001017class NetEqBgnTestOn : public NetEqBgnTest {
1018 protected:
1019 NetEqBgnTestOn() : NetEqBgnTest() {
1020 config_.background_noise_mode = NetEq::kBgnOn;
1021 }
1022
1023 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1024 EXPECT_NE(0, sum_squared_noise);
1025 }
1026};
1027
1028class NetEqBgnTestOff : public NetEqBgnTest {
1029 protected:
1030 NetEqBgnTestOff() : NetEqBgnTest() {
1031 config_.background_noise_mode = NetEq::kBgnOff;
1032 }
1033
1034 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1035 EXPECT_EQ(0, sum_squared_noise);
1036 }
1037};
1038
1039class NetEqBgnTestFade : public NetEqBgnTest {
1040 protected:
1041 NetEqBgnTestFade() : NetEqBgnTest() {
1042 config_.background_noise_mode = NetEq::kBgnFade;
1043 }
1044
1045 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1046 if (should_be_faded)
1047 EXPECT_EQ(0, sum_squared_noise);
1048 }
1049};
1050
henrika1d34fe92015-06-16 10:04:20 +02001051TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001052 CheckBgn(8000);
1053 CheckBgn(16000);
1054 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001055}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001056
henrika1d34fe92015-06-16 10:04:20 +02001057TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001058 CheckBgn(8000);
1059 CheckBgn(16000);
1060 CheckBgn(32000);
1061}
1062
henrika1d34fe92015-06-16 10:04:20 +02001063TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001064 CheckBgn(8000);
1065 CheckBgn(16000);
1066 CheckBgn(32000);
1067}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001068
kwiberg98ab3a42015-09-30 21:54:21 -07001069TEST_F(NetEqDecodingTest, IF_ISAC(SyncPacketInsert)) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001070 WebRtcRTPHeader rtp_info;
1071 uint32_t receive_timestamp = 0;
1072 // For the readability use the following payloads instead of the defaults of
1073 // this test.
1074 uint8_t kPcm16WbPayloadType = 1;
1075 uint8_t kCngNbPayloadType = 2;
1076 uint8_t kCngWbPayloadType = 3;
1077 uint8_t kCngSwb32PayloadType = 4;
1078 uint8_t kCngSwb48PayloadType = 5;
1079 uint8_t kAvtPayloadType = 6;
1080 uint8_t kRedPayloadType = 7;
1081 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1082
1083 // Register decoders.
1084 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
1085 kPcm16WbPayloadType));
1086 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
1087 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
1088 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
1089 kCngSwb32PayloadType));
1090 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
1091 kCngSwb48PayloadType));
1092 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
1093 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
1094 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
1095
1096 PopulateRtpInfo(0, 0, &rtp_info);
1097 rtp_info.header.payloadType = kPcm16WbPayloadType;
1098
1099 // The first packet injected cannot be sync-packet.
1100 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1101
1102 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001103 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001104 uint8_t payload[kPayloadBytes] = {0};
1105 ASSERT_EQ(0, neteq_->InsertPacket(
1106 rtp_info, payload, kPayloadBytes, receive_timestamp));
1107
1108 // Next packet. Last packet contained 10 ms audio.
1109 rtp_info.header.sequenceNumber++;
1110 rtp_info.header.timestamp += kBlockSize16kHz;
1111 receive_timestamp += kBlockSize16kHz;
1112
1113 // Unacceptable payload types CNG, AVT (DTMF), RED.
1114 rtp_info.header.payloadType = kCngNbPayloadType;
1115 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1116
1117 rtp_info.header.payloadType = kCngWbPayloadType;
1118 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1119
1120 rtp_info.header.payloadType = kCngSwb32PayloadType;
1121 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1122
1123 rtp_info.header.payloadType = kCngSwb48PayloadType;
1124 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1125
1126 rtp_info.header.payloadType = kAvtPayloadType;
1127 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1128
1129 rtp_info.header.payloadType = kRedPayloadType;
1130 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1131
1132 // Change of codec cannot be initiated with a sync packet.
1133 rtp_info.header.payloadType = kIsacPayloadType;
1134 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1135
1136 // Change of SSRC is not allowed with a sync packet.
1137 rtp_info.header.payloadType = kPcm16WbPayloadType;
1138 ++rtp_info.header.ssrc;
1139 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1140
1141 --rtp_info.header.ssrc;
1142 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1143}
1144
1145// First insert several noise like packets, then sync-packets. Decoding all
1146// packets should not produce error, statistics should not show any packet loss
1147// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001148// TODO(turajs) we will have a better test if we have a referece NetEq, and
1149// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1150// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001151TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001152 WebRtcRTPHeader rtp_info;
1153 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001154 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001155 uint8_t payload[kPayloadBytes];
1156 int16_t decoded[kBlockSize16kHz];
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001157 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001158 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001159 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1160 }
1161 // Insert some packets which decode to noise. We are not interested in
1162 // actual decoded values.
1163 NetEqOutputType output_type;
1164 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001165 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001166 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001167 for (int n = 0; n < 100; ++n) {
1168 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1169 receive_timestamp));
1170 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1171 &samples_per_channel, &num_channels,
1172 &output_type));
1173 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1174 ASSERT_EQ(1, num_channels);
1175
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001176 rtp_info.header.sequenceNumber++;
1177 rtp_info.header.timestamp += kBlockSize16kHz;
1178 receive_timestamp += kBlockSize16kHz;
1179 }
1180 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001181
1182 // Make sure sufficient number of sync packets are inserted that we can
1183 // conduct a test.
1184 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001185 // Insert sync-packets, the decoded sequence should be all-zero.
1186 for (int n = 0; n < kNumSyncPackets; ++n) {
1187 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1188 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1189 &samples_per_channel, &num_channels,
1190 &output_type));
1191 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1192 ASSERT_EQ(1, num_channels);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001193 if (n > algorithmic_frame_delay) {
1194 EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels));
1195 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001196 rtp_info.header.sequenceNumber++;
1197 rtp_info.header.timestamp += kBlockSize16kHz;
1198 receive_timestamp += kBlockSize16kHz;
1199 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001200
1201 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001202 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001203 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
1204 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1205 receive_timestamp));
1206 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1207 &samples_per_channel, &num_channels,
1208 &output_type));
1209 if (n >= algorithmic_frame_delay + 1) {
1210 // Expect that this frame contain samples from regular RTP.
1211 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1212 }
1213 rtp_info.header.sequenceNumber++;
1214 rtp_info.header.timestamp += kBlockSize16kHz;
1215 receive_timestamp += kBlockSize16kHz;
1216 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001217 NetEqNetworkStatistics network_stats;
1218 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1219 // Expecting a "clean" network.
1220 EXPECT_EQ(0, network_stats.packet_loss_rate);
1221 EXPECT_EQ(0, network_stats.expand_rate);
1222 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001223 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001224}
1225
1226// Test if the size of the packet buffer reported correctly when containing
1227// sync packets. Also, test if network packets override sync packets. That is to
1228// prefer decoding a network packet to a sync packet, if both have same sequence
1229// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001230TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001231 WebRtcRTPHeader rtp_info;
1232 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001233 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001234 uint8_t payload[kPayloadBytes];
1235 int16_t decoded[kBlockSize16kHz];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001236 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001237 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1238 }
1239 // Insert some packets which decode to noise. We are not interested in
1240 // actual decoded values.
1241 NetEqOutputType output_type;
1242 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001243 size_t samples_per_channel;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001244 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001245 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1246 for (int n = 0; n < algorithmic_frame_delay; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001247 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1248 receive_timestamp));
1249 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1250 &samples_per_channel, &num_channels,
1251 &output_type));
1252 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1253 ASSERT_EQ(1, num_channels);
1254 rtp_info.header.sequenceNumber++;
1255 rtp_info.header.timestamp += kBlockSize16kHz;
1256 receive_timestamp += kBlockSize16kHz;
1257 }
1258 const int kNumSyncPackets = 10;
1259
1260 WebRtcRTPHeader first_sync_packet_rtp_info;
1261 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1262
1263 // Insert sync-packets, but no decoding.
1264 for (int n = 0; n < kNumSyncPackets; ++n) {
1265 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1266 rtp_info.header.sequenceNumber++;
1267 rtp_info.header.timestamp += kBlockSize16kHz;
1268 receive_timestamp += kBlockSize16kHz;
1269 }
1270 NetEqNetworkStatistics network_stats;
1271 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001272 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1273 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001274
1275 // Rewind |rtp_info| to that of the first sync packet.
1276 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1277
1278 // Insert.
1279 for (int n = 0; n < kNumSyncPackets; ++n) {
1280 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1281 receive_timestamp));
1282 rtp_info.header.sequenceNumber++;
1283 rtp_info.header.timestamp += kBlockSize16kHz;
1284 receive_timestamp += kBlockSize16kHz;
1285 }
1286
1287 // Decode.
1288 for (int n = 0; n < kNumSyncPackets; ++n) {
1289 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1290 &samples_per_channel, &num_channels,
1291 &output_type));
1292 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1293 ASSERT_EQ(1, num_channels);
1294 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1295 }
1296}
1297
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001298void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1299 uint32_t start_timestamp,
1300 const std::set<uint16_t>& drop_seq_numbers,
1301 bool expect_seq_no_wrap,
1302 bool expect_timestamp_wrap) {
1303 uint16_t seq_no = start_seq_no;
1304 uint32_t timestamp = start_timestamp;
1305 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1306 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1307 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001308 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001309 double next_input_time_ms = 0.0;
1310 int16_t decoded[kBlockSize16kHz];
1311 int num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -07001312 size_t samples_per_channel;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001313 NetEqOutputType output_type;
1314 uint32_t receive_timestamp = 0;
1315
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001316 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001317 const int kSpeechDurationMs = 2000;
1318 int packets_inserted = 0;
1319 uint16_t last_seq_no;
1320 uint32_t last_timestamp;
1321 bool timestamp_wrapped = false;
1322 bool seq_no_wrapped = false;
1323 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1324 // Each turn in this for loop is 10 ms.
1325 while (next_input_time_ms <= t_ms) {
1326 // Insert one 30 ms speech frame.
1327 uint8_t payload[kPayloadBytes] = {0};
1328 WebRtcRTPHeader rtp_info;
1329 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1330 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1331 // This sequence number was not in the set to drop. Insert it.
1332 ASSERT_EQ(0,
1333 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1334 receive_timestamp));
1335 ++packets_inserted;
1336 }
1337 NetEqNetworkStatistics network_stats;
1338 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1339
1340 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1341 // packet size for first few packets. Therefore we refrain from checking
1342 // the criteria.
1343 if (packets_inserted > 4) {
1344 // Expect preferred and actual buffer size to be no more than 2 frames.
1345 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001346 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1347 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001348 }
1349 last_seq_no = seq_no;
1350 last_timestamp = timestamp;
1351
1352 ++seq_no;
1353 timestamp += kSamples;
1354 receive_timestamp += kSamples;
1355 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1356
1357 seq_no_wrapped |= seq_no < last_seq_no;
1358 timestamp_wrapped |= timestamp < last_timestamp;
1359 }
1360 // Pull out data once.
1361 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1362 &samples_per_channel, &num_channels,
1363 &output_type));
1364 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1365 ASSERT_EQ(1, num_channels);
1366
1367 // Expect delay (in samples) to be less than 2 packets.
wu@webrtc.org94454b72014-06-05 20:34:08 +00001368 EXPECT_LE(timestamp - PlayoutTimestamp(),
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001369 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001370 }
1371 // Make sure we have actually tested wrap-around.
1372 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1373 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1374}
1375
1376TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1377 // Start with a sequence number that will soon wrap.
1378 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1379 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1380}
1381
1382TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1383 // Start with a sequence number that will soon wrap.
1384 std::set<uint16_t> drop_seq_numbers;
1385 drop_seq_numbers.insert(0xFFFF);
1386 drop_seq_numbers.insert(0x0);
1387 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1388}
1389
1390TEST_F(NetEqDecodingTest, TimestampWrap) {
1391 // Start with a timestamp that will soon wrap.
1392 std::set<uint16_t> drop_seq_numbers;
1393 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1394}
1395
1396TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1397 // Start with a timestamp and a sequence number that will wrap at the same
1398 // time.
1399 std::set<uint16_t> drop_seq_numbers;
1400 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1401}
1402
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001403void NetEqDecodingTest::DuplicateCng() {
1404 uint16_t seq_no = 0;
1405 uint32_t timestamp = 0;
1406 const int kFrameSizeMs = 10;
1407 const int kSampleRateKhz = 16;
1408 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001409 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001410
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001411 const int algorithmic_delay_samples = std::max(
1412 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001413 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001414 // correct.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001415 size_t out_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001416 int num_channels;
1417 NetEqOutputType type;
1418 uint8_t payload[kPayloadBytes] = {0};
1419 WebRtcRTPHeader rtp_info;
1420 for (int i = 0; i < 3; ++i) {
1421 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1422 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1423 ++seq_no;
1424 timestamp += kSamples;
1425
1426 // Pull audio once.
1427 ASSERT_EQ(0,
1428 neteq_->GetAudio(
1429 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1430 ASSERT_EQ(kBlockSize16kHz, out_len);
1431 }
1432 // Verify speech output.
1433 EXPECT_EQ(kOutputNormal, type);
1434
1435 // Insert same CNG packet twice.
1436 const int kCngPeriodMs = 100;
1437 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001438 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001439 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1440 // This is the first time this CNG packet is inserted.
1441 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1442
1443 // Pull audio once and make sure CNG is played.
1444 ASSERT_EQ(0,
1445 neteq_->GetAudio(
1446 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1447 ASSERT_EQ(kBlockSize16kHz, out_len);
1448 EXPECT_EQ(kOutputCNG, type);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001449 EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001450
1451 // Insert the same CNG packet again. Note that at this point it is old, since
1452 // we have already decoded the first copy of it.
1453 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1454
1455 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1456 // we have already pulled out CNG once.
1457 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
1458 ASSERT_EQ(0,
1459 neteq_->GetAudio(
1460 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1461 ASSERT_EQ(kBlockSize16kHz, out_len);
1462 EXPECT_EQ(kOutputCNG, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001463 EXPECT_EQ(timestamp - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001464 PlayoutTimestamp());
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001465 }
1466
1467 // Insert speech again.
1468 ++seq_no;
1469 timestamp += kCngPeriodSamples;
1470 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1471 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1472
1473 // Pull audio once and verify that the output is speech again.
1474 ASSERT_EQ(0,
1475 neteq_->GetAudio(
1476 kMaxBlockSize, out_data_, &out_len, &num_channels, &type));
1477 ASSERT_EQ(kBlockSize16kHz, out_len);
1478 EXPECT_EQ(kOutputNormal, type);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001479 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
wu@webrtc.org94454b72014-06-05 20:34:08 +00001480 PlayoutTimestamp());
1481}
1482
1483uint32_t NetEqDecodingTest::PlayoutTimestamp() {
1484 uint32_t playout_timestamp = 0;
1485 EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp));
1486 return playout_timestamp;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001487}
1488
1489TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001490
1491TEST_F(NetEqDecodingTest, CngFirst) {
1492 uint16_t seq_no = 0;
1493 uint32_t timestamp = 0;
1494 const int kFrameSizeMs = 10;
1495 const int kSampleRateKhz = 16;
1496 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1497 const int kPayloadBytes = kSamples * 2;
1498 const int kCngPeriodMs = 100;
1499 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1500 size_t payload_len;
1501
1502 uint8_t payload[kPayloadBytes] = {0};
1503 WebRtcRTPHeader rtp_info;
1504
1505 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1506 ASSERT_EQ(NetEq::kOK,
1507 neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
1508 ++seq_no;
1509 timestamp += kCngPeriodSamples;
1510
1511 // Pull audio once and make sure CNG is played.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001512 size_t out_len;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001513 int num_channels;
1514 NetEqOutputType type;
1515 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1516 &num_channels, &type));
1517 ASSERT_EQ(kBlockSize16kHz, out_len);
1518 EXPECT_EQ(kOutputCNG, type);
1519
1520 // Insert some speech packets.
1521 for (int i = 0; i < 3; ++i) {
1522 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1523 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
1524 ++seq_no;
1525 timestamp += kSamples;
1526
1527 // Pull audio once.
1528 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
1529 &num_channels, &type));
1530 ASSERT_EQ(kBlockSize16kHz, out_len);
1531 }
1532 // Verify speech output.
1533 EXPECT_EQ(kOutputNormal, type);
1534}
1535
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001536} // namespace webrtc