blob: 61282698f153c6c327e929d2b01885ac34a90345 [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 Bonadei71207422017-09-15 13:58:09 +020015#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/neteq/accelerate.h"
17#include "modules/audio_coding/neteq/expand.h"
18#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(
91 new MockDelayPeakDetector(tick_timer_));
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,
101 delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700102 mock_delay_manager_ = mock.get();
103 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
104 deps.delay_manager = std::move(mock);
105 }
106 delay_manager_ = deps.delay_manager.get();
107
108 if (use_mock_dtmf_buffer_) {
109 std::unique_ptr<MockDtmfBuffer> mock(
110 new MockDtmfBuffer(config_.sample_rate_hz));
111 mock_dtmf_buffer_ = mock.get();
112 deps.dtmf_buffer = std::move(mock);
113 }
114 dtmf_buffer_ = deps.dtmf_buffer.get();
115
116 if (use_mock_dtmf_tone_generator_) {
117 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
118 mock_dtmf_tone_generator_ = mock.get();
119 deps.dtmf_tone_generator = std::move(mock);
120 }
121 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
122
123 if (use_mock_packet_buffer_) {
124 std::unique_ptr<MockPacketBuffer> mock(
125 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
126 mock_packet_buffer_ = mock.get();
127 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700128 }
129 packet_buffer_ = deps.packet_buffer.get();
130
131 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700132 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700133 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700134 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700135 }
ossua70695a2016-09-22 02:06:28 -0700136 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137
138 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
139 new TimestampScaler(*deps.decoder_database.get()));
140
141 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000142 ASSERT_TRUE(neteq_ != NULL);
143 }
144
Niels Möllera0f44302018-11-30 10:45:12 +0100145 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
146
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000147 void UseNoMocks() {
148 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
149 use_mock_buffer_level_filter_ = false;
150 use_mock_decoder_database_ = false;
151 use_mock_delay_peak_detector_ = false;
152 use_mock_delay_manager_ = false;
153 use_mock_dtmf_buffer_ = false;
154 use_mock_dtmf_tone_generator_ = false;
155 use_mock_packet_buffer_ = false;
156 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157 }
158
159 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000160 if (use_mock_buffer_level_filter_) {
161 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
162 }
163 if (use_mock_decoder_database_) {
164 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
165 }
166 if (use_mock_delay_manager_) {
167 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
168 }
169 if (use_mock_delay_peak_detector_) {
170 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
171 }
172 if (use_mock_dtmf_buffer_) {
173 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
174 }
175 if (use_mock_dtmf_tone_generator_) {
176 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
177 }
178 if (use_mock_packet_buffer_) {
179 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
180 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 }
182
solenberg2779bab2016-11-17 04:45:19 -0800183 void TestDtmfPacket(NetEqDecoder decoder_type) {
184 const size_t kPayloadLength = 4;
185 const uint8_t kPayloadType = 110;
186 const uint32_t kReceiveTime = 17;
187 const int kSampleRateHz = 16000;
188 config_.sample_rate_hz = kSampleRateHz;
189 UseNoMocks();
190 CreateInstance();
191 // Event: 2, E bit, Volume: 17, Length: 4336.
192 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700193 RTPHeader rtp_header;
194 rtp_header.payloadType = kPayloadType;
195 rtp_header.sequenceNumber = 0x1234;
196 rtp_header.timestamp = 0x12345678;
197 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800198
199 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
200 decoder_type, "telephone-event", kPayloadType));
201
202 // Insert first packet.
203 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700204 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800205
206 // Pull audio once.
207 const size_t kMaxOutputSize =
208 static_cast<size_t>(10 * kSampleRateHz / 1000);
209 AudioFrame output;
210 bool muted;
211 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
212 ASSERT_FALSE(muted);
213 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
214 EXPECT_EQ(1u, output.num_channels_);
215 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
216
217 // Verify first 64 samples of actual output.
218 const std::vector<int16_t> kOutput({
219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
220 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
221 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
222 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
223 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
224 -2534, -1163 });
225 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700226 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800227 }
228
henrik.lundin1d9061e2016-04-26 12:19:34 -0700229 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000230 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700231 TickTimer* tick_timer_ = nullptr;
232 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
233 BufferLevelFilter* buffer_level_filter_ = nullptr;
234 bool use_mock_buffer_level_filter_ = true;
235 MockDecoderDatabase* mock_decoder_database_ = nullptr;
236 DecoderDatabase* decoder_database_ = nullptr;
237 bool use_mock_decoder_database_ = true;
238 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
239 DelayPeakDetector* delay_peak_detector_ = nullptr;
240 bool use_mock_delay_peak_detector_ = true;
241 MockDelayManager* mock_delay_manager_ = nullptr;
242 DelayManager* delay_manager_ = nullptr;
243 bool use_mock_delay_manager_ = true;
244 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
245 DtmfBuffer* dtmf_buffer_ = nullptr;
246 bool use_mock_dtmf_buffer_ = true;
247 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
248 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
249 bool use_mock_dtmf_tone_generator_ = true;
250 MockPacketBuffer* mock_packet_buffer_ = nullptr;
251 PacketBuffer* packet_buffer_ = nullptr;
252 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700253 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
254 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700255 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256};
257
258
259// This tests the interface class NetEq.
260// TODO(hlundin): Move to separate file?
261TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000262 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700263 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000264 delete neteq;
265}
266
kwiberg5adaf732016-10-04 09:33:27 -0700267TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000268 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700270 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800271 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000272 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800273 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
274 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275}
276
kwiberg5adaf732016-10-04 09:33:27 -0700277TEST_F(NetEqImplTest, RegisterPayloadType) {
278 CreateInstance();
279 constexpr int rtp_payload_type = 0;
280 const SdpAudioFormat format("pcmu", 8000, 1);
281 EXPECT_CALL(*mock_decoder_database_,
282 RegisterPayload(rtp_payload_type, format));
283 neteq_->RegisterPayloadType(rtp_payload_type, format);
284}
285
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000287 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000289 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200291 // Check that kOK is returned when database returns kDecoderNotFound, because
292 // removing a payload type that was never registered is not an error.
293 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294}
295
kwiberg6b19b562016-09-20 04:02:25 -0700296TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
297 CreateInstance();
298 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
299 neteq_->RemoveAllPayloadTypes();
300}
301
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000302TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000303 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000304 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305 const uint8_t kPayloadType = 0;
306 const uint16_t kFirstSequenceNumber = 0x1234;
307 const uint32_t kFirstTimestamp = 0x12345678;
308 const uint32_t kSsrc = 0x87654321;
309 const uint32_t kFirstReceiveTime = 17;
310 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700311 RTPHeader rtp_header;
312 rtp_header.payloadType = kPayloadType;
313 rtp_header.sequenceNumber = kFirstSequenceNumber;
314 rtp_header.timestamp = kFirstTimestamp;
315 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700316 Packet fake_packet;
317 fake_packet.payload_type = kPayloadType;
318 fake_packet.sequence_number = kFirstSequenceNumber;
319 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320
kwibergc0f2dcf2016-05-31 06:28:03 -0700321 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
322 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100323 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800324 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200325 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800326 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700327 EXPECT_EQ("pcmu", format.name);
328
329 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
330 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
331 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
332 // BWE update function called with first packet.
333 EXPECT_CALL(*mock_decoder,
334 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
335 kFirstTimestamp, kFirstReceiveTime));
336 // BWE update function called with second packet.
337 EXPECT_CALL(
338 *mock_decoder,
339 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
340 kFirstTimestamp + 160, kFirstReceiveTime + 155));
341 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
342
343 *dec = std::move(mock_decoder);
344 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200345 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, absl::nullopt,
ossu84bc9852016-08-26 05:41:23 -0700346 mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347
348 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000349 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 .WillRepeatedly(Return(&info));
351
352 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000353 EXPECT_CALL(*mock_packet_buffer_, Empty())
354 .WillOnce(Return(false)); // Called once after first packet is inserted.
355 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200357 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100359 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
360 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
362 // index) is a pointer, and the variable pointed to is set to kPayloadType.
363 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
364 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700365 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000366 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700367 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368
369 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000370 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 .Times(1);
372
373 // Expectations for delay manager.
374 {
375 // All expectations within this block must be called in this specific order.
376 InSequence sequence; // Dummy variable.
377 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000378 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 .Times(2)
380 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000381 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000383 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000385 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
386 .WillOnce(Return(0));
387 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 .WillOnce(Return(0));
389 }
390
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700392 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393
394 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700395 rtp_header.timestamp += 160;
396 rtp_header.sequenceNumber += 1;
397 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398}
399
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000400TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
401 UseNoMocks();
402 CreateInstance();
403
404 const int kPayloadLengthSamples = 80;
405 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
406 const uint8_t kPayloadType = 17; // Just an arbitrary number.
407 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
408 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700409 RTPHeader rtp_header;
410 rtp_header.payloadType = kPayloadType;
411 rtp_header.sequenceNumber = 0x1234;
412 rtp_header.timestamp = 0x12345678;
413 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000414
kwibergee1879c2015-10-29 06:20:28 -0700415 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800416 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000417
418 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700419 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000420 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700421 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
422 rtp_header.timestamp += kPayloadLengthSamples;
423 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000424 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
425 }
426
427 // Insert one more packet and make sure the buffer got flushed. That is, it
428 // should only hold one single packet.
429 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700430 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700431 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700432 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700433 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
434 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000435}
436
solenberg2779bab2016-11-17 04:45:19 -0800437TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
438 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
439}
solenberg99df6c02016-10-11 04:35:34 -0700440
solenberg2779bab2016-11-17 04:45:19 -0800441TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
442 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
443}
solenberg99df6c02016-10-11 04:35:34 -0700444
solenberg2779bab2016-11-17 04:45:19 -0800445TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
446 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
447}
solenberg99df6c02016-10-11 04:35:34 -0700448
solenberg2779bab2016-11-17 04:45:19 -0800449TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
450 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700451}
452
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000453// This test verifies that timestamps propagate from the incoming packets
454// through to the sync buffer and to the playout timestamp.
455TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000456 const uint8_t kPayloadType = 17; // Just an arbitrary number.
457 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
458 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700459 const size_t kPayloadLengthSamples =
460 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000461 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
462 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700463 RTPHeader rtp_header;
464 rtp_header.payloadType = kPayloadType;
465 rtp_header.sequenceNumber = 0x1234;
466 rtp_header.timestamp = 0x12345678;
467 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000468
469 // This is a dummy decoder that produces as many output samples as the input
470 // has bytes. The output is an increasing series, starting at 1 for the first
471 // sample, and then increasing by 1 for each sample.
472 class CountingSamplesDecoder : public AudioDecoder {
473 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000474 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475
476 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100477 int DecodeInternal(const uint8_t* encoded,
478 size_t encoded_len,
479 int /* sample_rate_hz */,
480 int16_t* decoded,
481 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482 for (size_t i = 0; i < encoded_len; ++i) {
483 decoded[i] = next_value_++;
484 }
485 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200486 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000487 }
488
Karl Wiberg43766482015-08-27 15:22:11 +0200489 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000490
kwiberg347d3512016-06-16 01:59:09 -0700491 int SampleRateHz() const override { return kSampleRateHz; }
492
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000493 size_t Channels() const override { return 1; }
494
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000495 uint16_t next_value() const { return next_value_; }
496
497 private:
498 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000499 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
Niels Möllerb7180c02018-12-06 13:07:11 +0100501 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
502 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
503
504 UseNoMocks();
505 CreateInstance(decoder_factory);
506
507 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
508 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000509
510 // Insert one packet.
511 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700512 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000513
514 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700515 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800516 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700517 bool muted;
518 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
519 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800520 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
521 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800522 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000523
524 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700525 EXPECT_EQ(kPayloadLengthSamples,
526 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527
528 // The value of the last of the output samples is the same as the number of
529 // samples played from the decoded packet. Thus, this number + the RTP
530 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200531 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700532 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200533 absl::optional<uint32_t>(rtp_header.timestamp +
534 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700535 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000536
537 // Check the timestamp for the last value in the sync buffer. This should
538 // be one full frame length ahead of the RTP timestamp.
539 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
540 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700541 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000542 sync_buffer->end_timestamp());
543
544 // Check that the number of samples still to play from the sync buffer add
545 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800546 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700547 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800548 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000549}
550
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000551TEST_F(NetEqImplTest, ReorderedPacket) {
552 UseNoMocks();
553 CreateInstance();
554
555 const uint8_t kPayloadType = 17; // Just an arbitrary number.
556 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
557 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700558 const size_t kPayloadLengthSamples =
559 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000560 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
561 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700562 RTPHeader rtp_header;
563 rtp_header.payloadType = kPayloadType;
564 rtp_header.sequenceNumber = 0x1234;
565 rtp_header.timestamp = 0x12345678;
566 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000567
568 // Create a mock decoder object.
569 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200570 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700571 EXPECT_CALL(mock_decoder, SampleRateHz())
572 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000573 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000574 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
575 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700576 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200577 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000578 int16_t dummy_output[kPayloadLengthSamples] = {0};
579 // The below expectation will make the mock decoder write
580 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100581 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
582 kSampleRateHz, _, _))
583 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100585 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200586 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
kwibergee1879c2015-10-29 06:20:28 -0700587 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
588 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700589 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000590
591 // Insert one packet.
592 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700593 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000594
595 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700596 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800597 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700598 bool muted;
599 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800600 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
601 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800602 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603
604 // Insert two more packets. The first one is out of order, and is already too
605 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700606 rtp_header.sequenceNumber -= 1;
607 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608 payload[0] = 1;
609 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700610 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
611 rtp_header.sequenceNumber += 2;
612 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000613 payload[0] = 2;
614 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700615 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616
617 // Expect only the second packet to be decoded (the one with "2" as the first
618 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100619 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
620 kSampleRateHz, _, _))
621 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000622 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100623 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200624 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000625
626 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700627 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800628 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
629 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800630 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000631
632 // Now check the packet buffer, and make sure it is empty, since the
633 // out-of-order packet should have been discarded.
634 EXPECT_TRUE(packet_buffer_->Empty());
635
636 EXPECT_CALL(mock_decoder, Die());
637}
638
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000639// This test verifies that NetEq can handle the situation where the first
640// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000641TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000642 UseNoMocks();
643 CreateInstance();
644
645 const uint8_t kPayloadType = 17; // Just an arbitrary number.
646 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
647 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700648 const size_t kPayloadLengthSamples =
649 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700650 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000651 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700652 RTPHeader rtp_header;
653 rtp_header.payloadType = kPayloadType;
654 rtp_header.sequenceNumber = 0x1234;
655 rtp_header.timestamp = 0x12345678;
656 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000657
658 // Insert one packet. Note that we have not registered any payload type, so
659 // this packet will be rejected.
660 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700661 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000662
663 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700664 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800665 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700666 bool muted;
667 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800668 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
669 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
670 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800671 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000672
673 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700674 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800675 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000676
677 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700679 rtp_header.sequenceNumber++;
680 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000681 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700682 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000683 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
684 }
685
686 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700687 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700688 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800689 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
690 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
691 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800692 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000693 << "NetEq did not decode the packets as expected.";
694 }
695}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000696
697// This test verifies that NetEq can handle comfort noise and enters/quits codec
698// internal CNG mode properly.
699TEST_F(NetEqImplTest, CodecInternalCng) {
700 UseNoMocks();
701 CreateInstance();
702
703 const uint8_t kPayloadType = 17; // Just an arbitrary number.
704 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
705 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700706 const size_t kPayloadLengthSamples =
707 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
708 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000709 uint8_t payload[kPayloadLengthBytes] = {0};
710 int16_t dummy_output[kPayloadLengthSamples] = {0};
711
henrik.lundin246ef3e2017-04-24 09:14:32 -0700712 RTPHeader rtp_header;
713 rtp_header.payloadType = kPayloadType;
714 rtp_header.sequenceNumber = 0x1234;
715 rtp_header.timestamp = 0x12345678;
716 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000717
718 // Create a mock decoder object.
719 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200720 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700721 EXPECT_CALL(mock_decoder, SampleRateHz())
722 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000723 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000724 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
725 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700726 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200727 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700728 // Packed duration when asking the decoder for more CNG data (without a new
729 // packet).
730 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200731 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000732
733 // Pointee(x) verifies that first byte of the payload equals x, this makes it
734 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100735 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), 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::kSpeech),
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, DecodeInternal(Pointee(1), kPayloadLengthBytes,
743 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,
750 DecodeInternal(IsNull(), 0, 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::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200754 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000755
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100756 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
757 kSampleRateKhz * 1000, _, _))
758 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000759 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100760 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200761 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000762
Karl Wibergd8399e62015-05-25 14:39:56 +0200763 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700764 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700765 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000766
767 // Insert one packet (decoder will return speech).
768 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700769 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000770
771 // Insert second packet (decoder will return CNG).
772 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700773 rtp_header.sequenceNumber++;
774 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000775 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700776 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000777
Peter Kastingdce40cf2015-08-24 14:52:23 -0700778 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800779 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800780 AudioFrame::SpeechType expected_type[8] = {
781 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
782 AudioFrame::kCNG, AudioFrame::kCNG,
783 AudioFrame::kCNG, AudioFrame::kCNG,
784 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000785 };
786 int expected_timestamp_increment[8] = {
787 -1, // will not be used.
788 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700789 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
790 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000791 50 * kSampleRateKhz, 10 * kSampleRateKhz
792 };
793
henrik.lundin7a926812016-05-12 13:51:28 -0700794 bool muted;
795 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200796 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700797 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000798
henrik.lundin0d96ab72016-04-06 12:28:26 -0700799 // Lambda for verifying the timestamps.
800 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200801 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700802 if (expected_timestamp_increment[i] == -1) {
803 // Expect to get an empty timestamp value during CNG and PLC.
804 EXPECT_FALSE(ts) << "i = " << i;
805 } else {
806 ASSERT_TRUE(ts) << "i = " << i;
807 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
808 << "i = " << i;
809 last_timestamp = ts;
810 }
811 };
812
Peter Kastingdce40cf2015-08-24 14:52:23 -0700813 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800814 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
815 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800816 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700817 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700818 SCOPED_TRACE("");
819 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820 }
821
822 // Insert third packet, which leaves a gap from last packet.
823 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700824 rtp_header.sequenceNumber += 2;
825 rtp_header.timestamp += 2 * 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 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800830 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
831 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800832 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700833 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700834 SCOPED_TRACE("");
835 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000836 }
837
838 // Now check the packet buffer, and make sure it is empty.
839 EXPECT_TRUE(packet_buffer_->Empty());
840
841 EXPECT_CALL(mock_decoder, Die());
842}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000843
844TEST_F(NetEqImplTest, UnsupportedDecoder) {
845 UseNoMocks();
846 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700847 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800848 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000849
850 const uint8_t kPayloadType = 17; // Just an arbitrary number.
851 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
852 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000853
Peter Kastingdce40cf2015-08-24 14:52:23 -0700854 const size_t kPayloadLengthSamples =
855 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000856 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700857 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200858 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700859 RTPHeader rtp_header;
860 rtp_header.payloadType = kPayloadType;
861 rtp_header.sequenceNumber = 0x1234;
862 rtp_header.timestamp = 0x12345678;
863 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000864
ossu61a208b2016-09-20 01:38:00 -0700865 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000866
867 const uint8_t kFirstPayloadValue = 1;
868 const uint8_t kSecondPayloadValue = 2;
869
ossu61a208b2016-09-20 01:38:00 -0700870 EXPECT_CALL(decoder,
871 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
872 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200873 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000874
ossu61a208b2016-09-20 01:38:00 -0700875 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000876 .Times(0);
877
ossu61a208b2016-09-20 01:38:00 -0700878 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
879 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000880 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700881 .WillOnce(DoAll(
882 SetArrayArgument<3>(dummy_output,
883 dummy_output + kPayloadLengthSamples * kChannels),
884 SetArgPointee<4>(AudioDecoder::kSpeech),
885 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000886
ossu61a208b2016-09-20 01:38:00 -0700887 EXPECT_CALL(decoder,
888 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
889 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200890 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700891
892 EXPECT_CALL(decoder, SampleRateHz())
893 .WillRepeatedly(Return(kSampleRateHz));
894
895 EXPECT_CALL(decoder, Channels())
896 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000897
kwibergee1879c2015-10-29 06:20:28 -0700898 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700899 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700900 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000901
902 // Insert one packet.
903 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
904 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700905 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000906
907 // Insert another packet.
908 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700909 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910 // The second timestamp needs to be at least 30 ms after the first to make
911 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700912 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000913 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700914 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000915
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700917 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800918 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200919 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700920 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800921 // Output size and number of channels should be correct.
922 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
923 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
924 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000925
henrik.lundin6d8e0112016-03-04 10:34:21 -0800926 // Second call to GetAudio will decode the packet that is ok. No errors are
927 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700928 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800929 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
930 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700931
932 // Die isn't called through NiceMock (since it's called by the
933 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
934 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000935}
936
henrik.lundin116c84e2015-08-27 13:14:48 -0700937// This test inserts packets until the buffer is flushed. After that, it asks
938// NetEq for the network statistics. The purpose of the test is to make sure
939// that even though the buffer size increment is negative (which it becomes when
940// the packet causing a flush is inserted), the packet length stored in the
941// decision logic remains valid.
942TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
943 UseNoMocks();
944 CreateInstance();
945
946 const size_t kPayloadLengthSamples = 80;
947 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
948 const uint8_t kPayloadType = 17; // Just an arbitrary number.
949 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
950 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700951 RTPHeader rtp_header;
952 rtp_header.payloadType = kPayloadType;
953 rtp_header.sequenceNumber = 0x1234;
954 rtp_header.timestamp = 0x12345678;
955 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700956
kwibergee1879c2015-10-29 06:20:28 -0700957 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800958 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700959
960 // Insert packets until the buffer flushes.
961 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
962 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
963 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700964 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
965 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
966 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700967 }
968 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
969
970 // Ask for network statistics. This should not crash.
971 NetEqNetworkStatistics stats;
972 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
973}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200974
975TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
976 UseNoMocks();
977 CreateInstance();
978
979 const uint8_t kPayloadType = 17; // Just an arbitrary number.
980 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
981 const int kSampleRateHz = 8000;
982 const size_t kPayloadLengthSamples =
983 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
984 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
985 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700986 RTPHeader rtp_header;
987 rtp_header.payloadType = kPayloadType;
988 rtp_header.sequenceNumber = 0x1234;
989 rtp_header.timestamp = 0x12345678;
990 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200991
992 // Create a mock decoder object.
993 MockAudioDecoder mock_decoder;
994 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700995 EXPECT_CALL(mock_decoder, SampleRateHz())
996 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200997 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
998 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
999 .WillRepeatedly(Return(0));
1000 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001001 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001002 int16_t dummy_output[kPayloadLengthSamples] = {0};
1003 // The below expectation will make the mock decoder write
1004 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1005 // speech. That is, the decoded length is 5 samples shorter than the expected.
1006 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001007 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001008 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001009 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001010 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001011 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001012 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
kwibergee1879c2015-10-29 06:20:28 -07001013 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1014 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001015 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001016
1017 // Insert one packet.
1018 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001019 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001020
1021 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1022
1023 // Pull audio once.
1024 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001025 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001026 bool muted;
1027 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001028 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1029 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001030 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001031
1032 EXPECT_CALL(mock_decoder, Die());
1033}
minyuel6d92bf52015-09-23 15:20:39 +02001034
1035// This test checks the behavior of NetEq when audio decoder fails.
1036TEST_F(NetEqImplTest, DecodingError) {
1037 UseNoMocks();
1038 CreateInstance();
1039
1040 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1041 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1042 const int kSampleRateHz = 8000;
1043 const int kDecoderErrorCode = -97; // Any negative number.
1044
1045 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1046 const size_t kFrameLengthSamples =
1047 static_cast<size_t>(5 * kSampleRateHz / 1000);
1048
1049 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1050
1051 uint8_t payload[kPayloadLengthBytes] = {0};
1052
henrik.lundin246ef3e2017-04-24 09:14:32 -07001053 RTPHeader rtp_header;
1054 rtp_header.payloadType = kPayloadType;
1055 rtp_header.sequenceNumber = 0x1234;
1056 rtp_header.timestamp = 0x12345678;
1057 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001058
1059 // Create a mock decoder object.
1060 MockAudioDecoder mock_decoder;
1061 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001062 EXPECT_CALL(mock_decoder, SampleRateHz())
1063 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001064 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1065 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1066 .WillRepeatedly(Return(0));
1067 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001068 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001069 EXPECT_CALL(mock_decoder, ErrorCode())
1070 .WillOnce(Return(kDecoderErrorCode));
1071 EXPECT_CALL(mock_decoder, HasDecodePlc())
1072 .WillOnce(Return(false));
1073 int16_t dummy_output[kFrameLengthSamples] = {0};
1074
1075 {
1076 InSequence sequence; // Dummy variable.
1077 // Mock decoder works normally the first time.
1078 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001079 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001080 .Times(3)
1081 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001082 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001083 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001084 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001085 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001086 .RetiresOnSaturation();
1087
1088 // Then mock decoder fails. A common reason for failure can be buffer being
1089 // too short
1090 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001091 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001092 .WillOnce(Return(-1))
1093 .RetiresOnSaturation();
1094
1095 // Mock decoder finally returns to normal.
1096 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001097 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001098 .Times(2)
1099 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001100 DoAll(SetArrayArgument<3>(dummy_output,
1101 dummy_output + kFrameLengthSamples),
1102 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001103 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001104 }
1105
kwibergee1879c2015-10-29 06:20:28 -07001106 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1107 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001108 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001109
1110 // Insert packets.
1111 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001112 rtp_header.sequenceNumber += 1;
1113 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001114 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001115 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001116 }
1117
1118 // Pull audio.
1119 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001120 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001121 bool muted;
1122 EXPECT_EQ(NetEq::kOK, 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 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001126
1127 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001128 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001129 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1130 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001131 // We are not expecting anything for output.speech_type_, since an error was
1132 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001133
1134 // Pull audio again, should continue an expansion.
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::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001139
1140 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001141 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001142 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1143 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001144 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001145
1146 EXPECT_CALL(mock_decoder, Die());
1147}
1148
1149// This test checks the behavior of NetEq when audio decoder fails during CNG.
1150TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1151 UseNoMocks();
1152 CreateInstance();
1153
1154 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1155 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1156 const int kSampleRateHz = 8000;
1157 const int kDecoderErrorCode = -97; // Any negative number.
1158
1159 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1160 const size_t kFrameLengthSamples =
1161 static_cast<size_t>(5 * kSampleRateHz / 1000);
1162
1163 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1164
1165 uint8_t payload[kPayloadLengthBytes] = {0};
1166
henrik.lundin246ef3e2017-04-24 09:14:32 -07001167 RTPHeader rtp_header;
1168 rtp_header.payloadType = kPayloadType;
1169 rtp_header.sequenceNumber = 0x1234;
1170 rtp_header.timestamp = 0x12345678;
1171 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001172
1173 // Create a mock decoder object.
1174 MockAudioDecoder mock_decoder;
1175 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001176 EXPECT_CALL(mock_decoder, SampleRateHz())
1177 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001178 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1179 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1180 .WillRepeatedly(Return(0));
1181 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001182 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001183 EXPECT_CALL(mock_decoder, ErrorCode())
1184 .WillOnce(Return(kDecoderErrorCode));
1185 int16_t dummy_output[kFrameLengthSamples] = {0};
1186
1187 {
1188 InSequence sequence; // Dummy variable.
1189 // Mock decoder works normally the first 2 times.
1190 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001191 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001192 .Times(2)
1193 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001194 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001195 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001196 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001197 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001198 .RetiresOnSaturation();
1199
1200 // Then mock decoder fails. A common reason for failure can be buffer being
1201 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001202 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001203 .WillOnce(Return(-1))
1204 .RetiresOnSaturation();
1205
1206 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001207 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001208 .Times(2)
1209 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001210 DoAll(SetArrayArgument<3>(dummy_output,
1211 dummy_output + kFrameLengthSamples),
1212 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001213 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001214 }
1215
kwibergee1879c2015-10-29 06:20:28 -07001216 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1217 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001218 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001219
1220 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1221 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 rtp_header.sequenceNumber += 1;
1223 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001224 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001225 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001226 }
1227
1228 // Pull audio.
1229 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001230 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001231 bool muted;
1232 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001233 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1234 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001235 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001236
1237 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001238 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001239 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1240 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001241 // We are not expecting anything for output.speech_type_, since an error was
1242 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001243
1244 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001245 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001246 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1247 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001248 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001249
1250 EXPECT_CALL(mock_decoder, Die());
1251}
1252
henrik.lundind89814b2015-11-23 06:49:25 -08001253// Tests that the return value from last_output_sample_rate_hz() is equal to the
1254// configured inital sample rate.
1255TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1256 UseNoMocks();
1257 config_.sample_rate_hz = 48000;
1258 CreateInstance();
1259 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1260}
1261
henrik.lundined497212016-04-25 10:11:38 -07001262TEST_F(NetEqImplTest, TickTimerIncrement) {
1263 UseNoMocks();
1264 CreateInstance();
1265 ASSERT_TRUE(tick_timer_);
1266 EXPECT_EQ(0u, tick_timer_->ticks());
1267 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001268 bool muted;
1269 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001270 EXPECT_EQ(1u, tick_timer_->ticks());
1271}
1272
henrik.lundin114c1b32017-04-26 07:47:32 -07001273TEST_F(NetEqImplTest, TargetDelayMs) {
1274 UseNoMocks();
1275 use_mock_delay_manager_ = true;
1276 CreateInstance();
1277 // Let the dummy target delay be 17 packets.
1278 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1279 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1280 .WillOnce(Return(kTargetLevelPacketsQ8));
1281 // Default packet size before any packet has been decoded is 30 ms, so we are
1282 // expecting 17 * 30 = 510 ms target delay.
1283 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1284}
1285
henrik.lundinb8c55b12017-05-10 07:38:01 -07001286TEST_F(NetEqImplTest, InsertEmptyPacket) {
1287 UseNoMocks();
1288 use_mock_delay_manager_ = true;
1289 CreateInstance();
1290
1291 RTPHeader rtp_header;
1292 rtp_header.payloadType = 17;
1293 rtp_header.sequenceNumber = 0x1234;
1294 rtp_header.timestamp = 0x12345678;
1295 rtp_header.ssrc = 0x87654321;
1296
1297 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1298 neteq_->InsertEmptyPacket(rtp_header);
1299}
1300
minyue5bd33972016-05-02 04:46:11 -07001301class Decoder120ms : public AudioDecoder {
1302 public:
kwiberg347d3512016-06-16 01:59:09 -07001303 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1304 : sample_rate_hz_(sample_rate_hz),
1305 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001306 speech_type_(speech_type) {}
1307
1308 int DecodeInternal(const uint8_t* encoded,
1309 size_t encoded_len,
1310 int sample_rate_hz,
1311 int16_t* decoded,
1312 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001313 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001314 size_t decoded_len =
1315 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1316 for (size_t i = 0; i < decoded_len; ++i) {
1317 decoded[i] = next_value_++;
1318 }
1319 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001320 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001321 }
1322
1323 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001324 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001325 size_t Channels() const override { return 2; }
1326
1327 private:
kwiberg347d3512016-06-16 01:59:09 -07001328 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001329 int16_t next_value_;
1330 SpeechType speech_type_;
1331};
1332
1333class NetEqImplTest120ms : public NetEqImplTest {
1334 protected:
1335 NetEqImplTest120ms() : NetEqImplTest() {}
1336 virtual ~NetEqImplTest120ms() {}
1337
1338 void CreateInstanceNoMocks() {
1339 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001340 CreateInstance(decoder_factory_);
1341 EXPECT_TRUE(neteq_->RegisterPayloadType(
1342 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001343 }
1344
1345 void CreateInstanceWithDelayManagerMock() {
1346 UseNoMocks();
1347 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001348 CreateInstance(decoder_factory_);
1349 EXPECT_TRUE(neteq_->RegisterPayloadType(
1350 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001351 }
1352
1353 uint32_t timestamp_diff_between_packets() const {
1354 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1355 }
1356
1357 uint32_t first_timestamp() const { return 10u; }
1358
1359 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001360 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001361 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001362 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1363 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001364 }
1365 }
1366
1367 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001368 RTPHeader rtp_header;
1369 rtp_header.payloadType = kPayloadType;
1370 rtp_header.sequenceNumber = sequence_number_;
1371 rtp_header.timestamp = timestamp;
1372 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001373 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1374 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001375 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001376 sequence_number_++;
1377 }
1378
1379 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001380 const uint32_t sampling_freq = kSamplingFreq_;
1381 decoder_factory_ =
1382 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1383 [sampling_freq, speech_type]() {
1384 std::unique_ptr<AudioDecoder> decoder =
1385 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1386 RTC_CHECK_EQ(2, decoder->Channels());
1387 return decoder;
1388 });
minyue5bd33972016-05-02 04:46:11 -07001389 }
1390
Niels Möllera0f44302018-11-30 10:45:12 +01001391 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001392 AudioFrame output_;
1393 const uint32_t kPayloadType = 17;
1394 const uint32_t kSamplingFreq_ = 48000;
1395 uint16_t sequence_number_ = 1;
1396};
1397
minyue5bd33972016-05-02 04:46:11 -07001398TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001399 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001400 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001401
1402 InsertPacket(first_timestamp());
1403 GetFirstPacket();
1404
henrik.lundin7a926812016-05-12 13:51:28 -07001405 bool muted;
1406 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001407 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1408}
1409
1410TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001411 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001412 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001413
1414 InsertPacket(first_timestamp());
1415 GetFirstPacket();
1416
1417 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1418}
1419
1420TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001421 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001422 CreateInstanceWithDelayManagerMock();
1423
minyue5bd33972016-05-02 04:46:11 -07001424 InsertPacket(first_timestamp());
1425
1426 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001427 bool muted;
1428 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001429
1430 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1431
1432 // Delay manager reports a target level which should cause a Merge.
1433 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1434
henrik.lundin7a926812016-05-12 13:51:28 -07001435 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001436 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1437}
1438
1439TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001440 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001441 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001442
1443 InsertPacket(first_timestamp());
1444 GetFirstPacket();
1445
henrik.lundin7a926812016-05-12 13:51:28 -07001446 bool muted;
1447 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001448 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1449}
1450
1451TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001452 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001453 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001454
1455 InsertPacket(first_timestamp());
1456 GetFirstPacket();
1457 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1458
1459 // Delay manager report buffer limit which should cause a FastAccelerate.
1460 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1461 .Times(1)
1462 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1463
henrik.lundin7a926812016-05-12 13:51:28 -07001464 bool muted;
1465 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001466 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1467}
1468
1469TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001470 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001471 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001472
1473 InsertPacket(first_timestamp());
1474 GetFirstPacket();
1475
1476 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1477
1478 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1479 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1480 .Times(1)
1481 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1482
henrik.lundin7a926812016-05-12 13:51:28 -07001483 bool muted;
1484 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001485 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1486}
1487
1488TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001489 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001490 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001491
1492 InsertPacket(first_timestamp());
1493 GetFirstPacket();
1494
1495 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1496
1497 // Delay manager report buffer limit which should cause a Accelerate.
1498 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1499 .Times(1)
1500 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1501
henrik.lundin7a926812016-05-12 13:51:28 -07001502 bool muted;
1503 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001504 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1505}
1506
minyuel6d92bf52015-09-23 15:20:39 +02001507}// namespace webrtc