blob: 8b3f28f54668af3bce3aca0e890f8e8783b8ee9f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020024#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
26#include "modules/audio_coding/neteq/tools/audio_loop.h"
27#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
28#include "modules/include/module_common_types.h"
29#include "rtc_base/flags.h"
30#include "rtc_base/ignore_wundef.h"
31#include "rtc_base/protobuf_utils.h"
32#include "rtc_base/sha1digest.h"
33#include "rtc_base/stringencode.h"
34#include "test/gtest.h"
35#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020036#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "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,
Henrik Lundin8cd750d2017-10-12 13:07:11 +020055 const std::string& checksum_android_32,
56 const std::string& checksum_android_64,
minyue4f906772016-04-29 11:05:14 -070057 const std::string& checksum_win_32,
58 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070059#if defined(WEBRTC_ANDROID)
Henrik Lundin8cd750d2017-10-12 13:07:11 +020060 #ifdef WEBRTC_ARCH_64_BITS
61 return checksum_android_64;
62 #else
63 return checksum_android_32;
64 #endif // WEBRTC_ARCH_64_BITS
kwiberg77eab702016-09-28 17:42:01 -070065#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070066 #ifdef WEBRTC_ARCH_64_BITS
67 return checksum_win_64;
68 #else
69 return checksum_win_32;
70 #endif // WEBRTC_ARCH_64_BITS
71#else
72 return checksum_general;
73#endif // WEBRTC_WIN
74}
75
minyue5f026d02015-12-16 07:36:04 -080076#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
77void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
78 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
79 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
80 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
81 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
82 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
minyue5f026d02015-12-16 07:36:04 -080083 stats->set_expand_rate(stats_raw.expand_rate);
84 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
85 stats->set_preemptive_rate(stats_raw.preemptive_rate);
86 stats->set_accelerate_rate(stats_raw.accelerate_rate);
87 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020088 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
minyue5f026d02015-12-16 07:36:04 -080089 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
90 stats->set_added_zero_samples(stats_raw.added_zero_samples);
91 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
92 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
93 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
94 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
95}
96
97void Convert(const webrtc::RtcpStatistics& stats_raw,
98 webrtc::neteq_unittest::RtcpStatistics* stats) {
99 stats->set_fraction_lost(stats_raw.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700100 stats->set_cumulative_lost(stats_raw.packets_lost);
minyue5f026d02015-12-16 07:36:04 -0800101 stats->set_extended_max_sequence_number(
srte186d9c32017-08-04 05:03:53 -0700102 stats_raw.extended_highest_sequence_number);
minyue5f026d02015-12-16 07:36:04 -0800103 stats->set_jitter(stats_raw.jitter);
104}
105
minyue4f906772016-04-29 11:05:14 -0700106void AddMessage(FILE* file, rtc::MessageDigest* digest,
107 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800108 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700109 if (file)
110 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
111 digest->Update(&size, sizeof(size));
112
113 if (file)
114 ASSERT_EQ(static_cast<size_t>(size),
115 fwrite(message.data(), sizeof(char), size, file));
116 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800117}
118
minyue5f026d02015-12-16 07:36:04 -0800119#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
120
henrik.lundin7a926812016-05-12 13:51:28 -0700121void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700122 ASSERT_EQ(true,
123 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
124 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
125 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700126 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
127 "pcma", 8));
128#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700129 ASSERT_EQ(true,
130 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700131#endif
132#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700133 ASSERT_EQ(true,
134 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700135#endif
136#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700137 ASSERT_EQ(true,
138 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700139#endif
140#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700141 ASSERT_EQ(true,
142 neteq->RegisterPayloadType(
143 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700144#endif
kwiberg5adaf732016-10-04 09:33:27 -0700145 ASSERT_EQ(true,
146 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
147 ASSERT_EQ(true,
148 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
149 ASSERT_EQ(true,
150 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
151 ASSERT_EQ(true,
152 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
153 ASSERT_EQ(true,
154 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700155}
minyue5f026d02015-12-16 07:36:04 -0800156} // namespace
157
minyue4f906772016-04-29 11:05:14 -0700158class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 public:
minyue4f906772016-04-29 11:05:14 -0700160 explicit ResultSink(const std::string& output_file);
161 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162
yujo36b1a5f2017-06-12 12:45:32 -0700163 template<typename T> void AddResult(const T* test_results, size_t length);
minyue4f906772016-04-29 11:05:14 -0700164
165 void AddResult(const NetEqNetworkStatistics& stats);
166 void AddResult(const RtcpStatistics& stats);
167
168 void VerifyChecksum(const std::string& ref_check_sum);
169
170 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700172 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173};
174
minyue4f906772016-04-29 11:05:14 -0700175ResultSink::ResultSink(const std::string &output_file)
176 : output_fp_(nullptr),
177 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178 if (!output_file.empty()) {
179 output_fp_ = fopen(output_file.c_str(), "wb");
180 EXPECT_TRUE(output_fp_ != NULL);
181 }
182}
183
minyue4f906772016-04-29 11:05:14 -0700184ResultSink::~ResultSink() {
185 if (output_fp_)
186 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187}
188
yujo36b1a5f2017-06-12 12:45:32 -0700189template<typename T>
190void ResultSink::AddResult(const T* test_results, size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 if (output_fp_) {
yujo36b1a5f2017-06-12 12:45:32 -0700192 ASSERT_EQ(length, fwrite(test_results, sizeof(T), length, output_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193 }
yujo36b1a5f2017-06-12 12:45:32 -0700194 digest_->Update(test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195}
196
minyue4f906772016-04-29 11:05:14 -0700197void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800198#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800199 neteq_unittest::NetEqNetworkStatistics stats;
200 Convert(stats_raw, &stats);
201
mbonadei7c2c8432017-04-07 00:59:12 -0700202 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800203 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700204 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800205#else
206 FAIL() << "Writing to reference file requires Proto Buffer.";
207#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208}
209
minyue4f906772016-04-29 11:05:14 -0700210void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800211#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800212 neteq_unittest::RtcpStatistics stats;
213 Convert(stats_raw, &stats);
214
mbonadei7c2c8432017-04-07 00:59:12 -0700215 ProtoString stats_string;
minyue5f026d02015-12-16 07:36:04 -0800216 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700217 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800218#else
219 FAIL() << "Writing to reference file requires Proto Buffer.";
220#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000221}
222
minyue4f906772016-04-29 11:05:14 -0700223void ResultSink::VerifyChecksum(const std::string& checksum) {
224 std::vector<char> buffer;
225 buffer.resize(digest_->Size());
226 digest_->Finish(&buffer[0], buffer.size());
227 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
228 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000229}
230
231class NetEqDecodingTest : public ::testing::Test {
232 protected:
233 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
234 // constants below can be changed.
235 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
237 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
238 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800239 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000240 static const int kInitSampleRateHz = 8000;
241
242 NetEqDecodingTest();
243 virtual void SetUp();
244 virtual void TearDown();
245 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800247 void Process();
minyue5f026d02015-12-16 07:36:04 -0800248
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000249 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700250 const std::string& output_checksum,
251 const std::string& network_stats_checksum,
252 const std::string& rtcp_stats_checksum,
253 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800254
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255 static void PopulateRtpInfo(int frame_index,
256 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700257 RTPHeader* rtp_info);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258 static void PopulateCng(int frame_index,
259 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700260 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000262 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000264 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
265 const std::set<uint16_t>& drop_seq_numbers,
266 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
267
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000268 void LongCngWithClockDrift(double drift_factor,
269 double network_freeze_ms,
270 bool pull_audio_during_freeze,
271 int delay_tolerance_ms,
272 int max_time_to_speech_ms);
273
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000274 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000275
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000276 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000277 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800278 std::unique_ptr<test::RtpFileSource> rtp_source_;
279 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800281 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000283 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284};
285
286// Allocating the static const so that it can be passed by reference.
287const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700288const size_t NetEqDecodingTest::kBlockSize8kHz;
289const size_t NetEqDecodingTest::kBlockSize16kHz;
290const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291const int NetEqDecodingTest::kInitSampleRateHz;
292
293NetEqDecodingTest::NetEqDecodingTest()
294 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000295 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000297 output_sample_rate_(kInitSampleRateHz),
298 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000299 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300}
301
302void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700303 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000304 NetEqNetworkStatistics stat;
305 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
306 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700308 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309}
310
311void NetEqDecodingTest::TearDown() {
312 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313}
314
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000316 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317}
318
henrik.lundin6d8e0112016-03-04 10:34:21 -0800319void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000321 while (packet_ && sim_clock_ >= packet_->time_ms()) {
322 if (packet_->payload_length_bytes() > 0) {
ivoc72c08ed2016-01-20 07:26:24 -0800323#ifndef WEBRTC_CODEC_ISAC
324 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700325 if (packet_->header().payloadType != 104)
ivoc72c08ed2016-01-20 07:26:24 -0800326#endif
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200327 ASSERT_EQ(0,
328 neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700329 packet_->header(),
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200330 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 Lundinac0a5032017-09-25 12:22:46 +0200376 uint64_t last_concealed_samples = 0;
377 uint64_t last_total_samples_received = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000378 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 std::ostringstream ss;
380 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
381 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800382 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700383 ASSERT_NO_FATAL_FAILURE(output.AddResult(
yujo36b1a5f2017-06-12 12:45:32 -0700384 out_frame_.data(), out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385
386 // Query the network statistics API once per second
387 if (sim_clock_ % 1000 == 0) {
388 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700389 NetEqNetworkStatistics current_network_stats;
390 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
391 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
392
henrik.lundin9c3efd02015-08-27 13:12:22 -0700393 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700394 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
395 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396
Henrik Lundinac0a5032017-09-25 12:22:46 +0200397 // Verify that liftime stats and network stats report similar loss
398 // concealment rates.
399 auto lifetime_stats = neteq_->GetLifetimeStatistics();
400 const uint64_t delta_concealed_samples =
401 lifetime_stats.concealed_samples - last_concealed_samples;
402 last_concealed_samples = lifetime_stats.concealed_samples;
403 const uint64_t delta_total_samples_received =
404 lifetime_stats.total_samples_received - last_total_samples_received;
405 last_total_samples_received = lifetime_stats.total_samples_received;
406 // The tolerance is 1% but expressed in Q14.
407 EXPECT_NEAR(
408 (delta_concealed_samples << 14) / delta_total_samples_received,
409 current_network_stats.expand_rate, (2 << 14) / 100.0);
410
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000411 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700412 RtcpStatistics current_rtcp_stats;
413 neteq_->GetRtcpStatistics(&current_rtcp_stats);
414 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000415 }
416 }
minyue4f906772016-04-29 11:05:14 -0700417
418 SCOPED_TRACE("Check output audio.");
419 output.VerifyChecksum(output_checksum);
420 SCOPED_TRACE("Check network stats.");
421 network_stats.VerifyChecksum(network_stats_checksum);
422 SCOPED_TRACE("Check rtcp stats.");
423 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000424}
425
426void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
427 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700428 RTPHeader* rtp_info) {
429 rtp_info->sequenceNumber = frame_index;
430 rtp_info->timestamp = timestamp;
431 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
432 rtp_info->payloadType = 94; // PCM16b WB codec.
433 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000434}
435
436void NetEqDecodingTest::PopulateCng(int frame_index,
437 int timestamp,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700438 RTPHeader* rtp_info,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000439 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000440 size_t* payload_len) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700441 rtp_info->sequenceNumber = frame_index;
442 rtp_info->timestamp = timestamp;
443 rtp_info->ssrc = 0x1234; // Just an arbitrary SSRC.
444 rtp_info->payloadType = 98; // WB CNG.
445 rtp_info->markerBit = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000446 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
447 *payload_len = 1; // Only noise level, no spectral parameters.
448}
449
ivoc72c08ed2016-01-20 07:26:24 -0800450#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
451 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
452 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700453 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800454#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700455#else
minyue5f026d02015-12-16 07:36:04 -0800456#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700457#endif
minyue5f026d02015-12-16 07:36:04 -0800458TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800459 const std::string input_rtp_file =
460 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000461
minyue4f906772016-04-29 11:05:14 -0700462 const std::string output_checksum = PlatformChecksum(
soren9f2c18e2017-04-10 02:22:46 -0700463 "09fa7646e2ad032a0b156177b95f09012430f81f",
464 "1c64eb8b55ce8878676c6a1e6ddd78f48de0668b",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200465 "not used",
soren9f2c18e2017-04-10 02:22:46 -0700466 "09fa7646e2ad032a0b156177b95f09012430f81f",
467 "759fef89a5de52bd17e733dc255c671ce86be909");
minyue4f906772016-04-29 11:05:14 -0700468
henrik.lundin2979f552017-05-05 05:04:16 -0700469 const std::string network_stats_checksum =
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200470 PlatformChecksum("5b4262ca328e5f066af5d34f3380521583dd20de",
471 "80235b6d727281203acb63b98f9a9e85d95f7ec0",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200472 "not used",
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200473 "5b4262ca328e5f066af5d34f3380521583dd20de",
474 "5b4262ca328e5f066af5d34f3380521583dd20de");
minyue4f906772016-04-29 11:05:14 -0700475
476 const std::string rtcp_stats_checksum = PlatformChecksum(
477 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
478 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200479 "not used",
minyue4f906772016-04-29 11:05:14 -0700480 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
481 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
482
483 DecodeAndCompare(input_rtp_file,
484 output_checksum,
485 network_stats_checksum,
486 rtcp_stats_checksum,
oprypin9b2f20c2017-08-29 05:51:57 -0700487 FLAG_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000488}
489
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200490#if !defined(WEBRTC_IOS) && \
minyue93c08b72015-12-22 09:57:41 -0800491 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyue-webrtc516711c2017-07-27 17:45:49 +0200492 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800493#define MAYBE_TestOpusBitExactness TestOpusBitExactness
494#else
495#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
496#endif
minyue-webrtcadb58b82017-07-26 17:59:59 +0200497TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800498 const std::string input_rtp_file =
499 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800500
minyue4f906772016-04-29 11:05:14 -0700501 const std::string output_checksum = PlatformChecksum(
minyue-webrtcadb58b82017-07-26 17:59:59 +0200502 "721e1e0c6effe4b2401536a4eef11512c9fb709c",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200503 "2e3c3e451532967e981fbc39b8cfb55e1df1ff7f",
504 "f403940a1936bff040d1d158624f69bdccbc3423",
minyue-webrtcadb58b82017-07-26 17:59:59 +0200505 "721e1e0c6effe4b2401536a4eef11512c9fb709c",
506 "721e1e0c6effe4b2401536a4eef11512c9fb709c");
minyue4f906772016-04-29 11:05:14 -0700507
henrik.lundin2979f552017-05-05 05:04:16 -0700508 const std::string network_stats_checksum =
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200509 PlatformChecksum("4e749c46e2611877120ac7a20cbbe555cfbd70ea",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200510 "1edee6d07e0005327c32a77f9b3c0c1f03780e9f",
511 "ff806c574f82a089dec4c37ea1224b1eb0822d23",
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200512 "4e749c46e2611877120ac7a20cbbe555cfbd70ea",
513 "4e749c46e2611877120ac7a20cbbe555cfbd70ea");
minyue4f906772016-04-29 11:05:14 -0700514
515 const std::string rtcp_stats_checksum = PlatformChecksum(
516 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
517 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
518 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
Henrik Lundin8cd750d2017-10-12 13:07:11 +0200519 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
minyue4f906772016-04-29 11:05:14 -0700520 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
521
522 DecodeAndCompare(input_rtp_file,
523 output_checksum,
524 network_stats_checksum,
525 rtcp_stats_checksum,
oprypin9b2f20c2017-08-29 05:51:57 -0700526 FLAG_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800527}
528
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000529// Use fax mode to avoid time-scaling. This is to simplify the testing of
530// packet waiting times in the packet buffer.
531class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
532 protected:
533 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
534 config_.playout_mode = kPlayoutFax;
535 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200536 void TestJitterBufferDelay(bool apply_packet_loss);
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000537};
538
539TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000540 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
541 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000542 const size_t kSamples = 10 * 16;
543 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000544 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800545 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700546 RTPHeader rtp_info;
547 rtp_info.sequenceNumber = i;
548 rtp_info.timestamp = i * kSamples;
549 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
550 rtp_info.payloadType = 94; // PCM16b WB codec.
551 rtp_info.markerBit = 0;
552 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000553 }
554 // Pull out all data.
555 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700556 bool muted;
557 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800558 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559 }
560
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200561 NetEqNetworkStatistics stats;
562 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000563 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
564 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200565 // each packet. Thus, we are calculating the statistics for a series from 10
566 // to 300, in steps of 10 ms.
567 EXPECT_EQ(155, stats.mean_waiting_time_ms);
568 EXPECT_EQ(155, stats.median_waiting_time_ms);
569 EXPECT_EQ(10, stats.min_waiting_time_ms);
570 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000571
572 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200573 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
574 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
575 EXPECT_EQ(-1, stats.median_waiting_time_ms);
576 EXPECT_EQ(-1, stats.min_waiting_time_ms);
577 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578}
579
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000580TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000581 const int kNumFrames = 3000; // Needed for convergence.
582 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000583 const size_t kSamples = 10 * 16;
584 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585 while (frame_index < kNumFrames) {
586 // Insert one packet each time, except every 10th time where we insert two
587 // packets at once. This will create a negative clock-drift of approx. 10%.
588 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
589 for (int n = 0; n < num_packets; ++n) {
590 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700591 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000592 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700593 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000594 ++frame_index;
595 }
596
597 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700598 bool muted;
599 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800600 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000601 }
602
603 NetEqNetworkStatistics network_stats;
604 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700605 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606}
607
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000608TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609 const int kNumFrames = 5000; // Needed for convergence.
610 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000611 const size_t kSamples = 10 * 16;
612 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000613 for (int i = 0; i < kNumFrames; ++i) {
614 // Insert one packet each time, except every 10th time where we don't insert
615 // any packet. This will create a positive clock-drift of approx. 11%.
616 int num_packets = (i % 10 == 9 ? 0 : 1);
617 for (int n = 0; n < num_packets; ++n) {
618 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700619 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000620 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700621 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622 ++frame_index;
623 }
624
625 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700626 bool muted;
627 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800628 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629 }
630
631 NetEqNetworkStatistics network_stats;
632 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700633 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000634}
635
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000636void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
637 double network_freeze_ms,
638 bool pull_audio_during_freeze,
639 int delay_tolerance_ms,
640 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000641 uint16_t seq_no = 0;
642 uint32_t timestamp = 0;
643 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000644 const size_t kSamples = kFrameSizeMs * 16;
645 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646 double next_input_time_ms = 0.0;
647 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700648 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649
650 // Insert speech for 5 seconds.
651 const int kSpeechDurationMs = 5000;
652 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
653 // Each turn in this for loop is 10 ms.
654 while (next_input_time_ms <= t_ms) {
655 // Insert one 30 ms speech frame.
656 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700657 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000658 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700659 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000660 ++seq_no;
661 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000662 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000663 }
664 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700665 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800666 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000667 }
668
henrik.lundin55480f52016-03-08 02:37:57 -0800669 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -0700670 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700671 ASSERT_TRUE(playout_timestamp);
672 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673
674 // Insert CNG for 1 minute (= 60000 ms).
675 const int kCngPeriodMs = 100;
676 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
677 const int kCngDurationMs = 60000;
678 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
679 // Each turn in this for loop is 10 ms.
680 while (next_input_time_ms <= t_ms) {
681 // Insert one CNG frame each 100 ms.
682 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000683 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700684 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000685 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800686 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700687 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800688 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000689 ++seq_no;
690 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000691 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692 }
693 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700694 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800695 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000696 }
697
henrik.lundin55480f52016-03-08 02:37:57 -0800698 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000699
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000700 if (network_freeze_ms > 0) {
701 // First keep pulling audio for |network_freeze_ms| without inserting
702 // any data, then insert CNG data corresponding to |network_freeze_ms|
703 // without pulling any output audio.
704 const double loop_end_time = t_ms + network_freeze_ms;
705 for (; t_ms < loop_end_time; t_ms += 10) {
706 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700707 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800708 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800709 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000710 }
711 bool pull_once = pull_audio_during_freeze;
712 // If |pull_once| is true, GetAudio will be called once half-way through
713 // the network recovery period.
714 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
715 while (next_input_time_ms <= t_ms) {
716 if (pull_once && next_input_time_ms >= pull_time_ms) {
717 pull_once = false;
718 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700719 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800720 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800721 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000722 t_ms += 10;
723 }
724 // Insert one CNG frame each 100 ms.
725 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000726 size_t payload_len;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700727 RTPHeader rtp_info;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000728 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800729 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700730 rtp_info,
kwibergee2bac22015-11-11 10:34:00 -0800731 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 ++seq_no;
733 timestamp += kCngPeriodSamples;
734 next_input_time_ms += kCngPeriodMs * drift_factor;
735 }
736 }
737
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000739 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800740 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741 // Each turn in this for loop is 10 ms.
742 while (next_input_time_ms <= t_ms) {
743 // Insert one 30 ms speech frame.
744 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700745 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000746 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700747 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000748 ++seq_no;
749 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000750 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000751 }
752 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700753 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800754 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000755 // Increase clock.
756 t_ms += 10;
757 }
758
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000759 // Check that the speech starts again within reasonable time.
760 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
761 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin114c1b32017-04-26 07:47:32 -0700762 playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -0700763 ASSERT_TRUE(playout_timestamp);
764 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000765 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000766 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
767 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000768}
769
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000770TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000771 // Apply a clock drift of -25 ms / s (sender faster than receiver).
772 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000773 const double kNetworkFreezeTimeMs = 0.0;
774 const bool kGetAudioDuringFreezeRecovery = false;
775 const int kDelayToleranceMs = 20;
776 const int kMaxTimeToSpeechMs = 100;
777 LongCngWithClockDrift(kDriftFactor,
778 kNetworkFreezeTimeMs,
779 kGetAudioDuringFreezeRecovery,
780 kDelayToleranceMs,
781 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000782}
783
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000784TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000785 // Apply a clock drift of +25 ms / s (sender slower than receiver).
786 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000787 const double kNetworkFreezeTimeMs = 0.0;
788 const bool kGetAudioDuringFreezeRecovery = false;
789 const int kDelayToleranceMs = 20;
790 const int kMaxTimeToSpeechMs = 100;
791 LongCngWithClockDrift(kDriftFactor,
792 kNetworkFreezeTimeMs,
793 kGetAudioDuringFreezeRecovery,
794 kDelayToleranceMs,
795 kMaxTimeToSpeechMs);
796}
797
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000798TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000799 // Apply a clock drift of -25 ms / s (sender faster than receiver).
800 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
801 const double kNetworkFreezeTimeMs = 5000.0;
802 const bool kGetAudioDuringFreezeRecovery = false;
803 const int kDelayToleranceMs = 50;
804 const int kMaxTimeToSpeechMs = 200;
805 LongCngWithClockDrift(kDriftFactor,
806 kNetworkFreezeTimeMs,
807 kGetAudioDuringFreezeRecovery,
808 kDelayToleranceMs,
809 kMaxTimeToSpeechMs);
810}
811
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000812TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000813 // Apply a clock drift of +25 ms / s (sender slower than receiver).
814 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
815 const double kNetworkFreezeTimeMs = 5000.0;
816 const bool kGetAudioDuringFreezeRecovery = false;
817 const int kDelayToleranceMs = 20;
818 const int kMaxTimeToSpeechMs = 100;
819 LongCngWithClockDrift(kDriftFactor,
820 kNetworkFreezeTimeMs,
821 kGetAudioDuringFreezeRecovery,
822 kDelayToleranceMs,
823 kMaxTimeToSpeechMs);
824}
825
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000826TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000827 // Apply a clock drift of +25 ms / s (sender slower than receiver).
828 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
829 const double kNetworkFreezeTimeMs = 5000.0;
830 const bool kGetAudioDuringFreezeRecovery = true;
831 const int kDelayToleranceMs = 20;
832 const int kMaxTimeToSpeechMs = 100;
833 LongCngWithClockDrift(kDriftFactor,
834 kNetworkFreezeTimeMs,
835 kGetAudioDuringFreezeRecovery,
836 kDelayToleranceMs,
837 kMaxTimeToSpeechMs);
838}
839
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000840TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000841 const double kDriftFactor = 1.0; // No drift.
842 const double kNetworkFreezeTimeMs = 0.0;
843 const bool kGetAudioDuringFreezeRecovery = false;
844 const int kDelayToleranceMs = 10;
845 const int kMaxTimeToSpeechMs = 50;
846 LongCngWithClockDrift(kDriftFactor,
847 kNetworkFreezeTimeMs,
848 kGetAudioDuringFreezeRecovery,
849 kDelayToleranceMs,
850 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000851}
852
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000853TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000854 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000855 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700856 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000857 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700858 rtp_info.payloadType = 1; // Not registered as a decoder.
859 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000860}
861
Peter Boströme2976c82016-01-04 22:44:05 +0100862#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800863#define MAYBE_DecoderError DecoderError
864#else
865#define MAYBE_DecoderError DISABLED_DecoderError
866#endif
867
Peter Boströme2976c82016-01-04 22:44:05 +0100868TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000869 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700871 RTPHeader rtp_info;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000872 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700873 rtp_info.payloadType = 103; // iSAC, but the payload is invalid.
874 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000875 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
876 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700877 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800878 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700879 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 }
henrik.lundin7a926812016-05-12 13:51:28 -0700881 bool muted;
882 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
883 ASSERT_FALSE(muted);
ivoc72c08ed2016-01-20 07:26:24 -0800884
yujo36b1a5f2017-06-12 12:45:32 -0700885 // Verify that the first 160 samples are set to 0.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000886 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700887 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000888 for (int i = 0; i < kExpectedOutputLength; ++i) {
889 std::ostringstream ss;
890 ss << "i = " << i;
891 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700892 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000893 }
894}
895
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000896TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000897 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
898 // to GetAudio.
yujo36b1a5f2017-06-12 12:45:32 -0700899 int16_t* out_frame_data = out_frame_.mutable_data();
henrik.lundin6d8e0112016-03-04 10:34:21 -0800900 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700901 out_frame_data[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000902 }
henrik.lundin7a926812016-05-12 13:51:28 -0700903 bool muted;
904 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
905 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 // Verify that the first block of samples is set to 0.
907 static const int kExpectedOutputLength =
908 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
yujo36b1a5f2017-06-12 12:45:32 -0700909 const int16_t* const_out_frame_data = out_frame_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000910 for (int i = 0; i < kExpectedOutputLength; ++i) {
911 std::ostringstream ss;
912 ss << "i = " << i;
913 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
yujo36b1a5f2017-06-12 12:45:32 -0700914 EXPECT_EQ(0, const_out_frame_data[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000915 }
henrik.lundind89814b2015-11-23 06:49:25 -0800916 // Verify that the sample rate did not change from the initial configuration.
917 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000918}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000919
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000920class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000921 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000922 virtual void TestCondition(double sum_squared_noise,
923 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000924
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000925 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700926 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000927 uint8_t payload_type = 0xFF; // Invalid.
928 if (sampling_rate_hz == 8000) {
929 expected_samples_per_channel = kBlockSize8kHz;
930 payload_type = 93; // PCM 16, 8 kHz.
931 } else if (sampling_rate_hz == 16000) {
932 expected_samples_per_channel = kBlockSize16kHz;
933 payload_type = 94; // PCM 16, 16 kHZ.
934 } else if (sampling_rate_hz == 32000) {
935 expected_samples_per_channel = kBlockSize32kHz;
936 payload_type = 95; // PCM 16, 32 kHz.
937 } else {
938 ASSERT_TRUE(false); // Unsupported test case.
939 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000940
henrik.lundin6d8e0112016-03-04 10:34:21 -0800941 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000942 test::AudioLoop input;
943 // We are using the same 32 kHz input file for all tests, regardless of
944 // |sampling_rate_hz|. The output may sound weird, but the test is still
945 // valid.
946 ASSERT_TRUE(input.Init(
947 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
948 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700949 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000950
951 // Payload of 10 ms of PCM16 32 kHz.
952 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin246ef3e2017-04-24 09:14:32 -0700953 RTPHeader rtp_info;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000954 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700955 rtp_info.payloadType = payload_type;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000956
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000957 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700958 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000959 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800960 auto block = input.GetNextBlock();
961 ASSERT_EQ(expected_samples_per_channel, block.size());
962 size_t enc_len_bytes =
963 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000964 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
965
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200966 ASSERT_EQ(0, neteq_->InsertPacket(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700967 rtp_info,
Henrik Lundin70c09bd2017-04-24 15:56:56 +0200968 rtc::ArrayView<const uint8_t>(payload, enc_len_bytes),
969 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700971 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800972 ASSERT_EQ(1u, output.num_channels_);
973 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800974 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000975
976 // Next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700977 rtp_info.timestamp += expected_samples_per_channel;
978 rtp_info.sequenceNumber++;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000979 receive_timestamp += expected_samples_per_channel;
980 }
981
henrik.lundin6d8e0112016-03-04 10:34:21 -0800982 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000983
984 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
985 // one frame without checking speech-type. This is the first frame pulled
986 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700987 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800988 ASSERT_EQ(1u, output.num_channels_);
989 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000990
991 // To be able to test the fading of background noise we need at lease to
992 // pull 611 frames.
993 const int kFadingThreshold = 611;
994
995 // Test several CNG-to-PLC packet for the expected behavior. The number 20
996 // is arbitrary, but sufficiently large to test enough number of frames.
997 const int kNumPlcToCngTestFrames = 20;
998 bool plc_to_cng = false;
999 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001000 output.Reset();
yujo36b1a5f2017-06-12 12:45:32 -07001001 // Set to non-zero.
1002 memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes);
henrik.lundin7a926812016-05-12 13:51:28 -07001003 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1004 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001005 ASSERT_EQ(1u, output.num_channels_);
1006 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001007 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001008 plc_to_cng = true;
1009 double sum_squared = 0;
yujo36b1a5f2017-06-12 12:45:32 -07001010 const int16_t* output_data = output.data();
henrik.lundin6d8e0112016-03-04 10:34:21 -08001011 for (size_t k = 0;
1012 k < output.num_channels_ * output.samples_per_channel_; ++k)
yujo36b1a5f2017-06-12 12:45:32 -07001013 sum_squared += output_data[k] * output_data[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001014 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001015 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001016 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001017 }
1018 }
1019 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1020 }
1021};
1022
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001023class NetEqBgnTestOn : public NetEqBgnTest {
1024 protected:
1025 NetEqBgnTestOn() : NetEqBgnTest() {
1026 config_.background_noise_mode = NetEq::kBgnOn;
1027 }
1028
1029 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1030 EXPECT_NE(0, sum_squared_noise);
1031 }
1032};
1033
1034class NetEqBgnTestOff : public NetEqBgnTest {
1035 protected:
1036 NetEqBgnTestOff() : NetEqBgnTest() {
1037 config_.background_noise_mode = NetEq::kBgnOff;
1038 }
1039
1040 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1041 EXPECT_EQ(0, sum_squared_noise);
1042 }
1043};
1044
1045class NetEqBgnTestFade : public NetEqBgnTest {
1046 protected:
1047 NetEqBgnTestFade() : NetEqBgnTest() {
1048 config_.background_noise_mode = NetEq::kBgnFade;
1049 }
1050
1051 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1052 if (should_be_faded)
1053 EXPECT_EQ(0, sum_squared_noise);
1054 }
1055};
1056
henrika1d34fe92015-06-16 10:04:20 +02001057TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001058 CheckBgn(8000);
1059 CheckBgn(16000);
1060 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001061}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001062
henrika1d34fe92015-06-16 10:04:20 +02001063TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001064 CheckBgn(8000);
1065 CheckBgn(16000);
1066 CheckBgn(32000);
1067}
1068
henrika1d34fe92015-06-16 10:04:20 +02001069TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001070 CheckBgn(8000);
1071 CheckBgn(16000);
1072 CheckBgn(32000);
1073}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001074
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001075void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1076 uint32_t start_timestamp,
1077 const std::set<uint16_t>& drop_seq_numbers,
1078 bool expect_seq_no_wrap,
1079 bool expect_timestamp_wrap) {
1080 uint16_t seq_no = start_seq_no;
1081 uint32_t timestamp = start_timestamp;
1082 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1083 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1084 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001085 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001086 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001087 uint32_t receive_timestamp = 0;
1088
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001089 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001090 const int kSpeechDurationMs = 2000;
1091 int packets_inserted = 0;
1092 uint16_t last_seq_no;
1093 uint32_t last_timestamp;
1094 bool timestamp_wrapped = false;
1095 bool seq_no_wrapped = false;
1096 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1097 // Each turn in this for loop is 10 ms.
1098 while (next_input_time_ms <= t_ms) {
1099 // Insert one 30 ms speech frame.
1100 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001101 RTPHeader rtp_info;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001102 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1103 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1104 // This sequence number was not in the set to drop. Insert it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001105 ASSERT_EQ(0,
1106 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001107 ++packets_inserted;
1108 }
1109 NetEqNetworkStatistics network_stats;
1110 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1111
1112 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1113 // packet size for first few packets. Therefore we refrain from checking
1114 // the criteria.
1115 if (packets_inserted > 4) {
1116 // Expect preferred and actual buffer size to be no more than 2 frames.
1117 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001118 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1119 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001120 }
1121 last_seq_no = seq_no;
1122 last_timestamp = timestamp;
1123
1124 ++seq_no;
1125 timestamp += kSamples;
1126 receive_timestamp += kSamples;
1127 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1128
1129 seq_no_wrapped |= seq_no < last_seq_no;
1130 timestamp_wrapped |= timestamp < last_timestamp;
1131 }
1132 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001133 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001134 bool muted;
1135 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001136 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1137 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001138
1139 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin114c1b32017-04-26 07:47:32 -07001140 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001141 ASSERT_TRUE(playout_timestamp);
1142 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001143 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001144 }
1145 // Make sure we have actually tested wrap-around.
1146 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1147 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1148}
1149
1150TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1151 // Start with a sequence number that will soon wrap.
1152 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1153 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1154}
1155
1156TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1157 // Start with a sequence number that will soon wrap.
1158 std::set<uint16_t> drop_seq_numbers;
1159 drop_seq_numbers.insert(0xFFFF);
1160 drop_seq_numbers.insert(0x0);
1161 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1162}
1163
1164TEST_F(NetEqDecodingTest, TimestampWrap) {
1165 // Start with a timestamp that will soon wrap.
1166 std::set<uint16_t> drop_seq_numbers;
1167 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1168}
1169
1170TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1171 // Start with a timestamp and a sequence number that will wrap at the same
1172 // time.
1173 std::set<uint16_t> drop_seq_numbers;
1174 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1175}
1176
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001177void NetEqDecodingTest::DuplicateCng() {
1178 uint16_t seq_no = 0;
1179 uint32_t timestamp = 0;
1180 const int kFrameSizeMs = 10;
1181 const int kSampleRateKhz = 16;
1182 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001183 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001184
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001185 const int algorithmic_delay_samples = std::max(
1186 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001187 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001188 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001189 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001190 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001191 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001192 for (int i = 0; i < 3; ++i) {
1193 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001194 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001195 ++seq_no;
1196 timestamp += kSamples;
1197
1198 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001199 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001200 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001201 }
1202 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001203 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001204
1205 // Insert same CNG packet twice.
1206 const int kCngPeriodMs = 100;
1207 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001208 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001209 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1210 // This is the first time this CNG packet is inserted.
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 once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001216 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001217 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001218 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001219 EXPECT_FALSE(
1220 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001221 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1222 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001223
1224 // Insert the same CNG packet again. Note that at this point it is old, since
1225 // we have already decoded the first copy of it.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001226 ASSERT_EQ(
1227 0, neteq_->InsertPacket(
1228 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001229
1230 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1231 // we have already pulled out CNG once.
1232 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001233 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001234 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001235 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001236 EXPECT_FALSE(
1237 neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001238 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001239 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001240 }
1241
1242 // Insert speech again.
1243 ++seq_no;
1244 timestamp += kCngPeriodSamples;
1245 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001246 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001247
1248 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001249 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001250 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001251 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin114c1b32017-04-26 07:47:32 -07001252 rtc::Optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin0d96ab72016-04-06 12:28:26 -07001253 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001254 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001255 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001256}
1257
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001258TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001259
1260TEST_F(NetEqDecodingTest, CngFirst) {
1261 uint16_t seq_no = 0;
1262 uint32_t timestamp = 0;
1263 const int kFrameSizeMs = 10;
1264 const int kSampleRateKhz = 16;
1265 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1266 const int kPayloadBytes = kSamples * 2;
1267 const int kCngPeriodMs = 100;
1268 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1269 size_t payload_len;
1270
1271 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001272 RTPHeader rtp_info;
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001273
1274 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001275 ASSERT_EQ(
1276 NetEq::kOK,
1277 neteq_->InsertPacket(
1278 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001279 ++seq_no;
1280 timestamp += kCngPeriodSamples;
1281
1282 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001283 bool muted;
1284 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.lundin55480f52016-03-08 02:37:57 -08001286 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001287
1288 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001289 const uint32_t first_speech_timestamp = timestamp;
1290 int timeout_counter = 0;
1291 do {
1292 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001293 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001294 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001295 ++seq_no;
1296 timestamp += kSamples;
1297
1298 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001299 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001300 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001301 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001302 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001303 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001304}
henrik.lundin7a926812016-05-12 13:51:28 -07001305
1306class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1307 public:
1308 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1309 config_.enable_muted_state = true;
1310 }
1311
1312 protected:
1313 static constexpr size_t kSamples = 10 * 16;
1314 static constexpr size_t kPayloadBytes = kSamples * 2;
1315
1316 void InsertPacket(uint32_t rtp_timestamp) {
1317 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001318 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001319 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001320 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001321 }
1322
henrik.lundin42feb512016-09-20 06:51:40 -07001323 void InsertCngPacket(uint32_t rtp_timestamp) {
1324 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001325 RTPHeader rtp_info;
henrik.lundin42feb512016-09-20 06:51:40 -07001326 size_t payload_len;
1327 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001328 EXPECT_EQ(
1329 NetEq::kOK,
1330 neteq_->InsertPacket(
1331 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin42feb512016-09-20 06:51:40 -07001332 }
1333
henrik.lundin7a926812016-05-12 13:51:28 -07001334 bool GetAudioReturnMuted() {
1335 bool muted;
1336 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1337 return muted;
1338 }
1339
1340 void GetAudioUntilMuted() {
1341 while (!GetAudioReturnMuted()) {
1342 ASSERT_LT(counter_++, 1000) << "Test timed out";
1343 }
1344 }
1345
1346 void GetAudioUntilNormal() {
1347 bool muted = false;
1348 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1349 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1350 ASSERT_LT(counter_++, 1000) << "Test timed out";
1351 }
1352 EXPECT_FALSE(muted);
1353 }
1354
1355 int counter_ = 0;
1356};
1357
1358// Verifies that NetEq goes in and out of muted state as expected.
1359TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1360 // Insert one speech packet.
1361 InsertPacket(0);
1362 // Pull out audio once and expect it not to be muted.
1363 EXPECT_FALSE(GetAudioReturnMuted());
1364 // Pull data until faded out.
1365 GetAudioUntilMuted();
henrik.lundina4491072017-07-06 05:23:53 -07001366 EXPECT_TRUE(out_frame_.muted());
henrik.lundin7a926812016-05-12 13:51:28 -07001367
1368 // Verify that output audio is not written during muted mode. Other parameters
1369 // should be correct, though.
1370 AudioFrame new_frame;
yujo36b1a5f2017-06-12 12:45:32 -07001371 int16_t* frame_data = new_frame.mutable_data();
1372 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1373 frame_data[i] = 17;
henrik.lundin7a926812016-05-12 13:51:28 -07001374 }
1375 bool muted;
1376 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1377 EXPECT_TRUE(muted);
henrik.lundina4491072017-07-06 05:23:53 -07001378 EXPECT_TRUE(out_frame_.muted());
yujo36b1a5f2017-06-12 12:45:32 -07001379 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
1380 EXPECT_EQ(17, frame_data[i]);
henrik.lundin7a926812016-05-12 13:51:28 -07001381 }
1382 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1383 new_frame.timestamp_);
1384 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1385 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1386 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1387 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1388 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1389
1390 // Insert new data. Timestamp is corrected for the time elapsed since the last
1391 // packet. Verify that normal operation resumes.
1392 InsertPacket(kSamples * counter_);
1393 GetAudioUntilNormal();
henrik.lundina4491072017-07-06 05:23:53 -07001394 EXPECT_FALSE(out_frame_.muted());
henrik.lundin612c25e2016-05-25 08:21:04 -07001395
1396 NetEqNetworkStatistics stats;
1397 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1398 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1399 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1400 // concealment samples in this test.
1401 EXPECT_GT(stats.expand_rate, 14000);
1402 // And, it should be greater than the speech_expand_rate.
1403 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001404}
1405
1406// Verifies that NetEq goes out of muted state when given a delayed packet.
1407TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1408 // Insert one speech packet.
1409 InsertPacket(0);
1410 // Pull out audio once and expect it not to be muted.
1411 EXPECT_FALSE(GetAudioReturnMuted());
1412 // Pull data until faded out.
1413 GetAudioUntilMuted();
1414 // Insert new data. Timestamp is only corrected for the half of the time
1415 // elapsed since the last packet. That is, the new packet is delayed. Verify
1416 // that normal operation resumes.
1417 InsertPacket(kSamples * counter_ / 2);
1418 GetAudioUntilNormal();
1419}
1420
1421// Verifies that NetEq goes out of muted state when given a future packet.
1422TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1423 // Insert one speech packet.
1424 InsertPacket(0);
1425 // Pull out audio once and expect it not to be muted.
1426 EXPECT_FALSE(GetAudioReturnMuted());
1427 // Pull data until faded out.
1428 GetAudioUntilMuted();
1429 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1430 // last packet. That is, the new packet is too early. Verify that normal
1431 // operation resumes.
1432 InsertPacket(kSamples * counter_ * 2);
1433 GetAudioUntilNormal();
1434}
1435
1436// Verifies that NetEq goes out of muted state when given an old packet.
1437TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1438 // Insert one speech packet.
1439 InsertPacket(0);
1440 // Pull out audio once and expect it not to be muted.
1441 EXPECT_FALSE(GetAudioReturnMuted());
1442 // Pull data until faded out.
1443 GetAudioUntilMuted();
1444
1445 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1446 // Insert packet which is older than the first packet.
1447 InsertPacket(kSamples * (counter_ - 1000));
1448 EXPECT_FALSE(GetAudioReturnMuted());
1449 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1450}
1451
henrik.lundin42feb512016-09-20 06:51:40 -07001452// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1453// packet stream is suspended for a long time.
1454TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1455 // Insert one CNG packet.
1456 InsertCngPacket(0);
1457
1458 // Pull 10 seconds of audio (10 ms audio generated per lap).
1459 for (int i = 0; i < 1000; ++i) {
1460 bool muted;
1461 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1462 ASSERT_FALSE(muted);
1463 }
1464 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1465}
1466
1467// Verifies that NetEq goes back to normal after a long CNG period with the
1468// packet stream suspended.
1469TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1470 // Insert one CNG packet.
1471 InsertCngPacket(0);
1472
1473 // Pull 10 seconds of audio (10 ms audio generated per lap).
1474 for (int i = 0; i < 1000; ++i) {
1475 bool muted;
1476 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1477 }
1478
1479 // Insert new data. Timestamp is corrected for the time elapsed since the last
1480 // packet. Verify that normal operation resumes.
1481 InsertPacket(kSamples * counter_);
1482 GetAudioUntilNormal();
1483}
1484
henrik.lundin7a926812016-05-12 13:51:28 -07001485class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1486 public:
1487 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1488
1489 void SetUp() override {
1490 NetEqDecodingTest::SetUp();
1491 config2_ = config_;
1492 }
1493
1494 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001495 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001496 ASSERT_TRUE(neteq2_);
1497 LoadDecoders(neteq2_.get());
1498 }
1499
1500 protected:
1501 std::unique_ptr<NetEq> neteq2_;
1502 NetEq::Config config2_;
1503};
1504
1505namespace {
1506::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1507 const AudioFrame& b) {
1508 if (a.timestamp_ != b.timestamp_)
1509 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1510 << " != " << b.timestamp_ << ")";
1511 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1512 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1513 << a.sample_rate_hz_
1514 << " != " << b.sample_rate_hz_ << ")";
1515 if (a.samples_per_channel_ != b.samples_per_channel_)
1516 return ::testing::AssertionFailure()
1517 << "samples_per_channel_ diff (" << a.samples_per_channel_
1518 << " != " << b.samples_per_channel_ << ")";
1519 if (a.num_channels_ != b.num_channels_)
1520 return ::testing::AssertionFailure() << "num_channels_ diff ("
1521 << a.num_channels_
1522 << " != " << b.num_channels_ << ")";
1523 if (a.speech_type_ != b.speech_type_)
1524 return ::testing::AssertionFailure() << "speech_type_ diff ("
1525 << a.speech_type_
1526 << " != " << b.speech_type_ << ")";
1527 if (a.vad_activity_ != b.vad_activity_)
1528 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1529 << a.vad_activity_
1530 << " != " << b.vad_activity_ << ")";
1531 return ::testing::AssertionSuccess();
1532}
1533
1534::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1535 const AudioFrame& b) {
1536 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1537 if (!res)
1538 return res;
1539 if (memcmp(
yujo36b1a5f2017-06-12 12:45:32 -07001540 a.data(), b.data(),
1541 a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) != 0) {
henrik.lundin7a926812016-05-12 13:51:28 -07001542 return ::testing::AssertionFailure() << "data_ diff";
1543 }
1544 return ::testing::AssertionSuccess();
1545}
1546
1547} // namespace
1548
1549TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1550 ASSERT_FALSE(config_.enable_muted_state);
1551 config2_.enable_muted_state = true;
1552 CreateSecondInstance();
1553
1554 // Insert one speech packet into both NetEqs.
1555 const size_t kSamples = 10 * 16;
1556 const size_t kPayloadBytes = kSamples * 2;
1557 uint8_t payload[kPayloadBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001558 RTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001559 PopulateRtpInfo(0, 0, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001560 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1561 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001562
1563 AudioFrame out_frame1, out_frame2;
1564 bool muted;
1565 for (int i = 0; i < 1000; ++i) {
1566 std::ostringstream ss;
1567 ss << "i = " << i;
1568 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1569 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1570 EXPECT_FALSE(muted);
1571 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1572 if (muted) {
1573 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1574 } else {
1575 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1576 }
1577 }
1578 EXPECT_TRUE(muted);
1579
1580 // Insert new data. Timestamp is corrected for the time elapsed since the last
1581 // packet.
1582 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
henrik.lundin246ef3e2017-04-24 09:14:32 -07001583 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1584 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
henrik.lundin7a926812016-05-12 13:51:28 -07001585
1586 int counter = 0;
1587 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1588 ASSERT_LT(counter++, 1000) << "Test timed out";
1589 std::ostringstream ss;
1590 ss << "counter = " << counter;
1591 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1592 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1593 EXPECT_FALSE(muted);
1594 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1595 if (muted) {
1596 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1597 } else {
1598 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1599 }
1600 }
1601 EXPECT_FALSE(muted);
1602}
1603
henrik.lundin114c1b32017-04-26 07:47:32 -07001604TEST_F(NetEqDecodingTest, LastDecodedTimestampsEmpty) {
1605 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1606
1607 // Pull out data once.
1608 AudioFrame output;
1609 bool muted;
1610 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1611
1612 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1613}
1614
1615TEST_F(NetEqDecodingTest, LastDecodedTimestampsOneDecoded) {
1616 // Insert one packet with PCM16b WB data (this is what PopulateRtpInfo does by
1617 // default). Make the length 10 ms.
1618 constexpr size_t kPayloadSamples = 16 * 10;
1619 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1620 uint8_t payload[kPayloadBytes] = {0};
1621
1622 RTPHeader rtp_info;
1623 constexpr uint32_t kRtpTimestamp = 0x1234;
1624 PopulateRtpInfo(0, kRtpTimestamp, &rtp_info);
1625 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1626
1627 // Pull out data once.
1628 AudioFrame output;
1629 bool muted;
1630 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1631
1632 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp}),
1633 neteq_->LastDecodedTimestamps());
1634
1635 // Nothing decoded on the second call.
1636 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1637 EXPECT_TRUE(neteq_->LastDecodedTimestamps().empty());
1638}
1639
1640TEST_F(NetEqDecodingTest, LastDecodedTimestampsTwoDecoded) {
1641 // Insert two packets with PCM16b WB data (this is what PopulateRtpInfo does
1642 // by default). Make the length 5 ms so that NetEq must decode them both in
1643 // the same GetAudio call.
1644 constexpr size_t kPayloadSamples = 16 * 5;
1645 constexpr size_t kPayloadBytes = 2 * kPayloadSamples;
1646 uint8_t payload[kPayloadBytes] = {0};
1647
1648 RTPHeader rtp_info;
1649 constexpr uint32_t kRtpTimestamp1 = 0x1234;
1650 PopulateRtpInfo(0, kRtpTimestamp1, &rtp_info);
1651 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1652 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + kPayloadSamples;
1653 PopulateRtpInfo(1, kRtpTimestamp2, &rtp_info);
1654 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1655
1656 // Pull out data once.
1657 AudioFrame output;
1658 bool muted;
1659 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1660
1661 EXPECT_EQ(std::vector<uint32_t>({kRtpTimestamp1, kRtpTimestamp2}),
1662 neteq_->LastDecodedTimestamps());
1663}
1664
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001665TEST_F(NetEqDecodingTest, TestConcealmentEvents) {
1666 const int kNumConcealmentEvents = 19;
1667 const size_t kSamples = 10 * 16;
1668 const size_t kPayloadBytes = kSamples * 2;
1669 int seq_no = 0;
1670 RTPHeader rtp_info;
1671 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1672 rtp_info.payloadType = 94; // PCM16b WB codec.
1673 rtp_info.markerBit = 0;
1674 const uint8_t payload[kPayloadBytes] = {0};
1675 bool muted;
1676
1677 for (int i = 0; i < kNumConcealmentEvents; i++) {
1678 // Insert some packets of 10 ms size.
1679 for (int j = 0; j < 10; j++) {
1680 rtp_info.sequenceNumber = seq_no++;
1681 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1682 neteq_->InsertPacket(rtp_info, payload, 0);
1683 neteq_->GetAudio(&out_frame_, &muted);
1684 }
1685
1686 // Lose a number of packets.
1687 int num_lost = 1 + i;
1688 for (int j = 0; j < num_lost; j++) {
1689 seq_no++;
1690 neteq_->GetAudio(&out_frame_, &muted);
1691 }
1692 }
1693
1694 // Check number of concealment events.
1695 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1696 EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events));
1697}
1698
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001699// Test that the jitter buffer delay stat is computed correctly.
1700void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) {
1701 const int kNumPackets = 10;
1702 const int kDelayInNumPackets = 2;
1703 const int kPacketLenMs = 10; // All packets are of 10 ms size.
1704 const size_t kSamples = kPacketLenMs * 16;
1705 const size_t kPayloadBytes = kSamples * 2;
1706 RTPHeader rtp_info;
1707 rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC.
1708 rtp_info.payloadType = 94; // PCM16b WB codec.
1709 rtp_info.markerBit = 0;
1710 const uint8_t payload[kPayloadBytes] = {0};
1711 bool muted;
1712 int packets_sent = 0;
1713 int packets_received = 0;
1714 int expected_delay = 0;
1715 while (packets_received < kNumPackets) {
1716 // Insert packet.
1717 if (packets_sent < kNumPackets) {
1718 rtp_info.sequenceNumber = packets_sent++;
1719 rtp_info.timestamp = rtp_info.sequenceNumber * kSamples;
1720 neteq_->InsertPacket(rtp_info, payload, 0);
1721 }
1722
1723 // Get packet.
1724 if (packets_sent > kDelayInNumPackets) {
1725 neteq_->GetAudio(&out_frame_, &muted);
1726 packets_received++;
1727
1728 // The delay reported by the jitter buffer never exceeds
1729 // the number of samples previously fetched with GetAudio
1730 // (hence the min()).
1731 int packets_delay = std::min(packets_received, kDelayInNumPackets + 1);
1732
1733 // The increase of the expected delay is the product of
1734 // the current delay of the jitter buffer in ms * the
1735 // number of samples that are sent for play out.
1736 int current_delay_ms = packets_delay * kPacketLenMs;
1737 expected_delay += current_delay_ms * kSamples;
1738 }
1739 }
1740
1741 if (apply_packet_loss) {
1742 // Extra call to GetAudio to cause concealment.
1743 neteq_->GetAudio(&out_frame_, &muted);
1744 }
1745
1746 // Check jitter buffer delay.
1747 NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics();
1748 EXPECT_EQ(expected_delay, static_cast<int>(stats.jitter_buffer_delay_ms));
1749}
1750
1751TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) {
1752 TestJitterBufferDelay(false);
1753}
1754
1755TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) {
1756 TestJitterBufferDelay(true);
1757}
1758
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001759} // namespace webrtc