blob: c52f2d6aa5cc996b166866ec32d14d94847cb55e [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Henrik Kjellander74640892015-10-29 11:31:02 +010011#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000023#include "gflags/gflags.h"
kwiberg087bd342017-02-10 08:15:44 -080024#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
kwiberg77eab702016-09-28 17:42:01 -070025#include "webrtc/base/ignore_wundef.h"
minyue4f906772016-04-29 11:05:14 -070026#include "webrtc/base/sha1digest.h"
27#include "webrtc/base/stringencode.h"
kwibergac9f8762016-09-30 22:29:43 -070028#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080031#include "webrtc/modules/include/module_common_types.h"
kwibergac9f8762016-09-30 22:29:43 -070032#include "webrtc/test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033#include "webrtc/test/testsupport/fileutils.h"
34#include "webrtc/typedefs.h"
35
minyue5f026d02015-12-16 07:36:04 -080036#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070037RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080038#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
39#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
40#else
kjellandere3e902e2017-02-28 08:01:46 -080041#include "webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
minyue5f026d02015-12-16 07:36:04 -080042#endif
kwiberg77eab702016-09-28 17:42:01 -070043RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080044#endif
45
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000046DEFINE_bool(gen_ref, false, "Generate reference files.");
47
kwiberg5adaf732016-10-04 09:33:27 -070048namespace webrtc {
49
minyue5f026d02015-12-16 07:36:04 -080050namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
minyue4f906772016-04-29 11:05:14 -070052const std::string& PlatformChecksum(const std::string& checksum_general,
53 const std::string& checksum_android,
54 const std::string& checksum_win_32,
55 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070056#if defined(WEBRTC_ANDROID)
minyue4f906772016-04-29 11:05:14 -070057 return checksum_android;
kwiberg77eab702016-09-28 17:42:01 -070058#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070059 #ifdef WEBRTC_ARCH_64_BITS
60 return checksum_win_64;
61 #else
62 return checksum_win_32;
63 #endif // WEBRTC_ARCH_64_BITS
64#else
65 return checksum_general;
66#endif // WEBRTC_WIN
67}
68
minyue5f026d02015-12-16 07:36:04 -080069#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
70void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
71 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
72 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
73 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
74 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
75 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
76 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
77 stats->set_expand_rate(stats_raw.expand_rate);
78 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
79 stats->set_preemptive_rate(stats_raw.preemptive_rate);
80 stats->set_accelerate_rate(stats_raw.accelerate_rate);
81 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
82 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
83 stats->set_added_zero_samples(stats_raw.added_zero_samples);
84 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
85 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
86 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
87 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
88}
89
90void Convert(const webrtc::RtcpStatistics& stats_raw,
91 webrtc::neteq_unittest::RtcpStatistics* stats) {
92 stats->set_fraction_lost(stats_raw.fraction_lost);
93 stats->set_cumulative_lost(stats_raw.cumulative_lost);
94 stats->set_extended_max_sequence_number(
95 stats_raw.extended_max_sequence_number);
96 stats->set_jitter(stats_raw.jitter);
97}
98
minyue4f906772016-04-29 11:05:14 -070099void AddMessage(FILE* file, rtc::MessageDigest* digest,
100 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800101 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700102 if (file)
103 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
104 digest->Update(&size, sizeof(size));
105
106 if (file)
107 ASSERT_EQ(static_cast<size_t>(size),
108 fwrite(message.data(), sizeof(char), size, file));
109 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800110}
111
minyue5f026d02015-12-16 07:36:04 -0800112#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
113
henrik.lundin7a926812016-05-12 13:51:28 -0700114void LoadDecoders(webrtc::NetEq* neteq) {
kwiberg5adaf732016-10-04 09:33:27 -0700115 ASSERT_EQ(true,
116 neteq->RegisterPayloadType(0, SdpAudioFormat("pcmu", 8000, 1)));
117 // Use non-SdpAudioFormat argument when registering PCMa, so that we get test
118 // coverage for that as well.
henrik.lundin7a926812016-05-12 13:51:28 -0700119 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
120 "pcma", 8));
121#ifdef WEBRTC_CODEC_ILBC
kwiberg5adaf732016-10-04 09:33:27 -0700122 ASSERT_EQ(true,
123 neteq->RegisterPayloadType(102, SdpAudioFormat("ilbc", 8000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700124#endif
125#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
kwiberg5adaf732016-10-04 09:33:27 -0700126 ASSERT_EQ(true,
127 neteq->RegisterPayloadType(103, SdpAudioFormat("isac", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700128#endif
129#ifdef WEBRTC_CODEC_ISAC
kwiberg5adaf732016-10-04 09:33:27 -0700130 ASSERT_EQ(true,
131 neteq->RegisterPayloadType(104, SdpAudioFormat("isac", 32000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700132#endif
133#ifdef WEBRTC_CODEC_OPUS
kwiberg5adaf732016-10-04 09:33:27 -0700134 ASSERT_EQ(true,
135 neteq->RegisterPayloadType(
136 111, SdpAudioFormat("opus", 48000, 2, {{"stereo", "0"}})));
henrik.lundin7a926812016-05-12 13:51:28 -0700137#endif
kwiberg5adaf732016-10-04 09:33:27 -0700138 ASSERT_EQ(true,
139 neteq->RegisterPayloadType(93, SdpAudioFormat("L16", 8000, 1)));
140 ASSERT_EQ(true,
141 neteq->RegisterPayloadType(94, SdpAudioFormat("L16", 16000, 1)));
142 ASSERT_EQ(true,
143 neteq->RegisterPayloadType(95, SdpAudioFormat("L16", 32000, 1)));
144 ASSERT_EQ(true,
145 neteq->RegisterPayloadType(13, SdpAudioFormat("cn", 8000, 1)));
146 ASSERT_EQ(true,
147 neteq->RegisterPayloadType(98, SdpAudioFormat("cn", 16000, 1)));
henrik.lundin7a926812016-05-12 13:51:28 -0700148}
minyue5f026d02015-12-16 07:36:04 -0800149} // namespace
150
minyue4f906772016-04-29 11:05:14 -0700151class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000152 public:
minyue4f906772016-04-29 11:05:14 -0700153 explicit ResultSink(const std::string& output_file);
154 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155
minyue4f906772016-04-29 11:05:14 -0700156 template<typename T, size_t n> void AddResult(
157 const T (&test_results)[n],
158 size_t length);
159
160 void AddResult(const NetEqNetworkStatistics& stats);
161 void AddResult(const RtcpStatistics& stats);
162
163 void VerifyChecksum(const std::string& ref_check_sum);
164
165 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700167 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168};
169
minyue4f906772016-04-29 11:05:14 -0700170ResultSink::ResultSink(const std::string &output_file)
171 : output_fp_(nullptr),
172 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 if (!output_file.empty()) {
174 output_fp_ = fopen(output_file.c_str(), "wb");
175 EXPECT_TRUE(output_fp_ != NULL);
176 }
177}
178
minyue4f906772016-04-29 11:05:14 -0700179ResultSink::~ResultSink() {
180 if (output_fp_)
181 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182}
183
184template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700185void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 if (output_fp_) {
187 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
188 }
minyue4f906772016-04-29 11:05:14 -0700189 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190}
191
minyue4f906772016-04-29 11:05:14 -0700192void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800193#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800194 neteq_unittest::NetEqNetworkStatistics stats;
195 Convert(stats_raw, &stats);
196
mbonadeid00aad52017-03-31 03:08:07 -0700197 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800198 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700199 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800200#else
201 FAIL() << "Writing to reference file requires Proto Buffer.";
202#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203}
204
minyue4f906772016-04-29 11:05:14 -0700205void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800206#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800207 neteq_unittest::RtcpStatistics stats;
208 Convert(stats_raw, &stats);
209
mbonadeid00aad52017-03-31 03:08:07 -0700210 std::string stats_string;
minyue5f026d02015-12-16 07:36:04 -0800211 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700212 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800213#else
214 FAIL() << "Writing to reference file requires Proto Buffer.";
215#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216}
217
minyue4f906772016-04-29 11:05:14 -0700218void ResultSink::VerifyChecksum(const std::string& checksum) {
219 std::vector<char> buffer;
220 buffer.resize(digest_->Size());
221 digest_->Finish(&buffer[0], buffer.size());
222 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
223 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224}
225
226class NetEqDecodingTest : public ::testing::Test {
227 protected:
228 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
229 // constants below can be changed.
230 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700231 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
232 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
233 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800234 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000235 static const int kInitSampleRateHz = 8000;
236
237 NetEqDecodingTest();
238 virtual void SetUp();
239 virtual void TearDown();
240 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800242 void Process();
minyue5f026d02015-12-16 07:36:04 -0800243
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000244 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700245 const std::string& output_checksum,
246 const std::string& network_stats_checksum,
247 const std::string& rtcp_stats_checksum,
248 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800249
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250 static void PopulateRtpInfo(int frame_index,
251 int timestamp,
252 WebRtcRTPHeader* rtp_info);
253 static void PopulateCng(int frame_index,
254 int timestamp,
255 WebRtcRTPHeader* rtp_info,
256 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000257 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000259 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
260 const std::set<uint16_t>& drop_seq_numbers,
261 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
262
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000263 void LongCngWithClockDrift(double drift_factor,
264 double network_freeze_ms,
265 bool pull_audio_during_freeze,
266 int delay_tolerance_ms,
267 int max_time_to_speech_ms);
268
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000269 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000270
henrik.lundin0d96ab72016-04-06 12:28:26 -0700271 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000272
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000274 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800275 std::unique_ptr<test::RtpFileSource> rtp_source_;
276 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800278 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000280 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281};
282
283// Allocating the static const so that it can be passed by reference.
284const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700285const size_t NetEqDecodingTest::kBlockSize8kHz;
286const size_t NetEqDecodingTest::kBlockSize16kHz;
287const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288const int NetEqDecodingTest::kInitSampleRateHz;
289
290NetEqDecodingTest::NetEqDecodingTest()
291 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000292 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000294 output_sample_rate_(kInitSampleRateHz),
295 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000296 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297}
298
299void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700300 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000301 NetEqNetworkStatistics stat;
302 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
303 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700305 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306}
307
308void NetEqDecodingTest::TearDown() {
309 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310}
311
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000313 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314}
315
henrik.lundin6d8e0112016-03-04 10:34:21 -0800316void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000318 while (packet_ && sim_clock_ >= packet_->time_ms()) {
319 if (packet_->payload_length_bytes() > 0) {
320 WebRtcRTPHeader rtp_header;
321 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800322#ifndef WEBRTC_CODEC_ISAC
323 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
324 if (rtp_header.header.payloadType != 104)
325#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800327 rtp_header,
328 rtc::ArrayView<const uint8_t>(
329 packet_->payload(), packet_->payload_length_bytes()),
330 static_cast<uint32_t>(packet_->time_ms() *
331 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 }
333 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700334 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 }
336
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000337 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700338 bool muted;
339 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
340 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800341 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
342 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
343 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
344 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
345 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800346 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347
348 // Increase time.
349 sim_clock_ += kTimeStepMs;
350}
351
minyue4f906772016-04-29 11:05:14 -0700352void NetEqDecodingTest::DecodeAndCompare(
353 const std::string& rtp_file,
354 const std::string& output_checksum,
355 const std::string& network_stats_checksum,
356 const std::string& rtcp_stats_checksum,
357 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358 OpenInputFile(rtp_file);
359
minyue4f906772016-04-29 11:05:14 -0700360 std::string ref_out_file =
361 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
362 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363
minyue4f906772016-04-29 11:05:14 -0700364 std::string stat_out_file =
365 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
366 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000367
minyue4f906772016-04-29 11:05:14 -0700368 std::string rtcp_out_file =
369 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
370 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000371
henrik.lundin46ba49c2016-05-24 22:50:47 -0700372 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000374 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 std::ostringstream ss;
376 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
377 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800378 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700379 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800380 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381
382 // Query the network statistics API once per second
383 if (sim_clock_ % 1000 == 0) {
384 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700385 NetEqNetworkStatistics current_network_stats;
386 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
387 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
388
henrik.lundin9c3efd02015-08-27 13:12:22 -0700389 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700390 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
391 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392
393 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700394 RtcpStatistics current_rtcp_stats;
395 neteq_->GetRtcpStatistics(&current_rtcp_stats);
396 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 }
398 }
minyue4f906772016-04-29 11:05:14 -0700399
400 SCOPED_TRACE("Check output audio.");
401 output.VerifyChecksum(output_checksum);
402 SCOPED_TRACE("Check network stats.");
403 network_stats.VerifyChecksum(network_stats_checksum);
404 SCOPED_TRACE("Check rtcp stats.");
405 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000406}
407
408void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
409 int timestamp,
410 WebRtcRTPHeader* rtp_info) {
411 rtp_info->header.sequenceNumber = frame_index;
412 rtp_info->header.timestamp = timestamp;
413 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
414 rtp_info->header.payloadType = 94; // PCM16b WB codec.
415 rtp_info->header.markerBit = 0;
416}
417
418void NetEqDecodingTest::PopulateCng(int frame_index,
419 int timestamp,
420 WebRtcRTPHeader* rtp_info,
421 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000422 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000423 rtp_info->header.sequenceNumber = frame_index;
424 rtp_info->header.timestamp = timestamp;
425 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
426 rtp_info->header.payloadType = 98; // WB CNG.
427 rtp_info->header.markerBit = 0;
428 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
429 *payload_len = 1; // Only noise level, no spectral parameters.
430}
431
ivoc72c08ed2016-01-20 07:26:24 -0800432#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
433 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
434 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700435 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800436#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700437#else
minyue5f026d02015-12-16 07:36:04 -0800438#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700439#endif
minyue5f026d02015-12-16 07:36:04 -0800440TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800441 const std::string input_rtp_file =
442 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000443
minyue4f906772016-04-29 11:05:14 -0700444 const std::string output_checksum = PlatformChecksum(
ivocffecbbf2016-12-16 05:51:49 -0800445 "5a8184bc60c0d7dddb50af8966360675476a8d8b",
446 "be982d2c5685dd1ca4ea5d352283df50e8e5b46d",
447 "5a8184bc60c0d7dddb50af8966360675476a8d8b",
448 "c86aec95439748f4949de95b50c94be291118615");
minyue4f906772016-04-29 11:05:14 -0700449
450 const std::string network_stats_checksum = PlatformChecksum(
henrik.lundin0d838572016-10-13 03:35:55 -0700451 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f",
452 "c8b2a93842e48d014f7e6efe10ae96cb3892b129",
453 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f",
454 "f59b3dfdb9b1b8bbb61abedd7c8cf3fc47c21f5f");
minyue4f906772016-04-29 11:05:14 -0700455
456 const std::string rtcp_stats_checksum = PlatformChecksum(
457 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
458 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
459 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
460 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
461
462 DecodeAndCompare(input_rtp_file,
463 output_checksum,
464 network_stats_checksum,
465 rtcp_stats_checksum,
466 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000467}
468
minyue93c08b72015-12-22 09:57:41 -0800469#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
470 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
minyuea613eb62017-03-14 14:33:30 -0700471 defined(WEBRTC_CODEC_OPUS) && \
472 !WEBRTC_OPUS_SUPPORT_120MS_PTIME
minyue93c08b72015-12-22 09:57:41 -0800473#define MAYBE_TestOpusBitExactness TestOpusBitExactness
474#else
475#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
476#endif
flim64a7eab2016-08-12 04:36:05 -0700477TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800478 const std::string input_rtp_file =
479 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800480
minyue4f906772016-04-29 11:05:14 -0700481 const std::string output_checksum = PlatformChecksum(
flim64a7eab2016-08-12 04:36:05 -0700482 "9d7d52bc94e941d106aa518f324f16a58d231586",
483 "9d7d52bc94e941d106aa518f324f16a58d231586",
484 "9d7d52bc94e941d106aa518f324f16a58d231586",
485 "9d7d52bc94e941d106aa518f324f16a58d231586");
minyue4f906772016-04-29 11:05:14 -0700486
487 const std::string network_stats_checksum = PlatformChecksum(
henrik.lundin0d838572016-10-13 03:35:55 -0700488 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
489 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
490 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8",
491 "d8379381d5a619f0616bb3c0a8a9eea1704a8ab8");
minyue4f906772016-04-29 11:05:14 -0700492
493 const std::string rtcp_stats_checksum = PlatformChecksum(
494 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
495 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
496 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
497 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
498
499 DecodeAndCompare(input_rtp_file,
500 output_checksum,
501 network_stats_checksum,
502 rtcp_stats_checksum,
503 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800504}
505
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000506// Use fax mode to avoid time-scaling. This is to simplify the testing of
507// packet waiting times in the packet buffer.
508class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
509 protected:
510 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
511 config_.playout_mode = kPlayoutFax;
512 }
513};
514
515TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000516 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
517 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000518 const size_t kSamples = 10 * 16;
519 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000520 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800521 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000522 WebRtcRTPHeader rtp_info;
523 rtp_info.header.sequenceNumber = i;
524 rtp_info.header.timestamp = i * kSamples;
525 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
526 rtp_info.header.payloadType = 94; // PCM16b WB codec.
527 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800528 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000529 }
530 // Pull out all data.
531 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700532 bool muted;
533 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800534 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000535 }
536
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200537 NetEqNetworkStatistics stats;
538 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000539 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
540 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200541 // each packet. Thus, we are calculating the statistics for a series from 10
542 // to 300, in steps of 10 ms.
543 EXPECT_EQ(155, stats.mean_waiting_time_ms);
544 EXPECT_EQ(155, stats.median_waiting_time_ms);
545 EXPECT_EQ(10, stats.min_waiting_time_ms);
546 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000547
548 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200549 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
550 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
551 EXPECT_EQ(-1, stats.median_waiting_time_ms);
552 EXPECT_EQ(-1, stats.min_waiting_time_ms);
553 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554}
555
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000556TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557 const int kNumFrames = 3000; // Needed for convergence.
558 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000559 const size_t kSamples = 10 * 16;
560 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561 while (frame_index < kNumFrames) {
562 // Insert one packet each time, except every 10th time where we insert two
563 // packets at once. This will create a negative clock-drift of approx. 10%.
564 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
565 for (int n = 0; n < num_packets; ++n) {
566 uint8_t payload[kPayloadBytes] = {0};
567 WebRtcRTPHeader rtp_info;
568 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800569 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000570 ++frame_index;
571 }
572
573 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700574 bool muted;
575 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800576 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000577 }
578
579 NetEqNetworkStatistics network_stats;
580 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700581 EXPECT_EQ(-103192, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000582}
583
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000584TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585 const int kNumFrames = 5000; // Needed for convergence.
586 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000587 const size_t kSamples = 10 * 16;
588 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000589 for (int i = 0; i < kNumFrames; ++i) {
590 // Insert one packet each time, except every 10th time where we don't insert
591 // any packet. This will create a positive clock-drift of approx. 11%.
592 int num_packets = (i % 10 == 9 ? 0 : 1);
593 for (int n = 0; n < num_packets; ++n) {
594 uint8_t payload[kPayloadBytes] = {0};
595 WebRtcRTPHeader rtp_info;
596 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800597 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000598 ++frame_index;
599 }
600
601 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700602 bool muted;
603 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800604 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 }
606
607 NetEqNetworkStatistics network_stats;
608 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
henrik.lundin0d838572016-10-13 03:35:55 -0700609 EXPECT_EQ(110953, network_stats.clockdrift_ppm);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000610}
611
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000612void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
613 double network_freeze_ms,
614 bool pull_audio_during_freeze,
615 int delay_tolerance_ms,
616 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000617 uint16_t seq_no = 0;
618 uint32_t timestamp = 0;
619 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000620 const size_t kSamples = kFrameSizeMs * 16;
621 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622 double next_input_time_ms = 0.0;
623 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700624 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000625
626 // Insert speech for 5 seconds.
627 const int kSpeechDurationMs = 5000;
628 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
629 // Each turn in this for loop is 10 ms.
630 while (next_input_time_ms <= t_ms) {
631 // Insert one 30 ms speech frame.
632 uint8_t payload[kPayloadBytes] = {0};
633 WebRtcRTPHeader rtp_info;
634 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800635 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 ++seq_no;
637 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000638 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639 }
640 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700641 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800642 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000643 }
644
henrik.lundin55480f52016-03-08 02:37:57 -0800645 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700646 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
647 ASSERT_TRUE(playout_timestamp);
648 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000649
650 // Insert CNG for 1 minute (= 60000 ms).
651 const int kCngPeriodMs = 100;
652 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
653 const int kCngDurationMs = 60000;
654 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
655 // Each turn in this for loop is 10 ms.
656 while (next_input_time_ms <= t_ms) {
657 // Insert one CNG frame each 100 ms.
658 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000659 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000660 WebRtcRTPHeader rtp_info;
661 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800662 ASSERT_EQ(0, neteq_->InsertPacket(
663 rtp_info,
664 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000665 ++seq_no;
666 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000667 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 }
669 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700670 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800671 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000672 }
673
henrik.lundin55480f52016-03-08 02:37:57 -0800674 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000675
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000676 if (network_freeze_ms > 0) {
677 // First keep pulling audio for |network_freeze_ms| without inserting
678 // any data, then insert CNG data corresponding to |network_freeze_ms|
679 // without pulling any output audio.
680 const double loop_end_time = t_ms + network_freeze_ms;
681 for (; t_ms < loop_end_time; t_ms += 10) {
682 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700683 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800684 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800685 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000686 }
687 bool pull_once = pull_audio_during_freeze;
688 // If |pull_once| is true, GetAudio will be called once half-way through
689 // the network recovery period.
690 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
691 while (next_input_time_ms <= t_ms) {
692 if (pull_once && next_input_time_ms >= pull_time_ms) {
693 pull_once = false;
694 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700695 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800696 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800697 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000698 t_ms += 10;
699 }
700 // Insert one CNG frame each 100 ms.
701 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000702 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000703 WebRtcRTPHeader rtp_info;
704 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800705 ASSERT_EQ(0, neteq_->InsertPacket(
706 rtp_info,
707 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000708 ++seq_no;
709 timestamp += kCngPeriodSamples;
710 next_input_time_ms += kCngPeriodMs * drift_factor;
711 }
712 }
713
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000714 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800716 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000717 // Each turn in this for loop is 10 ms.
718 while (next_input_time_ms <= t_ms) {
719 // Insert one 30 ms speech frame.
720 uint8_t payload[kPayloadBytes] = {0};
721 WebRtcRTPHeader rtp_info;
722 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800723 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724 ++seq_no;
725 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000726 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000727 }
728 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700729 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800730 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 // Increase clock.
732 t_ms += 10;
733 }
734
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000735 // Check that the speech starts again within reasonable time.
736 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
737 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700738 playout_timestamp = PlayoutTimestamp();
739 ASSERT_TRUE(playout_timestamp);
740 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000742 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
743 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744}
745
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000746TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000747 // Apply a clock drift of -25 ms / s (sender faster than receiver).
748 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000749 const double kNetworkFreezeTimeMs = 0.0;
750 const bool kGetAudioDuringFreezeRecovery = false;
751 const int kDelayToleranceMs = 20;
752 const int kMaxTimeToSpeechMs = 100;
753 LongCngWithClockDrift(kDriftFactor,
754 kNetworkFreezeTimeMs,
755 kGetAudioDuringFreezeRecovery,
756 kDelayToleranceMs,
757 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000758}
759
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000760TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000761 // Apply a clock drift of +25 ms / s (sender slower than receiver).
762 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000763 const double kNetworkFreezeTimeMs = 0.0;
764 const bool kGetAudioDuringFreezeRecovery = false;
765 const int kDelayToleranceMs = 20;
766 const int kMaxTimeToSpeechMs = 100;
767 LongCngWithClockDrift(kDriftFactor,
768 kNetworkFreezeTimeMs,
769 kGetAudioDuringFreezeRecovery,
770 kDelayToleranceMs,
771 kMaxTimeToSpeechMs);
772}
773
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000774TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000775 // Apply a clock drift of -25 ms / s (sender faster than receiver).
776 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
777 const double kNetworkFreezeTimeMs = 5000.0;
778 const bool kGetAudioDuringFreezeRecovery = false;
779 const int kDelayToleranceMs = 50;
780 const int kMaxTimeToSpeechMs = 200;
781 LongCngWithClockDrift(kDriftFactor,
782 kNetworkFreezeTimeMs,
783 kGetAudioDuringFreezeRecovery,
784 kDelayToleranceMs,
785 kMaxTimeToSpeechMs);
786}
787
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000788TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000789 // Apply a clock drift of +25 ms / s (sender slower than receiver).
790 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
791 const double kNetworkFreezeTimeMs = 5000.0;
792 const bool kGetAudioDuringFreezeRecovery = false;
793 const int kDelayToleranceMs = 20;
794 const int kMaxTimeToSpeechMs = 100;
795 LongCngWithClockDrift(kDriftFactor,
796 kNetworkFreezeTimeMs,
797 kGetAudioDuringFreezeRecovery,
798 kDelayToleranceMs,
799 kMaxTimeToSpeechMs);
800}
801
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000802TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000803 // Apply a clock drift of +25 ms / s (sender slower than receiver).
804 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
805 const double kNetworkFreezeTimeMs = 5000.0;
806 const bool kGetAudioDuringFreezeRecovery = true;
807 const int kDelayToleranceMs = 20;
808 const int kMaxTimeToSpeechMs = 100;
809 LongCngWithClockDrift(kDriftFactor,
810 kNetworkFreezeTimeMs,
811 kGetAudioDuringFreezeRecovery,
812 kDelayToleranceMs,
813 kMaxTimeToSpeechMs);
814}
815
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000816TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000817 const double kDriftFactor = 1.0; // No drift.
818 const double kNetworkFreezeTimeMs = 0.0;
819 const bool kGetAudioDuringFreezeRecovery = false;
820 const int kDelayToleranceMs = 10;
821 const int kMaxTimeToSpeechMs = 50;
822 LongCngWithClockDrift(kDriftFactor,
823 kNetworkFreezeTimeMs,
824 kGetAudioDuringFreezeRecovery,
825 kDelayToleranceMs,
826 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000827}
828
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000829TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000830 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000831 uint8_t payload[kPayloadBytes] = {0};
832 WebRtcRTPHeader rtp_info;
833 PopulateRtpInfo(0, 0, &rtp_info);
834 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800835 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000836 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
837}
838
Peter Boströme2976c82016-01-04 22:44:05 +0100839#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800840#define MAYBE_DecoderError DecoderError
841#else
842#define MAYBE_DecoderError DISABLED_DecoderError
843#endif
844
Peter Boströme2976c82016-01-04 22:44:05 +0100845TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000846 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000847 uint8_t payload[kPayloadBytes] = {0};
848 WebRtcRTPHeader rtp_info;
849 PopulateRtpInfo(0, 0, &rtp_info);
850 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800851 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000852 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
853 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800854 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
855 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000856 }
henrik.lundin7a926812016-05-12 13:51:28 -0700857 bool muted;
858 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
859 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000860 // Verify that there is a decoder error to check.
861 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800862
863 enum NetEqDecoderError {
864 ISAC_LENGTH_MISMATCH = 6730,
865 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
866 };
867#if defined(WEBRTC_CODEC_ISAC)
868 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
869#elif defined(WEBRTC_CODEC_ISACFX)
870 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
871#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000872 // Verify that the first 160 samples are set to 0, and that the remaining
873 // samples are left unmodified.
874 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
875 for (int i = 0; i < kExpectedOutputLength; ++i) {
876 std::ostringstream ss;
877 ss << "i = " << i;
878 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800879 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000880 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800881 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
882 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000883 std::ostringstream ss;
884 ss << "i = " << i;
885 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800886 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000887 }
888}
889
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000890TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000891 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
892 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800893 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
894 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000895 }
henrik.lundin7a926812016-05-12 13:51:28 -0700896 bool muted;
897 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
898 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000899 // Verify that the first block of samples is set to 0.
900 static const int kExpectedOutputLength =
901 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
902 for (int i = 0; i < kExpectedOutputLength; ++i) {
903 std::ostringstream ss;
904 ss << "i = " << i;
905 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800906 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000907 }
henrik.lundind89814b2015-11-23 06:49:25 -0800908 // Verify that the sample rate did not change from the initial configuration.
909 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000910}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000911
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000912class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000913 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000914 virtual void TestCondition(double sum_squared_noise,
915 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000916
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000917 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700918 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000919 uint8_t payload_type = 0xFF; // Invalid.
920 if (sampling_rate_hz == 8000) {
921 expected_samples_per_channel = kBlockSize8kHz;
922 payload_type = 93; // PCM 16, 8 kHz.
923 } else if (sampling_rate_hz == 16000) {
924 expected_samples_per_channel = kBlockSize16kHz;
925 payload_type = 94; // PCM 16, 16 kHZ.
926 } else if (sampling_rate_hz == 32000) {
927 expected_samples_per_channel = kBlockSize32kHz;
928 payload_type = 95; // PCM 16, 32 kHz.
929 } else {
930 ASSERT_TRUE(false); // Unsupported test case.
931 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000932
henrik.lundin6d8e0112016-03-04 10:34:21 -0800933 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000934 test::AudioLoop input;
935 // We are using the same 32 kHz input file for all tests, regardless of
936 // |sampling_rate_hz|. The output may sound weird, but the test is still
937 // valid.
938 ASSERT_TRUE(input.Init(
939 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
940 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700941 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000942
943 // Payload of 10 ms of PCM16 32 kHz.
944 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000945 WebRtcRTPHeader rtp_info;
946 PopulateRtpInfo(0, 0, &rtp_info);
947 rtp_info.header.payloadType = payload_type;
948
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000949 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700950 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000951 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800952 auto block = input.GetNextBlock();
953 ASSERT_EQ(expected_samples_per_channel, block.size());
954 size_t enc_len_bytes =
955 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000956 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
957
kwibergee2bac22015-11-11 10:34:00 -0800958 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
959 payload, enc_len_bytes),
960 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800961 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700962 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800963 ASSERT_EQ(1u, output.num_channels_);
964 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800965 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000966
967 // Next packet.
968 rtp_info.header.timestamp += expected_samples_per_channel;
969 rtp_info.header.sequenceNumber++;
970 receive_timestamp += expected_samples_per_channel;
971 }
972
henrik.lundin6d8e0112016-03-04 10:34:21 -0800973 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000974
975 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
976 // one frame without checking speech-type. This is the first frame pulled
977 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700978 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800979 ASSERT_EQ(1u, output.num_channels_);
980 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000981
982 // To be able to test the fading of background noise we need at lease to
983 // pull 611 frames.
984 const int kFadingThreshold = 611;
985
986 // Test several CNG-to-PLC packet for the expected behavior. The number 20
987 // is arbitrary, but sufficiently large to test enough number of frames.
988 const int kNumPlcToCngTestFrames = 20;
989 bool plc_to_cng = false;
990 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800991 output.Reset();
992 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin7a926812016-05-12 13:51:28 -0700993 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
994 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800995 ASSERT_EQ(1u, output.num_channels_);
996 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800997 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000998 plc_to_cng = true;
999 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001000 for (size_t k = 0;
1001 k < output.num_channels_ * output.samples_per_channel_; ++k)
1002 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001003 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001004 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001005 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001006 }
1007 }
1008 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1009 }
1010};
1011
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001012class NetEqBgnTestOn : public NetEqBgnTest {
1013 protected:
1014 NetEqBgnTestOn() : NetEqBgnTest() {
1015 config_.background_noise_mode = NetEq::kBgnOn;
1016 }
1017
1018 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1019 EXPECT_NE(0, sum_squared_noise);
1020 }
1021};
1022
1023class NetEqBgnTestOff : public NetEqBgnTest {
1024 protected:
1025 NetEqBgnTestOff() : NetEqBgnTest() {
1026 config_.background_noise_mode = NetEq::kBgnOff;
1027 }
1028
1029 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1030 EXPECT_EQ(0, sum_squared_noise);
1031 }
1032};
1033
1034class NetEqBgnTestFade : public NetEqBgnTest {
1035 protected:
1036 NetEqBgnTestFade() : NetEqBgnTest() {
1037 config_.background_noise_mode = NetEq::kBgnFade;
1038 }
1039
1040 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1041 if (should_be_faded)
1042 EXPECT_EQ(0, sum_squared_noise);
1043 }
1044};
1045
henrika1d34fe92015-06-16 10:04:20 +02001046TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001047 CheckBgn(8000);
1048 CheckBgn(16000);
1049 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001050}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001051
henrika1d34fe92015-06-16 10:04:20 +02001052TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001053 CheckBgn(8000);
1054 CheckBgn(16000);
1055 CheckBgn(32000);
1056}
1057
henrika1d34fe92015-06-16 10:04:20 +02001058TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001059 CheckBgn(8000);
1060 CheckBgn(16000);
1061 CheckBgn(32000);
1062}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001063
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001064void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1065 uint32_t start_timestamp,
1066 const std::set<uint16_t>& drop_seq_numbers,
1067 bool expect_seq_no_wrap,
1068 bool expect_timestamp_wrap) {
1069 uint16_t seq_no = start_seq_no;
1070 uint32_t timestamp = start_timestamp;
1071 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1072 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1073 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001074 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001075 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001076 uint32_t receive_timestamp = 0;
1077
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001078 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001079 const int kSpeechDurationMs = 2000;
1080 int packets_inserted = 0;
1081 uint16_t last_seq_no;
1082 uint32_t last_timestamp;
1083 bool timestamp_wrapped = false;
1084 bool seq_no_wrapped = false;
1085 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1086 // Each turn in this for loop is 10 ms.
1087 while (next_input_time_ms <= t_ms) {
1088 // Insert one 30 ms speech frame.
1089 uint8_t payload[kPayloadBytes] = {0};
1090 WebRtcRTPHeader rtp_info;
1091 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1092 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1093 // This sequence number was not in the set to drop. Insert it.
1094 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001095 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001096 ++packets_inserted;
1097 }
1098 NetEqNetworkStatistics network_stats;
1099 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1100
1101 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1102 // packet size for first few packets. Therefore we refrain from checking
1103 // the criteria.
1104 if (packets_inserted > 4) {
1105 // Expect preferred and actual buffer size to be no more than 2 frames.
1106 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001107 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1108 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001109 }
1110 last_seq_no = seq_no;
1111 last_timestamp = timestamp;
1112
1113 ++seq_no;
1114 timestamp += kSamples;
1115 receive_timestamp += kSamples;
1116 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1117
1118 seq_no_wrapped |= seq_no < last_seq_no;
1119 timestamp_wrapped |= timestamp < last_timestamp;
1120 }
1121 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001122 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001123 bool muted;
1124 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001125 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1126 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001127
1128 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001129 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1130 ASSERT_TRUE(playout_timestamp);
1131 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001132 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001133 }
1134 // Make sure we have actually tested wrap-around.
1135 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1136 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1137}
1138
1139TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1140 // Start with a sequence number that will soon wrap.
1141 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1142 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1143}
1144
1145TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1146 // Start with a sequence number that will soon wrap.
1147 std::set<uint16_t> drop_seq_numbers;
1148 drop_seq_numbers.insert(0xFFFF);
1149 drop_seq_numbers.insert(0x0);
1150 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1151}
1152
1153TEST_F(NetEqDecodingTest, TimestampWrap) {
1154 // Start with a timestamp that will soon wrap.
1155 std::set<uint16_t> drop_seq_numbers;
1156 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1157}
1158
1159TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1160 // Start with a timestamp and a sequence number that will wrap at the same
1161 // time.
1162 std::set<uint16_t> drop_seq_numbers;
1163 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1164}
1165
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001166void NetEqDecodingTest::DuplicateCng() {
1167 uint16_t seq_no = 0;
1168 uint32_t timestamp = 0;
1169 const int kFrameSizeMs = 10;
1170 const int kSampleRateKhz = 16;
1171 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001172 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001173
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001174 const int algorithmic_delay_samples = std::max(
1175 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001176 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001177 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001178 uint8_t payload[kPayloadBytes] = {0};
1179 WebRtcRTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001180 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001181 for (int i = 0; i < 3; ++i) {
1182 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001183 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001184 ++seq_no;
1185 timestamp += kSamples;
1186
1187 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001188 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001189 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001190 }
1191 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001192 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001193
1194 // Insert same CNG packet twice.
1195 const int kCngPeriodMs = 100;
1196 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001197 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001198 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1199 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001200 ASSERT_EQ(
1201 0, neteq_->InsertPacket(
1202 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001203
1204 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001205 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001206 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001207 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001208 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1209 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1210 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001211
1212 // Insert the same CNG packet again. Note that at this point it is old, since
1213 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001214 ASSERT_EQ(
1215 0, neteq_->InsertPacket(
1216 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001217
1218 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1219 // we have already pulled out CNG once.
1220 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001221 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001222 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001223 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001224 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001225 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001226 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001227 }
1228
1229 // Insert speech again.
1230 ++seq_no;
1231 timestamp += kCngPeriodSamples;
1232 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001233 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001234
1235 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001236 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001237 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001239 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1240 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001241 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001242 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001243}
1244
henrik.lundin0d96ab72016-04-06 12:28:26 -07001245rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1246 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001247}
1248
1249TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001250
1251TEST_F(NetEqDecodingTest, CngFirst) {
1252 uint16_t seq_no = 0;
1253 uint32_t timestamp = 0;
1254 const int kFrameSizeMs = 10;
1255 const int kSampleRateKhz = 16;
1256 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1257 const int kPayloadBytes = kSamples * 2;
1258 const int kCngPeriodMs = 100;
1259 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1260 size_t payload_len;
1261
1262 uint8_t payload[kPayloadBytes] = {0};
1263 WebRtcRTPHeader rtp_info;
1264
1265 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001266 ASSERT_EQ(
1267 NetEq::kOK,
1268 neteq_->InsertPacket(
1269 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001270 ++seq_no;
1271 timestamp += kCngPeriodSamples;
1272
1273 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001274 bool muted;
1275 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001276 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001277 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001278
1279 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001280 const uint32_t first_speech_timestamp = timestamp;
1281 int timeout_counter = 0;
1282 do {
1283 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001284 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001285 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001286 ++seq_no;
1287 timestamp += kSamples;
1288
1289 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001290 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001291 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001292 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001293 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001294 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001295}
henrik.lundin7a926812016-05-12 13:51:28 -07001296
1297class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1298 public:
1299 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1300 config_.enable_muted_state = true;
1301 }
1302
1303 protected:
1304 static constexpr size_t kSamples = 10 * 16;
1305 static constexpr size_t kPayloadBytes = kSamples * 2;
1306
1307 void InsertPacket(uint32_t rtp_timestamp) {
1308 uint8_t payload[kPayloadBytes] = {0};
1309 WebRtcRTPHeader rtp_info;
1310 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
1311 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1312 }
1313
henrik.lundin42feb512016-09-20 06:51:40 -07001314 void InsertCngPacket(uint32_t rtp_timestamp) {
1315 uint8_t payload[kPayloadBytes] = {0};
1316 WebRtcRTPHeader rtp_info;
1317 size_t payload_len;
1318 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
1319 EXPECT_EQ(
1320 NetEq::kOK,
1321 neteq_->InsertPacket(
1322 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
1323 }
1324
henrik.lundin7a926812016-05-12 13:51:28 -07001325 bool GetAudioReturnMuted() {
1326 bool muted;
1327 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1328 return muted;
1329 }
1330
1331 void GetAudioUntilMuted() {
1332 while (!GetAudioReturnMuted()) {
1333 ASSERT_LT(counter_++, 1000) << "Test timed out";
1334 }
1335 }
1336
1337 void GetAudioUntilNormal() {
1338 bool muted = false;
1339 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1340 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1341 ASSERT_LT(counter_++, 1000) << "Test timed out";
1342 }
1343 EXPECT_FALSE(muted);
1344 }
1345
1346 int counter_ = 0;
1347};
1348
1349// Verifies that NetEq goes in and out of muted state as expected.
1350TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1351 // Insert one speech packet.
1352 InsertPacket(0);
1353 // Pull out audio once and expect it not to be muted.
1354 EXPECT_FALSE(GetAudioReturnMuted());
1355 // Pull data until faded out.
1356 GetAudioUntilMuted();
1357
1358 // Verify that output audio is not written during muted mode. Other parameters
1359 // should be correct, though.
1360 AudioFrame new_frame;
1361 for (auto& d : new_frame.data_) {
1362 d = 17;
1363 }
1364 bool muted;
1365 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1366 EXPECT_TRUE(muted);
1367 for (auto d : new_frame.data_) {
1368 EXPECT_EQ(17, d);
1369 }
1370 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1371 new_frame.timestamp_);
1372 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1373 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1374 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1375 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1376 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1377
1378 // Insert new data. Timestamp is corrected for the time elapsed since the last
1379 // packet. Verify that normal operation resumes.
1380 InsertPacket(kSamples * counter_);
1381 GetAudioUntilNormal();
henrik.lundin612c25e2016-05-25 08:21:04 -07001382
1383 NetEqNetworkStatistics stats;
1384 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1385 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1386 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1387 // concealment samples in this test.
1388 EXPECT_GT(stats.expand_rate, 14000);
1389 // And, it should be greater than the speech_expand_rate.
1390 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001391}
1392
1393// Verifies that NetEq goes out of muted state when given a delayed packet.
1394TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1395 // Insert one speech packet.
1396 InsertPacket(0);
1397 // Pull out audio once and expect it not to be muted.
1398 EXPECT_FALSE(GetAudioReturnMuted());
1399 // Pull data until faded out.
1400 GetAudioUntilMuted();
1401 // Insert new data. Timestamp is only corrected for the half of the time
1402 // elapsed since the last packet. That is, the new packet is delayed. Verify
1403 // that normal operation resumes.
1404 InsertPacket(kSamples * counter_ / 2);
1405 GetAudioUntilNormal();
1406}
1407
1408// Verifies that NetEq goes out of muted state when given a future packet.
1409TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1410 // Insert one speech packet.
1411 InsertPacket(0);
1412 // Pull out audio once and expect it not to be muted.
1413 EXPECT_FALSE(GetAudioReturnMuted());
1414 // Pull data until faded out.
1415 GetAudioUntilMuted();
1416 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1417 // last packet. That is, the new packet is too early. Verify that normal
1418 // operation resumes.
1419 InsertPacket(kSamples * counter_ * 2);
1420 GetAudioUntilNormal();
1421}
1422
1423// Verifies that NetEq goes out of muted state when given an old packet.
1424TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1425 // Insert one speech packet.
1426 InsertPacket(0);
1427 // Pull out audio once and expect it not to be muted.
1428 EXPECT_FALSE(GetAudioReturnMuted());
1429 // Pull data until faded out.
1430 GetAudioUntilMuted();
1431
1432 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1433 // Insert packet which is older than the first packet.
1434 InsertPacket(kSamples * (counter_ - 1000));
1435 EXPECT_FALSE(GetAudioReturnMuted());
1436 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1437}
1438
henrik.lundin42feb512016-09-20 06:51:40 -07001439// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1440// packet stream is suspended for a long time.
1441TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1442 // Insert one CNG packet.
1443 InsertCngPacket(0);
1444
1445 // Pull 10 seconds of audio (10 ms audio generated per lap).
1446 for (int i = 0; i < 1000; ++i) {
1447 bool muted;
1448 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1449 ASSERT_FALSE(muted);
1450 }
1451 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1452}
1453
1454// Verifies that NetEq goes back to normal after a long CNG period with the
1455// packet stream suspended.
1456TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1457 // Insert one CNG packet.
1458 InsertCngPacket(0);
1459
1460 // Pull 10 seconds of audio (10 ms audio generated per lap).
1461 for (int i = 0; i < 1000; ++i) {
1462 bool muted;
1463 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1464 }
1465
1466 // Insert new data. Timestamp is corrected for the time elapsed since the last
1467 // packet. Verify that normal operation resumes.
1468 InsertPacket(kSamples * counter_);
1469 GetAudioUntilNormal();
1470}
1471
henrik.lundin7a926812016-05-12 13:51:28 -07001472class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1473 public:
1474 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1475
1476 void SetUp() override {
1477 NetEqDecodingTest::SetUp();
1478 config2_ = config_;
1479 }
1480
1481 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001482 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001483 ASSERT_TRUE(neteq2_);
1484 LoadDecoders(neteq2_.get());
1485 }
1486
1487 protected:
1488 std::unique_ptr<NetEq> neteq2_;
1489 NetEq::Config config2_;
1490};
1491
1492namespace {
1493::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1494 const AudioFrame& b) {
1495 if (a.timestamp_ != b.timestamp_)
1496 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1497 << " != " << b.timestamp_ << ")";
1498 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1499 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1500 << a.sample_rate_hz_
1501 << " != " << b.sample_rate_hz_ << ")";
1502 if (a.samples_per_channel_ != b.samples_per_channel_)
1503 return ::testing::AssertionFailure()
1504 << "samples_per_channel_ diff (" << a.samples_per_channel_
1505 << " != " << b.samples_per_channel_ << ")";
1506 if (a.num_channels_ != b.num_channels_)
1507 return ::testing::AssertionFailure() << "num_channels_ diff ("
1508 << a.num_channels_
1509 << " != " << b.num_channels_ << ")";
1510 if (a.speech_type_ != b.speech_type_)
1511 return ::testing::AssertionFailure() << "speech_type_ diff ("
1512 << a.speech_type_
1513 << " != " << b.speech_type_ << ")";
1514 if (a.vad_activity_ != b.vad_activity_)
1515 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1516 << a.vad_activity_
1517 << " != " << b.vad_activity_ << ")";
1518 return ::testing::AssertionSuccess();
1519}
1520
1521::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1522 const AudioFrame& b) {
1523 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1524 if (!res)
1525 return res;
1526 if (memcmp(
1527 a.data_, b.data_,
1528 a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) {
1529 return ::testing::AssertionFailure() << "data_ diff";
1530 }
1531 return ::testing::AssertionSuccess();
1532}
1533
1534} // namespace
1535
1536TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1537 ASSERT_FALSE(config_.enable_muted_state);
1538 config2_.enable_muted_state = true;
1539 CreateSecondInstance();
1540
1541 // Insert one speech packet into both NetEqs.
1542 const size_t kSamples = 10 * 16;
1543 const size_t kPayloadBytes = kSamples * 2;
1544 uint8_t payload[kPayloadBytes] = {0};
1545 WebRtcRTPHeader rtp_info;
1546 PopulateRtpInfo(0, 0, &rtp_info);
1547 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1548 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1549
1550 AudioFrame out_frame1, out_frame2;
1551 bool muted;
1552 for (int i = 0; i < 1000; ++i) {
1553 std::ostringstream ss;
1554 ss << "i = " << i;
1555 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1556 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1557 EXPECT_FALSE(muted);
1558 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1559 if (muted) {
1560 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1561 } else {
1562 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1563 }
1564 }
1565 EXPECT_TRUE(muted);
1566
1567 // Insert new data. Timestamp is corrected for the time elapsed since the last
1568 // packet.
1569 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
1570 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1571 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1572
1573 int counter = 0;
1574 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1575 ASSERT_LT(counter++, 1000) << "Test timed out";
1576 std::ostringstream ss;
1577 ss << "counter = " << counter;
1578 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1579 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1580 EXPECT_FALSE(muted);
1581 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1582 if (muted) {
1583 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1584 } else {
1585 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1586 }
1587 }
1588 EXPECT_FALSE(muted);
1589}
1590
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001591} // namespace webrtc