blob: e982db2b7d2663a3408eb79adc7b00410c030f0e [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
Henrik Kjellander74640892015-10-29 11:31:02 +010011#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000023#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000024#include "testing/gtest/include/gtest/gtest.h"
minyue4f906772016-04-29 11:05:14 -070025#include "webrtc/base/sha1digest.h"
26#include "webrtc/base/stringencode.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000027#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000028#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010029#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080030#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031#include "webrtc/test/testsupport/fileutils.h"
32#include "webrtc/typedefs.h"
33
minyue5f026d02015-12-16 07:36:04 -080034#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
35#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
36#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
37#else
38#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
39#endif
40#endif
41
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000042DEFINE_bool(gen_ref, false, "Generate reference files.");
43
minyue5f026d02015-12-16 07:36:04 -080044namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
minyue4f906772016-04-29 11:05:14 -070046const std::string& PlatformChecksum(const std::string& checksum_general,
47 const std::string& checksum_android,
48 const std::string& checksum_win_32,
49 const std::string& checksum_win_64) {
50#ifdef WEBRTC_ANDROID
51 return checksum_android;
52#elif WEBRTC_WIN
53 #ifdef WEBRTC_ARCH_64_BITS
54 return checksum_win_64;
55 #else
56 return checksum_win_32;
57 #endif // WEBRTC_ARCH_64_BITS
58#else
59 return checksum_general;
60#endif // WEBRTC_WIN
61}
62
minyue5f026d02015-12-16 07:36:04 -080063bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000064 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000066 all_zero = buf[n] == 0;
67 return all_zero;
68}
69
minyue5f026d02015-12-16 07:36:04 -080070bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000071 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070072 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000073 all_non_zero = buf[n] != 0;
74 return all_non_zero;
75}
76
minyue5f026d02015-12-16 07:36:04 -080077#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
78void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
79 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
80 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
81 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
82 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
83 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
84 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
85 stats->set_expand_rate(stats_raw.expand_rate);
86 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
87 stats->set_preemptive_rate(stats_raw.preemptive_rate);
88 stats->set_accelerate_rate(stats_raw.accelerate_rate);
89 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
90 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
91 stats->set_added_zero_samples(stats_raw.added_zero_samples);
92 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
93 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
94 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
95 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
96}
97
98void Convert(const webrtc::RtcpStatistics& stats_raw,
99 webrtc::neteq_unittest::RtcpStatistics* stats) {
100 stats->set_fraction_lost(stats_raw.fraction_lost);
101 stats->set_cumulative_lost(stats_raw.cumulative_lost);
102 stats->set_extended_max_sequence_number(
103 stats_raw.extended_max_sequence_number);
104 stats->set_jitter(stats_raw.jitter);
105}
106
minyue4f906772016-04-29 11:05:14 -0700107void AddMessage(FILE* file, rtc::MessageDigest* digest,
108 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800109 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700110 if (file)
111 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
112 digest->Update(&size, sizeof(size));
113
114 if (file)
115 ASSERT_EQ(static_cast<size_t>(size),
116 fwrite(message.data(), sizeof(char), size, file));
117 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800118}
119
minyue5f026d02015-12-16 07:36:04 -0800120#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
121
122} // namespace
123
124namespace webrtc {
125
minyue4f906772016-04-29 11:05:14 -0700126class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 public:
minyue4f906772016-04-29 11:05:14 -0700128 explicit ResultSink(const std::string& output_file);
129 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130
minyue4f906772016-04-29 11:05:14 -0700131 template<typename T, size_t n> void AddResult(
132 const T (&test_results)[n],
133 size_t length);
134
135 void AddResult(const NetEqNetworkStatistics& stats);
136 void AddResult(const RtcpStatistics& stats);
137
138 void VerifyChecksum(const std::string& ref_check_sum);
139
140 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700142 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143};
144
minyue4f906772016-04-29 11:05:14 -0700145ResultSink::ResultSink(const std::string &output_file)
146 : output_fp_(nullptr),
147 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 if (!output_file.empty()) {
149 output_fp_ = fopen(output_file.c_str(), "wb");
150 EXPECT_TRUE(output_fp_ != NULL);
151 }
152}
153
minyue4f906772016-04-29 11:05:14 -0700154ResultSink::~ResultSink() {
155 if (output_fp_)
156 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157}
158
159template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700160void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 if (output_fp_) {
162 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
163 }
minyue4f906772016-04-29 11:05:14 -0700164 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165}
166
minyue4f906772016-04-29 11:05:14 -0700167void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800168#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800169 neteq_unittest::NetEqNetworkStatistics stats;
170 Convert(stats_raw, &stats);
171
172 std::string stats_string;
173 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700174 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800175#else
176 FAIL() << "Writing to reference file requires Proto Buffer.";
177#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178}
179
minyue4f906772016-04-29 11:05:14 -0700180void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800181#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800182 neteq_unittest::RtcpStatistics stats;
183 Convert(stats_raw, &stats);
184
185 std::string stats_string;
186 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700187 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800188#else
189 FAIL() << "Writing to reference file requires Proto Buffer.";
190#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191}
192
minyue4f906772016-04-29 11:05:14 -0700193void ResultSink::VerifyChecksum(const std::string& checksum) {
194 std::vector<char> buffer;
195 buffer.resize(digest_->Size());
196 digest_->Finish(&buffer[0], buffer.size());
197 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
198 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199}
200
201class NetEqDecodingTest : public ::testing::Test {
202 protected:
203 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
204 // constants below can be changed.
205 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700206 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
207 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
208 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800209 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000210 static const int kInitSampleRateHz = 8000;
211
212 NetEqDecodingTest();
213 virtual void SetUp();
214 virtual void TearDown();
215 void SelectDecoders(NetEqDecoder* used_codec);
216 void LoadDecoders();
217 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800218 void Process();
minyue5f026d02015-12-16 07:36:04 -0800219
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000220 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700221 const std::string& output_checksum,
222 const std::string& network_stats_checksum,
223 const std::string& rtcp_stats_checksum,
224 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800225
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000226 static void PopulateRtpInfo(int frame_index,
227 int timestamp,
228 WebRtcRTPHeader* rtp_info);
229 static void PopulateCng(int frame_index,
230 int timestamp,
231 WebRtcRTPHeader* rtp_info,
232 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000233 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000234
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000235 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
236 const std::set<uint16_t>& drop_seq_numbers,
237 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
238
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000239 void LongCngWithClockDrift(double drift_factor,
240 double network_freeze_ms,
241 bool pull_audio_during_freeze,
242 int delay_tolerance_ms,
243 int max_time_to_speech_ms);
244
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000245 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000246
henrik.lundin0d96ab72016-04-06 12:28:26 -0700247 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000248
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000250 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800251 std::unique_ptr<test::RtpFileSource> rtp_source_;
252 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800254 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000256 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257};
258
259// Allocating the static const so that it can be passed by reference.
260const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700261const size_t NetEqDecodingTest::kBlockSize8kHz;
262const size_t NetEqDecodingTest::kBlockSize16kHz;
263const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264const int NetEqDecodingTest::kInitSampleRateHz;
265
266NetEqDecodingTest::NetEqDecodingTest()
267 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000268 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000270 output_sample_rate_(kInitSampleRateHz),
271 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000272 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273}
274
275void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000276 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000277 NetEqNetworkStatistics stat;
278 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
279 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 ASSERT_TRUE(neteq_);
281 LoadDecoders();
282}
283
284void NetEqDecodingTest::TearDown() {
285 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286}
287
288void NetEqDecodingTest::LoadDecoders() {
289 // Load PCMu.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800290 ASSERT_EQ(0,
291 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMu, "pcmu", 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292 // Load PCMa.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800293 ASSERT_EQ(0,
294 neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCMa, "pcma", 8));
kwiberg98ab3a42015-09-30 21:54:21 -0700295#ifdef WEBRTC_CODEC_ILBC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 // Load iLBC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800297 ASSERT_EQ(
298 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderILBC, "ilbc", 102));
kwiberg98ab3a42015-09-30 21:54:21 -0700299#endif
300#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 // Load iSAC.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800302 ASSERT_EQ(
303 0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", 103));
kwiberg98ab3a42015-09-30 21:54:21 -0700304#endif
305#ifdef WEBRTC_CODEC_ISAC
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306 // Load iSAC SWB.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800307 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISACswb,
308 "isac-swb", 104));
kwiberg98ab3a42015-09-30 21:54:21 -0700309#endif
minyue93c08b72015-12-22 09:57:41 -0800310#ifdef WEBRTC_CODEC_OPUS
311 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderOpus,
312 "opus", 111));
313#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314 // Load PCM16B nb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800315 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16B,
316 "pcm16-nb", 93));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 // Load PCM16B wb.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800318 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
319 "pcm16-wb", 94));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320 // Load PCM16B swb32.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800321 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bswb32kHz,
322 "pcm16-swb32", 95));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 // Load CNG 8 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800324 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
325 "cng-nb", 13));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 // Load CNG 16 kHz.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800327 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
328 "cng-wb", 98));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329}
330
331void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000332 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333}
334
henrik.lundin6d8e0112016-03-04 10:34:21 -0800335void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000337 while (packet_ && sim_clock_ >= packet_->time_ms()) {
338 if (packet_->payload_length_bytes() > 0) {
339 WebRtcRTPHeader rtp_header;
340 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800341#ifndef WEBRTC_CODEC_ISAC
342 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
343 if (rtp_header.header.payloadType != 104)
344#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800346 rtp_header,
347 rtc::ArrayView<const uint8_t>(
348 packet_->payload(), packet_->payload_length_bytes()),
349 static_cast<uint32_t>(packet_->time_ms() *
350 (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.lundin55480f52016-03-08 02:37:57 -0800357 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800358 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
359 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
360 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
361 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
362 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800363 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
365 // Increase time.
366 sim_clock_ += kTimeStepMs;
367}
368
minyue4f906772016-04-29 11:05:14 -0700369void NetEqDecodingTest::DecodeAndCompare(
370 const std::string& rtp_file,
371 const std::string& output_checksum,
372 const std::string& network_stats_checksum,
373 const std::string& rtcp_stats_checksum,
374 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 OpenInputFile(rtp_file);
376
minyue4f906772016-04-29 11:05:14 -0700377 std::string ref_out_file =
378 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
379 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380
minyue4f906772016-04-29 11:05:14 -0700381 std::string stat_out_file =
382 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
383 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000384
minyue4f906772016-04-29 11:05:14 -0700385 std::string rtcp_out_file =
386 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
387 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000388
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000389 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000391 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 std::ostringstream ss;
393 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
394 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800395 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700396 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800397 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398
399 // Query the network statistics API once per second
400 if (sim_clock_ % 1000 == 0) {
401 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700402 NetEqNetworkStatistics current_network_stats;
403 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
404 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
405
henrik.lundin9c3efd02015-08-27 13:12:22 -0700406 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700407 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
408 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000409
410 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700411 RtcpStatistics current_rtcp_stats;
412 neteq_->GetRtcpStatistics(&current_rtcp_stats);
413 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414 }
415 }
minyue4f906772016-04-29 11:05:14 -0700416
417 SCOPED_TRACE("Check output audio.");
418 output.VerifyChecksum(output_checksum);
419 SCOPED_TRACE("Check network stats.");
420 network_stats.VerifyChecksum(network_stats_checksum);
421 SCOPED_TRACE("Check rtcp stats.");
422 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423}
424
425void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
426 int timestamp,
427 WebRtcRTPHeader* rtp_info) {
428 rtp_info->header.sequenceNumber = frame_index;
429 rtp_info->header.timestamp = timestamp;
430 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
431 rtp_info->header.payloadType = 94; // PCM16b WB codec.
432 rtp_info->header.markerBit = 0;
433}
434
435void NetEqDecodingTest::PopulateCng(int frame_index,
436 int timestamp,
437 WebRtcRTPHeader* rtp_info,
438 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000439 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000440 rtp_info->header.sequenceNumber = frame_index;
441 rtp_info->header.timestamp = timestamp;
442 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
443 rtp_info->header.payloadType = 98; // WB CNG.
444 rtp_info->header.markerBit = 0;
445 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
446 *payload_len = 1; // Only noise level, no spectral parameters.
447}
448
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200449// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
ivoc72c08ed2016-01-20 07:26:24 -0800450#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
451 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
452 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200453 !defined(WEBRTC_ARCH_ARM64) && !defined(UNDEFINED_SANITIZER)
minyue5f026d02015-12-16 07:36:04 -0800454#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700455#else
minyue5f026d02015-12-16 07:36:04 -0800456#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700457#endif
minyue5f026d02015-12-16 07:36:04 -0800458TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800459 const std::string input_rtp_file =
460 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000461
minyue4f906772016-04-29 11:05:14 -0700462 const std::string output_checksum = PlatformChecksum(
463 "f587883b7c371ee8d87dbf1b0f07525af7d959b8",
464 "a349bd71dba548029b05d1d2a6dc7caafab9a856",
465 "f587883b7c371ee8d87dbf1b0f07525af7d959b8",
466 "08266b198e7686b3cd9330813e0d2cd72fc8fdc2");
467
468 const std::string network_stats_checksum = PlatformChecksum(
469 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
470 "2853ab577fe571adfc7b18f77bbe58f1253d2019",
471 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
472 "2cf380a05ee07080bd72471e8ec7777a39644ec9");
473
474 const std::string rtcp_stats_checksum = PlatformChecksum(
475 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
476 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
477 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
478 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
479
480 DecodeAndCompare(input_rtp_file,
481 output_checksum,
482 network_stats_checksum,
483 rtcp_stats_checksum,
484 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000485}
486
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200487// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
minyue93c08b72015-12-22 09:57:41 -0800488#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
489 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +0200490 defined(WEBRTC_CODEC_OPUS) && !defined(UNDEFINED_SANITIZER)
minyue93c08b72015-12-22 09:57:41 -0800491#define MAYBE_TestOpusBitExactness TestOpusBitExactness
492#else
493#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
494#endif
495TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
496 const std::string input_rtp_file =
497 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800498
minyue4f906772016-04-29 11:05:14 -0700499 const std::string output_checksum = PlatformChecksum(
500 "c23004d91ffbe5e7a1f24620fc89b58c0426040f",
501 "c23004d91ffbe5e7a1f24620fc89b58c0426040f",
502 "c23004d91ffbe5e7a1f24620fc89b58c0426040f",
503 "c23004d91ffbe5e7a1f24620fc89b58c0426040f");
504
505 const std::string network_stats_checksum = PlatformChecksum(
506 "dc2d9f584efb0111ebcd71a2c86f1fb09cd8c2bb",
507 "dc2d9f584efb0111ebcd71a2c86f1fb09cd8c2bb",
508 "dc2d9f584efb0111ebcd71a2c86f1fb09cd8c2bb",
509 "dc2d9f584efb0111ebcd71a2c86f1fb09cd8c2bb");
510
511 const std::string rtcp_stats_checksum = PlatformChecksum(
512 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
513 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
514 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
515 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
516
517 DecodeAndCompare(input_rtp_file,
518 output_checksum,
519 network_stats_checksum,
520 rtcp_stats_checksum,
521 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800522}
523
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000524// Use fax mode to avoid time-scaling. This is to simplify the testing of
525// packet waiting times in the packet buffer.
526class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
527 protected:
528 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
529 config_.playout_mode = kPlayoutFax;
530 }
531};
532
533TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000534 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
535 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000536 const size_t kSamples = 10 * 16;
537 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800539 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000540 WebRtcRTPHeader rtp_info;
541 rtp_info.header.sequenceNumber = i;
542 rtp_info.header.timestamp = i * kSamples;
543 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
544 rtp_info.header.payloadType = 94; // PCM16b WB codec.
545 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800546 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000547 }
548 // Pull out all data.
549 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin55480f52016-03-08 02:37:57 -0800550 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800551 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000552 }
553
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200554 NetEqNetworkStatistics stats;
555 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000556 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
557 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200558 // each packet. Thus, we are calculating the statistics for a series from 10
559 // to 300, in steps of 10 ms.
560 EXPECT_EQ(155, stats.mean_waiting_time_ms);
561 EXPECT_EQ(155, stats.median_waiting_time_ms);
562 EXPECT_EQ(10, stats.min_waiting_time_ms);
563 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564
565 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200566 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
567 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
568 EXPECT_EQ(-1, stats.median_waiting_time_ms);
569 EXPECT_EQ(-1, stats.min_waiting_time_ms);
570 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000571}
572
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000573TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574 const int kNumFrames = 3000; // Needed for convergence.
575 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000576 const size_t kSamples = 10 * 16;
577 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578 while (frame_index < kNumFrames) {
579 // Insert one packet each time, except every 10th time where we insert two
580 // packets at once. This will create a negative clock-drift of approx. 10%.
581 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
582 for (int n = 0; n < num_packets; ++n) {
583 uint8_t payload[kPayloadBytes] = {0};
584 WebRtcRTPHeader rtp_info;
585 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800586 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587 ++frame_index;
588 }
589
590 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800591 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800592 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000593 }
594
595 NetEqNetworkStatistics network_stats;
596 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
597 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
598}
599
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000600TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000601 const int kNumFrames = 5000; // Needed for convergence.
602 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000603 const size_t kSamples = 10 * 16;
604 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 for (int i = 0; i < kNumFrames; ++i) {
606 // Insert one packet each time, except every 10th time where we don't insert
607 // any packet. This will create a positive clock-drift of approx. 11%.
608 int num_packets = (i % 10 == 9 ? 0 : 1);
609 for (int n = 0; n < num_packets; ++n) {
610 uint8_t payload[kPayloadBytes] = {0};
611 WebRtcRTPHeader rtp_info;
612 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800613 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 ++frame_index;
615 }
616
617 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800618 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800619 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000620 }
621
622 NetEqNetworkStatistics network_stats;
623 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
624 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
625}
626
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000627void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
628 double network_freeze_ms,
629 bool pull_audio_during_freeze,
630 int delay_tolerance_ms,
631 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632 uint16_t seq_no = 0;
633 uint32_t timestamp = 0;
634 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000635 const size_t kSamples = kFrameSizeMs * 16;
636 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000637 double next_input_time_ms = 0.0;
638 double t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639
640 // Insert speech for 5 seconds.
641 const int kSpeechDurationMs = 5000;
642 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
643 // Each turn in this for loop is 10 ms.
644 while (next_input_time_ms <= t_ms) {
645 // Insert one 30 ms speech frame.
646 uint8_t payload[kPayloadBytes] = {0};
647 WebRtcRTPHeader rtp_info;
648 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800649 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650 ++seq_no;
651 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000652 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000653 }
654 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800655 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800656 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 }
658
henrik.lundin55480f52016-03-08 02:37:57 -0800659 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700660 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
661 ASSERT_TRUE(playout_timestamp);
662 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000663
664 // Insert CNG for 1 minute (= 60000 ms).
665 const int kCngPeriodMs = 100;
666 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
667 const int kCngDurationMs = 60000;
668 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
669 // Each turn in this for loop is 10 ms.
670 while (next_input_time_ms <= t_ms) {
671 // Insert one CNG frame each 100 ms.
672 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000673 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000674 WebRtcRTPHeader rtp_info;
675 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800676 ASSERT_EQ(0, neteq_->InsertPacket(
677 rtp_info,
678 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000679 ++seq_no;
680 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000681 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000682 }
683 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800684 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800685 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000686 }
687
henrik.lundin55480f52016-03-08 02:37:57 -0800688 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000689
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000690 if (network_freeze_ms > 0) {
691 // First keep pulling audio for |network_freeze_ms| without inserting
692 // any data, then insert CNG data corresponding to |network_freeze_ms|
693 // without pulling any output audio.
694 const double loop_end_time = t_ms + network_freeze_ms;
695 for (; t_ms < loop_end_time; t_ms += 10) {
696 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800697 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800698 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800699 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000700 }
701 bool pull_once = pull_audio_during_freeze;
702 // If |pull_once| is true, GetAudio will be called once half-way through
703 // the network recovery period.
704 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
705 while (next_input_time_ms <= t_ms) {
706 if (pull_once && next_input_time_ms >= pull_time_ms) {
707 pull_once = false;
708 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800709 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800710 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800711 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000712 t_ms += 10;
713 }
714 // Insert one CNG frame each 100 ms.
715 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000716 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000717 WebRtcRTPHeader rtp_info;
718 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800719 ASSERT_EQ(0, neteq_->InsertPacket(
720 rtp_info,
721 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000722 ++seq_no;
723 timestamp += kCngPeriodSamples;
724 next_input_time_ms += kCngPeriodMs * drift_factor;
725 }
726 }
727
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000728 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000729 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800730 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 // Each turn in this for loop is 10 ms.
732 while (next_input_time_ms <= t_ms) {
733 // Insert one 30 ms speech frame.
734 uint8_t payload[kPayloadBytes] = {0};
735 WebRtcRTPHeader rtp_info;
736 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800737 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738 ++seq_no;
739 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741 }
742 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800743 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800744 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000745 // Increase clock.
746 t_ms += 10;
747 }
748
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000749 // Check that the speech starts again within reasonable time.
750 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
751 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700752 playout_timestamp = PlayoutTimestamp();
753 ASSERT_TRUE(playout_timestamp);
754 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000756 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
757 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000758}
759
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000760TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000761 // Apply a clock drift of -25 ms / s (sender faster than receiver).
762 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000763 const double kNetworkFreezeTimeMs = 0.0;
764 const bool kGetAudioDuringFreezeRecovery = false;
765 const int kDelayToleranceMs = 20;
766 const int kMaxTimeToSpeechMs = 100;
767 LongCngWithClockDrift(kDriftFactor,
768 kNetworkFreezeTimeMs,
769 kGetAudioDuringFreezeRecovery,
770 kDelayToleranceMs,
771 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000772}
773
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000774TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000775 // Apply a clock drift of +25 ms / s (sender slower than receiver).
776 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000777 const double kNetworkFreezeTimeMs = 0.0;
778 const bool kGetAudioDuringFreezeRecovery = false;
779 const int kDelayToleranceMs = 20;
780 const int kMaxTimeToSpeechMs = 100;
781 LongCngWithClockDrift(kDriftFactor,
782 kNetworkFreezeTimeMs,
783 kGetAudioDuringFreezeRecovery,
784 kDelayToleranceMs,
785 kMaxTimeToSpeechMs);
786}
787
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000788TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000789 // Apply a clock drift of -25 ms / s (sender faster than receiver).
790 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
791 const double kNetworkFreezeTimeMs = 5000.0;
792 const bool kGetAudioDuringFreezeRecovery = false;
793 const int kDelayToleranceMs = 50;
794 const int kMaxTimeToSpeechMs = 200;
795 LongCngWithClockDrift(kDriftFactor,
796 kNetworkFreezeTimeMs,
797 kGetAudioDuringFreezeRecovery,
798 kDelayToleranceMs,
799 kMaxTimeToSpeechMs);
800}
801
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000802TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000803 // Apply a clock drift of +25 ms / s (sender slower than receiver).
804 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
805 const double kNetworkFreezeTimeMs = 5000.0;
806 const bool kGetAudioDuringFreezeRecovery = false;
807 const int kDelayToleranceMs = 20;
808 const int kMaxTimeToSpeechMs = 100;
809 LongCngWithClockDrift(kDriftFactor,
810 kNetworkFreezeTimeMs,
811 kGetAudioDuringFreezeRecovery,
812 kDelayToleranceMs,
813 kMaxTimeToSpeechMs);
814}
815
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000816TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000817 // Apply a clock drift of +25 ms / s (sender slower than receiver).
818 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
819 const double kNetworkFreezeTimeMs = 5000.0;
820 const bool kGetAudioDuringFreezeRecovery = true;
821 const int kDelayToleranceMs = 20;
822 const int kMaxTimeToSpeechMs = 100;
823 LongCngWithClockDrift(kDriftFactor,
824 kNetworkFreezeTimeMs,
825 kGetAudioDuringFreezeRecovery,
826 kDelayToleranceMs,
827 kMaxTimeToSpeechMs);
828}
829
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000830TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000831 const double kDriftFactor = 1.0; // No drift.
832 const double kNetworkFreezeTimeMs = 0.0;
833 const bool kGetAudioDuringFreezeRecovery = false;
834 const int kDelayToleranceMs = 10;
835 const int kMaxTimeToSpeechMs = 50;
836 LongCngWithClockDrift(kDriftFactor,
837 kNetworkFreezeTimeMs,
838 kGetAudioDuringFreezeRecovery,
839 kDelayToleranceMs,
840 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000841}
842
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000843TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000844 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000845 uint8_t payload[kPayloadBytes] = {0};
846 WebRtcRTPHeader rtp_info;
847 PopulateRtpInfo(0, 0, &rtp_info);
848 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800849 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000850 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
851}
852
Peter Boströme2976c82016-01-04 22:44:05 +0100853#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800854#define MAYBE_DecoderError DecoderError
855#else
856#define MAYBE_DecoderError DISABLED_DecoderError
857#endif
858
Peter Boströme2976c82016-01-04 22:44:05 +0100859TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000860 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000861 uint8_t payload[kPayloadBytes] = {0};
862 WebRtcRTPHeader rtp_info;
863 PopulateRtpInfo(0, 0, &rtp_info);
864 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800865 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000866 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
867 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800868 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
869 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 }
henrik.lundin55480f52016-03-08 02:37:57 -0800871 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000872 // Verify that there is a decoder error to check.
873 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800874
875 enum NetEqDecoderError {
876 ISAC_LENGTH_MISMATCH = 6730,
877 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
878 };
879#if defined(WEBRTC_CODEC_ISAC)
880 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
881#elif defined(WEBRTC_CODEC_ISACFX)
882 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
883#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000884 // Verify that the first 160 samples are set to 0, and that the remaining
885 // samples are left unmodified.
886 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
887 for (int i = 0; i < kExpectedOutputLength; ++i) {
888 std::ostringstream ss;
889 ss << "i = " << i;
890 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800891 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000892 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800893 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
894 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000895 std::ostringstream ss;
896 ss << "i = " << i;
897 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800898 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000899 }
900}
901
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000902TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000903 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
904 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800905 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
906 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000907 }
henrik.lundin55480f52016-03-08 02:37:57 -0800908 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 // Verify that the first block of samples is set to 0.
910 static const int kExpectedOutputLength =
911 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
912 for (int i = 0; i < kExpectedOutputLength; ++i) {
913 std::ostringstream ss;
914 ss << "i = " << i;
915 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000917 }
henrik.lundind89814b2015-11-23 06:49:25 -0800918 // Verify that the sample rate did not change from the initial configuration.
919 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000920}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000921
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000922class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000923 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000924 virtual void TestCondition(double sum_squared_noise,
925 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000926
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000927 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700928 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000929 uint8_t payload_type = 0xFF; // Invalid.
930 if (sampling_rate_hz == 8000) {
931 expected_samples_per_channel = kBlockSize8kHz;
932 payload_type = 93; // PCM 16, 8 kHz.
933 } else if (sampling_rate_hz == 16000) {
934 expected_samples_per_channel = kBlockSize16kHz;
935 payload_type = 94; // PCM 16, 16 kHZ.
936 } else if (sampling_rate_hz == 32000) {
937 expected_samples_per_channel = kBlockSize32kHz;
938 payload_type = 95; // PCM 16, 32 kHz.
939 } else {
940 ASSERT_TRUE(false); // Unsupported test case.
941 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000942
henrik.lundin6d8e0112016-03-04 10:34:21 -0800943 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000944 test::AudioLoop input;
945 // We are using the same 32 kHz input file for all tests, regardless of
946 // |sampling_rate_hz|. The output may sound weird, but the test is still
947 // valid.
948 ASSERT_TRUE(input.Init(
949 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
950 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700951 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000952
953 // Payload of 10 ms of PCM16 32 kHz.
954 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000955 WebRtcRTPHeader rtp_info;
956 PopulateRtpInfo(0, 0, &rtp_info);
957 rtp_info.header.payloadType = payload_type;
958
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000959 uint32_t receive_timestamp = 0;
960 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800961 auto block = input.GetNextBlock();
962 ASSERT_EQ(expected_samples_per_channel, block.size());
963 size_t enc_len_bytes =
964 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000965 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
966
kwibergee2bac22015-11-11 10:34:00 -0800967 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
968 payload, enc_len_bytes),
969 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 output.Reset();
henrik.lundin55480f52016-03-08 02:37:57 -0800971 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800972 ASSERT_EQ(1u, output.num_channels_);
973 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800974 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000975
976 // Next packet.
977 rtp_info.header.timestamp += expected_samples_per_channel;
978 rtp_info.header.sequenceNumber++;
979 receive_timestamp += expected_samples_per_channel;
980 }
981
henrik.lundin6d8e0112016-03-04 10:34:21 -0800982 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000983
984 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
985 // one frame without checking speech-type. This is the first frame pulled
986 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin55480f52016-03-08 02:37:57 -0800987 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800988 ASSERT_EQ(1u, output.num_channels_);
989 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000990
991 // To be able to test the fading of background noise we need at lease to
992 // pull 611 frames.
993 const int kFadingThreshold = 611;
994
995 // Test several CNG-to-PLC packet for the expected behavior. The number 20
996 // is arbitrary, but sufficiently large to test enough number of frames.
997 const int kNumPlcToCngTestFrames = 20;
998 bool plc_to_cng = false;
999 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001000 output.Reset();
1001 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin55480f52016-03-08 02:37:57 -08001002 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001003 ASSERT_EQ(1u, output.num_channels_);
1004 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001005 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001006 plc_to_cng = true;
1007 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001008 for (size_t k = 0;
1009 k < output.num_channels_ * output.samples_per_channel_; ++k)
1010 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001011 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001012 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001013 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001014 }
1015 }
1016 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1017 }
1018};
1019
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001020class NetEqBgnTestOn : public NetEqBgnTest {
1021 protected:
1022 NetEqBgnTestOn() : NetEqBgnTest() {
1023 config_.background_noise_mode = NetEq::kBgnOn;
1024 }
1025
1026 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1027 EXPECT_NE(0, sum_squared_noise);
1028 }
1029};
1030
1031class NetEqBgnTestOff : public NetEqBgnTest {
1032 protected:
1033 NetEqBgnTestOff() : NetEqBgnTest() {
1034 config_.background_noise_mode = NetEq::kBgnOff;
1035 }
1036
1037 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1038 EXPECT_EQ(0, sum_squared_noise);
1039 }
1040};
1041
1042class NetEqBgnTestFade : public NetEqBgnTest {
1043 protected:
1044 NetEqBgnTestFade() : NetEqBgnTest() {
1045 config_.background_noise_mode = NetEq::kBgnFade;
1046 }
1047
1048 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1049 if (should_be_faded)
1050 EXPECT_EQ(0, sum_squared_noise);
1051 }
1052};
1053
henrika1d34fe92015-06-16 10:04:20 +02001054TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001055 CheckBgn(8000);
1056 CheckBgn(16000);
1057 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001058}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001059
henrika1d34fe92015-06-16 10:04:20 +02001060TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001061 CheckBgn(8000);
1062 CheckBgn(16000);
1063 CheckBgn(32000);
1064}
1065
henrika1d34fe92015-06-16 10:04:20 +02001066TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001067 CheckBgn(8000);
1068 CheckBgn(16000);
1069 CheckBgn(32000);
1070}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001071
Peter Boströme2976c82016-01-04 22:44:05 +01001072#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001073#define MAYBE_SyncPacketInsert SyncPacketInsert
1074#else
1075#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1076#endif
1077TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001078 WebRtcRTPHeader rtp_info;
1079 uint32_t receive_timestamp = 0;
1080 // For the readability use the following payloads instead of the defaults of
1081 // this test.
1082 uint8_t kPcm16WbPayloadType = 1;
1083 uint8_t kCngNbPayloadType = 2;
1084 uint8_t kCngWbPayloadType = 3;
1085 uint8_t kCngSwb32PayloadType = 4;
1086 uint8_t kCngSwb48PayloadType = 5;
1087 uint8_t kAvtPayloadType = 6;
1088 uint8_t kRedPayloadType = 7;
1089 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1090
1091 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001092 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001093 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001094 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001095 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001096 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001097 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001098 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001099 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001100 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001101 "cng-swb48", kCngSwb48PayloadType));
1102 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001103 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001104 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001105 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001106 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001107 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001108
1109 PopulateRtpInfo(0, 0, &rtp_info);
1110 rtp_info.header.payloadType = kPcm16WbPayloadType;
1111
1112 // The first packet injected cannot be sync-packet.
1113 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1114
1115 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001116 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001117 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001118 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001119
1120 // Next packet. Last packet contained 10 ms audio.
1121 rtp_info.header.sequenceNumber++;
1122 rtp_info.header.timestamp += kBlockSize16kHz;
1123 receive_timestamp += kBlockSize16kHz;
1124
1125 // Unacceptable payload types CNG, AVT (DTMF), RED.
1126 rtp_info.header.payloadType = kCngNbPayloadType;
1127 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1128
1129 rtp_info.header.payloadType = kCngWbPayloadType;
1130 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1131
1132 rtp_info.header.payloadType = kCngSwb32PayloadType;
1133 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1134
1135 rtp_info.header.payloadType = kCngSwb48PayloadType;
1136 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1137
1138 rtp_info.header.payloadType = kAvtPayloadType;
1139 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1140
1141 rtp_info.header.payloadType = kRedPayloadType;
1142 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1143
1144 // Change of codec cannot be initiated with a sync packet.
1145 rtp_info.header.payloadType = kIsacPayloadType;
1146 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1147
1148 // Change of SSRC is not allowed with a sync packet.
1149 rtp_info.header.payloadType = kPcm16WbPayloadType;
1150 ++rtp_info.header.ssrc;
1151 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1152
1153 --rtp_info.header.ssrc;
1154 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1155}
1156
1157// First insert several noise like packets, then sync-packets. Decoding all
1158// packets should not produce error, statistics should not show any packet loss
1159// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001160// TODO(turajs) we will have a better test if we have a referece NetEq, and
1161// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1162// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001163TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001164 WebRtcRTPHeader rtp_info;
1165 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001166 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001167 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001168 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001169 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001170 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001171 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1172 }
1173 // Insert some packets which decode to noise. We are not interested in
1174 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001175 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001176 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001177 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001178 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001179 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1180 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001181
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));
henrik.lundin55480f52016-03-08 02:37:57 -08001194 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001195 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1196 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001197 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001198 EXPECT_TRUE(IsAllZero(
1199 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001200 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001201 rtp_info.header.sequenceNumber++;
1202 rtp_info.header.timestamp += kBlockSize16kHz;
1203 receive_timestamp += kBlockSize16kHz;
1204 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001205
1206 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001207 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001208 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001209 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001210 ASSERT_EQ(0, neteq_->GetAudio(&output));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001211 if (n >= algorithmic_frame_delay + 1) {
1212 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001213 EXPECT_TRUE(IsAllNonZero(
1214 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001215 }
1216 rtp_info.header.sequenceNumber++;
1217 rtp_info.header.timestamp += kBlockSize16kHz;
1218 receive_timestamp += kBlockSize16kHz;
1219 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001220 NetEqNetworkStatistics network_stats;
1221 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1222 // Expecting a "clean" network.
1223 EXPECT_EQ(0, network_stats.packet_loss_rate);
1224 EXPECT_EQ(0, network_stats.expand_rate);
1225 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001226 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001227}
1228
1229// Test if the size of the packet buffer reported correctly when containing
1230// sync packets. Also, test if network packets override sync packets. That is to
1231// prefer decoding a network packet to a sync packet, if both have same sequence
1232// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001233TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001234 WebRtcRTPHeader rtp_info;
1235 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001236 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001237 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001238 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001239 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001240 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1241 }
1242 // Insert some packets which decode to noise. We are not interested in
1243 // actual decoded values.
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) {
kwibergee2bac22015-11-11 10:34:00 -08001247 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001248 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001249 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1250 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001251 rtp_info.header.sequenceNumber++;
1252 rtp_info.header.timestamp += kBlockSize16kHz;
1253 receive_timestamp += kBlockSize16kHz;
1254 }
1255 const int kNumSyncPackets = 10;
1256
1257 WebRtcRTPHeader first_sync_packet_rtp_info;
1258 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1259
1260 // Insert sync-packets, but no decoding.
1261 for (int n = 0; n < kNumSyncPackets; ++n) {
1262 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1263 rtp_info.header.sequenceNumber++;
1264 rtp_info.header.timestamp += kBlockSize16kHz;
1265 receive_timestamp += kBlockSize16kHz;
1266 }
1267 NetEqNetworkStatistics network_stats;
1268 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001269 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1270 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001271
1272 // Rewind |rtp_info| to that of the first sync packet.
1273 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1274
1275 // Insert.
1276 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001277 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001278 rtp_info.header.sequenceNumber++;
1279 rtp_info.header.timestamp += kBlockSize16kHz;
1280 receive_timestamp += kBlockSize16kHz;
1281 }
1282
1283 // Decode.
1284 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin55480f52016-03-08 02:37:57 -08001285 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001286 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1287 ASSERT_EQ(1u, output.num_channels_);
1288 EXPECT_TRUE(IsAllNonZero(
1289 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001290 }
1291}
1292
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001293void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1294 uint32_t start_timestamp,
1295 const std::set<uint16_t>& drop_seq_numbers,
1296 bool expect_seq_no_wrap,
1297 bool expect_timestamp_wrap) {
1298 uint16_t seq_no = start_seq_no;
1299 uint32_t timestamp = start_timestamp;
1300 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1301 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1302 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001303 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001304 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001305 uint32_t receive_timestamp = 0;
1306
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001307 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001308 const int kSpeechDurationMs = 2000;
1309 int packets_inserted = 0;
1310 uint16_t last_seq_no;
1311 uint32_t last_timestamp;
1312 bool timestamp_wrapped = false;
1313 bool seq_no_wrapped = false;
1314 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1315 // Each turn in this for loop is 10 ms.
1316 while (next_input_time_ms <= t_ms) {
1317 // Insert one 30 ms speech frame.
1318 uint8_t payload[kPayloadBytes] = {0};
1319 WebRtcRTPHeader rtp_info;
1320 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1321 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1322 // This sequence number was not in the set to drop. Insert it.
1323 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001324 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001325 ++packets_inserted;
1326 }
1327 NetEqNetworkStatistics network_stats;
1328 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1329
1330 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1331 // packet size for first few packets. Therefore we refrain from checking
1332 // the criteria.
1333 if (packets_inserted > 4) {
1334 // Expect preferred and actual buffer size to be no more than 2 frames.
1335 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001336 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1337 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001338 }
1339 last_seq_no = seq_no;
1340 last_timestamp = timestamp;
1341
1342 ++seq_no;
1343 timestamp += kSamples;
1344 receive_timestamp += kSamples;
1345 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1346
1347 seq_no_wrapped |= seq_no < last_seq_no;
1348 timestamp_wrapped |= timestamp < last_timestamp;
1349 }
1350 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001351 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -08001352 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001353 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1354 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001355
1356 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001357 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1358 ASSERT_TRUE(playout_timestamp);
1359 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001360 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001361 }
1362 // Make sure we have actually tested wrap-around.
1363 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1364 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1365}
1366
1367TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1368 // Start with a sequence number that will soon wrap.
1369 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1370 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1371}
1372
1373TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1374 // Start with a sequence number that will soon wrap.
1375 std::set<uint16_t> drop_seq_numbers;
1376 drop_seq_numbers.insert(0xFFFF);
1377 drop_seq_numbers.insert(0x0);
1378 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1379}
1380
1381TEST_F(NetEqDecodingTest, TimestampWrap) {
1382 // Start with a timestamp that will soon wrap.
1383 std::set<uint16_t> drop_seq_numbers;
1384 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1385}
1386
1387TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1388 // Start with a timestamp and a sequence number that will wrap at the same
1389 // time.
1390 std::set<uint16_t> drop_seq_numbers;
1391 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1392}
1393
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001394void NetEqDecodingTest::DuplicateCng() {
1395 uint16_t seq_no = 0;
1396 uint32_t timestamp = 0;
1397 const int kFrameSizeMs = 10;
1398 const int kSampleRateKhz = 16;
1399 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001400 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001401
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001402 const int algorithmic_delay_samples = std::max(
1403 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001404 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001405 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001406 uint8_t payload[kPayloadBytes] = {0};
1407 WebRtcRTPHeader rtp_info;
1408 for (int i = 0; i < 3; ++i) {
1409 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001410 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001411 ++seq_no;
1412 timestamp += kSamples;
1413
1414 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001415 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001416 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001417 }
1418 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001419 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001420
1421 // Insert same CNG packet twice.
1422 const int kCngPeriodMs = 100;
1423 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001424 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001425 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1426 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001427 ASSERT_EQ(
1428 0, neteq_->InsertPacket(
1429 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001430
1431 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001432 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001433 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001434 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001435 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1436 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1437 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001438
1439 // Insert the same CNG packet again. Note that at this point it is old, since
1440 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001441 ASSERT_EQ(
1442 0, neteq_->InsertPacket(
1443 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001444
1445 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1446 // we have already pulled out CNG once.
1447 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin55480f52016-03-08 02:37:57 -08001448 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001449 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001450 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001451 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001452 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001453 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001454 }
1455
1456 // Insert speech again.
1457 ++seq_no;
1458 timestamp += kCngPeriodSamples;
1459 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001460 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001461
1462 // Pull audio once and verify that the output is speech again.
henrik.lundin55480f52016-03-08 02:37:57 -08001463 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001464 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001465 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001466 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1467 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001468 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001469 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001470}
1471
henrik.lundin0d96ab72016-04-06 12:28:26 -07001472rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1473 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001474}
1475
1476TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001477
1478TEST_F(NetEqDecodingTest, CngFirst) {
1479 uint16_t seq_no = 0;
1480 uint32_t timestamp = 0;
1481 const int kFrameSizeMs = 10;
1482 const int kSampleRateKhz = 16;
1483 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1484 const int kPayloadBytes = kSamples * 2;
1485 const int kCngPeriodMs = 100;
1486 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1487 size_t payload_len;
1488
1489 uint8_t payload[kPayloadBytes] = {0};
1490 WebRtcRTPHeader rtp_info;
1491
1492 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001493 ASSERT_EQ(
1494 NetEq::kOK,
1495 neteq_->InsertPacket(
1496 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001497 ++seq_no;
1498 timestamp += kCngPeriodSamples;
1499
1500 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001501 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001502 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001503 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001504
1505 // Insert some speech packets.
1506 for (int i = 0; i < 3; ++i) {
1507 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001508 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001509 ++seq_no;
1510 timestamp += kSamples;
1511
1512 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001513 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001514 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001515 }
1516 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001517 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001518}
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001519} // namespace webrtc