blob: 15ec1f41915b60f77ac6f2d6e1001fbeeeab4776 [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"
17#include "modules/audio_coding/neteq/include/neteq.h"
18#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
19#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
20#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
21#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
22#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
23#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
24#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
25#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
26#include "modules/audio_coding/neteq/neteq_impl.h"
27#include "modules/audio_coding/neteq/preemptive_expand.h"
28#include "modules/audio_coding/neteq/sync_buffer.h"
29#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010030#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010031#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010032#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "test/gmock.h"
34#include "test/gtest.h"
35#include "test/mock_audio_decoder.h"
36#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000038using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039using ::testing::Return;
40using ::testing::ReturnNull;
41using ::testing::_;
42using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000043using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044using ::testing::InSequence;
45using ::testing::Invoke;
46using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000047using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000048using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50namespace webrtc {
51
52// This function is called when inserting a packet list into the mock packet
53// buffer. The purpose is to delete all inserted packets properly, to avoid
54// memory leaks in the test.
55int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070056 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057 return PacketBuffer::kOK;
58}
59
60class NetEqImplTest : public ::testing::Test {
61 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070062 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000063
Niels Möllera0f44302018-11-30 10:45:12 +010064 void CreateInstance(
65 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
66 ASSERT_TRUE(decoder_factory);
67 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070068
69 // Get a local pointer to NetEq's TickTimer object.
70 tick_timer_ = deps.tick_timer.get();
71
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070073 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
74 mock_buffer_level_filter_ = mock.get();
75 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000076 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 buffer_level_filter_ = deps.buffer_level_filter.get();
78
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
81 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
83 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000087
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070089 std::unique_ptr<MockDelayPeakDetector> mock(
90 new MockDelayPeakDetector(tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 mock_delay_peak_detector_ = mock.get();
92 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
93 deps.delay_peak_detector = std::move(mock);
94 }
95 delay_peak_detector_ = deps.delay_peak_detector.get();
96
97 if (use_mock_delay_manager_) {
98 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010099 config_.max_packets_in_buffer, config_.min_delay_ms,
100 delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700101 mock_delay_manager_ = mock.get();
102 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
103 deps.delay_manager = std::move(mock);
104 }
105 delay_manager_ = deps.delay_manager.get();
106
107 if (use_mock_dtmf_buffer_) {
108 std::unique_ptr<MockDtmfBuffer> mock(
109 new MockDtmfBuffer(config_.sample_rate_hz));
110 mock_dtmf_buffer_ = mock.get();
111 deps.dtmf_buffer = std::move(mock);
112 }
113 dtmf_buffer_ = deps.dtmf_buffer.get();
114
115 if (use_mock_dtmf_tone_generator_) {
116 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
117 mock_dtmf_tone_generator_ = mock.get();
118 deps.dtmf_tone_generator = std::move(mock);
119 }
120 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
121
122 if (use_mock_packet_buffer_) {
123 std::unique_ptr<MockPacketBuffer> mock(
124 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
125 mock_packet_buffer_ = mock.get();
126 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700127 }
128 packet_buffer_ = deps.packet_buffer.get();
129
130 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700131 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700133 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 }
ossua70695a2016-09-22 02:06:28 -0700135 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136
137 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
138 new TimestampScaler(*deps.decoder_database.get()));
139
140 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000141 ASSERT_TRUE(neteq_ != NULL);
142 }
143
Niels Möllera0f44302018-11-30 10:45:12 +0100144 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
145
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000146 void UseNoMocks() {
147 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
148 use_mock_buffer_level_filter_ = false;
149 use_mock_decoder_database_ = false;
150 use_mock_delay_peak_detector_ = false;
151 use_mock_delay_manager_ = false;
152 use_mock_dtmf_buffer_ = false;
153 use_mock_dtmf_tone_generator_ = false;
154 use_mock_packet_buffer_ = false;
155 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 }
157
158 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000159 if (use_mock_buffer_level_filter_) {
160 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
161 }
162 if (use_mock_decoder_database_) {
163 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
164 }
165 if (use_mock_delay_manager_) {
166 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
167 }
168 if (use_mock_delay_peak_detector_) {
169 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
170 }
171 if (use_mock_dtmf_buffer_) {
172 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
173 }
174 if (use_mock_dtmf_tone_generator_) {
175 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
176 }
177 if (use_mock_packet_buffer_) {
178 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
179 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 }
181
solenberg2779bab2016-11-17 04:45:19 -0800182 void TestDtmfPacket(NetEqDecoder decoder_type) {
183 const size_t kPayloadLength = 4;
184 const uint8_t kPayloadType = 110;
185 const uint32_t kReceiveTime = 17;
186 const int kSampleRateHz = 16000;
187 config_.sample_rate_hz = kSampleRateHz;
188 UseNoMocks();
189 CreateInstance();
190 // Event: 2, E bit, Volume: 17, Length: 4336.
191 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700192 RTPHeader rtp_header;
193 rtp_header.payloadType = kPayloadType;
194 rtp_header.sequenceNumber = 0x1234;
195 rtp_header.timestamp = 0x12345678;
196 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800197
198 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
199 decoder_type, "telephone-event", kPayloadType));
200
201 // Insert first packet.
202 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700203 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800204
205 // Pull audio once.
206 const size_t kMaxOutputSize =
207 static_cast<size_t>(10 * kSampleRateHz / 1000);
208 AudioFrame output;
209 bool muted;
210 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
211 ASSERT_FALSE(muted);
212 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
213 EXPECT_EQ(1u, output.num_channels_);
214 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
215
216 // Verify first 64 samples of actual output.
217 const std::vector<int16_t> kOutput({
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
219 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
220 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
221 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
222 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
223 -2534, -1163 });
224 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700225 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800226 }
227
henrik.lundin1d9061e2016-04-26 12:19:34 -0700228 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000229 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 TickTimer* tick_timer_ = nullptr;
231 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
232 BufferLevelFilter* buffer_level_filter_ = nullptr;
233 bool use_mock_buffer_level_filter_ = true;
234 MockDecoderDatabase* mock_decoder_database_ = nullptr;
235 DecoderDatabase* decoder_database_ = nullptr;
236 bool use_mock_decoder_database_ = true;
237 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
238 DelayPeakDetector* delay_peak_detector_ = nullptr;
239 bool use_mock_delay_peak_detector_ = true;
240 MockDelayManager* mock_delay_manager_ = nullptr;
241 DelayManager* delay_manager_ = nullptr;
242 bool use_mock_delay_manager_ = true;
243 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
244 DtmfBuffer* dtmf_buffer_ = nullptr;
245 bool use_mock_dtmf_buffer_ = true;
246 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
247 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
248 bool use_mock_dtmf_tone_generator_ = true;
249 MockPacketBuffer* mock_packet_buffer_ = nullptr;
250 PacketBuffer* packet_buffer_ = nullptr;
251 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700252 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
253 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700254 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255};
256
257
258// This tests the interface class NetEq.
259// TODO(hlundin): Move to separate file?
260TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000261 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700262 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 delete neteq;
264}
265
kwiberg5adaf732016-10-04 09:33:27 -0700266TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000267 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700269 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800270 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000271 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800272 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
273 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274}
275
kwiberg5adaf732016-10-04 09:33:27 -0700276TEST_F(NetEqImplTest, RegisterPayloadType) {
277 CreateInstance();
278 constexpr int rtp_payload_type = 0;
279 const SdpAudioFormat format("pcmu", 8000, 1);
280 EXPECT_CALL(*mock_decoder_database_,
281 RegisterPayload(rtp_payload_type, format));
282 neteq_->RegisterPayloadType(rtp_payload_type, format);
283}
284
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000286 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000288 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200290 // Check that kOK is returned when database returns kDecoderNotFound, because
291 // removing a payload type that was never registered is not an error.
292 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293}
294
kwiberg6b19b562016-09-20 04:02:25 -0700295TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
296 CreateInstance();
297 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
298 neteq_->RemoveAllPayloadTypes();
299}
300
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000302 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000303 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304 const uint8_t kPayloadType = 0;
305 const uint16_t kFirstSequenceNumber = 0x1234;
306 const uint32_t kFirstTimestamp = 0x12345678;
307 const uint32_t kSsrc = 0x87654321;
308 const uint32_t kFirstReceiveTime = 17;
309 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700310 RTPHeader rtp_header;
311 rtp_header.payloadType = kPayloadType;
312 rtp_header.sequenceNumber = kFirstSequenceNumber;
313 rtp_header.timestamp = kFirstTimestamp;
314 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700315 Packet fake_packet;
316 fake_packet.payload_type = kPayloadType;
317 fake_packet.sequence_number = kFirstSequenceNumber;
318 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319
kwibergc0f2dcf2016-05-31 06:28:03 -0700320 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
321 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100322 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800323 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200324 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800325 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700326 EXPECT_EQ("pcmu", format.name);
327
328 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
329 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
330 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
331 // BWE update function called with first packet.
332 EXPECT_CALL(*mock_decoder,
333 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
334 kFirstTimestamp, kFirstReceiveTime));
335 // BWE update function called with second packet.
336 EXPECT_CALL(
337 *mock_decoder,
338 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
339 kFirstTimestamp + 160, kFirstReceiveTime + 155));
340 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
341
342 *dec = std::move(mock_decoder);
343 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200344 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, absl::nullopt,
ossu84bc9852016-08-26 05:41:23 -0700345 mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346
347 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000348 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 .WillRepeatedly(Return(&info));
350
351 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000352 EXPECT_CALL(*mock_packet_buffer_, Empty())
353 .WillOnce(Return(false)); // Called once after first packet is inserted.
354 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200356 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100358 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
359 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
361 // index) is a pointer, and the variable pointed to is set to kPayloadType.
362 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
363 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700364 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000365 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700366 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367
368 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000369 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 .Times(1);
371
372 // Expectations for delay manager.
373 {
374 // All expectations within this block must be called in this specific order.
375 InSequence sequence; // Dummy variable.
376 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 .Times(2)
379 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000380 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000382 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000384 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
385 .WillOnce(Return(0));
386 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387 .WillOnce(Return(0));
388 }
389
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700391 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392
393 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700394 rtp_header.timestamp += 160;
395 rtp_header.sequenceNumber += 1;
396 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397}
398
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000399TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
400 UseNoMocks();
401 CreateInstance();
402
403 const int kPayloadLengthSamples = 80;
404 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
405 const uint8_t kPayloadType = 17; // Just an arbitrary number.
406 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
407 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700408 RTPHeader rtp_header;
409 rtp_header.payloadType = kPayloadType;
410 rtp_header.sequenceNumber = 0x1234;
411 rtp_header.timestamp = 0x12345678;
412 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413
kwibergee1879c2015-10-29 06:20:28 -0700414 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800415 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000416
417 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700418 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000419 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700420 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
421 rtp_header.timestamp += kPayloadLengthSamples;
422 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000423 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
424 }
425
426 // Insert one more packet and make sure the buffer got flushed. That is, it
427 // should only hold one single packet.
428 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700429 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700430 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700431 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700432 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
433 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000434}
435
solenberg2779bab2016-11-17 04:45:19 -0800436TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
437 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
438}
solenberg99df6c02016-10-11 04:35:34 -0700439
solenberg2779bab2016-11-17 04:45:19 -0800440TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
441 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
442}
solenberg99df6c02016-10-11 04:35:34 -0700443
solenberg2779bab2016-11-17 04:45:19 -0800444TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
445 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
446}
solenberg99df6c02016-10-11 04:35:34 -0700447
solenberg2779bab2016-11-17 04:45:19 -0800448TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
449 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700450}
451
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000452// This test verifies that timestamps propagate from the incoming packets
453// through to the sync buffer and to the playout timestamp.
454TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000455 const uint8_t kPayloadType = 17; // Just an arbitrary number.
456 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
457 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700458 const size_t kPayloadLengthSamples =
459 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000460 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
461 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700462 RTPHeader rtp_header;
463 rtp_header.payloadType = kPayloadType;
464 rtp_header.sequenceNumber = 0x1234;
465 rtp_header.timestamp = 0x12345678;
466 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000467
468 // This is a dummy decoder that produces as many output samples as the input
469 // has bytes. The output is an increasing series, starting at 1 for the first
470 // sample, and then increasing by 1 for each sample.
471 class CountingSamplesDecoder : public AudioDecoder {
472 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000473 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000474
475 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100476 int DecodeInternal(const uint8_t* encoded,
477 size_t encoded_len,
478 int /* sample_rate_hz */,
479 int16_t* decoded,
480 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000481 for (size_t i = 0; i < encoded_len; ++i) {
482 decoded[i] = next_value_++;
483 }
484 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200485 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000486 }
487
Karl Wiberg43766482015-08-27 15:22:11 +0200488 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489
kwiberg347d3512016-06-16 01:59:09 -0700490 int SampleRateHz() const override { return kSampleRateHz; }
491
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000492 size_t Channels() const override { return 1; }
493
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000494 uint16_t next_value() const { return next_value_; }
495
496 private:
497 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000498 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000499
Niels Möllerb7180c02018-12-06 13:07:11 +0100500 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
501 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
502
503 UseNoMocks();
504 CreateInstance(decoder_factory);
505
506 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100507 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000508
509 // Insert one packet.
510 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700511 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000512
513 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700514 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800515 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700516 bool muted;
517 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
518 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800519 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
520 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800521 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000522
523 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700524 EXPECT_EQ(kPayloadLengthSamples,
525 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000526
527 // The value of the last of the output samples is the same as the number of
528 // samples played from the decoded packet. Thus, this number + the RTP
529 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200530 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700531 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200532 absl::optional<uint32_t>(rtp_header.timestamp +
533 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700534 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000535
536 // Check the timestamp for the last value in the sync buffer. This should
537 // be one full frame length ahead of the RTP timestamp.
538 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
539 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700540 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000541 sync_buffer->end_timestamp());
542
543 // Check that the number of samples still to play from the sync buffer add
544 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800545 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700546 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800547 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000548}
549
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000550TEST_F(NetEqImplTest, ReorderedPacket) {
551 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100552 // Create a mock decoder object.
553 MockAudioDecoder mock_decoder;
554
555 CreateInstance(
556 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000557
558 const uint8_t kPayloadType = 17; // Just an arbitrary number.
559 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
560 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700561 const size_t kPayloadLengthSamples =
562 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000563 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
564 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700565 RTPHeader rtp_header;
566 rtp_header.payloadType = kPayloadType;
567 rtp_header.sequenceNumber = 0x1234;
568 rtp_header.timestamp = 0x12345678;
569 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000570
Karl Wiberg43766482015-08-27 15:22:11 +0200571 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700572 EXPECT_CALL(mock_decoder, SampleRateHz())
573 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000574 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
576 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700577 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200578 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000579 int16_t dummy_output[kPayloadLengthSamples] = {0};
580 // The below expectation will make the mock decoder write
581 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100582 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
583 kSampleRateHz, _, _))
584 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100586 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200587 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100588 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
589 SdpAudioFormat("L16", 8000, 1)));
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();
Niels Möller50b66d52018-12-11 14:43:21 +0100701 // Create a mock decoder object.
702 MockAudioDecoder mock_decoder;
703 CreateInstance(
704 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000705
706 const uint8_t kPayloadType = 17; // Just an arbitrary number.
707 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
708 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700709 const size_t kPayloadLengthSamples =
710 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
711 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000712 uint8_t payload[kPayloadLengthBytes] = {0};
713 int16_t dummy_output[kPayloadLengthSamples] = {0};
714
henrik.lundin246ef3e2017-04-24 09:14:32 -0700715 RTPHeader rtp_header;
716 rtp_header.payloadType = kPayloadType;
717 rtp_header.sequenceNumber = 0x1234;
718 rtp_header.timestamp = 0x12345678;
719 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000720
Karl Wiberg43766482015-08-27 15:22:11 +0200721 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700722 EXPECT_CALL(mock_decoder, SampleRateHz())
723 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000724 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000725 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
726 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700727 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200728 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700729 // Packed duration when asking the decoder for more CNG data (without a new
730 // packet).
731 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200732 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000733
734 // Pointee(x) verifies that first byte of the payload equals x, this makes it
735 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100736 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
737 kSampleRateKhz * 1000, _, _))
738 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000739 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100740 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200741 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000742
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100743 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
744 kSampleRateKhz * 1000, _, _))
745 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000746 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100747 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200748 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000749
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100750 EXPECT_CALL(mock_decoder,
751 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
752 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000753 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100754 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200755 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100757 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
758 kSampleRateKhz * 1000, _, _))
759 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000760 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100761 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200762 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000763
Niels Möller50b66d52018-12-11 14:43:21 +0100764 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
765 SdpAudioFormat("opus", 48000, 2)));
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();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100846 ::testing::NiceMock<MockAudioDecoder> decoder;
847
848 CreateInstance(
849 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700850 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800851 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000852
853 const uint8_t kPayloadType = 17; // Just an arbitrary number.
854 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
855 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000856
Peter Kastingdce40cf2015-08-24 14:52:23 -0700857 const size_t kPayloadLengthSamples =
858 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000859 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700860 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200861 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700862 RTPHeader rtp_header;
863 rtp_header.payloadType = kPayloadType;
864 rtp_header.sequenceNumber = 0x1234;
865 rtp_header.timestamp = 0x12345678;
866 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000867
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000868 const uint8_t kFirstPayloadValue = 1;
869 const uint8_t kSecondPayloadValue = 2;
870
ossu61a208b2016-09-20 01:38:00 -0700871 EXPECT_CALL(decoder,
872 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
873 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200874 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000875
ossu61a208b2016-09-20 01:38:00 -0700876 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000877 .Times(0);
878
ossu61a208b2016-09-20 01:38:00 -0700879 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
880 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000881 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700882 .WillOnce(DoAll(
883 SetArrayArgument<3>(dummy_output,
884 dummy_output + kPayloadLengthSamples * kChannels),
885 SetArgPointee<4>(AudioDecoder::kSpeech),
886 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000887
ossu61a208b2016-09-20 01:38:00 -0700888 EXPECT_CALL(decoder,
889 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
890 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200891 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700892
893 EXPECT_CALL(decoder, SampleRateHz())
894 .WillRepeatedly(Return(kSampleRateHz));
895
896 EXPECT_CALL(decoder, Channels())
897 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000898
Niels Möllera1eb9c72018-12-07 15:24:42 +0100899 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
900 SdpAudioFormat("L16", 8000, 1)));
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();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100977 // Create a mock decoder object.
978 MockAudioDecoder mock_decoder;
979
980 CreateInstance(
981 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200982
983 const uint8_t kPayloadType = 17; // Just an arbitrary number.
984 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
985 const int kSampleRateHz = 8000;
986 const size_t kPayloadLengthSamples =
987 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
988 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
989 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700990 RTPHeader rtp_header;
991 rtp_header.payloadType = kPayloadType;
992 rtp_header.sequenceNumber = 0x1234;
993 rtp_header.timestamp = 0x12345678;
994 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200995
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200996 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700997 EXPECT_CALL(mock_decoder, SampleRateHz())
998 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200999 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1000 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1001 .WillRepeatedly(Return(0));
1002 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001003 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001004 int16_t dummy_output[kPayloadLengthSamples] = {0};
1005 // The below expectation will make the mock decoder write
1006 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1007 // speech. That is, the decoded length is 5 samples shorter than the expected.
1008 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001009 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001010 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001011 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001012 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001013 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001014 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001015 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1016 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001017
1018 // Insert one packet.
1019 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001020 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001021
1022 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1023
1024 // Pull audio once.
1025 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001026 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001027 bool muted;
1028 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001029 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1030 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001031 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001032
1033 EXPECT_CALL(mock_decoder, Die());
1034}
minyuel6d92bf52015-09-23 15:20:39 +02001035
1036// This test checks the behavior of NetEq when audio decoder fails.
1037TEST_F(NetEqImplTest, DecodingError) {
1038 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001039 // Create a mock decoder object.
1040 MockAudioDecoder mock_decoder;
1041
1042 CreateInstance(
1043 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001044
1045 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1046 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1047 const int kSampleRateHz = 8000;
1048 const int kDecoderErrorCode = -97; // Any negative number.
1049
1050 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1051 const size_t kFrameLengthSamples =
1052 static_cast<size_t>(5 * kSampleRateHz / 1000);
1053
1054 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1055
1056 uint8_t payload[kPayloadLengthBytes] = {0};
1057
henrik.lundin246ef3e2017-04-24 09:14:32 -07001058 RTPHeader rtp_header;
1059 rtp_header.payloadType = kPayloadType;
1060 rtp_header.sequenceNumber = 0x1234;
1061 rtp_header.timestamp = 0x12345678;
1062 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001063
minyuel6d92bf52015-09-23 15:20:39 +02001064 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001065 EXPECT_CALL(mock_decoder, SampleRateHz())
1066 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001067 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1068 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1069 .WillRepeatedly(Return(0));
1070 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001071 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001072 EXPECT_CALL(mock_decoder, ErrorCode())
1073 .WillOnce(Return(kDecoderErrorCode));
1074 EXPECT_CALL(mock_decoder, HasDecodePlc())
1075 .WillOnce(Return(false));
1076 int16_t dummy_output[kFrameLengthSamples] = {0};
1077
1078 {
1079 InSequence sequence; // Dummy variable.
1080 // Mock decoder works normally the first time.
1081 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001082 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001083 .Times(3)
1084 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001085 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001086 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001087 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001088 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001089 .RetiresOnSaturation();
1090
1091 // Then mock decoder fails. A common reason for failure can be buffer being
1092 // too short
1093 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001094 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001095 .WillOnce(Return(-1))
1096 .RetiresOnSaturation();
1097
1098 // Mock decoder finally returns to normal.
1099 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001100 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001101 .Times(2)
1102 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001103 DoAll(SetArrayArgument<3>(dummy_output,
1104 dummy_output + kFrameLengthSamples),
1105 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001106 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001107 }
1108
Niels Möllera1eb9c72018-12-07 15:24:42 +01001109 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1110 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001111
1112 // Insert packets.
1113 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001114 rtp_header.sequenceNumber += 1;
1115 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001116 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001117 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001118 }
1119
1120 // Pull audio.
1121 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001122 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001123 bool muted;
1124 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001125 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1126 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001127 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001128
1129 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001130 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001131 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1132 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001133 // We are not expecting anything for output.speech_type_, since an error was
1134 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001135
1136 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001137 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001138 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1139 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001140 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001141
1142 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001143 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001144 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1145 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001146 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001147
1148 EXPECT_CALL(mock_decoder, Die());
1149}
1150
1151// This test checks the behavior of NetEq when audio decoder fails during CNG.
1152TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1153 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001154
1155 // Create a mock decoder object.
1156 MockAudioDecoder mock_decoder;
1157 CreateInstance(
1158 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001159
1160 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1161 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1162 const int kSampleRateHz = 8000;
1163 const int kDecoderErrorCode = -97; // Any negative number.
1164
1165 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1166 const size_t kFrameLengthSamples =
1167 static_cast<size_t>(5 * kSampleRateHz / 1000);
1168
1169 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1170
1171 uint8_t payload[kPayloadLengthBytes] = {0};
1172
henrik.lundin246ef3e2017-04-24 09:14:32 -07001173 RTPHeader rtp_header;
1174 rtp_header.payloadType = kPayloadType;
1175 rtp_header.sequenceNumber = 0x1234;
1176 rtp_header.timestamp = 0x12345678;
1177 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001178
minyuel6d92bf52015-09-23 15:20:39 +02001179 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001180 EXPECT_CALL(mock_decoder, SampleRateHz())
1181 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001182 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1183 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1184 .WillRepeatedly(Return(0));
1185 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001186 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001187 EXPECT_CALL(mock_decoder, ErrorCode())
1188 .WillOnce(Return(kDecoderErrorCode));
1189 int16_t dummy_output[kFrameLengthSamples] = {0};
1190
1191 {
1192 InSequence sequence; // Dummy variable.
1193 // Mock decoder works normally the first 2 times.
1194 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001195 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001196 .Times(2)
1197 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001198 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001199 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001200 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001201 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001202 .RetiresOnSaturation();
1203
1204 // Then mock decoder fails. A common reason for failure can be buffer being
1205 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001206 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001207 .WillOnce(Return(-1))
1208 .RetiresOnSaturation();
1209
1210 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001211 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001212 .Times(2)
1213 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001214 DoAll(SetArrayArgument<3>(dummy_output,
1215 dummy_output + kFrameLengthSamples),
1216 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001217 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001218 }
1219
Niels Möller50b66d52018-12-11 14:43:21 +01001220 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1221 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001222
1223 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1224 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001225 rtp_header.sequenceNumber += 1;
1226 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001227 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001228 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001229 }
1230
1231 // Pull audio.
1232 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001233 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001234 bool muted;
1235 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1237 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001239
1240 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001241 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001242 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1243 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001244 // We are not expecting anything for output.speech_type_, since an error was
1245 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001246
1247 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001248 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001249 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1250 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001251 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001252
1253 EXPECT_CALL(mock_decoder, Die());
1254}
1255
henrik.lundind89814b2015-11-23 06:49:25 -08001256// Tests that the return value from last_output_sample_rate_hz() is equal to the
1257// configured inital sample rate.
1258TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1259 UseNoMocks();
1260 config_.sample_rate_hz = 48000;
1261 CreateInstance();
1262 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1263}
1264
henrik.lundined497212016-04-25 10:11:38 -07001265TEST_F(NetEqImplTest, TickTimerIncrement) {
1266 UseNoMocks();
1267 CreateInstance();
1268 ASSERT_TRUE(tick_timer_);
1269 EXPECT_EQ(0u, tick_timer_->ticks());
1270 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001271 bool muted;
1272 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001273 EXPECT_EQ(1u, tick_timer_->ticks());
1274}
1275
henrik.lundin114c1b32017-04-26 07:47:32 -07001276TEST_F(NetEqImplTest, TargetDelayMs) {
1277 UseNoMocks();
1278 use_mock_delay_manager_ = true;
1279 CreateInstance();
1280 // Let the dummy target delay be 17 packets.
1281 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1282 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1283 .WillOnce(Return(kTargetLevelPacketsQ8));
1284 // Default packet size before any packet has been decoded is 30 ms, so we are
1285 // expecting 17 * 30 = 510 ms target delay.
1286 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1287}
1288
henrik.lundinb8c55b12017-05-10 07:38:01 -07001289TEST_F(NetEqImplTest, InsertEmptyPacket) {
1290 UseNoMocks();
1291 use_mock_delay_manager_ = true;
1292 CreateInstance();
1293
1294 RTPHeader rtp_header;
1295 rtp_header.payloadType = 17;
1296 rtp_header.sequenceNumber = 0x1234;
1297 rtp_header.timestamp = 0x12345678;
1298 rtp_header.ssrc = 0x87654321;
1299
1300 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1301 neteq_->InsertEmptyPacket(rtp_header);
1302}
1303
minyue5bd33972016-05-02 04:46:11 -07001304class Decoder120ms : public AudioDecoder {
1305 public:
kwiberg347d3512016-06-16 01:59:09 -07001306 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1307 : sample_rate_hz_(sample_rate_hz),
1308 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001309 speech_type_(speech_type) {}
1310
1311 int DecodeInternal(const uint8_t* encoded,
1312 size_t encoded_len,
1313 int sample_rate_hz,
1314 int16_t* decoded,
1315 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001316 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001317 size_t decoded_len =
1318 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1319 for (size_t i = 0; i < decoded_len; ++i) {
1320 decoded[i] = next_value_++;
1321 }
1322 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001323 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001324 }
1325
1326 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001327 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001328 size_t Channels() const override { return 2; }
1329
1330 private:
kwiberg347d3512016-06-16 01:59:09 -07001331 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001332 int16_t next_value_;
1333 SpeechType speech_type_;
1334};
1335
1336class NetEqImplTest120ms : public NetEqImplTest {
1337 protected:
1338 NetEqImplTest120ms() : NetEqImplTest() {}
1339 virtual ~NetEqImplTest120ms() {}
1340
1341 void CreateInstanceNoMocks() {
1342 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001343 CreateInstance(decoder_factory_);
1344 EXPECT_TRUE(neteq_->RegisterPayloadType(
1345 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001346 }
1347
1348 void CreateInstanceWithDelayManagerMock() {
1349 UseNoMocks();
1350 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001351 CreateInstance(decoder_factory_);
1352 EXPECT_TRUE(neteq_->RegisterPayloadType(
1353 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001354 }
1355
1356 uint32_t timestamp_diff_between_packets() const {
1357 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1358 }
1359
1360 uint32_t first_timestamp() const { return 10u; }
1361
1362 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001363 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001364 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001365 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1366 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001367 }
1368 }
1369
1370 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001371 RTPHeader rtp_header;
1372 rtp_header.payloadType = kPayloadType;
1373 rtp_header.sequenceNumber = sequence_number_;
1374 rtp_header.timestamp = timestamp;
1375 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001376 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1377 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001378 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001379 sequence_number_++;
1380 }
1381
1382 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001383 const uint32_t sampling_freq = kSamplingFreq_;
1384 decoder_factory_ =
1385 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1386 [sampling_freq, speech_type]() {
1387 std::unique_ptr<AudioDecoder> decoder =
1388 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1389 RTC_CHECK_EQ(2, decoder->Channels());
1390 return decoder;
1391 });
minyue5bd33972016-05-02 04:46:11 -07001392 }
1393
Niels Möllera0f44302018-11-30 10:45:12 +01001394 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001395 AudioFrame output_;
1396 const uint32_t kPayloadType = 17;
1397 const uint32_t kSamplingFreq_ = 48000;
1398 uint16_t sequence_number_ = 1;
1399};
1400
minyue5bd33972016-05-02 04:46:11 -07001401TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001402 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001403 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001404
1405 InsertPacket(first_timestamp());
1406 GetFirstPacket();
1407
henrik.lundin7a926812016-05-12 13:51:28 -07001408 bool muted;
1409 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001410 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1411}
1412
1413TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001414 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001415 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001416
1417 InsertPacket(first_timestamp());
1418 GetFirstPacket();
1419
1420 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1421}
1422
1423TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001424 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001425 CreateInstanceWithDelayManagerMock();
1426
minyue5bd33972016-05-02 04:46:11 -07001427 InsertPacket(first_timestamp());
1428
1429 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001430 bool muted;
1431 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001432
1433 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1434
1435 // Delay manager reports a target level which should cause a Merge.
1436 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1437
henrik.lundin7a926812016-05-12 13:51:28 -07001438 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001439 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1440}
1441
1442TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001443 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001444 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001445
1446 InsertPacket(first_timestamp());
1447 GetFirstPacket();
1448
henrik.lundin7a926812016-05-12 13:51:28 -07001449 bool muted;
1450 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001451 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1452}
1453
1454TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001455 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001456 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001457
1458 InsertPacket(first_timestamp());
1459 GetFirstPacket();
1460 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1461
1462 // Delay manager report buffer limit which should cause a FastAccelerate.
1463 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1464 .Times(1)
1465 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1466
henrik.lundin7a926812016-05-12 13:51:28 -07001467 bool muted;
1468 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001469 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1470}
1471
1472TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001473 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001474 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001475
1476 InsertPacket(first_timestamp());
1477 GetFirstPacket();
1478
1479 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1480
1481 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1482 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1483 .Times(1)
1484 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1485
henrik.lundin7a926812016-05-12 13:51:28 -07001486 bool muted;
1487 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001488 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1489}
1490
1491TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001492 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001493 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001494
1495 InsertPacket(first_timestamp());
1496 GetFirstPacket();
1497
1498 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1499
1500 // Delay manager report buffer limit which should cause a Accelerate.
1501 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1502 .Times(1)
1503 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1504
henrik.lundin7a926812016-05-12 13:51:28 -07001505 bool muted;
1506 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001507 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1508}
1509
minyuel6d92bf52015-09-23 15:20:39 +02001510}// namespace webrtc