blob: fae1e2324edab4c1c16546b52af35ab5d8a0f202 [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"
henrik.lundin246ef3e2017-04-24 09:14:32 -070026#include "webrtc/base/protobuf_utils.h"
minyue4f906772016-04-29 11:05:14 -070027#include "webrtc/base/sha1digest.h"
28#include "webrtc/base/stringencode.h"
henrik.lundin246ef3e2017-04-24 09:14:32 -070029#include "webrtc/common_types.h"
kwibergac9f8762016-09-30 22:29:43 -070030#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000031#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000032#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080033#include "webrtc/modules/include/module_common_types.h"
kwibergac9f8762016-09-30 22:29:43 -070034#include "webrtc/test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035#include "webrtc/test/testsupport/fileutils.h"
36#include "webrtc/typedefs.h"
37
minyue5f026d02015-12-16 07:36:04 -080038#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070039RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080040#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
41#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
42#else
kjellandere3e902e2017-02-28 08:01:46 -080043#include "webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080044#endif
kwiberg77eab702016-09-28 17:42:01 -070045RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080046#endif
47
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000048DEFINE_bool(gen_ref, false, "Generate reference files.");
49
kwiberg5adaf732016-10-04 09:33:27 -070050namespace webrtc {
51
minyue5f026d02015-12-16 07:36:04 -080052namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
minyue4f906772016-04-29 11:05:14 -070054const std::string& PlatformChecksum(const std::string& checksum_general,
55 const std::string& checksum_android,
56 const std::string& checksum_win_32,
57 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070058#if defined(WEBRTC_ANDROID)
minyue4f906772016-04-29 11:05:14 -070059 return checksum_android;
kwiberg77eab702016-09-28 17:42:01 -070060#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070061 #ifdef WEBRTC_ARCH_64_BITS
62 return checksum_win_64;
63 #else
64 return checksum_win_32;
65 #endif // WEBRTC_ARCH_64_BITS
66#else
67 return checksum_general;
68#endif // WEBRTC_WIN
69}
70
minyue5f026d02015-12-16 07:36:04 -080071#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
72void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
73 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
74 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
75 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
76 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
77 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
78 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
79 stats->set_expand_rate(stats_raw.expand_rate);
80 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
81 stats->set_preemptive_rate(stats_raw.preemptive_rate);
82 stats->set_accelerate_rate(stats_raw.accelerate_rate);
83 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
84 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
85 stats->set_added_zero_samples(stats_raw.added_zero_samples);
86 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
87 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
88 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
89 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
90}
91
92void Convert(const webrtc::RtcpStatistics& stats_raw,
93 webrtc::neteq_unittest::RtcpStatistics* stats) {
94 stats->set_fraction_lost(stats_raw.fraction_lost);
95 stats->set_cumulative_lost(stats_raw.cumulative_lost);
96 stats->set_extended_max_sequence_number(
97 stats_raw.extended_max_sequence_number);
98 stats->set_jitter(stats_raw.jitter);
99}
100
minyue4f906772016-04-29 11:05:14 -0700101void AddMessage(FILE* file, rtc::MessageDigest* digest,
102 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800103 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700104 if (file)
105 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
106 digest->Update(&size, sizeof(size));
107
108 if (file)
109 ASSERT_EQ(static_cast<size_t>(size),
110 fwrite(message.data(), sizeof(char), size, file));
111 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800112}
113
minyue5f026d02015-12-16 07:36:04 -0800114#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
115
henrik.lundin7a926812016-05-12 13:51:28 -0700116void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700117 ASSERT_EQ(true,
118 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
119 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
120 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700121 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
122 "pcma", 8));
123#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700124 ASSERT_EQ(true,
125 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700126#endif
127#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700128 ASSERT_EQ(true,
129 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700130#endif
131#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700132 ASSERT_EQ(true,
133 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700134#endif
135#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700136 ASSERT_EQ(true,
137 neteq->RegisterPayloadType(
138 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700139#endif
kwiberg5adaf732016-10-04 09:33:27 -0700140 ASSERT_EQ(true,
141 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
142 ASSERT_EQ(true,
143 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
144 ASSERT_EQ(true,
145 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
146 ASSERT_EQ(true,
147 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
148 ASSERT_EQ(true,
149 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700150}
minyue5f026d02015-12-16 07:36:04 -0800151} // namespace
152
minyue4f906772016-04-29 11:05:14 -0700153class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000154 public:
minyue4f906772016-04-29 11:05:14 -0700155 explicit ResultSink(const std::string& output_file);
156 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157
yujo36b1a5f2017-06-12 12:45:32 -0700158 template<typename T> void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700159
160 void AddResult(const NetEqNetworkStatistics& stats);
161 void AddResult(const RtcpStatistics& stats);
162
163 void VerifyChecksum(const std::string& ref_check_sum);
164
165 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700167 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168};
169
minyue4f906772016-04-29 11:05:14 -0700170ResultSink::ResultSink(const std::string &output_file)
171 : output_fp_(nullptr),
172 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 if (!output_file.empty()) {
174 output_fp_ = fopen(output_file.c_str(), "wb");
175 EXPECT_TRUE(output_fp_ != NULL);
176 }
177}
178
minyue4f906772016-04-29 11:05:14 -0700179ResultSink::~ResultSink() {
180 if (output_fp_)
181 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182}
183
yujo36b1a5f2017-06-12 12:45:32 -0700184template<typename T>
185void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700187 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188 }
yujo36b1a5f2017-06-12 12:45:32 -0700189 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190}
191
minyue4f906772016-04-29 11:05:14 -0700192void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800193#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800194 neteq_unittest::NetEqNetworkStatistics stats;
195 Convert(stats_raw, &stats);
196
mbonadei7c2c8432017-04-07 00:59:12 -0700197 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800198 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700199 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800200#else
201 FAIL() << "Writing to reference file requires Proto Buffer.";
202#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203}
204
minyue4f906772016-04-29 11:05:14 -0700205void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800206#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800207 neteq_unittest::RtcpStatistics stats;
208 Convert(stats_raw, &stats);
209
mbonadei7c2c8432017-04-07 00:59:12 -0700210 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800211 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700212 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800213#else
214 FAIL() << "Writing to reference file requires Proto Buffer.";
215#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216}
217
minyue4f906772016-04-29 11:05:14 -0700218void ResultSink::VerifyChecksum(const std::string& checksum) {
219 std::vector<char> buffer;
220 buffer.resize(digest_->Size());
221 digest_->Finish(&buffer[0], buffer.size());
222 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
223 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224}
225
226class NetEqDecodingTest : public ::testing::Test {
227 protected:
228 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
229 // constants below can be changed.
230 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700231 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
232 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
233 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800234 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000235 static const int kInitSampleRateHz = 8000;
236
237 NetEqDecodingTest();
238 virtual void SetUp();
239 virtual void TearDown();
240 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800242 void Process();
minyue5f026d02015-12-16 07:36:04 -0800243
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000244 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700245 const std::string& output_checksum,
246 const std::string& network_stats_checksum,
247 const std::string& rtcp_stats_checksum,
248 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800249
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250 static void PopulateRtpInfo(int frame_index,
251 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700252 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 static void PopulateCng(int frame_index,
254 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700255 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000257 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000259 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
260 const std::set<uint16_t>& drop_seq_numbers,
261 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
262
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000263 void LongCngWithClockDrift(double drift_factor,
264 double network_freeze_ms,
265 bool pull_audio_during_freeze,
266 int delay_tolerance_ms,
267 int max_time_to_speech_ms);
268
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000269 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000270
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000272 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800273 std::unique_ptr<test::RtpFileSource> rtp_source_;
274 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800276 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000278 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279};
280
281// Allocating the static const so that it can be passed by reference.
282const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700283const size_t NetEqDecodingTest::kBlockSize8kHz;
284const size_t NetEqDecodingTest::kBlockSize16kHz;
285const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286const int NetEqDecodingTest::kInitSampleRateHz;
287
288NetEqDecodingTest::NetEqDecodingTest()
289 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000290 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000292 output_sample_rate_(kInitSampleRateHz),
293 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000294 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295}
296
297void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700298 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000299 NetEqNetworkStatistics stat;
300 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
301 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000302 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700303 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304}
305
306void NetEqDecodingTest::TearDown() {
307 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308}
309
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000311 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312}
313
henrik.lundin6d8e0112016-03-04 10:34:21 -0800314void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000316 while (packet_ && sim_clock_ >= packet_->time_ms()) {
317 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800318#ifndef WEBRTC_CODEC_ISAC
319 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700320 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800321#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200322 ASSERT_EQ(0,
323 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700324 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200325 rtc::ArrayView<const uint8_t>(
326 packet_->payload(), packet_->payload_length_bytes()),
327 static_cast<uint32_t>(packet_->time_ms() *
328 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329 }
330 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700331 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 }
333
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000334 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700335 bool muted;
336 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
337 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800338 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
339 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
340 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
341 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
342 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800343 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344
345 // Increase time.
346 sim_clock_ += kTimeStepMs;
347}
348
minyue4f906772016-04-29 11:05:14 -0700349void NetEqDecodingTest::DecodeAndCompare(
350 const std::string& rtp_file,
351 const std::string& output_checksum,
352 const std::string& network_stats_checksum,
353 const std::string& rtcp_stats_checksum,
354 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 OpenInputFile(rtp_file);
356
minyue4f906772016-04-29 11:05:14 -0700357 std::string ref_out_file =
358 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
359 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360
minyue4f906772016-04-29 11:05:14 -0700361 std::string stat_out_file =
362 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
363 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000364
minyue4f906772016-04-29 11:05:14 -0700365 std::string rtcp_out_file =
366 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
367 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000368
henrik.lundin46ba49c2016-05-24 22:50:47 -0700369 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000371 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 std::ostringstream ss;
373 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
374 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800375 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700376 ASSERT_NO_FATAL_FAILURE(output.AddResult(
yujo36b1a5f2017-06-12 12:45:32 -0700377 out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378
379 // Query the network statistics API once per second
380 if (sim_clock_ % 1000 == 0) {
381 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700382 NetEqNetworkStatistics current_network_stats;
383 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
384 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
385
henrik.lundin9c3efd02015-08-27 13:12:22 -0700386 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700387 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
388 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389
390 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700391 RtcpStatistics current_rtcp_stats;
392 neteq_->GetRtcpStatistics(&current_rtcp_stats);
393 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 }
395 }
minyue4f906772016-04-29 11:05:14 -0700396
397 SCOPED_TRACE("Check output audio.");
398 output.VerifyChecksum(output_checksum);
399 SCOPED_TRACE("Check network stats.");
400 network_stats.VerifyChecksum(network_stats_checksum);
401 SCOPED_TRACE("Check rtcp stats.");
402 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000403}
404
405void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
406 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700407 RTPHeader* rtp_info) {
408 rtp_info->sequenceNumber = frame_index;
409 rtp_info->timestamp = timestamp;
410 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
411 rtp_info->payloadType = 94; // PCM16b WB codec.
412 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000413}
414
415void NetEqDecodingTest::PopulateCng(int frame_index,
416 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700417 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000418 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000419 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700420 rtp_info->sequenceNumber = frame_index;
421 rtp_info->timestamp = timestamp;
422 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
423 rtp_info->payloadType = 98; // WB CNG.
424 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
426 *payload_len = 1; // Only noise level, no spectral parameters.
427}
428
ivoc72c08ed2016-01-20 07:26:24 -0800429#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
430 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
431 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700432 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800433#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700434#else
minyue5f026d02015-12-16 07:36:04 -0800435#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700436#endif
minyue5f026d02015-12-16 07:36:04 -0800437TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800438 const std::string input_rtp_file =
439 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000440
minyue4f906772016-04-29 11:05:14 -0700441 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700442 "09fa7646e2ad032a0b156177b95f09012430f81f",
443 "1c64eb8b55ce8878676c6a1e6ddd78f48de0668b",
444 "09fa7646e2ad032a0b156177b95f09012430f81f",
445 "759fef89a5de52bd17e733dc255c671ce86be909");
minyue4f906772016-04-29 11:05:14 -0700446
henrik.lundin2979f552017-05-05 05:04:16 -0700447 const std::string network_stats_checksum =
448 PlatformChecksum("f7c2158761a531dd2804d13da0480033faa7be12",
449 "8b5e3c8247dce48cb33923eaa1a502ca91429d5e",
450 "f7c2158761a531dd2804d13da0480033faa7be12",
451 "f7c2158761a531dd2804d13da0480033faa7be12");
minyue4f906772016-04-29 11:05:14 -0700452
453 const std::string rtcp_stats_checksum = PlatformChecksum(
454 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
455 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
456 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
457 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
458
459 DecodeAndCompare(input_rtp_file,
460 output_checksum,
461 network_stats_checksum,
462 rtcp_stats_checksum,
463 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000464}
465
minyue93c08b72015-12-22 09:57:41 -0800466#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
467 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyuea613eb62017-03-14 14:33:30 -0700468 defined(WEBRTC_CODEC_OPUS) && \
469 !WEBRTC_OPUS_SUPPORT_120MS_PTIME
minyue93c08b72015-12-22 09:57:41 -0800470#define MAYBE_TestOpusBitExactness TestOpusBitExactness
471#else
472#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
473#endif
flim64a7eab2016-08-12 04:36:05 -0700474TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800475 const std::string input_rtp_file =
476 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800477
minyue4f906772016-04-29 11:05:14 -0700478 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700479 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
480 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
481 "6237dd113ad80d7764fe4c90b55b2ec035eae64e",
482 "6237dd113ad80d7764fe4c90b55b2ec035eae64e");
minyue4f906772016-04-29 11:05:14 -0700483
henrik.lundin2979f552017-05-05 05:04:16 -0700484 const std::string network_stats_checksum =
485 PlatformChecksum("0869a450a819b14bf2a91eb6f3629a3421d17606",
486 "0869a450a819b14bf2a91eb6f3629a3421d17606",
487 "0869a450a819b14bf2a91eb6f3629a3421d17606",
488 "0869a450a819b14bf2a91eb6f3629a3421d17606");
minyue4f906772016-04-29 11:05:14 -0700489
490 const std::string rtcp_stats_checksum = PlatformChecksum(
491 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
492 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
493 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
494 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
495
496 DecodeAndCompare(input_rtp_file,
497 output_checksum,
498 network_stats_checksum,
499 rtcp_stats_checksum,
500 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800501}
502
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000503// Use fax mode to avoid time-scaling. This is to simplify the testing of
504// packet waiting times in the packet buffer.
505class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
506 protected:
507 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
508 config_.playout_mode = kPlayoutFax;
509 }
510};
511
512TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000513 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
514 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000515 const size_t kSamples = 10 * 16;
516 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000517 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800518 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700519 RTPHeader rtp_info;
520 rtp_info.sequenceNumber = i;
521 rtp_info.timestamp = i * kSamples;
522 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
523 rtp_info.payloadType = 94; // PCM16b WB codec.
524 rtp_info.markerBit = 0;
525 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000526 }
527 // Pull out all data.
528 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700529 bool muted;
530 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800531 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000532 }
533
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200534 NetEqNetworkStatistics stats;
535 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000536 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
537 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200538 // each packet. Thus, we are calculating the statistics for a series from 10
539 // to 300, in steps of 10 ms.
540 EXPECT_EQ(155, stats.mean_waiting_time_ms);
541 EXPECT_EQ(155, stats.median_waiting_time_ms);
542 EXPECT_EQ(10, stats.min_waiting_time_ms);
543 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000544
545 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200546 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
547 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
548 EXPECT_EQ(-1, stats.median_waiting_time_ms);
549 EXPECT_EQ(-1, stats.min_waiting_time_ms);
550 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000551}
552
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000553TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554 const int kNumFrames = 3000; // Needed for convergence.
555 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000556 const size_t kSamples = 10 * 16;
557 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 while (frame_index < kNumFrames) {
559 // Insert one packet each time, except every 10th time where we insert two
560 // packets at once. This will create a negative clock-drift of approx. 10%.
561 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
562 for (int n = 0; n < num_packets; ++n) {
563 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700564 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000565 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700566 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000567 ++frame_index;
568 }
569
570 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700571 bool muted;
572 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800573 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574 }
575
576 NetEqNetworkStatistics network_stats;
577 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700578 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579}
580
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000581TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000582 const int kNumFrames = 5000; // Needed for convergence.
583 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000584 const size_t kSamples = 10 * 16;
585 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000586 for (int i = 0; i < kNumFrames; ++i) {
587 // Insert one packet each time, except every 10th time where we don't insert
588 // any packet. This will create a positive clock-drift of approx. 11%.
589 int num_packets = (i % 10 == 9 ? 0 : 1);
590 for (int n = 0; n < num_packets; ++n) {
591 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700592 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000593 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700594 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 ++frame_index;
596 }
597
598 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700599 bool muted;
600 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800601 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 }
603
604 NetEqNetworkStatistics network_stats;
605 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700606 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607}
608
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000609void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
610 double network_freeze_ms,
611 bool pull_audio_during_freeze,
612 int delay_tolerance_ms,
613 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000614 uint16_t seq_no = 0;
615 uint32_t timestamp = 0;
616 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000617 const size_t kSamples = kFrameSizeMs * 16;
618 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000619 double next_input_time_ms = 0.0;
620 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700621 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622
623 // Insert speech for 5 seconds.
624 const int kSpeechDurationMs = 5000;
625 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
626 // Each turn in this for loop is 10 ms.
627 while (next_input_time_ms <= t_ms) {
628 // Insert one 30 ms speech frame.
629 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700630 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000631 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700632 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 ++seq_no;
634 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000635 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 }
637 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700638 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800639 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640 }
641
henrik.lundin55480f52016-03-08 02:37:57 -0800642 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700643 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700644 ASSERT_TRUE(playout_timestamp);
645 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646
647 // Insert CNG for 1 minute (= 60000 ms).
648 const int kCngPeriodMs = 100;
649 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
650 const int kCngDurationMs = 60000;
651 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
652 // Each turn in this for loop is 10 ms.
653 while (next_input_time_ms <= t_ms) {
654 // Insert one CNG frame each 100 ms.
655 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000656 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700657 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000658 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800659 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700660 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800661 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000662 ++seq_no;
663 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000664 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000665 }
666 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700667 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800668 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000669 }
670
henrik.lundin55480f52016-03-08 02:37:57 -0800671 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000672
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000673 if (network_freeze_ms > 0) {
674 // First keep pulling audio for |network_freeze_ms| without inserting
675 // any data, then insert CNG data corresponding to |network_freeze_ms|
676 // without pulling any output audio.
677 const double loop_end_time = t_ms + network_freeze_ms;
678 for (; t_ms < loop_end_time; t_ms += 10) {
679 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700680 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800681 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800682 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000683 }
684 bool pull_once = pull_audio_during_freeze;
685 // If |pull_once| is true, GetAudio will be called once half-way through
686 // the network recovery period.
687 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
688 while (next_input_time_ms <= t_ms) {
689 if (pull_once && next_input_time_ms >= pull_time_ms) {
690 pull_once = false;
691 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700692 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800693 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800694 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000695 t_ms += 10;
696 }
697 // Insert one CNG frame each 100 ms.
698 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000699 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700700 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000701 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800702 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700703 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800704 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000705 ++seq_no;
706 timestamp += kCngPeriodSamples;
707 next_input_time_ms += kCngPeriodMs * drift_factor;
708 }
709 }
710
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000711 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000712 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800713 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 // Each turn in this for loop is 10 ms.
715 while (next_input_time_ms <= t_ms) {
716 // Insert one 30 ms speech frame.
717 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700718 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000719 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700720 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000721 ++seq_no;
722 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000723 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724 }
725 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700726 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800727 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000728 // Increase clock.
729 t_ms += 10;
730 }
731
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 // Check that the speech starts again within reasonable time.
733 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
734 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700735 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700736 ASSERT_TRUE(playout_timestamp);
737 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000739 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
740 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741}
742
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000743TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000744 // Apply a clock drift of -25 ms / s (sender faster than receiver).
745 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000746 const double kNetworkFreezeTimeMs = 0.0;
747 const bool kGetAudioDuringFreezeRecovery = false;
748 const int kDelayToleranceMs = 20;
749 const int kMaxTimeToSpeechMs = 100;
750 LongCngWithClockDrift(kDriftFactor,
751 kNetworkFreezeTimeMs,
752 kGetAudioDuringFreezeRecovery,
753 kDelayToleranceMs,
754 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000755}
756
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000757TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000758 // Apply a clock drift of +25 ms / s (sender slower than receiver).
759 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000760 const double kNetworkFreezeTimeMs = 0.0;
761 const bool kGetAudioDuringFreezeRecovery = false;
762 const int kDelayToleranceMs = 20;
763 const int kMaxTimeToSpeechMs = 100;
764 LongCngWithClockDrift(kDriftFactor,
765 kNetworkFreezeTimeMs,
766 kGetAudioDuringFreezeRecovery,
767 kDelayToleranceMs,
768 kMaxTimeToSpeechMs);
769}
770
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000771TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000772 // Apply a clock drift of -25 ms / s (sender faster than receiver).
773 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
774 const double kNetworkFreezeTimeMs = 5000.0;
775 const bool kGetAudioDuringFreezeRecovery = false;
776 const int kDelayToleranceMs = 50;
777 const int kMaxTimeToSpeechMs = 200;
778 LongCngWithClockDrift(kDriftFactor,
779 kNetworkFreezeTimeMs,
780 kGetAudioDuringFreezeRecovery,
781 kDelayToleranceMs,
782 kMaxTimeToSpeechMs);
783}
784
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000785TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000786 // Apply a clock drift of +25 ms / s (sender slower than receiver).
787 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
788 const double kNetworkFreezeTimeMs = 5000.0;
789 const bool kGetAudioDuringFreezeRecovery = false;
790 const int kDelayToleranceMs = 20;
791 const int kMaxTimeToSpeechMs = 100;
792 LongCngWithClockDrift(kDriftFactor,
793 kNetworkFreezeTimeMs,
794 kGetAudioDuringFreezeRecovery,
795 kDelayToleranceMs,
796 kMaxTimeToSpeechMs);
797}
798
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000799TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000800 // Apply a clock drift of +25 ms / s (sender slower than receiver).
801 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
802 const double kNetworkFreezeTimeMs = 5000.0;
803 const bool kGetAudioDuringFreezeRecovery = true;
804 const int kDelayToleranceMs = 20;
805 const int kMaxTimeToSpeechMs = 100;
806 LongCngWithClockDrift(kDriftFactor,
807 kNetworkFreezeTimeMs,
808 kGetAudioDuringFreezeRecovery,
809 kDelayToleranceMs,
810 kMaxTimeToSpeechMs);
811}
812
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000813TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000814 const double kDriftFactor = 1.0; // No drift.
815 const double kNetworkFreezeTimeMs = 0.0;
816 const bool kGetAudioDuringFreezeRecovery = false;
817 const int kDelayToleranceMs = 10;
818 const int kMaxTimeToSpeechMs = 50;
819 LongCngWithClockDrift(kDriftFactor,
820 kNetworkFreezeTimeMs,
821 kGetAudioDuringFreezeRecovery,
822 kDelayToleranceMs,
823 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000824}
825
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000826TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000827 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000828 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700829 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000830 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700831 rtp_info.payloadType = 1; // Not registered as a decoder.
832 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000833 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
834}
835
Peter Boströme2976c82016-01-04 22:44:05 +0100836#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800837#define MAYBE_DecoderError DecoderError
838#else
839#define MAYBE_DecoderError DISABLED_DecoderError
840#endif
841
Peter Boströme2976c82016-01-04 22:44:05 +0100842TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000843 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000844 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700845 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000846 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700847 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
848 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
850 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700851 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800852 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700853 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854 }
henrik.lundin7a926812016-05-12 13:51:28 -0700855 bool muted;
856 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
857 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000858 // Verify that there is a decoder error to check.
859 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800860
861 enum NetEqDecoderError {
862 ISAC_LENGTH_MISMATCH = 6730,
863 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
864 };
865#if defined(WEBRTC_CODEC_ISAC)
866 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
867#elif defined(WEBRTC_CODEC_ISACFX)
868 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
869#endif
yujo36b1a5f2017-06-12 12:45:32 -0700870 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000871 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700872 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000873 for (int i = 0; i < kExpectedOutputLength; ++i) {
874 std::ostringstream ss;
875 ss << "i = " << i;
876 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700877 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 }
879}
880
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000881TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000882 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
883 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700884 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800885 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700886 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000887 }
henrik.lundin7a926812016-05-12 13:51:28 -0700888 bool muted;
889 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
890 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000891 // Verify that the first block of samples is set to 0.
892 static const int kExpectedOutputLength =
893 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700894 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000895 for (int i = 0; i < kExpectedOutputLength; ++i) {
896 std::ostringstream ss;
897 ss << "i = " << i;
898 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700899 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000900 }
henrik.lundind89814b2015-11-23 06:49:25 -0800901 // Verify that the sample rate did not change from the initial configuration.
902 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000903}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000904
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000905class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000906 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000907 virtual void TestCondition(double sum_squared_noise,
908 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000909
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000910 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700911 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000912 uint8_t payload_type = 0xFF; // Invalid.
913 if (sampling_rate_hz == 8000) {
914 expected_samples_per_channel = kBlockSize8kHz;
915 payload_type = 93; // PCM 16, 8 kHz.
916 } else if (sampling_rate_hz == 16000) {
917 expected_samples_per_channel = kBlockSize16kHz;
918 payload_type = 94; // PCM 16, 16 kHZ.
919 } else if (sampling_rate_hz == 32000) {
920 expected_samples_per_channel = kBlockSize32kHz;
921 payload_type = 95; // PCM 16, 32 kHz.
922 } else {
923 ASSERT_TRUE(false); // Unsupported test case.
924 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000925
henrik.lundin6d8e0112016-03-04 10:34:21 -0800926 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000927 test::AudioLoop input;
928 // We are using the same 32 kHz input file for all tests, regardless of
929 // |sampling_rate_hz|. The output may sound weird, but the test is still
930 // valid.
931 ASSERT_TRUE(input.Init(
932 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
933 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700934 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000935
936 // Payload of 10 ms of PCM16 32 kHz.
937 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700938 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000939 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700940 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000941
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000942 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700943 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000944 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800945 auto block = input.GetNextBlock();
946 ASSERT_EQ(expected_samples_per_channel, block.size());
947 size_t enc_len_bytes =
948 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000949 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
950
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200951 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700952 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200953 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
954 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800955 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700956 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800957 ASSERT_EQ(1u, output.num_channels_);
958 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800959 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000960
961 // Next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700962 rtp_info.timestamp += expected_samples_per_channel;
963 rtp_info.sequenceNumber++;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000964 receive_timestamp += expected_samples_per_channel;
965 }
966
henrik.lundin6d8e0112016-03-04 10:34:21 -0800967 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000968
969 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
970 // one frame without checking speech-type. This is the first frame pulled
971 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700972 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800973 ASSERT_EQ(1u, output.num_channels_);
974 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000975
976 // To be able to test the fading of background noise we need at lease to
977 // pull 611 frames.
978 const int kFadingThreshold = 611;
979
980 // Test several CNG-to-PLC packet for the expected behavior. The number 20
981 // is arbitrary, but sufficiently large to test enough number of frames.
982 const int kNumPlcToCngTestFrames = 20;
983 bool plc_to_cng = false;
984 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800985 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -0700986 // Set to non-zero.
987 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -0700988 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
989 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800990 ASSERT_EQ(1u, output.num_channels_);
991 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800992 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000993 plc_to_cng = true;
994 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700995 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800996 for (size_t k = 0;
997 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -0700998 sum_squared += output_data[k] * output_data[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000999 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001000 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001001 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001002 }
1003 }
1004 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1005 }
1006};
1007
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001008class NetEqBgnTestOn : public NetEqBgnTest {
1009 protected:
1010 NetEqBgnTestOn() : NetEqBgnTest() {
1011 config_.background_noise_mode = NetEq::kBgnOn;
1012 }
1013
1014 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1015 EXPECT_NE(0, sum_squared_noise);
1016 }
1017};
1018
1019class NetEqBgnTestOff : public NetEqBgnTest {
1020 protected:
1021 NetEqBgnTestOff() : NetEqBgnTest() {
1022 config_.background_noise_mode = NetEq::kBgnOff;
1023 }
1024
1025 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1026 EXPECT_EQ(0, sum_squared_noise);
1027 }
1028};
1029
1030class NetEqBgnTestFade : public NetEqBgnTest {
1031 protected:
1032 NetEqBgnTestFade() : NetEqBgnTest() {
1033 config_.background_noise_mode = NetEq::kBgnFade;
1034 }
1035
1036 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1037 if (should_be_faded)
1038 EXPECT_EQ(0, sum_squared_noise);
1039 }
1040};
1041
henrika1d34fe92015-06-16 10:04:20 +02001042TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001043 CheckBgn(8000);
1044 CheckBgn(16000);
1045 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001046}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001047
henrika1d34fe92015-06-16 10:04:20 +02001048TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001049 CheckBgn(8000);
1050 CheckBgn(16000);
1051 CheckBgn(32000);
1052}
1053
henrika1d34fe92015-06-16 10:04:20 +02001054TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001055 CheckBgn(8000);
1056 CheckBgn(16000);
1057 CheckBgn(32000);
1058}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001059
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001060void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1061 uint32_t start_timestamp,
1062 const std::set<uint16_t>& drop_seq_numbers,
1063 bool expect_seq_no_wrap,
1064 bool expect_timestamp_wrap) {
1065 uint16_t seq_no = start_seq_no;
1066 uint32_t timestamp = start_timestamp;
1067 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1068 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1069 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001070 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001071 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001072 uint32_t receive_timestamp = 0;
1073
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001074 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001075 const int kSpeechDurationMs = 2000;
1076 int packets_inserted = 0;
1077 uint16_t last_seq_no;
1078 uint32_t last_timestamp;
1079 bool timestamp_wrapped = false;
1080 bool seq_no_wrapped = false;
1081 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1082 // Each turn in this for loop is 10 ms.
1083 while (next_input_time_ms <= t_ms) {
1084 // Insert one 30 ms speech frame.
1085 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001086 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001087 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1088 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1089 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001090 ASSERT_EQ(0,
1091 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001092 ++packets_inserted;
1093 }
1094 NetEqNetworkStatistics network_stats;
1095 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1096
1097 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1098 // packet size for first few packets. Therefore we refrain from checking
1099 // the criteria.
1100 if (packets_inserted > 4) {
1101 // Expect preferred and actual buffer size to be no more than 2 frames.
1102 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001103 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1104 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001105 }
1106 last_seq_no = seq_no;
1107 last_timestamp = timestamp;
1108
1109 ++seq_no;
1110 timestamp += kSamples;
1111 receive_timestamp += kSamples;
1112 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1113
1114 seq_no_wrapped |= seq_no < last_seq_no;
1115 timestamp_wrapped |= timestamp < last_timestamp;
1116 }
1117 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001118 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001119 bool muted;
1120 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001121 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1122 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001123
1124 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin114c1b32017-04-26 07:47:32 -07001125 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001126 ASSERT_TRUE(playout_timestamp);
1127 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001128 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001129 }
1130 // Make sure we have actually tested wrap-around.
1131 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1132 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1133}
1134
1135TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1136 // Start with a sequence number that will soon wrap.
1137 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1138 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1139}
1140
1141TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1142 // Start with a sequence number that will soon wrap.
1143 std::set<uint16_t> drop_seq_numbers;
1144 drop_seq_numbers.insert(0xFFFF);
1145 drop_seq_numbers.insert(0x0);
1146 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1147}
1148
1149TEST_F(NetEqDecodingTest, TimestampWrap) {
1150 // Start with a timestamp that will soon wrap.
1151 std::set<uint16_t> drop_seq_numbers;
1152 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1153}
1154
1155TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1156 // Start with a timestamp and a sequence number that will wrap at the same
1157 // time.
1158 std::set<uint16_t> drop_seq_numbers;
1159 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1160}
1161
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001162void NetEqDecodingTest::DuplicateCng() {
1163 uint16_t seq_no = 0;
1164 uint32_t timestamp = 0;
1165 const int kFrameSizeMs = 10;
1166 const int kSampleRateKhz = 16;
1167 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001168 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001169
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001170 const int algorithmic_delay_samples = std::max(
1171 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001172 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001173 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001174 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001175 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001176 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001177 for (int i = 0; i < 3; ++i) {
1178 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001179 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001180 ++seq_no;
1181 timestamp += kSamples;
1182
1183 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001184 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001185 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001186 }
1187 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001188 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001189
1190 // Insert same CNG packet twice.
1191 const int kCngPeriodMs = 100;
1192 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001193 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001194 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1195 // This is the first time this CNG packet is inserted.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001196 ASSERT_EQ(
1197 0, neteq_->InsertPacket(
1198 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001199
1200 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001201 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001202 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001203 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001204 EXPECT_FALSE(
1205 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001206 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1207 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001208
1209 // Insert the same CNG packet again. Note that at this point it is old, since
1210 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001211 ASSERT_EQ(
1212 0, neteq_->InsertPacket(
1213 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001214
1215 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1216 // we have already pulled out CNG once.
1217 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001218 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001219 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001220 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001221 EXPECT_FALSE(
1222 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001223 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001224 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001225 }
1226
1227 // Insert speech again.
1228 ++seq_no;
1229 timestamp += kCngPeriodSamples;
1230 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001231 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001232
1233 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001234 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001235 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001236 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001237 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001238 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001239 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001240 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001241}
1242
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001243TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001244
1245TEST_F(NetEqDecodingTest, CngFirst) {
1246 uint16_t seq_no = 0;
1247 uint32_t timestamp = 0;
1248 const int kFrameSizeMs = 10;
1249 const int kSampleRateKhz = 16;
1250 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1251 const int kPayloadBytes = kSamples * 2;
1252 const int kCngPeriodMs = 100;
1253 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1254 size_t payload_len;
1255
1256 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001257 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001258
1259 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001260 ASSERT_EQ(
1261 NetEq::kOK,
1262 neteq_->InsertPacket(
1263 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001264 ++seq_no;
1265 timestamp += kCngPeriodSamples;
1266
1267 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001268 bool muted;
1269 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001270 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001271 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001272
1273 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001274 const uint32_t first_speech_timestamp = timestamp;
1275 int timeout_counter = 0;
1276 do {
1277 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001278 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001279 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001280 ++seq_no;
1281 timestamp += kSamples;
1282
1283 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001284 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001285 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001286 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001287 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001288 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001289}
henrik.lundin7a926812016-05-12 13:51:28 -07001290
1291class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1292 public:
1293 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1294 config_.enable_muted_state = true;
1295 }
1296
1297 protected:
1298 static constexpr size_t kSamples = 10 * 16;
1299 static constexpr size_t kPayloadBytes = kSamples * 2;
1300
1301 void InsertPacket(uint32_t rtp_timestamp) {
1302 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001303 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001304 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001305 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001306 }
1307
henrik.lundin42feb512016-09-20 06:51:40 -07001308 void InsertCngPacket(uint32_t rtp_timestamp) {
1309 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001310 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001311 size_t payload_len;
1312 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001313 EXPECT_EQ(
1314 NetEq::kOK,
1315 neteq_->InsertPacket(
1316 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001317 }
1318
henrik.lundin7a926812016-05-12 13:51:28 -07001319 bool GetAudioReturnMuted() {
1320 bool muted;
1321 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1322 return muted;
1323 }
1324
1325 void GetAudioUntilMuted() {
1326 while (!GetAudioReturnMuted()) {
1327 ASSERT_LT(counter_++, 1000) << "Test timed out";
1328 }
1329 }
1330
1331 void GetAudioUntilNormal() {
1332 bool muted = false;
1333 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1334 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1335 ASSERT_LT(counter_++, 1000) << "Test timed out";
1336 }
1337 EXPECT_FALSE(muted);
1338 }
1339
1340 int counter_ = 0;
1341};
1342
1343// Verifies that NetEq goes in and out of muted state as expected.
1344TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1345 // Insert one speech packet.
1346 InsertPacket(0);
1347 // Pull out audio once and expect it not to be muted.
1348 EXPECT_FALSE(GetAudioReturnMuted());
1349 // Pull data until faded out.
1350 GetAudioUntilMuted();
1351
1352 // Verify that output audio is not written during muted mode. Other parameters
1353 // should be correct, though.
1354 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001355 int16_t* frame_data = new_frame.mutable_data();
1356 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1357 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001358 }
1359 bool muted;
1360 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1361 EXPECT_TRUE(muted);
yujo36b1a5f2017-06-12 12:45:32 -07001362 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1363 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001364 }
1365 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1366 new_frame.timestamp_);
1367 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1368 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1369 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1370 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1371 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1372
1373 // Insert new data. Timestamp is corrected for the time elapsed since the last
1374 // packet. Verify that normal operation resumes.
1375 InsertPacket(kSamples * counter_);
1376 GetAudioUntilNormal();
henrik.lundin612c25e2016-05-25 08:21:04 -07001377
1378 NetEqNetworkStatistics stats;
1379 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1380 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1381 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1382 // concealment samples in this test.
1383 EXPECT_GT(stats.expand_rate, 14000);
1384 // And, it should be greater than the speech_expand_rate.
1385 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001386}
1387
1388// Verifies that NetEq goes out of muted state when given a delayed packet.
1389TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1390 // Insert one speech packet.
1391 InsertPacket(0);
1392 // Pull out audio once and expect it not to be muted.
1393 EXPECT_FALSE(GetAudioReturnMuted());
1394 // Pull data until faded out.
1395 GetAudioUntilMuted();
1396 // Insert new data. Timestamp is only corrected for the half of the time
1397 // elapsed since the last packet. That is, the new packet is delayed. Verify
1398 // that normal operation resumes.
1399 InsertPacket(kSamples * counter_ / 2);
1400 GetAudioUntilNormal();
1401}
1402
1403// Verifies that NetEq goes out of muted state when given a future packet.
1404TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1405 // Insert one speech packet.
1406 InsertPacket(0);
1407 // Pull out audio once and expect it not to be muted.
1408 EXPECT_FALSE(GetAudioReturnMuted());
1409 // Pull data until faded out.
1410 GetAudioUntilMuted();
1411 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1412 // last packet. That is, the new packet is too early. Verify that normal
1413 // operation resumes.
1414 InsertPacket(kSamples * counter_ * 2);
1415 GetAudioUntilNormal();
1416}
1417
1418// Verifies that NetEq goes out of muted state when given an old packet.
1419TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1420 // Insert one speech packet.
1421 InsertPacket(0);
1422 // Pull out audio once and expect it not to be muted.
1423 EXPECT_FALSE(GetAudioReturnMuted());
1424 // Pull data until faded out.
1425 GetAudioUntilMuted();
1426
1427 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1428 // Insert packet which is older than the first packet.
1429 InsertPacket(kSamples * (counter_ - 1000));
1430 EXPECT_FALSE(GetAudioReturnMuted());
1431 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1432}
1433
henrik.lundin42feb512016-09-20 06:51:40 -07001434// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1435// packet stream is suspended for a long time.
1436TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1437 // Insert one CNG packet.
1438 InsertCngPacket(0);
1439
1440 // Pull 10 seconds of audio (10 ms audio generated per lap).
1441 for (int i = 0; i < 1000; ++i) {
1442 bool muted;
1443 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1444 ASSERT_FALSE(muted);
1445 }
1446 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1447}
1448
1449// Verifies that NetEq goes back to normal after a long CNG period with the
1450// packet stream suspended.
1451TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1452 // Insert one CNG packet.
1453 InsertCngPacket(0);
1454
1455 // Pull 10 seconds of audio (10 ms audio generated per lap).
1456 for (int i = 0; i < 1000; ++i) {
1457 bool muted;
1458 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1459 }
1460
1461 // Insert new data. Timestamp is corrected for the time elapsed since the last
1462 // packet. Verify that normal operation resumes.
1463 InsertPacket(kSamples * counter_);
1464 GetAudioUntilNormal();
1465}
1466
henrik.lundin7a926812016-05-12 13:51:28 -07001467class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1468 public:
1469 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1470
1471 void SetUp() override {
1472 NetEqDecodingTest::SetUp();
1473 config2_ = config_;
1474 }
1475
1476 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001477 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001478 ASSERT_TRUE(neteq2_);
1479 LoadDecoders(neteq2_.get());
1480 }
1481
1482 protected:
1483 std::unique_ptr<NetEq> neteq2_;
1484 NetEq::Config config2_;
1485};
1486
1487namespace {
1488::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1489 const AudioFrame& b) {
1490 if (a.timestamp_ != b.timestamp_)
1491 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1492 << " != " << b.timestamp_ << ")";
1493 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1494 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1495 << a.sample_rate_hz_
1496 << " != " << b.sample_rate_hz_ << ")";
1497 if (a.samples_per_channel_ != b.samples_per_channel_)
1498 return ::testing::AssertionFailure()
1499 << "samples_per_channel_ diff (" << a.samples_per_channel_
1500 << " != " << b.samples_per_channel_ << ")";
1501 if (a.num_channels_ != b.num_channels_)
1502 return ::testing::AssertionFailure() << "num_channels_ diff ("
1503 << a.num_channels_
1504 << " != " << b.num_channels_ << ")";
1505 if (a.speech_type_ != b.speech_type_)
1506 return ::testing::AssertionFailure() << "speech_type_ diff ("
1507 << a.speech_type_
1508 << " != " << b.speech_type_ << ")";
1509 if (a.vad_activity_ != b.vad_activity_)
1510 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1511 << a.vad_activity_
1512 << " != " << b.vad_activity_ << ")";
1513 return ::testing::AssertionSuccess();
1514}
1515
1516::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1517 const AudioFrame& b) {
1518 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1519 if (!res)
1520 return res;
1521 if (memcmp(
yujo36b1a5f2017-06-12 12:45:32 -07001522 a.data(), b.data(),
1523 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) != 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001524 return ::testing::AssertionFailure() << "data_ diff";
1525 }
1526 return ::testing::AssertionSuccess();
1527}
1528
1529} // namespace
1530
1531TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1532 ASSERT_FALSE(config_.enable_muted_state);
1533 config2_.enable_muted_state = true;
1534 CreateSecondInstance();
1535
1536 // Insert one speech packet into both NetEqs.
1537 const size_t kSamples = 10 * 16;
1538 const size_t kPayloadBytes = kSamples * 2;
1539 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001540 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001541 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001542 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1543 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001544
1545 AudioFrame out_frame1, out_frame2;
1546 bool muted;
1547 for (int i = 0; i < 1000; ++i) {
1548 std::ostringstream ss;
1549 ss << "i = " << i;
1550 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1551 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1552 EXPECT_FALSE(muted);
1553 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1554 if (muted) {
1555 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1556 } else {
1557 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1558 }
1559 }
1560 EXPECT_TRUE(muted);
1561
1562 // Insert new data. Timestamp is corrected for the time elapsed since the last
1563 // packet.
1564 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001565 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1566 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001567
1568 int counter = 0;
1569 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1570 ASSERT_LT(counter++, 1000) << "Test timed out";
1571 std::ostringstream ss;
1572 ss << "counter = " << counter;
1573 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1574 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1575 EXPECT_FALSE(muted);
1576 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1577 if (muted) {
1578 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1579 } else {
1580 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1581 }
1582 }
1583 EXPECT_FALSE(muted);
1584}
1585
henrik.lundin114c1b32017-04-26 07:47:32 -07001586TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1587 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1588
1589 // Pull out data once.
1590 AudioFrame output;
1591 bool muted;
1592 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1593
1594 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1595}
1596
1597TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1598 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1599 // default). Make the length 10 ms.
1600 constexpr size_t kPayloadSamples = 16 * 10;
1601 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1602 uint8_t payload[kPayloadBytes] = {0};
1603
1604 RTPHeader rtp_info;
1605 constexpr uint32_t kRtpTimestamp = 0x1234;
1606 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1607 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1608
1609 // Pull out data once.
1610 AudioFrame output;
1611 bool muted;
1612 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1613
1614 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1615 neteq_->LastDecodedTimestamps());
1616
1617 // Nothing decoded on the second call.
1618 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1619 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1620}
1621
1622TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1623 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1624 // by default). Make the length 5 ms so that NetEq must decode them both in
1625 // the same GetAudio call.
1626 constexpr size_t kPayloadSamples = 16 * 5;
1627 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1628 uint8_t payload[kPayloadBytes] = {0};
1629
1630 RTPHeader rtp_info;
1631 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1632 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1633 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1634 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1635 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1636 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1637
1638 // Pull out data once.
1639 AudioFrame output;
1640 bool muted;
1641 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1642
1643 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1644 neteq_->LastDecodedTimestamps());
1645}
1646
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001647} // namespace webrtc