blob: 14979aeb32d07587ad0602c2c37bb8d48b40c5ce [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Test to verify correct stereo and multi-channel operation.
12
henrik.lundin@webrtc.orgbe50ab62014-03-04 15:10:03 +000013#include <algorithm>
Yves Gerey665174f2018-06-19 15:03:05 +020014#include <list>
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020018#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010020#include "api/neteq/neteq.h"
21#include "api/test/neteq_factory_with_codecs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/tools/input_audio_file.h"
24#include "modules/audio_coding/neteq/tools/rtp_generator.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020025#include "rtc_base/strings/string_builder.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000026#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029
30namespace webrtc {
31
32struct TestParameters {
33 int frame_size;
34 int sample_rate;
Peter Kasting69558702016-01-12 16:26:35 -080035 size_t num_channels;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036};
37
38// This is a parameterized test. The test parameters are supplied through a
39// TestParameters struct, which is obtained through the GetParam() method.
40//
41// The objective of the test is to create a mono input signal and a
42// multi-channel input signal, where each channel is identical to the mono
43// input channel. The two input signals are processed through their respective
44// NetEq instances. After that, the output signals are compared. The expected
45// result is that each channel in the multi-channel output is identical to the
46// mono output.
47class NetEqStereoTest : public ::testing::TestWithParam<TestParameters> {
48 protected:
49 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 static const size_t kMaxBlockSize = 480; // 10 ms @ 48 kHz.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051 static const uint8_t kPayloadTypeMono = 95;
52 static const uint8_t kPayloadTypeMulti = 96;
53
54 NetEqStereoTest()
55 : num_channels_(GetParam().num_channels),
56 sample_rate_hz_(GetParam().sample_rate),
57 samples_per_ms_(sample_rate_hz_ / 1000),
58 frame_size_ms_(GetParam().frame_size),
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 frame_size_samples_(
60 static_cast<size_t>(frame_size_ms_ * samples_per_ms_)),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 output_size_samples_(10 * samples_per_ms_),
Alessio Bazzica8f319a32019-07-24 16:47:02 +000062 clock_(0),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 rtp_generator_mono_(samples_per_ms_),
64 rtp_generator_(samples_per_ms_),
65 payload_size_bytes_(0),
66 multi_payload_size_bytes_(0),
67 last_send_time_(0),
68 last_arrival_time_(0) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +000069 NetEq::Config config;
70 config.sample_rate_hz = sample_rate_hz_;
Ivo Creusen3ce44a32019-10-31 14:38:11 +010071 std::unique_ptr<NetEqFactory> neteq_factory =
72 CreateNetEqFactoryWithCodecs();
73 neteq_mono_ = neteq_factory->CreateNetEq(config, &clock_);
74 neteq_ = neteq_factory->CreateNetEq(config, &clock_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 input_ = new int16_t[frame_size_samples_];
76 encoded_ = new uint8_t[2 * frame_size_samples_];
77 input_multi_channel_ = new int16_t[frame_size_samples_ * num_channels_];
Yves Gerey665174f2018-06-19 15:03:05 +020078 encoded_multi_channel_ =
79 new uint8_t[frame_size_samples_ * 2 * num_channels_];
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 }
81
82 ~NetEqStereoTest() {
Yves Gerey665174f2018-06-19 15:03:05 +020083 delete[] input_;
84 delete[] encoded_;
85 delete[] input_multi_channel_;
86 delete[] encoded_multi_channel_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 }
88
89 virtual void SetUp() {
90 const std::string file_name =
91 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
92 input_file_.reset(new test::InputAudioFile(file_name));
Niels Möller05543682019-01-10 16:55:06 +010093 RTC_CHECK_GE(num_channels_, 2);
94 ASSERT_TRUE(neteq_mono_->RegisterPayloadType(
95 kPayloadTypeMono, SdpAudioFormat("l16", sample_rate_hz_, 1)));
96 ASSERT_TRUE(neteq_->RegisterPayloadType(
97 kPayloadTypeMulti,
98 SdpAudioFormat("l16", sample_rate_hz_, num_channels_)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099 }
100
101 virtual void TearDown() {}
102
103 int GetNewPackets() {
104 if (!input_file_->Read(frame_size_samples_, input_)) {
105 return -1;
106 }
Yves Gerey665174f2018-06-19 15:03:05 +0200107 payload_size_bytes_ =
108 WebRtcPcm16b_Encode(input_, frame_size_samples_, encoded_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109 if (frame_size_samples_ * 2 != payload_size_bytes_) {
110 return -1;
111 }
Yves Gerey665174f2018-06-19 15:03:05 +0200112 int next_send_time = rtp_generator_mono_.GetRtpHeader(
113 kPayloadTypeMono, frame_size_samples_, &rtp_header_mono_);
114 test::InputAudioFile::DuplicateInterleaved(
115 input_, frame_size_samples_, num_channels_, input_multi_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116 multi_payload_size_bytes_ = WebRtcPcm16b_Encode(
117 input_multi_channel_, frame_size_samples_ * num_channels_,
118 encoded_multi_channel_);
119 if (frame_size_samples_ * 2 * num_channels_ != multi_payload_size_bytes_) {
120 return -1;
121 }
122 rtp_generator_.GetRtpHeader(kPayloadTypeMulti, frame_size_samples_,
123 &rtp_header_);
124 return next_send_time;
125 }
126
ivoc72c08ed2016-01-20 07:26:24 -0800127 virtual void VerifyOutput(size_t num_samples) {
yujo36b1a5f2017-06-12 12:45:32 -0700128 const int16_t* output_data = output_.data();
129 const int16_t* output_multi_channel_data = output_multi_channel_.data();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130 for (size_t i = 0; i < num_samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800131 for (size_t j = 0; j < num_channels_; ++j) {
yujo36b1a5f2017-06-12 12:45:32 -0700132 ASSERT_EQ(output_data[i],
133 output_multi_channel_data[i * num_channels_ + j])
henrik.lundin6d8e0112016-03-04 10:34:21 -0800134 << "Diff in sample " << i << ", channel " << j << ".";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 }
136 }
137 }
138
139 virtual int GetArrivalTime(int send_time) {
140 int arrival_time = last_arrival_time_ + (send_time - last_send_time_);
141 last_send_time_ = send_time;
142 last_arrival_time_ = arrival_time;
143 return arrival_time;
144 }
145
146 virtual bool Lost() { return false; }
147
148 void RunTest(int num_loops) {
149 // Get next input packets (mono and multi-channel).
150 int next_send_time;
151 int next_arrival_time;
152 do {
153 next_send_time = GetNewPackets();
154 ASSERT_NE(-1, next_send_time);
155 next_arrival_time = GetArrivalTime(next_send_time);
156 } while (Lost()); // If lost, immediately read the next packet.
157
158 int time_now = 0;
159 for (int k = 0; k < num_loops; ++k) {
160 while (time_now >= next_arrival_time) {
161 // Insert packet in mono instance.
162 ASSERT_EQ(NetEq::kOK,
Karl Wiberg45eb1352019-10-10 14:23:00 +0200163 neteq_mono_->InsertPacket(
164 rtp_header_mono_, rtc::ArrayView<const uint8_t>(
165 encoded_, payload_size_bytes_)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 // Insert packet in multi-channel instance.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200167 ASSERT_EQ(NetEq::kOK, neteq_->InsertPacket(
168 rtp_header_, rtc::ArrayView<const uint8_t>(
169 encoded_multi_channel_,
170 multi_payload_size_bytes_)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 // Get next input packets (mono and multi-channel).
172 do {
173 next_send_time = GetNewPackets();
174 ASSERT_NE(-1, next_send_time);
175 next_arrival_time = GetArrivalTime(next_send_time);
176 } while (Lost()); // If lost, immediately read the next packet.
177 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178 // Get audio from mono instance.
henrik.lundin7a926812016-05-12 13:51:28 -0700179 bool muted;
180 EXPECT_EQ(NetEq::kOK, neteq_mono_->GetAudio(&output_, &muted));
181 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800182 EXPECT_EQ(1u, output_.num_channels_);
183 EXPECT_EQ(output_size_samples_, output_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 // Get audio from multi-channel instance.
henrik.lundin7a926812016-05-12 13:51:28 -0700185 ASSERT_EQ(NetEq::kOK, neteq_->GetAudio(&output_multi_channel_, &muted));
186 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800187 EXPECT_EQ(num_channels_, output_multi_channel_.num_channels_);
188 EXPECT_EQ(output_size_samples_,
189 output_multi_channel_.samples_per_channel_);
Jonas Olsson366a50c2018-09-06 13:41:30 +0200190 rtc::StringBuilder ss;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 ss << "Lap number " << k << ".";
192 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
193 // Compare mono and multi-channel.
194 ASSERT_NO_FATAL_FAILURE(VerifyOutput(output_size_samples_));
195
196 time_now += kTimeStepMs;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000197 clock_.AdvanceTimeMilliseconds(kTimeStepMs);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198 }
199 }
200
Peter Kasting69558702016-01-12 16:26:35 -0800201 const size_t num_channels_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 const int sample_rate_hz_;
203 const int samples_per_ms_;
204 const int frame_size_ms_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700205 const size_t frame_size_samples_;
206 const size_t output_size_samples_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000207 SimulatedClock clock_;
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100208 std::unique_ptr<NetEq> neteq_mono_;
209 std::unique_ptr<NetEq> neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000210 test::RtpGenerator rtp_generator_mono_;
211 test::RtpGenerator rtp_generator_;
212 int16_t* input_;
213 int16_t* input_multi_channel_;
214 uint8_t* encoded_;
215 uint8_t* encoded_multi_channel_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800216 AudioFrame output_;
217 AudioFrame output_multi_channel_;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700218 RTPHeader rtp_header_mono_;
219 RTPHeader rtp_header_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700220 size_t payload_size_bytes_;
221 size_t multi_payload_size_bytes_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000222 int last_send_time_;
223 int last_arrival_time_;
kwiberg2d0c3322016-02-14 09:28:33 -0800224 std::unique_ptr<test::InputAudioFile> input_file_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225};
226
227class NetEqStereoTestNoJitter : public NetEqStereoTest {
228 protected:
Yves Gerey665174f2018-06-19 15:03:05 +0200229 NetEqStereoTestNoJitter() : NetEqStereoTest() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000230 // Start the sender 100 ms before the receiver to pre-fill the buffer.
231 // This is to avoid doing preemptive expand early in the test.
232 // TODO(hlundin): Mock the decision making instead to control the modes.
233 last_arrival_time_ = -100;
234 }
235};
236
ivoc72c08ed2016-01-20 07:26:24 -0800237TEST_P(NetEqStereoTestNoJitter, RunTest) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000238 RunTest(8);
239}
240
241class NetEqStereoTestPositiveDrift : public NetEqStereoTest {
242 protected:
Yves Gerey665174f2018-06-19 15:03:05 +0200243 NetEqStereoTestPositiveDrift() : NetEqStereoTest(), drift_factor(0.9) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000244 // Start the sender 100 ms before the receiver to pre-fill the buffer.
245 // This is to avoid doing preemptive expand early in the test.
246 // TODO(hlundin): Mock the decision making instead to control the modes.
247 last_arrival_time_ = -100;
248 }
249 virtual int GetArrivalTime(int send_time) {
Yves Gerey665174f2018-06-19 15:03:05 +0200250 int arrival_time =
251 last_arrival_time_ + drift_factor * (send_time - last_send_time_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000252 last_send_time_ = send_time;
253 last_arrival_time_ = arrival_time;
254 return arrival_time;
255 }
256
257 double drift_factor;
258};
259
ivoc72c08ed2016-01-20 07:26:24 -0800260TEST_P(NetEqStereoTestPositiveDrift, RunTest) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261 RunTest(100);
262}
263
264class NetEqStereoTestNegativeDrift : public NetEqStereoTestPositiveDrift {
265 protected:
Yves Gerey665174f2018-06-19 15:03:05 +0200266 NetEqStereoTestNegativeDrift() : NetEqStereoTestPositiveDrift() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 drift_factor = 1.1;
268 last_arrival_time_ = 0;
269 }
270};
271
ivoc72c08ed2016-01-20 07:26:24 -0800272TEST_P(NetEqStereoTestNegativeDrift, RunTest) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 RunTest(100);
274}
275
276class NetEqStereoTestDelays : public NetEqStereoTest {
277 protected:
278 static const int kDelayInterval = 10;
279 static const int kDelay = 1000;
Yves Gerey665174f2018-06-19 15:03:05 +0200280 NetEqStereoTestDelays() : NetEqStereoTest(), frame_index_(0) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281
282 virtual int GetArrivalTime(int send_time) {
283 // Deliver immediately, unless we have a back-log.
284 int arrival_time = std::min(last_arrival_time_, send_time);
285 if (++frame_index_ % kDelayInterval == 0) {
286 // Delay this packet.
287 arrival_time += kDelay;
288 }
289 last_send_time_ = send_time;
290 last_arrival_time_ = arrival_time;
291 return arrival_time;
292 }
293
294 int frame_index_;
295};
296
ivoc72c08ed2016-01-20 07:26:24 -0800297TEST_P(NetEqStereoTestDelays, RunTest) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 RunTest(1000);
299}
300
301class NetEqStereoTestLosses : public NetEqStereoTest {
302 protected:
303 static const int kLossInterval = 10;
Yves Gerey665174f2018-06-19 15:03:05 +0200304 NetEqStereoTestLosses() : NetEqStereoTest(), frame_index_(0) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305
Yves Gerey665174f2018-06-19 15:03:05 +0200306 virtual bool Lost() { return (++frame_index_) % kLossInterval == 0; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307
ivoc72c08ed2016-01-20 07:26:24 -0800308 // TODO(hlundin): NetEq is not giving bitexact results for these cases.
309 virtual void VerifyOutput(size_t num_samples) {
310 for (size_t i = 0; i < num_samples; ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700311 const int16_t* output_data = output_.data();
312 const int16_t* output_multi_channel_data = output_multi_channel_.data();
Yves Gerey665174f2018-06-19 15:03:05 +0200313 auto first_channel_sample = output_multi_channel_data[i * num_channels_];
ivoc72c08ed2016-01-20 07:26:24 -0800314 for (size_t j = 0; j < num_channels_; ++j) {
315 const int kErrorMargin = 200;
yujo36b1a5f2017-06-12 12:45:32 -0700316 EXPECT_NEAR(output_data[i],
317 output_multi_channel_data[i * num_channels_ + j],
ivoc72c08ed2016-01-20 07:26:24 -0800318 kErrorMargin)
319 << "Diff in sample " << i << ", channel " << j << ".";
320 EXPECT_EQ(first_channel_sample,
yujo36b1a5f2017-06-12 12:45:32 -0700321 output_multi_channel_data[i * num_channels_ + j]);
ivoc72c08ed2016-01-20 07:26:24 -0800322 }
323 }
324 }
325
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326 int frame_index_;
327};
328
ivoc72c08ed2016-01-20 07:26:24 -0800329TEST_P(NetEqStereoTestLosses, RunTest) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330 RunTest(100);
331}
332
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333// Creates a list of parameter sets.
334std::list<TestParameters> GetTestParameters() {
335 std::list<TestParameters> l;
336 const int sample_rates[] = {8000, 16000, 32000};
337 const int num_rates = sizeof(sample_rates) / sizeof(sample_rates[0]);
338 // Loop through sample rates.
339 for (int rate_index = 0; rate_index < num_rates; ++rate_index) {
340 int sample_rate = sample_rates[rate_index];
341 // Loop through all frame sizes between 10 and 60 ms.
342 for (int frame_size = 10; frame_size <= 60; frame_size += 10) {
343 TestParameters p;
344 p.frame_size = frame_size;
345 p.sample_rate = sample_rate;
346 p.num_channels = 2;
347 l.push_back(p);
348 if (sample_rate == 8000) {
349 // Add a five-channel test for 8000 Hz.
350 p.num_channels = 5;
351 l.push_back(p);
352 }
353 }
354 }
355 return l;
356}
357
358// Pretty-printing the test parameters in case of an error.
359void PrintTo(const TestParameters& p, ::std::ostream* os) {
Yves Gerey665174f2018-06-19 15:03:05 +0200360 *os << "{frame_size = " << p.frame_size
361 << ", num_channels = " << p.num_channels
362 << ", sample_rate = " << p.sample_rate << "}";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363}
364
365// Instantiate the tests. Each test is instantiated using the function above,
366// so that all different parameter combinations are tested.
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100367INSTANTIATE_TEST_SUITE_P(MultiChannel,
368 NetEqStereoTestNoJitter,
369 ::testing::ValuesIn(GetTestParameters()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100371INSTANTIATE_TEST_SUITE_P(MultiChannel,
372 NetEqStereoTestPositiveDrift,
373 ::testing::ValuesIn(GetTestParameters()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100375INSTANTIATE_TEST_SUITE_P(MultiChannel,
376 NetEqStereoTestNegativeDrift,
377 ::testing::ValuesIn(GetTestParameters()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100379INSTANTIATE_TEST_SUITE_P(MultiChannel,
380 NetEqStereoTestDelays,
381 ::testing::ValuesIn(GetTestParameters()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100383INSTANTIATE_TEST_SUITE_P(MultiChannel,
384 NetEqStereoTestLosses,
385 ::testing::ValuesIn(GetTestParameters()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386
387} // namespace webrtc