blob: 592323e9a5bee8fbc6e858f65ec881b6f66bfde2 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020024#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
26#include "modules/audio_coding/neteq/tools/audio_loop.h"
27#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
28#include "modules/include/module_common_types.h"
29#include "rtc_base/flags.h"
30#include "rtc_base/ignore_wundef.h"
31#include "rtc_base/protobuf_utils.h"
Mirko Bonadeia8110272017-10-18 14:22:50 +020032#include "rtc_base/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/sha1digest.h"
34#include "rtc_base/stringencode.h"
35#include "test/gtest.h"
36#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020037#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
minyue5f026d02015-12-16 07:36:04 -080039#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070040RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080041#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
42#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
43#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080045#endif
kwiberg77eab702016-09-28 17:42:01 -070046RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080047#endif
48
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000049DEFINE_bool(gen_ref, false, "Generate reference files.");
50
kwiberg5adaf732016-10-04 09:33:27 -070051namespace webrtc {
52
minyue5f026d02015-12-16 07:36:04 -080053namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
minyue4f906772016-04-29 11:05:14 -070055const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020056 const std::string& checksum_android_32,
57 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070058 const std::string& checksum_win_32,
59 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070060#if defined(WEBRTC_ANDROID)
Henrik Lundin8cd750d2017-10-12 13:07:11 +020061 #ifdef WEBRTC_ARCH_64_BITS
62 return checksum_android_64;
63 #else
64 return checksum_android_32;
65 #endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070066#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070067 #ifdef WEBRTC_ARCH_64_BITS
68 return checksum_win_64;
69 #else
70 return checksum_win_32;
71 #endif // WEBRTC_ARCH_64_BITS
72#else
73 return checksum_general;
74#endif // WEBRTC_WIN
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);
minyue5f026d02015-12-16 07:36:04 -080084 stats->set_expand_rate(stats_raw.expand_rate);
85 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
86 stats->set_preemptive_rate(stats_raw.preemptive_rate);
87 stats->set_accelerate_rate(stats_raw.accelerate_rate);
88 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020089 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
minyue5f026d02015-12-16 07:36:04 -080090 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);
srte186d9c32017-08-04 05:03:53 -0700101 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800102 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700103 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800104 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
henrik.lundin7a926812016-05-12 13:51:28 -0700122void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700123 ASSERT_EQ(true,
124 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
125 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
126 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700127 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
128 "pcma", 8));
129#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700130 ASSERT_EQ(true,
131 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700132#endif
133#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700134 ASSERT_EQ(true,
135 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700136#endif
137#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700138 ASSERT_EQ(true,
139 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700140#endif
141#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700142 ASSERT_EQ(true,
143 neteq->RegisterPayloadType(
144 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700145#endif
kwiberg5adaf732016-10-04 09:33:27 -0700146 ASSERT_EQ(true,
147 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
148 ASSERT_EQ(true,
149 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
150 ASSERT_EQ(true,
151 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
152 ASSERT_EQ(true,
153 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
154 ASSERT_EQ(true,
155 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700156}
minyue5f026d02015-12-16 07:36:04 -0800157} // namespace
158
minyue4f906772016-04-29 11:05:14 -0700159class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 public:
minyue4f906772016-04-29 11:05:14 -0700161 explicit ResultSink(const std::string& output_file);
162 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163
yujo36b1a5f2017-06-12 12:45:32 -0700164 template<typename T> void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700165
166 void AddResult(const NetEqNetworkStatistics& stats);
167 void AddResult(const RtcpStatistics& stats);
168
169 void VerifyChecksum(const std::string& ref_check_sum);
170
171 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000172 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700173 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174};
175
minyue4f906772016-04-29 11:05:14 -0700176ResultSink::ResultSink(const std::string &output_file)
177 : output_fp_(nullptr),
178 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179 if (!output_file.empty()) {
180 output_fp_ = fopen(output_file.c_str(), "wb");
181 EXPECT_TRUE(output_fp_ != NULL);
182 }
183}
184
minyue4f906772016-04-29 11:05:14 -0700185ResultSink::~ResultSink() {
186 if (output_fp_)
187 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188}
189
yujo36b1a5f2017-06-12 12:45:32 -0700190template<typename T>
191void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700193 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194 }
yujo36b1a5f2017-06-12 12:45:32 -0700195 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196}
197
minyue4f906772016-04-29 11:05:14 -0700198void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800199#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800200 neteq_unittest::NetEqNetworkStatistics stats;
201 Convert(stats_raw, &stats);
202
mbonadei7c2c8432017-04-07 00:59:12 -0700203 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800204 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700205 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800206#else
207 FAIL() << "Writing to reference file requires Proto Buffer.";
208#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000209}
210
minyue4f906772016-04-29 11:05:14 -0700211void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800212#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800213 neteq_unittest::RtcpStatistics stats;
214 Convert(stats_raw, &stats);
215
mbonadei7c2c8432017-04-07 00:59:12 -0700216 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800217 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700218 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800219#else
220 FAIL() << "Writing to reference file requires Proto Buffer.";
221#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000222}
223
minyue4f906772016-04-29 11:05:14 -0700224void ResultSink::VerifyChecksum(const std::string& checksum) {
225 std::vector<char> buffer;
226 buffer.resize(digest_->Size());
227 digest_->Finish(&buffer[0], buffer.size());
228 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
229 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000230}
231
232class NetEqDecodingTest : public ::testing::Test {
233 protected:
234 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
235 // constants below can be changed.
236 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700237 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
238 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
239 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800240 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 static const int kInitSampleRateHz = 8000;
242
243 NetEqDecodingTest();
244 virtual void SetUp();
245 virtual void TearDown();
246 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000247 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800248 void Process();
minyue5f026d02015-12-16 07:36:04 -0800249
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000250 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700251 const std::string& output_checksum,
252 const std::string& network_stats_checksum,
253 const std::string& rtcp_stats_checksum,
254 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800255
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 static void PopulateRtpInfo(int frame_index,
257 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700258 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259 static void PopulateCng(int frame_index,
260 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700261 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000262 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000263 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000265 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
266 const std::set<uint16_t>& drop_seq_numbers,
267 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
268
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000269 void LongCngWithClockDrift(double drift_factor,
270 double network_freeze_ms,
271 bool pull_audio_during_freeze,
272 int delay_tolerance_ms,
273 int max_time_to_speech_ms);
274
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000275 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000276
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000278 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800279 std::unique_ptr<test::RtpFileSource> rtp_source_;
280 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800282 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000284 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285};
286
287// Allocating the static const so that it can be passed by reference.
288const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700289const size_t NetEqDecodingTest::kBlockSize8kHz;
290const size_t NetEqDecodingTest::kBlockSize16kHz;
291const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292const int NetEqDecodingTest::kInitSampleRateHz;
293
294NetEqDecodingTest::NetEqDecodingTest()
295 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000296 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000298 output_sample_rate_(kInitSampleRateHz),
299 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000300 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301}
302
303void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700304 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000305 NetEqNetworkStatistics stat;
306 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
307 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700309 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310}
311
312void NetEqDecodingTest::TearDown() {
313 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314}
315
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000316void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000317 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000318}
319
henrik.lundin6d8e0112016-03-04 10:34:21 -0800320void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000322 while (packet_ && sim_clock_ >= packet_->time_ms()) {
323 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800324#ifndef WEBRTC_CODEC_ISAC
325 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700326 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800327#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200328 ASSERT_EQ(0,
329 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700330 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200331 rtc::ArrayView<const uint8_t>(
332 packet_->payload(), packet_->payload_length_bytes()),
333 static_cast<uint32_t>(packet_->time_ms() *
334 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 }
336 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700337 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338 }
339
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000340 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700341 bool muted;
342 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
343 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800344 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
345 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
346 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
347 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
348 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800349 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350
351 // Increase time.
352 sim_clock_ += kTimeStepMs;
353}
354
minyue4f906772016-04-29 11:05:14 -0700355void NetEqDecodingTest::DecodeAndCompare(
356 const std::string& rtp_file,
357 const std::string& output_checksum,
358 const std::string& network_stats_checksum,
359 const std::string& rtcp_stats_checksum,
360 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361 OpenInputFile(rtp_file);
362
minyue4f906772016-04-29 11:05:14 -0700363 std::string ref_out_file =
364 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
365 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366
minyue4f906772016-04-29 11:05:14 -0700367 std::string stat_out_file =
368 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
369 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000370
minyue4f906772016-04-29 11:05:14 -0700371 std::string rtcp_out_file =
372 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
373 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000374
henrik.lundin46ba49c2016-05-24 22:50:47 -0700375 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200377 uint64_t last_concealed_samples = 0;
378 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000379 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 std::ostringstream ss;
381 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
382 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800383 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700384 ASSERT_NO_FATAL_FAILURE(output.AddResult(
yujo36b1a5f2017-06-12 12:45:32 -0700385 out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386
387 // Query the network statistics API once per second
388 if (sim_clock_ % 1000 == 0) {
389 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700390 NetEqNetworkStatistics current_network_stats;
391 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
392 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
393
henrik.lundin9c3efd02015-08-27 13:12:22 -0700394 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700395 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
396 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397
Henrik Lundinac0a5032017-09-25 12:22:46 +0200398 // Verify that liftime stats and network stats report similar loss
399 // concealment rates.
400 auto lifetime_stats = neteq_->GetLifetimeStatistics();
401 const uint64_t delta_concealed_samples =
402 lifetime_stats.concealed_samples - last_concealed_samples;
403 last_concealed_samples = lifetime_stats.concealed_samples;
404 const uint64_t delta_total_samples_received =
405 lifetime_stats.total_samples_received - last_total_samples_received;
406 last_total_samples_received = lifetime_stats.total_samples_received;
407 // The tolerance is 1% but expressed in Q14.
408 EXPECT_NEAR(
409 (delta_concealed_samples << 14) / delta_total_samples_received,
410 current_network_stats.expand_rate, (2 << 14) / 100.0);
411
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000412 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700413 RtcpStatistics current_rtcp_stats;
414 neteq_->GetRtcpStatistics(&current_rtcp_stats);
415 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000416 }
417 }
minyue4f906772016-04-29 11:05:14 -0700418
419 SCOPED_TRACE("Check output audio.");
420 output.VerifyChecksum(output_checksum);
421 SCOPED_TRACE("Check network stats.");
422 network_stats.VerifyChecksum(network_stats_checksum);
423 SCOPED_TRACE("Check rtcp stats.");
424 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425}
426
427void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
428 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700429 RTPHeader* rtp_info) {
430 rtp_info->sequenceNumber = frame_index;
431 rtp_info->timestamp = timestamp;
432 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
433 rtp_info->payloadType = 94; // PCM16b WB codec.
434 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000435}
436
437void NetEqDecodingTest::PopulateCng(int frame_index,
438 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700439 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000440 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000441 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700442 rtp_info->sequenceNumber = frame_index;
443 rtp_info->timestamp = timestamp;
444 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
445 rtp_info->payloadType = 98; // WB CNG.
446 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000447 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
448 *payload_len = 1; // Only noise level, no spectral parameters.
449}
450
ivoc72c08ed2016-01-20 07:26:24 -0800451#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
452 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
453 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700454 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800455#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700456#else
minyue5f026d02015-12-16 07:36:04 -0800457#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700458#endif
minyue5f026d02015-12-16 07:36:04 -0800459TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800460 const std::string input_rtp_file =
461 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000462
minyue4f906772016-04-29 11:05:14 -0700463 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700464 "09fa7646e2ad032a0b156177b95f09012430f81f",
465 "1c64eb8b55ce8878676c6a1e6ddd78f48de0668b",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200466 "not used",
soren9f2c18e2017-04-10 02:22:46 -0700467 "09fa7646e2ad032a0b156177b95f09012430f81f",
468 "759fef89a5de52bd17e733dc255c671ce86be909");
minyue4f906772016-04-29 11:05:14 -0700469
henrik.lundin2979f552017-05-05 05:04:16 -0700470 const std::string network_stats_checksum =
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200471 PlatformChecksum("5b4262ca328e5f066af5d34f3380521583dd20de",
472 "80235b6d727281203acb63b98f9a9e85d95f7ec0",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200473 "not used",
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200474 "5b4262ca328e5f066af5d34f3380521583dd20de",
475 "5b4262ca328e5f066af5d34f3380521583dd20de");
minyue4f906772016-04-29 11:05:14 -0700476
477 const std::string rtcp_stats_checksum = PlatformChecksum(
478 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
479 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200480 "not used",
minyue4f906772016-04-29 11:05:14 -0700481 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
482 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
483
484 DecodeAndCompare(input_rtp_file,
485 output_checksum,
486 network_stats_checksum,
487 rtcp_stats_checksum,
oprypin9b2f20c2017-08-29 05:51:57 -0700488 FLAG_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000489}
490
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200491#if !defined(WEBRTC_IOS) && \
minyue93c08b72015-12-22 09:57:41 -0800492 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200493 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800494#define MAYBE_TestOpusBitExactness TestOpusBitExactness
495#else
496#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
497#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200498TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800499 const std::string input_rtp_file =
500 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800501
minyue4f906772016-04-29 11:05:14 -0700502 const std::string output_checksum = PlatformChecksum(
minyue-webrtcadb58b82017-07-26 17:59:59 +0200503 "721e1e0c6effe4b2401536a4eef11512c9fb709c",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200504 "2e3c3e451532967e981fbc39b8cfb55e1df1ff7f",
505 "f403940a1936bff040d1d158624f69bdccbc3423",
minyue-webrtcadb58b82017-07-26 17:59:59 +0200506 "721e1e0c6effe4b2401536a4eef11512c9fb709c",
507 "721e1e0c6effe4b2401536a4eef11512c9fb709c");
minyue4f906772016-04-29 11:05:14 -0700508
henrik.lundin2979f552017-05-05 05:04:16 -0700509 const std::string network_stats_checksum =
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200510 PlatformChecksum("4e749c46e2611877120ac7a20cbbe555cfbd70ea",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200511 "1edee6d07e0005327c32a77f9b3c0c1f03780e9f",
512 "ff806c574f82a089dec4c37ea1224b1eb0822d23",
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200513 "4e749c46e2611877120ac7a20cbbe555cfbd70ea",
514 "4e749c46e2611877120ac7a20cbbe555cfbd70ea");
minyue4f906772016-04-29 11:05:14 -0700515
516 const std::string rtcp_stats_checksum = PlatformChecksum(
517 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
518 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
519 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200520 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
minyue4f906772016-04-29 11:05:14 -0700521 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
522
523 DecodeAndCompare(input_rtp_file,
524 output_checksum,
525 network_stats_checksum,
526 rtcp_stats_checksum,
oprypin9b2f20c2017-08-29 05:51:57 -0700527 FLAG_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800528}
529
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000530// Use fax mode to avoid time-scaling. This is to simplify the testing of
531// packet waiting times in the packet buffer.
532class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
533 protected:
534 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
535 config_.playout_mode = kPlayoutFax;
536 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200537 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000538};
539
540TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000541 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
542 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000543 const size_t kSamples = 10 * 16;
544 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800546 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700547 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200548 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
549 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700550 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
551 rtp_info.payloadType = 94; // PCM16b WB codec.
552 rtp_info.markerBit = 0;
553 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554 }
555 // Pull out all data.
556 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700557 bool muted;
558 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800559 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000560 }
561
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200562 NetEqNetworkStatistics stats;
563 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
565 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200566 // each packet. Thus, we are calculating the statistics for a series from 10
567 // to 300, in steps of 10 ms.
568 EXPECT_EQ(155, stats.mean_waiting_time_ms);
569 EXPECT_EQ(155, stats.median_waiting_time_ms);
570 EXPECT_EQ(10, stats.min_waiting_time_ms);
571 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572
573 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200574 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
575 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
576 EXPECT_EQ(-1, stats.median_waiting_time_ms);
577 EXPECT_EQ(-1, stats.min_waiting_time_ms);
578 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579}
580
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000581TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000582 const int kNumFrames = 3000; // Needed for convergence.
583 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000584 const size_t kSamples = 10 * 16;
585 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000586 while (frame_index < kNumFrames) {
587 // Insert one packet each time, except every 10th time where we insert two
588 // packets at once. This will create a negative clock-drift of approx. 10%.
589 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
590 for (int n = 0; n < num_packets; ++n) {
591 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700592 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000593 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700594 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 ++frame_index;
596 }
597
598 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700599 bool muted;
600 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800601 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 }
603
604 NetEqNetworkStatistics network_stats;
605 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700606 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607}
608
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000609TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610 const int kNumFrames = 5000; // Needed for convergence.
611 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000612 const size_t kSamples = 10 * 16;
613 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 for (int i = 0; i < kNumFrames; ++i) {
615 // Insert one packet each time, except every 10th time where we don't insert
616 // any packet. This will create a positive clock-drift of approx. 11%.
617 int num_packets = (i % 10 == 9 ? 0 : 1);
618 for (int n = 0; n < num_packets; ++n) {
619 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700620 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000621 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700622 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000623 ++frame_index;
624 }
625
626 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700627 bool muted;
628 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800629 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000630 }
631
632 NetEqNetworkStatistics network_stats;
633 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700634 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635}
636
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000637void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
638 double network_freeze_ms,
639 bool pull_audio_during_freeze,
640 int delay_tolerance_ms,
641 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000642 uint16_t seq_no = 0;
643 uint32_t timestamp = 0;
644 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000645 const size_t kSamples = kFrameSizeMs * 16;
646 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000647 double next_input_time_ms = 0.0;
648 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700649 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650
651 // Insert speech for 5 seconds.
652 const int kSpeechDurationMs = 5000;
653 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
654 // Each turn in this for loop is 10 ms.
655 while (next_input_time_ms <= t_ms) {
656 // Insert one 30 ms speech frame.
657 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700658 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000659 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700660 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661 ++seq_no;
662 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000663 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 }
665 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700666 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800667 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 }
669
henrik.lundin55480f52016-03-08 02:37:57 -0800670 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700671 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700672 ASSERT_TRUE(playout_timestamp);
673 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000674
675 // Insert CNG for 1 minute (= 60000 ms).
676 const int kCngPeriodMs = 100;
677 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
678 const int kCngDurationMs = 60000;
679 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
680 // Each turn in this for loop is 10 ms.
681 while (next_input_time_ms <= t_ms) {
682 // Insert one CNG frame each 100 ms.
683 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000684 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700685 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000686 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800687 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700688 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800689 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000690 ++seq_no;
691 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000692 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 }
694 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700695 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800696 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000697 }
698
henrik.lundin55480f52016-03-08 02:37:57 -0800699 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000701 if (network_freeze_ms > 0) {
702 // First keep pulling audio for |network_freeze_ms| without inserting
703 // any data, then insert CNG data corresponding to |network_freeze_ms|
704 // without pulling any output audio.
705 const double loop_end_time = t_ms + network_freeze_ms;
706 for (; t_ms < loop_end_time; t_ms += 10) {
707 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700708 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800709 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800710 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000711 }
712 bool pull_once = pull_audio_during_freeze;
713 // If |pull_once| is true, GetAudio will be called once half-way through
714 // the network recovery period.
715 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
716 while (next_input_time_ms <= t_ms) {
717 if (pull_once && next_input_time_ms >= pull_time_ms) {
718 pull_once = false;
719 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700720 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800721 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800722 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000723 t_ms += 10;
724 }
725 // Insert one CNG frame each 100 ms.
726 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000727 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700728 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000729 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800730 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700731 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800732 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000733 ++seq_no;
734 timestamp += kCngPeriodSamples;
735 next_input_time_ms += kCngPeriodMs * drift_factor;
736 }
737 }
738
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000740 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800741 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000742 // Each turn in this for loop is 10 ms.
743 while (next_input_time_ms <= t_ms) {
744 // Insert one 30 ms speech frame.
745 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700746 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000747 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700748 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749 ++seq_no;
750 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000751 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000752 }
753 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700754 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800755 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000756 // Increase clock.
757 t_ms += 10;
758 }
759
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000760 // Check that the speech starts again within reasonable time.
761 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
762 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700763 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700764 ASSERT_TRUE(playout_timestamp);
765 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000766 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
768 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000769}
770
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000771TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000772 // Apply a clock drift of -25 ms / s (sender faster than receiver).
773 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000774 const double kNetworkFreezeTimeMs = 0.0;
775 const bool kGetAudioDuringFreezeRecovery = false;
776 const int kDelayToleranceMs = 20;
777 const int kMaxTimeToSpeechMs = 100;
778 LongCngWithClockDrift(kDriftFactor,
779 kNetworkFreezeTimeMs,
780 kGetAudioDuringFreezeRecovery,
781 kDelayToleranceMs,
782 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000783}
784
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000785TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000786 // Apply a clock drift of +25 ms / s (sender slower than receiver).
787 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000788 const double kNetworkFreezeTimeMs = 0.0;
789 const bool kGetAudioDuringFreezeRecovery = false;
790 const int kDelayToleranceMs = 20;
791 const int kMaxTimeToSpeechMs = 100;
792 LongCngWithClockDrift(kDriftFactor,
793 kNetworkFreezeTimeMs,
794 kGetAudioDuringFreezeRecovery,
795 kDelayToleranceMs,
796 kMaxTimeToSpeechMs);
797}
798
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000799TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000800 // Apply a clock drift of -25 ms / s (sender faster than receiver).
801 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
802 const double kNetworkFreezeTimeMs = 5000.0;
803 const bool kGetAudioDuringFreezeRecovery = false;
804 const int kDelayToleranceMs = 50;
805 const int kMaxTimeToSpeechMs = 200;
806 LongCngWithClockDrift(kDriftFactor,
807 kNetworkFreezeTimeMs,
808 kGetAudioDuringFreezeRecovery,
809 kDelayToleranceMs,
810 kMaxTimeToSpeechMs);
811}
812
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000813TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000814 // Apply a clock drift of +25 ms / s (sender slower than receiver).
815 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
816 const double kNetworkFreezeTimeMs = 5000.0;
817 const bool kGetAudioDuringFreezeRecovery = false;
818 const int kDelayToleranceMs = 20;
819 const int kMaxTimeToSpeechMs = 100;
820 LongCngWithClockDrift(kDriftFactor,
821 kNetworkFreezeTimeMs,
822 kGetAudioDuringFreezeRecovery,
823 kDelayToleranceMs,
824 kMaxTimeToSpeechMs);
825}
826
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000827TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000828 // Apply a clock drift of +25 ms / s (sender slower than receiver).
829 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
830 const double kNetworkFreezeTimeMs = 5000.0;
831 const bool kGetAudioDuringFreezeRecovery = true;
832 const int kDelayToleranceMs = 20;
833 const int kMaxTimeToSpeechMs = 100;
834 LongCngWithClockDrift(kDriftFactor,
835 kNetworkFreezeTimeMs,
836 kGetAudioDuringFreezeRecovery,
837 kDelayToleranceMs,
838 kMaxTimeToSpeechMs);
839}
840
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000841TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000842 const double kDriftFactor = 1.0; // No drift.
843 const double kNetworkFreezeTimeMs = 0.0;
844 const bool kGetAudioDuringFreezeRecovery = false;
845 const int kDelayToleranceMs = 10;
846 const int kMaxTimeToSpeechMs = 50;
847 LongCngWithClockDrift(kDriftFactor,
848 kNetworkFreezeTimeMs,
849 kGetAudioDuringFreezeRecovery,
850 kDelayToleranceMs,
851 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000852}
853
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000854TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000855 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000856 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700857 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000858 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700859 rtp_info.payloadType = 1; // Not registered as a decoder.
860 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000861}
862
Peter Boströme2976c82016-01-04 22:44:05 +0100863#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800864#define MAYBE_DecoderError DecoderError
865#else
866#define MAYBE_DecoderError DISABLED_DecoderError
867#endif
868
Peter Boströme2976c82016-01-04 22:44:05 +0100869TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000870 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700872 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000873 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700874 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
875 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000876 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
877 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700878 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800879 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700880 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000881 }
henrik.lundin7a926812016-05-12 13:51:28 -0700882 bool muted;
883 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
884 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800885
yujo36b1a5f2017-06-12 12:45:32 -0700886 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000887 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700888 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 for (int i = 0; i < kExpectedOutputLength; ++i) {
890 std::ostringstream ss;
891 ss << "i = " << i;
892 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700893 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000894 }
895}
896
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000897TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
899 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700900 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800901 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700902 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000903 }
henrik.lundin7a926812016-05-12 13:51:28 -0700904 bool muted;
905 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
906 ASSERT_FALSE(muted);
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.
yujo36b1a5f2017-06-12 12:45:32 -0700910 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000911 for (int i = 0; i < kExpectedOutputLength; ++i) {
912 std::ostringstream ss;
913 ss << "i = " << i;
914 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700915 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 }
henrik.lundind89814b2015-11-23 06:49:25 -0800917 // Verify that the sample rate did not change from the initial configuration.
918 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000919}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000920
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000921class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000922 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000923 virtual void TestCondition(double sum_squared_noise,
924 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000925
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000926 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700927 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000928 uint8_t payload_type = 0xFF; // Invalid.
929 if (sampling_rate_hz == 8000) {
930 expected_samples_per_channel = kBlockSize8kHz;
931 payload_type = 93; // PCM 16, 8 kHz.
932 } else if (sampling_rate_hz == 16000) {
933 expected_samples_per_channel = kBlockSize16kHz;
934 payload_type = 94; // PCM 16, 16 kHZ.
935 } else if (sampling_rate_hz == 32000) {
936 expected_samples_per_channel = kBlockSize32kHz;
937 payload_type = 95; // PCM 16, 32 kHz.
938 } else {
939 ASSERT_TRUE(false); // Unsupported test case.
940 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000941
henrik.lundin6d8e0112016-03-04 10:34:21 -0800942 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000943 test::AudioLoop input;
944 // We are using the same 32 kHz input file for all tests, regardless of
945 // |sampling_rate_hz|. The output may sound weird, but the test is still
946 // valid.
947 ASSERT_TRUE(input.Init(
948 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
949 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700950 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000951
952 // Payload of 10 ms of PCM16 32 kHz.
953 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700954 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000955 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700956 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000957
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000958 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700959 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000960 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
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200967 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700968 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200969 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
970 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800971 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700972 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800973 ASSERT_EQ(1u, output.num_channels_);
974 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800975 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000976
977 // Next packet.
Mirko Bonadeia8110272017-10-18 14:22:50 +0200978 rtp_info.timestamp += rtc::checked_cast<uint32_t>(
979 expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700980 rtp_info.sequenceNumber++;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200981 receive_timestamp += rtc::checked_cast<uint32_t>(
982 expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000983 }
984
henrik.lundin6d8e0112016-03-04 10:34:21 -0800985 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000986
987 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
988 // one frame without checking speech-type. This is the first frame pulled
989 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700990 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800991 ASSERT_EQ(1u, output.num_channels_);
992 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000993
994 // To be able to test the fading of background noise we need at lease to
995 // pull 611 frames.
996 const int kFadingThreshold = 611;
997
998 // Test several CNG-to-PLC packet for the expected behavior. The number 20
999 // is arbitrary, but sufficiently large to test enough number of frames.
1000 const int kNumPlcToCngTestFrames = 20;
1001 bool plc_to_cng = false;
1002 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001003 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -07001004 // Set to non-zero.
1005 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -07001006 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1007 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001008 ASSERT_EQ(1u, output.num_channels_);
1009 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001010 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001011 plc_to_cng = true;
1012 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001013 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001014 for (size_t k = 0;
1015 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001016 sum_squared += output_data[k] * output_data[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001017 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001018 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001019 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001020 }
1021 }
1022 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1023 }
1024};
1025
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001026class NetEqBgnTestOn : public NetEqBgnTest {
1027 protected:
1028 NetEqBgnTestOn() : NetEqBgnTest() {
1029 config_.background_noise_mode = NetEq::kBgnOn;
1030 }
1031
1032 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1033 EXPECT_NE(0, sum_squared_noise);
1034 }
1035};
1036
1037class NetEqBgnTestOff : public NetEqBgnTest {
1038 protected:
1039 NetEqBgnTestOff() : NetEqBgnTest() {
1040 config_.background_noise_mode = NetEq::kBgnOff;
1041 }
1042
1043 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1044 EXPECT_EQ(0, sum_squared_noise);
1045 }
1046};
1047
1048class NetEqBgnTestFade : public NetEqBgnTest {
1049 protected:
1050 NetEqBgnTestFade() : NetEqBgnTest() {
1051 config_.background_noise_mode = NetEq::kBgnFade;
1052 }
1053
1054 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1055 if (should_be_faded)
1056 EXPECT_EQ(0, sum_squared_noise);
1057 }
1058};
1059
henrika1d34fe92015-06-16 10:04:20 +02001060TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001061 CheckBgn(8000);
1062 CheckBgn(16000);
1063 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001064}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001065
henrika1d34fe92015-06-16 10:04:20 +02001066TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001067 CheckBgn(8000);
1068 CheckBgn(16000);
1069 CheckBgn(32000);
1070}
1071
henrika1d34fe92015-06-16 10:04:20 +02001072TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001073 CheckBgn(8000);
1074 CheckBgn(16000);
1075 CheckBgn(32000);
1076}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001077
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001078void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1079 uint32_t start_timestamp,
1080 const std::set<uint16_t>& drop_seq_numbers,
1081 bool expect_seq_no_wrap,
1082 bool expect_timestamp_wrap) {
1083 uint16_t seq_no = start_seq_no;
1084 uint32_t timestamp = start_timestamp;
1085 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1086 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1087 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001088 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001089 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001090 uint32_t receive_timestamp = 0;
1091
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001092 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001093 const int kSpeechDurationMs = 2000;
1094 int packets_inserted = 0;
1095 uint16_t last_seq_no;
1096 uint32_t last_timestamp;
1097 bool timestamp_wrapped = false;
1098 bool seq_no_wrapped = false;
1099 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1100 // Each turn in this for loop is 10 ms.
1101 while (next_input_time_ms <= t_ms) {
1102 // Insert one 30 ms speech frame.
1103 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001104 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001105 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1106 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1107 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001108 ASSERT_EQ(0,
1109 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001110 ++packets_inserted;
1111 }
1112 NetEqNetworkStatistics network_stats;
1113 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1114
1115 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1116 // packet size for first few packets. Therefore we refrain from checking
1117 // the criteria.
1118 if (packets_inserted > 4) {
1119 // Expect preferred and actual buffer size to be no more than 2 frames.
1120 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001121 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1122 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001123 }
1124 last_seq_no = seq_no;
1125 last_timestamp = timestamp;
1126
1127 ++seq_no;
1128 timestamp += kSamples;
1129 receive_timestamp += kSamples;
1130 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1131
1132 seq_no_wrapped |= seq_no < last_seq_no;
1133 timestamp_wrapped |= timestamp < last_timestamp;
1134 }
1135 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001136 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001137 bool muted;
1138 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001139 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1140 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001141
1142 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin114c1b32017-04-26 07:47:32 -07001143 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001144 ASSERT_TRUE(playout_timestamp);
1145 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001146 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001147 }
1148 // Make sure we have actually tested wrap-around.
1149 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1150 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1151}
1152
1153TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1154 // Start with a sequence number that will soon wrap.
1155 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1156 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1157}
1158
1159TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1160 // Start with a sequence number that will soon wrap.
1161 std::set<uint16_t> drop_seq_numbers;
1162 drop_seq_numbers.insert(0xFFFF);
1163 drop_seq_numbers.insert(0x0);
1164 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1165}
1166
1167TEST_F(NetEqDecodingTest, TimestampWrap) {
1168 // Start with a timestamp that will soon wrap.
1169 std::set<uint16_t> drop_seq_numbers;
1170 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1171}
1172
1173TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1174 // Start with a timestamp and a sequence number that will wrap at the same
1175 // time.
1176 std::set<uint16_t> drop_seq_numbers;
1177 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1178}
1179
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001180void NetEqDecodingTest::DuplicateCng() {
1181 uint16_t seq_no = 0;
1182 uint32_t timestamp = 0;
1183 const int kFrameSizeMs = 10;
1184 const int kSampleRateKhz = 16;
1185 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001186 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001187
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001188 const int algorithmic_delay_samples = std::max(
1189 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001190 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001191 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001192 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001193 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001194 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001195 for (int i = 0; i < 3; ++i) {
1196 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001197 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001198 ++seq_no;
1199 timestamp += kSamples;
1200
1201 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001202 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001203 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001204 }
1205 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001206 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001207
1208 // Insert same CNG packet twice.
1209 const int kCngPeriodMs = 100;
1210 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001211 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001212 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1213 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001214 ASSERT_EQ(
1215 0, neteq_->InsertPacket(
1216 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001217
1218 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001219 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001220 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001221 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001222 EXPECT_FALSE(
1223 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001224 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1225 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001226
1227 // Insert the same CNG packet again. Note that at this point it is old, since
1228 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001229 ASSERT_EQ(
1230 0, neteq_->InsertPacket(
1231 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001232
1233 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1234 // we have already pulled out CNG once.
1235 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001236 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001237 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001239 EXPECT_FALSE(
1240 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001241 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001242 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001243 }
1244
1245 // Insert speech again.
1246 ++seq_no;
1247 timestamp += kCngPeriodSamples;
1248 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001249 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001250
1251 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001252 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001253 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001254 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001255 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001256 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001257 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001258 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001259}
1260
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001261TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001262
1263TEST_F(NetEqDecodingTest, CngFirst) {
1264 uint16_t seq_no = 0;
1265 uint32_t timestamp = 0;
1266 const int kFrameSizeMs = 10;
1267 const int kSampleRateKhz = 16;
1268 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1269 const int kPayloadBytes = kSamples * 2;
1270 const int kCngPeriodMs = 100;
1271 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1272 size_t payload_len;
1273
1274 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001275 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001276
1277 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001278 ASSERT_EQ(
1279 NetEq::kOK,
1280 neteq_->InsertPacket(
1281 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001282 ++seq_no;
1283 timestamp += kCngPeriodSamples;
1284
1285 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001286 bool muted;
1287 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001288 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001289 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001290
1291 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001292 const uint32_t first_speech_timestamp = timestamp;
1293 int timeout_counter = 0;
1294 do {
1295 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001296 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001297 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001298 ++seq_no;
1299 timestamp += kSamples;
1300
1301 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001302 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001303 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001304 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001305 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001306 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001307}
henrik.lundin7a926812016-05-12 13:51:28 -07001308
1309class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1310 public:
1311 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1312 config_.enable_muted_state = true;
1313 }
1314
1315 protected:
1316 static constexpr size_t kSamples = 10 * 16;
1317 static constexpr size_t kPayloadBytes = kSamples * 2;
1318
1319 void InsertPacket(uint32_t rtp_timestamp) {
1320 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001321 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001322 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001323 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001324 }
1325
henrik.lundin42feb512016-09-20 06:51:40 -07001326 void InsertCngPacket(uint32_t rtp_timestamp) {
1327 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001328 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001329 size_t payload_len;
1330 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001331 EXPECT_EQ(
1332 NetEq::kOK,
1333 neteq_->InsertPacket(
1334 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001335 }
1336
henrik.lundin7a926812016-05-12 13:51:28 -07001337 bool GetAudioReturnMuted() {
1338 bool muted;
1339 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1340 return muted;
1341 }
1342
1343 void GetAudioUntilMuted() {
1344 while (!GetAudioReturnMuted()) {
1345 ASSERT_LT(counter_++, 1000) << "Test timed out";
1346 }
1347 }
1348
1349 void GetAudioUntilNormal() {
1350 bool muted = false;
1351 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1352 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1353 ASSERT_LT(counter_++, 1000) << "Test timed out";
1354 }
1355 EXPECT_FALSE(muted);
1356 }
1357
1358 int counter_ = 0;
1359};
1360
1361// Verifies that NetEq goes in and out of muted state as expected.
1362TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1363 // Insert one speech packet.
1364 InsertPacket(0);
1365 // Pull out audio once and expect it not to be muted.
1366 EXPECT_FALSE(GetAudioReturnMuted());
1367 // Pull data until faded out.
1368 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001369 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001370
1371 // Verify that output audio is not written during muted mode. Other parameters
1372 // should be correct, though.
1373 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001374 int16_t* frame_data = new_frame.mutable_data();
1375 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1376 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001377 }
1378 bool muted;
1379 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1380 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001381 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001382 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1383 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001384 }
1385 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1386 new_frame.timestamp_);
1387 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1388 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1389 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1390 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1391 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1392
1393 // Insert new data. Timestamp is corrected for the time elapsed since the last
1394 // packet. Verify that normal operation resumes.
1395 InsertPacket(kSamples * counter_);
1396 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001397 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001398
1399 NetEqNetworkStatistics stats;
1400 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1401 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1402 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1403 // concealment samples in this test.
1404 EXPECT_GT(stats.expand_rate, 14000);
1405 // And, it should be greater than the speech_expand_rate.
1406 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001407}
1408
1409// Verifies that NetEq goes out of muted state when given a delayed packet.
1410TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1411 // Insert one speech packet.
1412 InsertPacket(0);
1413 // Pull out audio once and expect it not to be muted.
1414 EXPECT_FALSE(GetAudioReturnMuted());
1415 // Pull data until faded out.
1416 GetAudioUntilMuted();
1417 // Insert new data. Timestamp is only corrected for the half of the time
1418 // elapsed since the last packet. That is, the new packet is delayed. Verify
1419 // that normal operation resumes.
1420 InsertPacket(kSamples * counter_ / 2);
1421 GetAudioUntilNormal();
1422}
1423
1424// Verifies that NetEq goes out of muted state when given a future packet.
1425TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1426 // Insert one speech packet.
1427 InsertPacket(0);
1428 // Pull out audio once and expect it not to be muted.
1429 EXPECT_FALSE(GetAudioReturnMuted());
1430 // Pull data until faded out.
1431 GetAudioUntilMuted();
1432 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1433 // last packet. That is, the new packet is too early. Verify that normal
1434 // operation resumes.
1435 InsertPacket(kSamples * counter_ * 2);
1436 GetAudioUntilNormal();
1437}
1438
1439// Verifies that NetEq goes out of muted state when given an old packet.
1440TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1441 // Insert one speech packet.
1442 InsertPacket(0);
1443 // Pull out audio once and expect it not to be muted.
1444 EXPECT_FALSE(GetAudioReturnMuted());
1445 // Pull data until faded out.
1446 GetAudioUntilMuted();
1447
1448 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1449 // Insert packet which is older than the first packet.
1450 InsertPacket(kSamples * (counter_ - 1000));
1451 EXPECT_FALSE(GetAudioReturnMuted());
1452 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1453}
1454
henrik.lundin42feb512016-09-20 06:51:40 -07001455// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1456// packet stream is suspended for a long time.
1457TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1458 // Insert one CNG packet.
1459 InsertCngPacket(0);
1460
1461 // Pull 10 seconds of audio (10 ms audio generated per lap).
1462 for (int i = 0; i < 1000; ++i) {
1463 bool muted;
1464 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1465 ASSERT_FALSE(muted);
1466 }
1467 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1468}
1469
1470// Verifies that NetEq goes back to normal after a long CNG period with the
1471// packet stream suspended.
1472TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1473 // Insert one CNG packet.
1474 InsertCngPacket(0);
1475
1476 // Pull 10 seconds of audio (10 ms audio generated per lap).
1477 for (int i = 0; i < 1000; ++i) {
1478 bool muted;
1479 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1480 }
1481
1482 // Insert new data. Timestamp is corrected for the time elapsed since the last
1483 // packet. Verify that normal operation resumes.
1484 InsertPacket(kSamples * counter_);
1485 GetAudioUntilNormal();
1486}
1487
henrik.lundin7a926812016-05-12 13:51:28 -07001488class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1489 public:
1490 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1491
1492 void SetUp() override {
1493 NetEqDecodingTest::SetUp();
1494 config2_ = config_;
1495 }
1496
1497 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001498 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001499 ASSERT_TRUE(neteq2_);
1500 LoadDecoders(neteq2_.get());
1501 }
1502
1503 protected:
1504 std::unique_ptr<NetEq> neteq2_;
1505 NetEq::Config config2_;
1506};
1507
1508namespace {
1509::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1510 const AudioFrame& b) {
1511 if (a.timestamp_ != b.timestamp_)
1512 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1513 << " != " << b.timestamp_ << ")";
1514 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1515 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1516 << a.sample_rate_hz_
1517 << " != " << b.sample_rate_hz_ << ")";
1518 if (a.samples_per_channel_ != b.samples_per_channel_)
1519 return ::testing::AssertionFailure()
1520 << "samples_per_channel_ diff (" << a.samples_per_channel_
1521 << " != " << b.samples_per_channel_ << ")";
1522 if (a.num_channels_ != b.num_channels_)
1523 return ::testing::AssertionFailure() << "num_channels_ diff ("
1524 << a.num_channels_
1525 << " != " << b.num_channels_ << ")";
1526 if (a.speech_type_ != b.speech_type_)
1527 return ::testing::AssertionFailure() << "speech_type_ diff ("
1528 << a.speech_type_
1529 << " != " << b.speech_type_ << ")";
1530 if (a.vad_activity_ != b.vad_activity_)
1531 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1532 << a.vad_activity_
1533 << " != " << b.vad_activity_ << ")";
1534 return ::testing::AssertionSuccess();
1535}
1536
1537::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1538 const AudioFrame& b) {
1539 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1540 if (!res)
1541 return res;
1542 if (memcmp(
yujo36b1a5f2017-06-12 12:45:32 -07001543 a.data(), b.data(),
1544 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) != 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001545 return ::testing::AssertionFailure() << "data_ diff";
1546 }
1547 return ::testing::AssertionSuccess();
1548}
1549
1550} // namespace
1551
1552TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1553 ASSERT_FALSE(config_.enable_muted_state);
1554 config2_.enable_muted_state = true;
1555 CreateSecondInstance();
1556
1557 // Insert one speech packet into both NetEqs.
1558 const size_t kSamples = 10 * 16;
1559 const size_t kPayloadBytes = kSamples * 2;
1560 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001561 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001562 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001563 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1564 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001565
1566 AudioFrame out_frame1, out_frame2;
1567 bool muted;
1568 for (int i = 0; i < 1000; ++i) {
1569 std::ostringstream ss;
1570 ss << "i = " << i;
1571 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1572 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1573 EXPECT_FALSE(muted);
1574 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1575 if (muted) {
1576 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1577 } else {
1578 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1579 }
1580 }
1581 EXPECT_TRUE(muted);
1582
1583 // Insert new data. Timestamp is corrected for the time elapsed since the last
1584 // packet.
1585 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001586 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1587 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001588
1589 int counter = 0;
1590 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1591 ASSERT_LT(counter++, 1000) << "Test timed out";
1592 std::ostringstream ss;
1593 ss << "counter = " << counter;
1594 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1595 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1596 EXPECT_FALSE(muted);
1597 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1598 if (muted) {
1599 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1600 } else {
1601 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1602 }
1603 }
1604 EXPECT_FALSE(muted);
1605}
1606
henrik.lundin114c1b32017-04-26 07:47:32 -07001607TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1608 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1609
1610 // Pull out data once.
1611 AudioFrame output;
1612 bool muted;
1613 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1614
1615 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1616}
1617
1618TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1619 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1620 // default). Make the length 10 ms.
1621 constexpr size_t kPayloadSamples = 16 * 10;
1622 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1623 uint8_t payload[kPayloadBytes] = {0};
1624
1625 RTPHeader rtp_info;
1626 constexpr uint32_t kRtpTimestamp = 0x1234;
1627 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1628 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1629
1630 // Pull out data once.
1631 AudioFrame output;
1632 bool muted;
1633 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1634
1635 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1636 neteq_->LastDecodedTimestamps());
1637
1638 // Nothing decoded on the second call.
1639 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1640 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1641}
1642
1643TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1644 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1645 // by default). Make the length 5 ms so that NetEq must decode them both in
1646 // the same GetAudio call.
1647 constexpr size_t kPayloadSamples = 16 * 5;
1648 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1649 uint8_t payload[kPayloadBytes] = {0};
1650
1651 RTPHeader rtp_info;
1652 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1653 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1654 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1655 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1656 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1657 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1658
1659 // Pull out data once.
1660 AudioFrame output;
1661 bool muted;
1662 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1663
1664 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1665 neteq_->LastDecodedTimestamps());
1666}
1667
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001668TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1669 const int kNumConcealmentEvents = 19;
1670 const size_t kSamples = 10 * 16;
1671 const size_t kPayloadBytes = kSamples * 2;
1672 int seq_no = 0;
1673 RTPHeader rtp_info;
1674 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1675 rtp_info.payloadType = 94; // PCM16b WB codec.
1676 rtp_info.markerBit = 0;
1677 const uint8_t payload[kPayloadBytes] = {0};
1678 bool muted;
1679
1680 for (int i = 0; i < kNumConcealmentEvents; i++) {
1681 // Insert some packets of 10 ms size.
1682 for (int j = 0; j < 10; j++) {
1683 rtp_info.sequenceNumber = seq_no++;
1684 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1685 neteq_->InsertPacket(rtp_info, payload, 0);
1686 neteq_->GetAudio(&out_frame_, &muted);
1687 }
1688
1689 // Lose a number of packets.
1690 int num_lost = 1 + i;
1691 for (int j = 0; j < num_lost; j++) {
1692 seq_no++;
1693 neteq_->GetAudio(&out_frame_, &muted);
1694 }
1695 }
1696
1697 // Check number of concealment events.
1698 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1699 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1700}
1701
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001702// Test that the jitter buffer delay stat is computed correctly.
1703void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1704 const int kNumPackets = 10;
1705 const int kDelayInNumPackets = 2;
1706 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1707 const size_t kSamples = kPacketLenMs * 16;
1708 const size_t kPayloadBytes = kSamples * 2;
1709 RTPHeader rtp_info;
1710 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1711 rtp_info.payloadType = 94; // PCM16b WB codec.
1712 rtp_info.markerBit = 0;
1713 const uint8_t payload[kPayloadBytes] = {0};
1714 bool muted;
1715 int packets_sent = 0;
1716 int packets_received = 0;
1717 int expected_delay = 0;
1718 while (packets_received < kNumPackets) {
1719 // Insert packet.
1720 if (packets_sent < kNumPackets) {
1721 rtp_info.sequenceNumber = packets_sent++;
1722 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1723 neteq_->InsertPacket(rtp_info, payload, 0);
1724 }
1725
1726 // Get packet.
1727 if (packets_sent > kDelayInNumPackets) {
1728 neteq_->GetAudio(&out_frame_, &muted);
1729 packets_received++;
1730
1731 // The delay reported by the jitter buffer never exceeds
1732 // the number of samples previously fetched with GetAudio
1733 // (hence the min()).
1734 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1735
1736 // The increase of the expected delay is the product of
1737 // the current delay of the jitter buffer in ms * the
1738 // number of samples that are sent for play out.
1739 int current_delay_ms = packets_delay * kPacketLenMs;
1740 expected_delay += current_delay_ms * kSamples;
1741 }
1742 }
1743
1744 if (apply_packet_loss) {
1745 // Extra call to GetAudio to cause concealment.
1746 neteq_->GetAudio(&out_frame_, &muted);
1747 }
1748
1749 // Check jitter buffer delay.
1750 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1751 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
1752}
1753
1754TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1755 TestJitterBufferDelay(false);
1756}
1757
1758TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1759 TestJitterBufferDelay(true);
1760}
1761
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001762} // namespace webrtc