blob: 5875493b61172734258ae938b3c0a1be6f9071b5 [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
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000040using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041using ::testing::Return;
42using ::testing::ReturnNull;
43using ::testing::_;
44using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000045using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046using ::testing::InSequence;
47using ::testing::Invoke;
48using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000049using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000050using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
52namespace webrtc {
53
54// This function is called when inserting a packet list into the mock packet
55// buffer. The purpose is to delete all inserted packets properly, to avoid
56// memory leaks in the test.
57int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070058 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 return PacketBuffer::kOK;
60}
61
62class NetEqImplTest : public ::testing::Test {
63 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070064 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000065
Niels Möllera0f44302018-11-30 10:45:12 +010066 void CreateInstance(
67 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
68 ASSERT_TRUE(decoder_factory);
69 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070070
71 // Get a local pointer to NetEq's TickTimer object.
72 tick_timer_ = deps.tick_timer.get();
73
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000074 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070075 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
76 mock_buffer_level_filter_ = mock.get();
77 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000078 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070079 buffer_level_filter_ = deps.buffer_level_filter.get();
80
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000081 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070082 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
83 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000084 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
85 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000087 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000089
henrik.lundin1d9061e2016-04-26 12:19:34 -070090 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070091 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010092 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070093 mock_delay_peak_detector_ = mock.get();
94 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
95 deps.delay_peak_detector = std::move(mock);
96 }
97 delay_peak_detector_ = deps.delay_peak_detector.get();
98
99 if (use_mock_delay_manager_) {
100 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100101 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
102 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100103 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100104 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700105 mock_delay_manager_ = mock.get();
106 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
107 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
691// This test verifies that NetEq can handle comfort noise and enters/quits codec
692// internal CNG mode properly.
693TEST_F(NetEqImplTest, CodecInternalCng) {
694 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100695 // Create a mock decoder object.
696 MockAudioDecoder mock_decoder;
697 CreateInstance(
698 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000699
700 const uint8_t kPayloadType = 17; // Just an arbitrary number.
701 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
702 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 const size_t kPayloadLengthSamples =
704 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
705 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000706 uint8_t payload[kPayloadLengthBytes] = {0};
707 int16_t dummy_output[kPayloadLengthSamples] = {0};
708
henrik.lundin246ef3e2017-04-24 09:14:32 -0700709 RTPHeader rtp_header;
710 rtp_header.payloadType = kPayloadType;
711 rtp_header.sequenceNumber = 0x1234;
712 rtp_header.timestamp = 0x12345678;
713 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000714
Karl Wiberg43766482015-08-27 15:22:11 +0200715 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700716 EXPECT_CALL(mock_decoder, SampleRateHz())
717 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000718 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000719 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
720 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700721 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200722 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700723 // Packed duration when asking the decoder for more CNG data (without a new
724 // packet).
725 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200726 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000727
728 // Pointee(x) verifies that first byte of the payload equals x, this makes it
729 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100730 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
731 kSampleRateKhz * 1000, _, _))
732 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000733 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100734 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200735 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000736
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100737 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
738 kSampleRateKhz * 1000, _, _))
739 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000740 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100741 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200742 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000743
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100744 EXPECT_CALL(mock_decoder,
745 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
746 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000747 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100748 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200749 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000750
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100751 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
752 kSampleRateKhz * 1000, _, _))
753 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000754 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100755 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200756 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000757
Niels Möller50b66d52018-12-11 14:43:21 +0100758 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
759 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000760
761 // Insert one packet (decoder will return speech).
762 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700763 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000764
765 // Insert second packet (decoder will return CNG).
766 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700767 rtp_header.sequenceNumber++;
768 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000769 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700770 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000771
Peter Kastingdce40cf2015-08-24 14:52:23 -0700772 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800773 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800774 AudioFrame::SpeechType expected_type[8] = {
775 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
776 AudioFrame::kCNG, AudioFrame::kCNG,
777 AudioFrame::kCNG, AudioFrame::kCNG,
778 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000779 };
780 int expected_timestamp_increment[8] = {
781 -1, // will not be used.
782 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700783 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
784 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000785 50 * kSampleRateKhz, 10 * kSampleRateKhz
786 };
787
henrik.lundin7a926812016-05-12 13:51:28 -0700788 bool muted;
789 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200790 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700791 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000792
henrik.lundin0d96ab72016-04-06 12:28:26 -0700793 // Lambda for verifying the timestamps.
794 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200795 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700796 if (expected_timestamp_increment[i] == -1) {
797 // Expect to get an empty timestamp value during CNG and PLC.
798 EXPECT_FALSE(ts) << "i = " << i;
799 } else {
800 ASSERT_TRUE(ts) << "i = " << i;
801 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
802 << "i = " << i;
803 last_timestamp = ts;
804 }
805 };
806
Peter Kastingdce40cf2015-08-24 14:52:23 -0700807 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800808 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
809 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800810 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700811 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700812 SCOPED_TRACE("");
813 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000814 }
815
816 // Insert third packet, which leaves a gap from last packet.
817 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700818 rtp_header.sequenceNumber += 2;
819 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700821 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000822
Peter Kastingdce40cf2015-08-24 14:52:23 -0700823 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800824 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
825 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800826 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700827 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700828 SCOPED_TRACE("");
829 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000830 }
831
832 // Now check the packet buffer, and make sure it is empty.
833 EXPECT_TRUE(packet_buffer_->Empty());
834
835 EXPECT_CALL(mock_decoder, Die());
836}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000837
838TEST_F(NetEqImplTest, UnsupportedDecoder) {
839 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100840 ::testing::NiceMock<MockAudioDecoder> decoder;
841
842 CreateInstance(
843 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700844 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800845 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000846
847 const uint8_t kPayloadType = 17; // Just an arbitrary number.
848 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
849 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000850
Peter Kastingdce40cf2015-08-24 14:52:23 -0700851 const size_t kPayloadLengthSamples =
852 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000853 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700854 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200855 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700856 RTPHeader rtp_header;
857 rtp_header.payloadType = kPayloadType;
858 rtp_header.sequenceNumber = 0x1234;
859 rtp_header.timestamp = 0x12345678;
860 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000861
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000862 const uint8_t kFirstPayloadValue = 1;
863 const uint8_t kSecondPayloadValue = 2;
864
ossu61a208b2016-09-20 01:38:00 -0700865 EXPECT_CALL(decoder,
866 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
867 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200868 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000869
ossu61a208b2016-09-20 01:38:00 -0700870 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000871 .Times(0);
872
ossu61a208b2016-09-20 01:38:00 -0700873 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
874 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000875 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700876 .WillOnce(DoAll(
877 SetArrayArgument<3>(dummy_output,
878 dummy_output + kPayloadLengthSamples * kChannels),
879 SetArgPointee<4>(AudioDecoder::kSpeech),
880 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000881
ossu61a208b2016-09-20 01:38:00 -0700882 EXPECT_CALL(decoder,
883 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
884 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200885 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700886
887 EXPECT_CALL(decoder, SampleRateHz())
888 .WillRepeatedly(Return(kSampleRateHz));
889
890 EXPECT_CALL(decoder, Channels())
891 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000892
Niels Möllera1eb9c72018-12-07 15:24:42 +0100893 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
894 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000895
896 // Insert one packet.
897 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
898 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700899 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000900
901 // Insert another packet.
902 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700903 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000904 // The second timestamp needs to be at least 30 ms after the first to make
905 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700906 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000907 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700908 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000909
henrik.lundin6d8e0112016-03-04 10:34:21 -0800910 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700911 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800912 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200913 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700914 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800915 // Output size and number of channels should be correct.
916 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
917 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
918 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000919
henrik.lundin6d8e0112016-03-04 10:34:21 -0800920 // Second call to GetAudio will decode the packet that is ok. No errors are
921 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700922 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800923 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
924 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700925
926 // Die isn't called through NiceMock (since it's called by the
927 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
928 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000929}
930
henrik.lundin116c84e2015-08-27 13:14:48 -0700931// This test inserts packets until the buffer is flushed. After that, it asks
932// NetEq for the network statistics. The purpose of the test is to make sure
933// that even though the buffer size increment is negative (which it becomes when
934// the packet causing a flush is inserted), the packet length stored in the
935// decision logic remains valid.
936TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
937 UseNoMocks();
938 CreateInstance();
939
940 const size_t kPayloadLengthSamples = 80;
941 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
942 const uint8_t kPayloadType = 17; // Just an arbitrary number.
943 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
944 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700945 RTPHeader rtp_header;
946 rtp_header.payloadType = kPayloadType;
947 rtp_header.sequenceNumber = 0x1234;
948 rtp_header.timestamp = 0x12345678;
949 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700950
Niels Möller05543682019-01-10 16:55:06 +0100951 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
952 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -0700953
954 // Insert packets until the buffer flushes.
955 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
956 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
957 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700958 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
959 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
960 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700961 }
962 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
963
964 // Ask for network statistics. This should not crash.
965 NetEqNetworkStatistics stats;
966 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
967}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200968
969TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
970 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100971 // Create a mock decoder object.
972 MockAudioDecoder mock_decoder;
973
974 CreateInstance(
975 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200976
977 const uint8_t kPayloadType = 17; // Just an arbitrary number.
978 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
979 const int kSampleRateHz = 8000;
980 const size_t kPayloadLengthSamples =
981 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
982 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
983 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700984 RTPHeader rtp_header;
985 rtp_header.payloadType = kPayloadType;
986 rtp_header.sequenceNumber = 0x1234;
987 rtp_header.timestamp = 0x12345678;
988 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200989
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200990 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700991 EXPECT_CALL(mock_decoder, SampleRateHz())
992 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200993 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
994 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
995 .WillRepeatedly(Return(0));
996 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200997 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200998 int16_t dummy_output[kPayloadLengthSamples] = {0};
999 // The below expectation will make the mock decoder write
1000 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1001 // speech. That is, the decoded length is 5 samples shorter than the expected.
1002 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001003 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001004 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001005 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001006 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001007 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001008 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001009 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1010 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001011
1012 // Insert one packet.
1013 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001014 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001015
1016 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1017
1018 // Pull audio once.
1019 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001020 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001021 bool muted;
1022 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001023 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1024 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001025 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001026
1027 EXPECT_CALL(mock_decoder, Die());
1028}
minyuel6d92bf52015-09-23 15:20:39 +02001029
1030// This test checks the behavior of NetEq when audio decoder fails.
1031TEST_F(NetEqImplTest, DecodingError) {
1032 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001033 // Create a mock decoder object.
1034 MockAudioDecoder mock_decoder;
1035
1036 CreateInstance(
1037 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001038
1039 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1040 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1041 const int kSampleRateHz = 8000;
1042 const int kDecoderErrorCode = -97; // Any negative number.
1043
1044 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1045 const size_t kFrameLengthSamples =
1046 static_cast<size_t>(5 * kSampleRateHz / 1000);
1047
1048 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1049
1050 uint8_t payload[kPayloadLengthBytes] = {0};
1051
henrik.lundin246ef3e2017-04-24 09:14:32 -07001052 RTPHeader rtp_header;
1053 rtp_header.payloadType = kPayloadType;
1054 rtp_header.sequenceNumber = 0x1234;
1055 rtp_header.timestamp = 0x12345678;
1056 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001057
minyuel6d92bf52015-09-23 15:20:39 +02001058 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001059 EXPECT_CALL(mock_decoder, SampleRateHz())
1060 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001061 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1062 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1063 .WillRepeatedly(Return(0));
1064 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001065 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001066 EXPECT_CALL(mock_decoder, ErrorCode())
1067 .WillOnce(Return(kDecoderErrorCode));
1068 EXPECT_CALL(mock_decoder, HasDecodePlc())
1069 .WillOnce(Return(false));
1070 int16_t dummy_output[kFrameLengthSamples] = {0};
1071
1072 {
1073 InSequence sequence; // Dummy variable.
1074 // Mock decoder works normally the first time.
1075 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001076 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001077 .Times(3)
1078 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001079 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001080 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001081 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001082 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001083 .RetiresOnSaturation();
1084
1085 // Then mock decoder fails. A common reason for failure can be buffer being
1086 // too short
1087 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001088 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001089 .WillOnce(Return(-1))
1090 .RetiresOnSaturation();
1091
1092 // Mock decoder finally returns to normal.
1093 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001094 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001095 .Times(2)
1096 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001097 DoAll(SetArrayArgument<3>(dummy_output,
1098 dummy_output + kFrameLengthSamples),
1099 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001100 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001101 }
1102
Niels Möllera1eb9c72018-12-07 15:24:42 +01001103 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1104 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001105
1106 // Insert packets.
1107 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001108 rtp_header.sequenceNumber += 1;
1109 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001110 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001111 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001112 }
1113
1114 // Pull audio.
1115 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001116 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001117 bool muted;
1118 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001119 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1120 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001121 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001122
1123 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001124 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001125 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1126 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001127 // We are not expecting anything for output.speech_type_, since an error was
1128 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001129
1130 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001131 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001132 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1133 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001134 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001135
1136 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001137 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001138 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1139 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001140 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001141
1142 EXPECT_CALL(mock_decoder, Die());
1143}
1144
1145// This test checks the behavior of NetEq when audio decoder fails during CNG.
1146TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1147 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001148
1149 // Create a mock decoder object.
1150 MockAudioDecoder mock_decoder;
1151 CreateInstance(
1152 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001153
1154 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1155 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1156 const int kSampleRateHz = 8000;
1157 const int kDecoderErrorCode = -97; // Any negative number.
1158
1159 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1160 const size_t kFrameLengthSamples =
1161 static_cast<size_t>(5 * kSampleRateHz / 1000);
1162
1163 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1164
1165 uint8_t payload[kPayloadLengthBytes] = {0};
1166
henrik.lundin246ef3e2017-04-24 09:14:32 -07001167 RTPHeader rtp_header;
1168 rtp_header.payloadType = kPayloadType;
1169 rtp_header.sequenceNumber = 0x1234;
1170 rtp_header.timestamp = 0x12345678;
1171 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001172
minyuel6d92bf52015-09-23 15:20:39 +02001173 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001174 EXPECT_CALL(mock_decoder, SampleRateHz())
1175 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001176 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1177 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1178 .WillRepeatedly(Return(0));
1179 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001180 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001181 EXPECT_CALL(mock_decoder, ErrorCode())
1182 .WillOnce(Return(kDecoderErrorCode));
1183 int16_t dummy_output[kFrameLengthSamples] = {0};
1184
1185 {
1186 InSequence sequence; // Dummy variable.
1187 // Mock decoder works normally the first 2 times.
1188 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001189 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001190 .Times(2)
1191 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001192 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001193 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001194 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001195 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001196 .RetiresOnSaturation();
1197
1198 // Then mock decoder fails. A common reason for failure can be buffer being
1199 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001200 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001201 .WillOnce(Return(-1))
1202 .RetiresOnSaturation();
1203
1204 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001205 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001206 .Times(2)
1207 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001208 DoAll(SetArrayArgument<3>(dummy_output,
1209 dummy_output + kFrameLengthSamples),
1210 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001211 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001212 }
1213
Niels Möller50b66d52018-12-11 14:43:21 +01001214 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1215 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001216
1217 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1218 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001219 rtp_header.sequenceNumber += 1;
1220 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001221 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001223 }
1224
1225 // Pull audio.
1226 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001227 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001228 bool muted;
1229 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001230 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1231 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001232 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001233
1234 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001235 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1237 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 // We are not expecting anything for output.speech_type_, since an error was
1239 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001240
1241 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001242 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001243 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1244 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001245 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001246
1247 EXPECT_CALL(mock_decoder, Die());
1248}
1249
henrik.lundind89814b2015-11-23 06:49:25 -08001250// Tests that the return value from last_output_sample_rate_hz() is equal to the
1251// configured inital sample rate.
1252TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1253 UseNoMocks();
1254 config_.sample_rate_hz = 48000;
1255 CreateInstance();
1256 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1257}
1258
henrik.lundined497212016-04-25 10:11:38 -07001259TEST_F(NetEqImplTest, TickTimerIncrement) {
1260 UseNoMocks();
1261 CreateInstance();
1262 ASSERT_TRUE(tick_timer_);
1263 EXPECT_EQ(0u, tick_timer_->ticks());
1264 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001265 bool muted;
1266 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001267 EXPECT_EQ(1u, tick_timer_->ticks());
1268}
1269
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001270TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1271 UseNoMocks();
1272 use_mock_delay_manager_ = true;
1273 CreateInstance();
1274
1275 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1276 .WillOnce(Return(true))
1277 .WillOnce(Return(false));
1278
1279 const int delay_ms = 200;
1280
1281 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1282 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1283}
1284
1285TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1286 UseNoMocks();
1287 use_mock_delay_manager_ = true;
1288 CreateInstance();
1289
1290 const int delay_ms = 200;
1291
1292 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1293 .WillOnce(Return(delay_ms));
1294
1295 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1296}
1297
henrik.lundin114c1b32017-04-26 07:47:32 -07001298TEST_F(NetEqImplTest, TargetDelayMs) {
1299 UseNoMocks();
1300 use_mock_delay_manager_ = true;
1301 CreateInstance();
1302 // Let the dummy target delay be 17 packets.
1303 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1304 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1305 .WillOnce(Return(kTargetLevelPacketsQ8));
1306 // Default packet size before any packet has been decoded is 30 ms, so we are
1307 // expecting 17 * 30 = 510 ms target delay.
1308 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1309}
1310
henrik.lundinb8c55b12017-05-10 07:38:01 -07001311TEST_F(NetEqImplTest, InsertEmptyPacket) {
1312 UseNoMocks();
1313 use_mock_delay_manager_ = true;
1314 CreateInstance();
1315
1316 RTPHeader rtp_header;
1317 rtp_header.payloadType = 17;
1318 rtp_header.sequenceNumber = 0x1234;
1319 rtp_header.timestamp = 0x12345678;
1320 rtp_header.ssrc = 0x87654321;
1321
1322 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1323 neteq_->InsertEmptyPacket(rtp_header);
1324}
1325
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001326TEST_F(NetEqImplTest, EnableRtxHandling) {
1327 UseNoMocks();
1328 use_mock_delay_manager_ = true;
1329 config_.enable_rtx_handling = true;
1330 CreateInstance();
1331 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1332 .Times(1)
1333 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1334
1335 const int kPayloadLengthSamples = 80;
1336 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1337 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1338 const uint32_t kReceiveTime = 17;
1339 uint8_t payload[kPayloadLengthBytes] = {0};
1340 RTPHeader rtp_header;
1341 rtp_header.payloadType = kPayloadType;
1342 rtp_header.sequenceNumber = 0x1234;
1343 rtp_header.timestamp = 0x12345678;
1344 rtp_header.ssrc = 0x87654321;
1345
Niels Möller05543682019-01-10 16:55:06 +01001346 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1347 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001348 EXPECT_EQ(NetEq::kOK,
1349 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1350 AudioFrame output;
1351 bool muted;
1352 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1353
1354 // Insert second packet that was sent before the first packet.
1355 rtp_header.sequenceNumber -= 1;
1356 rtp_header.timestamp -= kPayloadLengthSamples;
1357 EXPECT_CALL(*mock_delay_manager_,
1358 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1359 EXPECT_EQ(NetEq::kOK,
1360 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1361}
1362
minyue5bd33972016-05-02 04:46:11 -07001363class Decoder120ms : public AudioDecoder {
1364 public:
kwiberg347d3512016-06-16 01:59:09 -07001365 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1366 : sample_rate_hz_(sample_rate_hz),
1367 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001368 speech_type_(speech_type) {}
1369
1370 int DecodeInternal(const uint8_t* encoded,
1371 size_t encoded_len,
1372 int sample_rate_hz,
1373 int16_t* decoded,
1374 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001375 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001376 size_t decoded_len =
1377 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1378 for (size_t i = 0; i < decoded_len; ++i) {
1379 decoded[i] = next_value_++;
1380 }
1381 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001382 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001383 }
1384
1385 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001386 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001387 size_t Channels() const override { return 2; }
1388
1389 private:
kwiberg347d3512016-06-16 01:59:09 -07001390 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001391 int16_t next_value_;
1392 SpeechType speech_type_;
1393};
1394
1395class NetEqImplTest120ms : public NetEqImplTest {
1396 protected:
1397 NetEqImplTest120ms() : NetEqImplTest() {}
1398 virtual ~NetEqImplTest120ms() {}
1399
1400 void CreateInstanceNoMocks() {
1401 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001402 CreateInstance(decoder_factory_);
1403 EXPECT_TRUE(neteq_->RegisterPayloadType(
1404 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001405 }
1406
1407 void CreateInstanceWithDelayManagerMock() {
1408 UseNoMocks();
1409 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001410 CreateInstance(decoder_factory_);
1411 EXPECT_TRUE(neteq_->RegisterPayloadType(
1412 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001413 }
1414
1415 uint32_t timestamp_diff_between_packets() const {
1416 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1417 }
1418
1419 uint32_t first_timestamp() const { return 10u; }
1420
1421 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001422 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001423 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001424 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1425 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001426 }
1427 }
1428
1429 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001430 RTPHeader rtp_header;
1431 rtp_header.payloadType = kPayloadType;
1432 rtp_header.sequenceNumber = sequence_number_;
1433 rtp_header.timestamp = timestamp;
1434 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001435 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1436 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001437 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001438 sequence_number_++;
1439 }
1440
1441 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001442 const uint32_t sampling_freq = kSamplingFreq_;
1443 decoder_factory_ =
1444 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1445 [sampling_freq, speech_type]() {
1446 std::unique_ptr<AudioDecoder> decoder =
1447 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1448 RTC_CHECK_EQ(2, decoder->Channels());
1449 return decoder;
1450 });
minyue5bd33972016-05-02 04:46:11 -07001451 }
1452
Niels Möllera0f44302018-11-30 10:45:12 +01001453 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001454 AudioFrame output_;
1455 const uint32_t kPayloadType = 17;
1456 const uint32_t kSamplingFreq_ = 48000;
1457 uint16_t sequence_number_ = 1;
1458};
1459
minyue5bd33972016-05-02 04:46:11 -07001460TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001461 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001462 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001463
1464 InsertPacket(first_timestamp());
1465 GetFirstPacket();
1466
henrik.lundin7a926812016-05-12 13:51:28 -07001467 bool muted;
1468 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001469 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1470}
1471
1472TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001473 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001474 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001475
1476 InsertPacket(first_timestamp());
1477 GetFirstPacket();
1478
1479 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1480}
1481
1482TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001483 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001484 CreateInstanceWithDelayManagerMock();
1485
minyue5bd33972016-05-02 04:46:11 -07001486 InsertPacket(first_timestamp());
1487
1488 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001489 bool muted;
1490 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001491
1492 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1493
1494 // Delay manager reports a target level which should cause a Merge.
1495 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1496
henrik.lundin7a926812016-05-12 13:51:28 -07001497 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001498 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1499}
1500
1501TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001502 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001503 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001504
1505 InsertPacket(first_timestamp());
1506 GetFirstPacket();
1507
henrik.lundin7a926812016-05-12 13:51:28 -07001508 bool muted;
1509 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001510 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1511}
1512
1513TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001514 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001515 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001516
1517 InsertPacket(first_timestamp());
1518 GetFirstPacket();
1519 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1520
1521 // Delay manager report buffer limit which should cause a FastAccelerate.
1522 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1523 .Times(1)
1524 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
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(kFastAccelerate, neteq_->last_operation_for_test());
1529}
1530
1531TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001532 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001533 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001534
1535 InsertPacket(first_timestamp());
1536 GetFirstPacket();
1537
1538 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1539
1540 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1541 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1542 .Times(1)
1543 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1544
henrik.lundin7a926812016-05-12 13:51:28 -07001545 bool muted;
1546 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001547 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1548}
1549
1550TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001551 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001552 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001553
1554 InsertPacket(first_timestamp());
1555 GetFirstPacket();
1556
1557 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1558
1559 // Delay manager report buffer limit which should cause a Accelerate.
1560 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1561 .Times(1)
1562 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1563
henrik.lundin7a926812016-05-12 13:51:28 -07001564 bool muted;
1565 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001566 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1567}
1568
minyuel6d92bf52015-09-23 15:20:39 +02001569}// namespace webrtc