blob: 099127e51e93db1b46ce335a0103cb12d732f9cd [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
Ivo Creusen3ce44a32019-10-31 14:38:11 +010011#include "api/neteq/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020023#include "absl/flags/flag.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020024#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
Yves Gerey3a65f392019-11-11 18:05:42 +010027#include "modules/audio_coding/neteq/test/neteq_decoding_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_coding/neteq/tools/audio_loop.h"
Henrik Lundin7687ad52018-07-02 10:14:46 +020029#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
30#include "modules/audio_coding/neteq/tools/neteq_test.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "modules/include/module_common_types_public.h"
Niels Möller53382cb2018-11-27 14:05:08 +010032#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/ignore_wundef.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/message_digest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/string_encode.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"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020044ABSL_FLAG(bool, gen_ref, false, "Generate reference files.");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000045
kwiberg5adaf732016-10-04 09:33:27 -070046namespace webrtc {
47
minyue5f026d02015-12-16 07:36:04 -080048namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
minyue4f906772016-04-29 11:05:14 -070050const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020051 const std::string& checksum_android_32,
52 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070053 const std::string& checksum_win_32,
54 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070055#if defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +020056#ifdef WEBRTC_ARCH_64_BITS
57 return checksum_android_64;
58#else
59 return checksum_android_32;
60#endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070061#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020062#ifdef WEBRTC_ARCH_64_BITS
63 return checksum_win_64;
64#else
65 return checksum_win_32;
66#endif // WEBRTC_ARCH_64_BITS
minyue4f906772016-04-29 11:05:14 -070067#else
68 return checksum_general;
69#endif // WEBRTC_WIN
70}
71
minyue5f026d02015-12-16 07:36:04 -080072} // namespace
73
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074
ivoc72c08ed2016-01-20 07:26:24 -080075#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
76 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +010077 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -080078#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -070079#else
minyue5f026d02015-12-16 07:36:04 -080080#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -070081#endif
minyue5f026d02015-12-16 07:36:04 -080082TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -080083 const std::string input_rtp_file =
84 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +000085
Yves Gerey665174f2018-06-19 15:03:05 +020086 const std::string output_checksum =
Jakob Ivarssond723da12021-01-15 17:44:56 +010087 PlatformChecksum("6c35140ce4d75874bdd60aa1872400b05fd05ca2",
88 "ab451bb8301d9a92fbf4de91556b56f1ea38b4ce", "not used",
89 "6c35140ce4d75874bdd60aa1872400b05fd05ca2",
90 "64b46bb3c1165537a880ae8404afce2efba456c0");
minyue4f906772016-04-29 11:05:14 -070091
henrik.lundin2979f552017-05-05 05:04:16 -070092 const std::string network_stats_checksum =
Jakob Ivarssond723da12021-01-15 17:44:56 +010093 PlatformChecksum("90594d85fa31d3d9584d79293bf7aa4ee55ed751",
94 "77b9c3640b81aff6a38d69d07dd782d39c15321d", "not used",
95 "90594d85fa31d3d9584d79293bf7aa4ee55ed751",
96 "90594d85fa31d3d9584d79293bf7aa4ee55ed751");
minyue4f906772016-04-29 11:05:14 -070097
Yves Gerey665174f2018-06-19 15:03:05 +020098 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020099 absl::GetFlag(FLAGS_gen_ref));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100}
101
Yves Gerey665174f2018-06-19 15:03:05 +0200102#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200103 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800104#define MAYBE_TestOpusBitExactness TestOpusBitExactness
105#else
106#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
107#endif
Ivo Creusen16ddae92020-03-04 17:16:59 +0100108TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800109 const std::string input_rtp_file =
110 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800111
Yves Gereya038e712018-11-14 10:45:50 +0100112 const std::string maybe_sse =
Jakob Ivarssond723da12021-01-15 17:44:56 +0100113 "c7887ff60eecf460332c6c7a28c81561f9e8a40f"
114 "|673dd422cfc174152536d3b13af64f9722520ab5";
Yves Gereya038e712018-11-14 10:45:50 +0100115 const std::string output_checksum = PlatformChecksum(
Jakob Ivarssond723da12021-01-15 17:44:56 +0100116 maybe_sse, "e39283dd61a89cead3786ef8642d2637cc447296",
117 "53d8073eb848b70974cba9e26424f4946508fd19", maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700118
Yves Gerey75e22902019-09-06 03:07:55 +0200119 const std::string network_stats_checksum =
Jakob Ivarssond723da12021-01-15 17:44:56 +0100120 PlatformChecksum("c438bfa3b018f77691279eb9c63730569f54585c",
121 "8a474ed0992591e0c84f593824bb05979c3de157",
122 "9a05378dbf7e6edd56cdeb8ec45bcd6d8589623c",
123 "c438bfa3b018f77691279eb9c63730569f54585c",
124 "c438bfa3b018f77691279eb9c63730569f54585c");
minyue4f906772016-04-29 11:05:14 -0700125
Yves Gerey665174f2018-06-19 15:03:05 +0200126 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200127 absl::GetFlag(FLAGS_gen_ref));
minyue93c08b72015-12-22 09:57:41 -0800128}
129
Jakob Ivarssone7a55812021-03-03 14:18:15 +0100130// TODO(http://bugs.webrtc.org/12518): Enable the test after Opus has been
131// updated.
132TEST_F(NetEqDecodingTest, DISABLED_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100133 const std::string input_rtp_file =
134 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
135
Yves Gereya038e712018-11-14 10:45:50 +0100136 const std::string maybe_sse =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200137 "0fb0a3d6b3758ca6e108368bb777cd38d0a865af"
138 "|79cfb99a21338ba977eb0e15eb8464e2db9436f8";
Yves Gereya038e712018-11-14 10:45:50 +0100139 const std::string output_checksum = PlatformChecksum(
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200140 maybe_sse, "b6632690f8d7c2340c838df2821fc014f1cc8360",
141 "f890b9eb9bc5ab8313489230726b297f6a0825af", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100142
143 const std::string network_stats_checksum =
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200144 "18983bb67a57628c604dbdefa99574c6e0c5bb48";
Henrik Lundine9619f82017-11-27 14:05:27 +0100145
Henrik Lundine9619f82017-11-27 14:05:27 +0100146 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200147 absl::GetFlag(FLAGS_gen_ref));
Henrik Lundine9619f82017-11-27 14:05:27 +0100148}
149
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000150// Use fax mode to avoid time-scaling. This is to simplify the testing of
151// packet waiting times in the packet buffer.
152class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
153 protected:
154 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200155 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000156 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200157 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000158};
159
160TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
162 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000163 const size_t kSamples = 10 * 16;
164 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800166 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700167 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200168 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
169 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700170 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
171 rtp_info.payloadType = 94; // PCM16b WB codec.
172 rtp_info.markerBit = 0;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200173 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175 // Pull out all data.
176 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700177 bool muted;
178 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800179 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 }
181
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200182 NetEqNetworkStatistics stats;
183 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
185 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200186 // each packet. Thus, we are calculating the statistics for a series from 10
187 // to 300, in steps of 10 ms.
188 EXPECT_EQ(155, stats.mean_waiting_time_ms);
189 EXPECT_EQ(155, stats.median_waiting_time_ms);
190 EXPECT_EQ(10, stats.min_waiting_time_ms);
191 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192
193 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200194 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
195 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
196 EXPECT_EQ(-1, stats.median_waiting_time_ms);
197 EXPECT_EQ(-1, stats.min_waiting_time_ms);
198 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199}
200
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000202TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000203 // Apply a clock drift of -25 ms / s (sender faster than receiver).
204 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000205 const double kNetworkFreezeTimeMs = 0.0;
206 const bool kGetAudioDuringFreezeRecovery = false;
207 const int kDelayToleranceMs = 20;
208 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200209 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
210 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000211 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000212}
213
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000214TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000215 // Apply a clock drift of +25 ms / s (sender slower than receiver).
216 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000217 const double kNetworkFreezeTimeMs = 0.0;
218 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200219 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000220 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200221 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
222 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000223 kMaxTimeToSpeechMs);
224}
225
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000226TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000227 // Apply a clock drift of -25 ms / s (sender faster than receiver).
228 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
229 const double kNetworkFreezeTimeMs = 5000.0;
230 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200231 const int kDelayToleranceMs = 60;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000232 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200233 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
234 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000235 kMaxTimeToSpeechMs);
236}
237
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000238TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000239 // Apply a clock drift of +25 ms / s (sender slower than receiver).
240 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
241 const double kNetworkFreezeTimeMs = 5000.0;
242 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200243 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000244 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200245 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
246 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000247 kMaxTimeToSpeechMs);
248}
249
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000250TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000251 // Apply a clock drift of +25 ms / s (sender slower than receiver).
252 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
253 const double kNetworkFreezeTimeMs = 5000.0;
254 const bool kGetAudioDuringFreezeRecovery = true;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200255 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000256 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200257 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
258 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000259 kMaxTimeToSpeechMs);
260}
261
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000262TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000263 const double kDriftFactor = 1.0; // No drift.
264 const double kNetworkFreezeTimeMs = 0.0;
265 const bool kGetAudioDuringFreezeRecovery = false;
266 const int kDelayToleranceMs = 10;
267 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200268 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
269 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000270 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000271}
272
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000273TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000274 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700276 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700278 rtp_info.payloadType = 1; // Not registered as a decoder.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200279 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280}
281
Peter Boströme2976c82016-01-04 22:44:05 +0100282#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800283#define MAYBE_DecoderError DecoderError
284#else
285#define MAYBE_DecoderError DISABLED_DecoderError
286#endif
287
Peter Boströme2976c82016-01-04 22:44:05 +0100288TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000289 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700291 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700293 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200294 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
296 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700297 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800298 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700299 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300 }
henrik.lundin7a926812016-05-12 13:51:28 -0700301 bool muted;
302 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
303 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800304
yujo36b1a5f2017-06-12 12:45:32 -0700305 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700307 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200309 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310 ss << "i = " << i;
311 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700312 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313 }
314}
315
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000316TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
318 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700319 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800320 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700321 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 }
henrik.lundin7a926812016-05-12 13:51:28 -0700323 bool muted;
324 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
325 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 // Verify that the first block of samples is set to 0.
327 static const int kExpectedOutputLength =
328 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700329 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200331 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 ss << "i = " << i;
333 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700334 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 }
henrik.lundind89814b2015-11-23 06:49:25 -0800336 // Verify that the sample rate did not change from the initial configuration.
337 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000339
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000340class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000341 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000342 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700343 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000344 uint8_t payload_type = 0xFF; // Invalid.
345 if (sampling_rate_hz == 8000) {
346 expected_samples_per_channel = kBlockSize8kHz;
347 payload_type = 93; // PCM 16, 8 kHz.
348 } else if (sampling_rate_hz == 16000) {
349 expected_samples_per_channel = kBlockSize16kHz;
350 payload_type = 94; // PCM 16, 16 kHZ.
351 } else if (sampling_rate_hz == 32000) {
352 expected_samples_per_channel = kBlockSize32kHz;
353 payload_type = 95; // PCM 16, 32 kHz.
354 } else {
355 ASSERT_TRUE(false); // Unsupported test case.
356 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000357
henrik.lundin6d8e0112016-03-04 10:34:21 -0800358 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000359 test::AudioLoop input;
360 // We are using the same 32 kHz input file for all tests, regardless of
361 // |sampling_rate_hz|. The output may sound weird, but the test is still
362 // valid.
363 ASSERT_TRUE(input.Init(
364 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
365 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700366 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000367
368 // Payload of 10 ms of PCM16 32 kHz.
369 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700370 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000371 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700372 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000373
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000374 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700375 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000376 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800377 auto block = input.GetNextBlock();
378 ASSERT_EQ(expected_samples_per_channel, block.size());
379 size_t enc_len_bytes =
380 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000381 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
382
Karl Wiberg45eb1352019-10-10 14:23:00 +0200383 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
384 payload, enc_len_bytes)));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800385 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700386 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800387 ASSERT_EQ(1u, output.num_channels_);
388 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800389 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000390
391 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200392 rtp_info.timestamp +=
393 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700394 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200395 receive_timestamp +=
396 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000397 }
398
henrik.lundin6d8e0112016-03-04 10:34:21 -0800399 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000400
401 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
402 // one frame without checking speech-type. This is the first frame pulled
403 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700404 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800405 ASSERT_EQ(1u, output.num_channels_);
406 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000407
408 // To be able to test the fading of background noise we need at lease to
409 // pull 611 frames.
410 const int kFadingThreshold = 611;
411
412 // Test several CNG-to-PLC packet for the expected behavior. The number 20
413 // is arbitrary, but sufficiently large to test enough number of frames.
414 const int kNumPlcToCngTestFrames = 20;
415 bool plc_to_cng = false;
416 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800417 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700418 // Set to non-zero.
419 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700420 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
421 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800422 ASSERT_EQ(1u, output.num_channels_);
423 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800424 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000425 plc_to_cng = true;
426 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700427 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800428 for (size_t k = 0;
429 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -0700430 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +0200431 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000432 } else {
henrik.lundin55480f52016-03-08 02:37:57 -0800433 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000434 }
435 }
436 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
437 }
438};
439
Henrik Lundin67190172018-04-20 15:34:48 +0200440TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000441 CheckBgn(8000);
442 CheckBgn(16000);
443 CheckBgn(32000);
444}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000445
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000446TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
447 // Start with a sequence number that will soon wrap.
448 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
449 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
450}
451
452TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
453 // Start with a sequence number that will soon wrap.
454 std::set<uint16_t> drop_seq_numbers;
455 drop_seq_numbers.insert(0xFFFF);
456 drop_seq_numbers.insert(0x0);
457 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
458}
459
460TEST_F(NetEqDecodingTest, TimestampWrap) {
461 // Start with a timestamp that will soon wrap.
462 std::set<uint16_t> drop_seq_numbers;
463 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
464}
465
466TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
467 // Start with a timestamp and a sequence number that will wrap at the same
468 // time.
469 std::set<uint16_t> drop_seq_numbers;
470 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
471}
472
Yves Gerey3a65f392019-11-11 18:05:42 +0100473TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000474 uint16_t seq_no = 0;
475 uint32_t timestamp = 0;
476 const int kFrameSizeMs = 10;
477 const int kSampleRateKhz = 16;
478 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000479 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000480
Yves Gerey665174f2018-06-19 15:03:05 +0200481 const int algorithmic_delay_samples =
482 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000483 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000484 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000485 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700486 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700487 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000488 for (int i = 0; i < 3; ++i) {
489 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200490 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000491 ++seq_no;
492 timestamp += kSamples;
493
494 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700495 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800496 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000497 }
498 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -0800499 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000500
501 // Insert same CNG packet twice.
502 const int kCngPeriodMs = 100;
503 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000504 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000505 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
506 // This is the first time this CNG packet is inserted.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200507 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
508 payload, payload_len)));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000509
510 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -0700511 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800512 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800513 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700514 EXPECT_FALSE(
515 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -0700516 EXPECT_EQ(timestamp - algorithmic_delay_samples,
517 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000518
519 // Insert the same CNG packet again. Note that at this point it is old, since
520 // we have already decoded the first copy of it.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200521 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
522 payload, payload_len)));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000523
524 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
525 // we have already pulled out CNG once.
526 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -0700527 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800528 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800529 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700530 EXPECT_FALSE(
531 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000532 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700533 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000534 }
535
536 // Insert speech again.
537 ++seq_no;
538 timestamp += kCngPeriodSamples;
539 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200540 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000541
542 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -0700543 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800544 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800545 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200546 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700547 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000548 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700549 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000550}
551
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000552TEST_F(NetEqDecodingTest, CngFirst) {
553 uint16_t seq_no = 0;
554 uint32_t timestamp = 0;
555 const int kFrameSizeMs = 10;
556 const int kSampleRateKhz = 16;
557 const int kSamples = kFrameSizeMs * kSampleRateKhz;
558 const int kPayloadBytes = kSamples * 2;
559 const int kCngPeriodMs = 100;
560 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
561 size_t payload_len;
562
563 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700564 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000565
566 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200567 ASSERT_EQ(NetEq::kOK,
568 neteq_->InsertPacket(
569 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len)));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000570 ++seq_no;
571 timestamp += kCngPeriodSamples;
572
573 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -0700574 bool muted;
575 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800576 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800577 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000578
579 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -0700580 const uint32_t first_speech_timestamp = timestamp;
581 int timeout_counter = 0;
582 do {
583 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000584 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200585 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000586 ++seq_no;
587 timestamp += kSamples;
588
589 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700590 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800591 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -0700592 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000593 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -0800594 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000595}
henrik.lundin7a926812016-05-12 13:51:28 -0700596
597class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
598 public:
599 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
600 config_.enable_muted_state = true;
601 }
602
603 protected:
604 static constexpr size_t kSamples = 10 * 16;
605 static constexpr size_t kPayloadBytes = kSamples * 2;
606
607 void InsertPacket(uint32_t rtp_timestamp) {
608 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700609 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700610 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200611 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin7a926812016-05-12 13:51:28 -0700612 }
613
henrik.lundin42feb512016-09-20 06:51:40 -0700614 void InsertCngPacket(uint32_t rtp_timestamp) {
615 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700616 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -0700617 size_t payload_len;
618 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200619 EXPECT_EQ(NetEq::kOK,
620 neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
621 payload, payload_len)));
henrik.lundin42feb512016-09-20 06:51:40 -0700622 }
623
henrik.lundin7a926812016-05-12 13:51:28 -0700624 bool GetAudioReturnMuted() {
625 bool muted;
626 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
627 return muted;
628 }
629
630 void GetAudioUntilMuted() {
631 while (!GetAudioReturnMuted()) {
632 ASSERT_LT(counter_++, 1000) << "Test timed out";
633 }
634 }
635
636 void GetAudioUntilNormal() {
637 bool muted = false;
638 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
639 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
640 ASSERT_LT(counter_++, 1000) << "Test timed out";
641 }
642 EXPECT_FALSE(muted);
643 }
644
645 int counter_ = 0;
646};
647
648// Verifies that NetEq goes in and out of muted state as expected.
649TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
650 // Insert one speech packet.
651 InsertPacket(0);
652 // Pull out audio once and expect it not to be muted.
653 EXPECT_FALSE(GetAudioReturnMuted());
654 // Pull data until faded out.
655 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -0700656 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -0700657
658 // Verify that output audio is not written during muted mode. Other parameters
659 // should be correct, though.
660 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -0700661 int16_t* frame_data = new_frame.mutable_data();
662 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
663 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -0700664 }
665 bool muted;
666 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
667 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -0700668 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -0700669 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
670 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -0700671 }
672 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
673 new_frame.timestamp_);
674 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
675 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
676 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
677 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
678 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
679
680 // Insert new data. Timestamp is corrected for the time elapsed since the last
681 // packet. Verify that normal operation resumes.
682 InsertPacket(kSamples * counter_);
683 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -0700684 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -0700685
686 NetEqNetworkStatistics stats;
687 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
688 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
689 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
690 // concealment samples in this test.
691 EXPECT_GT(stats.expand_rate, 14000);
692 // And, it should be greater than the speech_expand_rate.
693 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -0700694}
695
696// Verifies that NetEq goes out of muted state when given a delayed packet.
697TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
698 // Insert one speech packet.
699 InsertPacket(0);
700 // Pull out audio once and expect it not to be muted.
701 EXPECT_FALSE(GetAudioReturnMuted());
702 // Pull data until faded out.
703 GetAudioUntilMuted();
704 // Insert new data. Timestamp is only corrected for the half of the time
705 // elapsed since the last packet. That is, the new packet is delayed. Verify
706 // that normal operation resumes.
707 InsertPacket(kSamples * counter_ / 2);
708 GetAudioUntilNormal();
709}
710
711// Verifies that NetEq goes out of muted state when given a future packet.
712TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
713 // Insert one speech packet.
714 InsertPacket(0);
715 // Pull out audio once and expect it not to be muted.
716 EXPECT_FALSE(GetAudioReturnMuted());
717 // Pull data until faded out.
718 GetAudioUntilMuted();
719 // Insert new data. Timestamp is over-corrected for the time elapsed since the
720 // last packet. That is, the new packet is too early. Verify that normal
721 // operation resumes.
722 InsertPacket(kSamples * counter_ * 2);
723 GetAudioUntilNormal();
724}
725
726// Verifies that NetEq goes out of muted state when given an old packet.
727TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
728 // Insert one speech packet.
729 InsertPacket(0);
730 // Pull out audio once and expect it not to be muted.
731 EXPECT_FALSE(GetAudioReturnMuted());
732 // Pull data until faded out.
733 GetAudioUntilMuted();
734
735 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200736 // Insert a few packets which are older than the first packet.
737 for (int i = 0; i < 5; ++i) {
738 InsertPacket(kSamples * (i - 1000));
739 }
henrik.lundin7a926812016-05-12 13:51:28 -0700740 EXPECT_FALSE(GetAudioReturnMuted());
741 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
742}
743
henrik.lundin42feb512016-09-20 06:51:40 -0700744// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
745// packet stream is suspended for a long time.
746TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
747 // Insert one CNG packet.
748 InsertCngPacket(0);
749
750 // Pull 10 seconds of audio (10 ms audio generated per lap).
751 for (int i = 0; i < 1000; ++i) {
752 bool muted;
753 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
754 ASSERT_FALSE(muted);
755 }
756 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
757}
758
759// Verifies that NetEq goes back to normal after a long CNG period with the
760// packet stream suspended.
761TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
762 // Insert one CNG packet.
763 InsertCngPacket(0);
764
765 // Pull 10 seconds of audio (10 ms audio generated per lap).
766 for (int i = 0; i < 1000; ++i) {
767 bool muted;
768 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
769 }
770
771 // Insert new data. Timestamp is corrected for the time elapsed since the last
772 // packet. Verify that normal operation resumes.
773 InsertPacket(kSamples * counter_);
774 GetAudioUntilNormal();
775}
776
henrik.lundin7a926812016-05-12 13:51:28 -0700777namespace {
778::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
779 const AudioFrame& b) {
780 if (a.timestamp_ != b.timestamp_)
781 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
782 << " != " << b.timestamp_ << ")";
783 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +0200784 return ::testing::AssertionFailure()
785 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
786 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700787 if (a.samples_per_channel_ != b.samples_per_channel_)
788 return ::testing::AssertionFailure()
789 << "samples_per_channel_ diff (" << a.samples_per_channel_
790 << " != " << b.samples_per_channel_ << ")";
791 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +0200792 return ::testing::AssertionFailure()
793 << "num_channels_ diff (" << a.num_channels_
794 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700795 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +0200796 return ::testing::AssertionFailure()
797 << "speech_type_ diff (" << a.speech_type_
798 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700799 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +0200800 return ::testing::AssertionFailure()
801 << "vad_activity_ diff (" << a.vad_activity_
802 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700803 return ::testing::AssertionSuccess();
804}
805
806::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
807 const AudioFrame& b) {
808 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
809 if (!res)
810 return res;
Yves Gerey665174f2018-06-19 15:03:05 +0200811 if (memcmp(a.data(), b.data(),
812 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
813 0) {
henrik.lundin7a926812016-05-12 13:51:28 -0700814 return ::testing::AssertionFailure() << "data_ diff";
815 }
816 return ::testing::AssertionSuccess();
817}
818
819} // namespace
820
821TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
822 ASSERT_FALSE(config_.enable_muted_state);
823 config2_.enable_muted_state = true;
824 CreateSecondInstance();
825
826 // Insert one speech packet into both NetEqs.
827 const size_t kSamples = 10 * 16;
828 const size_t kPayloadBytes = kSamples * 2;
829 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700830 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700831 PopulateRtpInfo(0, 0, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200832 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
833 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
henrik.lundin7a926812016-05-12 13:51:28 -0700834
835 AudioFrame out_frame1, out_frame2;
836 bool muted;
837 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200838 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -0700839 ss << "i = " << i;
840 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
841 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
842 EXPECT_FALSE(muted);
843 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
844 if (muted) {
845 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
846 } else {
847 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
848 }
849 }
850 EXPECT_TRUE(muted);
851
852 // Insert new data. Timestamp is corrected for the time elapsed since the last
853 // packet.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200854 for (int i = 0; i < 5; ++i) {
855 PopulateRtpInfo(0, kSamples * 1000 + kSamples * i, &rtp_info);
856 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
857 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
858 }
henrik.lundin7a926812016-05-12 13:51:28 -0700859
860 int counter = 0;
861 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
862 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +0200863 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -0700864 ss << "counter = " << counter;
865 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
866 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
867 EXPECT_FALSE(muted);
868 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
869 if (muted) {
870 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
871 } else {
872 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
873 }
874 }
875 EXPECT_FALSE(muted);
876}
877
henrik.lundin114c1b32017-04-26 07:47:32 -0700878TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
879 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
880
881 // Pull out data once.
882 AudioFrame output;
883 bool muted;
884 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
885
886 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
887}
888
889TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
890 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
891 // default). Make the length 10 ms.
892 constexpr size_t kPayloadSamples = 16 * 10;
893 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
894 uint8_t payload[kPayloadBytes] = {0};
895
896 RTPHeader rtp_info;
897 constexpr uint32_t kRtpTimestamp = 0x1234;
898 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200899 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700900
901 // Pull out data once.
902 AudioFrame output;
903 bool muted;
904 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
905
906 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
907 neteq_->LastDecodedTimestamps());
908
909 // Nothing decoded on the second call.
910 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
911 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
912}
913
914TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
915 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
916 // by default). Make the length 5 ms so that NetEq must decode them both in
917 // the same GetAudio call.
918 constexpr size_t kPayloadSamples = 16 * 5;
919 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
920 uint8_t payload[kPayloadBytes] = {0};
921
922 RTPHeader rtp_info;
923 constexpr uint32_t kRtpTimestamp1 = 0x1234;
924 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200925 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700926 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
927 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200928 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700929
930 // Pull out data once.
931 AudioFrame output;
932 bool muted;
933 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
934
935 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
936 neteq_->LastDecodedTimestamps());
937}
938
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200939TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
940 const int kNumConcealmentEvents = 19;
941 const size_t kSamples = 10 * 16;
942 const size_t kPayloadBytes = kSamples * 2;
943 int seq_no = 0;
944 RTPHeader rtp_info;
945 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
946 rtp_info.payloadType = 94; // PCM16b WB codec.
947 rtp_info.markerBit = 0;
948 const uint8_t payload[kPayloadBytes] = {0};
949 bool muted;
950
951 for (int i = 0; i < kNumConcealmentEvents; i++) {
952 // Insert some packets of 10 ms size.
953 for (int j = 0; j < 10; j++) {
954 rtp_info.sequenceNumber = seq_no++;
955 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200956 neteq_->InsertPacket(rtp_info, payload);
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200957 neteq_->GetAudio(&out_frame_, &muted);
958 }
959
960 // Lose a number of packets.
961 int num_lost = 1 + i;
962 for (int j = 0; j < num_lost; j++) {
963 seq_no++;
964 neteq_->GetAudio(&out_frame_, &muted);
965 }
966 }
967
968 // Check number of concealment events.
969 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
970 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
971}
972
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200973// Test that the jitter buffer delay stat is computed correctly.
974void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
975 const int kNumPackets = 10;
976 const int kDelayInNumPackets = 2;
977 const int kPacketLenMs = 10; // All packets are of 10 ms size.
978 const size_t kSamples = kPacketLenMs * 16;
979 const size_t kPayloadBytes = kSamples * 2;
980 RTPHeader rtp_info;
981 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
982 rtp_info.payloadType = 94; // PCM16b WB codec.
983 rtp_info.markerBit = 0;
984 const uint8_t payload[kPayloadBytes] = {0};
985 bool muted;
986 int packets_sent = 0;
987 int packets_received = 0;
988 int expected_delay = 0;
Artem Titove618cc92020-03-11 11:18:54 +0100989 int expected_target_delay = 0;
Chen Xing0acffb52019-01-15 15:46:29 +0100990 uint64_t expected_emitted_count = 0;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200991 while (packets_received < kNumPackets) {
992 // Insert packet.
993 if (packets_sent < kNumPackets) {
994 rtp_info.sequenceNumber = packets_sent++;
995 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200996 neteq_->InsertPacket(rtp_info, payload);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200997 }
998
999 // Get packet.
1000 if (packets_sent > kDelayInNumPackets) {
1001 neteq_->GetAudio(&out_frame_, &muted);
1002 packets_received++;
1003
1004 // The delay reported by the jitter buffer never exceeds
1005 // the number of samples previously fetched with GetAudio
1006 // (hence the min()).
1007 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1008
1009 // The increase of the expected delay is the product of
1010 // the current delay of the jitter buffer in ms * the
1011 // number of samples that are sent for play out.
1012 int current_delay_ms = packets_delay * kPacketLenMs;
1013 expected_delay += current_delay_ms * kSamples;
Artem Titove618cc92020-03-11 11:18:54 +01001014 expected_target_delay += neteq_->TargetDelayMs() * kSamples;
Chen Xing0acffb52019-01-15 15:46:29 +01001015 expected_emitted_count += kSamples;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001016 }
1017 }
1018
1019 if (apply_packet_loss) {
1020 // Extra call to GetAudio to cause concealment.
1021 neteq_->GetAudio(&out_frame_, &muted);
1022 }
1023
1024 // Check jitter buffer delay.
1025 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
Artem Titove618cc92020-03-11 11:18:54 +01001026 EXPECT_EQ(expected_delay,
1027 rtc::checked_cast<int>(stats.jitter_buffer_delay_ms));
Chen Xing0acffb52019-01-15 15:46:29 +01001028 EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count);
Artem Titove618cc92020-03-11 11:18:54 +01001029 EXPECT_EQ(expected_target_delay,
1030 rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms));
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001031}
1032
1033TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1034 TestJitterBufferDelay(false);
1035}
1036
1037TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1038 TestJitterBufferDelay(true);
1039}
1040
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001041TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithAcceleration) {
1042 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1043 const size_t kSamples = kPacketLenMs * 16;
1044 const size_t kPayloadBytes = kSamples * 2;
1045 RTPHeader rtp_info;
1046 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1047 rtp_info.payloadType = 94; // PCM16b WB codec.
1048 rtp_info.markerBit = 0;
1049 const uint8_t payload[kPayloadBytes] = {0};
1050
Artem Titove618cc92020-03-11 11:18:54 +01001051 int expected_target_delay = neteq_->TargetDelayMs() * kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001052 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001053
1054 bool muted;
1055 neteq_->GetAudio(&out_frame_, &muted);
1056
1057 rtp_info.sequenceNumber += 1;
1058 rtp_info.timestamp += kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001059 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001060 rtp_info.sequenceNumber += 1;
1061 rtp_info.timestamp += kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001062 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001063
Artem Titove618cc92020-03-11 11:18:54 +01001064 expected_target_delay += neteq_->TargetDelayMs() * 2 * kSamples;
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001065 // We have two packets in the buffer and kAccelerate operation will
1066 // extract 20 ms of data.
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001067 neteq_->GetAudio(&out_frame_, &muted, NetEq::Operation::kAccelerate);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001068
1069 // Check jitter buffer delay.
1070 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1071 EXPECT_EQ(10 * kSamples * 3, stats.jitter_buffer_delay_ms);
1072 EXPECT_EQ(kSamples * 3, stats.jitter_buffer_emitted_count);
Artem Titove618cc92020-03-11 11:18:54 +01001073 EXPECT_EQ(expected_target_delay,
1074 rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms));
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001075}
1076
Henrik Lundin7687ad52018-07-02 10:14:46 +02001077namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001078TEST(NetEqNoTimeStretchingMode, RunTest) {
1079 NetEq::Config config;
1080 config.for_test_no_time_stretching = true;
1081 auto codecs = NetEqTest::StandardDecoderMap();
Henrik Lundin7687ad52018-07-02 10:14:46 +02001082 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1083 {1, kRtpExtensionAudioLevel},
1084 {3, kRtpExtensionAbsoluteSendTime},
1085 {5, kRtpExtensionTransportSequenceNumber},
1086 {7, kRtpExtensionVideoContentType},
1087 {8, kRtpExtensionVideoTiming}};
1088 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1089 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001090 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001091 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1092 new TimeLimitedNetEqInput(std::move(input), 20000));
1093 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1094 NetEqTest::Callbacks callbacks;
Ivo Creusencee751a2020-01-16 17:17:09 +01001095 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs,
1096 /*text_log=*/nullptr, /*neteq_factory=*/nullptr,
1097 /*input=*/std::move(input_time_limit), std::move(output),
1098 callbacks);
Henrik Lundin7687ad52018-07-02 10:14:46 +02001099 test.Run();
1100 const auto stats = test.SimulationStats();
1101 EXPECT_EQ(0, stats.accelerate_rate);
1102 EXPECT_EQ(0, stats.preemptive_rate);
1103}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001104
Henrik Lundinc49e9c22020-05-25 11:26:15 +02001105namespace {
1106// Helper classes and data types and functions for NetEqOutputDelayTest.
1107
1108class VectorAudioSink : public AudioSink {
1109 public:
1110 // Does not take ownership of the vector.
1111 VectorAudioSink(std::vector<int16_t>* output_vector) : v_(output_vector) {}
1112
1113 virtual ~VectorAudioSink() = default;
1114
1115 bool WriteArray(const int16_t* audio, size_t num_samples) override {
1116 v_->reserve(v_->size() + num_samples);
1117 for (size_t i = 0; i < num_samples; ++i) {
1118 v_->push_back(audio[i]);
1119 }
1120 return true;
1121 }
1122
1123 private:
1124 std::vector<int16_t>* const v_;
1125};
1126
1127struct TestResult {
1128 NetEqLifetimeStatistics lifetime_stats;
1129 NetEqNetworkStatistics network_stats;
1130 absl::optional<uint32_t> playout_timestamp;
1131 int target_delay_ms;
1132 int filtered_current_delay_ms;
1133 int sample_rate_hz;
1134};
1135
1136// This class is used as callback object to NetEqTest to collect some stats
1137// at the end of the simulation.
1138class SimEndStatsCollector : public NetEqSimulationEndedCallback {
1139 public:
1140 SimEndStatsCollector(TestResult& result) : result_(result) {}
1141
1142 void SimulationEnded(int64_t /*simulation_time_ms*/, NetEq* neteq) override {
1143 result_.playout_timestamp = neteq->GetPlayoutTimestamp();
1144 result_.target_delay_ms = neteq->TargetDelayMs();
1145 result_.filtered_current_delay_ms = neteq->FilteredCurrentDelayMs();
1146 result_.sample_rate_hz = neteq->last_output_sample_rate_hz();
1147 }
1148
1149 private:
1150 TestResult& result_;
1151};
1152
1153TestResult DelayLineNetEqTest(int delay_ms,
1154 std::vector<int16_t>* output_vector) {
1155 NetEq::Config config;
1156 config.for_test_no_time_stretching = true;
1157 config.extra_output_delay_ms = delay_ms;
1158 auto codecs = NetEqTest::StandardDecoderMap();
1159 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1160 {1, kRtpExtensionAudioLevel},
1161 {3, kRtpExtensionAbsoluteSendTime},
1162 {5, kRtpExtensionTransportSequenceNumber},
1163 {7, kRtpExtensionVideoContentType},
1164 {8, kRtpExtensionVideoTiming}};
1165 std::unique_ptr<NetEqInput> input = std::make_unique<NetEqRtpDumpInput>(
1166 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
1167 rtp_ext_map, absl::nullopt /*No SSRC filter*/);
1168 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1169 new TimeLimitedNetEqInput(std::move(input), 10000));
1170 std::unique_ptr<AudioSink> output =
1171 std::make_unique<VectorAudioSink>(output_vector);
1172
1173 TestResult result;
1174 SimEndStatsCollector stats_collector(result);
1175 NetEqTest::Callbacks callbacks;
1176 callbacks.simulation_ended_callback = &stats_collector;
1177
1178 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs,
1179 /*text_log=*/nullptr, /*neteq_factory=*/nullptr,
1180 /*input=*/std::move(input_time_limit), std::move(output),
1181 callbacks);
1182 test.Run();
1183 result.lifetime_stats = test.LifetimeStats();
1184 result.network_stats = test.SimulationStats();
1185 return result;
1186}
1187} // namespace
1188
1189// Tests the extra output delay functionality of NetEq.
1190TEST(NetEqOutputDelayTest, RunTest) {
1191 std::vector<int16_t> output;
1192 const auto result_no_delay = DelayLineNetEqTest(0, &output);
1193 std::vector<int16_t> output_delayed;
1194 constexpr int kDelayMs = 100;
1195 const auto result_delay = DelayLineNetEqTest(kDelayMs, &output_delayed);
1196
1197 // Verify that the loss concealment remains unchanged. The point of the delay
1198 // is to not affect the jitter buffering behavior.
1199 // First verify that there are concealments in the test.
1200 EXPECT_GT(result_no_delay.lifetime_stats.concealed_samples, 0u);
1201 // And that not all of the output is concealment.
1202 EXPECT_GT(result_no_delay.lifetime_stats.total_samples_received,
1203 result_no_delay.lifetime_stats.concealed_samples);
1204 // Now verify that they remain unchanged by the delay.
1205 EXPECT_EQ(result_no_delay.lifetime_stats.concealed_samples,
1206 result_delay.lifetime_stats.concealed_samples);
1207 // Accelerate and pre-emptive expand should also be unchanged.
1208 EXPECT_EQ(result_no_delay.lifetime_stats.inserted_samples_for_deceleration,
1209 result_delay.lifetime_stats.inserted_samples_for_deceleration);
1210 EXPECT_EQ(result_no_delay.lifetime_stats.removed_samples_for_acceleration,
1211 result_delay.lifetime_stats.removed_samples_for_acceleration);
1212 // Verify that delay stats are increased with the delay chain.
1213 EXPECT_EQ(
1214 result_no_delay.lifetime_stats.jitter_buffer_delay_ms +
1215 kDelayMs * result_no_delay.lifetime_stats.jitter_buffer_emitted_count,
1216 result_delay.lifetime_stats.jitter_buffer_delay_ms);
1217 EXPECT_EQ(
1218 result_no_delay.lifetime_stats.jitter_buffer_target_delay_ms +
1219 kDelayMs * result_no_delay.lifetime_stats.jitter_buffer_emitted_count,
1220 result_delay.lifetime_stats.jitter_buffer_target_delay_ms);
1221 EXPECT_EQ(result_no_delay.network_stats.current_buffer_size_ms + kDelayMs,
1222 result_delay.network_stats.current_buffer_size_ms);
1223 EXPECT_EQ(result_no_delay.network_stats.preferred_buffer_size_ms + kDelayMs,
1224 result_delay.network_stats.preferred_buffer_size_ms);
1225 EXPECT_EQ(result_no_delay.network_stats.mean_waiting_time_ms + kDelayMs,
1226 result_delay.network_stats.mean_waiting_time_ms);
1227 EXPECT_EQ(result_no_delay.network_stats.median_waiting_time_ms + kDelayMs,
1228 result_delay.network_stats.median_waiting_time_ms);
1229 EXPECT_EQ(result_no_delay.network_stats.min_waiting_time_ms + kDelayMs,
1230 result_delay.network_stats.min_waiting_time_ms);
1231 EXPECT_EQ(result_no_delay.network_stats.max_waiting_time_ms + kDelayMs,
1232 result_delay.network_stats.max_waiting_time_ms);
1233
1234 ASSERT_TRUE(result_no_delay.playout_timestamp);
1235 ASSERT_TRUE(result_delay.playout_timestamp);
1236 EXPECT_EQ(*result_no_delay.playout_timestamp -
1237 static_cast<uint32_t>(
1238 kDelayMs *
1239 rtc::CheckedDivExact(result_no_delay.sample_rate_hz, 1000)),
1240 *result_delay.playout_timestamp);
1241 EXPECT_EQ(result_no_delay.target_delay_ms + kDelayMs,
1242 result_delay.target_delay_ms);
1243 EXPECT_EQ(result_no_delay.filtered_current_delay_ms + kDelayMs,
1244 result_delay.filtered_current_delay_ms);
1245
1246 // Verify expected delay in decoded signal. The test vector uses 8 kHz sample
1247 // rate, so the delay will be 8 times the delay in ms.
1248 constexpr size_t kExpectedDelaySamples = kDelayMs * 8;
1249 for (size_t i = 0;
1250 i < output.size() && i + kExpectedDelaySamples < output_delayed.size();
1251 ++i) {
1252 EXPECT_EQ(output[i], output_delayed[i + kExpectedDelaySamples]);
1253 }
1254}
1255
Henrik Lundinf7cba9f2020-06-10 18:19:27 +02001256// Tests the extra output delay functionality of NetEq when configured via
1257// field trial.
1258TEST(NetEqOutputDelayTest, RunTestWithFieldTrial) {
1259 test::ScopedFieldTrials field_trial(
1260 "WebRTC-Audio-NetEqExtraDelay/Enabled-50/");
1261 constexpr int kExpectedDelayMs = 50;
1262 std::vector<int16_t> output;
1263 const auto result = DelayLineNetEqTest(0, &output);
1264
1265 // The base delay values are taken from the resuts of the non-delayed case in
1266 // NetEqOutputDelayTest.RunTest above.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +02001267 EXPECT_EQ(20 + kExpectedDelayMs, result.target_delay_ms);
Henrik Lundinf7cba9f2020-06-10 18:19:27 +02001268 EXPECT_EQ(24 + kExpectedDelayMs, result.filtered_current_delay_ms);
1269}
1270
1271// Set a non-multiple-of-10 value in the field trial, and verify that we don't
1272// crash, and that the result is rounded down.
1273TEST(NetEqOutputDelayTest, RunTestWithFieldTrialOddValue) {
1274 test::ScopedFieldTrials field_trial(
1275 "WebRTC-Audio-NetEqExtraDelay/Enabled-103/");
1276 constexpr int kRoundedDelayMs = 100;
1277 std::vector<int16_t> output;
1278 const auto result = DelayLineNetEqTest(0, &output);
1279
1280 // The base delay values are taken from the resuts of the non-delayed case in
1281 // NetEqOutputDelayTest.RunTest above.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +02001282 EXPECT_EQ(20 + kRoundedDelayMs, result.target_delay_ms);
Henrik Lundinf7cba9f2020-06-10 18:19:27 +02001283 EXPECT_EQ(24 + kRoundedDelayMs, result.filtered_current_delay_ms);
1284}
1285
Henrik Lundin7687ad52018-07-02 10:14:46 +02001286} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001287} // namespace webrtc