blob: b3c25cae2d0f369d721dc8bb387cc2f61f96b20f [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
Alessio Bazzica8f319a32019-07-24 16:47:02 +000014#include <utility>
15#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010018#include "api/neteq/default_neteq_controller_factory.h"
19#include "api/neteq/neteq.h"
20#include "api/neteq/neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/neteq/accelerate.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020022#include "modules/audio_coding/neteq/decision_logic.h"
Ivo Creusen39cf3c72019-11-28 14:07:14 +010023#include "modules/audio_coding/neteq/default_neteq_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010025#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020029#include "modules/audio_coding/neteq/mock/mock_neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
31#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010033#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/audio_coding/neteq/sync_buffer.h"
35#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000037#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010038#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010039#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "test/gmock.h"
41#include "test/gtest.h"
42#include "test/mock_audio_decoder.h"
43#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010046using ::testing::AtLeast;
47using ::testing::DoAll;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000048using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049using ::testing::InSequence;
50using ::testing::Invoke;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000051using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000052using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010053using ::testing::Pointee;
54using ::testing::Return;
55using ::testing::ReturnNull;
56using ::testing::SetArgPointee;
57using ::testing::SetArrayArgument;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000058using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010059using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
61namespace webrtc {
62
63// This function is called when inserting a packet list into the mock packet
64// buffer. The purpose is to delete all inserted packets properly, to avoid
65// memory leaks in the test.
66int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070067 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 return PacketBuffer::kOK;
69}
70
71class NetEqImplTest : public ::testing::Test {
72 protected:
Alessio Bazzica8f319a32019-07-24 16:47:02 +000073 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000074
Niels Möllera0f44302018-11-30 10:45:12 +010075 void CreateInstance(
76 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
77 ASSERT_TRUE(decoder_factory);
Ivo Creusen3ce44a32019-10-31 14:38:11 +010078 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory,
79 DefaultNetEqControllerFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070080
81 // Get a local pointer to NetEq's TickTimer object.
82 tick_timer_ = deps.tick_timer.get();
83
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000084 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070085 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
86 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000087 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
88 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000090 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000092
henrik.lundin1d9061e2016-04-26 12:19:34 -070093 if (use_mock_dtmf_buffer_) {
94 std::unique_ptr<MockDtmfBuffer> mock(
95 new MockDtmfBuffer(config_.sample_rate_hz));
96 mock_dtmf_buffer_ = mock.get();
97 deps.dtmf_buffer = std::move(mock);
98 }
99 dtmf_buffer_ = deps.dtmf_buffer.get();
100
101 if (use_mock_dtmf_tone_generator_) {
102 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
103 mock_dtmf_tone_generator_ = mock.get();
104 deps.dtmf_tone_generator = std::move(mock);
105 }
106 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
107
108 if (use_mock_packet_buffer_) {
109 std::unique_ptr<MockPacketBuffer> mock(
110 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
111 mock_packet_buffer_ = mock.get();
112 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700113 }
114 packet_buffer_ = deps.packet_buffer.get();
115
Ivo Creusen53a31f72019-10-24 15:20:39 +0200116 if (use_mock_neteq_controller_) {
117 std::unique_ptr<MockNetEqController> mock(new MockNetEqController());
118 mock_neteq_controller_ = mock.get();
119 deps.neteq_controller = std::move(mock);
120 } else {
121 deps.stats = std::make_unique<StatisticsCalculator>();
122 NetEqController::Config controller_config;
123 controller_config.tick_timer = tick_timer_;
124 controller_config.base_min_delay_ms = config_.min_delay_ms;
125 controller_config.enable_rtx_handling = config_.enable_rtx_handling;
126 controller_config.allow_time_stretching = true;
127 controller_config.max_packets_in_buffer = config_.max_packets_in_buffer;
Ivo Creusen88636c62020-01-24 11:04:56 +0100128 controller_config.clock = &clock_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200129 deps.neteq_controller =
130 std::make_unique<DecisionLogic>(std::move(controller_config));
131 }
132 neteq_controller_ = deps.neteq_controller.get();
133
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700135 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700137 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700138 }
ossua70695a2016-09-22 02:06:28 -0700139 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700140
141 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
142 new TimestampScaler(*deps.decoder_database.get()));
143
144 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000145 ASSERT_TRUE(neteq_ != NULL);
146 }
147
Niels Möllera0f44302018-11-30 10:45:12 +0100148 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
149
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000150 void UseNoMocks() {
151 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000152 use_mock_decoder_database_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200153 use_mock_neteq_controller_ = false;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000154 use_mock_dtmf_buffer_ = false;
155 use_mock_dtmf_tone_generator_ = false;
156 use_mock_packet_buffer_ = false;
157 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158 }
159
160 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000161 if (use_mock_decoder_database_) {
162 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
163 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200164 if (use_mock_neteq_controller_) {
165 EXPECT_CALL(*mock_neteq_controller_, Die()).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000166 }
167 if (use_mock_dtmf_buffer_) {
168 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
169 }
170 if (use_mock_dtmf_tone_generator_) {
171 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
172 }
173 if (use_mock_packet_buffer_) {
174 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
175 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176 }
177
Niels Möller05543682019-01-10 16:55:06 +0100178 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800179 const size_t kPayloadLength = 4;
180 const uint8_t kPayloadType = 110;
solenberg2779bab2016-11-17 04:45:19 -0800181 const int kSampleRateHz = 16000;
182 config_.sample_rate_hz = kSampleRateHz;
183 UseNoMocks();
184 CreateInstance();
185 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200186 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700187 RTPHeader rtp_header;
188 rtp_header.payloadType = kPayloadType;
189 rtp_header.sequenceNumber = 0x1234;
190 rtp_header.timestamp = 0x12345678;
191 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800192
Niels Möller05543682019-01-10 16:55:06 +0100193 EXPECT_TRUE(neteq_->RegisterPayloadType(
194 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800195
196 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200197 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
solenberg2779bab2016-11-17 04:45:19 -0800198
199 // Pull audio once.
200 const size_t kMaxOutputSize =
201 static_cast<size_t>(10 * kSampleRateHz / 1000);
202 AudioFrame output;
203 bool muted;
204 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
205 ASSERT_FALSE(muted);
206 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
207 EXPECT_EQ(1u, output.num_channels_);
208 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
209
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000210 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
211 // returned by |GetAudio()|.
212 EXPECT_THAT(output.packet_infos_, IsEmpty());
213
solenberg2779bab2016-11-17 04:45:19 -0800214 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200215 const std::vector<int16_t> kOutput(
216 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
218 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
219 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
220 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
221 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
222 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800223 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700224 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800225 }
226
henrik.lundin1d9061e2016-04-26 12:19:34 -0700227 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000228 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000229 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 TickTimer* tick_timer_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700231 MockDecoderDatabase* mock_decoder_database_ = nullptr;
232 DecoderDatabase* decoder_database_ = nullptr;
233 bool use_mock_decoder_database_ = true;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200234 MockNetEqController* mock_neteq_controller_ = nullptr;
235 NetEqController* neteq_controller_ = nullptr;
236 bool use_mock_neteq_controller_ = true;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700237 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
238 DtmfBuffer* dtmf_buffer_ = nullptr;
239 bool use_mock_dtmf_buffer_ = true;
240 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
241 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
242 bool use_mock_dtmf_tone_generator_ = true;
243 MockPacketBuffer* mock_packet_buffer_ = nullptr;
244 PacketBuffer* packet_buffer_ = nullptr;
245 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700246 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
247 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700248 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249};
250
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251// This tests the interface class NetEq.
252// TODO(hlundin): Move to separate file?
253TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000254 NetEq::Config config;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000255 SimulatedClock clock(0);
Ivo Creusen39cf3c72019-11-28 14:07:14 +0100256 auto decoder_factory = CreateBuiltinAudioDecoderFactory();
257 std::unique_ptr<NetEq> neteq =
258 DefaultNetEqFactory().CreateNetEq(config, decoder_factory, &clock);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259}
260
kwiberg5adaf732016-10-04 09:33:27 -0700261TEST_F(NetEqImplTest, RegisterPayloadType) {
262 CreateInstance();
263 constexpr int rtp_payload_type = 0;
264 const SdpAudioFormat format("pcmu", 8000, 1);
265 EXPECT_CALL(*mock_decoder_database_,
266 RegisterPayload(rtp_payload_type, format));
267 neteq_->RegisterPayloadType(rtp_payload_type, format);
268}
269
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000271 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000273 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200275 // Check that kOK is returned when database returns kDecoderNotFound, because
276 // removing a payload type that was never registered is not an error.
277 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278}
279
kwiberg6b19b562016-09-20 04:02:25 -0700280TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
281 CreateInstance();
282 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
283 neteq_->RemoveAllPayloadTypes();
284}
285
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286TEST_F(NetEqImplTest, InsertPacket) {
Ivo Creusena2b31c32020-10-14 17:54:22 +0200287 using ::testing::AllOf;
288 using ::testing::Field;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000289 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000290 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 const uint8_t kPayloadType = 0;
292 const uint16_t kFirstSequenceNumber = 0x1234;
293 const uint32_t kFirstTimestamp = 0x12345678;
294 const uint32_t kSsrc = 0x87654321;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700296 RTPHeader rtp_header;
297 rtp_header.payloadType = kPayloadType;
298 rtp_header.sequenceNumber = kFirstSequenceNumber;
299 rtp_header.timestamp = kFirstTimestamp;
300 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700301 Packet fake_packet;
302 fake_packet.payload_type = kPayloadType;
303 fake_packet.sequence_number = kFirstSequenceNumber;
304 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305
Tommi87f70902021-04-27 14:43:08 +0200306 auto mock_decoder_factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100307 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800308 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200309 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800310 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700311 EXPECT_EQ("pcmu", format.name);
312
313 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
314 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
315 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
kwibergc0f2dcf2016-05-31 06:28:03 -0700316 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
317
318 *dec = std::move(mock_decoder);
319 }));
Niels Möller72899062019-01-11 09:36:13 +0100320 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
321 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322
323 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000324 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 .WillRepeatedly(Return(&info));
326
327 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000328 EXPECT_CALL(*mock_packet_buffer_, Empty())
329 .WillOnce(Return(false)); // Called once after first packet is inserted.
Ivo Creusen7b463c52020-11-25 11:32:40 +0100330 EXPECT_CALL(*mock_packet_buffer_, Flush(_)).Times(1);
331 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100333 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
334 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
336 // index) is a pointer, and the variable pointed to is set to kPayloadType.
337 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
338 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700339 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000340 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700341 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342
343 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200344 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345
346 // Expectations for delay manager.
347 {
348 // All expectations within this block must be called in this specific order.
349 InSequence sequence; // Dummy variable.
350 // Expectations when the first packet is inserted.
Ivo Creusena2b31c32020-10-14 17:54:22 +0200351 EXPECT_CALL(
352 *mock_neteq_controller_,
353 PacketArrived(
354 /*fs_hz*/ 8000,
355 /*should_update_stats*/ _,
356 /*info*/
357 AllOf(
358 Field(&NetEqController::PacketArrivedInfo::is_cng_or_dtmf,
359 false),
360 Field(&NetEqController::PacketArrivedInfo::main_sequence_number,
361 kFirstSequenceNumber),
362 Field(&NetEqController::PacketArrivedInfo::main_timestamp,
363 kFirstTimestamp))));
364 EXPECT_CALL(
365 *mock_neteq_controller_,
366 PacketArrived(
367 /*fs_hz*/ 8000,
368 /*should_update_stats*/ _,
369 /*info*/
370 AllOf(
371 Field(&NetEqController::PacketArrivedInfo::is_cng_or_dtmf,
372 false),
373 Field(&NetEqController::PacketArrivedInfo::main_sequence_number,
374 kFirstSequenceNumber + 1),
375 Field(&NetEqController::PacketArrivedInfo::main_timestamp,
376 kFirstTimestamp + 160))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 }
378
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200380 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381
382 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700383 rtp_header.timestamp += 160;
384 rtp_header.sequenceNumber += 1;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200385 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386}
387
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000388TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
389 UseNoMocks();
390 CreateInstance();
391
392 const int kPayloadLengthSamples = 80;
393 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200394 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000395 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700396 RTPHeader rtp_header;
397 rtp_header.payloadType = kPayloadType;
398 rtp_header.sequenceNumber = 0x1234;
399 rtp_header.timestamp = 0x12345678;
400 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000401
Niels Möller05543682019-01-10 16:55:06 +0100402 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
403 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000404
405 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700406 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
Karl Wiberg45eb1352019-10-10 14:23:00 +0200407 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700408 rtp_header.timestamp += kPayloadLengthSamples;
409 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000410 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
411 }
412
413 // Insert one more packet and make sure the buffer got flushed. That is, it
414 // should only hold one single packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200415 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700416 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700417 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700418 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
419 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000420}
421
solenberg2779bab2016-11-17 04:45:19 -0800422TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100423 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800424}
solenberg99df6c02016-10-11 04:35:34 -0700425
solenberg2779bab2016-11-17 04:45:19 -0800426TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100427 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800428}
solenberg99df6c02016-10-11 04:35:34 -0700429
solenberg2779bab2016-11-17 04:45:19 -0800430TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100431 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800432}
solenberg99df6c02016-10-11 04:35:34 -0700433
solenberg2779bab2016-11-17 04:45:19 -0800434TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100435 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700436}
437
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000438// This test verifies that timestamps propagate from the incoming packets
439// through to the sync buffer and to the playout timestamp.
440TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000441 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000442 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700443 const size_t kPayloadLengthSamples =
444 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000445 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
446 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700447 RTPHeader rtp_header;
448 rtp_header.payloadType = kPayloadType;
449 rtp_header.sequenceNumber = 0x1234;
450 rtp_header.timestamp = 0x12345678;
451 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000452 rtp_header.numCSRCs = 3;
453 rtp_header.arrOfCSRCs[0] = 43;
454 rtp_header.arrOfCSRCs[1] = 65;
455 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000456
457 // This is a dummy decoder that produces as many output samples as the input
458 // has bytes. The output is an increasing series, starting at 1 for the first
459 // sample, and then increasing by 1 for each sample.
460 class CountingSamplesDecoder : public AudioDecoder {
461 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000462 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000463
464 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100465 int DecodeInternal(const uint8_t* encoded,
466 size_t encoded_len,
467 int /* sample_rate_hz */,
468 int16_t* decoded,
469 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000470 for (size_t i = 0; i < encoded_len; ++i) {
471 decoded[i] = next_value_++;
472 }
473 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200474 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475 }
476
Karl Wiberg43766482015-08-27 15:22:11 +0200477 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000478
kwiberg347d3512016-06-16 01:59:09 -0700479 int SampleRateHz() const override { return kSampleRateHz; }
480
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000481 size_t Channels() const override { return 1; }
482
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000483 uint16_t next_value() const { return next_value_; }
484
485 private:
486 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000487 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000488
Tommi87f70902021-04-27 14:43:08 +0200489 auto decoder_factory =
490 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&decoder_);
Niels Möllerb7180c02018-12-06 13:07:11 +0100491
492 UseNoMocks();
493 CreateInstance(decoder_factory);
494
495 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100496 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000497
498 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000499 clock_.AdvanceTimeMilliseconds(123456);
Johannes Kronf7de74c2021-04-30 13:10:56 +0200500 Timestamp expected_receive_time = clock_.CurrentTime();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200501 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000502
503 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700504 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800505 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700506 bool muted;
507 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
508 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800509 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
510 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800511 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000512
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000513 // Verify |output.packet_infos_|.
514 ASSERT_THAT(output.packet_infos_, SizeIs(1));
515 {
516 const auto& packet_info = output.packet_infos_[0];
517 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
518 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
519 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
520 EXPECT_FALSE(packet_info.audio_level().has_value());
Johannes Kronf7de74c2021-04-30 13:10:56 +0200521 EXPECT_EQ(packet_info.receive_time(), expected_receive_time);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000522 }
523
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000524 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700525 EXPECT_EQ(kPayloadLengthSamples,
526 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527
528 // The value of the last of the output samples is the same as the number of
529 // samples played from the decoded packet. Thus, this number + the RTP
530 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200531 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700532 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200533 absl::optional<uint32_t>(rtp_header.timestamp +
534 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700535 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000536
537 // Check the timestamp for the last value in the sync buffer. This should
538 // be one full frame length ahead of the RTP timestamp.
539 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
540 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700541 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000542 sync_buffer->end_timestamp());
543
544 // Check that the number of samples still to play from the sync buffer add
545 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800546 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700547 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800548 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000549}
550
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000551TEST_F(NetEqImplTest, ReorderedPacket) {
552 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100553 // Create a mock decoder object.
554 MockAudioDecoder mock_decoder;
555
556 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +0200557 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000558
559 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000560 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700561 const size_t kPayloadLengthSamples =
562 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000563 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
564 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700565 RTPHeader rtp_header;
566 rtp_header.payloadType = kPayloadType;
567 rtp_header.sequenceNumber = 0x1234;
568 rtp_header.timestamp = 0x12345678;
569 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000570 rtp_header.extension.hasAudioLevel = true;
571 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000572
Karl Wiberg43766482015-08-27 15:22:11 +0200573 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700574 EXPECT_CALL(mock_decoder, SampleRateHz())
575 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000576 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin034154b2016-04-27 06:11:50 -0700577 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200578 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000579 int16_t dummy_output[kPayloadLengthSamples] = {0};
580 // The below expectation will make the mock decoder write
581 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100582 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
583 kSampleRateHz, _, _))
584 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100586 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200587 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100588 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
589 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000590
591 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000592 clock_.AdvanceTimeMilliseconds(123456);
Johannes Kronf7de74c2021-04-30 13:10:56 +0200593 Timestamp expected_receive_time = clock_.CurrentTime();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200594 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000595
596 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700597 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800598 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700599 bool muted;
600 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800601 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
602 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800603 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000604
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000605 // Verify |output.packet_infos_|.
606 ASSERT_THAT(output.packet_infos_, SizeIs(1));
607 {
608 const auto& packet_info = output.packet_infos_[0];
609 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
610 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
611 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
612 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
Johannes Kronf7de74c2021-04-30 13:10:56 +0200613 EXPECT_EQ(packet_info.receive_time(), expected_receive_time);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000614 }
615
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616 // Insert two more packets. The first one is out of order, and is already too
617 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700618 rtp_header.sequenceNumber -= 1;
619 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000620 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000621 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000622 clock_.AdvanceTimeMilliseconds(1000);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200623 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700624 rtp_header.sequenceNumber += 2;
625 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000626 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000627 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000628 clock_.AdvanceTimeMilliseconds(2000);
Johannes Kronf7de74c2021-04-30 13:10:56 +0200629 expected_receive_time = clock_.CurrentTime();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200630 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000631
632 // Expect only the second packet to be decoded (the one with "2" as the first
633 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100634 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
635 kSampleRateHz, _, _))
636 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000637 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100638 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200639 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000640
641 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700642 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800643 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
644 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800645 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000646
647 // Now check the packet buffer, and make sure it is empty, since the
648 // out-of-order packet should have been discarded.
649 EXPECT_TRUE(packet_buffer_->Empty());
650
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000651 // Verify |output.packet_infos_|. Expect to only see the second packet.
652 ASSERT_THAT(output.packet_infos_, SizeIs(1));
653 {
654 const auto& packet_info = output.packet_infos_[0];
655 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
656 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
657 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
658 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
Johannes Kronf7de74c2021-04-30 13:10:56 +0200659 EXPECT_EQ(packet_info.receive_time(), expected_receive_time);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000660 }
661
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000662 EXPECT_CALL(mock_decoder, Die());
663}
664
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000665// This test verifies that NetEq can handle the situation where the first
666// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000667TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000668 UseNoMocks();
669 CreateInstance();
670
671 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000672 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 const size_t kPayloadLengthSamples =
674 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700675 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000676 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700677 RTPHeader rtp_header;
678 rtp_header.payloadType = kPayloadType;
679 rtp_header.sequenceNumber = 0x1234;
680 rtp_header.timestamp = 0x12345678;
681 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000682
683 // Insert one packet. Note that we have not registered any payload type, so
684 // this packet will be rejected.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200685 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000686
687 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700688 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800689 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700690 bool muted;
691 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800692 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
693 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
694 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800695 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000696 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000697
698 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100699 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
700 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000701
702 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700704 rtp_header.sequenceNumber++;
705 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200706 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000707 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
708 }
709
710 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700711 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700712 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800713 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
714 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
715 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800716 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000717 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000718 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000719 }
720}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000721
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200722// This test verifies that audio interruption is not logged for the initial
723// PLC period before the first packet is deocoded.
724// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
Henrik Lundinfe047752019-11-19 12:58:11 +0100725// Make the test parametrized, so that we can test with different initial
726// sample rates in NetEq.
727class NetEqImplTestSampleRateParameter
728 : public NetEqImplTest,
729 public testing::WithParamInterface<int> {
730 protected:
731 NetEqImplTestSampleRateParameter()
732 : NetEqImplTest(), initial_sample_rate_hz_(GetParam()) {
733 config_.sample_rate_hz = initial_sample_rate_hz_;
734 }
735
736 const int initial_sample_rate_hz_;
737};
738
739// This test does the following:
740// 0. Set up NetEq with initial sample rate given by test parameter, and a codec
741// sample rate of 16000.
742// 1. Start calling GetAudio before inserting any encoded audio. The audio
743// produced will be PLC.
744// 2. Insert a number of encoded audio packets.
745// 3. Keep calling GetAudio and verify that no audio interruption was logged.
746// Call GetAudio until NetEq runs out of data again; PLC starts.
747// 4. Insert one more packet.
748// 5. Call GetAudio until that packet is decoded and the PLC ends.
749
750TEST_P(NetEqImplTestSampleRateParameter,
751 NoAudioInterruptionLoggedBeforeFirstDecode) {
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200752 UseNoMocks();
753 CreateInstance();
754
755 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundinfe047752019-11-19 12:58:11 +0100756 const int kPayloadSampleRateHz = 16000;
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200757 const size_t kPayloadLengthSamples =
Henrik Lundinfe047752019-11-19 12:58:11 +0100758 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200759 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
760 uint8_t payload[kPayloadLengthBytes] = {0};
761 RTPHeader rtp_header;
762 rtp_header.payloadType = kPayloadType;
763 rtp_header.sequenceNumber = 0x1234;
764 rtp_header.timestamp = 0x12345678;
765 rtp_header.ssrc = 0x87654321;
766
767 // Register the payload type.
Henrik Lundinfe047752019-11-19 12:58:11 +0100768 EXPECT_TRUE(neteq_->RegisterPayloadType(
769 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200770
771 // Pull audio several times. No packets have been inserted yet.
Henrik Lundinfe047752019-11-19 12:58:11 +0100772 const size_t initial_output_size =
773 static_cast<size_t>(10 * initial_sample_rate_hz_ / 1000); // 10 ms
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200774 AudioFrame output;
775 bool muted;
776 for (int i = 0; i < 100; ++i) {
777 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Henrik Lundinfe047752019-11-19 12:58:11 +0100778 EXPECT_EQ(initial_output_size, output.samples_per_channel_);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200779 EXPECT_EQ(1u, output.num_channels_);
780 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000781 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200782 }
783
Henrik Lundinfe047752019-11-19 12:58:11 +0100784 // Lambda for inserting packets.
785 auto insert_packet = [&]() {
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200786 rtp_header.sequenceNumber++;
787 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200788 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundinfe047752019-11-19 12:58:11 +0100789 };
790 // Insert 10 packets.
791 for (size_t i = 0; i < 10; ++i) {
792 insert_packet();
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200793 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
794 }
795
796 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Henrik Lundinfe047752019-11-19 12:58:11 +0100797 constexpr size_t kOutputSize =
798 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200799 for (size_t i = 0; i < 3; ++i) {
800 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Henrik Lundinfe047752019-11-19 12:58:11 +0100801 EXPECT_EQ(kOutputSize, output.samples_per_channel_);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200802 EXPECT_EQ(1u, output.num_channels_);
803 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
804 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000805 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200806 }
807
Henrik Lundinfe047752019-11-19 12:58:11 +0100808 // Verify that no interruption was logged.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200809 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200810 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundinfe047752019-11-19 12:58:11 +0100811
812 // Keep pulling audio data until a new PLC period is started.
813 size_t count_loops = 0;
814 while (output.speech_type_ == AudioFrame::kNormalSpeech) {
815 // Make sure we don't hang the test if we never go to PLC.
816 ASSERT_LT(++count_loops, 100u);
817 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
818 }
819
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200820 // Insert a few packets to avoid postpone decoding after expand.
821 for (size_t i = 0; i < 5; ++i) {
822 insert_packet();
823 }
Henrik Lundinfe047752019-11-19 12:58:11 +0100824
825 // Pull audio until the newly inserted packet is decoded and the PLC ends.
826 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
827 // Make sure we don't hang the test if we never go to PLC.
828 ASSERT_LT(++count_loops, 100u);
829 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
830 }
831
832 // Verify that no interruption was logged.
833 lifetime_stats = neteq_->GetLifetimeStatistics();
834 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200835}
836
Henrik Lundinfe047752019-11-19 12:58:11 +0100837// This test does the following:
838// 0. Set up NetEq with initial sample rate given by test parameter, and a codec
839// sample rate of 16000.
840// 1. Insert a number of encoded audio packets.
841// 2. Call GetAudio and verify that decoded audio is produced.
842// 3. Keep calling GetAudio until NetEq runs out of data; PLC starts.
843// 4. Keep calling GetAudio until PLC has been produced for at least 150 ms.
844// 5. Insert one more packet.
845// 6. Call GetAudio until that packet is decoded and the PLC ends.
846// 7. Verify that an interruption was logged.
847
848TEST_P(NetEqImplTestSampleRateParameter, AudioInterruptionLogged) {
849 UseNoMocks();
850 CreateInstance();
851
852 const uint8_t kPayloadType = 17; // Just an arbitrary number.
853 const int kPayloadSampleRateHz = 16000;
854 const size_t kPayloadLengthSamples =
855 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
856 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
857 uint8_t payload[kPayloadLengthBytes] = {0};
858 RTPHeader rtp_header;
859 rtp_header.payloadType = kPayloadType;
860 rtp_header.sequenceNumber = 0x1234;
861 rtp_header.timestamp = 0x12345678;
862 rtp_header.ssrc = 0x87654321;
863
864 // Register the payload type.
865 EXPECT_TRUE(neteq_->RegisterPayloadType(
866 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
867
868 // Lambda for inserting packets.
869 auto insert_packet = [&]() {
870 rtp_header.sequenceNumber++;
871 rtp_header.timestamp += kPayloadLengthSamples;
872 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
873 };
874 // Insert 10 packets.
875 for (size_t i = 0; i < 10; ++i) {
876 insert_packet();
877 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
878 }
879
880 AudioFrame output;
881 bool muted;
882 // Keep pulling audio data until a new PLC period is started.
883 size_t count_loops = 0;
884 do {
885 // Make sure we don't hang the test if we never go to PLC.
886 ASSERT_LT(++count_loops, 100u);
887 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
888 } while (output.speech_type_ == AudioFrame::kNormalSpeech);
889
890 // Pull audio 15 times, which produces 150 ms of output audio. This should
891 // all be produced as PLC. The total length of the gap will then be 150 ms
892 // plus an initial fraction of 10 ms at the start and the end of the PLC
893 // period. In total, less than 170 ms.
894 for (size_t i = 0; i < 15; ++i) {
895 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
896 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
897 }
898
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200899 // Insert a few packets to avoid postpone decoding after expand.
900 for (size_t i = 0; i < 5; ++i) {
901 insert_packet();
902 }
Henrik Lundinfe047752019-11-19 12:58:11 +0100903
904 // Pull audio until the newly inserted packet is decoded and the PLC ends.
905 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
906 // Make sure we don't hang the test if we never go to PLC.
907 ASSERT_LT(++count_loops, 100u);
908 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
909 }
910
911 // Verify that the interruption was logged.
912 auto lifetime_stats = neteq_->GetLifetimeStatistics();
913 EXPECT_EQ(1, lifetime_stats.interruption_count);
914 EXPECT_GT(lifetime_stats.total_interruption_duration_ms, 150);
915 EXPECT_LT(lifetime_stats.total_interruption_duration_ms, 170);
916}
917
918INSTANTIATE_TEST_SUITE_P(SampleRates,
919 NetEqImplTestSampleRateParameter,
920 testing::Values(8000, 16000, 32000, 48000));
921
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000922// This test verifies that NetEq can handle comfort noise and enters/quits codec
923// internal CNG mode properly.
924TEST_F(NetEqImplTest, CodecInternalCng) {
925 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100926 // Create a mock decoder object.
927 MockAudioDecoder mock_decoder;
928 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +0200929 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000930
931 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000932 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700933 const size_t kPayloadLengthSamples =
934 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
935 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000936 uint8_t payload[kPayloadLengthBytes] = {0};
937 int16_t dummy_output[kPayloadLengthSamples] = {0};
938
henrik.lundin246ef3e2017-04-24 09:14:32 -0700939 RTPHeader rtp_header;
940 rtp_header.payloadType = kPayloadType;
941 rtp_header.sequenceNumber = 0x1234;
942 rtp_header.timestamp = 0x12345678;
943 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000944
Karl Wiberg43766482015-08-27 15:22:11 +0200945 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700946 EXPECT_CALL(mock_decoder, SampleRateHz())
947 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000948 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700949 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200950 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700951 // Packed duration when asking the decoder for more CNG data (without a new
952 // packet).
953 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200954 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000955
956 // Pointee(x) verifies that first byte of the payload equals x, this makes it
957 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100958 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
959 kSampleRateKhz * 1000, _, _))
960 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000961 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100962 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200963 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000964
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100965 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
966 kSampleRateKhz * 1000, _, _))
967 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000968 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100969 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200970 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000971
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100972 EXPECT_CALL(mock_decoder,
973 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
974 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000975 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100976 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200977 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000978
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100979 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
980 kSampleRateKhz * 1000, _, _))
981 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000982 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100983 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200984 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000985
Niels Möller50b66d52018-12-11 14:43:21 +0100986 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
987 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000988
Peter Kastingdce40cf2015-08-24 14:52:23 -0700989 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800990 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800991 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200992 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
993 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
994 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000995 int expected_timestamp_increment[8] = {
996 -1, // will not be used.
997 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200998 -1,
999 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
1000 -1,
1001 -1,
1002 50 * kSampleRateKhz,
1003 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001004
Jakob Ivarssond723da12021-01-15 17:44:56 +01001005 // Insert one packet (decoder will return speech).
1006 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1007
henrik.lundin7a926812016-05-12 13:51:28 -07001008 bool muted;
1009 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +02001010 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -07001011 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001012
Jakob Ivarssond723da12021-01-15 17:44:56 +01001013 // Insert second packet (decoder will return CNG).
1014 payload[0] = 1;
1015 rtp_header.sequenceNumber++;
1016 rtp_header.timestamp += kPayloadLengthSamples;
1017 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1018
henrik.lundin0d96ab72016-04-06 12:28:26 -07001019 // Lambda for verifying the timestamps.
1020 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +02001021 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -07001022 if (expected_timestamp_increment[i] == -1) {
1023 // Expect to get an empty timestamp value during CNG and PLC.
1024 EXPECT_FALSE(ts) << "i = " << i;
1025 } else {
1026 ASSERT_TRUE(ts) << "i = " << i;
1027 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
1028 << "i = " << i;
1029 last_timestamp = ts;
1030 }
1031 };
1032
Peter Kastingdce40cf2015-08-24 14:52:23 -07001033 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001034 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1035 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001036 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -07001037 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -07001038 SCOPED_TRACE("");
1039 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001040 }
1041
1042 // Insert third packet, which leaves a gap from last packet.
1043 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -07001044 rtp_header.sequenceNumber += 2;
1045 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001046 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001047
Peter Kastingdce40cf2015-08-24 14:52:23 -07001048 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001049 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1050 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001051 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -07001052 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -07001053 SCOPED_TRACE("");
1054 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001055 }
1056
1057 // Now check the packet buffer, and make sure it is empty.
1058 EXPECT_TRUE(packet_buffer_->Empty());
1059
1060 EXPECT_CALL(mock_decoder, Die());
1061}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001062
1063TEST_F(NetEqImplTest, UnsupportedDecoder) {
1064 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001065 ::testing::NiceMock<MockAudioDecoder> decoder;
1066
1067 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +02001068 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -07001069 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -08001070 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001071
1072 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001073 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001074
Peter Kastingdce40cf2015-08-24 14:52:23 -07001075 const size_t kPayloadLengthSamples =
1076 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001077 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -07001078 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +02001079 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001080 RTPHeader rtp_header;
1081 rtp_header.payloadType = kPayloadType;
1082 rtp_header.sequenceNumber = 0x1234;
1083 rtp_header.timestamp = 0x12345678;
1084 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001085
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001086 const uint8_t kFirstPayloadValue = 1;
1087 const uint8_t kSecondPayloadValue = 2;
1088
ossu61a208b2016-09-20 01:38:00 -07001089 EXPECT_CALL(decoder,
1090 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
1091 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001092 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001093
ossu61a208b2016-09-20 01:38:00 -07001094 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001095 .Times(0);
1096
ossu61a208b2016-09-20 01:38:00 -07001097 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
1098 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001099 .Times(1)
ossu61a208b2016-09-20 01:38:00 -07001100 .WillOnce(DoAll(
1101 SetArrayArgument<3>(dummy_output,
1102 dummy_output + kPayloadLengthSamples * kChannels),
1103 SetArgPointee<4>(AudioDecoder::kSpeech),
1104 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001105
ossu61a208b2016-09-20 01:38:00 -07001106 EXPECT_CALL(decoder,
1107 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1108 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001109 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -07001110
Jonas Olssona4d87372019-07-05 19:08:33 +02001111 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -07001112
Jonas Olssona4d87372019-07-05 19:08:33 +02001113 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001114
Niels Möllera1eb9c72018-12-07 15:24:42 +01001115 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1116 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001117
1118 // Insert one packet.
1119 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001120 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001121
1122 // Insert another packet.
1123 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001124 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001125 // The second timestamp needs to be at least 30 ms after the first to make
1126 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001127 rtp_header.timestamp += 3 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001128 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001129
henrik.lundin6d8e0112016-03-04 10:34:21 -08001130 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001131 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001132 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001133 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001134 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001135 // Output size and number of channels should be correct.
1136 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1137 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1138 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001139 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001140
henrik.lundin6d8e0112016-03-04 10:34:21 -08001141 // Second call to GetAudio will decode the packet that is ok. No errors are
1142 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001143 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001144 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1145 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001146 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001147
1148 // Die isn't called through NiceMock (since it's called by the
1149 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1150 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001151}
1152
henrik.lundin116c84e2015-08-27 13:14:48 -07001153// This test inserts packets until the buffer is flushed. After that, it asks
1154// NetEq for the network statistics. The purpose of the test is to make sure
1155// that even though the buffer size increment is negative (which it becomes when
1156// the packet causing a flush is inserted), the packet length stored in the
1157// decision logic remains valid.
1158TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1159 UseNoMocks();
1160 CreateInstance();
1161
1162 const size_t kPayloadLengthSamples = 80;
1163 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1164 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin116c84e2015-08-27 13:14:48 -07001165 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001166 RTPHeader rtp_header;
1167 rtp_header.payloadType = kPayloadType;
1168 rtp_header.sequenceNumber = 0x1234;
1169 rtp_header.timestamp = 0x12345678;
1170 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001171
Niels Möller05543682019-01-10 16:55:06 +01001172 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1173 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001174
1175 // Insert packets until the buffer flushes.
1176 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1177 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
Karl Wiberg45eb1352019-10-10 14:23:00 +02001178 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -07001179 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1180 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001181 }
1182 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1183
1184 // Ask for network statistics. This should not crash.
1185 NetEqNetworkStatistics stats;
1186 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1187}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001188
1189TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1190 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001191 // Create a mock decoder object.
1192 MockAudioDecoder mock_decoder;
1193
1194 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +02001195 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001196
1197 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001198 const int kSampleRateHz = 8000;
1199 const size_t kPayloadLengthSamples =
1200 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1201 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1202 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001203 RTPHeader rtp_header;
1204 rtp_header.payloadType = kPayloadType;
1205 rtp_header.sequenceNumber = 0x1234;
1206 rtp_header.timestamp = 0x12345678;
1207 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001208
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001209 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001210 EXPECT_CALL(mock_decoder, SampleRateHz())
1211 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001212 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001213 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001214 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001215 int16_t dummy_output[kPayloadLengthSamples] = {0};
1216 // The below expectation will make the mock decoder write
1217 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1218 // speech. That is, the decoded length is 5 samples shorter than the expected.
1219 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001220 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001221 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001222 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001223 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001224 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001225 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001226 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1227 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001228
1229 // Insert one packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001230 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001231
1232 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1233
1234 // Pull audio once.
1235 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001237 bool muted;
1238 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001239 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1240 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001241 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001242 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001243
1244 EXPECT_CALL(mock_decoder, Die());
1245}
minyuel6d92bf52015-09-23 15:20:39 +02001246
1247// This test checks the behavior of NetEq when audio decoder fails.
1248TEST_F(NetEqImplTest, DecodingError) {
1249 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001250 // Create a mock decoder object.
1251 MockAudioDecoder mock_decoder;
1252
1253 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +02001254 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001255
1256 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001257 const int kSampleRateHz = 8000;
1258 const int kDecoderErrorCode = -97; // Any negative number.
1259
1260 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1261 const size_t kFrameLengthSamples =
1262 static_cast<size_t>(5 * kSampleRateHz / 1000);
1263
1264 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1265
1266 uint8_t payload[kPayloadLengthBytes] = {0};
1267
henrik.lundin246ef3e2017-04-24 09:14:32 -07001268 RTPHeader rtp_header;
1269 rtp_header.payloadType = kPayloadType;
1270 rtp_header.sequenceNumber = 0x1234;
1271 rtp_header.timestamp = 0x12345678;
1272 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001273
minyuel6d92bf52015-09-23 15:20:39 +02001274 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001275 EXPECT_CALL(mock_decoder, SampleRateHz())
1276 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001277 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001278 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001279 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001280 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1281 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001282 int16_t dummy_output[kFrameLengthSamples] = {0};
1283
1284 {
1285 InSequence sequence; // Dummy variable.
1286 // Mock decoder works normally the first time.
1287 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001288 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001289 .Times(3)
1290 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001291 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001292 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001293 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001294 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001295 .RetiresOnSaturation();
1296
1297 // Then mock decoder fails. A common reason for failure can be buffer being
1298 // too short
1299 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001300 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001301 .WillOnce(Return(-1))
1302 .RetiresOnSaturation();
1303
1304 // Mock decoder finally returns to normal.
1305 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001306 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001307 .Times(2)
1308 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001309 DoAll(SetArrayArgument<3>(dummy_output,
1310 dummy_output + kFrameLengthSamples),
1311 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001312 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001313 }
1314
Niels Möllera1eb9c72018-12-07 15:24:42 +01001315 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1316 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001317
1318 // Insert packets.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +02001319 for (int i = 0; i < 20; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001320 rtp_header.sequenceNumber += 1;
1321 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001322 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001323 }
1324
1325 // Pull audio.
1326 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001327 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001328 bool muted;
1329 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001330 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1331 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001332 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001333 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001334
1335 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001336 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001337 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1338 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001339 // We are not expecting anything for output.speech_type_, since an error was
1340 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001341
1342 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001343 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001344 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1345 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001346 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001347 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001348
1349 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001350 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001351 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1352 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001353 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001354 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001355
1356 EXPECT_CALL(mock_decoder, Die());
1357}
1358
1359// This test checks the behavior of NetEq when audio decoder fails during CNG.
1360TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1361 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001362
1363 // Create a mock decoder object.
1364 MockAudioDecoder mock_decoder;
1365 CreateInstance(
Tommi87f70902021-04-27 14:43:08 +02001366 rtc::make_ref_counted<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001367
1368 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001369 const int kSampleRateHz = 8000;
1370 const int kDecoderErrorCode = -97; // Any negative number.
1371
1372 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1373 const size_t kFrameLengthSamples =
1374 static_cast<size_t>(5 * kSampleRateHz / 1000);
1375
1376 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1377
1378 uint8_t payload[kPayloadLengthBytes] = {0};
1379
henrik.lundin246ef3e2017-04-24 09:14:32 -07001380 RTPHeader rtp_header;
1381 rtp_header.payloadType = kPayloadType;
1382 rtp_header.sequenceNumber = 0x1234;
1383 rtp_header.timestamp = 0x12345678;
1384 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001385
minyuel6d92bf52015-09-23 15:20:39 +02001386 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001387 EXPECT_CALL(mock_decoder, SampleRateHz())
1388 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001389 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001390 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001391 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001392 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001393 int16_t dummy_output[kFrameLengthSamples] = {0};
1394
1395 {
1396 InSequence sequence; // Dummy variable.
1397 // Mock decoder works normally the first 2 times.
1398 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001399 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001400 .Times(2)
1401 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001402 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001403 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001404 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001405 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001406 .RetiresOnSaturation();
1407
1408 // Then mock decoder fails. A common reason for failure can be buffer being
1409 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001410 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001411 .WillOnce(Return(-1))
1412 .RetiresOnSaturation();
1413
1414 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001415 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001416 .Times(2)
1417 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001418 DoAll(SetArrayArgument<3>(dummy_output,
1419 dummy_output + kFrameLengthSamples),
1420 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001421 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001422 }
1423
Niels Möller50b66d52018-12-11 14:43:21 +01001424 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1425 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001426
1427 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1428 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001429 rtp_header.sequenceNumber += 1;
1430 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001431 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001432 }
1433
1434 // Pull audio.
1435 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001436 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001437 bool muted;
1438 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001439 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1440 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001441 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001442
1443 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001444 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001445 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1446 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001447 // We are not expecting anything for output.speech_type_, since an error was
1448 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001449
1450 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001451 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001452 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1453 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001454 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001455
1456 EXPECT_CALL(mock_decoder, Die());
1457}
1458
henrik.lundind89814b2015-11-23 06:49:25 -08001459// Tests that the return value from last_output_sample_rate_hz() is equal to the
1460// configured inital sample rate.
1461TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1462 UseNoMocks();
1463 config_.sample_rate_hz = 48000;
1464 CreateInstance();
1465 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1466}
1467
henrik.lundined497212016-04-25 10:11:38 -07001468TEST_F(NetEqImplTest, TickTimerIncrement) {
1469 UseNoMocks();
1470 CreateInstance();
1471 ASSERT_TRUE(tick_timer_);
1472 EXPECT_EQ(0u, tick_timer_->ticks());
1473 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001474 bool muted;
1475 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001476 EXPECT_EQ(1u, tick_timer_->ticks());
1477}
1478
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001479TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1480 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001481 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001482 CreateInstance();
1483
Ivo Creusen53a31f72019-10-24 15:20:39 +02001484 EXPECT_CALL(*mock_neteq_controller_, SetBaseMinimumDelay(_))
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001485 .WillOnce(Return(true))
1486 .WillOnce(Return(false));
1487
1488 const int delay_ms = 200;
1489
1490 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1491 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1492}
1493
1494TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1495 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001496 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001497 CreateInstance();
1498
1499 const int delay_ms = 200;
1500
Ivo Creusen53a31f72019-10-24 15:20:39 +02001501 EXPECT_CALL(*mock_neteq_controller_, GetBaseMinimumDelay())
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001502 .WillOnce(Return(delay_ms));
1503
1504 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1505}
1506
henrik.lundin114c1b32017-04-26 07:47:32 -07001507TEST_F(NetEqImplTest, TargetDelayMs) {
1508 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001509 use_mock_neteq_controller_ = true;
henrik.lundin114c1b32017-04-26 07:47:32 -07001510 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001511 constexpr int kTargetLevelMs = 510;
1512 EXPECT_CALL(*mock_neteq_controller_, TargetLevelMs())
1513 .WillOnce(Return(kTargetLevelMs));
1514 EXPECT_EQ(510, neteq_->TargetDelayMs());
henrik.lundin114c1b32017-04-26 07:47:32 -07001515}
1516
henrik.lundinb8c55b12017-05-10 07:38:01 -07001517TEST_F(NetEqImplTest, InsertEmptyPacket) {
1518 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001519 use_mock_neteq_controller_ = true;
henrik.lundinb8c55b12017-05-10 07:38:01 -07001520 CreateInstance();
1521
1522 RTPHeader rtp_header;
1523 rtp_header.payloadType = 17;
1524 rtp_header.sequenceNumber = 0x1234;
1525 rtp_header.timestamp = 0x12345678;
1526 rtp_header.ssrc = 0x87654321;
1527
Ivo Creusen53a31f72019-10-24 15:20:39 +02001528 EXPECT_CALL(*mock_neteq_controller_, RegisterEmptyPacket());
henrik.lundinb8c55b12017-05-10 07:38:01 -07001529 neteq_->InsertEmptyPacket(rtp_header);
1530}
1531
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001532TEST_F(NetEqImplTest, EnableRtxHandling) {
Ivo Creusena2b31c32020-10-14 17:54:22 +02001533 using ::testing::AllOf;
1534 using ::testing::Field;
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001535 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001536 use_mock_neteq_controller_ = true;
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001537 config_.enable_rtx_handling = true;
1538 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001539 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001540 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001541 .WillOnce(Return(NetEq::Operation::kNormal));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001542
1543 const int kPayloadLengthSamples = 80;
1544 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1545 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001546 uint8_t payload[kPayloadLengthBytes] = {0};
1547 RTPHeader rtp_header;
1548 rtp_header.payloadType = kPayloadType;
1549 rtp_header.sequenceNumber = 0x1234;
1550 rtp_header.timestamp = 0x12345678;
1551 rtp_header.ssrc = 0x87654321;
1552
Niels Möller05543682019-01-10 16:55:06 +01001553 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1554 SdpAudioFormat("l16", 8000, 1)));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001555 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001556 AudioFrame output;
1557 bool muted;
1558 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1559
1560 // Insert second packet that was sent before the first packet.
1561 rtp_header.sequenceNumber -= 1;
1562 rtp_header.timestamp -= kPayloadLengthSamples;
Ivo Creusena2b31c32020-10-14 17:54:22 +02001563 EXPECT_CALL(
1564 *mock_neteq_controller_,
1565 PacketArrived(
1566 /*fs_hz*/ 8000,
1567 /*should_update_stats*/ _,
1568 /*info*/
1569 AllOf(
1570 Field(&NetEqController::PacketArrivedInfo::packet_length_samples,
1571 kPayloadLengthSamples),
1572 Field(&NetEqController::PacketArrivedInfo::main_sequence_number,
1573 rtp_header.sequenceNumber),
1574 Field(&NetEqController::PacketArrivedInfo::main_timestamp,
1575 rtp_header.timestamp))));
Ivo Creusen53a31f72019-10-24 15:20:39 +02001576
Karl Wiberg45eb1352019-10-10 14:23:00 +02001577 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001578}
1579
minyue5bd33972016-05-02 04:46:11 -07001580class Decoder120ms : public AudioDecoder {
1581 public:
kwiberg347d3512016-06-16 01:59:09 -07001582 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1583 : sample_rate_hz_(sample_rate_hz),
1584 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001585 speech_type_(speech_type) {}
1586
1587 int DecodeInternal(const uint8_t* encoded,
1588 size_t encoded_len,
1589 int sample_rate_hz,
1590 int16_t* decoded,
1591 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001592 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001593 size_t decoded_len =
1594 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1595 for (size_t i = 0; i < decoded_len; ++i) {
1596 decoded[i] = next_value_++;
1597 }
1598 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001599 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001600 }
1601
1602 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001603 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001604 size_t Channels() const override { return 2; }
1605
1606 private:
kwiberg347d3512016-06-16 01:59:09 -07001607 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001608 int16_t next_value_;
1609 SpeechType speech_type_;
1610};
1611
1612class NetEqImplTest120ms : public NetEqImplTest {
1613 protected:
1614 NetEqImplTest120ms() : NetEqImplTest() {}
1615 virtual ~NetEqImplTest120ms() {}
1616
1617 void CreateInstanceNoMocks() {
1618 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001619 CreateInstance(decoder_factory_);
1620 EXPECT_TRUE(neteq_->RegisterPayloadType(
1621 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001622 }
1623
1624 void CreateInstanceWithDelayManagerMock() {
1625 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001626 use_mock_neteq_controller_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001627 CreateInstance(decoder_factory_);
1628 EXPECT_TRUE(neteq_->RegisterPayloadType(
1629 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001630 }
1631
1632 uint32_t timestamp_diff_between_packets() const {
1633 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1634 }
1635
1636 uint32_t first_timestamp() const { return 10u; }
1637
1638 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001639 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001640 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001641 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1642 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001643 }
1644 }
1645
1646 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001647 RTPHeader rtp_header;
1648 rtp_header.payloadType = kPayloadType;
1649 rtp_header.sequenceNumber = sequence_number_;
1650 rtp_header.timestamp = timestamp;
1651 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001652 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1653 uint8_t payload[kPayloadLengthBytes] = {0};
Karl Wiberg45eb1352019-10-10 14:23:00 +02001654 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue5bd33972016-05-02 04:46:11 -07001655 sequence_number_++;
1656 }
1657
1658 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001659 const uint32_t sampling_freq = kSamplingFreq_;
Tommi87f70902021-04-27 14:43:08 +02001660 decoder_factory_ = rtc::make_ref_counted<test::FunctionAudioDecoderFactory>(
1661 [sampling_freq, speech_type]() {
1662 std::unique_ptr<AudioDecoder> decoder =
1663 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
1664 RTC_CHECK_EQ(2, decoder->Channels());
1665 return decoder;
1666 });
minyue5bd33972016-05-02 04:46:11 -07001667 }
1668
Niels Möllera0f44302018-11-30 10:45:12 +01001669 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001670 AudioFrame output_;
1671 const uint32_t kPayloadType = 17;
1672 const uint32_t kSamplingFreq_ = 48000;
1673 uint16_t sequence_number_ = 1;
1674};
1675
minyue5bd33972016-05-02 04:46:11 -07001676TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001677 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001678 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001679
1680 InsertPacket(first_timestamp());
1681 GetFirstPacket();
1682
henrik.lundin7a926812016-05-12 13:51:28 -07001683 bool muted;
1684 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001685 EXPECT_EQ(NetEq::Operation::kCodecInternalCng,
1686 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001687}
1688
1689TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001690 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001691 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001692
1693 InsertPacket(first_timestamp());
1694 GetFirstPacket();
1695
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001696 EXPECT_EQ(NetEq::Operation::kNormal, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001697}
1698
1699TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001700 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001701 CreateInstanceWithDelayManagerMock();
1702
Ivo Creusen53a31f72019-10-24 15:20:39 +02001703 EXPECT_CALL(*mock_neteq_controller_, CngOff()).WillRepeatedly(Return(true));
minyue5bd33972016-05-02 04:46:11 -07001704 InsertPacket(first_timestamp());
1705
1706 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001707 bool muted;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001708 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001709 .WillOnce(Return(NetEq::Operation::kExpand));
henrik.lundin7a926812016-05-12 13:51:28 -07001710 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001711
1712 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1713
Ivo Creusen53a31f72019-10-24 15:20:39 +02001714 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001715 .WillOnce(Return(NetEq::Operation::kMerge));
minyue5bd33972016-05-02 04:46:11 -07001716
henrik.lundin7a926812016-05-12 13:51:28 -07001717 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001718 EXPECT_EQ(NetEq::Operation::kMerge, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001719}
1720
1721TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001722 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001723 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001724
1725 InsertPacket(first_timestamp());
1726 GetFirstPacket();
1727
henrik.lundin7a926812016-05-12 13:51:28 -07001728 bool muted;
1729 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001730 EXPECT_EQ(NetEq::Operation::kExpand, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001731}
1732
1733TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001734 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001735 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001736
1737 InsertPacket(first_timestamp());
1738 GetFirstPacket();
1739 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1740
Ivo Creusen53a31f72019-10-24 15:20:39 +02001741 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001742 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001743 .WillOnce(Return(NetEq::Operation::kFastAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001744
henrik.lundin7a926812016-05-12 13:51:28 -07001745 bool muted;
1746 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001747 EXPECT_EQ(NetEq::Operation::kFastAccelerate,
1748 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001749}
1750
1751TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001752 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001753 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001754
1755 InsertPacket(first_timestamp());
1756 GetFirstPacket();
1757
1758 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1759
Ivo Creusen53a31f72019-10-24 15:20:39 +02001760 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001761 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001762 .WillOnce(Return(NetEq::Operation::kPreemptiveExpand));
minyue5bd33972016-05-02 04:46:11 -07001763
henrik.lundin7a926812016-05-12 13:51:28 -07001764 bool muted;
1765 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001766 EXPECT_EQ(NetEq::Operation::kPreemptiveExpand,
1767 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001768}
1769
1770TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001771 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001772 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001773
1774 InsertPacket(first_timestamp());
1775 GetFirstPacket();
1776
1777 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1778
Ivo Creusen53a31f72019-10-24 15:20:39 +02001779 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001780 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001781 .WillOnce(Return(NetEq::Operation::kAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001782
henrik.lundin7a926812016-05-12 13:51:28 -07001783 bool muted;
1784 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001785 EXPECT_EQ(NetEq::Operation::kAccelerate, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001786}
1787
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001788} // namespace webrtc