blob: 2757b024b8fb7b45eb51622e3551c3c647661d40 [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
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020023#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/audio_codecs/builtin_audio_decoder_factory.h"
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"
Henrik Lundin7687ad52018-07-02 10:14:46 +020027#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
28#include "modules/audio_coding/neteq/tools/neteq_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "modules/include/module_common_types_public.h"
Niels Möller53382cb2018-11-27 14:05:08 +010031#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/ignore_wundef.h"
Joachim Bauch4e909192017-12-19 22:27:51 +010034#include "rtc_base/messagedigest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/stringencode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020039#include "rtc_base/system/arch.h"
Henrik Lundine9619f82017-11-27 14:05:27 +010040#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "test/gtest.h"
42#include "test/testsupport/fileutils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Mirko Bonadei81ca3bf2018-01-09 09:40:39 +010044// This must come after test/gtest.h
45#include "rtc_base/flags.h" // NOLINT(build/include)
46
minyue5f026d02015-12-16 07:36:04 -080047#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070048RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080049#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
50#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
51#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080053#endif
kwiberg77eab702016-09-28 17:42:01 -070054RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080055#endif
56
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020057WEBRTC_DEFINE_bool(gen_ref, false, "Generate reference files.");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000058
kwiberg5adaf732016-10-04 09:33:27 -070059namespace webrtc {
60
minyue5f026d02015-12-16 07:36:04 -080061namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
minyue4f906772016-04-29 11:05:14 -070063const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020064 const std::string& checksum_android_32,
65 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070066 const std::string& checksum_win_32,
67 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070068#if defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +020069#ifdef WEBRTC_ARCH_64_BITS
70 return checksum_android_64;
71#else
72 return checksum_android_32;
73#endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070074#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020075#ifdef WEBRTC_ARCH_64_BITS
76 return checksum_win_64;
77#else
78 return checksum_win_32;
79#endif // WEBRTC_ARCH_64_BITS
minyue4f906772016-04-29 11:05:14 -070080#else
81 return checksum_general;
82#endif // WEBRTC_WIN
83}
84
minyue5f026d02015-12-16 07:36:04 -080085#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
86void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
87 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
88 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
89 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
90 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
91 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
minyue5f026d02015-12-16 07:36:04 -080092 stats->set_expand_rate(stats_raw.expand_rate);
93 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
94 stats->set_preemptive_rate(stats_raw.preemptive_rate);
95 stats->set_accelerate_rate(stats_raw.accelerate_rate);
96 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020097 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
minyue5f026d02015-12-16 07:36:04 -080098 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
99 stats->set_added_zero_samples(stats_raw.added_zero_samples);
100 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
101 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
102 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
103 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
104}
105
106void Convert(const webrtc::RtcpStatistics& stats_raw,
107 webrtc::neteq_unittest::RtcpStatistics* stats) {
108 stats->set_fraction_lost(stats_raw.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700109 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800110 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700111 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800112 stats->set_jitter(stats_raw.jitter);
113}
114
Yves Gerey665174f2018-06-19 15:03:05 +0200115void AddMessage(FILE* file,
116 rtc::MessageDigest* digest,
minyue4f906772016-04-29 11:05:14 -0700117 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800118 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700119 if (file)
120 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
121 digest->Update(&size, sizeof(size));
122
123 if (file)
124 ASSERT_EQ(static_cast<size_t>(size),
125 fwrite(message.data(), sizeof(char), size, file));
126 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800127}
128
minyue5f026d02015-12-16 07:36:04 -0800129#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
130
henrik.lundin7a926812016-05-12 13:51:28 -0700131void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700132 ASSERT_EQ(true,
133 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
134 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
135 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700136 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
137 "pcma", 8));
138#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700139 ASSERT_EQ(true,
140 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700141#endif
142#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700143 ASSERT_EQ(true,
144 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700145#endif
146#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700147 ASSERT_EQ(true,
148 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700149#endif
150#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700151 ASSERT_EQ(true,
152 neteq->RegisterPayloadType(
153 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700154#endif
kwiberg5adaf732016-10-04 09:33:27 -0700155 ASSERT_EQ(true,
156 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
157 ASSERT_EQ(true,
158 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
159 ASSERT_EQ(true,
160 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
161 ASSERT_EQ(true,
162 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
163 ASSERT_EQ(true,
164 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700165}
minyue5f026d02015-12-16 07:36:04 -0800166} // namespace
167
minyue4f906772016-04-29 11:05:14 -0700168class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 public:
minyue4f906772016-04-29 11:05:14 -0700170 explicit ResultSink(const std::string& output_file);
171 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000172
Yves Gerey665174f2018-06-19 15:03:05 +0200173 template <typename T>
174 void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700175
176 void AddResult(const NetEqNetworkStatistics& stats);
177 void AddResult(const RtcpStatistics& stats);
178
179 void VerifyChecksum(const std::string& ref_check_sum);
180
181 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700183 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184};
185
Joachim Bauch4e909192017-12-19 22:27:51 +0100186ResultSink::ResultSink(const std::string& output_file)
minyue4f906772016-04-29 11:05:14 -0700187 : output_fp_(nullptr),
Joachim Bauch4e909192017-12-19 22:27:51 +0100188 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 if (!output_file.empty()) {
190 output_fp_ = fopen(output_file.c_str(), "wb");
191 EXPECT_TRUE(output_fp_ != NULL);
192 }
193}
194
minyue4f906772016-04-29 11:05:14 -0700195ResultSink::~ResultSink() {
196 if (output_fp_)
197 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198}
199
Yves Gerey665174f2018-06-19 15:03:05 +0200200template <typename T>
yujo36b1a5f2017-06-12 12:45:32 -0700201void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700203 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 }
yujo36b1a5f2017-06-12 12:45:32 -0700205 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206}
207
minyue4f906772016-04-29 11:05:14 -0700208void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800209#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800210 neteq_unittest::NetEqNetworkStatistics stats;
211 Convert(stats_raw, &stats);
212
mbonadei7c2c8432017-04-07 00:59:12 -0700213 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800214 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700215 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800216#else
217 FAIL() << "Writing to reference file requires Proto Buffer.";
218#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000219}
220
minyue4f906772016-04-29 11:05:14 -0700221void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800222#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800223 neteq_unittest::RtcpStatistics stats;
224 Convert(stats_raw, &stats);
225
mbonadei7c2c8432017-04-07 00:59:12 -0700226 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800227 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700228 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800229#else
230 FAIL() << "Writing to reference file requires Proto Buffer.";
231#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232}
233
minyue4f906772016-04-29 11:05:14 -0700234void ResultSink::VerifyChecksum(const std::string& checksum) {
235 std::vector<char> buffer;
236 buffer.resize(digest_->Size());
237 digest_->Finish(&buffer[0], buffer.size());
238 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
Yves Gereya038e712018-11-14 10:45:50 +0100239 if (checksum.size() == result.size()) {
240 EXPECT_EQ(checksum, result);
241 } else {
242 // Check result is one the '|'-separated checksums.
243 EXPECT_NE(checksum.find(result), std::string::npos)
244 << result << " should be one of these:\n"
245 << checksum;
246 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000247}
248
249class NetEqDecodingTest : public ::testing::Test {
250 protected:
251 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
252 // constants below can be changed.
253 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700254 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
255 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
256 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800257 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258 static const int kInitSampleRateHz = 8000;
259
260 NetEqDecodingTest();
261 virtual void SetUp();
262 virtual void TearDown();
263 void SelectDecoders(NetEqDecoder* used_codec);
Yves Gerey665174f2018-06-19 15:03:05 +0200264 void OpenInputFile(const std::string& rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800265 void Process();
minyue5f026d02015-12-16 07:36:04 -0800266
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000267 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700268 const std::string& output_checksum,
269 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700270 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800271
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 static void PopulateRtpInfo(int frame_index,
273 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700274 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275 static void PopulateCng(int frame_index,
276 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700277 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000279 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280
Yves Gerey665174f2018-06-19 15:03:05 +0200281 void WrapTest(uint16_t start_seq_no,
282 uint32_t start_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000283 const std::set<uint16_t>& drop_seq_numbers,
Yves Gerey665174f2018-06-19 15:03:05 +0200284 bool expect_seq_no_wrap,
285 bool expect_timestamp_wrap);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000286
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000287 void LongCngWithClockDrift(double drift_factor,
288 double network_freeze_ms,
289 bool pull_audio_during_freeze,
290 int delay_tolerance_ms,
291 int max_time_to_speech_ms);
292
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000293 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000294
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000296 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800297 std::unique_ptr<test::RtpFileSource> rtp_source_;
298 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800300 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000302 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303};
304
305// Allocating the static const so that it can be passed by reference.
306const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700307const size_t NetEqDecodingTest::kBlockSize8kHz;
308const size_t NetEqDecodingTest::kBlockSize16kHz;
309const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310const int NetEqDecodingTest::kInitSampleRateHz;
311
312NetEqDecodingTest::NetEqDecodingTest()
313 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000314 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000316 output_sample_rate_(kInitSampleRateHz),
317 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000318 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319}
320
321void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700322 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000323 NetEqNetworkStatistics stat;
324 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
325 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700327 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328}
329
330void NetEqDecodingTest::TearDown() {
331 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332}
333
Yves Gerey665174f2018-06-19 15:03:05 +0200334void NetEqDecodingTest::OpenInputFile(const std::string& rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000335 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336}
337
henrik.lundin6d8e0112016-03-04 10:34:21 -0800338void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000340 while (packet_ && sim_clock_ >= packet_->time_ms()) {
341 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800342#ifndef WEBRTC_CODEC_ISAC
343 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700344 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800345#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200346 ASSERT_EQ(0,
347 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700348 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200349 rtc::ArrayView<const uint8_t>(
350 packet_->payload(), packet_->payload_length_bytes()),
351 static_cast<uint32_t>(packet_->time_ms() *
352 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 }
354 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700355 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 }
357
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000358 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700359 bool muted;
360 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
361 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800362 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
363 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
364 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
365 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
366 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800367 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368
369 // Increase time.
370 sim_clock_ += kTimeStepMs;
371}
372
minyue4f906772016-04-29 11:05:14 -0700373void NetEqDecodingTest::DecodeAndCompare(
374 const std::string& rtp_file,
375 const std::string& output_checksum,
376 const std::string& network_stats_checksum,
minyue4f906772016-04-29 11:05:14 -0700377 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 OpenInputFile(rtp_file);
379
minyue4f906772016-04-29 11:05:14 -0700380 std::string ref_out_file =
381 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
382 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383
minyue4f906772016-04-29 11:05:14 -0700384 std::string stat_out_file =
385 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
386 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000387
henrik.lundin46ba49c2016-05-24 22:50:47 -0700388 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389 int i = 0;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200390 uint64_t last_concealed_samples = 0;
391 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000392 while (packet_) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200393 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
395 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800396 ASSERT_NO_FATAL_FAILURE(Process());
Yves Gerey665174f2018-06-19 15:03:05 +0200397 ASSERT_NO_FATAL_FAILURE(
398 output.AddResult(out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399
400 // Query the network statistics API once per second
401 if (sim_clock_ % 1000 == 0) {
402 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700403 NetEqNetworkStatistics current_network_stats;
404 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
405 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
406
Henrik Lundinac0a5032017-09-25 12:22:46 +0200407 // Verify that liftime stats and network stats report similar loss
408 // concealment rates.
409 auto lifetime_stats = neteq_->GetLifetimeStatistics();
410 const uint64_t delta_concealed_samples =
411 lifetime_stats.concealed_samples - last_concealed_samples;
412 last_concealed_samples = lifetime_stats.concealed_samples;
413 const uint64_t delta_total_samples_received =
414 lifetime_stats.total_samples_received - last_total_samples_received;
415 last_total_samples_received = lifetime_stats.total_samples_received;
416 // The tolerance is 1% but expressed in Q14.
417 EXPECT_NEAR(
418 (delta_concealed_samples << 14) / delta_total_samples_received,
419 current_network_stats.expand_rate, (2 << 14) / 100.0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000420 }
421 }
minyue4f906772016-04-29 11:05:14 -0700422
423 SCOPED_TRACE("Check output audio.");
424 output.VerifyChecksum(output_checksum);
425 SCOPED_TRACE("Check network stats.");
426 network_stats.VerifyChecksum(network_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000427}
428
429void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
430 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700431 RTPHeader* rtp_info) {
432 rtp_info->sequenceNumber = frame_index;
433 rtp_info->timestamp = timestamp;
434 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
435 rtp_info->payloadType = 94; // PCM16b WB codec.
436 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000437}
438
439void NetEqDecodingTest::PopulateCng(int frame_index,
440 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700441 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000443 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700444 rtp_info->sequenceNumber = frame_index;
445 rtp_info->timestamp = timestamp;
446 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
447 rtp_info->payloadType = 98; // WB CNG.
448 rtp_info->markerBit = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200449 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000450 *payload_len = 1; // Only noise level, no spectral parameters.
451}
452
ivoc72c08ed2016-01-20 07:26:24 -0800453#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
454 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +0100455 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800456#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700457#else
minyue5f026d02015-12-16 07:36:04 -0800458#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700459#endif
minyue5f026d02015-12-16 07:36:04 -0800460TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800461 const std::string input_rtp_file =
462 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000463
Yves Gerey665174f2018-06-19 15:03:05 +0200464 const std::string output_checksum =
465 PlatformChecksum("0c6dc227f781c81a229970f8fceda1a012498cba",
466 "15c4a2202877a414515e218bdb7992f0ad53e5af", "not used",
467 "0c6dc227f781c81a229970f8fceda1a012498cba",
468 "25fc4c863caa499aa447a5b8d059f5452cbcc500");
minyue4f906772016-04-29 11:05:14 -0700469
henrik.lundin2979f552017-05-05 05:04:16 -0700470 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200471 PlatformChecksum("4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
Yves Gerey665174f2018-06-19 15:03:05 +0200472 "e339cb2adf5ab3dfc21cb7205d670a34751e8336", "not used",
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200473 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9",
474 "4b2370f5c794741d2a46be5c7935c66ef3fb53e9");
minyue4f906772016-04-29 11:05:14 -0700475
Yves Gerey665174f2018-06-19 15:03:05 +0200476 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100477 FLAG_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000478}
479
Yves Gerey665174f2018-06-19 15:03:05 +0200480#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200481 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800482#define MAYBE_TestOpusBitExactness TestOpusBitExactness
483#else
484#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
485#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200486TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800487 const std::string input_rtp_file =
488 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800489
Yves Gereya038e712018-11-14 10:45:50 +0100490 // Checksum depends on libopus being compiled with or without SSE.
491 const std::string maybe_sse =
492 "14a63b3c7b925c82296be4bafc71bec85f2915c2|"
493 "2c05677daa968d6c68b92adf4affb7cd9bb4d363";
494 const std::string output_checksum = PlatformChecksum(
495 maybe_sse, "b7b7ed802b0e18ee416973bf3b9ae98599b0181d",
496 "5876e52dda90d5ca433c3726555b907b97c86374", maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700497
henrik.lundin2979f552017-05-05 05:04:16 -0700498 const std::string network_stats_checksum =
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200499 PlatformChecksum("adb3272498e436d1c019cbfd71610e9510c54497",
500 "fa935a91abc7291db47428a2d7c5361b98713a92",
501 "42106aa5267300f709f63737707ef07afd9dac61",
502 "adb3272498e436d1c019cbfd71610e9510c54497",
503 "adb3272498e436d1c019cbfd71610e9510c54497");
minyue4f906772016-04-29 11:05:14 -0700504
Yves Gerey665174f2018-06-19 15:03:05 +0200505 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100506 FLAG_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800507}
508
Yves Gerey665174f2018-06-19 15:03:05 +0200509#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100510 defined(WEBRTC_CODEC_OPUS)
511#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
512#else
513#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
514#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100515TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100516 const std::string input_rtp_file =
517 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
518
Yves Gereya038e712018-11-14 10:45:50 +0100519 const std::string maybe_sse =
520 "713af6c92881f5aab1285765ee6680da9d1c06ce|"
521 "2ac10c4e79aeedd0df2863b079da5848b40f00b5";
522 const std::string output_checksum = PlatformChecksum(
523 maybe_sse, "3ec991b96872123f1554c03c543ca5d518431e46",
524 "da9f9a2d94e0c2d67342fad4965d7b91cda50b25", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100525
526 const std::string network_stats_checksum =
527 "bab58dc587d956f326056d7340c96eb9d2d3cc21";
528
Henrik Lundine9619f82017-11-27 14:05:27 +0100529 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Niels Möllerd51b3552018-11-21 13:51:24 +0100530 FLAG_gen_ref);
Henrik Lundine9619f82017-11-27 14:05:27 +0100531}
532
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000533// Use fax mode to avoid time-scaling. This is to simplify the testing of
534// packet waiting times in the packet buffer.
535class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
536 protected:
537 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200538 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000539 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200540 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000541};
542
543TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000544 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
545 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000546 const size_t kSamples = 10 * 16;
547 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000548 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800549 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700550 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200551 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
552 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700553 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
554 rtp_info.payloadType = 94; // PCM16b WB codec.
555 rtp_info.markerBit = 0;
556 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557 }
558 // Pull out all data.
559 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700560 bool muted;
561 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800562 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000563 }
564
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200565 NetEqNetworkStatistics stats;
566 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
568 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200569 // each packet. Thus, we are calculating the statistics for a series from 10
570 // to 300, in steps of 10 ms.
571 EXPECT_EQ(155, stats.mean_waiting_time_ms);
572 EXPECT_EQ(155, stats.median_waiting_time_ms);
573 EXPECT_EQ(10, stats.min_waiting_time_ms);
574 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000575
576 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200577 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
578 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
579 EXPECT_EQ(-1, stats.median_waiting_time_ms);
580 EXPECT_EQ(-1, stats.min_waiting_time_ms);
581 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000582}
583
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000584TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585 const int kNumFrames = 3000; // Needed for convergence.
586 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000587 const size_t kSamples = 10 * 16;
588 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 while (frame_index < kNumFrames) {
590 // Insert one packet each time, except every 10th time where we insert two
591 // packets at once. This will create a negative clock-drift of approx. 10%.
592 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
593 for (int n = 0; n < num_packets; ++n) {
594 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700595 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000596 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700597 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598 ++frame_index;
599 }
600
601 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700602 bool muted;
603 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800604 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 }
606
607 NetEqNetworkStatistics network_stats;
608 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700609 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610}
611
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000612TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000613 const int kNumFrames = 5000; // Needed for convergence.
614 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000615 const size_t kSamples = 10 * 16;
616 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000617 for (int i = 0; i < kNumFrames; ++i) {
618 // Insert one packet each time, except every 10th time where we don't insert
619 // any packet. This will create a positive clock-drift of approx. 11%.
620 int num_packets = (i % 10 == 9 ? 0 : 1);
621 for (int n = 0; n < num_packets; ++n) {
622 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700623 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000624 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700625 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000626 ++frame_index;
627 }
628
629 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700630 bool muted;
631 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800632 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 }
634
635 NetEqNetworkStatistics network_stats;
636 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700637 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638}
639
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000640void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
641 double network_freeze_ms,
642 bool pull_audio_during_freeze,
643 int delay_tolerance_ms,
644 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000645 uint16_t seq_no = 0;
646 uint32_t timestamp = 0;
647 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000648 const size_t kSamples = kFrameSizeMs * 16;
649 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650 double next_input_time_ms = 0.0;
651 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700652 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000653
654 // Insert speech for 5 seconds.
655 const int kSpeechDurationMs = 5000;
656 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
657 // Each turn in this for loop is 10 ms.
658 while (next_input_time_ms <= t_ms) {
659 // Insert one 30 ms speech frame.
660 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700661 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000662 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700663 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000664 ++seq_no;
665 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000666 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000667 }
668 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700669 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800670 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000671 }
672
henrik.lundin55480f52016-03-08 02:37:57 -0800673 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200674 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700675 ASSERT_TRUE(playout_timestamp);
676 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677
678 // Insert CNG for 1 minute (= 60000 ms).
679 const int kCngPeriodMs = 100;
680 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
681 const int kCngDurationMs = 60000;
682 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
683 // Each turn in this for loop is 10 ms.
684 while (next_input_time_ms <= t_ms) {
685 // Insert one CNG frame each 100 ms.
686 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000687 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700688 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000689 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800690 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700691 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800692 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693 ++seq_no;
694 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000695 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000696 }
697 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700698 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800699 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000700 }
701
henrik.lundin55480f52016-03-08 02:37:57 -0800702 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000703
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000704 if (network_freeze_ms > 0) {
705 // First keep pulling audio for |network_freeze_ms| without inserting
706 // any data, then insert CNG data corresponding to |network_freeze_ms|
707 // without pulling any output audio.
708 const double loop_end_time = t_ms + network_freeze_ms;
709 for (; t_ms < loop_end_time; t_ms += 10) {
710 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700711 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800712 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800713 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000714 }
715 bool pull_once = pull_audio_during_freeze;
716 // If |pull_once| is true, GetAudio will be called once half-way through
717 // the network recovery period.
718 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
719 while (next_input_time_ms <= t_ms) {
720 if (pull_once && next_input_time_ms >= pull_time_ms) {
721 pull_once = false;
722 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700723 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800724 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800725 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000726 t_ms += 10;
727 }
728 // Insert one CNG frame each 100 ms.
729 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000730 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700731 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800733 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700734 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800735 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000736 ++seq_no;
737 timestamp += kCngPeriodSamples;
738 next_input_time_ms += kCngPeriodMs * drift_factor;
739 }
740 }
741
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000742 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000743 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800744 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000745 // Each turn in this for loop is 10 ms.
746 while (next_input_time_ms <= t_ms) {
747 // Insert one 30 ms speech frame.
748 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700749 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000750 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700751 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000752 ++seq_no;
753 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000754 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 }
756 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700757 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800758 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000759 // Increase clock.
760 t_ms += 10;
761 }
762
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000763 // Check that the speech starts again within reasonable time.
764 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
765 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700766 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700767 ASSERT_TRUE(playout_timestamp);
768 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000769 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000770 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
771 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000772}
773
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000774TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000775 // Apply a clock drift of -25 ms / s (sender faster than receiver).
776 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000777 const double kNetworkFreezeTimeMs = 0.0;
778 const bool kGetAudioDuringFreezeRecovery = false;
779 const int kDelayToleranceMs = 20;
780 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200781 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
782 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000783 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000784}
785
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000786TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000787 // Apply a clock drift of +25 ms / s (sender slower than receiver).
788 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000789 const double kNetworkFreezeTimeMs = 0.0;
790 const bool kGetAudioDuringFreezeRecovery = false;
791 const int kDelayToleranceMs = 20;
792 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200793 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
794 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000795 kMaxTimeToSpeechMs);
796}
797
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000798TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000799 // Apply a clock drift of -25 ms / s (sender faster than receiver).
800 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
801 const double kNetworkFreezeTimeMs = 5000.0;
802 const bool kGetAudioDuringFreezeRecovery = false;
803 const int kDelayToleranceMs = 50;
804 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200805 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
806 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000807 kMaxTimeToSpeechMs);
808}
809
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000810TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000811 // Apply a clock drift of +25 ms / s (sender slower than receiver).
812 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
813 const double kNetworkFreezeTimeMs = 5000.0;
814 const bool kGetAudioDuringFreezeRecovery = false;
815 const int kDelayToleranceMs = 20;
816 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200817 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
818 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000819 kMaxTimeToSpeechMs);
820}
821
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000822TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000823 // Apply a clock drift of +25 ms / s (sender slower than receiver).
824 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
825 const double kNetworkFreezeTimeMs = 5000.0;
826 const bool kGetAudioDuringFreezeRecovery = true;
827 const int kDelayToleranceMs = 20;
828 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200829 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
830 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000831 kMaxTimeToSpeechMs);
832}
833
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000834TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000835 const double kDriftFactor = 1.0; // No drift.
836 const double kNetworkFreezeTimeMs = 0.0;
837 const bool kGetAudioDuringFreezeRecovery = false;
838 const int kDelayToleranceMs = 10;
839 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200840 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
841 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000842 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000843}
844
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000845TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000846 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700848 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700850 rtp_info.payloadType = 1; // Not registered as a decoder.
851 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000852}
853
Peter Boströme2976c82016-01-04 22:44:05 +0100854#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800855#define MAYBE_DecoderError DecoderError
856#else
857#define MAYBE_DecoderError DISABLED_DecoderError
858#endif
859
Peter Boströme2976c82016-01-04 22:44:05 +0100860TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000861 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000862 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700863 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000864 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700865 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
866 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000867 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
868 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700869 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800870 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700871 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000872 }
henrik.lundin7a926812016-05-12 13:51:28 -0700873 bool muted;
874 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
875 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800876
yujo36b1a5f2017-06-12 12:45:32 -0700877 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700879 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200881 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 ss << "i = " << i;
883 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700884 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000885 }
886}
887
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000888TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
890 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700891 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800892 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700893 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000894 }
henrik.lundin7a926812016-05-12 13:51:28 -0700895 bool muted;
896 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
897 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 // Verify that the first block of samples is set to 0.
899 static const int kExpectedOutputLength =
900 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700901 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000902 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200903 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000904 ss << "i = " << i;
905 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700906 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000907 }
henrik.lundind89814b2015-11-23 06:49:25 -0800908 // Verify that the sample rate did not change from the initial configuration.
909 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000910}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000911
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000912class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000913 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000914 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700915 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000916 uint8_t payload_type = 0xFF; // Invalid.
917 if (sampling_rate_hz == 8000) {
918 expected_samples_per_channel = kBlockSize8kHz;
919 payload_type = 93; // PCM 16, 8 kHz.
920 } else if (sampling_rate_hz == 16000) {
921 expected_samples_per_channel = kBlockSize16kHz;
922 payload_type = 94; // PCM 16, 16 kHZ.
923 } else if (sampling_rate_hz == 32000) {
924 expected_samples_per_channel = kBlockSize32kHz;
925 payload_type = 95; // PCM 16, 32 kHz.
926 } else {
927 ASSERT_TRUE(false); // Unsupported test case.
928 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000929
henrik.lundin6d8e0112016-03-04 10:34:21 -0800930 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000931 test::AudioLoop input;
932 // We are using the same 32 kHz input file for all tests, regardless of
933 // |sampling_rate_hz|. The output may sound weird, but the test is still
934 // valid.
935 ASSERT_TRUE(input.Init(
936 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
937 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700938 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000939
940 // Payload of 10 ms of PCM16 32 kHz.
941 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700942 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000943 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700944 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000945
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000946 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700947 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000948 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800949 auto block = input.GetNextBlock();
950 ASSERT_EQ(expected_samples_per_channel, block.size());
951 size_t enc_len_bytes =
952 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000953 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
954
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200955 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700956 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200957 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
958 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800959 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700960 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800961 ASSERT_EQ(1u, output.num_channels_);
962 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800963 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000964
965 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200966 rtp_info.timestamp +=
967 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700968 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200969 receive_timestamp +=
970 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000971 }
972
henrik.lundin6d8e0112016-03-04 10:34:21 -0800973 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000974
975 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
976 // one frame without checking speech-type. This is the first frame pulled
977 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700978 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800979 ASSERT_EQ(1u, output.num_channels_);
980 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000981
982 // To be able to test the fading of background noise we need at lease to
983 // pull 611 frames.
984 const int kFadingThreshold = 611;
985
986 // Test several CNG-to-PLC packet for the expected behavior. The number 20
987 // is arbitrary, but sufficiently large to test enough number of frames.
988 const int kNumPlcToCngTestFrames = 20;
989 bool plc_to_cng = false;
990 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800991 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700992 // Set to non-zero.
993 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700994 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
995 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800996 ASSERT_EQ(1u, output.num_channels_);
997 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800998 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000999 plc_to_cng = true;
1000 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001001 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001002 for (size_t k = 0;
1003 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001004 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +02001005 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001006 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001007 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001008 }
1009 }
1010 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1011 }
1012};
1013
Henrik Lundin67190172018-04-20 15:34:48 +02001014TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001015 CheckBgn(8000);
1016 CheckBgn(16000);
1017 CheckBgn(32000);
1018}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001019
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001020void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1021 uint32_t start_timestamp,
1022 const std::set<uint16_t>& drop_seq_numbers,
1023 bool expect_seq_no_wrap,
1024 bool expect_timestamp_wrap) {
1025 uint16_t seq_no = start_seq_no;
1026 uint32_t timestamp = start_timestamp;
1027 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1028 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1029 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001030 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001031 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001032 uint32_t receive_timestamp = 0;
1033
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001034 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001035 const int kSpeechDurationMs = 2000;
1036 int packets_inserted = 0;
1037 uint16_t last_seq_no;
1038 uint32_t last_timestamp;
1039 bool timestamp_wrapped = false;
1040 bool seq_no_wrapped = false;
1041 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1042 // Each turn in this for loop is 10 ms.
1043 while (next_input_time_ms <= t_ms) {
1044 // Insert one 30 ms speech frame.
1045 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001046 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001047 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1048 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1049 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001050 ASSERT_EQ(0,
1051 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001052 ++packets_inserted;
1053 }
1054 NetEqNetworkStatistics network_stats;
1055 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1056
1057 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1058 // packet size for first few packets. Therefore we refrain from checking
1059 // the criteria.
1060 if (packets_inserted > 4) {
1061 // Expect preferred and actual buffer size to be no more than 2 frames.
1062 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
Yves Gerey665174f2018-06-19 15:03:05 +02001063 EXPECT_LE(network_stats.current_buffer_size_ms,
1064 kFrameSizeMs * 2 + algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001065 }
1066 last_seq_no = seq_no;
1067 last_timestamp = timestamp;
1068
1069 ++seq_no;
1070 timestamp += kSamples;
1071 receive_timestamp += kSamples;
1072 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1073
1074 seq_no_wrapped |= seq_no < last_seq_no;
1075 timestamp_wrapped |= timestamp < last_timestamp;
1076 }
1077 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001078 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001079 bool muted;
1080 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001081 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1082 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001083
1084 // Expect delay (in samples) to be less than 2 packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +02001085 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001086 ASSERT_TRUE(playout_timestamp);
1087 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001088 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001089 }
1090 // Make sure we have actually tested wrap-around.
1091 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1092 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1093}
1094
1095TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1096 // Start with a sequence number that will soon wrap.
1097 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1098 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1099}
1100
1101TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1102 // Start with a sequence number that will soon wrap.
1103 std::set<uint16_t> drop_seq_numbers;
1104 drop_seq_numbers.insert(0xFFFF);
1105 drop_seq_numbers.insert(0x0);
1106 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1107}
1108
1109TEST_F(NetEqDecodingTest, TimestampWrap) {
1110 // Start with a timestamp that will soon wrap.
1111 std::set<uint16_t> drop_seq_numbers;
1112 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1113}
1114
1115TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1116 // Start with a timestamp and a sequence number that will wrap at the same
1117 // time.
1118 std::set<uint16_t> drop_seq_numbers;
1119 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1120}
1121
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001122void NetEqDecodingTest::DuplicateCng() {
1123 uint16_t seq_no = 0;
1124 uint32_t timestamp = 0;
1125 const int kFrameSizeMs = 10;
1126 const int kSampleRateKhz = 16;
1127 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001128 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001129
Yves Gerey665174f2018-06-19 15:03:05 +02001130 const int algorithmic_delay_samples =
1131 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001132 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001133 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001134 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001135 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001136 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001137 for (int i = 0; i < 3; ++i) {
1138 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001139 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001140 ++seq_no;
1141 timestamp += kSamples;
1142
1143 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001144 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001145 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001146 }
1147 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001148 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001149
1150 // Insert same CNG packet twice.
1151 const int kCngPeriodMs = 100;
1152 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001153 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001154 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1155 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001156 ASSERT_EQ(
1157 0, neteq_->InsertPacket(
1158 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001159
1160 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001161 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001162 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001163 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001164 EXPECT_FALSE(
1165 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001166 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1167 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001168
1169 // Insert the same CNG packet again. Note that at this point it is old, since
1170 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001171 ASSERT_EQ(
1172 0, neteq_->InsertPacket(
1173 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001174
1175 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1176 // we have already pulled out CNG once.
1177 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001178 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001179 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001180 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001181 EXPECT_FALSE(
1182 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001183 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001184 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001185 }
1186
1187 // Insert speech again.
1188 ++seq_no;
1189 timestamp += kCngPeriodSamples;
1190 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001191 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001192
1193 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001194 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001195 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001196 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +02001197 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001198 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001199 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001200 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001201}
1202
Yves Gerey665174f2018-06-19 15:03:05 +02001203TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
1204 DuplicateCng();
1205}
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001206
1207TEST_F(NetEqDecodingTest, CngFirst) {
1208 uint16_t seq_no = 0;
1209 uint32_t timestamp = 0;
1210 const int kFrameSizeMs = 10;
1211 const int kSampleRateKhz = 16;
1212 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1213 const int kPayloadBytes = kSamples * 2;
1214 const int kCngPeriodMs = 100;
1215 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1216 size_t payload_len;
1217
1218 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001219 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001220
1221 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 ASSERT_EQ(
1223 NetEq::kOK,
1224 neteq_->InsertPacket(
1225 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001226 ++seq_no;
1227 timestamp += kCngPeriodSamples;
1228
1229 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001230 bool muted;
1231 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001232 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001233 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001234
1235 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001236 const uint32_t first_speech_timestamp = timestamp;
1237 int timeout_counter = 0;
1238 do {
1239 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001240 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001241 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001242 ++seq_no;
1243 timestamp += kSamples;
1244
1245 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001246 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001247 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001248 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001249 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001250 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001251}
henrik.lundin7a926812016-05-12 13:51:28 -07001252
1253class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1254 public:
1255 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1256 config_.enable_muted_state = true;
1257 }
1258
1259 protected:
1260 static constexpr size_t kSamples = 10 * 16;
1261 static constexpr size_t kPayloadBytes = kSamples * 2;
1262
1263 void InsertPacket(uint32_t rtp_timestamp) {
1264 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001265 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001266 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001267 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001268 }
1269
henrik.lundin42feb512016-09-20 06:51:40 -07001270 void InsertCngPacket(uint32_t rtp_timestamp) {
1271 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001272 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001273 size_t payload_len;
1274 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001275 EXPECT_EQ(
1276 NetEq::kOK,
1277 neteq_->InsertPacket(
1278 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001279 }
1280
henrik.lundin7a926812016-05-12 13:51:28 -07001281 bool GetAudioReturnMuted() {
1282 bool muted;
1283 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1284 return muted;
1285 }
1286
1287 void GetAudioUntilMuted() {
1288 while (!GetAudioReturnMuted()) {
1289 ASSERT_LT(counter_++, 1000) << "Test timed out";
1290 }
1291 }
1292
1293 void GetAudioUntilNormal() {
1294 bool muted = false;
1295 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1296 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1297 ASSERT_LT(counter_++, 1000) << "Test timed out";
1298 }
1299 EXPECT_FALSE(muted);
1300 }
1301
1302 int counter_ = 0;
1303};
1304
1305// Verifies that NetEq goes in and out of muted state as expected.
1306TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1307 // Insert one speech packet.
1308 InsertPacket(0);
1309 // Pull out audio once and expect it not to be muted.
1310 EXPECT_FALSE(GetAudioReturnMuted());
1311 // Pull data until faded out.
1312 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001313 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001314
1315 // Verify that output audio is not written during muted mode. Other parameters
1316 // should be correct, though.
1317 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001318 int16_t* frame_data = new_frame.mutable_data();
1319 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1320 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001321 }
1322 bool muted;
1323 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1324 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001325 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001326 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1327 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001328 }
1329 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1330 new_frame.timestamp_);
1331 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1332 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1333 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1334 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1335 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1336
1337 // Insert new data. Timestamp is corrected for the time elapsed since the last
1338 // packet. Verify that normal operation resumes.
1339 InsertPacket(kSamples * counter_);
1340 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001341 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001342
1343 NetEqNetworkStatistics stats;
1344 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1345 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1346 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1347 // concealment samples in this test.
1348 EXPECT_GT(stats.expand_rate, 14000);
1349 // And, it should be greater than the speech_expand_rate.
1350 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001351}
1352
1353// Verifies that NetEq goes out of muted state when given a delayed packet.
1354TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1355 // Insert one speech packet.
1356 InsertPacket(0);
1357 // Pull out audio once and expect it not to be muted.
1358 EXPECT_FALSE(GetAudioReturnMuted());
1359 // Pull data until faded out.
1360 GetAudioUntilMuted();
1361 // Insert new data. Timestamp is only corrected for the half of the time
1362 // elapsed since the last packet. That is, the new packet is delayed. Verify
1363 // that normal operation resumes.
1364 InsertPacket(kSamples * counter_ / 2);
1365 GetAudioUntilNormal();
1366}
1367
1368// Verifies that NetEq goes out of muted state when given a future packet.
1369TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1370 // Insert one speech packet.
1371 InsertPacket(0);
1372 // Pull out audio once and expect it not to be muted.
1373 EXPECT_FALSE(GetAudioReturnMuted());
1374 // Pull data until faded out.
1375 GetAudioUntilMuted();
1376 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1377 // last packet. That is, the new packet is too early. Verify that normal
1378 // operation resumes.
1379 InsertPacket(kSamples * counter_ * 2);
1380 GetAudioUntilNormal();
1381}
1382
1383// Verifies that NetEq goes out of muted state when given an old packet.
1384TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1385 // Insert one speech packet.
1386 InsertPacket(0);
1387 // Pull out audio once and expect it not to be muted.
1388 EXPECT_FALSE(GetAudioReturnMuted());
1389 // Pull data until faded out.
1390 GetAudioUntilMuted();
1391
1392 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1393 // Insert packet which is older than the first packet.
1394 InsertPacket(kSamples * (counter_ - 1000));
1395 EXPECT_FALSE(GetAudioReturnMuted());
1396 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1397}
1398
henrik.lundin42feb512016-09-20 06:51:40 -07001399// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1400// packet stream is suspended for a long time.
1401TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1402 // Insert one CNG packet.
1403 InsertCngPacket(0);
1404
1405 // Pull 10 seconds of audio (10 ms audio generated per lap).
1406 for (int i = 0; i < 1000; ++i) {
1407 bool muted;
1408 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1409 ASSERT_FALSE(muted);
1410 }
1411 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1412}
1413
1414// Verifies that NetEq goes back to normal after a long CNG period with the
1415// packet stream suspended.
1416TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1417 // Insert one CNG packet.
1418 InsertCngPacket(0);
1419
1420 // Pull 10 seconds of audio (10 ms audio generated per lap).
1421 for (int i = 0; i < 1000; ++i) {
1422 bool muted;
1423 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1424 }
1425
1426 // Insert new data. Timestamp is corrected for the time elapsed since the last
1427 // packet. Verify that normal operation resumes.
1428 InsertPacket(kSamples * counter_);
1429 GetAudioUntilNormal();
1430}
1431
henrik.lundin7a926812016-05-12 13:51:28 -07001432class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1433 public:
1434 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1435
1436 void SetUp() override {
1437 NetEqDecodingTest::SetUp();
1438 config2_ = config_;
1439 }
1440
1441 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001442 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001443 ASSERT_TRUE(neteq2_);
1444 LoadDecoders(neteq2_.get());
1445 }
1446
1447 protected:
1448 std::unique_ptr<NetEq> neteq2_;
1449 NetEq::Config config2_;
1450};
1451
1452namespace {
1453::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1454 const AudioFrame& b) {
1455 if (a.timestamp_ != b.timestamp_)
1456 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1457 << " != " << b.timestamp_ << ")";
1458 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +02001459 return ::testing::AssertionFailure()
1460 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
1461 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001462 if (a.samples_per_channel_ != b.samples_per_channel_)
1463 return ::testing::AssertionFailure()
1464 << "samples_per_channel_ diff (" << a.samples_per_channel_
1465 << " != " << b.samples_per_channel_ << ")";
1466 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +02001467 return ::testing::AssertionFailure()
1468 << "num_channels_ diff (" << a.num_channels_
1469 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001470 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +02001471 return ::testing::AssertionFailure()
1472 << "speech_type_ diff (" << a.speech_type_
1473 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001474 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +02001475 return ::testing::AssertionFailure()
1476 << "vad_activity_ diff (" << a.vad_activity_
1477 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -07001478 return ::testing::AssertionSuccess();
1479}
1480
1481::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1482 const AudioFrame& b) {
1483 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1484 if (!res)
1485 return res;
Yves Gerey665174f2018-06-19 15:03:05 +02001486 if (memcmp(a.data(), b.data(),
1487 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
1488 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001489 return ::testing::AssertionFailure() << "data_ diff";
1490 }
1491 return ::testing::AssertionSuccess();
1492}
1493
1494} // namespace
1495
1496TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1497 ASSERT_FALSE(config_.enable_muted_state);
1498 config2_.enable_muted_state = true;
1499 CreateSecondInstance();
1500
1501 // Insert one speech packet into both NetEqs.
1502 const size_t kSamples = 10 * 16;
1503 const size_t kPayloadBytes = kSamples * 2;
1504 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001505 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001506 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001507 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1508 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001509
1510 AudioFrame out_frame1, out_frame2;
1511 bool muted;
1512 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001513 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001514 ss << "i = " << i;
1515 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1516 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1517 EXPECT_FALSE(muted);
1518 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1519 if (muted) {
1520 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1521 } else {
1522 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1523 }
1524 }
1525 EXPECT_TRUE(muted);
1526
1527 // Insert new data. Timestamp is corrected for the time elapsed since the last
1528 // packet.
1529 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001530 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1531 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001532
1533 int counter = 0;
1534 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1535 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +02001536 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -07001537 ss << "counter = " << counter;
1538 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1539 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1540 EXPECT_FALSE(muted);
1541 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1542 if (muted) {
1543 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1544 } else {
1545 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1546 }
1547 }
1548 EXPECT_FALSE(muted);
1549}
1550
henrik.lundin114c1b32017-04-26 07:47:32 -07001551TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1552 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1553
1554 // Pull out data once.
1555 AudioFrame output;
1556 bool muted;
1557 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1558
1559 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1560}
1561
1562TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1563 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1564 // default). Make the length 10 ms.
1565 constexpr size_t kPayloadSamples = 16 * 10;
1566 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1567 uint8_t payload[kPayloadBytes] = {0};
1568
1569 RTPHeader rtp_info;
1570 constexpr uint32_t kRtpTimestamp = 0x1234;
1571 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1572 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1573
1574 // Pull out data once.
1575 AudioFrame output;
1576 bool muted;
1577 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1578
1579 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1580 neteq_->LastDecodedTimestamps());
1581
1582 // Nothing decoded on the second call.
1583 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1584 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1585}
1586
1587TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1588 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1589 // by default). Make the length 5 ms so that NetEq must decode them both in
1590 // the same GetAudio call.
1591 constexpr size_t kPayloadSamples = 16 * 5;
1592 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1593 uint8_t payload[kPayloadBytes] = {0};
1594
1595 RTPHeader rtp_info;
1596 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1597 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1598 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1599 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1600 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1601 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1602
1603 // Pull out data once.
1604 AudioFrame output;
1605 bool muted;
1606 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1607
1608 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1609 neteq_->LastDecodedTimestamps());
1610}
1611
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001612TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1613 const int kNumConcealmentEvents = 19;
1614 const size_t kSamples = 10 * 16;
1615 const size_t kPayloadBytes = kSamples * 2;
1616 int seq_no = 0;
1617 RTPHeader rtp_info;
1618 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1619 rtp_info.payloadType = 94; // PCM16b WB codec.
1620 rtp_info.markerBit = 0;
1621 const uint8_t payload[kPayloadBytes] = {0};
1622 bool muted;
1623
1624 for (int i = 0; i < kNumConcealmentEvents; i++) {
1625 // Insert some packets of 10 ms size.
1626 for (int j = 0; j < 10; j++) {
1627 rtp_info.sequenceNumber = seq_no++;
1628 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1629 neteq_->InsertPacket(rtp_info, payload, 0);
1630 neteq_->GetAudio(&out_frame_, &muted);
1631 }
1632
1633 // Lose a number of packets.
1634 int num_lost = 1 + i;
1635 for (int j = 0; j < num_lost; j++) {
1636 seq_no++;
1637 neteq_->GetAudio(&out_frame_, &muted);
1638 }
1639 }
1640
1641 // Check number of concealment events.
1642 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1643 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1644}
1645
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001646// Test that the jitter buffer delay stat is computed correctly.
1647void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1648 const int kNumPackets = 10;
1649 const int kDelayInNumPackets = 2;
1650 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1651 const size_t kSamples = kPacketLenMs * 16;
1652 const size_t kPayloadBytes = kSamples * 2;
1653 RTPHeader rtp_info;
1654 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1655 rtp_info.payloadType = 94; // PCM16b WB codec.
1656 rtp_info.markerBit = 0;
1657 const uint8_t payload[kPayloadBytes] = {0};
1658 bool muted;
1659 int packets_sent = 0;
1660 int packets_received = 0;
1661 int expected_delay = 0;
1662 while (packets_received < kNumPackets) {
1663 // Insert packet.
1664 if (packets_sent < kNumPackets) {
1665 rtp_info.sequenceNumber = packets_sent++;
1666 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1667 neteq_->InsertPacket(rtp_info, payload, 0);
1668 }
1669
1670 // Get packet.
1671 if (packets_sent > kDelayInNumPackets) {
1672 neteq_->GetAudio(&out_frame_, &muted);
1673 packets_received++;
1674
1675 // The delay reported by the jitter buffer never exceeds
1676 // the number of samples previously fetched with GetAudio
1677 // (hence the min()).
1678 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1679
1680 // The increase of the expected delay is the product of
1681 // the current delay of the jitter buffer in ms * the
1682 // number of samples that are sent for play out.
1683 int current_delay_ms = packets_delay * kPacketLenMs;
1684 expected_delay += current_delay_ms * kSamples;
1685 }
1686 }
1687
1688 if (apply_packet_loss) {
1689 // Extra call to GetAudio to cause concealment.
1690 neteq_->GetAudio(&out_frame_, &muted);
1691 }
1692
1693 // Check jitter buffer delay.
1694 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1695 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
1696}
1697
1698TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1699 TestJitterBufferDelay(false);
1700}
1701
1702TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1703 TestJitterBufferDelay(true);
1704}
1705
Henrik Lundin7687ad52018-07-02 10:14:46 +02001706namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001707TEST(NetEqNoTimeStretchingMode, RunTest) {
1708 NetEq::Config config;
1709 config.for_test_no_time_stretching = true;
1710 auto codecs = NetEqTest::StandardDecoderMap();
Henrik Lundin7687ad52018-07-02 10:14:46 +02001711 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1712 {1, kRtpExtensionAudioLevel},
1713 {3, kRtpExtensionAbsoluteSendTime},
1714 {5, kRtpExtensionTransportSequenceNumber},
1715 {7, kRtpExtensionVideoContentType},
1716 {8, kRtpExtensionVideoTiming}};
1717 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1718 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001719 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001720 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1721 new TimeLimitedNetEqInput(std::move(input), 20000));
1722 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1723 NetEqTest::Callbacks callbacks;
Niels Möllerbd6dee82019-01-02 09:39:47 +01001724 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, nullptr,
1725 std::move(input_time_limit), std::move(output), callbacks);
Henrik Lundin7687ad52018-07-02 10:14:46 +02001726 test.Run();
1727 const auto stats = test.SimulationStats();
1728 EXPECT_EQ(0, stats.accelerate_rate);
1729 EXPECT_EQ(0, stats.preemptive_rate);
1730}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001731
1732} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001733} // namespace webrtc