1 /*
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
11 #include "api/neteq/neteq.h"
12
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h> // memset
16
17 #include <algorithm>
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <vector>
22
23 #include "absl/flags/flag.h"
24 #include "api/audio/audio_frame.h"
25 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
26 #include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
27 #include "modules/audio_coding/neteq/test/neteq_decoding_test.h"
28 #include "modules/audio_coding/neteq/tools/audio_loop.h"
29 #include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
30 #include "modules/audio_coding/neteq/tools/neteq_test.h"
31 #include "modules/include/module_common_types_public.h"
32 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
33 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
34 #include "rtc_base/ignore_wundef.h"
35 #include "rtc_base/message_digest.h"
36 #include "rtc_base/numerics/safe_conversions.h"
37 #include "rtc_base/string_encode.h"
38 #include "rtc_base/strings/string_builder.h"
39 #include "rtc_base/system/arch.h"
40 #include "test/field_trial.h"
41 #include "test/gtest.h"
42 #include "test/testsupport/file_utils.h"
43
44 ABSL_FLAG(bool, gen_ref, false, "Generate reference files.");
45
46 namespace webrtc {
47
48 namespace {
49
PlatformChecksum(const std::string & checksum_general,const std::string & checksum_android_32,const std::string & checksum_android_64,const std::string & checksum_win_32,const std::string & checksum_win_64)50 const std::string& PlatformChecksum(const std::string& checksum_general,
51 const std::string& checksum_android_32,
52 const std::string& checksum_android_64,
53 const std::string& checksum_win_32,
54 const std::string& checksum_win_64) {
55 #if defined(WEBRTC_ANDROID)
56 #ifdef WEBRTC_ARCH_64_BITS
57 return checksum_android_64;
58 #else
59 return checksum_android_32;
60 #endif // WEBRTC_ARCH_64_BITS
61 #elif defined(WEBRTC_WIN)
62 #ifdef WEBRTC_ARCH_64_BITS
63 return checksum_win_64;
64 #else
65 return checksum_win_32;
66 #endif // WEBRTC_ARCH_64_BITS
67 #else
68 return checksum_general;
69 #endif // WEBRTC_WIN
70 }
71
72 } // namespace
73
74
75 #if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
76 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
77 defined(WEBRTC_CODEC_ILBC) && !defined(WEBRTC_ARCH_ARM64)
78 #define MAYBE_TestBitExactness TestBitExactness
79 #else
80 #define MAYBE_TestBitExactness DISABLED_TestBitExactness
81 #endif
TEST_F(NetEqDecodingTest,MAYBE_TestBitExactness)82 TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
83 const std::string input_rtp_file =
84 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
85
86 const std::string output_checksum =
87 PlatformChecksum("6ae9f643dc3e5f3452d28a772eef7e00e74158bc",
88 "f4374430e870d66268c1b8e22fb700eb072d567e", "not used",
89 "6ae9f643dc3e5f3452d28a772eef7e00e74158bc",
90 "8d73c98645917cdeaaa01c20cf095ccc5a10b2b5");
91
92 const std::string network_stats_checksum =
93 PlatformChecksum("3d186ea7e243abfdbd3d39b8ebf8f02a318117e4",
94 "0b725774133da5dd823f2046663c12a76e0dbd79", "not used",
95 "3d186ea7e243abfdbd3d39b8ebf8f02a318117e4",
96 "3d186ea7e243abfdbd3d39b8ebf8f02a318117e4");
97
98 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
99 absl::GetFlag(FLAGS_gen_ref));
100 }
101
102 #if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
103 defined(WEBRTC_CODEC_OPUS)
104 #define MAYBE_TestOpusBitExactness TestOpusBitExactness
105 #else
106 #define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
107 #endif
TEST_F(NetEqDecodingTest,MAYBE_TestOpusBitExactness)108 TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
109 const std::string input_rtp_file =
110 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
111
112 const std::string maybe_sse =
113 "554ad4133934e3920f97575579a46f674683d77c"
114 "|de316e2bfb15192edb820fe5fb579d11ff5a524b";
115 const std::string output_checksum = PlatformChecksum(
116 maybe_sse, "459c356a0ef245ddff381f7d82d205d426ef2002",
117 "625055e5eb0e6de2c9d170b4494eadc5afab08c8", maybe_sse, maybe_sse);
118
119 const std::string network_stats_checksum =
120 PlatformChecksum("439a3d0c9b5115e6d4f8387f64ed2d57cae29b0a",
121 "048f33d85d0a32a328b7da42448f560456a5fef0",
122 "c876f2a04c4f0a91da7f084f80e87871b7c5a4a1",
123 "439a3d0c9b5115e6d4f8387f64ed2d57cae29b0a",
124 "439a3d0c9b5115e6d4f8387f64ed2d57cae29b0a");
125
126 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
127 absl::GetFlag(FLAGS_gen_ref));
128 }
129
130 #if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
131 defined(WEBRTC_CODEC_OPUS)
132 #define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness
133 #else
134 #define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness
135 #endif
TEST_F(NetEqDecodingTest,MAYBE_TestOpusDtxBitExactness)136 TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) {
137 const std::string input_rtp_file =
138 webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp");
139
140 const std::string maybe_sse =
141 "df5d1d3019bf3764829b84f4fb315721f4adde29"
142 "|5935d2fad14a69a8b61dbc8e6f2d37c8c0814925";
143 const std::string output_checksum = PlatformChecksum(
144 maybe_sse, "551df04e8f45cd99eff28503edf0cf92974898ac",
145 "709a3f0f380393d3a67bace10e2265b90a6ebbeb", maybe_sse, maybe_sse);
146
147 const std::string network_stats_checksum =
148 "8caf49765f35b6862066d3f17531ce44d8e25f60";
149
150 DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum,
151 absl::GetFlag(FLAGS_gen_ref));
152 }
153
154 // Use fax mode to avoid time-scaling. This is to simplify the testing of
155 // packet waiting times in the packet buffer.
156 class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
157 protected:
NetEqDecodingTestFaxMode()158 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
159 config_.for_test_no_time_stretching = true;
160 }
161 void TestJitterBufferDelay(bool apply_packet_loss);
162 };
163
TEST_F(NetEqDecodingTestFaxMode,TestFrameWaitingTimeStatistics)164 TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
165 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
166 size_t num_frames = 30;
167 const size_t kSamples = 10 * 16;
168 const size_t kPayloadBytes = kSamples * 2;
169 for (size_t i = 0; i < num_frames; ++i) {
170 const uint8_t payload[kPayloadBytes] = {0};
171 RTPHeader rtp_info;
172 rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i);
173 rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples);
174 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
175 rtp_info.payloadType = 94; // PCM16b WB codec.
176 rtp_info.markerBit = 0;
177 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
178 }
179 // Pull out all data.
180 for (size_t i = 0; i < num_frames; ++i) {
181 bool muted;
182 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
183 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
184 }
185
186 NetEqNetworkStatistics stats;
187 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
188 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
189 // spacing (per definition), we expect the delay to increase with 10 ms for
190 // each packet. Thus, we are calculating the statistics for a series from 10
191 // to 300, in steps of 10 ms.
192 EXPECT_EQ(155, stats.mean_waiting_time_ms);
193 EXPECT_EQ(155, stats.median_waiting_time_ms);
194 EXPECT_EQ(10, stats.min_waiting_time_ms);
195 EXPECT_EQ(300, stats.max_waiting_time_ms);
196
197 // Check statistics again and make sure it's been reset.
198 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
199 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
200 EXPECT_EQ(-1, stats.median_waiting_time_ms);
201 EXPECT_EQ(-1, stats.min_waiting_time_ms);
202 EXPECT_EQ(-1, stats.max_waiting_time_ms);
203 }
204
205
TEST_F(NetEqDecodingTest,LongCngWithNegativeClockDrift)206 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
207 // Apply a clock drift of -25 ms / s (sender faster than receiver).
208 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
209 const double kNetworkFreezeTimeMs = 0.0;
210 const bool kGetAudioDuringFreezeRecovery = false;
211 const int kDelayToleranceMs = 20;
212 const int kMaxTimeToSpeechMs = 100;
213 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
214 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
215 kMaxTimeToSpeechMs);
216 }
217
TEST_F(NetEqDecodingTest,LongCngWithPositiveClockDrift)218 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
219 // Apply a clock drift of +25 ms / s (sender slower than receiver).
220 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
221 const double kNetworkFreezeTimeMs = 0.0;
222 const bool kGetAudioDuringFreezeRecovery = false;
223 const int kDelayToleranceMs = 40;
224 const int kMaxTimeToSpeechMs = 100;
225 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
226 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
227 kMaxTimeToSpeechMs);
228 }
229
TEST_F(NetEqDecodingTest,LongCngWithNegativeClockDriftNetworkFreeze)230 TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
231 // Apply a clock drift of -25 ms / s (sender faster than receiver).
232 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
233 const double kNetworkFreezeTimeMs = 5000.0;
234 const bool kGetAudioDuringFreezeRecovery = false;
235 const int kDelayToleranceMs = 60;
236 const int kMaxTimeToSpeechMs = 200;
237 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
238 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
239 kMaxTimeToSpeechMs);
240 }
241
TEST_F(NetEqDecodingTest,LongCngWithPositiveClockDriftNetworkFreeze)242 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
243 // Apply a clock drift of +25 ms / s (sender slower than receiver).
244 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
245 const double kNetworkFreezeTimeMs = 5000.0;
246 const bool kGetAudioDuringFreezeRecovery = false;
247 const int kDelayToleranceMs = 40;
248 const int kMaxTimeToSpeechMs = 100;
249 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
250 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
251 kMaxTimeToSpeechMs);
252 }
253
TEST_F(NetEqDecodingTest,LongCngWithPositiveClockDriftNetworkFreezeExtraPull)254 TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
255 // Apply a clock drift of +25 ms / s (sender slower than receiver).
256 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
257 const double kNetworkFreezeTimeMs = 5000.0;
258 const bool kGetAudioDuringFreezeRecovery = true;
259 const int kDelayToleranceMs = 40;
260 const int kMaxTimeToSpeechMs = 100;
261 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
262 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
263 kMaxTimeToSpeechMs);
264 }
265
TEST_F(NetEqDecodingTest,LongCngWithoutClockDrift)266 TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
267 const double kDriftFactor = 1.0; // No drift.
268 const double kNetworkFreezeTimeMs = 0.0;
269 const bool kGetAudioDuringFreezeRecovery = false;
270 const int kDelayToleranceMs = 10;
271 const int kMaxTimeToSpeechMs = 50;
272 LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs,
273 kGetAudioDuringFreezeRecovery, kDelayToleranceMs,
274 kMaxTimeToSpeechMs);
275 }
276
TEST_F(NetEqDecodingTest,UnknownPayloadType)277 TEST_F(NetEqDecodingTest, UnknownPayloadType) {
278 const size_t kPayloadBytes = 100;
279 uint8_t payload[kPayloadBytes] = {0};
280 RTPHeader rtp_info;
281 PopulateRtpInfo(0, 0, &rtp_info);
282 rtp_info.payloadType = 1; // Not registered as a decoder.
283 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload));
284 }
285
286 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
287 #define MAYBE_DecoderError DecoderError
288 #else
289 #define MAYBE_DecoderError DISABLED_DecoderError
290 #endif
291
TEST_F(NetEqDecodingTest,MAYBE_DecoderError)292 TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
293 const size_t kPayloadBytes = 100;
294 uint8_t payload[kPayloadBytes] = {0};
295 RTPHeader rtp_info;
296 PopulateRtpInfo(0, 0, &rtp_info);
297 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
298 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
299 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
300 // to GetAudio.
301 int16_t* out_frame_data = out_frame_.mutable_data();
302 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
303 out_frame_data[i] = 1;
304 }
305 bool muted;
306 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
307 ASSERT_FALSE(muted);
308
309 // Verify that the first 160 samples are set to 0.
310 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
311 const int16_t* const_out_frame_data = out_frame_.data();
312 for (int i = 0; i < kExpectedOutputLength; ++i) {
313 rtc::StringBuilder ss;
314 ss << "i = " << i;
315 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
316 EXPECT_EQ(0, const_out_frame_data[i]);
317 }
318 }
319
TEST_F(NetEqDecodingTest,GetAudioBeforeInsertPacket)320 TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
321 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
322 // to GetAudio.
323 int16_t* out_frame_data = out_frame_.mutable_data();
324 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
325 out_frame_data[i] = 1;
326 }
327 bool muted;
328 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
329 ASSERT_FALSE(muted);
330 // Verify that the first block of samples is set to 0.
331 static const int kExpectedOutputLength =
332 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
333 const int16_t* const_out_frame_data = out_frame_.data();
334 for (int i = 0; i < kExpectedOutputLength; ++i) {
335 rtc::StringBuilder ss;
336 ss << "i = " << i;
337 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
338 EXPECT_EQ(0, const_out_frame_data[i]);
339 }
340 // Verify that the sample rate did not change from the initial configuration.
341 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
342 }
343
344 class NetEqBgnTest : public NetEqDecodingTest {
345 protected:
CheckBgn(int sampling_rate_hz)346 void CheckBgn(int sampling_rate_hz) {
347 size_t expected_samples_per_channel = 0;
348 uint8_t payload_type = 0xFF; // Invalid.
349 if (sampling_rate_hz == 8000) {
350 expected_samples_per_channel = kBlockSize8kHz;
351 payload_type = 93; // PCM 16, 8 kHz.
352 } else if (sampling_rate_hz == 16000) {
353 expected_samples_per_channel = kBlockSize16kHz;
354 payload_type = 94; // PCM 16, 16 kHZ.
355 } else if (sampling_rate_hz == 32000) {
356 expected_samples_per_channel = kBlockSize32kHz;
357 payload_type = 95; // PCM 16, 32 kHz.
358 } else {
359 ASSERT_TRUE(false); // Unsupported test case.
360 }
361
362 AudioFrame output;
363 test::AudioLoop input;
364 // We are using the same 32 kHz input file for all tests, regardless of
365 // |sampling_rate_hz|. The output may sound weird, but the test is still
366 // valid.
367 ASSERT_TRUE(input.Init(
368 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
369 10 * sampling_rate_hz, // Max 10 seconds loop length.
370 expected_samples_per_channel));
371
372 // Payload of 10 ms of PCM16 32 kHz.
373 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
374 RTPHeader rtp_info;
375 PopulateRtpInfo(0, 0, &rtp_info);
376 rtp_info.payloadType = payload_type;
377
378 uint32_t receive_timestamp = 0;
379 bool muted;
380 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
381 auto block = input.GetNextBlock();
382 ASSERT_EQ(expected_samples_per_channel, block.size());
383 size_t enc_len_bytes =
384 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
385 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
386
387 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
388 payload, enc_len_bytes)));
389 output.Reset();
390 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
391 ASSERT_EQ(1u, output.num_channels_);
392 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
393 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
394
395 // Next packet.
396 rtp_info.timestamp +=
397 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
398 rtp_info.sequenceNumber++;
399 receive_timestamp +=
400 rtc::checked_cast<uint32_t>(expected_samples_per_channel);
401 }
402
403 output.Reset();
404
405 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
406 // one frame without checking speech-type. This is the first frame pulled
407 // without inserting any packet, and might not be labeled as PLC.
408 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
409 ASSERT_EQ(1u, output.num_channels_);
410 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
411
412 // To be able to test the fading of background noise we need at lease to
413 // pull 611 frames.
414 const int kFadingThreshold = 611;
415
416 // Test several CNG-to-PLC packet for the expected behavior. The number 20
417 // is arbitrary, but sufficiently large to test enough number of frames.
418 const int kNumPlcToCngTestFrames = 20;
419 bool plc_to_cng = false;
420 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
421 output.Reset();
422 // Set to non-zero.
423 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
424 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
425 ASSERT_FALSE(muted);
426 ASSERT_EQ(1u, output.num_channels_);
427 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
428 if (output.speech_type_ == AudioFrame::kPLCCNG) {
429 plc_to_cng = true;
430 double sum_squared = 0;
431 const int16_t* output_data = output.data();
432 for (size_t k = 0;
433 k < output.num_channels_ * output.samples_per_channel_; ++k)
434 sum_squared += output_data[k] * output_data[k];
435 EXPECT_EQ(0, sum_squared);
436 } else {
437 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
438 }
439 }
440 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
441 }
442 };
443
TEST_F(NetEqBgnTest,RunTest)444 TEST_F(NetEqBgnTest, RunTest) {
445 CheckBgn(8000);
446 CheckBgn(16000);
447 CheckBgn(32000);
448 }
449
TEST_F(NetEqDecodingTest,SequenceNumberWrap)450 TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
451 // Start with a sequence number that will soon wrap.
452 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
453 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
454 }
455
TEST_F(NetEqDecodingTest,SequenceNumberWrapAndDrop)456 TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
457 // Start with a sequence number that will soon wrap.
458 std::set<uint16_t> drop_seq_numbers;
459 drop_seq_numbers.insert(0xFFFF);
460 drop_seq_numbers.insert(0x0);
461 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
462 }
463
TEST_F(NetEqDecodingTest,TimestampWrap)464 TEST_F(NetEqDecodingTest, TimestampWrap) {
465 // Start with a timestamp that will soon wrap.
466 std::set<uint16_t> drop_seq_numbers;
467 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
468 }
469
TEST_F(NetEqDecodingTest,TimestampAndSequenceNumberWrap)470 TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
471 // Start with a timestamp and a sequence number that will wrap at the same
472 // time.
473 std::set<uint16_t> drop_seq_numbers;
474 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
475 }
476
TEST_F(NetEqDecodingTest,DiscardDuplicateCng)477 TEST_F(NetEqDecodingTest, DiscardDuplicateCng) {
478 uint16_t seq_no = 0;
479 uint32_t timestamp = 0;
480 const int kFrameSizeMs = 10;
481 const int kSampleRateKhz = 16;
482 const int kSamples = kFrameSizeMs * kSampleRateKhz;
483 const size_t kPayloadBytes = kSamples * 2;
484
485 const int algorithmic_delay_samples =
486 std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
487 // Insert three speech packets. Three are needed to get the frame length
488 // correct.
489 uint8_t payload[kPayloadBytes] = {0};
490 RTPHeader rtp_info;
491 bool muted;
492 for (int i = 0; i < 3; ++i) {
493 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
494 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
495 ++seq_no;
496 timestamp += kSamples;
497
498 // Pull audio once.
499 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
500 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
501 }
502 // Verify speech output.
503 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
504
505 // Insert same CNG packet twice.
506 const int kCngPeriodMs = 100;
507 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
508 size_t payload_len;
509 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
510 // This is the first time this CNG packet is inserted.
511 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
512 payload, payload_len)));
513
514 // Pull audio once and make sure CNG is played.
515 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
516 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
517 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
518 EXPECT_FALSE(
519 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
520 EXPECT_EQ(timestamp - algorithmic_delay_samples,
521 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
522
523 // Insert the same CNG packet again. Note that at this point it is old, since
524 // we have already decoded the first copy of it.
525 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
526 payload, payload_len)));
527
528 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
529 // we have already pulled out CNG once.
530 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
531 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
532 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
533 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
534 EXPECT_FALSE(
535 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
536 EXPECT_EQ(timestamp - algorithmic_delay_samples,
537 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
538 }
539
540 // Insert speech again.
541 ++seq_no;
542 timestamp += kCngPeriodSamples;
543 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
544 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
545
546 // Pull audio once and verify that the output is speech again.
547 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
548 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
549 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
550 absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
551 ASSERT_TRUE(playout_timestamp);
552 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
553 *playout_timestamp);
554 }
555
TEST_F(NetEqDecodingTest,CngFirst)556 TEST_F(NetEqDecodingTest, CngFirst) {
557 uint16_t seq_no = 0;
558 uint32_t timestamp = 0;
559 const int kFrameSizeMs = 10;
560 const int kSampleRateKhz = 16;
561 const int kSamples = kFrameSizeMs * kSampleRateKhz;
562 const int kPayloadBytes = kSamples * 2;
563 const int kCngPeriodMs = 100;
564 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
565 size_t payload_len;
566
567 uint8_t payload[kPayloadBytes] = {0};
568 RTPHeader rtp_info;
569
570 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
571 ASSERT_EQ(NetEq::kOK,
572 neteq_->InsertPacket(
573 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len)));
574 ++seq_no;
575 timestamp += kCngPeriodSamples;
576
577 // Pull audio once and make sure CNG is played.
578 bool muted;
579 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
580 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
581 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
582
583 // Insert some speech packets.
584 const uint32_t first_speech_timestamp = timestamp;
585 int timeout_counter = 0;
586 do {
587 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
588 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
589 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
590 ++seq_no;
591 timestamp += kSamples;
592
593 // Pull audio once.
594 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
595 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
596 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
597 // Verify speech output.
598 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
599 }
600
601 class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
602 public:
NetEqDecodingTestWithMutedState()603 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
604 config_.enable_muted_state = true;
605 }
606
607 protected:
608 static constexpr size_t kSamples = 10 * 16;
609 static constexpr size_t kPayloadBytes = kSamples * 2;
610
InsertPacket(uint32_t rtp_timestamp)611 void InsertPacket(uint32_t rtp_timestamp) {
612 uint8_t payload[kPayloadBytes] = {0};
613 RTPHeader rtp_info;
614 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
615 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
616 }
617
InsertCngPacket(uint32_t rtp_timestamp)618 void InsertCngPacket(uint32_t rtp_timestamp) {
619 uint8_t payload[kPayloadBytes] = {0};
620 RTPHeader rtp_info;
621 size_t payload_len;
622 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
623 EXPECT_EQ(NetEq::kOK,
624 neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
625 payload, payload_len)));
626 }
627
GetAudioReturnMuted()628 bool GetAudioReturnMuted() {
629 bool muted;
630 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
631 return muted;
632 }
633
GetAudioUntilMuted()634 void GetAudioUntilMuted() {
635 while (!GetAudioReturnMuted()) {
636 ASSERT_LT(counter_++, 1000) << "Test timed out";
637 }
638 }
639
GetAudioUntilNormal()640 void GetAudioUntilNormal() {
641 bool muted = false;
642 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
643 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
644 ASSERT_LT(counter_++, 1000) << "Test timed out";
645 }
646 EXPECT_FALSE(muted);
647 }
648
649 int counter_ = 0;
650 };
651
652 // Verifies that NetEq goes in and out of muted state as expected.
TEST_F(NetEqDecodingTestWithMutedState,MutedState)653 TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
654 // Insert one speech packet.
655 InsertPacket(0);
656 // Pull out audio once and expect it not to be muted.
657 EXPECT_FALSE(GetAudioReturnMuted());
658 // Pull data until faded out.
659 GetAudioUntilMuted();
660 EXPECT_TRUE(out_frame_.muted());
661
662 // Verify that output audio is not written during muted mode. Other parameters
663 // should be correct, though.
664 AudioFrame new_frame;
665 int16_t* frame_data = new_frame.mutable_data();
666 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
667 frame_data[i] = 17;
668 }
669 bool muted;
670 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
671 EXPECT_TRUE(muted);
672 EXPECT_TRUE(out_frame_.muted());
673 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
674 EXPECT_EQ(17, frame_data[i]);
675 }
676 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
677 new_frame.timestamp_);
678 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
679 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
680 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
681 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
682 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
683
684 // Insert new data. Timestamp is corrected for the time elapsed since the last
685 // packet. Verify that normal operation resumes.
686 InsertPacket(kSamples * counter_);
687 GetAudioUntilNormal();
688 EXPECT_FALSE(out_frame_.muted());
689
690 NetEqNetworkStatistics stats;
691 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
692 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
693 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
694 // concealment samples in this test.
695 EXPECT_GT(stats.expand_rate, 14000);
696 // And, it should be greater than the speech_expand_rate.
697 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
698 }
699
700 // Verifies that NetEq goes out of muted state when given a delayed packet.
TEST_F(NetEqDecodingTestWithMutedState,MutedStateDelayedPacket)701 TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
702 // Insert one speech packet.
703 InsertPacket(0);
704 // Pull out audio once and expect it not to be muted.
705 EXPECT_FALSE(GetAudioReturnMuted());
706 // Pull data until faded out.
707 GetAudioUntilMuted();
708 // Insert new data. Timestamp is only corrected for the half of the time
709 // elapsed since the last packet. That is, the new packet is delayed. Verify
710 // that normal operation resumes.
711 InsertPacket(kSamples * counter_ / 2);
712 GetAudioUntilNormal();
713 }
714
715 // Verifies that NetEq goes out of muted state when given a future packet.
TEST_F(NetEqDecodingTestWithMutedState,MutedStateFuturePacket)716 TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
717 // Insert one speech packet.
718 InsertPacket(0);
719 // Pull out audio once and expect it not to be muted.
720 EXPECT_FALSE(GetAudioReturnMuted());
721 // Pull data until faded out.
722 GetAudioUntilMuted();
723 // Insert new data. Timestamp is over-corrected for the time elapsed since the
724 // last packet. That is, the new packet is too early. Verify that normal
725 // operation resumes.
726 InsertPacket(kSamples * counter_ * 2);
727 GetAudioUntilNormal();
728 }
729
730 // Verifies that NetEq goes out of muted state when given an old packet.
TEST_F(NetEqDecodingTestWithMutedState,MutedStateOldPacket)731 TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
732 // Insert one speech packet.
733 InsertPacket(0);
734 // Pull out audio once and expect it not to be muted.
735 EXPECT_FALSE(GetAudioReturnMuted());
736 // Pull data until faded out.
737 GetAudioUntilMuted();
738
739 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
740 // Insert packet which is older than the first packet.
741 InsertPacket(kSamples * (counter_ - 1000));
742 EXPECT_FALSE(GetAudioReturnMuted());
743 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
744 }
745
746 // Verifies that NetEq doesn't enter muted state when CNG mode is active and the
747 // packet stream is suspended for a long time.
TEST_F(NetEqDecodingTestWithMutedState,DoNotMuteExtendedCngWithoutPackets)748 TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
749 // Insert one CNG packet.
750 InsertCngPacket(0);
751
752 // Pull 10 seconds of audio (10 ms audio generated per lap).
753 for (int i = 0; i < 1000; ++i) {
754 bool muted;
755 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
756 ASSERT_FALSE(muted);
757 }
758 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
759 }
760
761 // Verifies that NetEq goes back to normal after a long CNG period with the
762 // packet stream suspended.
TEST_F(NetEqDecodingTestWithMutedState,RecoverAfterExtendedCngWithoutPackets)763 TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
764 // Insert one CNG packet.
765 InsertCngPacket(0);
766
767 // Pull 10 seconds of audio (10 ms audio generated per lap).
768 for (int i = 0; i < 1000; ++i) {
769 bool muted;
770 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
771 }
772
773 // Insert new data. Timestamp is corrected for the time elapsed since the last
774 // packet. Verify that normal operation resumes.
775 InsertPacket(kSamples * counter_);
776 GetAudioUntilNormal();
777 }
778
779 namespace {
AudioFramesEqualExceptData(const AudioFrame & a,const AudioFrame & b)780 ::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
781 const AudioFrame& b) {
782 if (a.timestamp_ != b.timestamp_)
783 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
784 << " != " << b.timestamp_ << ")";
785 if (a.sample_rate_hz_ != b.sample_rate_hz_)
786 return ::testing::AssertionFailure()
787 << "sample_rate_hz_ diff (" << a.sample_rate_hz_
788 << " != " << b.sample_rate_hz_ << ")";
789 if (a.samples_per_channel_ != b.samples_per_channel_)
790 return ::testing::AssertionFailure()
791 << "samples_per_channel_ diff (" << a.samples_per_channel_
792 << " != " << b.samples_per_channel_ << ")";
793 if (a.num_channels_ != b.num_channels_)
794 return ::testing::AssertionFailure()
795 << "num_channels_ diff (" << a.num_channels_
796 << " != " << b.num_channels_ << ")";
797 if (a.speech_type_ != b.speech_type_)
798 return ::testing::AssertionFailure()
799 << "speech_type_ diff (" << a.speech_type_
800 << " != " << b.speech_type_ << ")";
801 if (a.vad_activity_ != b.vad_activity_)
802 return ::testing::AssertionFailure()
803 << "vad_activity_ diff (" << a.vad_activity_
804 << " != " << b.vad_activity_ << ")";
805 return ::testing::AssertionSuccess();
806 }
807
AudioFramesEqual(const AudioFrame & a,const AudioFrame & b)808 ::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
809 const AudioFrame& b) {
810 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
811 if (!res)
812 return res;
813 if (memcmp(a.data(), b.data(),
814 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) !=
815 0) {
816 return ::testing::AssertionFailure() << "data_ diff";
817 }
818 return ::testing::AssertionSuccess();
819 }
820
821 } // namespace
822
TEST_F(NetEqDecodingTestTwoInstances,CompareMutedStateOnOff)823 TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
824 ASSERT_FALSE(config_.enable_muted_state);
825 config2_.enable_muted_state = true;
826 CreateSecondInstance();
827
828 // Insert one speech packet into both NetEqs.
829 const size_t kSamples = 10 * 16;
830 const size_t kPayloadBytes = kSamples * 2;
831 uint8_t payload[kPayloadBytes] = {0};
832 RTPHeader rtp_info;
833 PopulateRtpInfo(0, 0, &rtp_info);
834 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
835 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
836
837 AudioFrame out_frame1, out_frame2;
838 bool muted;
839 for (int i = 0; i < 1000; ++i) {
840 rtc::StringBuilder ss;
841 ss << "i = " << i;
842 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
843 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
844 EXPECT_FALSE(muted);
845 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
846 if (muted) {
847 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
848 } else {
849 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
850 }
851 }
852 EXPECT_TRUE(muted);
853
854 // Insert new data. Timestamp is corrected for the time elapsed since the last
855 // packet.
856 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
857 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
858 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload));
859
860 int counter = 0;
861 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
862 ASSERT_LT(counter++, 1000) << "Test timed out";
863 rtc::StringBuilder ss;
864 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
TEST_F(NetEqDecodingTest,LastDecodedTimestampsEmpty)878 TEST_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
TEST_F(NetEqDecodingTest,LastDecodedTimestampsOneDecoded)889 TEST_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);
899 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
900
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
TEST_F(NetEqDecodingTest,LastDecodedTimestampsTwoDecoded)914 TEST_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);
925 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
926 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
927 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
928 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload));
929
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
TEST_F(NetEqDecodingTest,TestConcealmentEvents)939 TEST_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;
956 neteq_->InsertPacket(rtp_info, payload);
957 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
973 // Test that the jitter buffer delay stat is computed correctly.
TestJitterBufferDelay(bool apply_packet_loss)974 void 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;
989 int expected_target_delay = 0;
990 uint64_t expected_emitted_count = 0;
991 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;
996 neteq_->InsertPacket(rtp_info, payload);
997 }
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;
1014 expected_target_delay += neteq_->TargetDelayMs() * kSamples;
1015 expected_emitted_count += kSamples;
1016 }
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,
1027 rtc::checked_cast<int>(stats.jitter_buffer_delay_ms));
1028 EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count);
1029 EXPECT_EQ(expected_target_delay,
1030 rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms));
1031 }
1032
TEST_F(NetEqDecodingTestFaxMode,TestJitterBufferDelayWithoutLoss)1033 TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1034 TestJitterBufferDelay(false);
1035 }
1036
TEST_F(NetEqDecodingTestFaxMode,TestJitterBufferDelayWithLoss)1037 TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1038 TestJitterBufferDelay(true);
1039 }
1040
TEST_F(NetEqDecodingTestFaxMode,TestJitterBufferDelayWithAcceleration)1041 TEST_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
1051 int expected_target_delay = neteq_->TargetDelayMs() * kSamples;
1052 neteq_->InsertPacket(rtp_info, payload);
1053
1054 bool muted;
1055 neteq_->GetAudio(&out_frame_, &muted);
1056
1057 rtp_info.sequenceNumber += 1;
1058 rtp_info.timestamp += kSamples;
1059 neteq_->InsertPacket(rtp_info, payload);
1060 rtp_info.sequenceNumber += 1;
1061 rtp_info.timestamp += kSamples;
1062 neteq_->InsertPacket(rtp_info, payload);
1063
1064 expected_target_delay += neteq_->TargetDelayMs() * 2 * kSamples;
1065 // We have two packets in the buffer and kAccelerate operation will
1066 // extract 20 ms of data.
1067 neteq_->GetAudio(&out_frame_, &muted, NetEq::Operation::kAccelerate);
1068
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);
1073 EXPECT_EQ(expected_target_delay,
1074 rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms));
1075 }
1076
1077 namespace test {
TEST(NetEqNoTimeStretchingMode,RunTest)1078 TEST(NetEqNoTimeStretchingMode, RunTest) {
1079 NetEq::Config config;
1080 config.for_test_no_time_stretching = true;
1081 auto codecs = NetEqTest::StandardDecoderMap();
1082 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"),
1090 rtp_ext_map, absl::nullopt /*No SSRC filter*/));
1091 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;
1095 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs,
1096 /*text_log=*/nullptr, /*neteq_factory=*/nullptr,
1097 /*input=*/std::move(input_time_limit), std::move(output),
1098 callbacks);
1099 test.Run();
1100 const auto stats = test.SimulationStats();
1101 EXPECT_EQ(0, stats.accelerate_rate);
1102 EXPECT_EQ(0, stats.preemptive_rate);
1103 }
1104
1105 } // namespace test
1106 } // namespace webrtc
1107