blob: 517f4ac85c8290db81531bada812e153cc31f21f [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
14
Niels Möllera0f44302018-11-30 10:45:12 +010015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/accelerate.h"
18#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010019#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/include/neteq.h"
21#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
22#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
23#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
24#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
25#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
26#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
27#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010030#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_coding/neteq/sync_buffer.h"
32#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010033#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010034#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010035#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "test/gmock.h"
37#include "test/gtest.h"
38#include "test/mock_audio_decoder.h"
39#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010042using ::testing::AtLeast;
43using ::testing::DoAll;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044using ::testing::InSequence;
45using ::testing::Invoke;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000046using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010047using ::testing::Pointee;
48using ::testing::Return;
49using ::testing::ReturnNull;
50using ::testing::SetArgPointee;
51using ::testing::SetArrayArgument;
52using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
54namespace webrtc {
55
56// This function is called when inserting a packet list into the mock packet
57// buffer. The purpose is to delete all inserted packets properly, to avoid
58// memory leaks in the test.
59int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070060 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 return PacketBuffer::kOK;
62}
63
64class NetEqImplTest : public ::testing::Test {
65 protected:
Ivo Creusen24192c22019-07-12 17:00:25 +020066 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000067
Niels Möllera0f44302018-11-30 10:45:12 +010068 void CreateInstance(
69 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
70 ASSERT_TRUE(decoder_factory);
Ivo Creusen24192c22019-07-12 17:00:25 +020071 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070072
73 // Get a local pointer to NetEq's TickTimer object.
74 tick_timer_ = deps.tick_timer.get();
75
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000076 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
78 mock_buffer_level_filter_ = mock.get();
79 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000080 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070081 buffer_level_filter_ = deps.buffer_level_filter.get();
82
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000083 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
85 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000086 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
87 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000089 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070090 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000091
henrik.lundin1d9061e2016-04-26 12:19:34 -070092 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070093 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010094 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070095 mock_delay_peak_detector_ = mock.get();
96 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
97 deps.delay_peak_detector = std::move(mock);
98 }
99 delay_peak_detector_ = deps.delay_peak_detector.get();
100
101 if (use_mock_delay_manager_) {
102 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100103 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
104 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100105 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100106 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700107 mock_delay_manager_ = mock.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700108 deps.delay_manager = std::move(mock);
109 }
110 delay_manager_ = deps.delay_manager.get();
111
112 if (use_mock_dtmf_buffer_) {
113 std::unique_ptr<MockDtmfBuffer> mock(
114 new MockDtmfBuffer(config_.sample_rate_hz));
115 mock_dtmf_buffer_ = mock.get();
116 deps.dtmf_buffer = std::move(mock);
117 }
118 dtmf_buffer_ = deps.dtmf_buffer.get();
119
120 if (use_mock_dtmf_tone_generator_) {
121 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
122 mock_dtmf_tone_generator_ = mock.get();
123 deps.dtmf_tone_generator = std::move(mock);
124 }
125 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
126
127 if (use_mock_packet_buffer_) {
128 std::unique_ptr<MockPacketBuffer> mock(
129 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
130 mock_packet_buffer_ = mock.get();
131 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132 }
133 packet_buffer_ = deps.packet_buffer.get();
134
135 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700136 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700138 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700139 }
ossua70695a2016-09-22 02:06:28 -0700140 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700141
142 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
143 new TimestampScaler(*deps.decoder_database.get()));
144
145 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000146 ASSERT_TRUE(neteq_ != NULL);
147 }
148
Niels Möllera0f44302018-11-30 10:45:12 +0100149 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
150
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000151 void UseNoMocks() {
152 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
153 use_mock_buffer_level_filter_ = false;
154 use_mock_decoder_database_ = false;
155 use_mock_delay_peak_detector_ = false;
156 use_mock_delay_manager_ = false;
157 use_mock_dtmf_buffer_ = false;
158 use_mock_dtmf_tone_generator_ = false;
159 use_mock_packet_buffer_ = false;
160 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 }
162
163 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000164 if (use_mock_buffer_level_filter_) {
165 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
166 }
167 if (use_mock_decoder_database_) {
168 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
169 }
170 if (use_mock_delay_manager_) {
171 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
172 }
173 if (use_mock_delay_peak_detector_) {
174 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
175 }
176 if (use_mock_dtmf_buffer_) {
177 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
178 }
179 if (use_mock_dtmf_tone_generator_) {
180 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
181 }
182 if (use_mock_packet_buffer_) {
183 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
184 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 }
186
Niels Möller05543682019-01-10 16:55:06 +0100187 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800188 const size_t kPayloadLength = 4;
189 const uint8_t kPayloadType = 110;
190 const uint32_t kReceiveTime = 17;
191 const int kSampleRateHz = 16000;
192 config_.sample_rate_hz = kSampleRateHz;
193 UseNoMocks();
194 CreateInstance();
195 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200196 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700197 RTPHeader rtp_header;
198 rtp_header.payloadType = kPayloadType;
199 rtp_header.sequenceNumber = 0x1234;
200 rtp_header.timestamp = 0x12345678;
201 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800202
Niels Möller05543682019-01-10 16:55:06 +0100203 EXPECT_TRUE(neteq_->RegisterPayloadType(
204 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800205
206 // Insert first packet.
207 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700208 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800209
210 // Pull audio once.
211 const size_t kMaxOutputSize =
212 static_cast<size_t>(10 * kSampleRateHz / 1000);
213 AudioFrame output;
214 bool muted;
215 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
216 ASSERT_FALSE(muted);
217 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
218 EXPECT_EQ(1u, output.num_channels_);
219 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
220
221 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200222 const std::vector<int16_t> kOutput(
223 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
225 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
226 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
227 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
228 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
229 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800230 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700231 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800232 }
233
henrik.lundin1d9061e2016-04-26 12:19:34 -0700234 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000235 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700236 TickTimer* tick_timer_ = nullptr;
237 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
238 BufferLevelFilter* buffer_level_filter_ = nullptr;
239 bool use_mock_buffer_level_filter_ = true;
240 MockDecoderDatabase* mock_decoder_database_ = nullptr;
241 DecoderDatabase* decoder_database_ = nullptr;
242 bool use_mock_decoder_database_ = true;
243 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
244 DelayPeakDetector* delay_peak_detector_ = nullptr;
245 bool use_mock_delay_peak_detector_ = true;
246 MockDelayManager* mock_delay_manager_ = nullptr;
247 DelayManager* delay_manager_ = nullptr;
248 bool use_mock_delay_manager_ = true;
249 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
250 DtmfBuffer* dtmf_buffer_ = nullptr;
251 bool use_mock_dtmf_buffer_ = true;
252 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
253 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
254 bool use_mock_dtmf_tone_generator_ = true;
255 MockPacketBuffer* mock_packet_buffer_ = nullptr;
256 PacketBuffer* packet_buffer_ = nullptr;
257 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700258 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
259 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700260 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261};
262
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263// This tests the interface class NetEq.
264// TODO(hlundin): Move to separate file?
265TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000266 NetEq::Config config;
Ivo Creusen24192c22019-07-12 17:00:25 +0200267 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 delete neteq;
269}
270
kwiberg5adaf732016-10-04 09:33:27 -0700271TEST_F(NetEqImplTest, RegisterPayloadType) {
272 CreateInstance();
273 constexpr int rtp_payload_type = 0;
274 const SdpAudioFormat format("pcmu", 8000, 1);
275 EXPECT_CALL(*mock_decoder_database_,
276 RegisterPayload(rtp_payload_type, format));
277 neteq_->RegisterPayloadType(rtp_payload_type, format);
278}
279
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000281 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000283 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200285 // Check that kOK is returned when database returns kDecoderNotFound, because
286 // removing a payload type that was never registered is not an error.
287 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288}
289
kwiberg6b19b562016-09-20 04:02:25 -0700290TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
291 CreateInstance();
292 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
293 neteq_->RemoveAllPayloadTypes();
294}
295
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000297 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000298 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 const uint8_t kPayloadType = 0;
300 const uint16_t kFirstSequenceNumber = 0x1234;
301 const uint32_t kFirstTimestamp = 0x12345678;
302 const uint32_t kSsrc = 0x87654321;
303 const uint32_t kFirstReceiveTime = 17;
304 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700305 RTPHeader rtp_header;
306 rtp_header.payloadType = kPayloadType;
307 rtp_header.sequenceNumber = kFirstSequenceNumber;
308 rtp_header.timestamp = kFirstTimestamp;
309 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700310 Packet fake_packet;
311 fake_packet.payload_type = kPayloadType;
312 fake_packet.sequence_number = kFirstSequenceNumber;
313 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314
kwibergc0f2dcf2016-05-31 06:28:03 -0700315 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
316 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100317 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800318 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200319 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800320 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700321 EXPECT_EQ("pcmu", format.name);
322
323 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
324 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
325 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
326 // BWE update function called with first packet.
327 EXPECT_CALL(*mock_decoder,
328 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
329 kFirstTimestamp, kFirstReceiveTime));
330 // BWE update function called with second packet.
331 EXPECT_CALL(
332 *mock_decoder,
333 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
334 kFirstTimestamp + 160, kFirstReceiveTime + 155));
335 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
336
337 *dec = std::move(mock_decoder);
338 }));
Niels Möller72899062019-01-11 09:36:13 +0100339 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
340 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341
342 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000343 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 .WillRepeatedly(Return(&info));
345
346 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000347 EXPECT_CALL(*mock_packet_buffer_, Empty())
348 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200349 EXPECT_CALL(*mock_packet_buffer_, Flush()).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.
Jonas Olssona4d87372019-07-05 19:08:33 +0200363 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
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));
Jonas Olssona4d87372019-07-05 19:08:33 +0200373 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0)).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000374 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000376 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
377 .WillOnce(Return(0));
378 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 .WillOnce(Return(0));
380 }
381
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700383 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384
385 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700386 rtp_header.timestamp += 160;
387 rtp_header.sequenceNumber += 1;
388 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389}
390
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000391TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
392 UseNoMocks();
393 CreateInstance();
394
395 const int kPayloadLengthSamples = 80;
396 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200397 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000398 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
399 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700400 RTPHeader rtp_header;
401 rtp_header.payloadType = kPayloadType;
402 rtp_header.sequenceNumber = 0x1234;
403 rtp_header.timestamp = 0x12345678;
404 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000405
Niels Möller05543682019-01-10 16:55:06 +0100406 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
407 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000408
409 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700410 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000411 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700412 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
413 rtp_header.timestamp += kPayloadLengthSamples;
414 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000415 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
416 }
417
418 // Insert one more packet and make sure the buffer got flushed. That is, it
419 // should only hold one single packet.
420 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700421 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700422 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700423 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700424 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
425 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000426}
427
solenberg2779bab2016-11-17 04:45:19 -0800428TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100429 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800430}
solenberg99df6c02016-10-11 04:35:34 -0700431
solenberg2779bab2016-11-17 04:45:19 -0800432TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100433 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800434}
solenberg99df6c02016-10-11 04:35:34 -0700435
solenberg2779bab2016-11-17 04:45:19 -0800436TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100437 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800438}
solenberg99df6c02016-10-11 04:35:34 -0700439
solenberg2779bab2016-11-17 04:45:19 -0800440TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100441 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700442}
443
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000444// This test verifies that timestamps propagate from the incoming packets
445// through to the sync buffer and to the playout timestamp.
446TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000447 const uint8_t kPayloadType = 17; // Just an arbitrary number.
448 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
449 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700450 const size_t kPayloadLengthSamples =
451 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000452 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
453 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700454 RTPHeader rtp_header;
455 rtp_header.payloadType = kPayloadType;
456 rtp_header.sequenceNumber = 0x1234;
457 rtp_header.timestamp = 0x12345678;
458 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000459
460 // This is a dummy decoder that produces as many output samples as the input
461 // has bytes. The output is an increasing series, starting at 1 for the first
462 // sample, and then increasing by 1 for each sample.
463 class CountingSamplesDecoder : public AudioDecoder {
464 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000465 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000466
467 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100468 int DecodeInternal(const uint8_t* encoded,
469 size_t encoded_len,
470 int /* sample_rate_hz */,
471 int16_t* decoded,
472 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000473 for (size_t i = 0; i < encoded_len; ++i) {
474 decoded[i] = next_value_++;
475 }
476 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200477 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000478 }
479
Karl Wiberg43766482015-08-27 15:22:11 +0200480 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000481
kwiberg347d3512016-06-16 01:59:09 -0700482 int SampleRateHz() const override { return kSampleRateHz; }
483
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000484 size_t Channels() const override { return 1; }
485
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000486 uint16_t next_value() const { return next_value_; }
487
488 private:
489 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000490 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000491
Niels Möllerb7180c02018-12-06 13:07:11 +0100492 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
493 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
494
495 UseNoMocks();
496 CreateInstance(decoder_factory);
497
498 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100499 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
501 // Insert one packet.
502 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700503 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000504
505 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700506 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800507 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700508 bool muted;
509 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
510 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800511 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
512 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800513 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000514
515 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700516 EXPECT_EQ(kPayloadLengthSamples,
517 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000518
519 // The value of the last of the output samples is the same as the number of
520 // samples played from the decoded packet. Thus, this number + the RTP
521 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200522 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700523 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200524 absl::optional<uint32_t>(rtp_header.timestamp +
525 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700526 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527
528 // Check the timestamp for the last value in the sync buffer. This should
529 // be one full frame length ahead of the RTP timestamp.
530 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
531 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700532 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000533 sync_buffer->end_timestamp());
534
535 // Check that the number of samples still to play from the sync buffer add
536 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800537 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700538 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800539 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000540}
541
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000542TEST_F(NetEqImplTest, ReorderedPacket) {
543 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100544 // Create a mock decoder object.
545 MockAudioDecoder mock_decoder;
546
547 CreateInstance(
548 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000549
550 const uint8_t kPayloadType = 17; // Just an arbitrary number.
551 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
552 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700553 const size_t kPayloadLengthSamples =
554 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000555 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
556 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700557 RTPHeader rtp_header;
558 rtp_header.payloadType = kPayloadType;
559 rtp_header.sequenceNumber = 0x1234;
560 rtp_header.timestamp = 0x12345678;
561 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000562
Karl Wiberg43766482015-08-27 15:22:11 +0200563 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700564 EXPECT_CALL(mock_decoder, SampleRateHz())
565 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000566 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000567 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
568 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700569 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200570 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000571 int16_t dummy_output[kPayloadLengthSamples] = {0};
572 // The below expectation will make the mock decoder write
573 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100574 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
575 kSampleRateHz, _, _))
576 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000577 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100578 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200579 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100580 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
581 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000582
583 // Insert one packet.
584 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700585 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000586
587 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700588 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800589 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700590 bool muted;
591 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800592 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
593 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800594 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000595
596 // Insert two more packets. The first one is out of order, and is already too
597 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700598 rtp_header.sequenceNumber -= 1;
599 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000600 payload[0] = 1;
601 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700602 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
603 rtp_header.sequenceNumber += 2;
604 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000605 payload[0] = 2;
606 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700607 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608
609 // Expect only the second packet to be decoded (the one with "2" as the first
610 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100611 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
612 kSampleRateHz, _, _))
613 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000614 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100615 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200616 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000617
618 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700619 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800620 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
621 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800622 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000623
624 // Now check the packet buffer, and make sure it is empty, since the
625 // out-of-order packet should have been discarded.
626 EXPECT_TRUE(packet_buffer_->Empty());
627
628 EXPECT_CALL(mock_decoder, Die());
629}
630
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000631// This test verifies that NetEq can handle the situation where the first
632// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000633TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000634 UseNoMocks();
635 CreateInstance();
636
637 const uint8_t kPayloadType = 17; // Just an arbitrary number.
638 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
639 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700640 const size_t kPayloadLengthSamples =
641 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700642 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000643 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700644 RTPHeader rtp_header;
645 rtp_header.payloadType = kPayloadType;
646 rtp_header.sequenceNumber = 0x1234;
647 rtp_header.timestamp = 0x12345678;
648 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000649
650 // Insert one packet. Note that we have not registered any payload type, so
651 // this packet will be rejected.
652 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700653 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000654
655 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700656 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800657 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700658 bool muted;
659 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800660 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
661 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
662 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800663 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000664
665 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100666 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
667 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000668
669 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700670 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700671 rtp_header.sequenceNumber++;
672 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000673 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700674 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000675 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
676 }
677
678 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700679 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700680 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800681 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
682 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
683 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800684 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000685 << "NetEq did not decode the packets as expected.";
686 }
687}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000688
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200689// This test verifies that audio interruption is not logged for the initial
690// PLC period before the first packet is deocoded.
691// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
692TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
693 UseNoMocks();
694 CreateInstance();
695
696 const uint8_t kPayloadType = 17; // Just an arbitrary number.
697 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
698 const int kSampleRateHz = 8000;
699 const size_t kPayloadLengthSamples =
700 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
701 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
702 uint8_t payload[kPayloadLengthBytes] = {0};
703 RTPHeader rtp_header;
704 rtp_header.payloadType = kPayloadType;
705 rtp_header.sequenceNumber = 0x1234;
706 rtp_header.timestamp = 0x12345678;
707 rtp_header.ssrc = 0x87654321;
708
709 // Register the payload type.
710 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
711 SdpAudioFormat("l16", 8000, 1)));
712
713 // Pull audio several times. No packets have been inserted yet.
714 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
715 AudioFrame output;
716 bool muted;
717 for (int i = 0; i < 100; ++i) {
718 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
719 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
720 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
721 EXPECT_EQ(1u, output.num_channels_);
722 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
723 }
724
725 // Insert 10 packets.
726 for (size_t i = 0; i < 10; ++i) {
727 rtp_header.sequenceNumber++;
728 rtp_header.timestamp += kPayloadLengthSamples;
729 EXPECT_EQ(NetEq::kOK,
730 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
731 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
732 }
733
734 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
735 for (size_t i = 0; i < 3; ++i) {
736 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
737 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
738 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
739 EXPECT_EQ(1u, output.num_channels_);
740 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
741 << "NetEq did not decode the packets as expected.";
742 }
743
744 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200745 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200746}
747
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000748// This test verifies that NetEq can handle comfort noise and enters/quits codec
749// internal CNG mode properly.
750TEST_F(NetEqImplTest, CodecInternalCng) {
751 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100752 // Create a mock decoder object.
753 MockAudioDecoder mock_decoder;
754 CreateInstance(
755 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756
757 const uint8_t kPayloadType = 17; // Just an arbitrary number.
758 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
759 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700760 const size_t kPayloadLengthSamples =
761 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
762 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000763 uint8_t payload[kPayloadLengthBytes] = {0};
764 int16_t dummy_output[kPayloadLengthSamples] = {0};
765
henrik.lundin246ef3e2017-04-24 09:14:32 -0700766 RTPHeader rtp_header;
767 rtp_header.payloadType = kPayloadType;
768 rtp_header.sequenceNumber = 0x1234;
769 rtp_header.timestamp = 0x12345678;
770 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000771
Karl Wiberg43766482015-08-27 15:22:11 +0200772 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700773 EXPECT_CALL(mock_decoder, SampleRateHz())
774 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000775 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000776 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
777 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700778 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200779 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700780 // Packed duration when asking the decoder for more CNG data (without a new
781 // packet).
782 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200783 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000784
785 // Pointee(x) verifies that first byte of the payload equals x, this makes it
786 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100787 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
788 kSampleRateKhz * 1000, _, _))
789 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000790 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100791 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200792 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000793
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100794 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
795 kSampleRateKhz * 1000, _, _))
796 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000797 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100798 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200799 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000800
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100801 EXPECT_CALL(mock_decoder,
802 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
803 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000804 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100805 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200806 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000807
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100808 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
809 kSampleRateKhz * 1000, _, _))
810 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000811 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100812 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200813 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000814
Niels Möller50b66d52018-12-11 14:43:21 +0100815 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
816 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817
818 // Insert one packet (decoder will return speech).
819 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
822 // Insert second packet (decoder will return CNG).
823 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700824 rtp_header.sequenceNumber++;
825 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000826 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700827 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000828
Peter Kastingdce40cf2015-08-24 14:52:23 -0700829 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800830 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800831 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200832 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
833 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
834 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000835 int expected_timestamp_increment[8] = {
836 -1, // will not be used.
837 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200838 -1,
839 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
840 -1,
841 -1,
842 50 * kSampleRateKhz,
843 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000844
henrik.lundin7a926812016-05-12 13:51:28 -0700845 bool muted;
846 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200847 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700848 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000849
henrik.lundin0d96ab72016-04-06 12:28:26 -0700850 // Lambda for verifying the timestamps.
851 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200852 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700853 if (expected_timestamp_increment[i] == -1) {
854 // Expect to get an empty timestamp value during CNG and PLC.
855 EXPECT_FALSE(ts) << "i = " << i;
856 } else {
857 ASSERT_TRUE(ts) << "i = " << i;
858 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
859 << "i = " << i;
860 last_timestamp = ts;
861 }
862 };
863
Peter Kastingdce40cf2015-08-24 14:52:23 -0700864 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800865 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
866 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800867 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700868 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700869 SCOPED_TRACE("");
870 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000871 }
872
873 // Insert third packet, which leaves a gap from last packet.
874 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700875 rtp_header.sequenceNumber += 2;
876 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000877 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700878 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000879
Peter Kastingdce40cf2015-08-24 14:52:23 -0700880 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800881 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
882 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800883 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700884 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700885 SCOPED_TRACE("");
886 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000887 }
888
889 // Now check the packet buffer, and make sure it is empty.
890 EXPECT_TRUE(packet_buffer_->Empty());
891
892 EXPECT_CALL(mock_decoder, Die());
893}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000894
895TEST_F(NetEqImplTest, UnsupportedDecoder) {
896 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100897 ::testing::NiceMock<MockAudioDecoder> decoder;
898
899 CreateInstance(
900 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700901 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800902 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000903
904 const uint8_t kPayloadType = 17; // Just an arbitrary number.
905 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
906 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000907
Peter Kastingdce40cf2015-08-24 14:52:23 -0700908 const size_t kPayloadLengthSamples =
909 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700911 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200912 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700913 RTPHeader rtp_header;
914 rtp_header.payloadType = kPayloadType;
915 rtp_header.sequenceNumber = 0x1234;
916 rtp_header.timestamp = 0x12345678;
917 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000918
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000919 const uint8_t kFirstPayloadValue = 1;
920 const uint8_t kSecondPayloadValue = 2;
921
ossu61a208b2016-09-20 01:38:00 -0700922 EXPECT_CALL(decoder,
923 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
924 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200925 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000926
ossu61a208b2016-09-20 01:38:00 -0700927 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000928 .Times(0);
929
ossu61a208b2016-09-20 01:38:00 -0700930 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
931 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000932 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700933 .WillOnce(DoAll(
934 SetArrayArgument<3>(dummy_output,
935 dummy_output + kPayloadLengthSamples * kChannels),
936 SetArgPointee<4>(AudioDecoder::kSpeech),
937 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000938
ossu61a208b2016-09-20 01:38:00 -0700939 EXPECT_CALL(decoder,
940 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
941 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200942 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700943
Jonas Olssona4d87372019-07-05 19:08:33 +0200944 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -0700945
Jonas Olssona4d87372019-07-05 19:08:33 +0200946 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000947
Niels Möllera1eb9c72018-12-07 15:24:42 +0100948 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
949 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000950
951 // Insert one packet.
952 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
953 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700954 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000955
956 // Insert another packet.
957 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700958 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000959 // The second timestamp needs to be at least 30 ms after the first to make
960 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700961 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000962 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700963 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000964
henrik.lundin6d8e0112016-03-04 10:34:21 -0800965 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700966 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800967 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200968 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700969 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 // Output size and number of channels should be correct.
971 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
972 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
973 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000974
henrik.lundin6d8e0112016-03-04 10:34:21 -0800975 // Second call to GetAudio will decode the packet that is ok. No errors are
976 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700977 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800978 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
979 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700980
981 // Die isn't called through NiceMock (since it's called by the
982 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
983 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000984}
985
henrik.lundin116c84e2015-08-27 13:14:48 -0700986// This test inserts packets until the buffer is flushed. After that, it asks
987// NetEq for the network statistics. The purpose of the test is to make sure
988// that even though the buffer size increment is negative (which it becomes when
989// the packet causing a flush is inserted), the packet length stored in the
990// decision logic remains valid.
991TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
992 UseNoMocks();
993 CreateInstance();
994
995 const size_t kPayloadLengthSamples = 80;
996 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
997 const uint8_t kPayloadType = 17; // Just an arbitrary number.
998 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
999 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001000 RTPHeader rtp_header;
1001 rtp_header.payloadType = kPayloadType;
1002 rtp_header.sequenceNumber = 0x1234;
1003 rtp_header.timestamp = 0x12345678;
1004 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001005
Niels Möller05543682019-01-10 16:55:06 +01001006 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1007 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001008
1009 // Insert packets until the buffer flushes.
1010 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1011 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1012 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001013 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1014 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1015 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001016 }
1017 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1018
1019 // Ask for network statistics. This should not crash.
1020 NetEqNetworkStatistics stats;
1021 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1022}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001023
1024TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1025 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001026 // Create a mock decoder object.
1027 MockAudioDecoder mock_decoder;
1028
1029 CreateInstance(
1030 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001031
1032 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1033 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1034 const int kSampleRateHz = 8000;
1035 const size_t kPayloadLengthSamples =
1036 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1037 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1038 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001039 RTPHeader rtp_header;
1040 rtp_header.payloadType = kPayloadType;
1041 rtp_header.sequenceNumber = 0x1234;
1042 rtp_header.timestamp = 0x12345678;
1043 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001044
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001045 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001046 EXPECT_CALL(mock_decoder, SampleRateHz())
1047 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001048 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1049 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1050 .WillRepeatedly(Return(0));
1051 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001052 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001053 int16_t dummy_output[kPayloadLengthSamples] = {0};
1054 // The below expectation will make the mock decoder write
1055 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1056 // speech. That is, the decoded length is 5 samples shorter than the expected.
1057 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001058 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001059 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001060 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001061 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001062 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001063 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001064 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1065 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001066
1067 // Insert one packet.
1068 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001069 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001070
1071 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1072
1073 // Pull audio once.
1074 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001075 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001076 bool muted;
1077 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001078 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1079 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001080 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001081
1082 EXPECT_CALL(mock_decoder, Die());
1083}
minyuel6d92bf52015-09-23 15:20:39 +02001084
1085// This test checks the behavior of NetEq when audio decoder fails.
1086TEST_F(NetEqImplTest, DecodingError) {
1087 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001088 // Create a mock decoder object.
1089 MockAudioDecoder mock_decoder;
1090
1091 CreateInstance(
1092 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001093
1094 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1095 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1096 const int kSampleRateHz = 8000;
1097 const int kDecoderErrorCode = -97; // Any negative number.
1098
1099 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1100 const size_t kFrameLengthSamples =
1101 static_cast<size_t>(5 * kSampleRateHz / 1000);
1102
1103 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1104
1105 uint8_t payload[kPayloadLengthBytes] = {0};
1106
henrik.lundin246ef3e2017-04-24 09:14:32 -07001107 RTPHeader rtp_header;
1108 rtp_header.payloadType = kPayloadType;
1109 rtp_header.sequenceNumber = 0x1234;
1110 rtp_header.timestamp = 0x12345678;
1111 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001112
minyuel6d92bf52015-09-23 15:20:39 +02001113 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001114 EXPECT_CALL(mock_decoder, SampleRateHz())
1115 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001116 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1117 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1118 .WillRepeatedly(Return(0));
1119 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001120 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001121 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1122 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001123 int16_t dummy_output[kFrameLengthSamples] = {0};
1124
1125 {
1126 InSequence sequence; // Dummy variable.
1127 // Mock decoder works normally the first time.
1128 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001129 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001130 .Times(3)
1131 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001132 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001133 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001134 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001135 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001136 .RetiresOnSaturation();
1137
1138 // Then mock decoder fails. A common reason for failure can be buffer being
1139 // too short
1140 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001141 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001142 .WillOnce(Return(-1))
1143 .RetiresOnSaturation();
1144
1145 // Mock decoder finally returns to normal.
1146 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001147 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001148 .Times(2)
1149 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001150 DoAll(SetArrayArgument<3>(dummy_output,
1151 dummy_output + kFrameLengthSamples),
1152 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001153 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001154 }
1155
Niels Möllera1eb9c72018-12-07 15:24:42 +01001156 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1157 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001158
1159 // Insert packets.
1160 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001161 rtp_header.sequenceNumber += 1;
1162 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001163 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001164 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001165 }
1166
1167 // Pull audio.
1168 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001169 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001170 bool muted;
1171 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001172 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1173 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001174 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001175
1176 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001177 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001178 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1179 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001180 // We are not expecting anything for output.speech_type_, since an error was
1181 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001182
1183 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001184 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001185 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1186 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001187 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001188
1189 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001190 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001191 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1192 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001193 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001194
1195 EXPECT_CALL(mock_decoder, Die());
1196}
1197
1198// This test checks the behavior of NetEq when audio decoder fails during CNG.
1199TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1200 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001201
1202 // Create a mock decoder object.
1203 MockAudioDecoder mock_decoder;
1204 CreateInstance(
1205 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001206
1207 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1208 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1209 const int kSampleRateHz = 8000;
1210 const int kDecoderErrorCode = -97; // Any negative number.
1211
1212 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1213 const size_t kFrameLengthSamples =
1214 static_cast<size_t>(5 * kSampleRateHz / 1000);
1215
1216 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1217
1218 uint8_t payload[kPayloadLengthBytes] = {0};
1219
henrik.lundin246ef3e2017-04-24 09:14:32 -07001220 RTPHeader rtp_header;
1221 rtp_header.payloadType = kPayloadType;
1222 rtp_header.sequenceNumber = 0x1234;
1223 rtp_header.timestamp = 0x12345678;
1224 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001225
minyuel6d92bf52015-09-23 15:20:39 +02001226 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001227 EXPECT_CALL(mock_decoder, SampleRateHz())
1228 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001229 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1230 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1231 .WillRepeatedly(Return(0));
1232 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001233 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001234 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001235 int16_t dummy_output[kFrameLengthSamples] = {0};
1236
1237 {
1238 InSequence sequence; // Dummy variable.
1239 // Mock decoder works normally the first 2 times.
1240 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001241 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001242 .Times(2)
1243 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001244 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001245 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001246 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001247 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001248 .RetiresOnSaturation();
1249
1250 // Then mock decoder fails. A common reason for failure can be buffer being
1251 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001252 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001253 .WillOnce(Return(-1))
1254 .RetiresOnSaturation();
1255
1256 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001257 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001258 .Times(2)
1259 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001260 DoAll(SetArrayArgument<3>(dummy_output,
1261 dummy_output + kFrameLengthSamples),
1262 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001263 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001264 }
1265
Niels Möller50b66d52018-12-11 14:43:21 +01001266 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1267 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001268
1269 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1270 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001271 rtp_header.sequenceNumber += 1;
1272 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001273 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001274 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001275 }
1276
1277 // Pull audio.
1278 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001279 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001280 bool muted;
1281 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001282 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1283 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001284 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001285
1286 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001287 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001288 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1289 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001290 // We are not expecting anything for output.speech_type_, since an error was
1291 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001292
1293 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001294 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001295 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1296 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001297 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001298
1299 EXPECT_CALL(mock_decoder, Die());
1300}
1301
henrik.lundind89814b2015-11-23 06:49:25 -08001302// Tests that the return value from last_output_sample_rate_hz() is equal to the
1303// configured inital sample rate.
1304TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1305 UseNoMocks();
1306 config_.sample_rate_hz = 48000;
1307 CreateInstance();
1308 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1309}
1310
henrik.lundined497212016-04-25 10:11:38 -07001311TEST_F(NetEqImplTest, TickTimerIncrement) {
1312 UseNoMocks();
1313 CreateInstance();
1314 ASSERT_TRUE(tick_timer_);
1315 EXPECT_EQ(0u, tick_timer_->ticks());
1316 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001317 bool muted;
1318 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001319 EXPECT_EQ(1u, tick_timer_->ticks());
1320}
1321
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001322TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1323 UseNoMocks();
1324 use_mock_delay_manager_ = true;
1325 CreateInstance();
1326
1327 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1328 .WillOnce(Return(true))
1329 .WillOnce(Return(false));
1330
1331 const int delay_ms = 200;
1332
1333 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1334 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1335}
1336
1337TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1338 UseNoMocks();
1339 use_mock_delay_manager_ = true;
1340 CreateInstance();
1341
1342 const int delay_ms = 200;
1343
1344 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1345 .WillOnce(Return(delay_ms));
1346
1347 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1348}
1349
henrik.lundin114c1b32017-04-26 07:47:32 -07001350TEST_F(NetEqImplTest, TargetDelayMs) {
1351 UseNoMocks();
1352 use_mock_delay_manager_ = true;
1353 CreateInstance();
1354 // Let the dummy target delay be 17 packets.
1355 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1356 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1357 .WillOnce(Return(kTargetLevelPacketsQ8));
1358 // Default packet size before any packet has been decoded is 30 ms, so we are
1359 // expecting 17 * 30 = 510 ms target delay.
1360 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1361}
1362
henrik.lundinb8c55b12017-05-10 07:38:01 -07001363TEST_F(NetEqImplTest, InsertEmptyPacket) {
1364 UseNoMocks();
1365 use_mock_delay_manager_ = true;
1366 CreateInstance();
1367
1368 RTPHeader rtp_header;
1369 rtp_header.payloadType = 17;
1370 rtp_header.sequenceNumber = 0x1234;
1371 rtp_header.timestamp = 0x12345678;
1372 rtp_header.ssrc = 0x87654321;
1373
1374 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1375 neteq_->InsertEmptyPacket(rtp_header);
1376}
1377
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001378TEST_F(NetEqImplTest, EnableRtxHandling) {
1379 UseNoMocks();
1380 use_mock_delay_manager_ = true;
1381 config_.enable_rtx_handling = true;
1382 CreateInstance();
1383 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1384 .Times(1)
1385 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1386
1387 const int kPayloadLengthSamples = 80;
1388 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1389 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1390 const uint32_t kReceiveTime = 17;
1391 uint8_t payload[kPayloadLengthBytes] = {0};
1392 RTPHeader rtp_header;
1393 rtp_header.payloadType = kPayloadType;
1394 rtp_header.sequenceNumber = 0x1234;
1395 rtp_header.timestamp = 0x12345678;
1396 rtp_header.ssrc = 0x87654321;
1397
Niels Möller05543682019-01-10 16:55:06 +01001398 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1399 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001400 EXPECT_EQ(NetEq::kOK,
1401 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1402 AudioFrame output;
1403 bool muted;
1404 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1405
1406 // Insert second packet that was sent before the first packet.
1407 rtp_header.sequenceNumber -= 1;
1408 rtp_header.timestamp -= kPayloadLengthSamples;
1409 EXPECT_CALL(*mock_delay_manager_,
1410 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1411 EXPECT_EQ(NetEq::kOK,
1412 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1413}
1414
minyue5bd33972016-05-02 04:46:11 -07001415class Decoder120ms : public AudioDecoder {
1416 public:
kwiberg347d3512016-06-16 01:59:09 -07001417 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1418 : sample_rate_hz_(sample_rate_hz),
1419 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001420 speech_type_(speech_type) {}
1421
1422 int DecodeInternal(const uint8_t* encoded,
1423 size_t encoded_len,
1424 int sample_rate_hz,
1425 int16_t* decoded,
1426 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001427 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001428 size_t decoded_len =
1429 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1430 for (size_t i = 0; i < decoded_len; ++i) {
1431 decoded[i] = next_value_++;
1432 }
1433 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001434 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001435 }
1436
1437 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001438 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001439 size_t Channels() const override { return 2; }
1440
1441 private:
kwiberg347d3512016-06-16 01:59:09 -07001442 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001443 int16_t next_value_;
1444 SpeechType speech_type_;
1445};
1446
1447class NetEqImplTest120ms : public NetEqImplTest {
1448 protected:
1449 NetEqImplTest120ms() : NetEqImplTest() {}
1450 virtual ~NetEqImplTest120ms() {}
1451
1452 void CreateInstanceNoMocks() {
1453 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001454 CreateInstance(decoder_factory_);
1455 EXPECT_TRUE(neteq_->RegisterPayloadType(
1456 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001457 }
1458
1459 void CreateInstanceWithDelayManagerMock() {
1460 UseNoMocks();
1461 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001462 CreateInstance(decoder_factory_);
1463 EXPECT_TRUE(neteq_->RegisterPayloadType(
1464 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001465 }
1466
1467 uint32_t timestamp_diff_between_packets() const {
1468 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1469 }
1470
1471 uint32_t first_timestamp() const { return 10u; }
1472
1473 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001474 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001475 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001476 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1477 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001478 }
1479 }
1480
1481 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001482 RTPHeader rtp_header;
1483 rtp_header.payloadType = kPayloadType;
1484 rtp_header.sequenceNumber = sequence_number_;
1485 rtp_header.timestamp = timestamp;
1486 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001487 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1488 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001489 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001490 sequence_number_++;
1491 }
1492
1493 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001494 const uint32_t sampling_freq = kSamplingFreq_;
1495 decoder_factory_ =
1496 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1497 [sampling_freq, speech_type]() {
1498 std::unique_ptr<AudioDecoder> decoder =
1499 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1500 RTC_CHECK_EQ(2, decoder->Channels());
1501 return decoder;
1502 });
minyue5bd33972016-05-02 04:46:11 -07001503 }
1504
Niels Möllera0f44302018-11-30 10:45:12 +01001505 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001506 AudioFrame output_;
1507 const uint32_t kPayloadType = 17;
1508 const uint32_t kSamplingFreq_ = 48000;
1509 uint16_t sequence_number_ = 1;
1510};
1511
minyue5bd33972016-05-02 04:46:11 -07001512TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001513 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001514 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001515
1516 InsertPacket(first_timestamp());
1517 GetFirstPacket();
1518
henrik.lundin7a926812016-05-12 13:51:28 -07001519 bool muted;
1520 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001521 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1522}
1523
1524TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001525 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001526 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001527
1528 InsertPacket(first_timestamp());
1529 GetFirstPacket();
1530
1531 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1532}
1533
1534TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001535 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001536 CreateInstanceWithDelayManagerMock();
1537
minyue5bd33972016-05-02 04:46:11 -07001538 InsertPacket(first_timestamp());
1539
1540 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001541 bool muted;
1542 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001543
1544 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1545
1546 // Delay manager reports a target level which should cause a Merge.
1547 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1548
henrik.lundin7a926812016-05-12 13:51:28 -07001549 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001550 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1551}
1552
1553TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001554 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001555 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001556
1557 InsertPacket(first_timestamp());
1558 GetFirstPacket();
1559
henrik.lundin7a926812016-05-12 13:51:28 -07001560 bool muted;
1561 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001562 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1563}
1564
1565TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001566 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001567 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001568
1569 InsertPacket(first_timestamp());
1570 GetFirstPacket();
1571 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1572
1573 // Delay manager report buffer limit which should cause a FastAccelerate.
1574 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1575 .Times(1)
1576 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1577
henrik.lundin7a926812016-05-12 13:51:28 -07001578 bool muted;
1579 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001580 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1581}
1582
1583TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001584 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001585 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001586
1587 InsertPacket(first_timestamp());
1588 GetFirstPacket();
1589
1590 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1591
1592 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1593 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1594 .Times(1)
1595 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1596
henrik.lundin7a926812016-05-12 13:51:28 -07001597 bool muted;
1598 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001599 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1600}
1601
1602TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001603 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001604 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001605
1606 InsertPacket(first_timestamp());
1607 GetFirstPacket();
1608
1609 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1610
1611 // Delay manager report buffer limit which should cause a Accelerate.
1612 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1613 .Times(1)
1614 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1615
henrik.lundin7a926812016-05-12 13:51:28 -07001616 bool muted;
1617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001618 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1619}
1620
Ivo Creusen24192c22019-07-12 17:00:25 +02001621}// namespace webrtc