blob: 86fbe9c75c3d474830d9de7b0a20c76f52f0411e [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"
29#include "modules/audio_coding/neteq/sync_buffer.h"
30#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010031#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010032#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010033#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "test/gmock.h"
35#include "test/gtest.h"
36#include "test/mock_audio_decoder.h"
37#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000039using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040using ::testing::Return;
41using ::testing::ReturnNull;
42using ::testing::_;
43using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000044using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045using ::testing::InSequence;
46using ::testing::Invoke;
47using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000048using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000049using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
51namespace webrtc {
52
53// This function is called when inserting a packet list into the mock packet
54// buffer. The purpose is to delete all inserted packets properly, to avoid
55// memory leaks in the test.
56int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070057 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058 return PacketBuffer::kOK;
59}
60
61class NetEqImplTest : public ::testing::Test {
62 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070063 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000064
Niels Möllera0f44302018-11-30 10:45:12 +010065 void CreateInstance(
66 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
67 ASSERT_TRUE(decoder_factory);
68 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070069
70 // Get a local pointer to NetEq's TickTimer object.
71 tick_timer_ = deps.tick_timer.get();
72
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000073 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070074 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
75 mock_buffer_level_filter_ = mock.get();
76 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000077 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070078 buffer_level_filter_ = deps.buffer_level_filter.get();
79
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000080 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070081 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
82 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000083 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
84 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070085 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000086 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000088
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070090 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010091 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070092 mock_delay_peak_detector_ = mock.get();
93 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
94 deps.delay_peak_detector = std::move(mock);
95 }
96 delay_peak_detector_ = deps.delay_peak_detector.get();
97
98 if (use_mock_delay_manager_) {
99 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100100 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
101 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100102 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
103 absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700104 mock_delay_manager_ = mock.get();
105 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
106 deps.delay_manager = std::move(mock);
107 }
108 delay_manager_ = deps.delay_manager.get();
109
110 if (use_mock_dtmf_buffer_) {
111 std::unique_ptr<MockDtmfBuffer> mock(
112 new MockDtmfBuffer(config_.sample_rate_hz));
113 mock_dtmf_buffer_ = mock.get();
114 deps.dtmf_buffer = std::move(mock);
115 }
116 dtmf_buffer_ = deps.dtmf_buffer.get();
117
118 if (use_mock_dtmf_tone_generator_) {
119 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
120 mock_dtmf_tone_generator_ = mock.get();
121 deps.dtmf_tone_generator = std::move(mock);
122 }
123 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
124
125 if (use_mock_packet_buffer_) {
126 std::unique_ptr<MockPacketBuffer> mock(
127 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
128 mock_packet_buffer_ = mock.get();
129 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700130 }
131 packet_buffer_ = deps.packet_buffer.get();
132
133 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700134 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700135 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700136 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137 }
ossua70695a2016-09-22 02:06:28 -0700138 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700139
140 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
141 new TimestampScaler(*deps.decoder_database.get()));
142
143 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000144 ASSERT_TRUE(neteq_ != NULL);
145 }
146
Niels Möllera0f44302018-11-30 10:45:12 +0100147 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
148
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000149 void UseNoMocks() {
150 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
151 use_mock_buffer_level_filter_ = false;
152 use_mock_decoder_database_ = false;
153 use_mock_delay_peak_detector_ = false;
154 use_mock_delay_manager_ = false;
155 use_mock_dtmf_buffer_ = false;
156 use_mock_dtmf_tone_generator_ = false;
157 use_mock_packet_buffer_ = false;
158 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 }
160
161 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000162 if (use_mock_buffer_level_filter_) {
163 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
164 }
165 if (use_mock_decoder_database_) {
166 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
167 }
168 if (use_mock_delay_manager_) {
169 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
170 }
171 if (use_mock_delay_peak_detector_) {
172 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
173 }
174 if (use_mock_dtmf_buffer_) {
175 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
176 }
177 if (use_mock_dtmf_tone_generator_) {
178 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
179 }
180 if (use_mock_packet_buffer_) {
181 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
182 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000183 }
184
Niels Möller05543682019-01-10 16:55:06 +0100185 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800186 const size_t kPayloadLength = 4;
187 const uint8_t kPayloadType = 110;
188 const uint32_t kReceiveTime = 17;
189 const int kSampleRateHz = 16000;
190 config_.sample_rate_hz = kSampleRateHz;
191 UseNoMocks();
192 CreateInstance();
193 // Event: 2, E bit, Volume: 17, Length: 4336.
194 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700195 RTPHeader rtp_header;
196 rtp_header.payloadType = kPayloadType;
197 rtp_header.sequenceNumber = 0x1234;
198 rtp_header.timestamp = 0x12345678;
199 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800200
Niels Möller05543682019-01-10 16:55:06 +0100201 EXPECT_TRUE(neteq_->RegisterPayloadType(
202 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800203
204 // Insert first packet.
205 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700206 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800207
208 // Pull audio once.
209 const size_t kMaxOutputSize =
210 static_cast<size_t>(10 * kSampleRateHz / 1000);
211 AudioFrame output;
212 bool muted;
213 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
214 ASSERT_FALSE(muted);
215 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
216 EXPECT_EQ(1u, output.num_channels_);
217 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
218
219 // Verify first 64 samples of actual output.
220 const std::vector<int16_t> kOutput({
221 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
222 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
223 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
224 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
225 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
226 -2534, -1163 });
227 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700228 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800229 }
230
henrik.lundin1d9061e2016-04-26 12:19:34 -0700231 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000232 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700233 TickTimer* tick_timer_ = nullptr;
234 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
235 BufferLevelFilter* buffer_level_filter_ = nullptr;
236 bool use_mock_buffer_level_filter_ = true;
237 MockDecoderDatabase* mock_decoder_database_ = nullptr;
238 DecoderDatabase* decoder_database_ = nullptr;
239 bool use_mock_decoder_database_ = true;
240 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
241 DelayPeakDetector* delay_peak_detector_ = nullptr;
242 bool use_mock_delay_peak_detector_ = true;
243 MockDelayManager* mock_delay_manager_ = nullptr;
244 DelayManager* delay_manager_ = nullptr;
245 bool use_mock_delay_manager_ = true;
246 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
247 DtmfBuffer* dtmf_buffer_ = nullptr;
248 bool use_mock_dtmf_buffer_ = true;
249 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
250 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
251 bool use_mock_dtmf_tone_generator_ = true;
252 MockPacketBuffer* mock_packet_buffer_ = nullptr;
253 PacketBuffer* packet_buffer_ = nullptr;
254 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700255 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
256 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700257 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258};
259
260
261// This tests the interface class NetEq.
262// TODO(hlundin): Move to separate file?
263TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000264 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700265 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 delete neteq;
267}
268
kwiberg5adaf732016-10-04 09:33:27 -0700269TEST_F(NetEqImplTest, RegisterPayloadType) {
270 CreateInstance();
271 constexpr int rtp_payload_type = 0;
272 const SdpAudioFormat format("pcmu", 8000, 1);
273 EXPECT_CALL(*mock_decoder_database_,
274 RegisterPayload(rtp_payload_type, format));
275 neteq_->RegisterPayloadType(rtp_payload_type, format);
276}
277
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000279 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000281 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200283 // Check that kOK is returned when database returns kDecoderNotFound, because
284 // removing a payload type that was never registered is not an error.
285 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286}
287
kwiberg6b19b562016-09-20 04:02:25 -0700288TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
289 CreateInstance();
290 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
291 neteq_->RemoveAllPayloadTypes();
292}
293
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000295 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000296 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 const uint8_t kPayloadType = 0;
298 const uint16_t kFirstSequenceNumber = 0x1234;
299 const uint32_t kFirstTimestamp = 0x12345678;
300 const uint32_t kSsrc = 0x87654321;
301 const uint32_t kFirstReceiveTime = 17;
302 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700303 RTPHeader rtp_header;
304 rtp_header.payloadType = kPayloadType;
305 rtp_header.sequenceNumber = kFirstSequenceNumber;
306 rtp_header.timestamp = kFirstTimestamp;
307 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700308 Packet fake_packet;
309 fake_packet.payload_type = kPayloadType;
310 fake_packet.sequence_number = kFirstSequenceNumber;
311 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312
kwibergc0f2dcf2016-05-31 06:28:03 -0700313 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
314 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100315 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800316 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200317 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800318 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700319 EXPECT_EQ("pcmu", format.name);
320
321 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
322 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
323 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
324 // BWE update function called with first packet.
325 EXPECT_CALL(*mock_decoder,
326 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
327 kFirstTimestamp, kFirstReceiveTime));
328 // BWE update function called with second packet.
329 EXPECT_CALL(
330 *mock_decoder,
331 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
332 kFirstTimestamp + 160, kFirstReceiveTime + 155));
333 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
334
335 *dec = std::move(mock_decoder);
336 }));
Niels Möller72899062019-01-11 09:36:13 +0100337 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
338 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339
340 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000341 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342 .WillRepeatedly(Return(&info));
343
344 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000345 EXPECT_CALL(*mock_packet_buffer_, Empty())
346 .WillOnce(Return(false)); // Called once after first packet is inserted.
347 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200349 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100351 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
352 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
354 // index) is a pointer, and the variable pointed to is set to kPayloadType.
355 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
356 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700357 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000358 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700359 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360
361 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000362 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 .Times(1);
364
365 // Expectations for delay manager.
366 {
367 // All expectations within this block must be called in this specific order.
368 InSequence sequence; // Dummy variable.
369 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000370 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 .Times(2)
372 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000373 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000375 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
378 .WillOnce(Return(0));
379 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 .WillOnce(Return(0));
381 }
382
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700384 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385
386 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700387 rtp_header.timestamp += 160;
388 rtp_header.sequenceNumber += 1;
389 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390}
391
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000392TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
393 UseNoMocks();
394 CreateInstance();
395
396 const int kPayloadLengthSamples = 80;
397 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
398 const uint8_t kPayloadType = 17; // Just an arbitrary number.
399 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
400 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700401 RTPHeader rtp_header;
402 rtp_header.payloadType = kPayloadType;
403 rtp_header.sequenceNumber = 0x1234;
404 rtp_header.timestamp = 0x12345678;
405 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000406
Niels Möller05543682019-01-10 16:55:06 +0100407 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
408 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000409
410 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700411 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000412 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700413 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
414 rtp_header.timestamp += kPayloadLengthSamples;
415 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000416 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
417 }
418
419 // Insert one more packet and make sure the buffer got flushed. That is, it
420 // should only hold one single packet.
421 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700422 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700423 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700424 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700425 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
426 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000427}
428
solenberg2779bab2016-11-17 04:45:19 -0800429TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100430 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800431}
solenberg99df6c02016-10-11 04:35:34 -0700432
solenberg2779bab2016-11-17 04:45:19 -0800433TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100434 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800435}
solenberg99df6c02016-10-11 04:35:34 -0700436
solenberg2779bab2016-11-17 04:45:19 -0800437TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100438 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800439}
solenberg99df6c02016-10-11 04:35:34 -0700440
solenberg2779bab2016-11-17 04:45:19 -0800441TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100442 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700443}
444
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000445// This test verifies that timestamps propagate from the incoming packets
446// through to the sync buffer and to the playout timestamp.
447TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000448 const uint8_t kPayloadType = 17; // Just an arbitrary number.
449 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
450 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700451 const size_t kPayloadLengthSamples =
452 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000453 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
454 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700455 RTPHeader rtp_header;
456 rtp_header.payloadType = kPayloadType;
457 rtp_header.sequenceNumber = 0x1234;
458 rtp_header.timestamp = 0x12345678;
459 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000460
461 // This is a dummy decoder that produces as many output samples as the input
462 // has bytes. The output is an increasing series, starting at 1 for the first
463 // sample, and then increasing by 1 for each sample.
464 class CountingSamplesDecoder : public AudioDecoder {
465 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000466 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000467
468 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100469 int DecodeInternal(const uint8_t* encoded,
470 size_t encoded_len,
471 int /* sample_rate_hz */,
472 int16_t* decoded,
473 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000474 for (size_t i = 0; i < encoded_len; ++i) {
475 decoded[i] = next_value_++;
476 }
477 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200478 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000479 }
480
Karl Wiberg43766482015-08-27 15:22:11 +0200481 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482
kwiberg347d3512016-06-16 01:59:09 -0700483 int SampleRateHz() const override { return kSampleRateHz; }
484
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000485 size_t Channels() const override { return 1; }
486
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000487 uint16_t next_value() const { return next_value_; }
488
489 private:
490 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000491 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000492
Niels Möllerb7180c02018-12-06 13:07:11 +0100493 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
494 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
495
496 UseNoMocks();
497 CreateInstance(decoder_factory);
498
499 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100500 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000501
502 // Insert one packet.
503 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700504 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000505
506 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700507 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800508 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700509 bool muted;
510 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
511 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800512 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
513 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800514 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000515
516 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700517 EXPECT_EQ(kPayloadLengthSamples,
518 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000519
520 // The value of the last of the output samples is the same as the number of
521 // samples played from the decoded packet. Thus, this number + the RTP
522 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200523 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700524 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200525 absl::optional<uint32_t>(rtp_header.timestamp +
526 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700527 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000528
529 // Check the timestamp for the last value in the sync buffer. This should
530 // be one full frame length ahead of the RTP timestamp.
531 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
532 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700533 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000534 sync_buffer->end_timestamp());
535
536 // Check that the number of samples still to play from the sync buffer add
537 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800538 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700539 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800540 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000541}
542
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000543TEST_F(NetEqImplTest, ReorderedPacket) {
544 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100545 // Create a mock decoder object.
546 MockAudioDecoder mock_decoder;
547
548 CreateInstance(
549 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000550
551 const uint8_t kPayloadType = 17; // Just an arbitrary number.
552 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
553 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700554 const size_t kPayloadLengthSamples =
555 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000556 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
557 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700558 RTPHeader rtp_header;
559 rtp_header.payloadType = kPayloadType;
560 rtp_header.sequenceNumber = 0x1234;
561 rtp_header.timestamp = 0x12345678;
562 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000563
Karl Wiberg43766482015-08-27 15:22:11 +0200564 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700565 EXPECT_CALL(mock_decoder, SampleRateHz())
566 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000567 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000568 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
569 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700570 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200571 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000572 int16_t dummy_output[kPayloadLengthSamples] = {0};
573 // The below expectation will make the mock decoder write
574 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100575 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
576 kSampleRateHz, _, _))
577 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000578 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100579 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200580 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100581 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
582 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000583
584 // Insert one packet.
585 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700586 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000587
588 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700589 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800590 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700591 bool muted;
592 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800593 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
594 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800595 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000596
597 // Insert two more packets. The first one is out of order, and is already too
598 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700599 rtp_header.sequenceNumber -= 1;
600 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000601 payload[0] = 1;
602 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700603 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
604 rtp_header.sequenceNumber += 2;
605 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000606 payload[0] = 2;
607 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700608 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000609
610 // Expect only the second packet to be decoded (the one with "2" as the first
611 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100612 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
613 kSampleRateHz, _, _))
614 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000615 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100616 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200617 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000618
619 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700620 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800621 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
622 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800623 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000624
625 // Now check the packet buffer, and make sure it is empty, since the
626 // out-of-order packet should have been discarded.
627 EXPECT_TRUE(packet_buffer_->Empty());
628
629 EXPECT_CALL(mock_decoder, Die());
630}
631
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000632// This test verifies that NetEq can handle the situation where the first
633// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000634TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000635 UseNoMocks();
636 CreateInstance();
637
638 const uint8_t kPayloadType = 17; // Just an arbitrary number.
639 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
640 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700641 const size_t kPayloadLengthSamples =
642 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700643 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000644 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700645 RTPHeader rtp_header;
646 rtp_header.payloadType = kPayloadType;
647 rtp_header.sequenceNumber = 0x1234;
648 rtp_header.timestamp = 0x12345678;
649 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000650
651 // Insert one packet. Note that we have not registered any payload type, so
652 // this packet will be rejected.
653 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700654 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000655
656 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700657 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800658 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700659 bool muted;
660 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800661 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
662 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
663 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800664 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000665
666 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100667 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
668 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000669
670 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700671 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700672 rtp_header.sequenceNumber++;
673 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000674 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700675 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000676 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
677 }
678
679 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700680 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700681 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800682 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
683 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
684 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800685 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000686 << "NetEq did not decode the packets as expected.";
687 }
688}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000689
690// This test verifies that NetEq can handle comfort noise and enters/quits codec
691// internal CNG mode properly.
692TEST_F(NetEqImplTest, CodecInternalCng) {
693 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100694 // Create a mock decoder object.
695 MockAudioDecoder mock_decoder;
696 CreateInstance(
697 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000698
699 const uint8_t kPayloadType = 17; // Just an arbitrary number.
700 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
701 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700702 const size_t kPayloadLengthSamples =
703 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
704 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000705 uint8_t payload[kPayloadLengthBytes] = {0};
706 int16_t dummy_output[kPayloadLengthSamples] = {0};
707
henrik.lundin246ef3e2017-04-24 09:14:32 -0700708 RTPHeader rtp_header;
709 rtp_header.payloadType = kPayloadType;
710 rtp_header.sequenceNumber = 0x1234;
711 rtp_header.timestamp = 0x12345678;
712 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000713
Karl Wiberg43766482015-08-27 15:22:11 +0200714 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700715 EXPECT_CALL(mock_decoder, SampleRateHz())
716 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000717 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000718 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
719 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700720 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200721 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700722 // Packed duration when asking the decoder for more CNG data (without a new
723 // packet).
724 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200725 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000726
727 // Pointee(x) verifies that first byte of the payload equals x, this makes it
728 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100729 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
730 kSampleRateKhz * 1000, _, _))
731 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000732 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100733 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200734 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000735
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100736 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
737 kSampleRateKhz * 1000, _, _))
738 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000739 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100740 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200741 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000742
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100743 EXPECT_CALL(mock_decoder,
744 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
745 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000746 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100747 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200748 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000749
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100750 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
751 kSampleRateKhz * 1000, _, _))
752 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000753 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100754 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200755 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756
Niels Möller50b66d52018-12-11 14:43:21 +0100757 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
758 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000759
760 // Insert one packet (decoder will return speech).
761 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700762 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000763
764 // Insert second packet (decoder will return CNG).
765 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700766 rtp_header.sequenceNumber++;
767 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000768 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700769 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000770
Peter Kastingdce40cf2015-08-24 14:52:23 -0700771 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800772 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800773 AudioFrame::SpeechType expected_type[8] = {
774 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
775 AudioFrame::kCNG, AudioFrame::kCNG,
776 AudioFrame::kCNG, AudioFrame::kCNG,
777 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000778 };
779 int expected_timestamp_increment[8] = {
780 -1, // will not be used.
781 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700782 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
783 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000784 50 * kSampleRateKhz, 10 * kSampleRateKhz
785 };
786
henrik.lundin7a926812016-05-12 13:51:28 -0700787 bool muted;
788 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200789 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700790 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000791
henrik.lundin0d96ab72016-04-06 12:28:26 -0700792 // Lambda for verifying the timestamps.
793 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200794 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700795 if (expected_timestamp_increment[i] == -1) {
796 // Expect to get an empty timestamp value during CNG and PLC.
797 EXPECT_FALSE(ts) << "i = " << i;
798 } else {
799 ASSERT_TRUE(ts) << "i = " << i;
800 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
801 << "i = " << i;
802 last_timestamp = ts;
803 }
804 };
805
Peter Kastingdce40cf2015-08-24 14:52:23 -0700806 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800807 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
808 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800809 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700810 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700811 SCOPED_TRACE("");
812 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000813 }
814
815 // Insert third packet, which leaves a gap from last packet.
816 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700817 rtp_header.sequenceNumber += 2;
818 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000819 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700820 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000821
Peter Kastingdce40cf2015-08-24 14:52:23 -0700822 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800823 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
824 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800825 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700826 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700827 SCOPED_TRACE("");
828 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000829 }
830
831 // Now check the packet buffer, and make sure it is empty.
832 EXPECT_TRUE(packet_buffer_->Empty());
833
834 EXPECT_CALL(mock_decoder, Die());
835}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000836
837TEST_F(NetEqImplTest, UnsupportedDecoder) {
838 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100839 ::testing::NiceMock<MockAudioDecoder> decoder;
840
841 CreateInstance(
842 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700843 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800844 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000845
846 const uint8_t kPayloadType = 17; // Just an arbitrary number.
847 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
848 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000849
Peter Kastingdce40cf2015-08-24 14:52:23 -0700850 const size_t kPayloadLengthSamples =
851 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000852 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700853 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200854 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700855 RTPHeader rtp_header;
856 rtp_header.payloadType = kPayloadType;
857 rtp_header.sequenceNumber = 0x1234;
858 rtp_header.timestamp = 0x12345678;
859 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000860
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000861 const uint8_t kFirstPayloadValue = 1;
862 const uint8_t kSecondPayloadValue = 2;
863
ossu61a208b2016-09-20 01:38:00 -0700864 EXPECT_CALL(decoder,
865 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
866 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200867 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000868
ossu61a208b2016-09-20 01:38:00 -0700869 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000870 .Times(0);
871
ossu61a208b2016-09-20 01:38:00 -0700872 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
873 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000874 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700875 .WillOnce(DoAll(
876 SetArrayArgument<3>(dummy_output,
877 dummy_output + kPayloadLengthSamples * kChannels),
878 SetArgPointee<4>(AudioDecoder::kSpeech),
879 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000880
ossu61a208b2016-09-20 01:38:00 -0700881 EXPECT_CALL(decoder,
882 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
883 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200884 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700885
886 EXPECT_CALL(decoder, SampleRateHz())
887 .WillRepeatedly(Return(kSampleRateHz));
888
889 EXPECT_CALL(decoder, Channels())
890 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000891
Niels Möllera1eb9c72018-12-07 15:24:42 +0100892 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
893 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000894
895 // Insert one packet.
896 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
897 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700898 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000899
900 // Insert another packet.
901 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700902 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000903 // The second timestamp needs to be at least 30 ms after the first to make
904 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700905 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000906 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700907 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000908
henrik.lundin6d8e0112016-03-04 10:34:21 -0800909 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700910 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200912 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700913 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800914 // Output size and number of channels should be correct.
915 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
916 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
917 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000918
henrik.lundin6d8e0112016-03-04 10:34:21 -0800919 // Second call to GetAudio will decode the packet that is ok. No errors are
920 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700921 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800922 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
923 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700924
925 // Die isn't called through NiceMock (since it's called by the
926 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
927 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000928}
929
henrik.lundin116c84e2015-08-27 13:14:48 -0700930// This test inserts packets until the buffer is flushed. After that, it asks
931// NetEq for the network statistics. The purpose of the test is to make sure
932// that even though the buffer size increment is negative (which it becomes when
933// the packet causing a flush is inserted), the packet length stored in the
934// decision logic remains valid.
935TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
936 UseNoMocks();
937 CreateInstance();
938
939 const size_t kPayloadLengthSamples = 80;
940 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
941 const uint8_t kPayloadType = 17; // Just an arbitrary number.
942 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
943 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700944 RTPHeader rtp_header;
945 rtp_header.payloadType = kPayloadType;
946 rtp_header.sequenceNumber = 0x1234;
947 rtp_header.timestamp = 0x12345678;
948 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700949
Niels Möller05543682019-01-10 16:55:06 +0100950 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
951 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -0700952
953 // Insert packets until the buffer flushes.
954 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
955 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
956 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700957 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
958 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
959 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700960 }
961 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
962
963 // Ask for network statistics. This should not crash.
964 NetEqNetworkStatistics stats;
965 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
966}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200967
968TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
969 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100970 // Create a mock decoder object.
971 MockAudioDecoder mock_decoder;
972
973 CreateInstance(
974 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200975
976 const uint8_t kPayloadType = 17; // Just an arbitrary number.
977 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
978 const int kSampleRateHz = 8000;
979 const size_t kPayloadLengthSamples =
980 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
981 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
982 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700983 RTPHeader rtp_header;
984 rtp_header.payloadType = kPayloadType;
985 rtp_header.sequenceNumber = 0x1234;
986 rtp_header.timestamp = 0x12345678;
987 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200988
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200989 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700990 EXPECT_CALL(mock_decoder, SampleRateHz())
991 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200992 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
993 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
994 .WillRepeatedly(Return(0));
995 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200996 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200997 int16_t dummy_output[kPayloadLengthSamples] = {0};
998 // The below expectation will make the mock decoder write
999 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1000 // speech. That is, the decoded length is 5 samples shorter than the expected.
1001 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001002 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001003 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001004 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001005 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001006 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001007 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001008 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1009 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001010
1011 // Insert one packet.
1012 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001013 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001014
1015 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1016
1017 // Pull audio once.
1018 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001019 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001020 bool muted;
1021 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001022 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1023 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001024 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001025
1026 EXPECT_CALL(mock_decoder, Die());
1027}
minyuel6d92bf52015-09-23 15:20:39 +02001028
1029// This test checks the behavior of NetEq when audio decoder fails.
1030TEST_F(NetEqImplTest, DecodingError) {
1031 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001032 // Create a mock decoder object.
1033 MockAudioDecoder mock_decoder;
1034
1035 CreateInstance(
1036 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001037
1038 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1039 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1040 const int kSampleRateHz = 8000;
1041 const int kDecoderErrorCode = -97; // Any negative number.
1042
1043 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1044 const size_t kFrameLengthSamples =
1045 static_cast<size_t>(5 * kSampleRateHz / 1000);
1046
1047 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1048
1049 uint8_t payload[kPayloadLengthBytes] = {0};
1050
henrik.lundin246ef3e2017-04-24 09:14:32 -07001051 RTPHeader rtp_header;
1052 rtp_header.payloadType = kPayloadType;
1053 rtp_header.sequenceNumber = 0x1234;
1054 rtp_header.timestamp = 0x12345678;
1055 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001056
minyuel6d92bf52015-09-23 15:20:39 +02001057 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001058 EXPECT_CALL(mock_decoder, SampleRateHz())
1059 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001060 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1061 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1062 .WillRepeatedly(Return(0));
1063 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001064 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001065 EXPECT_CALL(mock_decoder, ErrorCode())
1066 .WillOnce(Return(kDecoderErrorCode));
1067 EXPECT_CALL(mock_decoder, HasDecodePlc())
1068 .WillOnce(Return(false));
1069 int16_t dummy_output[kFrameLengthSamples] = {0};
1070
1071 {
1072 InSequence sequence; // Dummy variable.
1073 // Mock decoder works normally the first time.
1074 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001075 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001076 .Times(3)
1077 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001078 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001079 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001080 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001081 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001082 .RetiresOnSaturation();
1083
1084 // Then mock decoder fails. A common reason for failure can be buffer being
1085 // too short
1086 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001087 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001088 .WillOnce(Return(-1))
1089 .RetiresOnSaturation();
1090
1091 // Mock decoder finally returns to normal.
1092 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001093 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001094 .Times(2)
1095 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001096 DoAll(SetArrayArgument<3>(dummy_output,
1097 dummy_output + kFrameLengthSamples),
1098 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001099 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001100 }
1101
Niels Möllera1eb9c72018-12-07 15:24:42 +01001102 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1103 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001104
1105 // Insert packets.
1106 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001107 rtp_header.sequenceNumber += 1;
1108 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001109 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001110 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001111 }
1112
1113 // Pull audio.
1114 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001115 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001116 bool muted;
1117 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001118 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1119 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001120 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001121
1122 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001123 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001124 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1125 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001126 // We are not expecting anything for output.speech_type_, since an error was
1127 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001128
1129 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001130 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001131 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1132 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001133 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001134
1135 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001136 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001137 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1138 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001139 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001140
1141 EXPECT_CALL(mock_decoder, Die());
1142}
1143
1144// This test checks the behavior of NetEq when audio decoder fails during CNG.
1145TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1146 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001147
1148 // Create a mock decoder object.
1149 MockAudioDecoder mock_decoder;
1150 CreateInstance(
1151 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001152
1153 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1154 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1155 const int kSampleRateHz = 8000;
1156 const int kDecoderErrorCode = -97; // Any negative number.
1157
1158 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1159 const size_t kFrameLengthSamples =
1160 static_cast<size_t>(5 * kSampleRateHz / 1000);
1161
1162 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1163
1164 uint8_t payload[kPayloadLengthBytes] = {0};
1165
henrik.lundin246ef3e2017-04-24 09:14:32 -07001166 RTPHeader rtp_header;
1167 rtp_header.payloadType = kPayloadType;
1168 rtp_header.sequenceNumber = 0x1234;
1169 rtp_header.timestamp = 0x12345678;
1170 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001171
minyuel6d92bf52015-09-23 15:20:39 +02001172 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001173 EXPECT_CALL(mock_decoder, SampleRateHz())
1174 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001175 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1176 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1177 .WillRepeatedly(Return(0));
1178 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001179 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001180 EXPECT_CALL(mock_decoder, ErrorCode())
1181 .WillOnce(Return(kDecoderErrorCode));
1182 int16_t dummy_output[kFrameLengthSamples] = {0};
1183
1184 {
1185 InSequence sequence; // Dummy variable.
1186 // Mock decoder works normally the first 2 times.
1187 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001188 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001189 .Times(2)
1190 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001191 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001192 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001193 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001194 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001195 .RetiresOnSaturation();
1196
1197 // Then mock decoder fails. A common reason for failure can be buffer being
1198 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001199 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001200 .WillOnce(Return(-1))
1201 .RetiresOnSaturation();
1202
1203 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001204 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001205 .Times(2)
1206 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001207 DoAll(SetArrayArgument<3>(dummy_output,
1208 dummy_output + kFrameLengthSamples),
1209 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001210 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001211 }
1212
Niels Möller50b66d52018-12-11 14:43:21 +01001213 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1214 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001215
1216 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1217 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001218 rtp_header.sequenceNumber += 1;
1219 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001220 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001221 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001222 }
1223
1224 // Pull audio.
1225 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001226 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001227 bool muted;
1228 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1230 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001231 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001232
1233 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001234 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001235 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1236 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001237 // We are not expecting anything for output.speech_type_, since an error was
1238 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001239
1240 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001241 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001242 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1243 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001244 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001245
1246 EXPECT_CALL(mock_decoder, Die());
1247}
1248
henrik.lundind89814b2015-11-23 06:49:25 -08001249// Tests that the return value from last_output_sample_rate_hz() is equal to the
1250// configured inital sample rate.
1251TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1252 UseNoMocks();
1253 config_.sample_rate_hz = 48000;
1254 CreateInstance();
1255 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1256}
1257
henrik.lundined497212016-04-25 10:11:38 -07001258TEST_F(NetEqImplTest, TickTimerIncrement) {
1259 UseNoMocks();
1260 CreateInstance();
1261 ASSERT_TRUE(tick_timer_);
1262 EXPECT_EQ(0u, tick_timer_->ticks());
1263 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001264 bool muted;
1265 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001266 EXPECT_EQ(1u, tick_timer_->ticks());
1267}
1268
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001269TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1270 UseNoMocks();
1271 use_mock_delay_manager_ = true;
1272 CreateInstance();
1273
1274 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1275 .WillOnce(Return(true))
1276 .WillOnce(Return(false));
1277
1278 const int delay_ms = 200;
1279
1280 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1281 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1282}
1283
1284TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1285 UseNoMocks();
1286 use_mock_delay_manager_ = true;
1287 CreateInstance();
1288
1289 const int delay_ms = 200;
1290
1291 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1292 .WillOnce(Return(delay_ms));
1293
1294 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1295}
1296
henrik.lundin114c1b32017-04-26 07:47:32 -07001297TEST_F(NetEqImplTest, TargetDelayMs) {
1298 UseNoMocks();
1299 use_mock_delay_manager_ = true;
1300 CreateInstance();
1301 // Let the dummy target delay be 17 packets.
1302 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1303 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1304 .WillOnce(Return(kTargetLevelPacketsQ8));
1305 // Default packet size before any packet has been decoded is 30 ms, so we are
1306 // expecting 17 * 30 = 510 ms target delay.
1307 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1308}
1309
henrik.lundinb8c55b12017-05-10 07:38:01 -07001310TEST_F(NetEqImplTest, InsertEmptyPacket) {
1311 UseNoMocks();
1312 use_mock_delay_manager_ = true;
1313 CreateInstance();
1314
1315 RTPHeader rtp_header;
1316 rtp_header.payloadType = 17;
1317 rtp_header.sequenceNumber = 0x1234;
1318 rtp_header.timestamp = 0x12345678;
1319 rtp_header.ssrc = 0x87654321;
1320
1321 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1322 neteq_->InsertEmptyPacket(rtp_header);
1323}
1324
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001325TEST_F(NetEqImplTest, EnableRtxHandling) {
1326 UseNoMocks();
1327 use_mock_delay_manager_ = true;
1328 config_.enable_rtx_handling = true;
1329 CreateInstance();
1330 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1331 .Times(1)
1332 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1333
1334 const int kPayloadLengthSamples = 80;
1335 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1336 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1337 const uint32_t kReceiveTime = 17;
1338 uint8_t payload[kPayloadLengthBytes] = {0};
1339 RTPHeader rtp_header;
1340 rtp_header.payloadType = kPayloadType;
1341 rtp_header.sequenceNumber = 0x1234;
1342 rtp_header.timestamp = 0x12345678;
1343 rtp_header.ssrc = 0x87654321;
1344
Niels Möller05543682019-01-10 16:55:06 +01001345 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1346 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001347 EXPECT_EQ(NetEq::kOK,
1348 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1349 AudioFrame output;
1350 bool muted;
1351 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1352
1353 // Insert second packet that was sent before the first packet.
1354 rtp_header.sequenceNumber -= 1;
1355 rtp_header.timestamp -= kPayloadLengthSamples;
1356 EXPECT_CALL(*mock_delay_manager_,
1357 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1358 EXPECT_EQ(NetEq::kOK,
1359 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1360}
1361
minyue5bd33972016-05-02 04:46:11 -07001362class Decoder120ms : public AudioDecoder {
1363 public:
kwiberg347d3512016-06-16 01:59:09 -07001364 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1365 : sample_rate_hz_(sample_rate_hz),
1366 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001367 speech_type_(speech_type) {}
1368
1369 int DecodeInternal(const uint8_t* encoded,
1370 size_t encoded_len,
1371 int sample_rate_hz,
1372 int16_t* decoded,
1373 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001374 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001375 size_t decoded_len =
1376 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1377 for (size_t i = 0; i < decoded_len; ++i) {
1378 decoded[i] = next_value_++;
1379 }
1380 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001381 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001382 }
1383
1384 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001385 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001386 size_t Channels() const override { return 2; }
1387
1388 private:
kwiberg347d3512016-06-16 01:59:09 -07001389 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001390 int16_t next_value_;
1391 SpeechType speech_type_;
1392};
1393
1394class NetEqImplTest120ms : public NetEqImplTest {
1395 protected:
1396 NetEqImplTest120ms() : NetEqImplTest() {}
1397 virtual ~NetEqImplTest120ms() {}
1398
1399 void CreateInstanceNoMocks() {
1400 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001401 CreateInstance(decoder_factory_);
1402 EXPECT_TRUE(neteq_->RegisterPayloadType(
1403 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001404 }
1405
1406 void CreateInstanceWithDelayManagerMock() {
1407 UseNoMocks();
1408 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001409 CreateInstance(decoder_factory_);
1410 EXPECT_TRUE(neteq_->RegisterPayloadType(
1411 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001412 }
1413
1414 uint32_t timestamp_diff_between_packets() const {
1415 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1416 }
1417
1418 uint32_t first_timestamp() const { return 10u; }
1419
1420 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001421 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001422 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001423 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1424 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001425 }
1426 }
1427
1428 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001429 RTPHeader rtp_header;
1430 rtp_header.payloadType = kPayloadType;
1431 rtp_header.sequenceNumber = sequence_number_;
1432 rtp_header.timestamp = timestamp;
1433 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001434 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1435 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001436 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001437 sequence_number_++;
1438 }
1439
1440 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001441 const uint32_t sampling_freq = kSamplingFreq_;
1442 decoder_factory_ =
1443 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1444 [sampling_freq, speech_type]() {
1445 std::unique_ptr<AudioDecoder> decoder =
1446 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1447 RTC_CHECK_EQ(2, decoder->Channels());
1448 return decoder;
1449 });
minyue5bd33972016-05-02 04:46:11 -07001450 }
1451
Niels Möllera0f44302018-11-30 10:45:12 +01001452 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001453 AudioFrame output_;
1454 const uint32_t kPayloadType = 17;
1455 const uint32_t kSamplingFreq_ = 48000;
1456 uint16_t sequence_number_ = 1;
1457};
1458
minyue5bd33972016-05-02 04:46:11 -07001459TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001460 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001461 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001462
1463 InsertPacket(first_timestamp());
1464 GetFirstPacket();
1465
henrik.lundin7a926812016-05-12 13:51:28 -07001466 bool muted;
1467 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001468 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1469}
1470
1471TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001472 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001473 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001474
1475 InsertPacket(first_timestamp());
1476 GetFirstPacket();
1477
1478 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1479}
1480
1481TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001482 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001483 CreateInstanceWithDelayManagerMock();
1484
minyue5bd33972016-05-02 04:46:11 -07001485 InsertPacket(first_timestamp());
1486
1487 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001488 bool muted;
1489 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001490
1491 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1492
1493 // Delay manager reports a target level which should cause a Merge.
1494 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1495
henrik.lundin7a926812016-05-12 13:51:28 -07001496 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001497 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1498}
1499
1500TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001501 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001502 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001503
1504 InsertPacket(first_timestamp());
1505 GetFirstPacket();
1506
henrik.lundin7a926812016-05-12 13:51:28 -07001507 bool muted;
1508 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001509 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1510}
1511
1512TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001513 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001514 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001515
1516 InsertPacket(first_timestamp());
1517 GetFirstPacket();
1518 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1519
1520 // Delay manager report buffer limit which should cause a FastAccelerate.
1521 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1522 .Times(1)
1523 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1524
henrik.lundin7a926812016-05-12 13:51:28 -07001525 bool muted;
1526 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001527 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1528}
1529
1530TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001531 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001532 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001533
1534 InsertPacket(first_timestamp());
1535 GetFirstPacket();
1536
1537 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1538
1539 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1540 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1541 .Times(1)
1542 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1543
henrik.lundin7a926812016-05-12 13:51:28 -07001544 bool muted;
1545 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001546 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1547}
1548
1549TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001550 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001551 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001552
1553 InsertPacket(first_timestamp());
1554 GetFirstPacket();
1555
1556 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1557
1558 // Delay manager report buffer limit which should cause a Accelerate.
1559 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1560 .Times(1)
1561 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1562
henrik.lundin7a926812016-05-12 13:51:28 -07001563 bool muted;
1564 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001565 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1566}
1567
minyuel6d92bf52015-09-23 15:20:39 +02001568}// namespace webrtc