blob: 2b0d5832aeb296fc67a30f99ca513af4f26ca4b5 [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"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010017#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/include/neteq.h"
19#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
20#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
21#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
22#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
23#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
24#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
25#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
26#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
27#include "modules/audio_coding/neteq/neteq_impl.h"
28#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010029#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/sync_buffer.h"
31#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010032#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010033#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010034#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/gmock.h"
36#include "test/gtest.h"
37#include "test/mock_audio_decoder.h"
38#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010041using ::testing::AtLeast;
42using ::testing::DoAll;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043using ::testing::InSequence;
44using ::testing::Invoke;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000045using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010046using ::testing::Pointee;
47using ::testing::Return;
48using ::testing::ReturnNull;
49using ::testing::SetArgPointee;
50using ::testing::SetArrayArgument;
51using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
53namespace webrtc {
54
55// This function is called when inserting a packet list into the mock packet
56// buffer. The purpose is to delete all inserted packets properly, to avoid
57// memory leaks in the test.
58int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070059 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060 return PacketBuffer::kOK;
61}
62
63class NetEqImplTest : public ::testing::Test {
64 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070065 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000066
Niels Möllera0f44302018-11-30 10:45:12 +010067 void CreateInstance(
68 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
69 ASSERT_TRUE(decoder_factory);
70 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070071
72 // Get a local pointer to NetEq's TickTimer object.
73 tick_timer_ = deps.tick_timer.get();
74
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000075 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070076 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
77 mock_buffer_level_filter_ = mock.get();
78 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 buffer_level_filter_ = deps.buffer_level_filter.get();
81
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070083 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
84 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
86 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000088 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000090
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070092 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010093 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070094 mock_delay_peak_detector_ = mock.get();
95 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
96 deps.delay_peak_detector = std::move(mock);
97 }
98 delay_peak_detector_ = deps.delay_peak_detector.get();
99
100 if (use_mock_delay_manager_) {
101 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100102 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
103 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100104 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100105 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700106 mock_delay_manager_ = mock.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700107 deps.delay_manager = std::move(mock);
108 }
109 delay_manager_ = deps.delay_manager.get();
110
111 if (use_mock_dtmf_buffer_) {
112 std::unique_ptr<MockDtmfBuffer> mock(
113 new MockDtmfBuffer(config_.sample_rate_hz));
114 mock_dtmf_buffer_ = mock.get();
115 deps.dtmf_buffer = std::move(mock);
116 }
117 dtmf_buffer_ = deps.dtmf_buffer.get();
118
119 if (use_mock_dtmf_tone_generator_) {
120 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
121 mock_dtmf_tone_generator_ = mock.get();
122 deps.dtmf_tone_generator = std::move(mock);
123 }
124 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
125
126 if (use_mock_packet_buffer_) {
127 std::unique_ptr<MockPacketBuffer> mock(
128 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
129 mock_packet_buffer_ = mock.get();
130 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700131 }
132 packet_buffer_ = deps.packet_buffer.get();
133
134 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";
152 use_mock_buffer_level_filter_ = false;
153 use_mock_decoder_database_ = false;
154 use_mock_delay_peak_detector_ = false;
155 use_mock_delay_manager_ = false;
156 use_mock_dtmf_buffer_ = false;
157 use_mock_dtmf_tone_generator_ = false;
158 use_mock_packet_buffer_ = false;
159 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 }
161
162 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000163 if (use_mock_buffer_level_filter_) {
164 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
165 }
166 if (use_mock_decoder_database_) {
167 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
168 }
169 if (use_mock_delay_manager_) {
170 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
171 }
172 if (use_mock_delay_peak_detector_) {
173 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
174 }
175 if (use_mock_dtmf_buffer_) {
176 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
177 }
178 if (use_mock_dtmf_tone_generator_) {
179 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
180 }
181 if (use_mock_packet_buffer_) {
182 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
183 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 }
185
Niels Möller05543682019-01-10 16:55:06 +0100186 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800187 const size_t kPayloadLength = 4;
188 const uint8_t kPayloadType = 110;
189 const uint32_t kReceiveTime = 17;
190 const int kSampleRateHz = 16000;
191 config_.sample_rate_hz = kSampleRateHz;
192 UseNoMocks();
193 CreateInstance();
194 // Event: 2, E bit, Volume: 17, Length: 4336.
195 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700196 RTPHeader rtp_header;
197 rtp_header.payloadType = kPayloadType;
198 rtp_header.sequenceNumber = 0x1234;
199 rtp_header.timestamp = 0x12345678;
200 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800201
Niels Möller05543682019-01-10 16:55:06 +0100202 EXPECT_TRUE(neteq_->RegisterPayloadType(
203 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800204
205 // Insert first packet.
206 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700207 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800208
209 // Pull audio once.
210 const size_t kMaxOutputSize =
211 static_cast<size_t>(10 * kSampleRateHz / 1000);
212 AudioFrame output;
213 bool muted;
214 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
215 ASSERT_FALSE(muted);
216 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
217 EXPECT_EQ(1u, output.num_channels_);
218 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
219
220 // Verify first 64 samples of actual output.
221 const std::vector<int16_t> kOutput({
222 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
223 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
224 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
225 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
226 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
227 -2534, -1163 });
228 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700229 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800230 }
231
henrik.lundin1d9061e2016-04-26 12:19:34 -0700232 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000233 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700234 TickTimer* tick_timer_ = nullptr;
235 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
236 BufferLevelFilter* buffer_level_filter_ = nullptr;
237 bool use_mock_buffer_level_filter_ = true;
238 MockDecoderDatabase* mock_decoder_database_ = nullptr;
239 DecoderDatabase* decoder_database_ = nullptr;
240 bool use_mock_decoder_database_ = true;
241 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
242 DelayPeakDetector* delay_peak_detector_ = nullptr;
243 bool use_mock_delay_peak_detector_ = true;
244 MockDelayManager* mock_delay_manager_ = nullptr;
245 DelayManager* delay_manager_ = nullptr;
246 bool use_mock_delay_manager_ = true;
247 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
248 DtmfBuffer* dtmf_buffer_ = nullptr;
249 bool use_mock_dtmf_buffer_ = true;
250 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
251 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
252 bool use_mock_dtmf_tone_generator_ = true;
253 MockPacketBuffer* mock_packet_buffer_ = nullptr;
254 PacketBuffer* packet_buffer_ = nullptr;
255 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700256 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
257 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700258 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259};
260
261
262// This tests the interface class NetEq.
263// TODO(hlundin): Move to separate file?
264TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000265 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700266 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 delete neteq;
268}
269
kwiberg5adaf732016-10-04 09:33:27 -0700270TEST_F(NetEqImplTest, RegisterPayloadType) {
271 CreateInstance();
272 constexpr int rtp_payload_type = 0;
273 const SdpAudioFormat format("pcmu", 8000, 1);
274 EXPECT_CALL(*mock_decoder_database_,
275 RegisterPayload(rtp_payload_type, format));
276 neteq_->RegisterPayloadType(rtp_payload_type, format);
277}
278
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000280 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000282 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200284 // Check that kOK is returned when database returns kDecoderNotFound, because
285 // removing a payload type that was never registered is not an error.
286 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287}
288
kwiberg6b19b562016-09-20 04:02:25 -0700289TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
290 CreateInstance();
291 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
292 neteq_->RemoveAllPayloadTypes();
293}
294
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000296 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000297 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 const uint8_t kPayloadType = 0;
299 const uint16_t kFirstSequenceNumber = 0x1234;
300 const uint32_t kFirstTimestamp = 0x12345678;
301 const uint32_t kSsrc = 0x87654321;
302 const uint32_t kFirstReceiveTime = 17;
303 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700304 RTPHeader rtp_header;
305 rtp_header.payloadType = kPayloadType;
306 rtp_header.sequenceNumber = kFirstSequenceNumber;
307 rtp_header.timestamp = kFirstTimestamp;
308 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700309 Packet fake_packet;
310 fake_packet.payload_type = kPayloadType;
311 fake_packet.sequence_number = kFirstSequenceNumber;
312 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313
kwibergc0f2dcf2016-05-31 06:28:03 -0700314 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
315 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100316 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800317 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200318 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800319 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700320 EXPECT_EQ("pcmu", format.name);
321
322 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
323 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
324 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
325 // BWE update function called with first packet.
326 EXPECT_CALL(*mock_decoder,
327 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
328 kFirstTimestamp, kFirstReceiveTime));
329 // BWE update function called with second packet.
330 EXPECT_CALL(
331 *mock_decoder,
332 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
333 kFirstTimestamp + 160, kFirstReceiveTime + 155));
334 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
335
336 *dec = std::move(mock_decoder);
337 }));
Niels Möller72899062019-01-11 09:36:13 +0100338 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
339 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340
341 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000342 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 .WillRepeatedly(Return(&info));
344
345 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000346 EXPECT_CALL(*mock_packet_buffer_, Empty())
347 .WillOnce(Return(false)); // Called once after first packet is inserted.
348 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200350 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100352 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
353 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
355 // index) is a pointer, and the variable pointed to is set to kPayloadType.
356 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
357 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700358 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000359 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700360 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361
362 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000363 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 .Times(1);
365
366 // Expectations for delay manager.
367 {
368 // All expectations within this block must be called in this specific order.
369 InSequence sequence; // Dummy variable.
370 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000371 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 .Times(2)
373 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000374 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000376 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000378 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
379 .WillOnce(Return(0));
380 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 .WillOnce(Return(0));
382 }
383
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700385 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386
387 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700388 rtp_header.timestamp += 160;
389 rtp_header.sequenceNumber += 1;
390 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391}
392
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000393TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
394 UseNoMocks();
395 CreateInstance();
396
397 const int kPayloadLengthSamples = 80;
398 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
399 const uint8_t kPayloadType = 17; // Just an arbitrary number.
400 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
401 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700402 RTPHeader rtp_header;
403 rtp_header.payloadType = kPayloadType;
404 rtp_header.sequenceNumber = 0x1234;
405 rtp_header.timestamp = 0x12345678;
406 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000407
Niels Möller05543682019-01-10 16:55:06 +0100408 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
409 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000410
411 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700412 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700414 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
415 rtp_header.timestamp += kPayloadLengthSamples;
416 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000417 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
418 }
419
420 // Insert one more packet and make sure the buffer got flushed. That is, it
421 // should only hold one single packet.
422 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700423 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700424 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700425 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700426 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
427 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000428}
429
solenberg2779bab2016-11-17 04:45:19 -0800430TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100431 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800432}
solenberg99df6c02016-10-11 04:35:34 -0700433
solenberg2779bab2016-11-17 04:45:19 -0800434TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100435 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800436}
solenberg99df6c02016-10-11 04:35:34 -0700437
solenberg2779bab2016-11-17 04:45:19 -0800438TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100439 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800440}
solenberg99df6c02016-10-11 04:35:34 -0700441
solenberg2779bab2016-11-17 04:45:19 -0800442TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100443 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700444}
445
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000446// This test verifies that timestamps propagate from the incoming packets
447// through to the sync buffer and to the playout timestamp.
448TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000449 const uint8_t kPayloadType = 17; // Just an arbitrary number.
450 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
451 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700452 const size_t kPayloadLengthSamples =
453 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000454 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
455 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700456 RTPHeader rtp_header;
457 rtp_header.payloadType = kPayloadType;
458 rtp_header.sequenceNumber = 0x1234;
459 rtp_header.timestamp = 0x12345678;
460 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000461
462 // This is a dummy decoder that produces as many output samples as the input
463 // has bytes. The output is an increasing series, starting at 1 for the first
464 // sample, and then increasing by 1 for each sample.
465 class CountingSamplesDecoder : public AudioDecoder {
466 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000467 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000468
469 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100470 int DecodeInternal(const uint8_t* encoded,
471 size_t encoded_len,
472 int /* sample_rate_hz */,
473 int16_t* decoded,
474 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475 for (size_t i = 0; i < encoded_len; ++i) {
476 decoded[i] = next_value_++;
477 }
478 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200479 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000480 }
481
Karl Wiberg43766482015-08-27 15:22:11 +0200482 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000483
kwiberg347d3512016-06-16 01:59:09 -0700484 int SampleRateHz() const override { return kSampleRateHz; }
485
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000486 size_t Channels() const override { return 1; }
487
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000488 uint16_t next_value() const { return next_value_; }
489
490 private:
491 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000492 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000493
Niels Möllerb7180c02018-12-06 13:07:11 +0100494 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
495 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
496
497 UseNoMocks();
498 CreateInstance(decoder_factory);
499
500 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100501 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000502
503 // Insert one packet.
504 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700505 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000506
507 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700508 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800509 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700510 bool muted;
511 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
512 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800513 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
514 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800515 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000516
517 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700518 EXPECT_EQ(kPayloadLengthSamples,
519 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000520
521 // The value of the last of the output samples is the same as the number of
522 // samples played from the decoded packet. Thus, this number + the RTP
523 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200524 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700525 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200526 absl::optional<uint32_t>(rtp_header.timestamp +
527 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700528 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000529
530 // Check the timestamp for the last value in the sync buffer. This should
531 // be one full frame length ahead of the RTP timestamp.
532 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
533 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700534 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000535 sync_buffer->end_timestamp());
536
537 // Check that the number of samples still to play from the sync buffer add
538 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800539 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700540 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800541 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000542}
543
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000544TEST_F(NetEqImplTest, ReorderedPacket) {
545 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100546 // Create a mock decoder object.
547 MockAudioDecoder mock_decoder;
548
549 CreateInstance(
550 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000551
552 const uint8_t kPayloadType = 17; // Just an arbitrary number.
553 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
554 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700555 const size_t kPayloadLengthSamples =
556 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000557 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
558 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700559 RTPHeader rtp_header;
560 rtp_header.payloadType = kPayloadType;
561 rtp_header.sequenceNumber = 0x1234;
562 rtp_header.timestamp = 0x12345678;
563 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000564
Karl Wiberg43766482015-08-27 15:22:11 +0200565 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700566 EXPECT_CALL(mock_decoder, SampleRateHz())
567 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000568 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000569 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
570 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700571 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200572 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000573 int16_t dummy_output[kPayloadLengthSamples] = {0};
574 // The below expectation will make the mock decoder write
575 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100576 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
577 kSampleRateHz, _, _))
578 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000579 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100580 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200581 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100582 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
583 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584
585 // Insert one packet.
586 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700587 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000588
589 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700590 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800591 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700592 bool muted;
593 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800594 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
595 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800596 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000597
598 // Insert two more packets. The first one is out of order, and is already too
599 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700600 rtp_header.sequenceNumber -= 1;
601 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000602 payload[0] = 1;
603 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700604 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
605 rtp_header.sequenceNumber += 2;
606 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000607 payload[0] = 2;
608 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700609 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000610
611 // Expect only the second packet to be decoded (the one with "2" as the first
612 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100613 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
614 kSampleRateHz, _, _))
615 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100617 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200618 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000619
620 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700621 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800622 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
623 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800624 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000625
626 // Now check the packet buffer, and make sure it is empty, since the
627 // out-of-order packet should have been discarded.
628 EXPECT_TRUE(packet_buffer_->Empty());
629
630 EXPECT_CALL(mock_decoder, Die());
631}
632
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000633// This test verifies that NetEq can handle the situation where the first
634// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000635TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000636 UseNoMocks();
637 CreateInstance();
638
639 const uint8_t kPayloadType = 17; // Just an arbitrary number.
640 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
641 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700642 const size_t kPayloadLengthSamples =
643 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700644 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000645 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700646 RTPHeader rtp_header;
647 rtp_header.payloadType = kPayloadType;
648 rtp_header.sequenceNumber = 0x1234;
649 rtp_header.timestamp = 0x12345678;
650 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000651
652 // Insert one packet. Note that we have not registered any payload type, so
653 // this packet will be rejected.
654 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700655 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000656
657 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700658 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800659 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700660 bool muted;
661 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800662 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
663 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
664 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800665 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000666
667 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100668 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
669 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000670
671 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700672 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700673 rtp_header.sequenceNumber++;
674 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000675 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700676 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000677 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
678 }
679
680 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700682 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800683 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
684 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
685 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800686 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000687 << "NetEq did not decode the packets as expected.";
688 }
689}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000690
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200691// This test verifies that audio interruption is not logged for the initial
692// PLC period before the first packet is deocoded.
693// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
694TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
695 UseNoMocks();
696 CreateInstance();
697
698 const uint8_t kPayloadType = 17; // Just an arbitrary number.
699 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
700 const int kSampleRateHz = 8000;
701 const size_t kPayloadLengthSamples =
702 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
703 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
704 uint8_t payload[kPayloadLengthBytes] = {0};
705 RTPHeader rtp_header;
706 rtp_header.payloadType = kPayloadType;
707 rtp_header.sequenceNumber = 0x1234;
708 rtp_header.timestamp = 0x12345678;
709 rtp_header.ssrc = 0x87654321;
710
711 // Register the payload type.
712 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
713 SdpAudioFormat("l16", 8000, 1)));
714
715 // Pull audio several times. No packets have been inserted yet.
716 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
717 AudioFrame output;
718 bool muted;
719 for (int i = 0; i < 100; ++i) {
720 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
721 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
722 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
723 EXPECT_EQ(1u, output.num_channels_);
724 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
725 }
726
727 // Insert 10 packets.
728 for (size_t i = 0; i < 10; ++i) {
729 rtp_header.sequenceNumber++;
730 rtp_header.timestamp += kPayloadLengthSamples;
731 EXPECT_EQ(NetEq::kOK,
732 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
733 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
734 }
735
736 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
737 for (size_t i = 0; i < 3; ++i) {
738 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
739 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
740 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
741 EXPECT_EQ(1u, output.num_channels_);
742 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
743 << "NetEq did not decode the packets as expected.";
744 }
745
746 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200747 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200748}
749
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000750// This test verifies that NetEq can handle comfort noise and enters/quits codec
751// internal CNG mode properly.
752TEST_F(NetEqImplTest, CodecInternalCng) {
753 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100754 // Create a mock decoder object.
755 MockAudioDecoder mock_decoder;
756 CreateInstance(
757 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000758
759 const uint8_t kPayloadType = 17; // Just an arbitrary number.
760 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
761 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700762 const size_t kPayloadLengthSamples =
763 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
764 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765 uint8_t payload[kPayloadLengthBytes] = {0};
766 int16_t dummy_output[kPayloadLengthSamples] = {0};
767
henrik.lundin246ef3e2017-04-24 09:14:32 -0700768 RTPHeader rtp_header;
769 rtp_header.payloadType = kPayloadType;
770 rtp_header.sequenceNumber = 0x1234;
771 rtp_header.timestamp = 0x12345678;
772 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000773
Karl Wiberg43766482015-08-27 15:22:11 +0200774 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700775 EXPECT_CALL(mock_decoder, SampleRateHz())
776 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000777 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000778 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
779 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700780 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200781 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700782 // Packed duration when asking the decoder for more CNG data (without a new
783 // packet).
784 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200785 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000786
787 // Pointee(x) verifies that first byte of the payload equals x, this makes it
788 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100789 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
790 kSampleRateKhz * 1000, _, _))
791 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000792 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100793 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200794 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000795
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100796 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
797 kSampleRateKhz * 1000, _, _))
798 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000799 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100800 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200801 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000802
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100803 EXPECT_CALL(mock_decoder,
804 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
805 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000806 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100807 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200808 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000809
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100810 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
811 kSampleRateKhz * 1000, _, _))
812 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000813 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100814 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200815 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000816
Niels Möller50b66d52018-12-11 14:43:21 +0100817 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
818 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000819
820 // Insert one packet (decoder will return speech).
821 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700822 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823
824 // Insert second packet (decoder will return CNG).
825 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700826 rtp_header.sequenceNumber++;
827 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000828 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700829 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000830
Peter Kastingdce40cf2015-08-24 14:52:23 -0700831 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800832 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800833 AudioFrame::SpeechType expected_type[8] = {
834 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
835 AudioFrame::kCNG, AudioFrame::kCNG,
836 AudioFrame::kCNG, AudioFrame::kCNG,
837 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000838 };
839 int expected_timestamp_increment[8] = {
840 -1, // will not be used.
841 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700842 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
843 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000844 50 * kSampleRateKhz, 10 * kSampleRateKhz
845 };
846
henrik.lundin7a926812016-05-12 13:51:28 -0700847 bool muted;
848 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200849 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700850 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000851
henrik.lundin0d96ab72016-04-06 12:28:26 -0700852 // Lambda for verifying the timestamps.
853 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200854 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700855 if (expected_timestamp_increment[i] == -1) {
856 // Expect to get an empty timestamp value during CNG and PLC.
857 EXPECT_FALSE(ts) << "i = " << i;
858 } else {
859 ASSERT_TRUE(ts) << "i = " << i;
860 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
861 << "i = " << i;
862 last_timestamp = ts;
863 }
864 };
865
Peter Kastingdce40cf2015-08-24 14:52:23 -0700866 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800867 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
868 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800869 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700870 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700871 SCOPED_TRACE("");
872 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000873 }
874
875 // Insert third packet, which leaves a gap from last packet.
876 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700877 rtp_header.sequenceNumber += 2;
878 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000879 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700880 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000881
Peter Kastingdce40cf2015-08-24 14:52:23 -0700882 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800883 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
884 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800885 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700886 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700887 SCOPED_TRACE("");
888 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000889 }
890
891 // Now check the packet buffer, and make sure it is empty.
892 EXPECT_TRUE(packet_buffer_->Empty());
893
894 EXPECT_CALL(mock_decoder, Die());
895}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897TEST_F(NetEqImplTest, UnsupportedDecoder) {
898 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100899 ::testing::NiceMock<MockAudioDecoder> decoder;
900
901 CreateInstance(
902 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700903 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800904 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905
906 const uint8_t kPayloadType = 17; // Just an arbitrary number.
907 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
908 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000909
Peter Kastingdce40cf2015-08-24 14:52:23 -0700910 const size_t kPayloadLengthSamples =
911 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000912 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700913 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200914 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700915 RTPHeader rtp_header;
916 rtp_header.payloadType = kPayloadType;
917 rtp_header.sequenceNumber = 0x1234;
918 rtp_header.timestamp = 0x12345678;
919 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000920
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000921 const uint8_t kFirstPayloadValue = 1;
922 const uint8_t kSecondPayloadValue = 2;
923
ossu61a208b2016-09-20 01:38:00 -0700924 EXPECT_CALL(decoder,
925 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
926 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200927 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000928
ossu61a208b2016-09-20 01:38:00 -0700929 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000930 .Times(0);
931
ossu61a208b2016-09-20 01:38:00 -0700932 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
933 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000934 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700935 .WillOnce(DoAll(
936 SetArrayArgument<3>(dummy_output,
937 dummy_output + kPayloadLengthSamples * kChannels),
938 SetArgPointee<4>(AudioDecoder::kSpeech),
939 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000940
ossu61a208b2016-09-20 01:38:00 -0700941 EXPECT_CALL(decoder,
942 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
943 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200944 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700945
946 EXPECT_CALL(decoder, SampleRateHz())
947 .WillRepeatedly(Return(kSampleRateHz));
948
949 EXPECT_CALL(decoder, Channels())
950 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000951
Niels Möllera1eb9c72018-12-07 15:24:42 +0100952 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
953 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000954
955 // Insert one packet.
956 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
957 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700958 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000959
960 // Insert another packet.
961 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700962 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000963 // The second timestamp needs to be at least 30 ms after the first to make
964 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700965 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000966 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700967 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000968
henrik.lundin6d8e0112016-03-04 10:34:21 -0800969 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700970 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800971 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200972 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700973 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800974 // Output size and number of channels should be correct.
975 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
976 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
977 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000978
henrik.lundin6d8e0112016-03-04 10:34:21 -0800979 // Second call to GetAudio will decode the packet that is ok. No errors are
980 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700981 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800982 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
983 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700984
985 // Die isn't called through NiceMock (since it's called by the
986 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
987 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000988}
989
henrik.lundin116c84e2015-08-27 13:14:48 -0700990// This test inserts packets until the buffer is flushed. After that, it asks
991// NetEq for the network statistics. The purpose of the test is to make sure
992// that even though the buffer size increment is negative (which it becomes when
993// the packet causing a flush is inserted), the packet length stored in the
994// decision logic remains valid.
995TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
996 UseNoMocks();
997 CreateInstance();
998
999 const size_t kPayloadLengthSamples = 80;
1000 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1001 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1002 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1003 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001004 RTPHeader rtp_header;
1005 rtp_header.payloadType = kPayloadType;
1006 rtp_header.sequenceNumber = 0x1234;
1007 rtp_header.timestamp = 0x12345678;
1008 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001009
Niels Möller05543682019-01-10 16:55:06 +01001010 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1011 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001012
1013 // Insert packets until the buffer flushes.
1014 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1015 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1016 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001017 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1018 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1019 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001020 }
1021 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1022
1023 // Ask for network statistics. This should not crash.
1024 NetEqNetworkStatistics stats;
1025 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1026}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001027
1028TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1029 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001030 // Create a mock decoder object.
1031 MockAudioDecoder mock_decoder;
1032
1033 CreateInstance(
1034 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001035
1036 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1037 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1038 const int kSampleRateHz = 8000;
1039 const size_t kPayloadLengthSamples =
1040 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1041 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1042 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001043 RTPHeader rtp_header;
1044 rtp_header.payloadType = kPayloadType;
1045 rtp_header.sequenceNumber = 0x1234;
1046 rtp_header.timestamp = 0x12345678;
1047 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001048
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001049 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001050 EXPECT_CALL(mock_decoder, SampleRateHz())
1051 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001052 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1053 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1054 .WillRepeatedly(Return(0));
1055 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001056 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001057 int16_t dummy_output[kPayloadLengthSamples] = {0};
1058 // The below expectation will make the mock decoder write
1059 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1060 // speech. That is, the decoded length is 5 samples shorter than the expected.
1061 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001062 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001063 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001064 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001065 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001066 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001067 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001068 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1069 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001070
1071 // Insert one packet.
1072 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001073 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001074
1075 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1076
1077 // Pull audio once.
1078 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001079 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001080 bool muted;
1081 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001082 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1083 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001084 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001085
1086 EXPECT_CALL(mock_decoder, Die());
1087}
minyuel6d92bf52015-09-23 15:20:39 +02001088
1089// This test checks the behavior of NetEq when audio decoder fails.
1090TEST_F(NetEqImplTest, DecodingError) {
1091 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001092 // Create a mock decoder object.
1093 MockAudioDecoder mock_decoder;
1094
1095 CreateInstance(
1096 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001097
1098 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1099 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1100 const int kSampleRateHz = 8000;
1101 const int kDecoderErrorCode = -97; // Any negative number.
1102
1103 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1104 const size_t kFrameLengthSamples =
1105 static_cast<size_t>(5 * kSampleRateHz / 1000);
1106
1107 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1108
1109 uint8_t payload[kPayloadLengthBytes] = {0};
1110
henrik.lundin246ef3e2017-04-24 09:14:32 -07001111 RTPHeader rtp_header;
1112 rtp_header.payloadType = kPayloadType;
1113 rtp_header.sequenceNumber = 0x1234;
1114 rtp_header.timestamp = 0x12345678;
1115 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001116
minyuel6d92bf52015-09-23 15:20:39 +02001117 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001118 EXPECT_CALL(mock_decoder, SampleRateHz())
1119 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001120 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1121 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1122 .WillRepeatedly(Return(0));
1123 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001124 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001125 EXPECT_CALL(mock_decoder, ErrorCode())
1126 .WillOnce(Return(kDecoderErrorCode));
1127 EXPECT_CALL(mock_decoder, HasDecodePlc())
1128 .WillOnce(Return(false));
1129 int16_t dummy_output[kFrameLengthSamples] = {0};
1130
1131 {
1132 InSequence sequence; // Dummy variable.
1133 // Mock decoder works normally the first time.
1134 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001135 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001136 .Times(3)
1137 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001138 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001139 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001140 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001141 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001142 .RetiresOnSaturation();
1143
1144 // Then mock decoder fails. A common reason for failure can be buffer being
1145 // too short
1146 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001147 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001148 .WillOnce(Return(-1))
1149 .RetiresOnSaturation();
1150
1151 // Mock decoder finally returns to normal.
1152 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001153 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001154 .Times(2)
1155 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001156 DoAll(SetArrayArgument<3>(dummy_output,
1157 dummy_output + kFrameLengthSamples),
1158 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001159 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001160 }
1161
Niels Möllera1eb9c72018-12-07 15:24:42 +01001162 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1163 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001164
1165 // Insert packets.
1166 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001167 rtp_header.sequenceNumber += 1;
1168 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001169 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001170 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001171 }
1172
1173 // Pull audio.
1174 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001175 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001176 bool muted;
1177 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001178 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1179 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001180 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001181
1182 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001183 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001184 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1185 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001186 // We are not expecting anything for output.speech_type_, since an error was
1187 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001188
1189 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001190 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001191 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1192 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001193 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001194
1195 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001196 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001197 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1198 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001199 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001200
1201 EXPECT_CALL(mock_decoder, Die());
1202}
1203
1204// This test checks the behavior of NetEq when audio decoder fails during CNG.
1205TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1206 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001207
1208 // Create a mock decoder object.
1209 MockAudioDecoder mock_decoder;
1210 CreateInstance(
1211 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001212
1213 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1214 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1215 const int kSampleRateHz = 8000;
1216 const int kDecoderErrorCode = -97; // Any negative number.
1217
1218 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1219 const size_t kFrameLengthSamples =
1220 static_cast<size_t>(5 * kSampleRateHz / 1000);
1221
1222 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1223
1224 uint8_t payload[kPayloadLengthBytes] = {0};
1225
henrik.lundin246ef3e2017-04-24 09:14:32 -07001226 RTPHeader rtp_header;
1227 rtp_header.payloadType = kPayloadType;
1228 rtp_header.sequenceNumber = 0x1234;
1229 rtp_header.timestamp = 0x12345678;
1230 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001231
minyuel6d92bf52015-09-23 15:20:39 +02001232 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001233 EXPECT_CALL(mock_decoder, SampleRateHz())
1234 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001235 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1236 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1237 .WillRepeatedly(Return(0));
1238 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001239 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001240 EXPECT_CALL(mock_decoder, ErrorCode())
1241 .WillOnce(Return(kDecoderErrorCode));
1242 int16_t dummy_output[kFrameLengthSamples] = {0};
1243
1244 {
1245 InSequence sequence; // Dummy variable.
1246 // Mock decoder works normally the first 2 times.
1247 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001248 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001249 .Times(2)
1250 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001251 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001252 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001253 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001254 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001255 .RetiresOnSaturation();
1256
1257 // Then mock decoder fails. A common reason for failure can be buffer being
1258 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001259 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001260 .WillOnce(Return(-1))
1261 .RetiresOnSaturation();
1262
1263 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001264 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001265 .Times(2)
1266 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001267 DoAll(SetArrayArgument<3>(dummy_output,
1268 dummy_output + kFrameLengthSamples),
1269 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001270 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001271 }
1272
Niels Möller50b66d52018-12-11 14:43:21 +01001273 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1274 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001275
1276 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1277 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001278 rtp_header.sequenceNumber += 1;
1279 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001280 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001281 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001282 }
1283
1284 // Pull audio.
1285 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001286 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001287 bool muted;
1288 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001289 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1290 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001291 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001292
1293 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001294 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001295 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1296 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001297 // We are not expecting anything for output.speech_type_, since an error was
1298 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001299
1300 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001301 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001302 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1303 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001304 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001305
1306 EXPECT_CALL(mock_decoder, Die());
1307}
1308
henrik.lundind89814b2015-11-23 06:49:25 -08001309// Tests that the return value from last_output_sample_rate_hz() is equal to the
1310// configured inital sample rate.
1311TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1312 UseNoMocks();
1313 config_.sample_rate_hz = 48000;
1314 CreateInstance();
1315 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1316}
1317
henrik.lundined497212016-04-25 10:11:38 -07001318TEST_F(NetEqImplTest, TickTimerIncrement) {
1319 UseNoMocks();
1320 CreateInstance();
1321 ASSERT_TRUE(tick_timer_);
1322 EXPECT_EQ(0u, tick_timer_->ticks());
1323 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001324 bool muted;
1325 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001326 EXPECT_EQ(1u, tick_timer_->ticks());
1327}
1328
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001329TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1330 UseNoMocks();
1331 use_mock_delay_manager_ = true;
1332 CreateInstance();
1333
1334 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1335 .WillOnce(Return(true))
1336 .WillOnce(Return(false));
1337
1338 const int delay_ms = 200;
1339
1340 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1341 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1342}
1343
1344TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1345 UseNoMocks();
1346 use_mock_delay_manager_ = true;
1347 CreateInstance();
1348
1349 const int delay_ms = 200;
1350
1351 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1352 .WillOnce(Return(delay_ms));
1353
1354 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1355}
1356
henrik.lundin114c1b32017-04-26 07:47:32 -07001357TEST_F(NetEqImplTest, TargetDelayMs) {
1358 UseNoMocks();
1359 use_mock_delay_manager_ = true;
1360 CreateInstance();
1361 // Let the dummy target delay be 17 packets.
1362 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1363 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1364 .WillOnce(Return(kTargetLevelPacketsQ8));
1365 // Default packet size before any packet has been decoded is 30 ms, so we are
1366 // expecting 17 * 30 = 510 ms target delay.
1367 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1368}
1369
henrik.lundinb8c55b12017-05-10 07:38:01 -07001370TEST_F(NetEqImplTest, InsertEmptyPacket) {
1371 UseNoMocks();
1372 use_mock_delay_manager_ = true;
1373 CreateInstance();
1374
1375 RTPHeader rtp_header;
1376 rtp_header.payloadType = 17;
1377 rtp_header.sequenceNumber = 0x1234;
1378 rtp_header.timestamp = 0x12345678;
1379 rtp_header.ssrc = 0x87654321;
1380
1381 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1382 neteq_->InsertEmptyPacket(rtp_header);
1383}
1384
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001385TEST_F(NetEqImplTest, EnableRtxHandling) {
1386 UseNoMocks();
1387 use_mock_delay_manager_ = true;
1388 config_.enable_rtx_handling = true;
1389 CreateInstance();
1390 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1391 .Times(1)
1392 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1393
1394 const int kPayloadLengthSamples = 80;
1395 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1396 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1397 const uint32_t kReceiveTime = 17;
1398 uint8_t payload[kPayloadLengthBytes] = {0};
1399 RTPHeader rtp_header;
1400 rtp_header.payloadType = kPayloadType;
1401 rtp_header.sequenceNumber = 0x1234;
1402 rtp_header.timestamp = 0x12345678;
1403 rtp_header.ssrc = 0x87654321;
1404
Niels Möller05543682019-01-10 16:55:06 +01001405 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1406 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001407 EXPECT_EQ(NetEq::kOK,
1408 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1409 AudioFrame output;
1410 bool muted;
1411 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1412
1413 // Insert second packet that was sent before the first packet.
1414 rtp_header.sequenceNumber -= 1;
1415 rtp_header.timestamp -= kPayloadLengthSamples;
1416 EXPECT_CALL(*mock_delay_manager_,
1417 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1418 EXPECT_EQ(NetEq::kOK,
1419 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1420}
1421
minyue5bd33972016-05-02 04:46:11 -07001422class Decoder120ms : public AudioDecoder {
1423 public:
kwiberg347d3512016-06-16 01:59:09 -07001424 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1425 : sample_rate_hz_(sample_rate_hz),
1426 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001427 speech_type_(speech_type) {}
1428
1429 int DecodeInternal(const uint8_t* encoded,
1430 size_t encoded_len,
1431 int sample_rate_hz,
1432 int16_t* decoded,
1433 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001434 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001435 size_t decoded_len =
1436 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1437 for (size_t i = 0; i < decoded_len; ++i) {
1438 decoded[i] = next_value_++;
1439 }
1440 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001441 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001442 }
1443
1444 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001445 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001446 size_t Channels() const override { return 2; }
1447
1448 private:
kwiberg347d3512016-06-16 01:59:09 -07001449 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001450 int16_t next_value_;
1451 SpeechType speech_type_;
1452};
1453
1454class NetEqImplTest120ms : public NetEqImplTest {
1455 protected:
1456 NetEqImplTest120ms() : NetEqImplTest() {}
1457 virtual ~NetEqImplTest120ms() {}
1458
1459 void CreateInstanceNoMocks() {
1460 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001461 CreateInstance(decoder_factory_);
1462 EXPECT_TRUE(neteq_->RegisterPayloadType(
1463 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001464 }
1465
1466 void CreateInstanceWithDelayManagerMock() {
1467 UseNoMocks();
1468 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001469 CreateInstance(decoder_factory_);
1470 EXPECT_TRUE(neteq_->RegisterPayloadType(
1471 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001472 }
1473
1474 uint32_t timestamp_diff_between_packets() const {
1475 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1476 }
1477
1478 uint32_t first_timestamp() const { return 10u; }
1479
1480 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001481 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001482 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001483 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1484 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001485 }
1486 }
1487
1488 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001489 RTPHeader rtp_header;
1490 rtp_header.payloadType = kPayloadType;
1491 rtp_header.sequenceNumber = sequence_number_;
1492 rtp_header.timestamp = timestamp;
1493 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001494 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1495 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001496 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001497 sequence_number_++;
1498 }
1499
1500 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001501 const uint32_t sampling_freq = kSamplingFreq_;
1502 decoder_factory_ =
1503 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1504 [sampling_freq, speech_type]() {
1505 std::unique_ptr<AudioDecoder> decoder =
1506 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1507 RTC_CHECK_EQ(2, decoder->Channels());
1508 return decoder;
1509 });
minyue5bd33972016-05-02 04:46:11 -07001510 }
1511
Niels Möllera0f44302018-11-30 10:45:12 +01001512 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001513 AudioFrame output_;
1514 const uint32_t kPayloadType = 17;
1515 const uint32_t kSamplingFreq_ = 48000;
1516 uint16_t sequence_number_ = 1;
1517};
1518
minyue5bd33972016-05-02 04:46:11 -07001519TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001520 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001521 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001522
1523 InsertPacket(first_timestamp());
1524 GetFirstPacket();
1525
henrik.lundin7a926812016-05-12 13:51:28 -07001526 bool muted;
1527 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001528 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1529}
1530
1531TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001532 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001533 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001534
1535 InsertPacket(first_timestamp());
1536 GetFirstPacket();
1537
1538 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1539}
1540
1541TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001542 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001543 CreateInstanceWithDelayManagerMock();
1544
minyue5bd33972016-05-02 04:46:11 -07001545 InsertPacket(first_timestamp());
1546
1547 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001548 bool muted;
1549 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001550
1551 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1552
1553 // Delay manager reports a target level which should cause a Merge.
1554 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1555
henrik.lundin7a926812016-05-12 13:51:28 -07001556 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001557 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1558}
1559
1560TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001561 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001562 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001563
1564 InsertPacket(first_timestamp());
1565 GetFirstPacket();
1566
henrik.lundin7a926812016-05-12 13:51:28 -07001567 bool muted;
1568 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001569 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1570}
1571
1572TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001573 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001574 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001575
1576 InsertPacket(first_timestamp());
1577 GetFirstPacket();
1578 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1579
1580 // Delay manager report buffer limit which should cause a FastAccelerate.
1581 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1582 .Times(1)
1583 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1584
henrik.lundin7a926812016-05-12 13:51:28 -07001585 bool muted;
1586 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001587 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1588}
1589
1590TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001591 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001592 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001593
1594 InsertPacket(first_timestamp());
1595 GetFirstPacket();
1596
1597 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1598
1599 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1600 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1601 .Times(1)
1602 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1603
henrik.lundin7a926812016-05-12 13:51:28 -07001604 bool muted;
1605 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001606 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1607}
1608
1609TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001610 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001611 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001612
1613 InsertPacket(first_timestamp());
1614 GetFirstPacket();
1615
1616 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1617
1618 // Delay manager report buffer limit which should cause a Accelerate.
1619 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1620 .Times(1)
1621 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1622
henrik.lundin7a926812016-05-12 13:51:28 -07001623 bool muted;
1624 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001625 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1626}
1627
minyuel6d92bf52015-09-23 15:20:39 +02001628}// namespace webrtc