blob: 69c3876354776ce309bad9fd6fa8773a51f5ec8a [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
ivoc72c08ed2016-01-20 07:26:24 -0800449#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
450 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
451 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700452 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800453#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700454#else
minyue5f026d02015-12-16 07:36:04 -0800455#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700456#endif
minyue5f026d02015-12-16 07:36:04 -0800457TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800458 const std::string input_rtp_file =
459 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000460
minyue4f906772016-04-29 11:05:14 -0700461 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700462 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
463 "d2a6b6ff54b340cf9f961c7f07768d86b3761073",
464 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
465 "f9749813dbc3fb59dae761de518fec65b8407c5b");
minyue4f906772016-04-29 11:05:14 -0700466
467 const std::string network_stats_checksum = PlatformChecksum(
468 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
minyue53ff70f2016-05-02 01:50:30 -0700469 "01be67dc4c3b8e74743a45cbd8684c0535dec9ad",
minyue4f906772016-04-29 11:05:14 -0700470 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
471 "2cf380a05ee07080bd72471e8ec7777a39644ec9");
472
473 const std::string rtcp_stats_checksum = PlatformChecksum(
474 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
475 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
476 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
477 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
478
479 DecodeAndCompare(input_rtp_file,
480 output_checksum,
481 network_stats_checksum,
482 rtcp_stats_checksum,
483 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000484}
485
minyue93c08b72015-12-22 09:57:41 -0800486#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
487 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
pbosc7a65692016-05-06 12:50:04 -0700488 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800489#define MAYBE_TestOpusBitExactness TestOpusBitExactness
490#else
491#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
492#endif
493TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
494 const std::string input_rtp_file =
495 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800496
minyue4f906772016-04-29 11:05:14 -0700497 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700498 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
499 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
500 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
501 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4");
minyue4f906772016-04-29 11:05:14 -0700502
503 const std::string network_stats_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700504 "6eab76efbde753d4dde38983445ca16b4ce59b39",
505 "6eab76efbde753d4dde38983445ca16b4ce59b39",
506 "6eab76efbde753d4dde38983445ca16b4ce59b39",
507 "6eab76efbde753d4dde38983445ca16b4ce59b39");
minyue4f906772016-04-29 11:05:14 -0700508
509 const std::string rtcp_stats_checksum = PlatformChecksum(
510 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
511 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
512 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
513 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
514
515 DecodeAndCompare(input_rtp_file,
516 output_checksum,
517 network_stats_checksum,
518 rtcp_stats_checksum,
519 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800520}
521
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000522// Use fax mode to avoid time-scaling. This is to simplify the testing of
523// packet waiting times in the packet buffer.
524class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
525 protected:
526 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
527 config_.playout_mode = kPlayoutFax;
528 }
529};
530
531TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000532 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
533 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000534 const size_t kSamples = 10 * 16;
535 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000536 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800537 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538 WebRtcRTPHeader rtp_info;
539 rtp_info.header.sequenceNumber = i;
540 rtp_info.header.timestamp = i * kSamples;
541 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
542 rtp_info.header.payloadType = 94; // PCM16b WB codec.
543 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800544 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545 }
546 // Pull out all data.
547 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin55480f52016-03-08 02:37:57 -0800548 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800549 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000550 }
551
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200552 NetEqNetworkStatistics stats;
553 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
555 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200556 // each packet. Thus, we are calculating the statistics for a series from 10
557 // to 300, in steps of 10 ms.
558 EXPECT_EQ(155, stats.mean_waiting_time_ms);
559 EXPECT_EQ(155, stats.median_waiting_time_ms);
560 EXPECT_EQ(10, stats.min_waiting_time_ms);
561 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000562
563 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200564 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
565 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
566 EXPECT_EQ(-1, stats.median_waiting_time_ms);
567 EXPECT_EQ(-1, stats.min_waiting_time_ms);
568 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000569}
570
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000571TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572 const int kNumFrames = 3000; // 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 while (frame_index < kNumFrames) {
577 // Insert one packet each time, except every 10th time where we insert two
578 // packets at once. This will create a negative clock-drift of approx. 10%.
579 int num_packets = (frame_index % 10 == 0 ? 2 : 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);
kwibergee2bac22015-11-11 10:34:00 -0800584 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585 ++frame_index;
586 }
587
588 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800589 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800590 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 }
592
593 NetEqNetworkStatistics network_stats;
594 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
595 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
596}
597
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000598TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000599 const int kNumFrames = 5000; // Needed for convergence.
600 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000601 const size_t kSamples = 10 * 16;
602 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000603 for (int i = 0; i < kNumFrames; ++i) {
604 // Insert one packet each time, except every 10th time where we don't insert
605 // any packet. This will create a positive clock-drift of approx. 11%.
606 int num_packets = (i % 10 == 9 ? 0 : 1);
607 for (int n = 0; n < num_packets; ++n) {
608 uint8_t payload[kPayloadBytes] = {0};
609 WebRtcRTPHeader rtp_info;
610 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800611 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 ++frame_index;
613 }
614
615 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800616 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800617 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000618 }
619
620 NetEqNetworkStatistics network_stats;
621 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
622 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
623}
624
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000625void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
626 double network_freeze_ms,
627 bool pull_audio_during_freeze,
628 int delay_tolerance_ms,
629 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000630 uint16_t seq_no = 0;
631 uint32_t timestamp = 0;
632 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000633 const size_t kSamples = kFrameSizeMs * 16;
634 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635 double next_input_time_ms = 0.0;
636 double t_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000637
638 // Insert speech for 5 seconds.
639 const int kSpeechDurationMs = 5000;
640 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
641 // Each turn in this for loop is 10 ms.
642 while (next_input_time_ms <= t_ms) {
643 // Insert one 30 ms speech frame.
644 uint8_t payload[kPayloadBytes] = {0};
645 WebRtcRTPHeader rtp_info;
646 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800647 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000648 ++seq_no;
649 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000650 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000651 }
652 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800653 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800654 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000655 }
656
henrik.lundin55480f52016-03-08 02:37:57 -0800657 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700658 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
659 ASSERT_TRUE(playout_timestamp);
660 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661
662 // Insert CNG for 1 minute (= 60000 ms).
663 const int kCngPeriodMs = 100;
664 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
665 const int kCngDurationMs = 60000;
666 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
667 // Each turn in this for loop is 10 ms.
668 while (next_input_time_ms <= t_ms) {
669 // Insert one CNG frame each 100 ms.
670 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000671 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000672 WebRtcRTPHeader rtp_info;
673 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800674 ASSERT_EQ(0, neteq_->InsertPacket(
675 rtp_info,
676 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677 ++seq_no;
678 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000679 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000680 }
681 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800682 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800683 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000684 }
685
henrik.lundin55480f52016-03-08 02:37:57 -0800686 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000687
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000688 if (network_freeze_ms > 0) {
689 // First keep pulling audio for |network_freeze_ms| without inserting
690 // any data, then insert CNG data corresponding to |network_freeze_ms|
691 // without pulling any output audio.
692 const double loop_end_time = t_ms + network_freeze_ms;
693 for (; t_ms < loop_end_time; t_ms += 10) {
694 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800695 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800696 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800697 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000698 }
699 bool pull_once = pull_audio_during_freeze;
700 // If |pull_once| is true, GetAudio will be called once half-way through
701 // the network recovery period.
702 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
703 while (next_input_time_ms <= t_ms) {
704 if (pull_once && next_input_time_ms >= pull_time_ms) {
705 pull_once = false;
706 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800707 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800708 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800709 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000710 t_ms += 10;
711 }
712 // Insert one CNG frame each 100 ms.
713 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000714 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 WebRtcRTPHeader rtp_info;
716 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800717 ASSERT_EQ(0, neteq_->InsertPacket(
718 rtp_info,
719 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000720 ++seq_no;
721 timestamp += kCngPeriodSamples;
722 next_input_time_ms += kCngPeriodMs * drift_factor;
723 }
724 }
725
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000726 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000727 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800728 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 // Each turn in this for loop is 10 ms.
730 while (next_input_time_ms <= t_ms) {
731 // Insert one 30 ms speech frame.
732 uint8_t payload[kPayloadBytes] = {0};
733 WebRtcRTPHeader rtp_info;
734 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800735 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000736 ++seq_no;
737 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000738 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739 }
740 // Pull out data once.
henrik.lundin55480f52016-03-08 02:37:57 -0800741 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800742 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 // Increase clock.
744 t_ms += 10;
745 }
746
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000747 // Check that the speech starts again within reasonable time.
748 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
749 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700750 playout_timestamp = PlayoutTimestamp();
751 ASSERT_TRUE(playout_timestamp);
752 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000753 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000754 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
755 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000756}
757
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000758TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000759 // Apply a clock drift of -25 ms / s (sender faster than receiver).
760 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000761 const double kNetworkFreezeTimeMs = 0.0;
762 const bool kGetAudioDuringFreezeRecovery = false;
763 const int kDelayToleranceMs = 20;
764 const int kMaxTimeToSpeechMs = 100;
765 LongCngWithClockDrift(kDriftFactor,
766 kNetworkFreezeTimeMs,
767 kGetAudioDuringFreezeRecovery,
768 kDelayToleranceMs,
769 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000770}
771
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000772TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000773 // Apply a clock drift of +25 ms / s (sender slower than receiver).
774 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000775 const double kNetworkFreezeTimeMs = 0.0;
776 const bool kGetAudioDuringFreezeRecovery = false;
777 const int kDelayToleranceMs = 20;
778 const int kMaxTimeToSpeechMs = 100;
779 LongCngWithClockDrift(kDriftFactor,
780 kNetworkFreezeTimeMs,
781 kGetAudioDuringFreezeRecovery,
782 kDelayToleranceMs,
783 kMaxTimeToSpeechMs);
784}
785
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000786TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000787 // Apply a clock drift of -25 ms / s (sender faster than receiver).
788 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
789 const double kNetworkFreezeTimeMs = 5000.0;
790 const bool kGetAudioDuringFreezeRecovery = false;
791 const int kDelayToleranceMs = 50;
792 const int kMaxTimeToSpeechMs = 200;
793 LongCngWithClockDrift(kDriftFactor,
794 kNetworkFreezeTimeMs,
795 kGetAudioDuringFreezeRecovery,
796 kDelayToleranceMs,
797 kMaxTimeToSpeechMs);
798}
799
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000800TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000801 // Apply a clock drift of +25 ms / s (sender slower than receiver).
802 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
803 const double kNetworkFreezeTimeMs = 5000.0;
804 const bool kGetAudioDuringFreezeRecovery = false;
805 const int kDelayToleranceMs = 20;
806 const int kMaxTimeToSpeechMs = 100;
807 LongCngWithClockDrift(kDriftFactor,
808 kNetworkFreezeTimeMs,
809 kGetAudioDuringFreezeRecovery,
810 kDelayToleranceMs,
811 kMaxTimeToSpeechMs);
812}
813
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000814TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000815 // Apply a clock drift of +25 ms / s (sender slower than receiver).
816 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
817 const double kNetworkFreezeTimeMs = 5000.0;
818 const bool kGetAudioDuringFreezeRecovery = true;
819 const int kDelayToleranceMs = 20;
820 const int kMaxTimeToSpeechMs = 100;
821 LongCngWithClockDrift(kDriftFactor,
822 kNetworkFreezeTimeMs,
823 kGetAudioDuringFreezeRecovery,
824 kDelayToleranceMs,
825 kMaxTimeToSpeechMs);
826}
827
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000828TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000829 const double kDriftFactor = 1.0; // No drift.
830 const double kNetworkFreezeTimeMs = 0.0;
831 const bool kGetAudioDuringFreezeRecovery = false;
832 const int kDelayToleranceMs = 10;
833 const int kMaxTimeToSpeechMs = 50;
834 LongCngWithClockDrift(kDriftFactor,
835 kNetworkFreezeTimeMs,
836 kGetAudioDuringFreezeRecovery,
837 kDelayToleranceMs,
838 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000839}
840
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000841TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000842 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000843 uint8_t payload[kPayloadBytes] = {0};
844 WebRtcRTPHeader rtp_info;
845 PopulateRtpInfo(0, 0, &rtp_info);
846 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800847 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000848 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
849}
850
Peter Boströme2976c82016-01-04 22:44:05 +0100851#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800852#define MAYBE_DecoderError DecoderError
853#else
854#define MAYBE_DecoderError DISABLED_DecoderError
855#endif
856
Peter Boströme2976c82016-01-04 22:44:05 +0100857TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000858 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000859 uint8_t payload[kPayloadBytes] = {0};
860 WebRtcRTPHeader rtp_info;
861 PopulateRtpInfo(0, 0, &rtp_info);
862 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800863 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000864 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
865 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800866 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
867 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000868 }
henrik.lundin55480f52016-03-08 02:37:57 -0800869 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 // Verify that there is a decoder error to check.
871 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800872
873 enum NetEqDecoderError {
874 ISAC_LENGTH_MISMATCH = 6730,
875 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
876 };
877#if defined(WEBRTC_CODEC_ISAC)
878 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
879#elif defined(WEBRTC_CODEC_ISACFX)
880 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
881#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 // Verify that the first 160 samples are set to 0, and that the remaining
883 // samples are left unmodified.
884 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
885 for (int i = 0; i < kExpectedOutputLength; ++i) {
886 std::ostringstream ss;
887 ss << "i = " << i;
888 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800889 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000890 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800891 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
892 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000893 std::ostringstream ss;
894 ss << "i = " << i;
895 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800896 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000897 }
898}
899
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000900TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000901 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
902 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800903 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
904 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000905 }
henrik.lundin55480f52016-03-08 02:37:57 -0800906 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000907 // Verify that the first block of samples is set to 0.
908 static const int kExpectedOutputLength =
909 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
910 for (int i = 0; i < kExpectedOutputLength; ++i) {
911 std::ostringstream ss;
912 ss << "i = " << i;
913 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800914 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000915 }
henrik.lundind89814b2015-11-23 06:49:25 -0800916 // Verify that the sample rate did not change from the initial configuration.
917 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000918}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000919
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000920class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000921 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000922 virtual void TestCondition(double sum_squared_noise,
923 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000924
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000925 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700926 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000927 uint8_t payload_type = 0xFF; // Invalid.
928 if (sampling_rate_hz == 8000) {
929 expected_samples_per_channel = kBlockSize8kHz;
930 payload_type = 93; // PCM 16, 8 kHz.
931 } else if (sampling_rate_hz == 16000) {
932 expected_samples_per_channel = kBlockSize16kHz;
933 payload_type = 94; // PCM 16, 16 kHZ.
934 } else if (sampling_rate_hz == 32000) {
935 expected_samples_per_channel = kBlockSize32kHz;
936 payload_type = 95; // PCM 16, 32 kHz.
937 } else {
938 ASSERT_TRUE(false); // Unsupported test case.
939 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000940
henrik.lundin6d8e0112016-03-04 10:34:21 -0800941 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000942 test::AudioLoop input;
943 // We are using the same 32 kHz input file for all tests, regardless of
944 // |sampling_rate_hz|. The output may sound weird, but the test is still
945 // valid.
946 ASSERT_TRUE(input.Init(
947 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
948 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700949 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000950
951 // Payload of 10 ms of PCM16 32 kHz.
952 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000953 WebRtcRTPHeader rtp_info;
954 PopulateRtpInfo(0, 0, &rtp_info);
955 rtp_info.header.payloadType = payload_type;
956
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000957 uint32_t receive_timestamp = 0;
958 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800959 auto block = input.GetNextBlock();
960 ASSERT_EQ(expected_samples_per_channel, block.size());
961 size_t enc_len_bytes =
962 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000963 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
964
kwibergee2bac22015-11-11 10:34:00 -0800965 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
966 payload, enc_len_bytes),
967 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800968 output.Reset();
henrik.lundin55480f52016-03-08 02:37:57 -0800969 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 ASSERT_EQ(1u, output.num_channels_);
971 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800972 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000973
974 // Next packet.
975 rtp_info.header.timestamp += expected_samples_per_channel;
976 rtp_info.header.sequenceNumber++;
977 receive_timestamp += expected_samples_per_channel;
978 }
979
henrik.lundin6d8e0112016-03-04 10:34:21 -0800980 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000981
982 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
983 // one frame without checking speech-type. This is the first frame pulled
984 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin55480f52016-03-08 02:37:57 -0800985 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800986 ASSERT_EQ(1u, output.num_channels_);
987 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000988
989 // To be able to test the fading of background noise we need at lease to
990 // pull 611 frames.
991 const int kFadingThreshold = 611;
992
993 // Test several CNG-to-PLC packet for the expected behavior. The number 20
994 // is arbitrary, but sufficiently large to test enough number of frames.
995 const int kNumPlcToCngTestFrames = 20;
996 bool plc_to_cng = false;
997 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800998 output.Reset();
999 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin55480f52016-03-08 02:37:57 -08001000 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001001 ASSERT_EQ(1u, output.num_channels_);
1002 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001003 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001004 plc_to_cng = true;
1005 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001006 for (size_t k = 0;
1007 k < output.num_channels_ * output.samples_per_channel_; ++k)
1008 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001009 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001010 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001011 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001012 }
1013 }
1014 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1015 }
1016};
1017
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001018class NetEqBgnTestOn : public NetEqBgnTest {
1019 protected:
1020 NetEqBgnTestOn() : NetEqBgnTest() {
1021 config_.background_noise_mode = NetEq::kBgnOn;
1022 }
1023
1024 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1025 EXPECT_NE(0, sum_squared_noise);
1026 }
1027};
1028
1029class NetEqBgnTestOff : public NetEqBgnTest {
1030 protected:
1031 NetEqBgnTestOff() : NetEqBgnTest() {
1032 config_.background_noise_mode = NetEq::kBgnOff;
1033 }
1034
1035 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1036 EXPECT_EQ(0, sum_squared_noise);
1037 }
1038};
1039
1040class NetEqBgnTestFade : public NetEqBgnTest {
1041 protected:
1042 NetEqBgnTestFade() : NetEqBgnTest() {
1043 config_.background_noise_mode = NetEq::kBgnFade;
1044 }
1045
1046 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1047 if (should_be_faded)
1048 EXPECT_EQ(0, sum_squared_noise);
1049 }
1050};
1051
henrika1d34fe92015-06-16 10:04:20 +02001052TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001053 CheckBgn(8000);
1054 CheckBgn(16000);
1055 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001056}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001057
henrika1d34fe92015-06-16 10:04:20 +02001058TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001059 CheckBgn(8000);
1060 CheckBgn(16000);
1061 CheckBgn(32000);
1062}
1063
henrika1d34fe92015-06-16 10:04:20 +02001064TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001065 CheckBgn(8000);
1066 CheckBgn(16000);
1067 CheckBgn(32000);
1068}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001069
Peter Boströme2976c82016-01-04 22:44:05 +01001070#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001071#define MAYBE_SyncPacketInsert SyncPacketInsert
1072#else
1073#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1074#endif
1075TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001076 WebRtcRTPHeader rtp_info;
1077 uint32_t receive_timestamp = 0;
1078 // For the readability use the following payloads instead of the defaults of
1079 // this test.
1080 uint8_t kPcm16WbPayloadType = 1;
1081 uint8_t kCngNbPayloadType = 2;
1082 uint8_t kCngWbPayloadType = 3;
1083 uint8_t kCngSwb32PayloadType = 4;
1084 uint8_t kCngSwb48PayloadType = 5;
1085 uint8_t kAvtPayloadType = 6;
1086 uint8_t kRedPayloadType = 7;
1087 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1088
1089 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001090 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001091 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001092 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001093 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001094 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001095 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001096 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001097 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001098 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001099 "cng-swb48", kCngSwb48PayloadType));
1100 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001101 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001102 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001103 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001104 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001105 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001106
1107 PopulateRtpInfo(0, 0, &rtp_info);
1108 rtp_info.header.payloadType = kPcm16WbPayloadType;
1109
1110 // The first packet injected cannot be sync-packet.
1111 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1112
1113 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001114 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001115 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001116 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001117
1118 // Next packet. Last packet contained 10 ms audio.
1119 rtp_info.header.sequenceNumber++;
1120 rtp_info.header.timestamp += kBlockSize16kHz;
1121 receive_timestamp += kBlockSize16kHz;
1122
1123 // Unacceptable payload types CNG, AVT (DTMF), RED.
1124 rtp_info.header.payloadType = kCngNbPayloadType;
1125 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1126
1127 rtp_info.header.payloadType = kCngWbPayloadType;
1128 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1129
1130 rtp_info.header.payloadType = kCngSwb32PayloadType;
1131 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1132
1133 rtp_info.header.payloadType = kCngSwb48PayloadType;
1134 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1135
1136 rtp_info.header.payloadType = kAvtPayloadType;
1137 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1138
1139 rtp_info.header.payloadType = kRedPayloadType;
1140 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1141
1142 // Change of codec cannot be initiated with a sync packet.
1143 rtp_info.header.payloadType = kIsacPayloadType;
1144 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1145
1146 // Change of SSRC is not allowed with a sync packet.
1147 rtp_info.header.payloadType = kPcm16WbPayloadType;
1148 ++rtp_info.header.ssrc;
1149 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1150
1151 --rtp_info.header.ssrc;
1152 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1153}
1154
1155// First insert several noise like packets, then sync-packets. Decoding all
1156// packets should not produce error, statistics should not show any packet loss
1157// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001158// TODO(turajs) we will have a better test if we have a referece NetEq, and
1159// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1160// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001161TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001162 WebRtcRTPHeader rtp_info;
1163 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001164 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001165 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001166 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001167 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001168 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001169 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1170 }
1171 // Insert some packets which decode to noise. We are not interested in
1172 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001173 uint32_t receive_timestamp = 0;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001174 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001175 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001176 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001177 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1178 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001179
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001180 rtp_info.header.sequenceNumber++;
1181 rtp_info.header.timestamp += kBlockSize16kHz;
1182 receive_timestamp += kBlockSize16kHz;
1183 }
1184 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001185
1186 // Make sure sufficient number of sync packets are inserted that we can
1187 // conduct a test.
1188 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001189 // Insert sync-packets, the decoded sequence should be all-zero.
1190 for (int n = 0; n < kNumSyncPackets; ++n) {
1191 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001192 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001193 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1194 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001195 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001196 EXPECT_TRUE(IsAllZero(
1197 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001198 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001199 rtp_info.header.sequenceNumber++;
1200 rtp_info.header.timestamp += kBlockSize16kHz;
1201 receive_timestamp += kBlockSize16kHz;
1202 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001203
1204 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001205 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001206 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001207 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001208 ASSERT_EQ(0, neteq_->GetAudio(&output));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001209 if (n >= algorithmic_frame_delay + 1) {
1210 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001211 EXPECT_TRUE(IsAllNonZero(
1212 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001213 }
1214 rtp_info.header.sequenceNumber++;
1215 rtp_info.header.timestamp += kBlockSize16kHz;
1216 receive_timestamp += kBlockSize16kHz;
1217 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001218 NetEqNetworkStatistics network_stats;
1219 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1220 // Expecting a "clean" network.
1221 EXPECT_EQ(0, network_stats.packet_loss_rate);
1222 EXPECT_EQ(0, network_stats.expand_rate);
1223 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001224 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001225}
1226
1227// Test if the size of the packet buffer reported correctly when containing
1228// sync packets. Also, test if network packets override sync packets. That is to
1229// prefer decoding a network packet to a sync packet, if both have same sequence
1230// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001231TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232 WebRtcRTPHeader rtp_info;
1233 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001234 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001235 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001237 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001238 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1239 }
1240 // Insert some packets which decode to noise. We are not interested in
1241 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001242 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001243 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
1244 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001245 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin55480f52016-03-08 02:37:57 -08001246 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001247 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1248 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001249 rtp_info.header.sequenceNumber++;
1250 rtp_info.header.timestamp += kBlockSize16kHz;
1251 receive_timestamp += kBlockSize16kHz;
1252 }
1253 const int kNumSyncPackets = 10;
1254
1255 WebRtcRTPHeader first_sync_packet_rtp_info;
1256 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1257
1258 // Insert sync-packets, but no decoding.
1259 for (int n = 0; n < kNumSyncPackets; ++n) {
1260 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1261 rtp_info.header.sequenceNumber++;
1262 rtp_info.header.timestamp += kBlockSize16kHz;
1263 receive_timestamp += kBlockSize16kHz;
1264 }
1265 NetEqNetworkStatistics network_stats;
1266 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001267 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1268 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001269
1270 // Rewind |rtp_info| to that of the first sync packet.
1271 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1272
1273 // Insert.
1274 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001275 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001276 rtp_info.header.sequenceNumber++;
1277 rtp_info.header.timestamp += kBlockSize16kHz;
1278 receive_timestamp += kBlockSize16kHz;
1279 }
1280
1281 // Decode.
1282 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin55480f52016-03-08 02:37:57 -08001283 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001284 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1285 ASSERT_EQ(1u, output.num_channels_);
1286 EXPECT_TRUE(IsAllNonZero(
1287 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001288 }
1289}
1290
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001291void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1292 uint32_t start_timestamp,
1293 const std::set<uint16_t>& drop_seq_numbers,
1294 bool expect_seq_no_wrap,
1295 bool expect_timestamp_wrap) {
1296 uint16_t seq_no = start_seq_no;
1297 uint32_t timestamp = start_timestamp;
1298 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1299 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1300 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001301 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001302 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001303 uint32_t receive_timestamp = 0;
1304
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001305 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001306 const int kSpeechDurationMs = 2000;
1307 int packets_inserted = 0;
1308 uint16_t last_seq_no;
1309 uint32_t last_timestamp;
1310 bool timestamp_wrapped = false;
1311 bool seq_no_wrapped = false;
1312 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1313 // Each turn in this for loop is 10 ms.
1314 while (next_input_time_ms <= t_ms) {
1315 // Insert one 30 ms speech frame.
1316 uint8_t payload[kPayloadBytes] = {0};
1317 WebRtcRTPHeader rtp_info;
1318 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1319 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1320 // This sequence number was not in the set to drop. Insert it.
1321 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001322 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001323 ++packets_inserted;
1324 }
1325 NetEqNetworkStatistics network_stats;
1326 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1327
1328 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1329 // packet size for first few packets. Therefore we refrain from checking
1330 // the criteria.
1331 if (packets_inserted > 4) {
1332 // Expect preferred and actual buffer size to be no more than 2 frames.
1333 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001334 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1335 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001336 }
1337 last_seq_no = seq_no;
1338 last_timestamp = timestamp;
1339
1340 ++seq_no;
1341 timestamp += kSamples;
1342 receive_timestamp += kSamples;
1343 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1344
1345 seq_no_wrapped |= seq_no < last_seq_no;
1346 timestamp_wrapped |= timestamp < last_timestamp;
1347 }
1348 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001349 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -08001350 ASSERT_EQ(0, neteq_->GetAudio(&output));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001351 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1352 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001353
1354 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001355 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1356 ASSERT_TRUE(playout_timestamp);
1357 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001358 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001359 }
1360 // Make sure we have actually tested wrap-around.
1361 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1362 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1363}
1364
1365TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1366 // Start with a sequence number that will soon wrap.
1367 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1368 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1369}
1370
1371TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1372 // Start with a sequence number that will soon wrap.
1373 std::set<uint16_t> drop_seq_numbers;
1374 drop_seq_numbers.insert(0xFFFF);
1375 drop_seq_numbers.insert(0x0);
1376 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1377}
1378
1379TEST_F(NetEqDecodingTest, TimestampWrap) {
1380 // Start with a timestamp that will soon wrap.
1381 std::set<uint16_t> drop_seq_numbers;
1382 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1383}
1384
1385TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1386 // Start with a timestamp and a sequence number that will wrap at the same
1387 // time.
1388 std::set<uint16_t> drop_seq_numbers;
1389 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1390}
1391
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001392void NetEqDecodingTest::DuplicateCng() {
1393 uint16_t seq_no = 0;
1394 uint32_t timestamp = 0;
1395 const int kFrameSizeMs = 10;
1396 const int kSampleRateKhz = 16;
1397 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001398 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001399
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001400 const int algorithmic_delay_samples = std::max(
1401 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001402 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001403 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001404 uint8_t payload[kPayloadBytes] = {0};
1405 WebRtcRTPHeader rtp_info;
1406 for (int i = 0; i < 3; ++i) {
1407 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001408 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001409 ++seq_no;
1410 timestamp += kSamples;
1411
1412 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001413 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001414 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001415 }
1416 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001417 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001418
1419 // Insert same CNG packet twice.
1420 const int kCngPeriodMs = 100;
1421 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001422 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001423 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1424 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001425 ASSERT_EQ(
1426 0, neteq_->InsertPacket(
1427 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001428
1429 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001430 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001431 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001432 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001433 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1434 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1435 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001436
1437 // Insert the same CNG packet again. Note that at this point it is old, since
1438 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001439 ASSERT_EQ(
1440 0, neteq_->InsertPacket(
1441 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001442
1443 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1444 // we have already pulled out CNG once.
1445 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin55480f52016-03-08 02:37:57 -08001446 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001447 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001448 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001449 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001450 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001451 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001452 }
1453
1454 // Insert speech again.
1455 ++seq_no;
1456 timestamp += kCngPeriodSamples;
1457 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001458 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001459
1460 // Pull audio once and verify that the output is speech again.
henrik.lundin55480f52016-03-08 02:37:57 -08001461 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001462 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001463 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001464 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1465 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001466 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001467 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001468}
1469
henrik.lundin0d96ab72016-04-06 12:28:26 -07001470rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1471 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001472}
1473
1474TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001475
1476TEST_F(NetEqDecodingTest, CngFirst) {
1477 uint16_t seq_no = 0;
1478 uint32_t timestamp = 0;
1479 const int kFrameSizeMs = 10;
1480 const int kSampleRateKhz = 16;
1481 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1482 const int kPayloadBytes = kSamples * 2;
1483 const int kCngPeriodMs = 100;
1484 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1485 size_t payload_len;
1486
1487 uint8_t payload[kPayloadBytes] = {0};
1488 WebRtcRTPHeader rtp_info;
1489
1490 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001491 ASSERT_EQ(
1492 NetEq::kOK,
1493 neteq_->InsertPacket(
1494 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001495 ++seq_no;
1496 timestamp += kCngPeriodSamples;
1497
1498 // Pull audio once and make sure CNG is played.
henrik.lundin55480f52016-03-08 02:37:57 -08001499 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001500 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001501 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001502
1503 // Insert some speech packets.
1504 for (int i = 0; i < 3; ++i) {
1505 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001506 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001507 ++seq_no;
1508 timestamp += kSamples;
1509
1510 // Pull audio once.
henrik.lundin55480f52016-03-08 02:37:57 -08001511 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001512 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001513 }
1514 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001515 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001516}
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001517} // namespace webrtc