blob: e7552c10664e9e709a17decf25778dd667854271 [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 Ivarsson10403ae2018-11-27 15:45:20 +0100100 config_.max_packets_in_buffer, config_.min_delay_ms,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100101 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
102 absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700103 mock_delay_manager_ = mock.get();
104 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
105 deps.delay_manager = std::move(mock);
106 }
107 delay_manager_ = deps.delay_manager.get();
108
109 if (use_mock_dtmf_buffer_) {
110 std::unique_ptr<MockDtmfBuffer> mock(
111 new MockDtmfBuffer(config_.sample_rate_hz));
112 mock_dtmf_buffer_ = mock.get();
113 deps.dtmf_buffer = std::move(mock);
114 }
115 dtmf_buffer_ = deps.dtmf_buffer.get();
116
117 if (use_mock_dtmf_tone_generator_) {
118 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
119 mock_dtmf_tone_generator_ = mock.get();
120 deps.dtmf_tone_generator = std::move(mock);
121 }
122 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
123
124 if (use_mock_packet_buffer_) {
125 std::unique_ptr<MockPacketBuffer> mock(
126 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
127 mock_packet_buffer_ = mock.get();
128 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700129 }
130 packet_buffer_ = deps.packet_buffer.get();
131
132 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700133 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700135 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136 }
ossua70695a2016-09-22 02:06:28 -0700137 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700138
139 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
140 new TimestampScaler(*deps.decoder_database.get()));
141
142 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000143 ASSERT_TRUE(neteq_ != NULL);
144 }
145
Niels Möllera0f44302018-11-30 10:45:12 +0100146 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
147
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000148 void UseNoMocks() {
149 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
150 use_mock_buffer_level_filter_ = false;
151 use_mock_decoder_database_ = false;
152 use_mock_delay_peak_detector_ = false;
153 use_mock_delay_manager_ = false;
154 use_mock_dtmf_buffer_ = false;
155 use_mock_dtmf_tone_generator_ = false;
156 use_mock_packet_buffer_ = false;
157 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158 }
159
160 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000161 if (use_mock_buffer_level_filter_) {
162 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
163 }
164 if (use_mock_decoder_database_) {
165 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
166 }
167 if (use_mock_delay_manager_) {
168 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
169 }
170 if (use_mock_delay_peak_detector_) {
171 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
172 }
173 if (use_mock_dtmf_buffer_) {
174 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
175 }
176 if (use_mock_dtmf_tone_generator_) {
177 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
178 }
179 if (use_mock_packet_buffer_) {
180 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
181 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 }
183
Niels Möller05543682019-01-10 16:55:06 +0100184 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800185 const size_t kPayloadLength = 4;
186 const uint8_t kPayloadType = 110;
187 const uint32_t kReceiveTime = 17;
188 const int kSampleRateHz = 16000;
189 config_.sample_rate_hz = kSampleRateHz;
190 UseNoMocks();
191 CreateInstance();
192 // Event: 2, E bit, Volume: 17, Length: 4336.
193 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700194 RTPHeader rtp_header;
195 rtp_header.payloadType = kPayloadType;
196 rtp_header.sequenceNumber = 0x1234;
197 rtp_header.timestamp = 0x12345678;
198 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800199
Niels Möller05543682019-01-10 16:55:06 +0100200 EXPECT_TRUE(neteq_->RegisterPayloadType(
201 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800202
203 // Insert first packet.
204 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700205 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800206
207 // Pull audio once.
208 const size_t kMaxOutputSize =
209 static_cast<size_t>(10 * kSampleRateHz / 1000);
210 AudioFrame output;
211 bool muted;
212 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
213 ASSERT_FALSE(muted);
214 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
215 EXPECT_EQ(1u, output.num_channels_);
216 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
217
218 // Verify first 64 samples of actual output.
219 const std::vector<int16_t> kOutput({
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
221 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
222 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
223 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
224 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
225 -2534, -1163 });
226 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700227 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800228 }
229
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000231 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700232 TickTimer* tick_timer_ = nullptr;
233 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
234 BufferLevelFilter* buffer_level_filter_ = nullptr;
235 bool use_mock_buffer_level_filter_ = true;
236 MockDecoderDatabase* mock_decoder_database_ = nullptr;
237 DecoderDatabase* decoder_database_ = nullptr;
238 bool use_mock_decoder_database_ = true;
239 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
240 DelayPeakDetector* delay_peak_detector_ = nullptr;
241 bool use_mock_delay_peak_detector_ = true;
242 MockDelayManager* mock_delay_manager_ = nullptr;
243 DelayManager* delay_manager_ = nullptr;
244 bool use_mock_delay_manager_ = true;
245 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
246 DtmfBuffer* dtmf_buffer_ = nullptr;
247 bool use_mock_dtmf_buffer_ = true;
248 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
249 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
250 bool use_mock_dtmf_tone_generator_ = true;
251 MockPacketBuffer* mock_packet_buffer_ = nullptr;
252 PacketBuffer* packet_buffer_ = nullptr;
253 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700254 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
255 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700256 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257};
258
259
260// This tests the interface class NetEq.
261// TODO(hlundin): Move to separate file?
262TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000263 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700264 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 delete neteq;
266}
267
kwiberg5adaf732016-10-04 09:33:27 -0700268TEST_F(NetEqImplTest, RegisterPayloadType) {
269 CreateInstance();
270 constexpr int rtp_payload_type = 0;
271 const SdpAudioFormat format("pcmu", 8000, 1);
272 EXPECT_CALL(*mock_decoder_database_,
273 RegisterPayload(rtp_payload_type, format));
274 neteq_->RegisterPayloadType(rtp_payload_type, format);
275}
276
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000278 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000280 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200282 // Check that kOK is returned when database returns kDecoderNotFound, because
283 // removing a payload type that was never registered is not an error.
284 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285}
286
kwiberg6b19b562016-09-20 04:02:25 -0700287TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
288 CreateInstance();
289 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
290 neteq_->RemoveAllPayloadTypes();
291}
292
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000294 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000295 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 const uint8_t kPayloadType = 0;
297 const uint16_t kFirstSequenceNumber = 0x1234;
298 const uint32_t kFirstTimestamp = 0x12345678;
299 const uint32_t kSsrc = 0x87654321;
300 const uint32_t kFirstReceiveTime = 17;
301 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700302 RTPHeader rtp_header;
303 rtp_header.payloadType = kPayloadType;
304 rtp_header.sequenceNumber = kFirstSequenceNumber;
305 rtp_header.timestamp = kFirstTimestamp;
306 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700307 Packet fake_packet;
308 fake_packet.payload_type = kPayloadType;
309 fake_packet.sequence_number = kFirstSequenceNumber;
310 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311
kwibergc0f2dcf2016-05-31 06:28:03 -0700312 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
313 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100314 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800315 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200316 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800317 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700318 EXPECT_EQ("pcmu", format.name);
319
320 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
321 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
322 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
323 // BWE update function called with first packet.
324 EXPECT_CALL(*mock_decoder,
325 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
326 kFirstTimestamp, kFirstReceiveTime));
327 // BWE update function called with second packet.
328 EXPECT_CALL(
329 *mock_decoder,
330 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
331 kFirstTimestamp + 160, kFirstReceiveTime + 155));
332 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
333
334 *dec = std::move(mock_decoder);
335 }));
Niels Möller72899062019-01-11 09:36:13 +0100336 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
337 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338
339 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000340 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 .WillRepeatedly(Return(&info));
342
343 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000344 EXPECT_CALL(*mock_packet_buffer_, Empty())
345 .WillOnce(Return(false)); // Called once after first packet is inserted.
346 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200348 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100350 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
351 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
353 // index) is a pointer, and the variable pointed to is set to kPayloadType.
354 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
355 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700356 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000357 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700358 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000359
360 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000361 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 .Times(1);
363
364 // Expectations for delay manager.
365 {
366 // All expectations within this block must be called in this specific order.
367 InSequence sequence; // Dummy variable.
368 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000369 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 .Times(2)
371 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000372 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 .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.
397 const uint8_t kPayloadType = 17; // Just an arbitrary number.
398 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
689// This test verifies that NetEq can handle comfort noise and enters/quits codec
690// internal CNG mode properly.
691TEST_F(NetEqImplTest, CodecInternalCng) {
692 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100693 // Create a mock decoder object.
694 MockAudioDecoder mock_decoder;
695 CreateInstance(
696 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000697
698 const uint8_t kPayloadType = 17; // Just an arbitrary number.
699 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
700 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700701 const size_t kPayloadLengthSamples =
702 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
703 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000704 uint8_t payload[kPayloadLengthBytes] = {0};
705 int16_t dummy_output[kPayloadLengthSamples] = {0};
706
henrik.lundin246ef3e2017-04-24 09:14:32 -0700707 RTPHeader rtp_header;
708 rtp_header.payloadType = kPayloadType;
709 rtp_header.sequenceNumber = 0x1234;
710 rtp_header.timestamp = 0x12345678;
711 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000712
Karl Wiberg43766482015-08-27 15:22:11 +0200713 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700714 EXPECT_CALL(mock_decoder, SampleRateHz())
715 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000716 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000717 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
718 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700719 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200720 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700721 // Packed duration when asking the decoder for more CNG data (without a new
722 // packet).
723 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200724 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000725
726 // Pointee(x) verifies that first byte of the payload equals x, this makes it
727 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100728 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
729 kSampleRateKhz * 1000, _, _))
730 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000731 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100732 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200733 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000734
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100735 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
736 kSampleRateKhz * 1000, _, _))
737 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000738 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100739 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200740 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000741
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100742 EXPECT_CALL(mock_decoder,
743 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
744 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000745 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100746 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200747 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000748
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100749 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
750 kSampleRateKhz * 1000, _, _))
751 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000752 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100753 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200754 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000755
Niels Möller50b66d52018-12-11 14:43:21 +0100756 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
757 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000758
759 // Insert one packet (decoder will return speech).
760 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700761 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000762
763 // Insert second packet (decoder will return CNG).
764 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700765 rtp_header.sequenceNumber++;
766 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000767 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700768 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000769
Peter Kastingdce40cf2015-08-24 14:52:23 -0700770 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800771 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800772 AudioFrame::SpeechType expected_type[8] = {
773 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
774 AudioFrame::kCNG, AudioFrame::kCNG,
775 AudioFrame::kCNG, AudioFrame::kCNG,
776 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000777 };
778 int expected_timestamp_increment[8] = {
779 -1, // will not be used.
780 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700781 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
782 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000783 50 * kSampleRateKhz, 10 * kSampleRateKhz
784 };
785
henrik.lundin7a926812016-05-12 13:51:28 -0700786 bool muted;
787 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200788 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700789 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000790
henrik.lundin0d96ab72016-04-06 12:28:26 -0700791 // Lambda for verifying the timestamps.
792 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200793 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700794 if (expected_timestamp_increment[i] == -1) {
795 // Expect to get an empty timestamp value during CNG and PLC.
796 EXPECT_FALSE(ts) << "i = " << i;
797 } else {
798 ASSERT_TRUE(ts) << "i = " << i;
799 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
800 << "i = " << i;
801 last_timestamp = ts;
802 }
803 };
804
Peter Kastingdce40cf2015-08-24 14:52:23 -0700805 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800806 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
807 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800808 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700809 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700810 SCOPED_TRACE("");
811 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000812 }
813
814 // Insert third packet, which leaves a gap from last packet.
815 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700816 rtp_header.sequenceNumber += 2;
817 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000818 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700819 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820
Peter Kastingdce40cf2015-08-24 14:52:23 -0700821 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800822 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
823 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800824 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700825 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700826 SCOPED_TRACE("");
827 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000828 }
829
830 // Now check the packet buffer, and make sure it is empty.
831 EXPECT_TRUE(packet_buffer_->Empty());
832
833 EXPECT_CALL(mock_decoder, Die());
834}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000835
836TEST_F(NetEqImplTest, UnsupportedDecoder) {
837 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100838 ::testing::NiceMock<MockAudioDecoder> decoder;
839
840 CreateInstance(
841 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700842 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800843 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000844
845 const uint8_t kPayloadType = 17; // Just an arbitrary number.
846 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
847 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000848
Peter Kastingdce40cf2015-08-24 14:52:23 -0700849 const size_t kPayloadLengthSamples =
850 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000851 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700852 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200853 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700854 RTPHeader rtp_header;
855 rtp_header.payloadType = kPayloadType;
856 rtp_header.sequenceNumber = 0x1234;
857 rtp_header.timestamp = 0x12345678;
858 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000859
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000860 const uint8_t kFirstPayloadValue = 1;
861 const uint8_t kSecondPayloadValue = 2;
862
ossu61a208b2016-09-20 01:38:00 -0700863 EXPECT_CALL(decoder,
864 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
865 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200866 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000867
ossu61a208b2016-09-20 01:38:00 -0700868 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000869 .Times(0);
870
ossu61a208b2016-09-20 01:38:00 -0700871 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
872 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000873 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700874 .WillOnce(DoAll(
875 SetArrayArgument<3>(dummy_output,
876 dummy_output + kPayloadLengthSamples * kChannels),
877 SetArgPointee<4>(AudioDecoder::kSpeech),
878 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000879
ossu61a208b2016-09-20 01:38:00 -0700880 EXPECT_CALL(decoder,
881 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
882 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200883 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700884
885 EXPECT_CALL(decoder, SampleRateHz())
886 .WillRepeatedly(Return(kSampleRateHz));
887
888 EXPECT_CALL(decoder, Channels())
889 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000890
Niels Möllera1eb9c72018-12-07 15:24:42 +0100891 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
892 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000893
894 // Insert one packet.
895 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
896 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700897 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000898
899 // Insert another packet.
900 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700901 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000902 // The second timestamp needs to be at least 30 ms after the first to make
903 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700906 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000907
henrik.lundin6d8e0112016-03-04 10:34:21 -0800908 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700909 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800910 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200911 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700912 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800913 // Output size and number of channels should be correct.
914 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
915 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
916 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000917
henrik.lundin6d8e0112016-03-04 10:34:21 -0800918 // Second call to GetAudio will decode the packet that is ok. No errors are
919 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700920 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800921 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
922 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700923
924 // Die isn't called through NiceMock (since it's called by the
925 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
926 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000927}
928
henrik.lundin116c84e2015-08-27 13:14:48 -0700929// This test inserts packets until the buffer is flushed. After that, it asks
930// NetEq for the network statistics. The purpose of the test is to make sure
931// that even though the buffer size increment is negative (which it becomes when
932// the packet causing a flush is inserted), the packet length stored in the
933// decision logic remains valid.
934TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
935 UseNoMocks();
936 CreateInstance();
937
938 const size_t kPayloadLengthSamples = 80;
939 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
940 const uint8_t kPayloadType = 17; // Just an arbitrary number.
941 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
942 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700943 RTPHeader rtp_header;
944 rtp_header.payloadType = kPayloadType;
945 rtp_header.sequenceNumber = 0x1234;
946 rtp_header.timestamp = 0x12345678;
947 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700948
Niels Möller05543682019-01-10 16:55:06 +0100949 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
950 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -0700951
952 // Insert packets until the buffer flushes.
953 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
954 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
955 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700956 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
957 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
958 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700959 }
960 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
961
962 // Ask for network statistics. This should not crash.
963 NetEqNetworkStatistics stats;
964 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
965}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200966
967TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
968 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100969 // Create a mock decoder object.
970 MockAudioDecoder mock_decoder;
971
972 CreateInstance(
973 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200974
975 const uint8_t kPayloadType = 17; // Just an arbitrary number.
976 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
977 const int kSampleRateHz = 8000;
978 const size_t kPayloadLengthSamples =
979 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
980 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
981 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700982 RTPHeader rtp_header;
983 rtp_header.payloadType = kPayloadType;
984 rtp_header.sequenceNumber = 0x1234;
985 rtp_header.timestamp = 0x12345678;
986 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200987
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200988 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700989 EXPECT_CALL(mock_decoder, SampleRateHz())
990 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200991 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
992 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
993 .WillRepeatedly(Return(0));
994 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200995 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200996 int16_t dummy_output[kPayloadLengthSamples] = {0};
997 // The below expectation will make the mock decoder write
998 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
999 // speech. That is, the decoded length is 5 samples shorter than the expected.
1000 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001001 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001002 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001003 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001004 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001005 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001006 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001007 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1008 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001009
1010 // Insert one packet.
1011 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001012 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001013
1014 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1015
1016 // Pull audio once.
1017 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001018 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001019 bool muted;
1020 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001021 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1022 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001023 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001024
1025 EXPECT_CALL(mock_decoder, Die());
1026}
minyuel6d92bf52015-09-23 15:20:39 +02001027
1028// This test checks the behavior of NetEq when audio decoder fails.
1029TEST_F(NetEqImplTest, DecodingError) {
1030 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001031 // Create a mock decoder object.
1032 MockAudioDecoder mock_decoder;
1033
1034 CreateInstance(
1035 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001036
1037 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1038 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1039 const int kSampleRateHz = 8000;
1040 const int kDecoderErrorCode = -97; // Any negative number.
1041
1042 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1043 const size_t kFrameLengthSamples =
1044 static_cast<size_t>(5 * kSampleRateHz / 1000);
1045
1046 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1047
1048 uint8_t payload[kPayloadLengthBytes] = {0};
1049
henrik.lundin246ef3e2017-04-24 09:14:32 -07001050 RTPHeader rtp_header;
1051 rtp_header.payloadType = kPayloadType;
1052 rtp_header.sequenceNumber = 0x1234;
1053 rtp_header.timestamp = 0x12345678;
1054 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001055
minyuel6d92bf52015-09-23 15:20:39 +02001056 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001057 EXPECT_CALL(mock_decoder, SampleRateHz())
1058 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001059 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1060 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1061 .WillRepeatedly(Return(0));
1062 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001063 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001064 EXPECT_CALL(mock_decoder, ErrorCode())
1065 .WillOnce(Return(kDecoderErrorCode));
1066 EXPECT_CALL(mock_decoder, HasDecodePlc())
1067 .WillOnce(Return(false));
1068 int16_t dummy_output[kFrameLengthSamples] = {0};
1069
1070 {
1071 InSequence sequence; // Dummy variable.
1072 // Mock decoder works normally the first time.
1073 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001074 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001075 .Times(3)
1076 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001077 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001078 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001079 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001080 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001081 .RetiresOnSaturation();
1082
1083 // Then mock decoder fails. A common reason for failure can be buffer being
1084 // too short
1085 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001086 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001087 .WillOnce(Return(-1))
1088 .RetiresOnSaturation();
1089
1090 // Mock decoder finally returns to normal.
1091 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001092 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001093 .Times(2)
1094 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001095 DoAll(SetArrayArgument<3>(dummy_output,
1096 dummy_output + kFrameLengthSamples),
1097 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001098 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001099 }
1100
Niels Möllera1eb9c72018-12-07 15:24:42 +01001101 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1102 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001103
1104 // Insert packets.
1105 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001106 rtp_header.sequenceNumber += 1;
1107 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001108 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001109 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001110 }
1111
1112 // Pull audio.
1113 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001114 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001115 bool muted;
1116 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001117 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1118 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001119 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001120
1121 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001122 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001123 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1124 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001125 // We are not expecting anything for output.speech_type_, since an error was
1126 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001127
1128 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001129 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001130 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1131 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001132 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001133
1134 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001135 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001136 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1137 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001138 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001139
1140 EXPECT_CALL(mock_decoder, Die());
1141}
1142
1143// This test checks the behavior of NetEq when audio decoder fails during CNG.
1144TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1145 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001146
1147 // Create a mock decoder object.
1148 MockAudioDecoder mock_decoder;
1149 CreateInstance(
1150 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001151
1152 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1153 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1154 const int kSampleRateHz = 8000;
1155 const int kDecoderErrorCode = -97; // Any negative number.
1156
1157 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1158 const size_t kFrameLengthSamples =
1159 static_cast<size_t>(5 * kSampleRateHz / 1000);
1160
1161 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1162
1163 uint8_t payload[kPayloadLengthBytes] = {0};
1164
henrik.lundin246ef3e2017-04-24 09:14:32 -07001165 RTPHeader rtp_header;
1166 rtp_header.payloadType = kPayloadType;
1167 rtp_header.sequenceNumber = 0x1234;
1168 rtp_header.timestamp = 0x12345678;
1169 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001170
minyuel6d92bf52015-09-23 15:20:39 +02001171 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001172 EXPECT_CALL(mock_decoder, SampleRateHz())
1173 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001174 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1175 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1176 .WillRepeatedly(Return(0));
1177 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001178 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001179 EXPECT_CALL(mock_decoder, ErrorCode())
1180 .WillOnce(Return(kDecoderErrorCode));
1181 int16_t dummy_output[kFrameLengthSamples] = {0};
1182
1183 {
1184 InSequence sequence; // Dummy variable.
1185 // Mock decoder works normally the first 2 times.
1186 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001187 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001188 .Times(2)
1189 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001190 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001191 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001192 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001193 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001194 .RetiresOnSaturation();
1195
1196 // Then mock decoder fails. A common reason for failure can be buffer being
1197 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001198 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001199 .WillOnce(Return(-1))
1200 .RetiresOnSaturation();
1201
1202 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001203 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001204 .Times(2)
1205 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001206 DoAll(SetArrayArgument<3>(dummy_output,
1207 dummy_output + kFrameLengthSamples),
1208 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001209 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001210 }
1211
Niels Möller50b66d52018-12-11 14:43:21 +01001212 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1213 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001214
1215 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1216 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001217 rtp_header.sequenceNumber += 1;
1218 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001219 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001220 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001221 }
1222
1223 // Pull audio.
1224 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001225 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001226 bool muted;
1227 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001228 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1229 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001230 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001231
1232 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001233 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001234 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1235 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001236 // We are not expecting anything for output.speech_type_, since an error was
1237 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001238
1239 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001240 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001241 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1242 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001243 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001244
1245 EXPECT_CALL(mock_decoder, Die());
1246}
1247
henrik.lundind89814b2015-11-23 06:49:25 -08001248// Tests that the return value from last_output_sample_rate_hz() is equal to the
1249// configured inital sample rate.
1250TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1251 UseNoMocks();
1252 config_.sample_rate_hz = 48000;
1253 CreateInstance();
1254 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1255}
1256
henrik.lundined497212016-04-25 10:11:38 -07001257TEST_F(NetEqImplTest, TickTimerIncrement) {
1258 UseNoMocks();
1259 CreateInstance();
1260 ASSERT_TRUE(tick_timer_);
1261 EXPECT_EQ(0u, tick_timer_->ticks());
1262 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001263 bool muted;
1264 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001265 EXPECT_EQ(1u, tick_timer_->ticks());
1266}
1267
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001268TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1269 UseNoMocks();
1270 use_mock_delay_manager_ = true;
1271 CreateInstance();
1272
1273 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1274 .WillOnce(Return(true))
1275 .WillOnce(Return(false));
1276
1277 const int delay_ms = 200;
1278
1279 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1280 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1281}
1282
1283TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1284 UseNoMocks();
1285 use_mock_delay_manager_ = true;
1286 CreateInstance();
1287
1288 const int delay_ms = 200;
1289
1290 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1291 .WillOnce(Return(delay_ms));
1292
1293 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1294}
1295
henrik.lundin114c1b32017-04-26 07:47:32 -07001296TEST_F(NetEqImplTest, TargetDelayMs) {
1297 UseNoMocks();
1298 use_mock_delay_manager_ = true;
1299 CreateInstance();
1300 // Let the dummy target delay be 17 packets.
1301 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1302 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1303 .WillOnce(Return(kTargetLevelPacketsQ8));
1304 // Default packet size before any packet has been decoded is 30 ms, so we are
1305 // expecting 17 * 30 = 510 ms target delay.
1306 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1307}
1308
henrik.lundinb8c55b12017-05-10 07:38:01 -07001309TEST_F(NetEqImplTest, InsertEmptyPacket) {
1310 UseNoMocks();
1311 use_mock_delay_manager_ = true;
1312 CreateInstance();
1313
1314 RTPHeader rtp_header;
1315 rtp_header.payloadType = 17;
1316 rtp_header.sequenceNumber = 0x1234;
1317 rtp_header.timestamp = 0x12345678;
1318 rtp_header.ssrc = 0x87654321;
1319
1320 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1321 neteq_->InsertEmptyPacket(rtp_header);
1322}
1323
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001324TEST_F(NetEqImplTest, EnableRtxHandling) {
1325 UseNoMocks();
1326 use_mock_delay_manager_ = true;
1327 config_.enable_rtx_handling = true;
1328 CreateInstance();
1329 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1330 .Times(1)
1331 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1332
1333 const int kPayloadLengthSamples = 80;
1334 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1335 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1336 const uint32_t kReceiveTime = 17;
1337 uint8_t payload[kPayloadLengthBytes] = {0};
1338 RTPHeader rtp_header;
1339 rtp_header.payloadType = kPayloadType;
1340 rtp_header.sequenceNumber = 0x1234;
1341 rtp_header.timestamp = 0x12345678;
1342 rtp_header.ssrc = 0x87654321;
1343
Niels Möller05543682019-01-10 16:55:06 +01001344 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1345 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001346 EXPECT_EQ(NetEq::kOK,
1347 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1348 AudioFrame output;
1349 bool muted;
1350 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1351
1352 // Insert second packet that was sent before the first packet.
1353 rtp_header.sequenceNumber -= 1;
1354 rtp_header.timestamp -= kPayloadLengthSamples;
1355 EXPECT_CALL(*mock_delay_manager_,
1356 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1357 EXPECT_EQ(NetEq::kOK,
1358 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1359}
1360
minyue5bd33972016-05-02 04:46:11 -07001361class Decoder120ms : public AudioDecoder {
1362 public:
kwiberg347d3512016-06-16 01:59:09 -07001363 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1364 : sample_rate_hz_(sample_rate_hz),
1365 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001366 speech_type_(speech_type) {}
1367
1368 int DecodeInternal(const uint8_t* encoded,
1369 size_t encoded_len,
1370 int sample_rate_hz,
1371 int16_t* decoded,
1372 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001373 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001374 size_t decoded_len =
1375 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1376 for (size_t i = 0; i < decoded_len; ++i) {
1377 decoded[i] = next_value_++;
1378 }
1379 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001380 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001381 }
1382
1383 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001384 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001385 size_t Channels() const override { return 2; }
1386
1387 private:
kwiberg347d3512016-06-16 01:59:09 -07001388 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001389 int16_t next_value_;
1390 SpeechType speech_type_;
1391};
1392
1393class NetEqImplTest120ms : public NetEqImplTest {
1394 protected:
1395 NetEqImplTest120ms() : NetEqImplTest() {}
1396 virtual ~NetEqImplTest120ms() {}
1397
1398 void CreateInstanceNoMocks() {
1399 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001400 CreateInstance(decoder_factory_);
1401 EXPECT_TRUE(neteq_->RegisterPayloadType(
1402 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001403 }
1404
1405 void CreateInstanceWithDelayManagerMock() {
1406 UseNoMocks();
1407 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001408 CreateInstance(decoder_factory_);
1409 EXPECT_TRUE(neteq_->RegisterPayloadType(
1410 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001411 }
1412
1413 uint32_t timestamp_diff_between_packets() const {
1414 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1415 }
1416
1417 uint32_t first_timestamp() const { return 10u; }
1418
1419 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001420 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001421 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001422 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1423 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001424 }
1425 }
1426
1427 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001428 RTPHeader rtp_header;
1429 rtp_header.payloadType = kPayloadType;
1430 rtp_header.sequenceNumber = sequence_number_;
1431 rtp_header.timestamp = timestamp;
1432 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001433 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1434 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001435 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001436 sequence_number_++;
1437 }
1438
1439 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001440 const uint32_t sampling_freq = kSamplingFreq_;
1441 decoder_factory_ =
1442 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1443 [sampling_freq, speech_type]() {
1444 std::unique_ptr<AudioDecoder> decoder =
1445 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1446 RTC_CHECK_EQ(2, decoder->Channels());
1447 return decoder;
1448 });
minyue5bd33972016-05-02 04:46:11 -07001449 }
1450
Niels Möllera0f44302018-11-30 10:45:12 +01001451 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001452 AudioFrame output_;
1453 const uint32_t kPayloadType = 17;
1454 const uint32_t kSamplingFreq_ = 48000;
1455 uint16_t sequence_number_ = 1;
1456};
1457
minyue5bd33972016-05-02 04:46:11 -07001458TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001459 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001460 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001461
1462 InsertPacket(first_timestamp());
1463 GetFirstPacket();
1464
henrik.lundin7a926812016-05-12 13:51:28 -07001465 bool muted;
1466 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001467 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1468}
1469
1470TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001471 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001472 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001473
1474 InsertPacket(first_timestamp());
1475 GetFirstPacket();
1476
1477 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1478}
1479
1480TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001481 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001482 CreateInstanceWithDelayManagerMock();
1483
minyue5bd33972016-05-02 04:46:11 -07001484 InsertPacket(first_timestamp());
1485
1486 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001487 bool muted;
1488 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001489
1490 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1491
1492 // Delay manager reports a target level which should cause a Merge.
1493 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1494
henrik.lundin7a926812016-05-12 13:51:28 -07001495 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001496 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1497}
1498
1499TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001500 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001501 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001502
1503 InsertPacket(first_timestamp());
1504 GetFirstPacket();
1505
henrik.lundin7a926812016-05-12 13:51:28 -07001506 bool muted;
1507 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001508 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1509}
1510
1511TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001512 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001513 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001514
1515 InsertPacket(first_timestamp());
1516 GetFirstPacket();
1517 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1518
1519 // Delay manager report buffer limit which should cause a FastAccelerate.
1520 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1521 .Times(1)
1522 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1523
henrik.lundin7a926812016-05-12 13:51:28 -07001524 bool muted;
1525 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001526 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1527}
1528
1529TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001530 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001531 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001532
1533 InsertPacket(first_timestamp());
1534 GetFirstPacket();
1535
1536 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1537
1538 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1539 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1540 .Times(1)
1541 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1542
henrik.lundin7a926812016-05-12 13:51:28 -07001543 bool muted;
1544 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001545 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1546}
1547
1548TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001549 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001550 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001551
1552 InsertPacket(first_timestamp());
1553 GetFirstPacket();
1554
1555 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1556
1557 // Delay manager report buffer limit which should cause a Accelerate.
1558 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1559 .Times(1)
1560 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1561
henrik.lundin7a926812016-05-12 13:51:28 -07001562 bool muted;
1563 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001564 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1565}
1566
minyuel6d92bf52015-09-23 15:20:39 +02001567}// namespace webrtc