blob: a96812c3d429d9de04f90fdf95627baec6a49b8f [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"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010026#include "api/test/neteq_factory_with_codecs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
Yves Gerey3a65f392019-11-11 18:05:42 +010028#include "modules/audio_coding/neteq/test/neteq_decoding_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_coding/neteq/tools/audio_loop.h"
Henrik Lundin7687ad52018-07-02 10:14:46 +020030#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
31#include "modules/audio_coding/neteq/tools/neteq_test.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "modules/include/module_common_types_public.h"
Niels Möller53382cb2018-11-27 14:05:08 +010033#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010034#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/ignore_wundef.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/message_digest.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010037#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020039#include "rtc_base/strings/string_builder.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020040#include "rtc_base/system/arch.h"
Henrik Lundine9619f82017-11-27 14:05:27 +010041#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020045ABSL_FLAG(bool, gen_ref, false, "Generate reference files.");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000046
kwiberg5adaf732016-10-04 09:33:27 -070047namespace webrtc {
48
minyue5f026d02015-12-16 07:36:04 -080049namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
minyue4f906772016-04-29 11:05:14 -070051const std::string& PlatformChecksum(const std::string& checksum_general,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020052 const std::string& checksum_android_32,
53 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070054 const std::string& checksum_win_32,
55 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070056#if defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +020057#ifdef WEBRTC_ARCH_64_BITS
58 return checksum_android_64;
59#else
60 return checksum_android_32;
61#endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070062#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020063#ifdef WEBRTC_ARCH_64_BITS
64 return checksum_win_64;
65#else
66 return checksum_win_32;
67#endif // WEBRTC_ARCH_64_BITS
minyue4f906772016-04-29 11:05:14 -070068#else
69 return checksum_general;
70#endif // WEBRTC_WIN
71}
72
minyue5f026d02015-12-16 07:36:04 -080073} // namespace
74
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075
ivoc72c08ed2016-01-20 07:26:24 -080076#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
77 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
Karl Wibergeb254b42017-11-01 15:08:12 +010078 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -080079#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -070080#else
minyue5f026d02015-12-16 07:36:04 -080081#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -070082#endif
minyue5f026d02015-12-16 07:36:04 -080083TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -080084 const std::string input_rtp_file =
85 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +000086
Yves Gerey665174f2018-06-19 15:03:05 +020087 const std::string output_checksum =
Jakob Ivarsson507f4342019-09-03 13:04:41 +020088 PlatformChecksum("6ae9f643dc3e5f3452d28a772eef7e00e74158bc",
89 "f4374430e870d66268c1b8e22fb700eb072d567e", "not used",
90 "6ae9f643dc3e5f3452d28a772eef7e00e74158bc",
91 "8d73c98645917cdeaaa01c20cf095ccc5a10b2b5");
minyue4f906772016-04-29 11:05:14 -070092
henrik.lundin2979f552017-05-05 05:04:16 -070093 const std::string network_stats_checksum =
Jakob Ivarsson507f4342019-09-03 13:04:41 +020094 PlatformChecksum("3d186ea7e243abfdbd3d39b8ebf8f02a318117e4",
95 "0b725774133da5dd823f2046663c12a76e0dbd79", "not used",
96 "3d186ea7e243abfdbd3d39b8ebf8f02a318117e4",
97 "3d186ea7e243abfdbd3d39b8ebf8f02a318117e4");
minyue4f906772016-04-29 11:05:14 -070098
Yves Gerey665174f2018-06-19 15:03:05 +020099 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200100 absl::GetFlag(FLAGS_gen_ref));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101}
102
Yves Gerey665174f2018-06-19 15:03:05 +0200103#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200104 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800105#define MAYBE_TestOpusBitExactness TestOpusBitExactness
106#else
107#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
108#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200109TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800110 const std::string input_rtp_file =
111 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800112
Yves Gereya038e712018-11-14 10:45:50 +0100113 // Checksum depends on libopus being compiled with or without SSE.
114 const std::string maybe_sse =
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200115 "6b602683ca7285a98118b4824d72f4257952c18f|"
116 "eb0b68bddcac00fc85403df64f83126f8ea9bc93";
Yves Gereya038e712018-11-14 10:45:50 +0100117 const std::string output_checksum = PlatformChecksum(
Yves Gerey75e22902019-09-06 03:07:55 +0200118 maybe_sse, "f95f2a220c9ca5d60b81c4653d46e0de2bee159f",
119 "6f288a03d34958f62496f18fa85655593eef4dbe", maybe_sse, maybe_sse);
minyue4f906772016-04-29 11:05:14 -0700120
Yves Gerey75e22902019-09-06 03:07:55 +0200121 const std::string network_stats_checksum =
122 PlatformChecksum("87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544",
123 "6b8c29e39c82f5479f59726744d0cf3e88e725d3",
124 "c876f2a04c4f0a91da7f084f80e87871b7c5a4a1",
125 "87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544",
126 "87d2d3e5ca7f1b3fb7a501ffaa51ae29aea74544");
minyue4f906772016-04-29 11:05:14 -0700127
Yves Gerey665174f2018-06-19 15:03:05 +0200128 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200129 absl::GetFlag(FLAGS_gen_ref));
minyue93c08b72015-12-22 09:57:41 -0800130}
131
Yves Gerey665174f2018-06-19 15:03:05 +0200132#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
Henrik Lundine9619f82017-11-27 14:05:27 +0100133 defined(WEBRTC_CODEC_OPUS)
134#define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
135#else
136#define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
137#endif
Henrik Lundin4f2a4a12018-01-26 17:32:56 +0100138TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
Henrik Lundine9619f82017-11-27 14:05:27 +0100139 const std::string input_rtp_file =
140 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
141
Yves Gereya038e712018-11-14 10:45:50 +0100142 const std::string maybe_sse =
Minyue Li8e83c7a2019-11-04 14:47:52 +0100143 "0bdeb4ccf95a2577e38274360903ad099fc46787|"
144 "f7bbf5d92a0595a2a3445ffbaddfb20e98b6e94e";
Yves Gereya038e712018-11-14 10:45:50 +0100145 const std::string output_checksum = PlatformChecksum(
Minyue Li8e83c7a2019-11-04 14:47:52 +0100146 maybe_sse, "6d200cc51a001b6137abf67db2bb8eeb0375cdee",
147 "36d43761de86b12520cf2e63f97372a2b7c6f939", maybe_sse, maybe_sse);
Henrik Lundine9619f82017-11-27 14:05:27 +0100148
149 const std::string network_stats_checksum =
Jakob Ivarsson65024d92019-08-30 15:37:07 +0200150 "8caf49765f35b6862066d3f17531ce44d8e25f60";
Henrik Lundine9619f82017-11-27 14:05:27 +0100151
Henrik Lundine9619f82017-11-27 14:05:27 +0100152 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200153 absl::GetFlag(FLAGS_gen_ref));
Henrik Lundine9619f82017-11-27 14:05:27 +0100154}
155
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000156// Use fax mode to avoid time-scaling. This is to simplify the testing of
157// packet waiting times in the packet buffer.
158class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
159 protected:
160 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200161 config_.for_test_no_time_stretching = true;
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000162 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200163 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000164};
165
166TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
168 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000169 const size_t kSamples = 10 * 16;
170 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800172 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700173 RTPHeader rtp_info;
Mirko Bonadeia8110272017-10-18 14:22:50 +0200174 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
175 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700176 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
177 rtp_info.payloadType = 94; // PCM16b WB codec.
178 rtp_info.markerBit = 0;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200179 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 }
181 // Pull out all data.
182 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700183 bool muted;
184 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800185 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 }
187
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200188 NetEqNetworkStatistics stats;
189 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
191 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200192 // each packet. Thus, we are calculating the statistics for a series from 10
193 // to 300, in steps of 10 ms.
194 EXPECT_EQ(155, stats.mean_waiting_time_ms);
195 EXPECT_EQ(155, stats.median_waiting_time_ms);
196 EXPECT_EQ(10, stats.min_waiting_time_ms);
197 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198
199 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200200 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
201 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
202 EXPECT_EQ(-1, stats.median_waiting_time_ms);
203 EXPECT_EQ(-1, stats.min_waiting_time_ms);
204 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205}
206
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000208TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000209 // Apply a clock drift of -25 ms / s (sender faster than receiver).
210 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000211 const double kNetworkFreezeTimeMs = 0.0;
212 const bool kGetAudioDuringFreezeRecovery = false;
213 const int kDelayToleranceMs = 20;
214 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200215 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
216 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000217 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000218}
219
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000220TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000221 // Apply a clock drift of +25 ms / s (sender slower than receiver).
222 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000223 const double kNetworkFreezeTimeMs = 0.0;
224 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200225 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000226 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200227 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
228 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000229 kMaxTimeToSpeechMs);
230}
231
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000232TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000233 // Apply a clock drift of -25 ms / s (sender faster than receiver).
234 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
235 const double kNetworkFreezeTimeMs = 5000.0;
236 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarssona36c5912019-06-27 10:12:02 +0200237 const int kDelayToleranceMs = 60;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000238 const int kMaxTimeToSpeechMs = 200;
Yves Gerey665174f2018-06-19 15:03:05 +0200239 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
240 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000241 kMaxTimeToSpeechMs);
242}
243
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000244TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000245 // Apply a clock drift of +25 ms / s (sender slower than receiver).
246 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
247 const double kNetworkFreezeTimeMs = 5000.0;
248 const bool kGetAudioDuringFreezeRecovery = false;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200249 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000250 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200251 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
252 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000253 kMaxTimeToSpeechMs);
254}
255
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000256TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000257 // Apply a clock drift of +25 ms / s (sender slower than receiver).
258 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
259 const double kNetworkFreezeTimeMs = 5000.0;
260 const bool kGetAudioDuringFreezeRecovery = true;
Jakob Ivarsson507f4342019-09-03 13:04:41 +0200261 const int kDelayToleranceMs = 40;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000262 const int kMaxTimeToSpeechMs = 100;
Yves Gerey665174f2018-06-19 15:03:05 +0200263 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
264 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000265 kMaxTimeToSpeechMs);
266}
267
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000268TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000269 const double kDriftFactor = 1.0; // No drift.
270 const double kNetworkFreezeTimeMs = 0.0;
271 const bool kGetAudioDuringFreezeRecovery = false;
272 const int kDelayToleranceMs = 10;
273 const int kMaxTimeToSpeechMs = 50;
Yves Gerey665174f2018-06-19 15:03:05 +0200274 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
275 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000276 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000277}
278
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000279TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000280 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700282 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700284 rtp_info.payloadType = 1; // Not registered as a decoder.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200285 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286}
287
Peter Boströme2976c82016-01-04 22:44:05 +0100288#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800289#define MAYBE_DecoderError DecoderError
290#else
291#define MAYBE_DecoderError DISABLED_DecoderError
292#endif
293
Peter Boströme2976c82016-01-04 22:44:05 +0100294TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000295 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700297 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700299 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200300 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
302 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700303 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800304 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700305 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306 }
henrik.lundin7a926812016-05-12 13:51:28 -0700307 bool muted;
308 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
309 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800310
yujo36b1a5f2017-06-12 12:45:32 -0700311 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700313 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200315 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000316 ss << "i = " << i;
317 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700318 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319 }
320}
321
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000322TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
324 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700325 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800326 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700327 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328 }
henrik.lundin7a926812016-05-12 13:51:28 -0700329 bool muted;
330 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
331 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 // Verify that the first block of samples is set to 0.
333 static const int kExpectedOutputLength =
334 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700335 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 for (int i = 0; i < kExpectedOutputLength; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200337 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338 ss << "i = " << i;
339 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700340 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 }
henrik.lundind89814b2015-11-23 06:49:25 -0800342 // Verify that the sample rate did not change from the initial configuration.
343 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000345
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000346class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000347 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000348 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700349 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000350 uint8_t payload_type = 0xFF; // Invalid.
351 if (sampling_rate_hz == 8000) {
352 expected_samples_per_channel = kBlockSize8kHz;
353 payload_type = 93; // PCM 16, 8 kHz.
354 } else if (sampling_rate_hz == 16000) {
355 expected_samples_per_channel = kBlockSize16kHz;
356 payload_type = 94; // PCM 16, 16 kHZ.
357 } else if (sampling_rate_hz == 32000) {
358 expected_samples_per_channel = kBlockSize32kHz;
359 payload_type = 95; // PCM 16, 32 kHz.
360 } else {
361 ASSERT_TRUE(false); // Unsupported test case.
362 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000363
henrik.lundin6d8e0112016-03-04 10:34:21 -0800364 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000365 test::AudioLoop input;
366 // We are using the same 32 kHz input file for all tests, regardless of
367 // |sampling_rate_hz|. The output may sound weird, but the test is still
368 // valid.
369 ASSERT_TRUE(input.Init(
370 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
371 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700372 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000373
374 // Payload of 10 ms of PCM16 32 kHz.
375 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700376 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000377 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700378 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000379
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000380 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700381 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000382 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800383 auto block = input.GetNextBlock();
384 ASSERT_EQ(expected_samples_per_channel, block.size());
385 size_t enc_len_bytes =
386 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000387 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
388
Karl Wiberg45eb1352019-10-10 14:23:00 +0200389 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
390 payload, enc_len_bytes)));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800391 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700392 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800393 ASSERT_EQ(1u, output.num_channels_);
394 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800395 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000396
397 // Next packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200398 rtp_info.timestamp +=
399 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700400 rtp_info.sequenceNumber++;
Yves Gerey665174f2018-06-19 15:03:05 +0200401 receive_timestamp +=
402 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000403 }
404
henrik.lundin6d8e0112016-03-04 10:34:21 -0800405 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000406
407 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
408 // one frame without checking speech-type. This is the first frame pulled
409 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700410 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800411 ASSERT_EQ(1u, output.num_channels_);
412 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000413
414 // To be able to test the fading of background noise we need at lease to
415 // pull 611 frames.
416 const int kFadingThreshold = 611;
417
418 // Test several CNG-to-PLC packet for the expected behavior. The number 20
419 // is arbitrary, but sufficiently large to test enough number of frames.
420 const int kNumPlcToCngTestFrames = 20;
421 bool plc_to_cng = false;
422 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800423 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700424 // Set to non-zero.
425 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700426 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
427 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800428 ASSERT_EQ(1u, output.num_channels_);
429 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800430 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000431 plc_to_cng = true;
432 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700433 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800434 for (size_t k = 0;
435 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -0700436 sum_squared += output_data[k] * output_data[k];
Henrik Lundin67190172018-04-20 15:34:48 +0200437 EXPECT_EQ(0, sum_squared);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000438 } else {
henrik.lundin55480f52016-03-08 02:37:57 -0800439 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000440 }
441 }
442 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
443 }
444};
445
Henrik Lundin67190172018-04-20 15:34:48 +0200446TEST_F(NetEqBgnTest, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000447 CheckBgn(8000);
448 CheckBgn(16000);
449 CheckBgn(32000);
450}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000451
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000452TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
453 // Start with a sequence number that will soon wrap.
454 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
455 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
456}
457
458TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
459 // Start with a sequence number that will soon wrap.
460 std::set<uint16_t> drop_seq_numbers;
461 drop_seq_numbers.insert(0xFFFF);
462 drop_seq_numbers.insert(0x0);
463 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
464}
465
466TEST_F(NetEqDecodingTest, TimestampWrap) {
467 // Start with a timestamp that will soon wrap.
468 std::set<uint16_t> drop_seq_numbers;
469 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
470}
471
472TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
473 // Start with a timestamp and a sequence number that will wrap at the same
474 // time.
475 std::set<uint16_t> drop_seq_numbers;
476 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
477}
478
Yves Gerey3a65f392019-11-11 18:05:42 +0100479TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000480 uint16_t seq_no = 0;
481 uint32_t timestamp = 0;
482 const int kFrameSizeMs = 10;
483 const int kSampleRateKhz = 16;
484 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000485 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000486
Yves Gerey665174f2018-06-19 15:03:05 +0200487 const int algorithmic_delay_samples =
488 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000489 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000490 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000491 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700492 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700493 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000494 for (int i = 0; i < 3; ++i) {
495 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200496 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000497 ++seq_no;
498 timestamp += kSamples;
499
500 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700501 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800502 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000503 }
504 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -0800505 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000506
507 // Insert same CNG packet twice.
508 const int kCngPeriodMs = 100;
509 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000510 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000511 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
512 // This is the first time this CNG packet is inserted.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200513 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
514 payload, payload_len)));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000515
516 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -0700517 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800518 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800519 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700520 EXPECT_FALSE(
521 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -0700522 EXPECT_EQ(timestamp - algorithmic_delay_samples,
523 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000524
525 // Insert the same CNG packet again. Note that at this point it is old, since
526 // we have already decoded the first copy of it.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200527 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
528 payload, payload_len)));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000529
530 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
531 // we have already pulled out CNG once.
532 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -0700533 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800534 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800535 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700536 EXPECT_FALSE(
537 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000538 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700539 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000540 }
541
542 // Insert speech again.
543 ++seq_no;
544 timestamp += kCngPeriodSamples;
545 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200546 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000547
548 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -0700549 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800550 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800551 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200552 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700553 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000554 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700555 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +0000556}
557
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000558TEST_F(NetEqDecodingTest, CngFirst) {
559 uint16_t seq_no = 0;
560 uint32_t timestamp = 0;
561 const int kFrameSizeMs = 10;
562 const int kSampleRateKhz = 16;
563 const int kSamples = kFrameSizeMs * kSampleRateKhz;
564 const int kPayloadBytes = kSamples * 2;
565 const int kCngPeriodMs = 100;
566 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
567 size_t payload_len;
568
569 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700570 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000571
572 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200573 ASSERT_EQ(NetEq::kOK,
574 neteq_->InsertPacket(
575 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len)));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000576 ++seq_no;
577 timestamp += kCngPeriodSamples;
578
579 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -0700580 bool muted;
581 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800582 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800583 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000584
585 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -0700586 const uint32_t first_speech_timestamp = timestamp;
587 int timeout_counter = 0;
588 do {
589 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000590 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200591 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000592 ++seq_no;
593 timestamp += kSamples;
594
595 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700596 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800597 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -0700598 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000599 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -0800600 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +0000601}
henrik.lundin7a926812016-05-12 13:51:28 -0700602
603class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
604 public:
605 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
606 config_.enable_muted_state = true;
607 }
608
609 protected:
610 static constexpr size_t kSamples = 10 * 16;
611 static constexpr size_t kPayloadBytes = kSamples * 2;
612
613 void InsertPacket(uint32_t rtp_timestamp) {
614 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700615 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700616 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200617 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin7a926812016-05-12 13:51:28 -0700618 }
619
henrik.lundin42feb512016-09-20 06:51:40 -0700620 void InsertCngPacket(uint32_t rtp_timestamp) {
621 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700622 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -0700623 size_t payload_len;
624 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200625 EXPECT_EQ(NetEq::kOK,
626 neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
627 payload, payload_len)));
henrik.lundin42feb512016-09-20 06:51:40 -0700628 }
629
henrik.lundin7a926812016-05-12 13:51:28 -0700630 bool GetAudioReturnMuted() {
631 bool muted;
632 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
633 return muted;
634 }
635
636 void GetAudioUntilMuted() {
637 while (!GetAudioReturnMuted()) {
638 ASSERT_LT(counter_++, 1000) << "Test timed out";
639 }
640 }
641
642 void GetAudioUntilNormal() {
643 bool muted = false;
644 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
645 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
646 ASSERT_LT(counter_++, 1000) << "Test timed out";
647 }
648 EXPECT_FALSE(muted);
649 }
650
651 int counter_ = 0;
652};
653
654// Verifies that NetEq goes in and out of muted state as expected.
655TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
656 // Insert one speech packet.
657 InsertPacket(0);
658 // Pull out audio once and expect it not to be muted.
659 EXPECT_FALSE(GetAudioReturnMuted());
660 // Pull data until faded out.
661 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -0700662 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -0700663
664 // Verify that output audio is not written during muted mode. Other parameters
665 // should be correct, though.
666 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -0700667 int16_t* frame_data = new_frame.mutable_data();
668 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
669 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -0700670 }
671 bool muted;
672 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
673 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -0700674 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -0700675 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
676 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -0700677 }
678 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
679 new_frame.timestamp_);
680 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
681 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
682 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
683 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
684 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
685
686 // Insert new data. Timestamp is corrected for the time elapsed since the last
687 // packet. Verify that normal operation resumes.
688 InsertPacket(kSamples * counter_);
689 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -0700690 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -0700691
692 NetEqNetworkStatistics stats;
693 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
694 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
695 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
696 // concealment samples in this test.
697 EXPECT_GT(stats.expand_rate, 14000);
698 // And, it should be greater than the speech_expand_rate.
699 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -0700700}
701
702// Verifies that NetEq goes out of muted state when given a delayed packet.
703TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
704 // Insert one speech packet.
705 InsertPacket(0);
706 // Pull out audio once and expect it not to be muted.
707 EXPECT_FALSE(GetAudioReturnMuted());
708 // Pull data until faded out.
709 GetAudioUntilMuted();
710 // Insert new data. Timestamp is only corrected for the half of the time
711 // elapsed since the last packet. That is, the new packet is delayed. Verify
712 // that normal operation resumes.
713 InsertPacket(kSamples * counter_ / 2);
714 GetAudioUntilNormal();
715}
716
717// Verifies that NetEq goes out of muted state when given a future packet.
718TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
719 // Insert one speech packet.
720 InsertPacket(0);
721 // Pull out audio once and expect it not to be muted.
722 EXPECT_FALSE(GetAudioReturnMuted());
723 // Pull data until faded out.
724 GetAudioUntilMuted();
725 // Insert new data. Timestamp is over-corrected for the time elapsed since the
726 // last packet. That is, the new packet is too early. Verify that normal
727 // operation resumes.
728 InsertPacket(kSamples * counter_ * 2);
729 GetAudioUntilNormal();
730}
731
732// Verifies that NetEq goes out of muted state when given an old packet.
733TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
734 // Insert one speech packet.
735 InsertPacket(0);
736 // Pull out audio once and expect it not to be muted.
737 EXPECT_FALSE(GetAudioReturnMuted());
738 // Pull data until faded out.
739 GetAudioUntilMuted();
740
741 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
742 // Insert packet which is older than the first packet.
743 InsertPacket(kSamples * (counter_ - 1000));
744 EXPECT_FALSE(GetAudioReturnMuted());
745 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
746}
747
henrik.lundin42feb512016-09-20 06:51:40 -0700748// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
749// packet stream is suspended for a long time.
750TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
751 // Insert one CNG packet.
752 InsertCngPacket(0);
753
754 // Pull 10 seconds of audio (10 ms audio generated per lap).
755 for (int i = 0; i < 1000; ++i) {
756 bool muted;
757 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
758 ASSERT_FALSE(muted);
759 }
760 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
761}
762
763// Verifies that NetEq goes back to normal after a long CNG period with the
764// packet stream suspended.
765TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
766 // Insert one CNG packet.
767 InsertCngPacket(0);
768
769 // Pull 10 seconds of audio (10 ms audio generated per lap).
770 for (int i = 0; i < 1000; ++i) {
771 bool muted;
772 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
773 }
774
775 // Insert new data. Timestamp is corrected for the time elapsed since the last
776 // packet. Verify that normal operation resumes.
777 InsertPacket(kSamples * counter_);
778 GetAudioUntilNormal();
779}
780
henrik.lundin7a926812016-05-12 13:51:28 -0700781namespace {
782::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
783 const AudioFrame& b) {
784 if (a.timestamp_ != b.timestamp_)
785 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
786 << " != " << b.timestamp_ << ")";
787 if (a.sample_rate_hz_ != b.sample_rate_hz_)
Yves Gerey665174f2018-06-19 15:03:05 +0200788 return ::testing::AssertionFailure()
789 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
790 << " != " << b.sample_rate_hz_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700791 if (a.samples_per_channel_ != b.samples_per_channel_)
792 return ::testing::AssertionFailure()
793 << "samples_per_channel_ diff (" << a.samples_per_channel_
794 << " != " << b.samples_per_channel_ << ")";
795 if (a.num_channels_ != b.num_channels_)
Yves Gerey665174f2018-06-19 15:03:05 +0200796 return ::testing::AssertionFailure()
797 << "num_channels_ diff (" << a.num_channels_
798 << " != " << b.num_channels_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700799 if (a.speech_type_ != b.speech_type_)
Yves Gerey665174f2018-06-19 15:03:05 +0200800 return ::testing::AssertionFailure()
801 << "speech_type_ diff (" << a.speech_type_
802 << " != " << b.speech_type_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700803 if (a.vad_activity_ != b.vad_activity_)
Yves Gerey665174f2018-06-19 15:03:05 +0200804 return ::testing::AssertionFailure()
805 << "vad_activity_ diff (" << a.vad_activity_
806 << " != " << b.vad_activity_ << ")";
henrik.lundin7a926812016-05-12 13:51:28 -0700807 return ::testing::AssertionSuccess();
808}
809
810::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
811 const AudioFrame& b) {
812 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
813 if (!res)
814 return res;
Yves Gerey665174f2018-06-19 15:03:05 +0200815 if (memcmp(a.data(), b.data(),
816 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
817 0) {
henrik.lundin7a926812016-05-12 13:51:28 -0700818 return ::testing::AssertionFailure() << "data_ diff";
819 }
820 return ::testing::AssertionSuccess();
821}
822
823} // namespace
824
825TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
826 ASSERT_FALSE(config_.enable_muted_state);
827 config2_.enable_muted_state = true;
828 CreateSecondInstance();
829
830 // Insert one speech packet into both NetEqs.
831 const size_t kSamples = 10 * 16;
832 const size_t kPayloadBytes = kSamples * 2;
833 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700834 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -0700835 PopulateRtpInfo(0, 0, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200836 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
837 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
henrik.lundin7a926812016-05-12 13:51:28 -0700838
839 AudioFrame out_frame1, out_frame2;
840 bool muted;
841 for (int i = 0; i < 1000; ++i) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200842 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -0700843 ss << "i = " << i;
844 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
845 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
846 EXPECT_FALSE(muted);
847 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
848 if (muted) {
849 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
850 } else {
851 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
852 }
853 }
854 EXPECT_TRUE(muted);
855
856 // Insert new data. Timestamp is corrected for the time elapsed since the last
857 // packet.
858 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200859 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
860 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
henrik.lundin7a926812016-05-12 13:51:28 -0700861
862 int counter = 0;
863 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
864 ASSERT_LT(counter++, 1000) << "Test timed out";
Jonas Olsson366a50c2018-09-06 13:41:30 +0200865 rtc::StringBuilder ss;
henrik.lundin7a926812016-05-12 13:51:28 -0700866 ss << "counter = " << counter;
867 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
868 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
869 EXPECT_FALSE(muted);
870 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
871 if (muted) {
872 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
873 } else {
874 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
875 }
876 }
877 EXPECT_FALSE(muted);
878}
879
henrik.lundin114c1b32017-04-26 07:47:32 -0700880TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
881 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
882
883 // Pull out data once.
884 AudioFrame output;
885 bool muted;
886 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
887
888 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
889}
890
891TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
892 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
893 // default). Make the length 10 ms.
894 constexpr size_t kPayloadSamples = 16 * 10;
895 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
896 uint8_t payload[kPayloadBytes] = {0};
897
898 RTPHeader rtp_info;
899 constexpr uint32_t kRtpTimestamp = 0x1234;
900 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200901 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700902
903 // Pull out data once.
904 AudioFrame output;
905 bool muted;
906 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
907
908 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
909 neteq_->LastDecodedTimestamps());
910
911 // Nothing decoded on the second call.
912 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
913 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
914}
915
916TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
917 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
918 // by default). Make the length 5 ms so that NetEq must decode them both in
919 // the same GetAudio call.
920 constexpr size_t kPayloadSamples = 16 * 5;
921 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
922 uint8_t payload[kPayloadBytes] = {0};
923
924 RTPHeader rtp_info;
925 constexpr uint32_t kRtpTimestamp1 = 0x1234;
926 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200927 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700928 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
929 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200930 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
henrik.lundin114c1b32017-04-26 07:47:32 -0700931
932 // Pull out data once.
933 AudioFrame output;
934 bool muted;
935 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
936
937 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
938 neteq_->LastDecodedTimestamps());
939}
940
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200941TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
942 const int kNumConcealmentEvents = 19;
943 const size_t kSamples = 10 * 16;
944 const size_t kPayloadBytes = kSamples * 2;
945 int seq_no = 0;
946 RTPHeader rtp_info;
947 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
948 rtp_info.payloadType = 94; // PCM16b WB codec.
949 rtp_info.markerBit = 0;
950 const uint8_t payload[kPayloadBytes] = {0};
951 bool muted;
952
953 for (int i = 0; i < kNumConcealmentEvents; i++) {
954 // Insert some packets of 10 ms size.
955 for (int j = 0; j < 10; j++) {
956 rtp_info.sequenceNumber = seq_no++;
957 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200958 neteq_->InsertPacket(rtp_info, payload);
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200959 neteq_->GetAudio(&out_frame_, &muted);
960 }
961
962 // Lose a number of packets.
963 int num_lost = 1 + i;
964 for (int j = 0; j < num_lost; j++) {
965 seq_no++;
966 neteq_->GetAudio(&out_frame_, &muted);
967 }
968 }
969
970 // Check number of concealment events.
971 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
972 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
973}
974
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200975// Test that the jitter buffer delay stat is computed correctly.
976void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
977 const int kNumPackets = 10;
978 const int kDelayInNumPackets = 2;
979 const int kPacketLenMs = 10; // All packets are of 10 ms size.
980 const size_t kSamples = kPacketLenMs * 16;
981 const size_t kPayloadBytes = kSamples * 2;
982 RTPHeader rtp_info;
983 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
984 rtp_info.payloadType = 94; // PCM16b WB codec.
985 rtp_info.markerBit = 0;
986 const uint8_t payload[kPayloadBytes] = {0};
987 bool muted;
988 int packets_sent = 0;
989 int packets_received = 0;
990 int expected_delay = 0;
Chen Xing0acffb52019-01-15 15:46:29 +0100991 uint64_t expected_emitted_count = 0;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200992 while (packets_received < kNumPackets) {
993 // Insert packet.
994 if (packets_sent < kNumPackets) {
995 rtp_info.sequenceNumber = packets_sent++;
996 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200997 neteq_->InsertPacket(rtp_info, payload);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200998 }
999
1000 // Get packet.
1001 if (packets_sent > kDelayInNumPackets) {
1002 neteq_->GetAudio(&out_frame_, &muted);
1003 packets_received++;
1004
1005 // The delay reported by the jitter buffer never exceeds
1006 // the number of samples previously fetched with GetAudio
1007 // (hence the min()).
1008 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1009
1010 // The increase of the expected delay is the product of
1011 // the current delay of the jitter buffer in ms * the
1012 // number of samples that are sent for play out.
1013 int current_delay_ms = packets_delay * kPacketLenMs;
1014 expected_delay += current_delay_ms * 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();
1026 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
Chen Xing0acffb52019-01-15 15:46:29 +01001027 EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001028}
1029
1030TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1031 TestJitterBufferDelay(false);
1032}
1033
1034TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1035 TestJitterBufferDelay(true);
1036}
1037
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001038TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithAcceleration) {
1039 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1040 const size_t kSamples = kPacketLenMs * 16;
1041 const size_t kPayloadBytes = kSamples * 2;
1042 RTPHeader rtp_info;
1043 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1044 rtp_info.payloadType = 94; // PCM16b WB codec.
1045 rtp_info.markerBit = 0;
1046 const uint8_t payload[kPayloadBytes] = {0};
1047
Karl Wiberg45eb1352019-10-10 14:23:00 +02001048 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001049
1050 bool muted;
1051 neteq_->GetAudio(&out_frame_, &muted);
1052
1053 rtp_info.sequenceNumber += 1;
1054 rtp_info.timestamp += kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001055 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001056 rtp_info.sequenceNumber += 1;
1057 rtp_info.timestamp += kSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001058 neteq_->InsertPacket(rtp_info, payload);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001059
1060 // We have two packets in the buffer and kAccelerate operation will
1061 // extract 20 ms of data.
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001062 neteq_->GetAudio(&out_frame_, &muted, NetEq::Operation::kAccelerate);
Jakob Ivarsson26c59ff2019-02-28 09:55:49 +01001063
1064 // Check jitter buffer delay.
1065 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1066 EXPECT_EQ(10 * kSamples * 3, stats.jitter_buffer_delay_ms);
1067 EXPECT_EQ(kSamples * 3, stats.jitter_buffer_emitted_count);
1068}
1069
Henrik Lundin7687ad52018-07-02 10:14:46 +02001070namespace test {
Henrik Lundin7687ad52018-07-02 10:14:46 +02001071TEST(NetEqNoTimeStretchingMode, RunTest) {
1072 NetEq::Config config;
1073 config.for_test_no_time_stretching = true;
1074 auto codecs = NetEqTest::StandardDecoderMap();
Henrik Lundin7687ad52018-07-02 10:14:46 +02001075 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
1076 {1, kRtpExtensionAudioLevel},
1077 {3, kRtpExtensionAbsoluteSendTime},
1078 {5, kRtpExtensionTransportSequenceNumber},
1079 {7, kRtpExtensionVideoContentType},
1080 {8, kRtpExtensionVideoTiming}};
1081 std::unique_ptr<NetEqInput> input(new NetEqRtpDumpInput(
1082 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"),
Bjorn Terelius5350d1c2018-10-11 16:51:23 +02001083 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
Henrik Lundin7687ad52018-07-02 10:14:46 +02001084 std::unique_ptr<TimeLimitedNetEqInput> input_time_limit(
1085 new TimeLimitedNetEqInput(std::move(input), 20000));
1086 std::unique_ptr<AudioSink> output(new VoidAudioSink);
1087 NetEqTest::Callbacks callbacks;
Niels Möllerbd6dee82019-01-02 09:39:47 +01001088 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, nullptr,
1089 std::move(input_time_limit), std::move(output), callbacks);
Henrik Lundin7687ad52018-07-02 10:14:46 +02001090 test.Run();
1091 const auto stats = test.SimulationStats();
1092 EXPECT_EQ(0, stats.accelerate_rate);
1093 EXPECT_EQ(0, stats.preemptive_rate);
1094}
Henrik Lundin7687ad52018-07-02 10:14:46 +02001095
1096} // namespace test
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001097} // namespace webrtc