blob: 1a54c54c39453e0faefe821c62738348cfd97a03 [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
Henrik Kjellander74640892015-10-29 11:31:02 +010011#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000023#include "gflags/gflags.h"
kwiberg087bd342017-02-10 08:15:44 -080024#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
kwiberg77eab702016-09-28 17:42:01 -070025#include "webrtc/base/ignore_wundef.h"
minyue4f906772016-04-29 11:05:14 -070026#include "webrtc/base/sha1digest.h"
27#include "webrtc/base/stringencode.h"
mbonadei7c2c8432017-04-07 00:59:12 -070028#include "webrtc/base/protobuf_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070029#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000030#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000031#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080032#include "webrtc/modules/include/module_common_types.h"
kwibergac9f8762016-09-30 22:29:43 -070033#include "webrtc/test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034#include "webrtc/test/testsupport/fileutils.h"
35#include "webrtc/typedefs.h"
36
minyue5f026d02015-12-16 07:36:04 -080037#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070038RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080039#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
40#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
41#else
kjellandere3e902e2017-02-28 08:01:46 -080042#include "webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080043#endif
kwiberg77eab702016-09-28 17:42:01 -070044RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080045#endif
46
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000047DEFINE_bool(gen_ref, false, "Generate reference files.");
48
kwiberg5adaf732016-10-04 09:33:27 -070049namespace webrtc {
50
minyue5f026d02015-12-16 07:36:04 -080051namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
minyue4f906772016-04-29 11:05:14 -070053const std::string& PlatformChecksum(const std::string& checksum_general,
54 const std::string& checksum_android,
55 const std::string& checksum_win_32,
56 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070057#if defined(WEBRTC_ANDROID)
minyue4f906772016-04-29 11:05:14 -070058 return checksum_android;
kwiberg77eab702016-09-28 17:42:01 -070059#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070060 #ifdef WEBRTC_ARCH_64_BITS
61 return checksum_win_64;
62 #else
63 return checksum_win_32;
64 #endif // WEBRTC_ARCH_64_BITS
65#else
66 return checksum_general;
67#endif // WEBRTC_WIN
68}
69
minyue5f026d02015-12-16 07:36:04 -080070#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
71void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
72 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
73 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
74 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
75 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
76 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
77 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
78 stats->set_expand_rate(stats_raw.expand_rate);
79 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
80 stats->set_preemptive_rate(stats_raw.preemptive_rate);
81 stats->set_accelerate_rate(stats_raw.accelerate_rate);
82 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
83 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
84 stats->set_added_zero_samples(stats_raw.added_zero_samples);
85 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
86 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
87 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
88 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
89}
90
91void Convert(const webrtc::RtcpStatistics& stats_raw,
92 webrtc::neteq_unittest::RtcpStatistics* stats) {
93 stats->set_fraction_lost(stats_raw.fraction_lost);
94 stats->set_cumulative_lost(stats_raw.cumulative_lost);
95 stats->set_extended_max_sequence_number(
96 stats_raw.extended_max_sequence_number);
97 stats->set_jitter(stats_raw.jitter);
98}
99
minyue4f906772016-04-29 11:05:14 -0700100void AddMessage(FILE* file, rtc::MessageDigest* digest,
101 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800102 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700103 if (file)
104 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
105 digest->Update(&size, sizeof(size));
106
107 if (file)
108 ASSERT_EQ(static_cast<size_t>(size),
109 fwrite(message.data(), sizeof(char), size, file));
110 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800111}
112
minyue5f026d02015-12-16 07:36:04 -0800113#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
114
henrik.lundin7a926812016-05-12 13:51:28 -0700115void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700116 ASSERT_EQ(true,
117 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
118 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
119 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700120 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
121 "pcma", 8));
122#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700123 ASSERT_EQ(true,
124 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700125#endif
126#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700127 ASSERT_EQ(true,
128 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700129#endif
130#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700131 ASSERT_EQ(true,
132 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700133#endif
134#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700135 ASSERT_EQ(true,
136 neteq->RegisterPayloadType(
137 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700138#endif
kwiberg5adaf732016-10-04 09:33:27 -0700139 ASSERT_EQ(true,
140 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
141 ASSERT_EQ(true,
142 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
143 ASSERT_EQ(true,
144 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
145 ASSERT_EQ(true,
146 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
147 ASSERT_EQ(true,
148 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700149}
minyue5f026d02015-12-16 07:36:04 -0800150} // namespace
151
minyue4f906772016-04-29 11:05:14 -0700152class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000153 public:
minyue4f906772016-04-29 11:05:14 -0700154 explicit ResultSink(const std::string& output_file);
155 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156
minyue4f906772016-04-29 11:05:14 -0700157 template<typename T, size_t n> void AddResult(
158 const T (&test_results)[n],
159 size_t length);
160
161 void AddResult(const NetEqNetworkStatistics& stats);
162 void AddResult(const RtcpStatistics& stats);
163
164 void VerifyChecksum(const std::string& ref_check_sum);
165
166 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700168 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169};
170
minyue4f906772016-04-29 11:05:14 -0700171ResultSink::ResultSink(const std::string &output_file)
172 : output_fp_(nullptr),
173 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 if (!output_file.empty()) {
175 output_fp_ = fopen(output_file.c_str(), "wb");
176 EXPECT_TRUE(output_fp_ != NULL);
177 }
178}
179
minyue4f906772016-04-29 11:05:14 -0700180ResultSink::~ResultSink() {
181 if (output_fp_)
182 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000183}
184
185template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700186void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 if (output_fp_) {
188 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
189 }
minyue4f906772016-04-29 11:05:14 -0700190 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191}
192
minyue4f906772016-04-29 11:05:14 -0700193void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800194#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800195 neteq_unittest::NetEqNetworkStatistics stats;
196 Convert(stats_raw, &stats);
197
mbonadei7c2c8432017-04-07 00:59:12 -0700198 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800199 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700200 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800201#else
202 FAIL() << "Writing to reference file requires Proto Buffer.";
203#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204}
205
minyue4f906772016-04-29 11:05:14 -0700206void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800207#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800208 neteq_unittest::RtcpStatistics stats;
209 Convert(stats_raw, &stats);
210
mbonadei7c2c8432017-04-07 00:59:12 -0700211 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800212 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700213 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800214#else
215 FAIL() << "Writing to reference file requires Proto Buffer.";
216#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000217}
218
minyue4f906772016-04-29 11:05:14 -0700219void ResultSink::VerifyChecksum(const std::string& checksum) {
220 std::vector<char> buffer;
221 buffer.resize(digest_->Size());
222 digest_->Finish(&buffer[0], buffer.size());
223 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
224 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225}
226
227class NetEqDecodingTest : public ::testing::Test {
228 protected:
229 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
230 // constants below can be changed.
231 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700232 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
233 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
234 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800235 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236 static const int kInitSampleRateHz = 8000;
237
238 NetEqDecodingTest();
239 virtual void SetUp();
240 virtual void TearDown();
241 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800243 void Process();
minyue5f026d02015-12-16 07:36:04 -0800244
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000245 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700246 const std::string& output_checksum,
247 const std::string& network_stats_checksum,
248 const std::string& rtcp_stats_checksum,
249 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800250
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251 static void PopulateRtpInfo(int frame_index,
252 int timestamp,
253 WebRtcRTPHeader* rtp_info);
254 static void PopulateCng(int frame_index,
255 int timestamp,
256 WebRtcRTPHeader* rtp_info,
257 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000258 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000260 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
261 const std::set<uint16_t>& drop_seq_numbers,
262 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
263
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000264 void LongCngWithClockDrift(double drift_factor,
265 double network_freeze_ms,
266 bool pull_audio_during_freeze,
267 int delay_tolerance_ms,
268 int max_time_to_speech_ms);
269
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000270 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000271
henrik.lundin0d96ab72016-04-06 12:28:26 -0700272 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000273
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000275 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800276 std::unique_ptr<test::RtpFileSource> rtp_source_;
277 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800279 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000281 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282};
283
284// Allocating the static const so that it can be passed by reference.
285const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700286const size_t NetEqDecodingTest::kBlockSize8kHz;
287const size_t NetEqDecodingTest::kBlockSize16kHz;
288const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289const int NetEqDecodingTest::kInitSampleRateHz;
290
291NetEqDecodingTest::NetEqDecodingTest()
292 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000293 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000295 output_sample_rate_(kInitSampleRateHz),
296 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000297 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298}
299
300void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700301 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000302 NetEqNetworkStatistics stat;
303 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
304 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700306 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307}
308
309void NetEqDecodingTest::TearDown() {
310 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311}
312
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000314 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315}
316
henrik.lundin6d8e0112016-03-04 10:34:21 -0800317void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000318 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000319 while (packet_ && sim_clock_ >= packet_->time_ms()) {
320 if (packet_->payload_length_bytes() > 0) {
321 WebRtcRTPHeader rtp_header;
322 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800323#ifndef WEBRTC_CODEC_ISAC
324 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
325 if (rtp_header.header.payloadType != 104)
326#endif
henrik.lundin4d027572017-04-11 06:17:46 -0700327 ASSERT_EQ(0,
328 neteq_->InsertPacket(
329 rtp_header.header,
330 rtc::ArrayView<const uint8_t>(
331 packet_->payload(), packet_->payload_length_bytes()),
332 static_cast<uint32_t>(packet_->time_ms() *
333 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 }
335 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700336 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337 }
338
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000339 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700340 bool muted;
341 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
342 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800343 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
344 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
345 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
346 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
347 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800348 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349
350 // Increase time.
351 sim_clock_ += kTimeStepMs;
352}
353
minyue4f906772016-04-29 11:05:14 -0700354void NetEqDecodingTest::DecodeAndCompare(
355 const std::string& rtp_file,
356 const std::string& output_checksum,
357 const std::string& network_stats_checksum,
358 const std::string& rtcp_stats_checksum,
359 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 OpenInputFile(rtp_file);
361
minyue4f906772016-04-29 11:05:14 -0700362 std::string ref_out_file =
363 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
364 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365
minyue4f906772016-04-29 11:05:14 -0700366 std::string stat_out_file =
367 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
368 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000369
minyue4f906772016-04-29 11:05:14 -0700370 std::string rtcp_out_file =
371 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
372 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000373
henrik.lundin46ba49c2016-05-24 22:50:47 -0700374 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000376 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 std::ostringstream ss;
378 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
379 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800380 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700381 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800382 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383
384 // Query the network statistics API once per second
385 if (sim_clock_ % 1000 == 0) {
386 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700387 NetEqNetworkStatistics current_network_stats;
388 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
389 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
390
henrik.lundin9c3efd02015-08-27 13:12:22 -0700391 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700392 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
393 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394
395 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700396 RtcpStatistics current_rtcp_stats;
397 neteq_->GetRtcpStatistics(&current_rtcp_stats);
398 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399 }
400 }
minyue4f906772016-04-29 11:05:14 -0700401
402 SCOPED_TRACE("Check output audio.");
403 output.VerifyChecksum(output_checksum);
404 SCOPED_TRACE("Check network stats.");
405 network_stats.VerifyChecksum(network_stats_checksum);
406 SCOPED_TRACE("Check rtcp stats.");
407 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000408}
409
410void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
411 int timestamp,
412 WebRtcRTPHeader* rtp_info) {
413 rtp_info->header.sequenceNumber = frame_index;
414 rtp_info->header.timestamp = timestamp;
415 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
416 rtp_info->header.payloadType = 94; // PCM16b WB codec.
417 rtp_info->header.markerBit = 0;
418}
419
420void NetEqDecodingTest::PopulateCng(int frame_index,
421 int timestamp,
422 WebRtcRTPHeader* rtp_info,
423 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000424 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425 rtp_info->header.sequenceNumber = frame_index;
426 rtp_info->header.timestamp = timestamp;
427 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
428 rtp_info->header.payloadType = 98; // WB CNG.
429 rtp_info->header.markerBit = 0;
430 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
431 *payload_len = 1; // Only noise level, no spectral parameters.
432}
433
ivoc72c08ed2016-01-20 07:26:24 -0800434#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
435 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
436 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700437 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800438#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700439#else
minyue5f026d02015-12-16 07:36:04 -0800440#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700441#endif
minyue5f026d02015-12-16 07:36:04 -0800442TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800443 const std::string input_rtp_file =
444 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000445
minyue4f906772016-04-29 11:05:14 -0700446 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700447 "09fa7646e2ad032a0b156177b95f09012430f81f",
448 "1c64eb8b55ce8878676c6a1e6ddd78f48de0668b",
449 "09fa7646e2ad032a0b156177b95f09012430f81f",
450 "759fef89a5de52bd17e733dc255c671ce86be909");
minyue4f906772016-04-29 11:05:14 -0700451
452 const std::string network_stats_checksum = PlatformChecksum(
henrik.lundin0d838572016-10-13 03:35:55 -0700453 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f",
454 "c8b2a93842e48d014f7e6efe10ae96cb3892b129",
455 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f",
456 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f");
minyue4f906772016-04-29 11:05:14 -0700457
458 const std::string rtcp_stats_checksum = PlatformChecksum(
459 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
460 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
461 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
462 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
463
464 DecodeAndCompare(input_rtp_file,
465 output_checksum,
466 network_stats_checksum,
467 rtcp_stats_checksum,
468 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000469}
470
minyue93c08b72015-12-22 09:57:41 -0800471#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
472 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyuea613eb62017-03-14 14:33:30 -0700473 defined(WEBRTC_CODEC_OPUS) && \
474 !WEBRTC_OPUS_SUPPORT_120MS_PTIME
minyue93c08b72015-12-22 09:57:41 -0800475#define MAYBE_TestOpusBitExactness TestOpusBitExactness
476#else
477#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
478#endif
flim64a7eab2016-08-12 04:36:05 -0700479TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800480 const std::string input_rtp_file =
481 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800482
minyue4f906772016-04-29 11:05:14 -0700483 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700484 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
485 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
486 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
487 "6237dd113ad80d7764fe4c90b55b2ec035eae64e");
minyue4f906772016-04-29 11:05:14 -0700488
489 const std::string network_stats_checksum = PlatformChecksum(
henrik.lundin0d838572016-10-13 03:35:55 -0700490 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
491 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
492 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
493 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8");
minyue4f906772016-04-29 11:05:14 -0700494
495 const std::string rtcp_stats_checksum = PlatformChecksum(
496 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
497 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
498 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
499 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
500
501 DecodeAndCompare(input_rtp_file,
502 output_checksum,
503 network_stats_checksum,
504 rtcp_stats_checksum,
505 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800506}
507
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000508// Use fax mode to avoid time-scaling. This is to simplify the testing of
509// packet waiting times in the packet buffer.
510class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
511 protected:
512 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
513 config_.playout_mode = kPlayoutFax;
514 }
515};
516
517TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000518 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
519 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000520 const size_t kSamples = 10 * 16;
521 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000522 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800523 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000524 WebRtcRTPHeader rtp_info;
525 rtp_info.header.sequenceNumber = i;
526 rtp_info.header.timestamp = i * kSamples;
527 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
528 rtp_info.header.payloadType = 94; // PCM16b WB codec.
529 rtp_info.header.markerBit = 0;
henrik.lundin4d027572017-04-11 06:17:46 -0700530 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000531 }
532 // Pull out all data.
533 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700534 bool muted;
535 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800536 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000537 }
538
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200539 NetEqNetworkStatistics stats;
540 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000541 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
542 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200543 // each packet. Thus, we are calculating the statistics for a series from 10
544 // to 300, in steps of 10 ms.
545 EXPECT_EQ(155, stats.mean_waiting_time_ms);
546 EXPECT_EQ(155, stats.median_waiting_time_ms);
547 EXPECT_EQ(10, stats.min_waiting_time_ms);
548 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000549
550 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200551 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
552 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
553 EXPECT_EQ(-1, stats.median_waiting_time_ms);
554 EXPECT_EQ(-1, stats.min_waiting_time_ms);
555 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000556}
557
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000558TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559 const int kNumFrames = 3000; // Needed for convergence.
560 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000561 const size_t kSamples = 10 * 16;
562 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000563 while (frame_index < kNumFrames) {
564 // Insert one packet each time, except every 10th time where we insert two
565 // packets at once. This will create a negative clock-drift of approx. 10%.
566 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
567 for (int n = 0; n < num_packets; ++n) {
568 uint8_t payload[kPayloadBytes] = {0};
569 WebRtcRTPHeader rtp_info;
570 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -0700571 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572 ++frame_index;
573 }
574
575 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700576 bool muted;
577 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800578 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 }
580
581 NetEqNetworkStatistics network_stats;
582 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700583 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000584}
585
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000586TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587 const int kNumFrames = 5000; // Needed for convergence.
588 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000589 const size_t kSamples = 10 * 16;
590 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 for (int i = 0; i < kNumFrames; ++i) {
592 // Insert one packet each time, except every 10th time where we don't insert
593 // any packet. This will create a positive clock-drift of approx. 11%.
594 int num_packets = (i % 10 == 9 ? 0 : 1);
595 for (int n = 0; n < num_packets; ++n) {
596 uint8_t payload[kPayloadBytes] = {0};
597 WebRtcRTPHeader rtp_info;
598 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -0700599 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000600 ++frame_index;
601 }
602
603 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700604 bool muted;
605 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800606 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 }
608
609 NetEqNetworkStatistics network_stats;
610 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700611 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612}
613
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000614void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
615 double network_freeze_ms,
616 bool pull_audio_during_freeze,
617 int delay_tolerance_ms,
618 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 uint16_t seq_no = 0;
620 uint32_t timestamp = 0;
621 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000622 const size_t kSamples = kFrameSizeMs * 16;
623 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000624 double next_input_time_ms = 0.0;
625 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700626 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000627
628 // Insert speech for 5 seconds.
629 const int kSpeechDurationMs = 5000;
630 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
631 // Each turn in this for loop is 10 ms.
632 while (next_input_time_ms <= t_ms) {
633 // Insert one 30 ms speech frame.
634 uint8_t payload[kPayloadBytes] = {0};
635 WebRtcRTPHeader rtp_info;
636 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -0700637 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000638 ++seq_no;
639 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000640 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000641 }
642 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700643 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800644 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000645 }
646
henrik.lundin55480f52016-03-08 02:37:57 -0800647 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700648 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
649 ASSERT_TRUE(playout_timestamp);
650 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000651
652 // Insert CNG for 1 minute (= 60000 ms).
653 const int kCngPeriodMs = 100;
654 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
655 const int kCngDurationMs = 60000;
656 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
657 // Each turn in this for loop is 10 ms.
658 while (next_input_time_ms <= t_ms) {
659 // Insert one CNG frame each 100 ms.
660 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000661 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000662 WebRtcRTPHeader rtp_info;
663 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800664 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin4d027572017-04-11 06:17:46 -0700665 rtp_info.header,
kwibergee2bac22015-11-11 10:34:00 -0800666 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000667 ++seq_no;
668 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000669 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000670 }
671 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700672 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800673 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000674 }
675
henrik.lundin55480f52016-03-08 02:37:57 -0800676 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000678 if (network_freeze_ms > 0) {
679 // First keep pulling audio for |network_freeze_ms| without inserting
680 // any data, then insert CNG data corresponding to |network_freeze_ms|
681 // without pulling any output audio.
682 const double loop_end_time = t_ms + network_freeze_ms;
683 for (; t_ms < loop_end_time; t_ms += 10) {
684 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700685 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800686 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800687 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000688 }
689 bool pull_once = pull_audio_during_freeze;
690 // If |pull_once| is true, GetAudio will be called once half-way through
691 // the network recovery period.
692 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
693 while (next_input_time_ms <= t_ms) {
694 if (pull_once && next_input_time_ms >= pull_time_ms) {
695 pull_once = false;
696 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700697 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800698 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800699 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000700 t_ms += 10;
701 }
702 // Insert one CNG frame each 100 ms.
703 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000704 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000705 WebRtcRTPHeader rtp_info;
706 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800707 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin4d027572017-04-11 06:17:46 -0700708 rtp_info.header,
kwibergee2bac22015-11-11 10:34:00 -0800709 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000710 ++seq_no;
711 timestamp += kCngPeriodSamples;
712 next_input_time_ms += kCngPeriodMs * drift_factor;
713 }
714 }
715
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000716 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000717 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800718 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000719 // Each turn in this for loop is 10 ms.
720 while (next_input_time_ms <= t_ms) {
721 // Insert one 30 ms speech frame.
722 uint8_t payload[kPayloadBytes] = {0};
723 WebRtcRTPHeader rtp_info;
724 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -0700725 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000726 ++seq_no;
727 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000728 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000729 }
730 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700731 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800732 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733 // Increase clock.
734 t_ms += 10;
735 }
736
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000737 // Check that the speech starts again within reasonable time.
738 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
739 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700740 playout_timestamp = PlayoutTimestamp();
741 ASSERT_TRUE(playout_timestamp);
742 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000743 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000744 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
745 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000746}
747
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000748TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000749 // Apply a clock drift of -25 ms / s (sender faster than receiver).
750 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000751 const double kNetworkFreezeTimeMs = 0.0;
752 const bool kGetAudioDuringFreezeRecovery = false;
753 const int kDelayToleranceMs = 20;
754 const int kMaxTimeToSpeechMs = 100;
755 LongCngWithClockDrift(kDriftFactor,
756 kNetworkFreezeTimeMs,
757 kGetAudioDuringFreezeRecovery,
758 kDelayToleranceMs,
759 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000760}
761
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000762TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000763 // Apply a clock drift of +25 ms / s (sender slower than receiver).
764 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000765 const double kNetworkFreezeTimeMs = 0.0;
766 const bool kGetAudioDuringFreezeRecovery = false;
767 const int kDelayToleranceMs = 20;
768 const int kMaxTimeToSpeechMs = 100;
769 LongCngWithClockDrift(kDriftFactor,
770 kNetworkFreezeTimeMs,
771 kGetAudioDuringFreezeRecovery,
772 kDelayToleranceMs,
773 kMaxTimeToSpeechMs);
774}
775
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000776TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000777 // Apply a clock drift of -25 ms / s (sender faster than receiver).
778 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
779 const double kNetworkFreezeTimeMs = 5000.0;
780 const bool kGetAudioDuringFreezeRecovery = false;
781 const int kDelayToleranceMs = 50;
782 const int kMaxTimeToSpeechMs = 200;
783 LongCngWithClockDrift(kDriftFactor,
784 kNetworkFreezeTimeMs,
785 kGetAudioDuringFreezeRecovery,
786 kDelayToleranceMs,
787 kMaxTimeToSpeechMs);
788}
789
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000790TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000791 // Apply a clock drift of +25 ms / s (sender slower than receiver).
792 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
793 const double kNetworkFreezeTimeMs = 5000.0;
794 const bool kGetAudioDuringFreezeRecovery = false;
795 const int kDelayToleranceMs = 20;
796 const int kMaxTimeToSpeechMs = 100;
797 LongCngWithClockDrift(kDriftFactor,
798 kNetworkFreezeTimeMs,
799 kGetAudioDuringFreezeRecovery,
800 kDelayToleranceMs,
801 kMaxTimeToSpeechMs);
802}
803
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000804TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000805 // Apply a clock drift of +25 ms / s (sender slower than receiver).
806 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
807 const double kNetworkFreezeTimeMs = 5000.0;
808 const bool kGetAudioDuringFreezeRecovery = true;
809 const int kDelayToleranceMs = 20;
810 const int kMaxTimeToSpeechMs = 100;
811 LongCngWithClockDrift(kDriftFactor,
812 kNetworkFreezeTimeMs,
813 kGetAudioDuringFreezeRecovery,
814 kDelayToleranceMs,
815 kMaxTimeToSpeechMs);
816}
817
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000818TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000819 const double kDriftFactor = 1.0; // No drift.
820 const double kNetworkFreezeTimeMs = 0.0;
821 const bool kGetAudioDuringFreezeRecovery = false;
822 const int kDelayToleranceMs = 10;
823 const int kMaxTimeToSpeechMs = 50;
824 LongCngWithClockDrift(kDriftFactor,
825 kNetworkFreezeTimeMs,
826 kGetAudioDuringFreezeRecovery,
827 kDelayToleranceMs,
828 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000829}
830
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000831TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000832 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000833 uint8_t payload[kPayloadBytes] = {0};
834 WebRtcRTPHeader rtp_info;
835 PopulateRtpInfo(0, 0, &rtp_info);
836 rtp_info.header.payloadType = 1; // Not registered as a decoder.
henrik.lundin4d027572017-04-11 06:17:46 -0700837 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
839}
840
Peter Boströme2976c82016-01-04 22:44:05 +0100841#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800842#define MAYBE_DecoderError DecoderError
843#else
844#define MAYBE_DecoderError DISABLED_DecoderError
845#endif
846
Peter Boströme2976c82016-01-04 22:44:05 +0100847TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000848 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 uint8_t payload[kPayloadBytes] = {0};
850 WebRtcRTPHeader rtp_info;
851 PopulateRtpInfo(0, 0, &rtp_info);
852 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
henrik.lundin4d027572017-04-11 06:17:46 -0700853 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
855 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800856 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
857 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000858 }
henrik.lundin7a926812016-05-12 13:51:28 -0700859 bool muted;
860 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
861 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000862 // Verify that there is a decoder error to check.
863 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800864
865 enum NetEqDecoderError {
866 ISAC_LENGTH_MISMATCH = 6730,
867 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
868 };
869#if defined(WEBRTC_CODEC_ISAC)
870 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
871#elif defined(WEBRTC_CODEC_ISACFX)
872 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
873#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000874 // Verify that the first 160 samples are set to 0, and that the remaining
875 // samples are left unmodified.
876 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
877 for (int i = 0; i < kExpectedOutputLength; ++i) {
878 std::ostringstream ss;
879 ss << "i = " << i;
880 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800881 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800883 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
884 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000885 std::ostringstream ss;
886 ss << "i = " << i;
887 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800888 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 }
890}
891
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000892TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000893 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
894 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800895 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
896 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000897 }
henrik.lundin7a926812016-05-12 13:51:28 -0700898 bool muted;
899 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
900 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000901 // Verify that the first block of samples is set to 0.
902 static const int kExpectedOutputLength =
903 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
904 for (int i = 0; i < kExpectedOutputLength; ++i) {
905 std::ostringstream ss;
906 ss << "i = " << i;
907 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800908 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 }
henrik.lundind89814b2015-11-23 06:49:25 -0800910 // Verify that the sample rate did not change from the initial configuration.
911 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000913
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000914class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000915 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000916 virtual void TestCondition(double sum_squared_noise,
917 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000918
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000919 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700920 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000921 uint8_t payload_type = 0xFF; // Invalid.
922 if (sampling_rate_hz == 8000) {
923 expected_samples_per_channel = kBlockSize8kHz;
924 payload_type = 93; // PCM 16, 8 kHz.
925 } else if (sampling_rate_hz == 16000) {
926 expected_samples_per_channel = kBlockSize16kHz;
927 payload_type = 94; // PCM 16, 16 kHZ.
928 } else if (sampling_rate_hz == 32000) {
929 expected_samples_per_channel = kBlockSize32kHz;
930 payload_type = 95; // PCM 16, 32 kHz.
931 } else {
932 ASSERT_TRUE(false); // Unsupported test case.
933 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000934
henrik.lundin6d8e0112016-03-04 10:34:21 -0800935 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000936 test::AudioLoop input;
937 // We are using the same 32 kHz input file for all tests, regardless of
938 // |sampling_rate_hz|. The output may sound weird, but the test is still
939 // valid.
940 ASSERT_TRUE(input.Init(
941 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
942 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700943 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000944
945 // Payload of 10 ms of PCM16 32 kHz.
946 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000947 WebRtcRTPHeader rtp_info;
948 PopulateRtpInfo(0, 0, &rtp_info);
949 rtp_info.header.payloadType = payload_type;
950
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000951 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700952 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000953 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800954 auto block = input.GetNextBlock();
955 ASSERT_EQ(expected_samples_per_channel, block.size());
956 size_t enc_len_bytes =
957 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000958 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
959
henrik.lundin4d027572017-04-11 06:17:46 -0700960 ASSERT_EQ(0, neteq_->InsertPacket(
961 rtp_info.header,
962 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
963 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800964 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700965 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800966 ASSERT_EQ(1u, output.num_channels_);
967 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800968 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000969
970 // Next packet.
971 rtp_info.header.timestamp += expected_samples_per_channel;
972 rtp_info.header.sequenceNumber++;
973 receive_timestamp += expected_samples_per_channel;
974 }
975
henrik.lundin6d8e0112016-03-04 10:34:21 -0800976 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000977
978 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
979 // one frame without checking speech-type. This is the first frame pulled
980 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700981 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800982 ASSERT_EQ(1u, output.num_channels_);
983 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000984
985 // To be able to test the fading of background noise we need at lease to
986 // pull 611 frames.
987 const int kFadingThreshold = 611;
988
989 // Test several CNG-to-PLC packet for the expected behavior. The number 20
990 // is arbitrary, but sufficiently large to test enough number of frames.
991 const int kNumPlcToCngTestFrames = 20;
992 bool plc_to_cng = false;
993 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800994 output.Reset();
995 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin7a926812016-05-12 13:51:28 -0700996 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
997 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800998 ASSERT_EQ(1u, output.num_channels_);
999 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001000 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001001 plc_to_cng = true;
1002 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001003 for (size_t k = 0;
1004 k < output.num_channels_ * output.samples_per_channel_; ++k)
1005 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001006 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001007 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001008 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001009 }
1010 }
1011 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1012 }
1013};
1014
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001015class NetEqBgnTestOn : public NetEqBgnTest {
1016 protected:
1017 NetEqBgnTestOn() : NetEqBgnTest() {
1018 config_.background_noise_mode = NetEq::kBgnOn;
1019 }
1020
1021 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1022 EXPECT_NE(0, sum_squared_noise);
1023 }
1024};
1025
1026class NetEqBgnTestOff : public NetEqBgnTest {
1027 protected:
1028 NetEqBgnTestOff() : NetEqBgnTest() {
1029 config_.background_noise_mode = NetEq::kBgnOff;
1030 }
1031
1032 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1033 EXPECT_EQ(0, sum_squared_noise);
1034 }
1035};
1036
1037class NetEqBgnTestFade : public NetEqBgnTest {
1038 protected:
1039 NetEqBgnTestFade() : NetEqBgnTest() {
1040 config_.background_noise_mode = NetEq::kBgnFade;
1041 }
1042
1043 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1044 if (should_be_faded)
1045 EXPECT_EQ(0, sum_squared_noise);
1046 }
1047};
1048
henrika1d34fe92015-06-16 10:04:20 +02001049TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001050 CheckBgn(8000);
1051 CheckBgn(16000);
1052 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001053}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001054
henrika1d34fe92015-06-16 10:04:20 +02001055TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001056 CheckBgn(8000);
1057 CheckBgn(16000);
1058 CheckBgn(32000);
1059}
1060
henrika1d34fe92015-06-16 10:04:20 +02001061TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001062 CheckBgn(8000);
1063 CheckBgn(16000);
1064 CheckBgn(32000);
1065}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001066
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001067void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1068 uint32_t start_timestamp,
1069 const std::set<uint16_t>& drop_seq_numbers,
1070 bool expect_seq_no_wrap,
1071 bool expect_timestamp_wrap) {
1072 uint16_t seq_no = start_seq_no;
1073 uint32_t timestamp = start_timestamp;
1074 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1075 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1076 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001077 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001078 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001079 uint32_t receive_timestamp = 0;
1080
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001081 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001082 const int kSpeechDurationMs = 2000;
1083 int packets_inserted = 0;
1084 uint16_t last_seq_no;
1085 uint32_t last_timestamp;
1086 bool timestamp_wrapped = false;
1087 bool seq_no_wrapped = false;
1088 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1089 // Each turn in this for loop is 10 ms.
1090 while (next_input_time_ms <= t_ms) {
1091 // Insert one 30 ms speech frame.
1092 uint8_t payload[kPayloadBytes] = {0};
1093 WebRtcRTPHeader rtp_info;
1094 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1095 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1096 // This sequence number was not in the set to drop. Insert it.
henrik.lundin4d027572017-04-11 06:17:46 -07001097 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload,
1098 receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001099 ++packets_inserted;
1100 }
1101 NetEqNetworkStatistics network_stats;
1102 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1103
1104 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1105 // packet size for first few packets. Therefore we refrain from checking
1106 // the criteria.
1107 if (packets_inserted > 4) {
1108 // Expect preferred and actual buffer size to be no more than 2 frames.
1109 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001110 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1111 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001112 }
1113 last_seq_no = seq_no;
1114 last_timestamp = timestamp;
1115
1116 ++seq_no;
1117 timestamp += kSamples;
1118 receive_timestamp += kSamples;
1119 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1120
1121 seq_no_wrapped |= seq_no < last_seq_no;
1122 timestamp_wrapped |= timestamp < last_timestamp;
1123 }
1124 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001125 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001126 bool muted;
1127 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001128 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1129 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001130
1131 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001132 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1133 ASSERT_TRUE(playout_timestamp);
1134 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001135 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001136 }
1137 // Make sure we have actually tested wrap-around.
1138 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1139 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1140}
1141
1142TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1143 // Start with a sequence number that will soon wrap.
1144 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1145 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1146}
1147
1148TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1149 // Start with a sequence number that will soon wrap.
1150 std::set<uint16_t> drop_seq_numbers;
1151 drop_seq_numbers.insert(0xFFFF);
1152 drop_seq_numbers.insert(0x0);
1153 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1154}
1155
1156TEST_F(NetEqDecodingTest, TimestampWrap) {
1157 // Start with a timestamp that will soon wrap.
1158 std::set<uint16_t> drop_seq_numbers;
1159 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1160}
1161
1162TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1163 // Start with a timestamp and a sequence number that will wrap at the same
1164 // time.
1165 std::set<uint16_t> drop_seq_numbers;
1166 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1167}
1168
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001169void NetEqDecodingTest::DuplicateCng() {
1170 uint16_t seq_no = 0;
1171 uint32_t timestamp = 0;
1172 const int kFrameSizeMs = 10;
1173 const int kSampleRateKhz = 16;
1174 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001175 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001176
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001177 const int algorithmic_delay_samples = std::max(
1178 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001179 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001180 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001181 uint8_t payload[kPayloadBytes] = {0};
1182 WebRtcRTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001183 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001184 for (int i = 0; i < 3; ++i) {
1185 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001186 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001187 ++seq_no;
1188 timestamp += kSamples;
1189
1190 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001191 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001192 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001193 }
1194 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001195 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001196
1197 // Insert same CNG packet twice.
1198 const int kCngPeriodMs = 100;
1199 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001200 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001201 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1202 // This is the first time this CNG packet is inserted.
henrik.lundin4d027572017-04-11 06:17:46 -07001203 ASSERT_EQ(0, neteq_->InsertPacket(
1204 rtp_info.header,
1205 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001206
1207 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001208 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001209 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001210 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001211 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1212 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1213 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001214
1215 // Insert the same CNG packet again. Note that at this point it is old, since
1216 // we have already decoded the first copy of it.
henrik.lundin4d027572017-04-11 06:17:46 -07001217 ASSERT_EQ(0, neteq_->InsertPacket(
1218 rtp_info.header,
1219 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001220
1221 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1222 // we have already pulled out CNG once.
1223 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001224 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001225 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001226 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001227 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001228 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001229 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001230 }
1231
1232 // Insert speech again.
1233 ++seq_no;
1234 timestamp += kCngPeriodSamples;
1235 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001236 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001237
1238 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001239 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001240 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001241 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001242 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1243 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001244 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001245 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001246}
1247
henrik.lundin0d96ab72016-04-06 12:28:26 -07001248rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1249 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001250}
1251
1252TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001253
1254TEST_F(NetEqDecodingTest, CngFirst) {
1255 uint16_t seq_no = 0;
1256 uint32_t timestamp = 0;
1257 const int kFrameSizeMs = 10;
1258 const int kSampleRateKhz = 16;
1259 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1260 const int kPayloadBytes = kSamples * 2;
1261 const int kCngPeriodMs = 100;
1262 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1263 size_t payload_len;
1264
1265 uint8_t payload[kPayloadBytes] = {0};
1266 WebRtcRTPHeader rtp_info;
1267
1268 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin4d027572017-04-11 06:17:46 -07001269 ASSERT_EQ(NetEq::kOK,
1270 neteq_->InsertPacket(
1271 rtp_info.header,
1272 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001273 ++seq_no;
1274 timestamp += kCngPeriodSamples;
1275
1276 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001277 bool muted;
1278 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001279 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001280 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001281
1282 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001283 const uint32_t first_speech_timestamp = timestamp;
1284 int timeout_counter = 0;
1285 do {
1286 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001287 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001288 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001289 ++seq_no;
1290 timestamp += kSamples;
1291
1292 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001293 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001294 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001295 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001296 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001297 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001298}
henrik.lundin7a926812016-05-12 13:51:28 -07001299
1300class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1301 public:
1302 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1303 config_.enable_muted_state = true;
1304 }
1305
1306 protected:
1307 static constexpr size_t kSamples = 10 * 16;
1308 static constexpr size_t kPayloadBytes = kSamples * 2;
1309
1310 void InsertPacket(uint32_t rtp_timestamp) {
1311 uint8_t payload[kPayloadBytes] = {0};
1312 WebRtcRTPHeader rtp_info;
1313 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001314 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001315 }
1316
henrik.lundin42feb512016-09-20 06:51:40 -07001317 void InsertCngPacket(uint32_t rtp_timestamp) {
1318 uint8_t payload[kPayloadBytes] = {0};
1319 WebRtcRTPHeader rtp_info;
1320 size_t payload_len;
1321 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin4d027572017-04-11 06:17:46 -07001322 EXPECT_EQ(NetEq::kOK,
1323 neteq_->InsertPacket(
1324 rtp_info.header,
1325 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001326 }
1327
henrik.lundin7a926812016-05-12 13:51:28 -07001328 bool GetAudioReturnMuted() {
1329 bool muted;
1330 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1331 return muted;
1332 }
1333
1334 void GetAudioUntilMuted() {
1335 while (!GetAudioReturnMuted()) {
1336 ASSERT_LT(counter_++, 1000) << "Test timed out";
1337 }
1338 }
1339
1340 void GetAudioUntilNormal() {
1341 bool muted = false;
1342 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1343 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1344 ASSERT_LT(counter_++, 1000) << "Test timed out";
1345 }
1346 EXPECT_FALSE(muted);
1347 }
1348
1349 int counter_ = 0;
1350};
1351
1352// Verifies that NetEq goes in and out of muted state as expected.
1353TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1354 // Insert one speech packet.
1355 InsertPacket(0);
1356 // Pull out audio once and expect it not to be muted.
1357 EXPECT_FALSE(GetAudioReturnMuted());
1358 // Pull data until faded out.
1359 GetAudioUntilMuted();
1360
1361 // Verify that output audio is not written during muted mode. Other parameters
1362 // should be correct, though.
1363 AudioFrame new_frame;
1364 for (auto& d : new_frame.data_) {
1365 d = 17;
1366 }
1367 bool muted;
1368 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1369 EXPECT_TRUE(muted);
1370 for (auto d : new_frame.data_) {
1371 EXPECT_EQ(17, d);
1372 }
1373 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1374 new_frame.timestamp_);
1375 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1376 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1377 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1378 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1379 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1380
1381 // Insert new data. Timestamp is corrected for the time elapsed since the last
1382 // packet. Verify that normal operation resumes.
1383 InsertPacket(kSamples * counter_);
1384 GetAudioUntilNormal();
henrik.lundin612c25e2016-05-25 08:21:04 -07001385
1386 NetEqNetworkStatistics stats;
1387 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1388 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1389 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1390 // concealment samples in this test.
1391 EXPECT_GT(stats.expand_rate, 14000);
1392 // And, it should be greater than the speech_expand_rate.
1393 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001394}
1395
1396// Verifies that NetEq goes out of muted state when given a delayed packet.
1397TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1398 // Insert one speech packet.
1399 InsertPacket(0);
1400 // Pull out audio once and expect it not to be muted.
1401 EXPECT_FALSE(GetAudioReturnMuted());
1402 // Pull data until faded out.
1403 GetAudioUntilMuted();
1404 // Insert new data. Timestamp is only corrected for the half of the time
1405 // elapsed since the last packet. That is, the new packet is delayed. Verify
1406 // that normal operation resumes.
1407 InsertPacket(kSamples * counter_ / 2);
1408 GetAudioUntilNormal();
1409}
1410
1411// Verifies that NetEq goes out of muted state when given a future packet.
1412TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1413 // Insert one speech packet.
1414 InsertPacket(0);
1415 // Pull out audio once and expect it not to be muted.
1416 EXPECT_FALSE(GetAudioReturnMuted());
1417 // Pull data until faded out.
1418 GetAudioUntilMuted();
1419 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1420 // last packet. That is, the new packet is too early. Verify that normal
1421 // operation resumes.
1422 InsertPacket(kSamples * counter_ * 2);
1423 GetAudioUntilNormal();
1424}
1425
1426// Verifies that NetEq goes out of muted state when given an old packet.
1427TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1428 // Insert one speech packet.
1429 InsertPacket(0);
1430 // Pull out audio once and expect it not to be muted.
1431 EXPECT_FALSE(GetAudioReturnMuted());
1432 // Pull data until faded out.
1433 GetAudioUntilMuted();
1434
1435 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1436 // Insert packet which is older than the first packet.
1437 InsertPacket(kSamples * (counter_ - 1000));
1438 EXPECT_FALSE(GetAudioReturnMuted());
1439 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1440}
1441
henrik.lundin42feb512016-09-20 06:51:40 -07001442// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1443// packet stream is suspended for a long time.
1444TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1445 // Insert one CNG packet.
1446 InsertCngPacket(0);
1447
1448 // Pull 10 seconds of audio (10 ms audio generated per lap).
1449 for (int i = 0; i < 1000; ++i) {
1450 bool muted;
1451 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1452 ASSERT_FALSE(muted);
1453 }
1454 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1455}
1456
1457// Verifies that NetEq goes back to normal after a long CNG period with the
1458// packet stream suspended.
1459TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1460 // Insert one CNG packet.
1461 InsertCngPacket(0);
1462
1463 // Pull 10 seconds of audio (10 ms audio generated per lap).
1464 for (int i = 0; i < 1000; ++i) {
1465 bool muted;
1466 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1467 }
1468
1469 // Insert new data. Timestamp is corrected for the time elapsed since the last
1470 // packet. Verify that normal operation resumes.
1471 InsertPacket(kSamples * counter_);
1472 GetAudioUntilNormal();
1473}
1474
henrik.lundin7a926812016-05-12 13:51:28 -07001475class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1476 public:
1477 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1478
1479 void SetUp() override {
1480 NetEqDecodingTest::SetUp();
1481 config2_ = config_;
1482 }
1483
1484 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001485 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001486 ASSERT_TRUE(neteq2_);
1487 LoadDecoders(neteq2_.get());
1488 }
1489
1490 protected:
1491 std::unique_ptr<NetEq> neteq2_;
1492 NetEq::Config config2_;
1493};
1494
1495namespace {
1496::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1497 const AudioFrame& b) {
1498 if (a.timestamp_ != b.timestamp_)
1499 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1500 << " != " << b.timestamp_ << ")";
1501 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1502 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1503 << a.sample_rate_hz_
1504 << " != " << b.sample_rate_hz_ << ")";
1505 if (a.samples_per_channel_ != b.samples_per_channel_)
1506 return ::testing::AssertionFailure()
1507 << "samples_per_channel_ diff (" << a.samples_per_channel_
1508 << " != " << b.samples_per_channel_ << ")";
1509 if (a.num_channels_ != b.num_channels_)
1510 return ::testing::AssertionFailure() << "num_channels_ diff ("
1511 << a.num_channels_
1512 << " != " << b.num_channels_ << ")";
1513 if (a.speech_type_ != b.speech_type_)
1514 return ::testing::AssertionFailure() << "speech_type_ diff ("
1515 << a.speech_type_
1516 << " != " << b.speech_type_ << ")";
1517 if (a.vad_activity_ != b.vad_activity_)
1518 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1519 << a.vad_activity_
1520 << " != " << b.vad_activity_ << ")";
1521 return ::testing::AssertionSuccess();
1522}
1523
1524::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1525 const AudioFrame& b) {
1526 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1527 if (!res)
1528 return res;
1529 if (memcmp(
1530 a.data_, b.data_,
1531 a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) {
1532 return ::testing::AssertionFailure() << "data_ diff";
1533 }
1534 return ::testing::AssertionSuccess();
1535}
1536
1537} // namespace
1538
1539TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1540 ASSERT_FALSE(config_.enable_muted_state);
1541 config2_.enable_muted_state = true;
1542 CreateSecondInstance();
1543
1544 // Insert one speech packet into both NetEqs.
1545 const size_t kSamples = 10 * 16;
1546 const size_t kPayloadBytes = kSamples * 2;
1547 uint8_t payload[kPayloadBytes] = {0};
1548 WebRtcRTPHeader rtp_info;
1549 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001550 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
1551 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001552
1553 AudioFrame out_frame1, out_frame2;
1554 bool muted;
1555 for (int i = 0; i < 1000; ++i) {
1556 std::ostringstream ss;
1557 ss << "i = " << i;
1558 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1559 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1560 EXPECT_FALSE(muted);
1561 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1562 if (muted) {
1563 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1564 } else {
1565 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1566 }
1567 }
1568 EXPECT_TRUE(muted);
1569
1570 // Insert new data. Timestamp is corrected for the time elapsed since the last
1571 // packet.
1572 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin4d027572017-04-11 06:17:46 -07001573 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info.header, payload, 0));
1574 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info.header, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001575
1576 int counter = 0;
1577 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1578 ASSERT_LT(counter++, 1000) << "Test timed out";
1579 std::ostringstream ss;
1580 ss << "counter = " << counter;
1581 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1582 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1583 EXPECT_FALSE(muted);
1584 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1585 if (muted) {
1586 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1587 } else {
1588 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1589 }
1590 }
1591 EXPECT_FALSE(muted);
1592}
1593
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001594} // namespace webrtc