blob: a970104e303fcc0ab20704dd42ea2b96c6da5253 [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
kwiberg84be5112016-04-27 01:19:58 -070011#include <memory>
12
Niels Möllera0f44302018-11-30 10:45:12 +010013#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_coding/neteq/accelerate.h"
16#include "modules/audio_coding/neteq/expand.h"
17#include "modules/audio_coding/neteq/include/neteq.h"
18#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
19#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
20#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
21#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
22#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
23#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
24#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
25#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
26#include "modules/audio_coding/neteq/neteq_impl.h"
27#include "modules/audio_coding/neteq/preemptive_expand.h"
28#include "modules/audio_coding/neteq/sync_buffer.h"
29#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010030#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010031#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010032#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "test/gmock.h"
34#include "test/gtest.h"
35#include "test/mock_audio_decoder.h"
36#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000038using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039using ::testing::Return;
40using ::testing::ReturnNull;
41using ::testing::_;
42using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000043using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044using ::testing::InSequence;
45using ::testing::Invoke;
46using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000047using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000048using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50namespace webrtc {
51
52// This function is called when inserting a packet list into the mock packet
53// buffer. The purpose is to delete all inserted packets properly, to avoid
54// memory leaks in the test.
55int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070056 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057 return PacketBuffer::kOK;
58}
59
60class NetEqImplTest : public ::testing::Test {
61 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070062 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000063
Niels Möllera0f44302018-11-30 10:45:12 +010064 void CreateInstance(
65 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
66 ASSERT_TRUE(decoder_factory);
67 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070068
69 // Get a local pointer to NetEq's TickTimer object.
70 tick_timer_ = deps.tick_timer.get();
71
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070073 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
74 mock_buffer_level_filter_ = mock.get();
75 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000076 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 buffer_level_filter_ = deps.buffer_level_filter.get();
78
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
81 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
83 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000087
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070089 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010090 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 mock_delay_peak_detector_ = mock.get();
92 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
93 deps.delay_peak_detector = std::move(mock);
94 }
95 delay_peak_detector_ = deps.delay_peak_detector.get();
96
97 if (use_mock_delay_manager_) {
98 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010099 config_.max_packets_in_buffer, config_.min_delay_ms,
100 delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700101 mock_delay_manager_ = mock.get();
102 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
103 deps.delay_manager = std::move(mock);
104 }
105 delay_manager_ = deps.delay_manager.get();
106
107 if (use_mock_dtmf_buffer_) {
108 std::unique_ptr<MockDtmfBuffer> mock(
109 new MockDtmfBuffer(config_.sample_rate_hz));
110 mock_dtmf_buffer_ = mock.get();
111 deps.dtmf_buffer = std::move(mock);
112 }
113 dtmf_buffer_ = deps.dtmf_buffer.get();
114
115 if (use_mock_dtmf_tone_generator_) {
116 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
117 mock_dtmf_tone_generator_ = mock.get();
118 deps.dtmf_tone_generator = std::move(mock);
119 }
120 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
121
122 if (use_mock_packet_buffer_) {
123 std::unique_ptr<MockPacketBuffer> mock(
124 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
125 mock_packet_buffer_ = mock.get();
126 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700127 }
128 packet_buffer_ = deps.packet_buffer.get();
129
130 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700131 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700133 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 }
ossua70695a2016-09-22 02:06:28 -0700135 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136
137 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
138 new TimestampScaler(*deps.decoder_database.get()));
139
140 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000141 ASSERT_TRUE(neteq_ != NULL);
142 }
143
Niels Möllera0f44302018-11-30 10:45:12 +0100144 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
145
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000146 void UseNoMocks() {
147 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
148 use_mock_buffer_level_filter_ = false;
149 use_mock_decoder_database_ = false;
150 use_mock_delay_peak_detector_ = false;
151 use_mock_delay_manager_ = false;
152 use_mock_dtmf_buffer_ = false;
153 use_mock_dtmf_tone_generator_ = false;
154 use_mock_packet_buffer_ = false;
155 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 }
157
158 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000159 if (use_mock_buffer_level_filter_) {
160 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
161 }
162 if (use_mock_decoder_database_) {
163 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
164 }
165 if (use_mock_delay_manager_) {
166 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
167 }
168 if (use_mock_delay_peak_detector_) {
169 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
170 }
171 if (use_mock_dtmf_buffer_) {
172 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
173 }
174 if (use_mock_dtmf_tone_generator_) {
175 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
176 }
177 if (use_mock_packet_buffer_) {
178 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
179 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 }
181
Niels Möller05543682019-01-10 16:55:06 +0100182 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800183 const size_t kPayloadLength = 4;
184 const uint8_t kPayloadType = 110;
185 const uint32_t kReceiveTime = 17;
186 const int kSampleRateHz = 16000;
187 config_.sample_rate_hz = kSampleRateHz;
188 UseNoMocks();
189 CreateInstance();
190 // Event: 2, E bit, Volume: 17, Length: 4336.
191 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700192 RTPHeader rtp_header;
193 rtp_header.payloadType = kPayloadType;
194 rtp_header.sequenceNumber = 0x1234;
195 rtp_header.timestamp = 0x12345678;
196 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800197
Niels Möller05543682019-01-10 16:55:06 +0100198 EXPECT_TRUE(neteq_->RegisterPayloadType(
199 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800200
201 // Insert first packet.
202 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700203 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800204
205 // Pull audio once.
206 const size_t kMaxOutputSize =
207 static_cast<size_t>(10 * kSampleRateHz / 1000);
208 AudioFrame output;
209 bool muted;
210 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
211 ASSERT_FALSE(muted);
212 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
213 EXPECT_EQ(1u, output.num_channels_);
214 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
215
216 // Verify first 64 samples of actual output.
217 const std::vector<int16_t> kOutput({
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
219 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
220 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
221 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
222 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
223 -2534, -1163 });
224 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700225 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800226 }
227
henrik.lundin1d9061e2016-04-26 12:19:34 -0700228 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000229 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 TickTimer* tick_timer_ = nullptr;
231 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
232 BufferLevelFilter* buffer_level_filter_ = nullptr;
233 bool use_mock_buffer_level_filter_ = true;
234 MockDecoderDatabase* mock_decoder_database_ = nullptr;
235 DecoderDatabase* decoder_database_ = nullptr;
236 bool use_mock_decoder_database_ = true;
237 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
238 DelayPeakDetector* delay_peak_detector_ = nullptr;
239 bool use_mock_delay_peak_detector_ = true;
240 MockDelayManager* mock_delay_manager_ = nullptr;
241 DelayManager* delay_manager_ = nullptr;
242 bool use_mock_delay_manager_ = true;
243 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
244 DtmfBuffer* dtmf_buffer_ = nullptr;
245 bool use_mock_dtmf_buffer_ = true;
246 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
247 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
248 bool use_mock_dtmf_tone_generator_ = true;
249 MockPacketBuffer* mock_packet_buffer_ = nullptr;
250 PacketBuffer* packet_buffer_ = nullptr;
251 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700252 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
253 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700254 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255};
256
257
258// This tests the interface class NetEq.
259// TODO(hlundin): Move to separate file?
260TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000261 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700262 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 delete neteq;
264}
265
kwiberg5adaf732016-10-04 09:33:27 -0700266TEST_F(NetEqImplTest, RegisterPayloadType) {
267 CreateInstance();
268 constexpr int rtp_payload_type = 0;
269 const SdpAudioFormat format("pcmu", 8000, 1);
270 EXPECT_CALL(*mock_decoder_database_,
271 RegisterPayload(rtp_payload_type, format));
272 neteq_->RegisterPayloadType(rtp_payload_type, format);
273}
274
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000276 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000278 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200280 // Check that kOK is returned when database returns kDecoderNotFound, because
281 // removing a payload type that was never registered is not an error.
282 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283}
284
kwiberg6b19b562016-09-20 04:02:25 -0700285TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
286 CreateInstance();
287 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
288 neteq_->RemoveAllPayloadTypes();
289}
290
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000292 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000293 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 const uint8_t kPayloadType = 0;
295 const uint16_t kFirstSequenceNumber = 0x1234;
296 const uint32_t kFirstTimestamp = 0x12345678;
297 const uint32_t kSsrc = 0x87654321;
298 const uint32_t kFirstReceiveTime = 17;
299 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700300 RTPHeader rtp_header;
301 rtp_header.payloadType = kPayloadType;
302 rtp_header.sequenceNumber = kFirstSequenceNumber;
303 rtp_header.timestamp = kFirstTimestamp;
304 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700305 Packet fake_packet;
306 fake_packet.payload_type = kPayloadType;
307 fake_packet.sequence_number = kFirstSequenceNumber;
308 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309
kwibergc0f2dcf2016-05-31 06:28:03 -0700310 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
311 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100312 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800313 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200314 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800315 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700316 EXPECT_EQ("pcmu", format.name);
317
318 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
319 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
320 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
321 // BWE update function called with first packet.
322 EXPECT_CALL(*mock_decoder,
323 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
324 kFirstTimestamp, kFirstReceiveTime));
325 // BWE update function called with second packet.
326 EXPECT_CALL(
327 *mock_decoder,
328 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
329 kFirstTimestamp + 160, kFirstReceiveTime + 155));
330 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
331
332 *dec = std::move(mock_decoder);
333 }));
Niels Möller72899062019-01-11 09:36:13 +0100334 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
335 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336
337 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000338 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 .WillRepeatedly(Return(&info));
340
341 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000342 EXPECT_CALL(*mock_packet_buffer_, Empty())
343 .WillOnce(Return(false)); // Called once after first packet is inserted.
344 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200346 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100348 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
349 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
351 // index) is a pointer, and the variable pointed to is set to kPayloadType.
352 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
353 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700354 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000355 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700356 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357
358 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000359 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 .Times(1);
361
362 // Expectations for delay manager.
363 {
364 // All expectations within this block must be called in this specific order.
365 InSequence sequence; // Dummy variable.
366 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000367 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368 .Times(2)
369 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000370 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000372 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000374 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
375 .WillOnce(Return(0));
376 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 .WillOnce(Return(0));
378 }
379
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700381 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382
383 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700384 rtp_header.timestamp += 160;
385 rtp_header.sequenceNumber += 1;
386 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387}
388
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000389TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
390 UseNoMocks();
391 CreateInstance();
392
393 const int kPayloadLengthSamples = 80;
394 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
395 const uint8_t kPayloadType = 17; // Just an arbitrary number.
396 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
397 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700398 RTPHeader rtp_header;
399 rtp_header.payloadType = kPayloadType;
400 rtp_header.sequenceNumber = 0x1234;
401 rtp_header.timestamp = 0x12345678;
402 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000403
Niels Möller05543682019-01-10 16:55:06 +0100404 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
405 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000406
407 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700408 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000409 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700410 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
411 rtp_header.timestamp += kPayloadLengthSamples;
412 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
414 }
415
416 // Insert one more packet and make sure the buffer got flushed. That is, it
417 // should only hold one single packet.
418 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700419 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700420 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700421 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700422 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
423 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000424}
425
solenberg2779bab2016-11-17 04:45:19 -0800426TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100427 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800428}
solenberg99df6c02016-10-11 04:35:34 -0700429
solenberg2779bab2016-11-17 04:45:19 -0800430TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100431 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800432}
solenberg99df6c02016-10-11 04:35:34 -0700433
solenberg2779bab2016-11-17 04:45:19 -0800434TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100435 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800436}
solenberg99df6c02016-10-11 04:35:34 -0700437
solenberg2779bab2016-11-17 04:45:19 -0800438TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100439 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700440}
441
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000442// This test verifies that timestamps propagate from the incoming packets
443// through to the sync buffer and to the playout timestamp.
444TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000445 const uint8_t kPayloadType = 17; // Just an arbitrary number.
446 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
447 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700448 const size_t kPayloadLengthSamples =
449 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000450 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
451 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700452 RTPHeader rtp_header;
453 rtp_header.payloadType = kPayloadType;
454 rtp_header.sequenceNumber = 0x1234;
455 rtp_header.timestamp = 0x12345678;
456 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000457
458 // This is a dummy decoder that produces as many output samples as the input
459 // has bytes. The output is an increasing series, starting at 1 for the first
460 // sample, and then increasing by 1 for each sample.
461 class CountingSamplesDecoder : public AudioDecoder {
462 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000463 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000464
465 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100466 int DecodeInternal(const uint8_t* encoded,
467 size_t encoded_len,
468 int /* sample_rate_hz */,
469 int16_t* decoded,
470 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000471 for (size_t i = 0; i < encoded_len; ++i) {
472 decoded[i] = next_value_++;
473 }
474 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200475 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000476 }
477
Karl Wiberg43766482015-08-27 15:22:11 +0200478 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000479
kwiberg347d3512016-06-16 01:59:09 -0700480 int SampleRateHz() const override { return kSampleRateHz; }
481
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000482 size_t Channels() const override { return 1; }
483
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000484 uint16_t next_value() const { return next_value_; }
485
486 private:
487 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000488 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489
Niels Möllerb7180c02018-12-06 13:07:11 +0100490 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
491 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
492
493 UseNoMocks();
494 CreateInstance(decoder_factory);
495
496 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100497 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000498
499 // Insert one packet.
500 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700501 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
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
513 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700514 EXPECT_EQ(kPayloadLengthSamples,
515 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000516
517 // The value of the last of the output samples is the same as the number of
518 // samples played from the decoded packet. Thus, this number + the RTP
519 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200520 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700521 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200522 absl::optional<uint32_t>(rtp_header.timestamp +
523 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700524 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000525
526 // Check the timestamp for the last value in the sync buffer. This should
527 // be one full frame length ahead of the RTP timestamp.
528 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
529 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700530 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000531 sync_buffer->end_timestamp());
532
533 // Check that the number of samples still to play from the sync buffer add
534 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800535 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700536 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800537 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000538}
539
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000540TEST_F(NetEqImplTest, ReorderedPacket) {
541 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100542 // Create a mock decoder object.
543 MockAudioDecoder mock_decoder;
544
545 CreateInstance(
546 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000547
548 const uint8_t kPayloadType = 17; // Just an arbitrary number.
549 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
550 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700551 const size_t kPayloadLengthSamples =
552 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000553 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
554 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700555 RTPHeader rtp_header;
556 rtp_header.payloadType = kPayloadType;
557 rtp_header.sequenceNumber = 0x1234;
558 rtp_header.timestamp = 0x12345678;
559 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000560
Karl Wiberg43766482015-08-27 15:22:11 +0200561 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700562 EXPECT_CALL(mock_decoder, SampleRateHz())
563 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000564 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000565 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
566 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700567 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200568 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000569 int16_t dummy_output[kPayloadLengthSamples] = {0};
570 // The below expectation will make the mock decoder write
571 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100572 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
573 kSampleRateHz, _, _))
574 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100576 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200577 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100578 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
579 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000580
581 // Insert one packet.
582 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700583 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584
585 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700586 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800587 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700588 bool muted;
589 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800590 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
591 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800592 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000593
594 // Insert two more packets. The first one is out of order, and is already too
595 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700596 rtp_header.sequenceNumber -= 1;
597 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598 payload[0] = 1;
599 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700600 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
601 rtp_header.sequenceNumber += 2;
602 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 payload[0] = 2;
604 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000606
607 // Expect only the second packet to be decoded (the one with "2" as the first
608 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100609 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
610 kSampleRateHz, _, _))
611 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000612 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100613 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200614 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000615
616 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800618 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
619 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800620 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000621
622 // Now check the packet buffer, and make sure it is empty, since the
623 // out-of-order packet should have been discarded.
624 EXPECT_TRUE(packet_buffer_->Empty());
625
626 EXPECT_CALL(mock_decoder, Die());
627}
628
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000629// This test verifies that NetEq can handle the situation where the first
630// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000631TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000632 UseNoMocks();
633 CreateInstance();
634
635 const uint8_t kPayloadType = 17; // Just an arbitrary number.
636 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
637 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700638 const size_t kPayloadLengthSamples =
639 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700640 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000641 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700642 RTPHeader rtp_header;
643 rtp_header.payloadType = kPayloadType;
644 rtp_header.sequenceNumber = 0x1234;
645 rtp_header.timestamp = 0x12345678;
646 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000647
648 // Insert one packet. Note that we have not registered any payload type, so
649 // this packet will be rejected.
650 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700651 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000652
653 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700654 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800655 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700656 bool muted;
657 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800658 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
659 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
660 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800661 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000662
663 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100664 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
665 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000666
667 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700668 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700669 rtp_header.sequenceNumber++;
670 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700672 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000673 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
674 }
675
676 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700678 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800679 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
680 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
681 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800682 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000683 << "NetEq did not decode the packets as expected.";
684 }
685}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000686
687// This test verifies that NetEq can handle comfort noise and enters/quits codec
688// internal CNG mode properly.
689TEST_F(NetEqImplTest, CodecInternalCng) {
690 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100691 // Create a mock decoder object.
692 MockAudioDecoder mock_decoder;
693 CreateInstance(
694 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000695
696 const uint8_t kPayloadType = 17; // Just an arbitrary number.
697 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
698 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700699 const size_t kPayloadLengthSamples =
700 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
701 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000702 uint8_t payload[kPayloadLengthBytes] = {0};
703 int16_t dummy_output[kPayloadLengthSamples] = {0};
704
henrik.lundin246ef3e2017-04-24 09:14:32 -0700705 RTPHeader rtp_header;
706 rtp_header.payloadType = kPayloadType;
707 rtp_header.sequenceNumber = 0x1234;
708 rtp_header.timestamp = 0x12345678;
709 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000710
Karl Wiberg43766482015-08-27 15:22:11 +0200711 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700712 EXPECT_CALL(mock_decoder, SampleRateHz())
713 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000714 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000715 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
716 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700717 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200718 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700719 // Packed duration when asking the decoder for more CNG data (without a new
720 // packet).
721 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200722 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000723
724 // Pointee(x) verifies that first byte of the payload equals x, this makes it
725 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100726 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
727 kSampleRateKhz * 1000, _, _))
728 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000729 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100730 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200731 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000732
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100733 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
734 kSampleRateKhz * 1000, _, _))
735 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000736 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100737 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200738 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000739
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100740 EXPECT_CALL(mock_decoder,
741 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
742 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000743 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100744 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200745 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000746
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100747 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
748 kSampleRateKhz * 1000, _, _))
749 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000750 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100751 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200752 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000753
Niels Möller50b66d52018-12-11 14:43:21 +0100754 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
755 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756
757 // Insert one packet (decoder will return speech).
758 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700759 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000760
761 // Insert second packet (decoder will return CNG).
762 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700763 rtp_header.sequenceNumber++;
764 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700766 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000767
Peter Kastingdce40cf2015-08-24 14:52:23 -0700768 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800769 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800770 AudioFrame::SpeechType expected_type[8] = {
771 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
772 AudioFrame::kCNG, AudioFrame::kCNG,
773 AudioFrame::kCNG, AudioFrame::kCNG,
774 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000775 };
776 int expected_timestamp_increment[8] = {
777 -1, // will not be used.
778 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700779 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
780 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000781 50 * kSampleRateKhz, 10 * kSampleRateKhz
782 };
783
henrik.lundin7a926812016-05-12 13:51:28 -0700784 bool muted;
785 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200786 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700787 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000788
henrik.lundin0d96ab72016-04-06 12:28:26 -0700789 // Lambda for verifying the timestamps.
790 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200791 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700792 if (expected_timestamp_increment[i] == -1) {
793 // Expect to get an empty timestamp value during CNG and PLC.
794 EXPECT_FALSE(ts) << "i = " << i;
795 } else {
796 ASSERT_TRUE(ts) << "i = " << i;
797 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
798 << "i = " << i;
799 last_timestamp = ts;
800 }
801 };
802
Peter Kastingdce40cf2015-08-24 14:52:23 -0700803 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800804 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
805 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800806 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700807 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700808 SCOPED_TRACE("");
809 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000810 }
811
812 // Insert third packet, which leaves a gap from last packet.
813 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700814 rtp_header.sequenceNumber += 2;
815 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000816 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700817 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000818
Peter Kastingdce40cf2015-08-24 14:52:23 -0700819 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800820 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
821 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800822 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700823 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700824 SCOPED_TRACE("");
825 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000826 }
827
828 // Now check the packet buffer, and make sure it is empty.
829 EXPECT_TRUE(packet_buffer_->Empty());
830
831 EXPECT_CALL(mock_decoder, Die());
832}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000833
834TEST_F(NetEqImplTest, UnsupportedDecoder) {
835 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100836 ::testing::NiceMock<MockAudioDecoder> decoder;
837
838 CreateInstance(
839 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700840 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800841 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000842
843 const uint8_t kPayloadType = 17; // Just an arbitrary number.
844 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
845 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000846
Peter Kastingdce40cf2015-08-24 14:52:23 -0700847 const size_t kPayloadLengthSamples =
848 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000849 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700850 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200851 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700852 RTPHeader rtp_header;
853 rtp_header.payloadType = kPayloadType;
854 rtp_header.sequenceNumber = 0x1234;
855 rtp_header.timestamp = 0x12345678;
856 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000857
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000858 const uint8_t kFirstPayloadValue = 1;
859 const uint8_t kSecondPayloadValue = 2;
860
ossu61a208b2016-09-20 01:38:00 -0700861 EXPECT_CALL(decoder,
862 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
863 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200864 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000865
ossu61a208b2016-09-20 01:38:00 -0700866 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000867 .Times(0);
868
ossu61a208b2016-09-20 01:38:00 -0700869 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
870 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000871 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700872 .WillOnce(DoAll(
873 SetArrayArgument<3>(dummy_output,
874 dummy_output + kPayloadLengthSamples * kChannels),
875 SetArgPointee<4>(AudioDecoder::kSpeech),
876 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000877
ossu61a208b2016-09-20 01:38:00 -0700878 EXPECT_CALL(decoder,
879 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
880 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200881 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700882
883 EXPECT_CALL(decoder, SampleRateHz())
884 .WillRepeatedly(Return(kSampleRateHz));
885
886 EXPECT_CALL(decoder, Channels())
887 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000888
Niels Möllera1eb9c72018-12-07 15:24:42 +0100889 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
890 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000891
892 // Insert one packet.
893 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
894 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700895 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897 // Insert another packet.
898 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700899 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000900 // The second timestamp needs to be at least 30 ms after the first to make
901 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700902 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000903 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905
henrik.lundin6d8e0112016-03-04 10:34:21 -0800906 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700907 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800908 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200909 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700910 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 // Output size and number of channels should be correct.
912 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
913 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
914 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000915
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 // Second call to GetAudio will decode the packet that is ok. No errors are
917 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700918 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800919 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
920 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700921
922 // Die isn't called through NiceMock (since it's called by the
923 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
924 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000925}
926
henrik.lundin116c84e2015-08-27 13:14:48 -0700927// This test inserts packets until the buffer is flushed. After that, it asks
928// NetEq for the network statistics. The purpose of the test is to make sure
929// that even though the buffer size increment is negative (which it becomes when
930// the packet causing a flush is inserted), the packet length stored in the
931// decision logic remains valid.
932TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
933 UseNoMocks();
934 CreateInstance();
935
936 const size_t kPayloadLengthSamples = 80;
937 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
938 const uint8_t kPayloadType = 17; // Just an arbitrary number.
939 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
940 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700941 RTPHeader rtp_header;
942 rtp_header.payloadType = kPayloadType;
943 rtp_header.sequenceNumber = 0x1234;
944 rtp_header.timestamp = 0x12345678;
945 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700946
Niels Möller05543682019-01-10 16:55:06 +0100947 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
948 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -0700949
950 // Insert packets until the buffer flushes.
951 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
952 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
953 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700954 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
955 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
956 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700957 }
958 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
959
960 // Ask for network statistics. This should not crash.
961 NetEqNetworkStatistics stats;
962 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
963}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200964
965TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
966 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100967 // Create a mock decoder object.
968 MockAudioDecoder mock_decoder;
969
970 CreateInstance(
971 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200972
973 const uint8_t kPayloadType = 17; // Just an arbitrary number.
974 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
975 const int kSampleRateHz = 8000;
976 const size_t kPayloadLengthSamples =
977 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
978 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
979 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700980 RTPHeader rtp_header;
981 rtp_header.payloadType = kPayloadType;
982 rtp_header.sequenceNumber = 0x1234;
983 rtp_header.timestamp = 0x12345678;
984 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200985
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200986 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700987 EXPECT_CALL(mock_decoder, SampleRateHz())
988 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200989 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
990 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
991 .WillRepeatedly(Return(0));
992 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200993 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200994 int16_t dummy_output[kPayloadLengthSamples] = {0};
995 // The below expectation will make the mock decoder write
996 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
997 // speech. That is, the decoded length is 5 samples shorter than the expected.
998 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100999 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001000 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001001 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001002 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001003 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001004 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001005 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1006 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007
1008 // Insert one packet.
1009 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001010 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001011
1012 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1013
1014 // Pull audio once.
1015 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001016 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001017 bool muted;
1018 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001019 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1020 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001021 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001022
1023 EXPECT_CALL(mock_decoder, Die());
1024}
minyuel6d92bf52015-09-23 15:20:39 +02001025
1026// This test checks the behavior of NetEq when audio decoder fails.
1027TEST_F(NetEqImplTest, DecodingError) {
1028 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001029 // Create a mock decoder object.
1030 MockAudioDecoder mock_decoder;
1031
1032 CreateInstance(
1033 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001034
1035 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1036 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1037 const int kSampleRateHz = 8000;
1038 const int kDecoderErrorCode = -97; // Any negative number.
1039
1040 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1041 const size_t kFrameLengthSamples =
1042 static_cast<size_t>(5 * kSampleRateHz / 1000);
1043
1044 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1045
1046 uint8_t payload[kPayloadLengthBytes] = {0};
1047
henrik.lundin246ef3e2017-04-24 09:14:32 -07001048 RTPHeader rtp_header;
1049 rtp_header.payloadType = kPayloadType;
1050 rtp_header.sequenceNumber = 0x1234;
1051 rtp_header.timestamp = 0x12345678;
1052 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001053
minyuel6d92bf52015-09-23 15:20:39 +02001054 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001055 EXPECT_CALL(mock_decoder, SampleRateHz())
1056 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001057 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1058 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1059 .WillRepeatedly(Return(0));
1060 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001061 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001062 EXPECT_CALL(mock_decoder, ErrorCode())
1063 .WillOnce(Return(kDecoderErrorCode));
1064 EXPECT_CALL(mock_decoder, HasDecodePlc())
1065 .WillOnce(Return(false));
1066 int16_t dummy_output[kFrameLengthSamples] = {0};
1067
1068 {
1069 InSequence sequence; // Dummy variable.
1070 // Mock decoder works normally the first time.
1071 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001072 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001073 .Times(3)
1074 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001075 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001076 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001077 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001078 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001079 .RetiresOnSaturation();
1080
1081 // Then mock decoder fails. A common reason for failure can be buffer being
1082 // too short
1083 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001084 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001085 .WillOnce(Return(-1))
1086 .RetiresOnSaturation();
1087
1088 // Mock decoder finally returns to normal.
1089 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001090 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001091 .Times(2)
1092 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001093 DoAll(SetArrayArgument<3>(dummy_output,
1094 dummy_output + kFrameLengthSamples),
1095 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001096 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001097 }
1098
Niels Möllera1eb9c72018-12-07 15:24:42 +01001099 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1100 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001101
1102 // Insert packets.
1103 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001104 rtp_header.sequenceNumber += 1;
1105 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001106 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001107 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001108 }
1109
1110 // Pull audio.
1111 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001112 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001113 bool muted;
1114 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001115 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1116 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001117 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001118
1119 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001120 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001121 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1122 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001123 // We are not expecting anything for output.speech_type_, since an error was
1124 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001125
1126 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001127 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001128 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1129 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001130 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001131
1132 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001133 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001134 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1135 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001136 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001137
1138 EXPECT_CALL(mock_decoder, Die());
1139}
1140
1141// This test checks the behavior of NetEq when audio decoder fails during CNG.
1142TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1143 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001144
1145 // Create a mock decoder object.
1146 MockAudioDecoder mock_decoder;
1147 CreateInstance(
1148 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001149
1150 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1151 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1152 const int kSampleRateHz = 8000;
1153 const int kDecoderErrorCode = -97; // Any negative number.
1154
1155 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1156 const size_t kFrameLengthSamples =
1157 static_cast<size_t>(5 * kSampleRateHz / 1000);
1158
1159 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1160
1161 uint8_t payload[kPayloadLengthBytes] = {0};
1162
henrik.lundin246ef3e2017-04-24 09:14:32 -07001163 RTPHeader rtp_header;
1164 rtp_header.payloadType = kPayloadType;
1165 rtp_header.sequenceNumber = 0x1234;
1166 rtp_header.timestamp = 0x12345678;
1167 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001168
minyuel6d92bf52015-09-23 15:20:39 +02001169 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001170 EXPECT_CALL(mock_decoder, SampleRateHz())
1171 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001172 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1173 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1174 .WillRepeatedly(Return(0));
1175 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001176 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001177 EXPECT_CALL(mock_decoder, ErrorCode())
1178 .WillOnce(Return(kDecoderErrorCode));
1179 int16_t dummy_output[kFrameLengthSamples] = {0};
1180
1181 {
1182 InSequence sequence; // Dummy variable.
1183 // Mock decoder works normally the first 2 times.
1184 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001185 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001186 .Times(2)
1187 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001188 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001189 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001190 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001191 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001192 .RetiresOnSaturation();
1193
1194 // Then mock decoder fails. A common reason for failure can be buffer being
1195 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001196 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001197 .WillOnce(Return(-1))
1198 .RetiresOnSaturation();
1199
1200 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001201 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001202 .Times(2)
1203 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001204 DoAll(SetArrayArgument<3>(dummy_output,
1205 dummy_output + kFrameLengthSamples),
1206 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001207 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001208 }
1209
Niels Möller50b66d52018-12-11 14:43:21 +01001210 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1211 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001212
1213 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1214 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001215 rtp_header.sequenceNumber += 1;
1216 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001217 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001218 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001219 }
1220
1221 // Pull audio.
1222 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001223 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001224 bool muted;
1225 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001226 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1227 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001228 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001229
1230 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001231 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001232 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1233 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001234 // We are not expecting anything for output.speech_type_, since an error was
1235 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001236
1237 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001238 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001239 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1240 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001241 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001242
1243 EXPECT_CALL(mock_decoder, Die());
1244}
1245
henrik.lundind89814b2015-11-23 06:49:25 -08001246// Tests that the return value from last_output_sample_rate_hz() is equal to the
1247// configured inital sample rate.
1248TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1249 UseNoMocks();
1250 config_.sample_rate_hz = 48000;
1251 CreateInstance();
1252 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1253}
1254
henrik.lundined497212016-04-25 10:11:38 -07001255TEST_F(NetEqImplTest, TickTimerIncrement) {
1256 UseNoMocks();
1257 CreateInstance();
1258 ASSERT_TRUE(tick_timer_);
1259 EXPECT_EQ(0u, tick_timer_->ticks());
1260 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001261 bool muted;
1262 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001263 EXPECT_EQ(1u, tick_timer_->ticks());
1264}
1265
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001266TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1267 UseNoMocks();
1268 use_mock_delay_manager_ = true;
1269 CreateInstance();
1270
1271 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1272 .WillOnce(Return(true))
1273 .WillOnce(Return(false));
1274
1275 const int delay_ms = 200;
1276
1277 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1278 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1279}
1280
1281TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1282 UseNoMocks();
1283 use_mock_delay_manager_ = true;
1284 CreateInstance();
1285
1286 const int delay_ms = 200;
1287
1288 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1289 .WillOnce(Return(delay_ms));
1290
1291 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1292}
1293
henrik.lundin114c1b32017-04-26 07:47:32 -07001294TEST_F(NetEqImplTest, TargetDelayMs) {
1295 UseNoMocks();
1296 use_mock_delay_manager_ = true;
1297 CreateInstance();
1298 // Let the dummy target delay be 17 packets.
1299 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1300 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1301 .WillOnce(Return(kTargetLevelPacketsQ8));
1302 // Default packet size before any packet has been decoded is 30 ms, so we are
1303 // expecting 17 * 30 = 510 ms target delay.
1304 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1305}
1306
henrik.lundinb8c55b12017-05-10 07:38:01 -07001307TEST_F(NetEqImplTest, InsertEmptyPacket) {
1308 UseNoMocks();
1309 use_mock_delay_manager_ = true;
1310 CreateInstance();
1311
1312 RTPHeader rtp_header;
1313 rtp_header.payloadType = 17;
1314 rtp_header.sequenceNumber = 0x1234;
1315 rtp_header.timestamp = 0x12345678;
1316 rtp_header.ssrc = 0x87654321;
1317
1318 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1319 neteq_->InsertEmptyPacket(rtp_header);
1320}
1321
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001322TEST_F(NetEqImplTest, EnableRtxHandling) {
1323 UseNoMocks();
1324 use_mock_delay_manager_ = true;
1325 config_.enable_rtx_handling = true;
1326 CreateInstance();
1327 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1328 .Times(1)
1329 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1330
1331 const int kPayloadLengthSamples = 80;
1332 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1333 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1334 const uint32_t kReceiveTime = 17;
1335 uint8_t payload[kPayloadLengthBytes] = {0};
1336 RTPHeader rtp_header;
1337 rtp_header.payloadType = kPayloadType;
1338 rtp_header.sequenceNumber = 0x1234;
1339 rtp_header.timestamp = 0x12345678;
1340 rtp_header.ssrc = 0x87654321;
1341
Niels Möller05543682019-01-10 16:55:06 +01001342 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1343 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001344 EXPECT_EQ(NetEq::kOK,
1345 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1346 AudioFrame output;
1347 bool muted;
1348 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1349
1350 // Insert second packet that was sent before the first packet.
1351 rtp_header.sequenceNumber -= 1;
1352 rtp_header.timestamp -= kPayloadLengthSamples;
1353 EXPECT_CALL(*mock_delay_manager_,
1354 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1355 EXPECT_EQ(NetEq::kOK,
1356 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1357}
1358
minyue5bd33972016-05-02 04:46:11 -07001359class Decoder120ms : public AudioDecoder {
1360 public:
kwiberg347d3512016-06-16 01:59:09 -07001361 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1362 : sample_rate_hz_(sample_rate_hz),
1363 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001364 speech_type_(speech_type) {}
1365
1366 int DecodeInternal(const uint8_t* encoded,
1367 size_t encoded_len,
1368 int sample_rate_hz,
1369 int16_t* decoded,
1370 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001371 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001372 size_t decoded_len =
1373 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1374 for (size_t i = 0; i < decoded_len; ++i) {
1375 decoded[i] = next_value_++;
1376 }
1377 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001378 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001379 }
1380
1381 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001382 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001383 size_t Channels() const override { return 2; }
1384
1385 private:
kwiberg347d3512016-06-16 01:59:09 -07001386 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001387 int16_t next_value_;
1388 SpeechType speech_type_;
1389};
1390
1391class NetEqImplTest120ms : public NetEqImplTest {
1392 protected:
1393 NetEqImplTest120ms() : NetEqImplTest() {}
1394 virtual ~NetEqImplTest120ms() {}
1395
1396 void CreateInstanceNoMocks() {
1397 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001398 CreateInstance(decoder_factory_);
1399 EXPECT_TRUE(neteq_->RegisterPayloadType(
1400 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001401 }
1402
1403 void CreateInstanceWithDelayManagerMock() {
1404 UseNoMocks();
1405 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001406 CreateInstance(decoder_factory_);
1407 EXPECT_TRUE(neteq_->RegisterPayloadType(
1408 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001409 }
1410
1411 uint32_t timestamp_diff_between_packets() const {
1412 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1413 }
1414
1415 uint32_t first_timestamp() const { return 10u; }
1416
1417 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001418 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001419 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001420 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1421 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001422 }
1423 }
1424
1425 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001426 RTPHeader rtp_header;
1427 rtp_header.payloadType = kPayloadType;
1428 rtp_header.sequenceNumber = sequence_number_;
1429 rtp_header.timestamp = timestamp;
1430 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001431 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1432 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001433 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001434 sequence_number_++;
1435 }
1436
1437 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001438 const uint32_t sampling_freq = kSamplingFreq_;
1439 decoder_factory_ =
1440 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1441 [sampling_freq, speech_type]() {
1442 std::unique_ptr<AudioDecoder> decoder =
1443 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1444 RTC_CHECK_EQ(2, decoder->Channels());
1445 return decoder;
1446 });
minyue5bd33972016-05-02 04:46:11 -07001447 }
1448
Niels Möllera0f44302018-11-30 10:45:12 +01001449 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001450 AudioFrame output_;
1451 const uint32_t kPayloadType = 17;
1452 const uint32_t kSamplingFreq_ = 48000;
1453 uint16_t sequence_number_ = 1;
1454};
1455
minyue5bd33972016-05-02 04:46:11 -07001456TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001457 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001458 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001459
1460 InsertPacket(first_timestamp());
1461 GetFirstPacket();
1462
henrik.lundin7a926812016-05-12 13:51:28 -07001463 bool muted;
1464 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001465 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1466}
1467
1468TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001469 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001470 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001471
1472 InsertPacket(first_timestamp());
1473 GetFirstPacket();
1474
1475 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1476}
1477
1478TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001479 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001480 CreateInstanceWithDelayManagerMock();
1481
minyue5bd33972016-05-02 04:46:11 -07001482 InsertPacket(first_timestamp());
1483
1484 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001485 bool muted;
1486 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001487
1488 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1489
1490 // Delay manager reports a target level which should cause a Merge.
1491 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1492
henrik.lundin7a926812016-05-12 13:51:28 -07001493 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001494 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1495}
1496
1497TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001498 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001499 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001500
1501 InsertPacket(first_timestamp());
1502 GetFirstPacket();
1503
henrik.lundin7a926812016-05-12 13:51:28 -07001504 bool muted;
1505 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001506 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1507}
1508
1509TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001510 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001511 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001512
1513 InsertPacket(first_timestamp());
1514 GetFirstPacket();
1515 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1516
1517 // Delay manager report buffer limit which should cause a FastAccelerate.
1518 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1519 .Times(1)
1520 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1521
henrik.lundin7a926812016-05-12 13:51:28 -07001522 bool muted;
1523 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001524 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1525}
1526
1527TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001528 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001529 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001530
1531 InsertPacket(first_timestamp());
1532 GetFirstPacket();
1533
1534 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1535
1536 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1537 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1538 .Times(1)
1539 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1540
henrik.lundin7a926812016-05-12 13:51:28 -07001541 bool muted;
1542 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001543 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1544}
1545
1546TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001547 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001548 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001549
1550 InsertPacket(first_timestamp());
1551 GetFirstPacket();
1552
1553 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1554
1555 // Delay manager report buffer limit which should cause a Accelerate.
1556 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1557 .Times(1)
1558 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1559
henrik.lundin7a926812016-05-12 13:51:28 -07001560 bool muted;
1561 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001562 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1563}
1564
minyuel6d92bf52015-09-23 15:20:39 +02001565}// namespace webrtc