blob: b772dfa71de119fbcc0406eb4bed46160e47f5e8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "common_types.h" // NOLINT(build/include)
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "test/gmock.h"
32#include "test/gtest.h"
33#include "test/mock_audio_decoder.h"
34#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000036using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037using ::testing::Return;
38using ::testing::ReturnNull;
39using ::testing::_;
40using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000041using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042using ::testing::InSequence;
43using ::testing::Invoke;
44using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000045using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000046using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
48namespace webrtc {
49
50// This function is called when inserting a packet list into the mock packet
51// buffer. The purpose is to delete all inserted packets properly, to avoid
52// memory leaks in the test.
53int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070054 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055 return PacketBuffer::kOK;
56}
57
58class NetEqImplTest : public ::testing::Test {
59 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070060 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000061
62 void CreateInstance() {
ossue3525782016-05-25 07:37:43 -070063 NetEqImpl::Dependencies deps(config_, CreateBuiltinAudioDecoderFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070064
65 // Get a local pointer to NetEq's TickTimer object.
66 tick_timer_ = deps.tick_timer.get();
67
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000068 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070069 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
70 mock_buffer_level_filter_ = mock.get();
71 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070073 buffer_level_filter_ = deps.buffer_level_filter.get();
74
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000075 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070076 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
77 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000078 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
79 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000081 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070082 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000083
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070085 std::unique_ptr<MockDelayPeakDetector> mock(
86 new MockDelayPeakDetector(tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 mock_delay_peak_detector_ = mock.get();
88 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
89 deps.delay_peak_detector = std::move(mock);
90 }
91 delay_peak_detector_ = deps.delay_peak_detector.get();
92
93 if (use_mock_delay_manager_) {
94 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
henrik.lundin8f8c96d2016-04-28 23:19:20 -070095 config_.max_packets_in_buffer, delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070096 mock_delay_manager_ = mock.get();
97 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
98 deps.delay_manager = std::move(mock);
99 }
100 delay_manager_ = deps.delay_manager.get();
101
102 if (use_mock_dtmf_buffer_) {
103 std::unique_ptr<MockDtmfBuffer> mock(
104 new MockDtmfBuffer(config_.sample_rate_hz));
105 mock_dtmf_buffer_ = mock.get();
106 deps.dtmf_buffer = std::move(mock);
107 }
108 dtmf_buffer_ = deps.dtmf_buffer.get();
109
110 if (use_mock_dtmf_tone_generator_) {
111 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
112 mock_dtmf_tone_generator_ = mock.get();
113 deps.dtmf_tone_generator = std::move(mock);
114 }
115 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
116
117 if (use_mock_packet_buffer_) {
118 std::unique_ptr<MockPacketBuffer> mock(
119 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
120 mock_packet_buffer_ = mock.get();
121 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700122 }
123 packet_buffer_ = deps.packet_buffer.get();
124
125 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700126 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700127 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700128 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700129 }
ossua70695a2016-09-22 02:06:28 -0700130 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700131
132 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
133 new TimestampScaler(*deps.decoder_database.get()));
134
135 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000136 ASSERT_TRUE(neteq_ != NULL);
137 }
138
139 void UseNoMocks() {
140 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
141 use_mock_buffer_level_filter_ = false;
142 use_mock_decoder_database_ = false;
143 use_mock_delay_peak_detector_ = false;
144 use_mock_delay_manager_ = false;
145 use_mock_dtmf_buffer_ = false;
146 use_mock_dtmf_tone_generator_ = false;
147 use_mock_packet_buffer_ = false;
148 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000149 }
150
151 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000152 if (use_mock_buffer_level_filter_) {
153 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
154 }
155 if (use_mock_decoder_database_) {
156 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
157 }
158 if (use_mock_delay_manager_) {
159 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
160 }
161 if (use_mock_delay_peak_detector_) {
162 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
163 }
164 if (use_mock_dtmf_buffer_) {
165 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
166 }
167 if (use_mock_dtmf_tone_generator_) {
168 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
169 }
170 if (use_mock_packet_buffer_) {
171 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
172 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 }
174
solenberg2779bab2016-11-17 04:45:19 -0800175 void TestDtmfPacket(NetEqDecoder decoder_type) {
176 const size_t kPayloadLength = 4;
177 const uint8_t kPayloadType = 110;
178 const uint32_t kReceiveTime = 17;
179 const int kSampleRateHz = 16000;
180 config_.sample_rate_hz = kSampleRateHz;
181 UseNoMocks();
182 CreateInstance();
183 // Event: 2, E bit, Volume: 17, Length: 4336.
184 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700185 RTPHeader rtp_header;
186 rtp_header.payloadType = kPayloadType;
187 rtp_header.sequenceNumber = 0x1234;
188 rtp_header.timestamp = 0x12345678;
189 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800190
191 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
192 decoder_type, "telephone-event", kPayloadType));
193
194 // Insert first packet.
195 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700196 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800197
198 // Pull audio once.
199 const size_t kMaxOutputSize =
200 static_cast<size_t>(10 * kSampleRateHz / 1000);
201 AudioFrame output;
202 bool muted;
203 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
204 ASSERT_FALSE(muted);
205 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
206 EXPECT_EQ(1u, output.num_channels_);
207 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
208
209 // Verify first 64 samples of actual output.
210 const std::vector<int16_t> kOutput({
211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
212 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
213 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
214 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
215 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
216 -2534, -1163 });
217 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700218 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800219 }
220
henrik.lundin1d9061e2016-04-26 12:19:34 -0700221 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000222 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700223 TickTimer* tick_timer_ = nullptr;
224 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
225 BufferLevelFilter* buffer_level_filter_ = nullptr;
226 bool use_mock_buffer_level_filter_ = true;
227 MockDecoderDatabase* mock_decoder_database_ = nullptr;
228 DecoderDatabase* decoder_database_ = nullptr;
229 bool use_mock_decoder_database_ = true;
230 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
231 DelayPeakDetector* delay_peak_detector_ = nullptr;
232 bool use_mock_delay_peak_detector_ = true;
233 MockDelayManager* mock_delay_manager_ = nullptr;
234 DelayManager* delay_manager_ = nullptr;
235 bool use_mock_delay_manager_ = true;
236 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
237 DtmfBuffer* dtmf_buffer_ = nullptr;
238 bool use_mock_dtmf_buffer_ = true;
239 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
240 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
241 bool use_mock_dtmf_tone_generator_ = true;
242 MockPacketBuffer* mock_packet_buffer_ = nullptr;
243 PacketBuffer* packet_buffer_ = nullptr;
244 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700245 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
246 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700247 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248};
249
250
251// This tests the interface class NetEq.
252// TODO(hlundin): Move to separate file?
253TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000254 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700255 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 delete neteq;
257}
258
kwiberg5adaf732016-10-04 09:33:27 -0700259TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000260 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700262 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800263 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000264 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800265 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
266 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267}
268
kwiberg5adaf732016-10-04 09:33:27 -0700269TEST_F(NetEqImplTest, RegisterPayloadType) {
270 CreateInstance();
271 constexpr int rtp_payload_type = 0;
272 const SdpAudioFormat format("pcmu", 8000, 1);
273 EXPECT_CALL(*mock_decoder_database_,
274 RegisterPayload(rtp_payload_type, format));
275 neteq_->RegisterPayloadType(rtp_payload_type, format);
276}
277
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000279 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000281 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200283 // Check that kOK is returned when database returns kDecoderNotFound, because
284 // removing a payload type that was never registered is not an error.
285 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286}
287
kwiberg6b19b562016-09-20 04:02:25 -0700288TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
289 CreateInstance();
290 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
291 neteq_->RemoveAllPayloadTypes();
292}
293
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000295 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000296 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 const uint8_t kPayloadType = 0;
298 const uint16_t kFirstSequenceNumber = 0x1234;
299 const uint32_t kFirstTimestamp = 0x12345678;
300 const uint32_t kSsrc = 0x87654321;
301 const uint32_t kFirstReceiveTime = 17;
302 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700303 RTPHeader rtp_header;
304 rtp_header.payloadType = kPayloadType;
305 rtp_header.sequenceNumber = kFirstSequenceNumber;
306 rtp_header.timestamp = kFirstTimestamp;
307 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700308 Packet fake_packet;
309 fake_packet.payload_type = kPayloadType;
310 fake_packet.sequence_number = kFirstSequenceNumber;
311 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312
kwibergc0f2dcf2016-05-31 06:28:03 -0700313 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
314 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100315 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800316 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200317 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800318 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700319 EXPECT_EQ("pcmu", format.name);
320
321 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
322 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
323 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
324 // BWE update function called with first packet.
325 EXPECT_CALL(*mock_decoder,
326 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
327 kFirstTimestamp, kFirstReceiveTime));
328 // BWE update function called with second packet.
329 EXPECT_CALL(
330 *mock_decoder,
331 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
332 kFirstTimestamp + 160, kFirstReceiveTime + 155));
333 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
334
335 *dec = std::move(mock_decoder);
336 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200337 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, absl::nullopt,
ossu84bc9852016-08-26 05:41:23 -0700338 mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339
340 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000341 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342 .WillRepeatedly(Return(&info));
343
344 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000345 EXPECT_CALL(*mock_packet_buffer_, Empty())
346 .WillOnce(Return(false)); // Called once after first packet is inserted.
347 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200349 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100351 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
352 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
354 // index) is a pointer, and the variable pointed to is set to kPayloadType.
355 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
356 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700357 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000358 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700359 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360
361 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000362 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 .Times(1);
364
365 // Expectations for delay manager.
366 {
367 // All expectations within this block must be called in this specific order.
368 InSequence sequence; // Dummy variable.
369 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000370 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 .Times(2)
372 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000373 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000375 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
378 .WillOnce(Return(0));
379 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 .WillOnce(Return(0));
381 }
382
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700384 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385
386 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700387 rtp_header.timestamp += 160;
388 rtp_header.sequenceNumber += 1;
389 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390}
391
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000392TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
393 UseNoMocks();
394 CreateInstance();
395
396 const int kPayloadLengthSamples = 80;
397 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
398 const uint8_t kPayloadType = 17; // Just an arbitrary number.
399 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
400 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700401 RTPHeader rtp_header;
402 rtp_header.payloadType = kPayloadType;
403 rtp_header.sequenceNumber = 0x1234;
404 rtp_header.timestamp = 0x12345678;
405 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000406
kwibergee1879c2015-10-29 06:20:28 -0700407 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800408 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000409
410 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700411 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000412 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700413 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
414 rtp_header.timestamp += kPayloadLengthSamples;
415 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000416 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
417 }
418
419 // Insert one more packet and make sure the buffer got flushed. That is, it
420 // should only hold one single packet.
421 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700422 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700423 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700424 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700425 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
426 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000427}
428
solenberg2779bab2016-11-17 04:45:19 -0800429TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
430 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
431}
solenberg99df6c02016-10-11 04:35:34 -0700432
solenberg2779bab2016-11-17 04:45:19 -0800433TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
434 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
435}
solenberg99df6c02016-10-11 04:35:34 -0700436
solenberg2779bab2016-11-17 04:45:19 -0800437TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
438 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
439}
solenberg99df6c02016-10-11 04:35:34 -0700440
solenberg2779bab2016-11-17 04:45:19 -0800441TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
442 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700443}
444
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000445// This test verifies that timestamps propagate from the incoming packets
446// through to the sync buffer and to the playout timestamp.
447TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
448 UseNoMocks();
449 CreateInstance();
450
451 const uint8_t kPayloadType = 17; // Just an arbitrary number.
452 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
453 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700454 const size_t kPayloadLengthSamples =
455 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000456 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
457 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700458 RTPHeader rtp_header;
459 rtp_header.payloadType = kPayloadType;
460 rtp_header.sequenceNumber = 0x1234;
461 rtp_header.timestamp = 0x12345678;
462 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000463
464 // This is a dummy decoder that produces as many output samples as the input
465 // has bytes. The output is an increasing series, starting at 1 for the first
466 // sample, and then increasing by 1 for each sample.
467 class CountingSamplesDecoder : public AudioDecoder {
468 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000469 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000470
471 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100472 int DecodeInternal(const uint8_t* encoded,
473 size_t encoded_len,
474 int /* sample_rate_hz */,
475 int16_t* decoded,
476 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000477 for (size_t i = 0; i < encoded_len; ++i) {
478 decoded[i] = next_value_++;
479 }
480 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200481 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482 }
483
Karl Wiberg43766482015-08-27 15:22:11 +0200484 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000485
kwiberg347d3512016-06-16 01:59:09 -0700486 int SampleRateHz() const override { return kSampleRateHz; }
487
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000488 size_t Channels() const override { return 1; }
489
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000490 uint16_t next_value() const { return next_value_; }
491
492 private:
493 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000494 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000495
kwibergee1879c2015-10-29 06:20:28 -0700496 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
497 &decoder_, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700498 "dummy name", kPayloadType));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000499
500 // Insert one packet.
501 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700502 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000503
504 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700505 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800506 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700507 bool muted;
508 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
509 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800510 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
511 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800512 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000513
514 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700515 EXPECT_EQ(kPayloadLengthSamples,
516 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000517
518 // The value of the last of the output samples is the same as the number of
519 // samples played from the decoded packet. Thus, this number + the RTP
520 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200521 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700522 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200523 absl::optional<uint32_t>(rtp_header.timestamp +
524 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700525 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000526
527 // Check the timestamp for the last value in the sync buffer. This should
528 // be one full frame length ahead of the RTP timestamp.
529 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
530 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700531 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000532 sync_buffer->end_timestamp());
533
534 // Check that the number of samples still to play from the sync buffer add
535 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800536 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700537 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800538 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000539}
540
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000541TEST_F(NetEqImplTest, ReorderedPacket) {
542 UseNoMocks();
543 CreateInstance();
544
545 const uint8_t kPayloadType = 17; // Just an arbitrary number.
546 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
547 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700548 const size_t kPayloadLengthSamples =
549 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000550 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
551 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700552 RTPHeader rtp_header;
553 rtp_header.payloadType = kPayloadType;
554 rtp_header.sequenceNumber = 0x1234;
555 rtp_header.timestamp = 0x12345678;
556 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000557
558 // Create a mock decoder object.
559 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200560 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700561 EXPECT_CALL(mock_decoder, SampleRateHz())
562 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000563 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000564 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
565 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700566 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200567 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000568 int16_t dummy_output[kPayloadLengthSamples] = {0};
569 // The below expectation will make the mock decoder write
570 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100571 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
572 kSampleRateHz, _, _))
573 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000574 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100575 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200576 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
kwibergee1879c2015-10-29 06:20:28 -0700577 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
578 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700579 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000580
581 // Insert one packet.
582 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700583 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584
585 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700586 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800587 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700588 bool muted;
589 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800590 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
591 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800592 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000593
594 // Insert two more packets. The first one is out of order, and is already too
595 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700596 rtp_header.sequenceNumber -= 1;
597 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598 payload[0] = 1;
599 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700600 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
601 rtp_header.sequenceNumber += 2;
602 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 payload[0] = 2;
604 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000606
607 // Expect only the second packet to be decoded (the one with "2" as the first
608 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100609 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
610 kSampleRateHz, _, _))
611 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000612 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100613 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200614 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000615
616 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800618 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
619 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800620 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000621
622 // Now check the packet buffer, and make sure it is empty, since the
623 // out-of-order packet should have been discarded.
624 EXPECT_TRUE(packet_buffer_->Empty());
625
626 EXPECT_CALL(mock_decoder, Die());
627}
628
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000629// This test verifies that NetEq can handle the situation where the first
630// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000631TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000632 UseNoMocks();
633 CreateInstance();
634
635 const uint8_t kPayloadType = 17; // Just an arbitrary number.
636 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
637 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700638 const size_t kPayloadLengthSamples =
639 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700640 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000641 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700642 RTPHeader rtp_header;
643 rtp_header.payloadType = kPayloadType;
644 rtp_header.sequenceNumber = 0x1234;
645 rtp_header.timestamp = 0x12345678;
646 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000647
648 // Insert one packet. Note that we have not registered any payload type, so
649 // this packet will be rejected.
650 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700651 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000652
653 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700654 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800655 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700656 bool muted;
657 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800658 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
659 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
660 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800661 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000662
663 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700664 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800665 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000666
667 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700668 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700669 rtp_header.sequenceNumber++;
670 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700672 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000673 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
674 }
675
676 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700678 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800679 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
680 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
681 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800682 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000683 << "NetEq did not decode the packets as expected.";
684 }
685}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000686
687// This test verifies that NetEq can handle comfort noise and enters/quits codec
688// internal CNG mode properly.
689TEST_F(NetEqImplTest, CodecInternalCng) {
690 UseNoMocks();
691 CreateInstance();
692
693 const uint8_t kPayloadType = 17; // Just an arbitrary number.
694 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
695 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700696 const size_t kPayloadLengthSamples =
697 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
698 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000699 uint8_t payload[kPayloadLengthBytes] = {0};
700 int16_t dummy_output[kPayloadLengthSamples] = {0};
701
henrik.lundin246ef3e2017-04-24 09:14:32 -0700702 RTPHeader rtp_header;
703 rtp_header.payloadType = kPayloadType;
704 rtp_header.sequenceNumber = 0x1234;
705 rtp_header.timestamp = 0x12345678;
706 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000707
708 // Create a mock decoder object.
709 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200710 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700711 EXPECT_CALL(mock_decoder, SampleRateHz())
712 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000713 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000714 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
715 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700716 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200717 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700718 // Packed duration when asking the decoder for more CNG data (without a new
719 // packet).
720 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200721 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000722
723 // Pointee(x) verifies that first byte of the payload equals x, this makes it
724 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100725 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
726 kSampleRateKhz * 1000, _, _))
727 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000728 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100729 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200730 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000731
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100732 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
733 kSampleRateKhz * 1000, _, _))
734 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000735 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100736 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200737 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000738
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100739 EXPECT_CALL(mock_decoder,
740 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
741 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000742 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100743 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200744 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000745
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100746 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
747 kSampleRateKhz * 1000, _, _))
748 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000749 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100750 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200751 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000752
Karl Wibergd8399e62015-05-25 14:39:56 +0200753 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700754 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700755 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756
757 // Insert one packet (decoder will return speech).
758 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700759 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000760
761 // Insert second packet (decoder will return CNG).
762 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700763 rtp_header.sequenceNumber++;
764 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700766 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000767
Peter Kastingdce40cf2015-08-24 14:52:23 -0700768 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800769 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800770 AudioFrame::SpeechType expected_type[8] = {
771 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
772 AudioFrame::kCNG, AudioFrame::kCNG,
773 AudioFrame::kCNG, AudioFrame::kCNG,
774 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000775 };
776 int expected_timestamp_increment[8] = {
777 -1, // will not be used.
778 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700779 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
780 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000781 50 * kSampleRateKhz, 10 * kSampleRateKhz
782 };
783
henrik.lundin7a926812016-05-12 13:51:28 -0700784 bool muted;
785 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200786 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700787 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000788
henrik.lundin0d96ab72016-04-06 12:28:26 -0700789 // Lambda for verifying the timestamps.
790 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200791 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700792 if (expected_timestamp_increment[i] == -1) {
793 // Expect to get an empty timestamp value during CNG and PLC.
794 EXPECT_FALSE(ts) << "i = " << i;
795 } else {
796 ASSERT_TRUE(ts) << "i = " << i;
797 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
798 << "i = " << i;
799 last_timestamp = ts;
800 }
801 };
802
Peter Kastingdce40cf2015-08-24 14:52:23 -0700803 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800804 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
805 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800806 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700807 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700808 SCOPED_TRACE("");
809 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000810 }
811
812 // Insert third packet, which leaves a gap from last packet.
813 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700814 rtp_header.sequenceNumber += 2;
815 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000816 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700817 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000818
Peter Kastingdce40cf2015-08-24 14:52:23 -0700819 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800820 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
821 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800822 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700823 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700824 SCOPED_TRACE("");
825 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000826 }
827
828 // Now check the packet buffer, and make sure it is empty.
829 EXPECT_TRUE(packet_buffer_->Empty());
830
831 EXPECT_CALL(mock_decoder, Die());
832}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000833
834TEST_F(NetEqImplTest, UnsupportedDecoder) {
835 UseNoMocks();
836 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700837 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800838 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000839
840 const uint8_t kPayloadType = 17; // Just an arbitrary number.
841 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
842 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000843
Peter Kastingdce40cf2015-08-24 14:52:23 -0700844 const size_t kPayloadLengthSamples =
845 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000846 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700847 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200848 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700849 RTPHeader rtp_header;
850 rtp_header.payloadType = kPayloadType;
851 rtp_header.sequenceNumber = 0x1234;
852 rtp_header.timestamp = 0x12345678;
853 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000854
ossu61a208b2016-09-20 01:38:00 -0700855 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000856
857 const uint8_t kFirstPayloadValue = 1;
858 const uint8_t kSecondPayloadValue = 2;
859
ossu61a208b2016-09-20 01:38:00 -0700860 EXPECT_CALL(decoder,
861 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
862 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200863 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000864
ossu61a208b2016-09-20 01:38:00 -0700865 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000866 .Times(0);
867
ossu61a208b2016-09-20 01:38:00 -0700868 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
869 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000870 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700871 .WillOnce(DoAll(
872 SetArrayArgument<3>(dummy_output,
873 dummy_output + kPayloadLengthSamples * kChannels),
874 SetArgPointee<4>(AudioDecoder::kSpeech),
875 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000876
ossu61a208b2016-09-20 01:38:00 -0700877 EXPECT_CALL(decoder,
878 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
879 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200880 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700881
882 EXPECT_CALL(decoder, SampleRateHz())
883 .WillRepeatedly(Return(kSampleRateHz));
884
885 EXPECT_CALL(decoder, Channels())
886 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000887
kwibergee1879c2015-10-29 06:20:28 -0700888 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700889 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700890 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000891
892 // Insert one packet.
893 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
894 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700895 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897 // Insert another packet.
898 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700899 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000900 // The second timestamp needs to be at least 30 ms after the first to make
901 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700902 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000903 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905
henrik.lundin6d8e0112016-03-04 10:34:21 -0800906 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700907 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800908 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200909 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700910 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 // Output size and number of channels should be correct.
912 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
913 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
914 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000915
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 // Second call to GetAudio will decode the packet that is ok. No errors are
917 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700918 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800919 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
920 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700921
922 // Die isn't called through NiceMock (since it's called by the
923 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
924 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000925}
926
henrik.lundin116c84e2015-08-27 13:14:48 -0700927// This test inserts packets until the buffer is flushed. After that, it asks
928// NetEq for the network statistics. The purpose of the test is to make sure
929// that even though the buffer size increment is negative (which it becomes when
930// the packet causing a flush is inserted), the packet length stored in the
931// decision logic remains valid.
932TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
933 UseNoMocks();
934 CreateInstance();
935
936 const size_t kPayloadLengthSamples = 80;
937 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
938 const uint8_t kPayloadType = 17; // Just an arbitrary number.
939 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
940 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700941 RTPHeader rtp_header;
942 rtp_header.payloadType = kPayloadType;
943 rtp_header.sequenceNumber = 0x1234;
944 rtp_header.timestamp = 0x12345678;
945 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700946
kwibergee1879c2015-10-29 06:20:28 -0700947 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800948 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700949
950 // Insert packets until the buffer flushes.
951 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
952 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
953 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700954 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
955 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
956 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700957 }
958 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
959
960 // Ask for network statistics. This should not crash.
961 NetEqNetworkStatistics stats;
962 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
963}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200964
965TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
966 UseNoMocks();
967 CreateInstance();
968
969 const uint8_t kPayloadType = 17; // Just an arbitrary number.
970 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
971 const int kSampleRateHz = 8000;
972 const size_t kPayloadLengthSamples =
973 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
974 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
975 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700976 RTPHeader rtp_header;
977 rtp_header.payloadType = kPayloadType;
978 rtp_header.sequenceNumber = 0x1234;
979 rtp_header.timestamp = 0x12345678;
980 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200981
982 // Create a mock decoder object.
983 MockAudioDecoder mock_decoder;
984 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700985 EXPECT_CALL(mock_decoder, SampleRateHz())
986 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200987 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
988 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
989 .WillRepeatedly(Return(0));
990 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200991 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200992 int16_t dummy_output[kPayloadLengthSamples] = {0};
993 // The below expectation will make the mock decoder write
994 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
995 // speech. That is, the decoded length is 5 samples shorter than the expected.
996 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100997 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200998 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100999 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001000 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001001 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001002 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
kwibergee1879c2015-10-29 06:20:28 -07001003 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1004 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001005 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001006
1007 // Insert one packet.
1008 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001009 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001010
1011 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1012
1013 // Pull audio once.
1014 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001015 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001016 bool muted;
1017 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001018 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1019 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001020 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001021
1022 EXPECT_CALL(mock_decoder, Die());
1023}
minyuel6d92bf52015-09-23 15:20:39 +02001024
1025// This test checks the behavior of NetEq when audio decoder fails.
1026TEST_F(NetEqImplTest, DecodingError) {
1027 UseNoMocks();
1028 CreateInstance();
1029
1030 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1031 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1032 const int kSampleRateHz = 8000;
1033 const int kDecoderErrorCode = -97; // Any negative number.
1034
1035 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1036 const size_t kFrameLengthSamples =
1037 static_cast<size_t>(5 * kSampleRateHz / 1000);
1038
1039 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1040
1041 uint8_t payload[kPayloadLengthBytes] = {0};
1042
henrik.lundin246ef3e2017-04-24 09:14:32 -07001043 RTPHeader rtp_header;
1044 rtp_header.payloadType = kPayloadType;
1045 rtp_header.sequenceNumber = 0x1234;
1046 rtp_header.timestamp = 0x12345678;
1047 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001048
1049 // Create a mock decoder object.
1050 MockAudioDecoder mock_decoder;
1051 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001052 EXPECT_CALL(mock_decoder, SampleRateHz())
1053 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001054 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1055 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1056 .WillRepeatedly(Return(0));
1057 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001058 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001059 EXPECT_CALL(mock_decoder, ErrorCode())
1060 .WillOnce(Return(kDecoderErrorCode));
1061 EXPECT_CALL(mock_decoder, HasDecodePlc())
1062 .WillOnce(Return(false));
1063 int16_t dummy_output[kFrameLengthSamples] = {0};
1064
1065 {
1066 InSequence sequence; // Dummy variable.
1067 // Mock decoder works normally the first time.
1068 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001069 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001070 .Times(3)
1071 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001072 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001073 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001074 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001075 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001076 .RetiresOnSaturation();
1077
1078 // Then mock decoder fails. A common reason for failure can be buffer being
1079 // too short
1080 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001081 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001082 .WillOnce(Return(-1))
1083 .RetiresOnSaturation();
1084
1085 // Mock decoder finally returns to normal.
1086 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001087 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001088 .Times(2)
1089 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001090 DoAll(SetArrayArgument<3>(dummy_output,
1091 dummy_output + kFrameLengthSamples),
1092 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001093 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001094 }
1095
kwibergee1879c2015-10-29 06:20:28 -07001096 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1097 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001098 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001099
1100 // Insert packets.
1101 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001102 rtp_header.sequenceNumber += 1;
1103 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001104 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001105 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001106 }
1107
1108 // Pull audio.
1109 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001110 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001111 bool muted;
1112 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001113 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1114 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001115 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001116
1117 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001118 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001119 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1120 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001121 // We are not expecting anything for output.speech_type_, since an error was
1122 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001123
1124 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001125 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001126 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1127 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001128 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001129
1130 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001131 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001132 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1133 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001134 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001135
1136 EXPECT_CALL(mock_decoder, Die());
1137}
1138
1139// This test checks the behavior of NetEq when audio decoder fails during CNG.
1140TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1141 UseNoMocks();
1142 CreateInstance();
1143
1144 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1145 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1146 const int kSampleRateHz = 8000;
1147 const int kDecoderErrorCode = -97; // Any negative number.
1148
1149 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1150 const size_t kFrameLengthSamples =
1151 static_cast<size_t>(5 * kSampleRateHz / 1000);
1152
1153 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1154
1155 uint8_t payload[kPayloadLengthBytes] = {0};
1156
henrik.lundin246ef3e2017-04-24 09:14:32 -07001157 RTPHeader rtp_header;
1158 rtp_header.payloadType = kPayloadType;
1159 rtp_header.sequenceNumber = 0x1234;
1160 rtp_header.timestamp = 0x12345678;
1161 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001162
1163 // Create a mock decoder object.
1164 MockAudioDecoder mock_decoder;
1165 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001166 EXPECT_CALL(mock_decoder, SampleRateHz())
1167 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001168 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1169 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1170 .WillRepeatedly(Return(0));
1171 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001172 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001173 EXPECT_CALL(mock_decoder, ErrorCode())
1174 .WillOnce(Return(kDecoderErrorCode));
1175 int16_t dummy_output[kFrameLengthSamples] = {0};
1176
1177 {
1178 InSequence sequence; // Dummy variable.
1179 // Mock decoder works normally the first 2 times.
1180 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001181 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001182 .Times(2)
1183 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001184 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001185 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001186 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001187 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001188 .RetiresOnSaturation();
1189
1190 // Then mock decoder fails. A common reason for failure can be buffer being
1191 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001192 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001193 .WillOnce(Return(-1))
1194 .RetiresOnSaturation();
1195
1196 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001197 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001198 .Times(2)
1199 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001200 DoAll(SetArrayArgument<3>(dummy_output,
1201 dummy_output + kFrameLengthSamples),
1202 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001203 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001204 }
1205
kwibergee1879c2015-10-29 06:20:28 -07001206 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1207 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001208 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001209
1210 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1211 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001212 rtp_header.sequenceNumber += 1;
1213 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001214 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001215 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001216 }
1217
1218 // Pull audio.
1219 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001220 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001221 bool muted;
1222 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001223 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1224 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001225 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001226
1227 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001228 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1230 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001231 // We are not expecting anything for output.speech_type_, since an error was
1232 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001233
1234 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001235 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 EXPECT_CALL(mock_decoder, Die());
1241}
1242
henrik.lundind89814b2015-11-23 06:49:25 -08001243// Tests that the return value from last_output_sample_rate_hz() is equal to the
1244// configured inital sample rate.
1245TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1246 UseNoMocks();
1247 config_.sample_rate_hz = 48000;
1248 CreateInstance();
1249 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1250}
1251
henrik.lundined497212016-04-25 10:11:38 -07001252TEST_F(NetEqImplTest, TickTimerIncrement) {
1253 UseNoMocks();
1254 CreateInstance();
1255 ASSERT_TRUE(tick_timer_);
1256 EXPECT_EQ(0u, tick_timer_->ticks());
1257 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001258 bool muted;
1259 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001260 EXPECT_EQ(1u, tick_timer_->ticks());
1261}
1262
henrik.lundin114c1b32017-04-26 07:47:32 -07001263TEST_F(NetEqImplTest, TargetDelayMs) {
1264 UseNoMocks();
1265 use_mock_delay_manager_ = true;
1266 CreateInstance();
1267 // Let the dummy target delay be 17 packets.
1268 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1269 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1270 .WillOnce(Return(kTargetLevelPacketsQ8));
1271 // Default packet size before any packet has been decoded is 30 ms, so we are
1272 // expecting 17 * 30 = 510 ms target delay.
1273 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1274}
1275
henrik.lundinb8c55b12017-05-10 07:38:01 -07001276TEST_F(NetEqImplTest, InsertEmptyPacket) {
1277 UseNoMocks();
1278 use_mock_delay_manager_ = true;
1279 CreateInstance();
1280
1281 RTPHeader rtp_header;
1282 rtp_header.payloadType = 17;
1283 rtp_header.sequenceNumber = 0x1234;
1284 rtp_header.timestamp = 0x12345678;
1285 rtp_header.ssrc = 0x87654321;
1286
1287 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1288 neteq_->InsertEmptyPacket(rtp_header);
1289}
1290
minyue5bd33972016-05-02 04:46:11 -07001291class Decoder120ms : public AudioDecoder {
1292 public:
kwiberg347d3512016-06-16 01:59:09 -07001293 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1294 : sample_rate_hz_(sample_rate_hz),
1295 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001296 speech_type_(speech_type) {}
1297
1298 int DecodeInternal(const uint8_t* encoded,
1299 size_t encoded_len,
1300 int sample_rate_hz,
1301 int16_t* decoded,
1302 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001303 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001304 size_t decoded_len =
1305 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1306 for (size_t i = 0; i < decoded_len; ++i) {
1307 decoded[i] = next_value_++;
1308 }
1309 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001310 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001311 }
1312
1313 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001314 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001315 size_t Channels() const override { return 2; }
1316
1317 private:
kwiberg347d3512016-06-16 01:59:09 -07001318 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001319 int16_t next_value_;
1320 SpeechType speech_type_;
1321};
1322
1323class NetEqImplTest120ms : public NetEqImplTest {
1324 protected:
1325 NetEqImplTest120ms() : NetEqImplTest() {}
1326 virtual ~NetEqImplTest120ms() {}
1327
1328 void CreateInstanceNoMocks() {
1329 UseNoMocks();
1330 CreateInstance();
1331 }
1332
1333 void CreateInstanceWithDelayManagerMock() {
1334 UseNoMocks();
1335 use_mock_delay_manager_ = true;
1336 CreateInstance();
1337 }
1338
1339 uint32_t timestamp_diff_between_packets() const {
1340 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1341 }
1342
1343 uint32_t first_timestamp() const { return 10u; }
1344
1345 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001346 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001347 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001348 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1349 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001350 }
1351 }
1352
1353 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001354 RTPHeader rtp_header;
1355 rtp_header.payloadType = kPayloadType;
1356 rtp_header.sequenceNumber = sequence_number_;
1357 rtp_header.timestamp = timestamp;
1358 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001359 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1360 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001361 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001362 sequence_number_++;
1363 }
1364
1365 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
kwiberg347d3512016-06-16 01:59:09 -07001366 decoder_.reset(new Decoder120ms(kSamplingFreq_, speech_type));
minyue5bd33972016-05-02 04:46:11 -07001367 ASSERT_EQ(2u, decoder_->Channels());
1368 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1369 decoder_.get(), NetEqDecoder::kDecoderOpus_2ch,
kwiberg342f7402016-06-16 03:18:00 -07001370 "120ms codec", kPayloadType));
minyue5bd33972016-05-02 04:46:11 -07001371 }
1372
1373 std::unique_ptr<Decoder120ms> decoder_;
1374 AudioFrame output_;
1375 const uint32_t kPayloadType = 17;
1376 const uint32_t kSamplingFreq_ = 48000;
1377 uint16_t sequence_number_ = 1;
1378};
1379
minyue5bd33972016-05-02 04:46:11 -07001380TEST_F(NetEqImplTest120ms, CodecInternalCng) {
1381 CreateInstanceNoMocks();
1382 Register120msCodec(AudioDecoder::kComfortNoise);
1383
1384 InsertPacket(first_timestamp());
1385 GetFirstPacket();
1386
henrik.lundin7a926812016-05-12 13:51:28 -07001387 bool muted;
1388 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001389 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1390}
1391
1392TEST_F(NetEqImplTest120ms, Normal) {
1393 CreateInstanceNoMocks();
1394 Register120msCodec(AudioDecoder::kSpeech);
1395
1396 InsertPacket(first_timestamp());
1397 GetFirstPacket();
1398
1399 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1400}
1401
1402TEST_F(NetEqImplTest120ms, Merge) {
1403 CreateInstanceWithDelayManagerMock();
1404
1405 Register120msCodec(AudioDecoder::kSpeech);
1406 InsertPacket(first_timestamp());
1407
1408 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001409 bool muted;
1410 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001411
1412 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1413
1414 // Delay manager reports a target level which should cause a Merge.
1415 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1416
henrik.lundin7a926812016-05-12 13:51:28 -07001417 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001418 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1419}
1420
1421TEST_F(NetEqImplTest120ms, Expand) {
1422 CreateInstanceNoMocks();
1423 Register120msCodec(AudioDecoder::kSpeech);
1424
1425 InsertPacket(first_timestamp());
1426 GetFirstPacket();
1427
henrik.lundin7a926812016-05-12 13:51:28 -07001428 bool muted;
1429 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001430 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1431}
1432
1433TEST_F(NetEqImplTest120ms, FastAccelerate) {
1434 CreateInstanceWithDelayManagerMock();
1435 Register120msCodec(AudioDecoder::kSpeech);
1436
1437 InsertPacket(first_timestamp());
1438 GetFirstPacket();
1439 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1440
1441 // Delay manager report buffer limit which should cause a FastAccelerate.
1442 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1443 .Times(1)
1444 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
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(kFastAccelerate, neteq_->last_operation_for_test());
1449}
1450
1451TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
1452 CreateInstanceWithDelayManagerMock();
1453 Register120msCodec(AudioDecoder::kSpeech);
1454
1455 InsertPacket(first_timestamp());
1456 GetFirstPacket();
1457
1458 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1459
1460 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1461 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1462 .Times(1)
1463 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1464
henrik.lundin7a926812016-05-12 13:51:28 -07001465 bool muted;
1466 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001467 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1468}
1469
1470TEST_F(NetEqImplTest120ms, Accelerate) {
1471 CreateInstanceWithDelayManagerMock();
1472 Register120msCodec(AudioDecoder::kSpeech);
1473
1474 InsertPacket(first_timestamp());
1475 GetFirstPacket();
1476
1477 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1478
1479 // Delay manager report buffer limit which should cause a Accelerate.
1480 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1481 .Times(1)
1482 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1483
henrik.lundin7a926812016-05-12 13:51:28 -07001484 bool muted;
1485 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001486 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1487}
1488
minyuel6d92bf52015-09-23 15:20:39 +02001489}// namespace webrtc