blob: fafc2df03e9038ae68bb8392b63ecbc745374d7f [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
kwiberg087bd342017-02-10 08:15:44 -080013#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
henrik.lundin246ef3e2017-04-24 09:14:32 -070014#include "webrtc/common_types.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/accelerate.h"
16#include "webrtc/modules/audio_coding/neteq/expand.h"
kwiberg087bd342017-02-10 08:15:44 -080017#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000018#include "webrtc/modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
19#include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
20#include "webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h"
21#include "webrtc/modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
22#include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
23#include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
24#include "webrtc/modules/audio_coding/neteq/mock/mock_packet_buffer.h"
ossua70695a2016-09-22 02:06:28 -070025#include "webrtc/modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
kwiberg087bd342017-02-10 08:15:44 -080026#include "webrtc/modules/audio_coding/neteq/neteq_impl.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000027#include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
28#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
29#include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080030#include "webrtc/modules/include/module_common_types.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020031#include "webrtc/rtc_base/safe_conversions.h"
kwibergac9f8762016-09-30 22:29:43 -070032#include "webrtc/test/gmock.h"
33#include "webrtc/test/gtest.h"
kwiberg37e99fd2017-04-10 05:15:48 -070034#include "webrtc/test/mock_audio_decoder.h"
35#include "webrtc/test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000037using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038using ::testing::Return;
39using ::testing::ReturnNull;
40using ::testing::_;
41using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000042using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043using ::testing::InSequence;
44using ::testing::Invoke;
45using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000046using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000047using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048
49namespace webrtc {
50
51// This function is called when inserting a packet list into the mock packet
52// buffer. The purpose is to delete all inserted packets properly, to avoid
53// memory leaks in the test.
54int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070055 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 return PacketBuffer::kOK;
57}
58
59class NetEqImplTest : public ::testing::Test {
60 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070061 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000062
63 void CreateInstance() {
ossue3525782016-05-25 07:37:43 -070064 NetEqImpl::Dependencies deps(config_, CreateBuiltinAudioDecoderFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070065
66 // Get a local pointer to NetEq's TickTimer object.
67 tick_timer_ = deps.tick_timer.get();
68
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000069 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070070 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
71 mock_buffer_level_filter_ = mock.get();
72 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000073 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070074 buffer_level_filter_ = deps.buffer_level_filter.get();
75
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000076 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
78 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
80 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070081 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070083 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000084
henrik.lundin1d9061e2016-04-26 12:19:34 -070085 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070086 std::unique_ptr<MockDelayPeakDetector> mock(
87 new MockDelayPeakDetector(tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 mock_delay_peak_detector_ = mock.get();
89 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
90 deps.delay_peak_detector = std::move(mock);
91 }
92 delay_peak_detector_ = deps.delay_peak_detector.get();
93
94 if (use_mock_delay_manager_) {
95 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
henrik.lundin8f8c96d2016-04-28 23:19:20 -070096 config_.max_packets_in_buffer, delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070097 mock_delay_manager_ = mock.get();
98 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
99 deps.delay_manager = std::move(mock);
100 }
101 delay_manager_ = deps.delay_manager.get();
102
103 if (use_mock_dtmf_buffer_) {
104 std::unique_ptr<MockDtmfBuffer> mock(
105 new MockDtmfBuffer(config_.sample_rate_hz));
106 mock_dtmf_buffer_ = mock.get();
107 deps.dtmf_buffer = std::move(mock);
108 }
109 dtmf_buffer_ = deps.dtmf_buffer.get();
110
111 if (use_mock_dtmf_tone_generator_) {
112 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
113 mock_dtmf_tone_generator_ = mock.get();
114 deps.dtmf_tone_generator = std::move(mock);
115 }
116 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
117
118 if (use_mock_packet_buffer_) {
119 std::unique_ptr<MockPacketBuffer> mock(
120 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
121 mock_packet_buffer_ = mock.get();
122 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700123 }
124 packet_buffer_ = deps.packet_buffer.get();
125
126 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700127 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700128 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700129 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700130 }
ossua70695a2016-09-22 02:06:28 -0700131 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132
133 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
134 new TimestampScaler(*deps.decoder_database.get()));
135
136 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000137 ASSERT_TRUE(neteq_ != NULL);
138 }
139
140 void UseNoMocks() {
141 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
142 use_mock_buffer_level_filter_ = false;
143 use_mock_decoder_database_ = false;
144 use_mock_delay_peak_detector_ = false;
145 use_mock_delay_manager_ = false;
146 use_mock_dtmf_buffer_ = false;
147 use_mock_dtmf_tone_generator_ = false;
148 use_mock_packet_buffer_ = false;
149 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150 }
151
152 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000153 if (use_mock_buffer_level_filter_) {
154 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
155 }
156 if (use_mock_decoder_database_) {
157 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
158 }
159 if (use_mock_delay_manager_) {
160 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
161 }
162 if (use_mock_delay_peak_detector_) {
163 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
164 }
165 if (use_mock_dtmf_buffer_) {
166 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
167 }
168 if (use_mock_dtmf_tone_generator_) {
169 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
170 }
171 if (use_mock_packet_buffer_) {
172 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
173 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175
solenberg2779bab2016-11-17 04:45:19 -0800176 void TestDtmfPacket(NetEqDecoder decoder_type) {
177 const size_t kPayloadLength = 4;
178 const uint8_t kPayloadType = 110;
179 const uint32_t kReceiveTime = 17;
180 const int kSampleRateHz = 16000;
181 config_.sample_rate_hz = kSampleRateHz;
182 UseNoMocks();
183 CreateInstance();
184 // Event: 2, E bit, Volume: 17, Length: 4336.
185 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700186 RTPHeader rtp_header;
187 rtp_header.payloadType = kPayloadType;
188 rtp_header.sequenceNumber = 0x1234;
189 rtp_header.timestamp = 0x12345678;
190 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800191
192 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
193 decoder_type, "telephone-event", kPayloadType));
194
195 // Insert first packet.
196 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700197 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800198
199 // Pull audio once.
200 const size_t kMaxOutputSize =
201 static_cast<size_t>(10 * kSampleRateHz / 1000);
202 AudioFrame output;
203 bool muted;
204 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
205 ASSERT_FALSE(muted);
206 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
207 EXPECT_EQ(1u, output.num_channels_);
208 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
209
210 // Verify first 64 samples of actual output.
211 const std::vector<int16_t> kOutput({
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
213 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
214 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
215 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
216 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
217 -2534, -1163 });
218 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700219 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800220 }
221
henrik.lundin1d9061e2016-04-26 12:19:34 -0700222 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000223 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700224 TickTimer* tick_timer_ = nullptr;
225 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
226 BufferLevelFilter* buffer_level_filter_ = nullptr;
227 bool use_mock_buffer_level_filter_ = true;
228 MockDecoderDatabase* mock_decoder_database_ = nullptr;
229 DecoderDatabase* decoder_database_ = nullptr;
230 bool use_mock_decoder_database_ = true;
231 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
232 DelayPeakDetector* delay_peak_detector_ = nullptr;
233 bool use_mock_delay_peak_detector_ = true;
234 MockDelayManager* mock_delay_manager_ = nullptr;
235 DelayManager* delay_manager_ = nullptr;
236 bool use_mock_delay_manager_ = true;
237 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
238 DtmfBuffer* dtmf_buffer_ = nullptr;
239 bool use_mock_dtmf_buffer_ = true;
240 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
241 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
242 bool use_mock_dtmf_tone_generator_ = true;
243 MockPacketBuffer* mock_packet_buffer_ = nullptr;
244 PacketBuffer* packet_buffer_ = nullptr;
245 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700246 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
247 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700248 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249};
250
251
252// This tests the interface class NetEq.
253// TODO(hlundin): Move to separate file?
254TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000255 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700256 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257 delete neteq;
258}
259
kwiberg5adaf732016-10-04 09:33:27 -0700260TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000261 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000262 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700263 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800264 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000265 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800266 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
267 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268}
269
kwiberg5adaf732016-10-04 09:33:27 -0700270TEST_F(NetEqImplTest, RegisterPayloadType) {
271 CreateInstance();
272 constexpr int rtp_payload_type = 0;
273 const SdpAudioFormat format("pcmu", 8000, 1);
274 EXPECT_CALL(*mock_decoder_database_,
275 RegisterPayload(rtp_payload_type, format));
276 neteq_->RegisterPayloadType(rtp_payload_type, format);
277}
278
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000280 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000282 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200284 // Check that kOK is returned when database returns kDecoderNotFound, because
285 // removing a payload type that was never registered is not an error.
286 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287}
288
kwiberg6b19b562016-09-20 04:02:25 -0700289TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
290 CreateInstance();
291 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
292 neteq_->RemoveAllPayloadTypes();
293}
294
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000296 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000297 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 const uint8_t kPayloadType = 0;
299 const uint16_t kFirstSequenceNumber = 0x1234;
300 const uint32_t kFirstTimestamp = 0x12345678;
301 const uint32_t kSsrc = 0x87654321;
302 const uint32_t kFirstReceiveTime = 17;
303 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700304 RTPHeader rtp_header;
305 rtp_header.payloadType = kPayloadType;
306 rtp_header.sequenceNumber = kFirstSequenceNumber;
307 rtp_header.timestamp = kFirstTimestamp;
308 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700309 Packet fake_packet;
310 fake_packet.payload_type = kPayloadType;
311 fake_packet.sequence_number = kFirstSequenceNumber;
312 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313
kwibergc0f2dcf2016-05-31 06:28:03 -0700314 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
315 new rtc::RefCountedObject<MockAudioDecoderFactory>);
316 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800317 .WillOnce(Invoke([&](const SdpAudioFormat& format,
318 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 }));
ossuf1b08da2016-09-23 02:19:43 -0700337 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu,
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_, NumPacketsInBuffer())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 .WillOnce(Return(0)) // First packet.
347 .WillOnce(Return(1)) // Second packet.
348 .WillOnce(Return(2)); // Second packet, checking after it was inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000349 EXPECT_CALL(*mock_packet_buffer_, Empty())
350 .WillOnce(Return(false)); // Called once after first packet is inserted.
351 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200353 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 .Times(2)
henrik.lundinda8bbf62016-08-31 03:14:11 -0700355 .WillRepeatedly(
356 DoAll(SetArgPointee<2>(rtc::Optional<uint8_t>(kPayloadType)),
357 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
359 // index) is a pointer, and the variable pointed to is set to kPayloadType.
360 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
361 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700362 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000363 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700364 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365
366 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000367 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368 .Times(1);
369
370 // Expectations for delay manager.
371 {
372 // All expectations within this block must be called in this specific order.
373 InSequence sequence; // Dummy variable.
374 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000375 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 .Times(2)
377 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000378 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000380 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000382 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
383 .WillOnce(Return(0));
384 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 .WillOnce(Return(0));
386 }
387
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700389 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390
391 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700392 rtp_header.timestamp += 160;
393 rtp_header.sequenceNumber += 1;
394 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395}
396
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000397TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
398 UseNoMocks();
399 CreateInstance();
400
401 const int kPayloadLengthSamples = 80;
402 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
403 const uint8_t kPayloadType = 17; // Just an arbitrary number.
404 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
405 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700406 RTPHeader rtp_header;
407 rtp_header.payloadType = kPayloadType;
408 rtp_header.sequenceNumber = 0x1234;
409 rtp_header.timestamp = 0x12345678;
410 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000411
kwibergee1879c2015-10-29 06:20:28 -0700412 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800413 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000414
415 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700416 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000417 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700418 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
419 rtp_header.timestamp += kPayloadLengthSamples;
420 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000421 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
422 }
423
424 // Insert one more packet and make sure the buffer got flushed. That is, it
425 // should only hold one single packet.
426 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700427 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700428 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700429 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700430 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
431 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000432}
433
solenberg2779bab2016-11-17 04:45:19 -0800434TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
435 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
436}
solenberg99df6c02016-10-11 04:35:34 -0700437
solenberg2779bab2016-11-17 04:45:19 -0800438TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
439 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
440}
solenberg99df6c02016-10-11 04:35:34 -0700441
solenberg2779bab2016-11-17 04:45:19 -0800442TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
443 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
444}
solenberg99df6c02016-10-11 04:35:34 -0700445
solenberg2779bab2016-11-17 04:45:19 -0800446TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
447 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700448}
449
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000450// This test verifies that timestamps propagate from the incoming packets
451// through to the sync buffer and to the playout timestamp.
452TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
453 UseNoMocks();
454 CreateInstance();
455
456 const uint8_t kPayloadType = 17; // Just an arbitrary number.
457 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
458 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700459 const size_t kPayloadLengthSamples =
460 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000461 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
462 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700463 RTPHeader rtp_header;
464 rtp_header.payloadType = kPayloadType;
465 rtp_header.sequenceNumber = 0x1234;
466 rtp_header.timestamp = 0x12345678;
467 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000468
469 // This is a dummy decoder that produces as many output samples as the input
470 // has bytes. The output is an increasing series, starting at 1 for the first
471 // sample, and then increasing by 1 for each sample.
472 class CountingSamplesDecoder : public AudioDecoder {
473 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000474 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475
476 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100477 int DecodeInternal(const uint8_t* encoded,
478 size_t encoded_len,
479 int /* sample_rate_hz */,
480 int16_t* decoded,
481 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482 for (size_t i = 0; i < encoded_len; ++i) {
483 decoded[i] = next_value_++;
484 }
485 *speech_type = kSpeech;
486 return encoded_len;
487 }
488
Karl Wiberg43766482015-08-27 15:22:11 +0200489 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000490
kwiberg347d3512016-06-16 01:59:09 -0700491 int SampleRateHz() const override { return kSampleRateHz; }
492
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000493 size_t Channels() const override { return 1; }
494
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000495 uint16_t next_value() const { return next_value_; }
496
497 private:
498 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000499 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
kwibergee1879c2015-10-29 06:20:28 -0700501 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
502 &decoder_, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700503 "dummy name", kPayloadType));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000504
505 // Insert one packet.
506 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700507 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000508
509 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700510 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800511 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700512 bool muted;
513 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
514 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800515 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
516 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800517 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000518
519 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700520 EXPECT_EQ(kPayloadLengthSamples,
521 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000522
523 // The value of the last of the output samples is the same as the number of
524 // samples played from the decoded packet. Thus, this number + the RTP
525 // timestamp should match the playout timestamp.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700526 // Wrap the expected value in an rtc::Optional to compare them as such.
527 EXPECT_EQ(
henrik.lundin246ef3e2017-04-24 09:14:32 -0700528 rtc::Optional<uint32_t>(rtp_header.timestamp +
yujo36b1a5f2017-06-12 12:45:32 -0700529 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700530 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000531
532 // Check the timestamp for the last value in the sync buffer. This should
533 // be one full frame length ahead of the RTP timestamp.
534 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
535 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700536 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000537 sync_buffer->end_timestamp());
538
539 // Check that the number of samples still to play from the sync buffer add
540 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800541 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700542 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800543 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000544}
545
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000546TEST_F(NetEqImplTest, ReorderedPacket) {
547 UseNoMocks();
548 CreateInstance();
549
550 const uint8_t kPayloadType = 17; // Just an arbitrary number.
551 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
552 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700553 const size_t kPayloadLengthSamples =
554 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000555 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
556 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700557 RTPHeader rtp_header;
558 rtp_header.payloadType = kPayloadType;
559 rtp_header.sequenceNumber = 0x1234;
560 rtp_header.timestamp = 0x12345678;
561 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000562
563 // Create a mock decoder object.
564 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200565 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700566 EXPECT_CALL(mock_decoder, SampleRateHz())
567 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000568 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000569 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
570 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700571 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
572 .WillRepeatedly(Return(kPayloadLengthSamples));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000573 int16_t dummy_output[kPayloadLengthSamples] = {0};
574 // The below expectation will make the mock decoder write
575 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100576 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
577 kSampleRateHz, _, _))
578 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000579 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100580 SetArgPointee<4>(AudioDecoder::kSpeech),
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000581 Return(kPayloadLengthSamples)));
kwibergee1879c2015-10-29 06:20:28 -0700582 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
583 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700584 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585
586 // Insert one packet.
587 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700588 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000589
590 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700591 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800592 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700593 bool muted;
594 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800595 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
596 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800597 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598
599 // Insert two more packets. The first one is out of order, and is already too
600 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700601 rtp_header.sequenceNumber -= 1;
602 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 payload[0] = 1;
604 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
606 rtp_header.sequenceNumber += 2;
607 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608 payload[0] = 2;
609 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700610 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000611
612 // Expect only the second packet to be decoded (the one with "2" as the first
613 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100614 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
615 kSampleRateHz, _, _))
616 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000617 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100618 SetArgPointee<4>(AudioDecoder::kSpeech),
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000619 Return(kPayloadLengthSamples)));
620
621 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700622 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800623 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
624 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800625 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000626
627 // Now check the packet buffer, and make sure it is empty, since the
628 // out-of-order packet should have been discarded.
629 EXPECT_TRUE(packet_buffer_->Empty());
630
631 EXPECT_CALL(mock_decoder, Die());
632}
633
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000634// This test verifies that NetEq can handle the situation where the first
635// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000636TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000637 UseNoMocks();
638 CreateInstance();
639
640 const uint8_t kPayloadType = 17; // Just an arbitrary number.
641 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
642 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700643 const size_t kPayloadLengthSamples =
644 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700645 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000646 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700647 RTPHeader rtp_header;
648 rtp_header.payloadType = kPayloadType;
649 rtp_header.sequenceNumber = 0x1234;
650 rtp_header.timestamp = 0x12345678;
651 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000652
653 // Insert one packet. Note that we have not registered any payload type, so
654 // this packet will be rejected.
655 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700656 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000657
658 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700659 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800660 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700661 bool muted;
662 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800663 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
664 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
665 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800666 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000667
668 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700669 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800670 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671
672 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700674 rtp_header.sequenceNumber++;
675 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000676 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700677 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000678 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
679 }
680
681 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700682 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700683 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800684 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
685 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
686 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800687 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000688 << "NetEq did not decode the packets as expected.";
689 }
690}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000691
692// This test verifies that NetEq can handle comfort noise and enters/quits codec
693// internal CNG mode properly.
694TEST_F(NetEqImplTest, CodecInternalCng) {
695 UseNoMocks();
696 CreateInstance();
697
698 const uint8_t kPayloadType = 17; // Just an arbitrary number.
699 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
700 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700701 const size_t kPayloadLengthSamples =
702 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
703 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000704 uint8_t payload[kPayloadLengthBytes] = {0};
705 int16_t dummy_output[kPayloadLengthSamples] = {0};
706
henrik.lundin246ef3e2017-04-24 09:14:32 -0700707 RTPHeader rtp_header;
708 rtp_header.payloadType = kPayloadType;
709 rtp_header.sequenceNumber = 0x1234;
710 rtp_header.timestamp = 0x12345678;
711 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000712
713 // Create a mock decoder object.
714 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200715 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700716 EXPECT_CALL(mock_decoder, SampleRateHz())
717 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000718 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000719 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
720 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700721 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
722 .WillRepeatedly(Return(kPayloadLengthSamples));
723 // Packed duration when asking the decoder for more CNG data (without a new
724 // packet).
725 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
726 .WillRepeatedly(Return(kPayloadLengthSamples));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000727
728 // Pointee(x) verifies that first byte of the payload equals x, this makes it
729 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100730 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
731 kSampleRateKhz * 1000, _, _))
732 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000733 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100734 SetArgPointee<4>(AudioDecoder::kSpeech),
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000735 Return(kPayloadLengthSamples)));
736
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100737 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
738 kSampleRateKhz * 1000, _, _))
739 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000740 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100741 SetArgPointee<4>(AudioDecoder::kComfortNoise),
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000742 Return(kPayloadLengthSamples)));
743
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100744 EXPECT_CALL(mock_decoder,
745 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
746 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000747 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100748 SetArgPointee<4>(AudioDecoder::kComfortNoise),
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000749 Return(kPayloadLengthSamples)));
750
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100751 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
752 kSampleRateKhz * 1000, _, _))
753 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000754 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100755 SetArgPointee<4>(AudioDecoder::kSpeech),
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756 Return(kPayloadLengthSamples)));
757
Karl Wibergd8399e62015-05-25 14:39:56 +0200758 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700759 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700760 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000761
762 // Insert one packet (decoder will return speech).
763 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700764 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765
766 // Insert second packet (decoder will return CNG).
767 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700768 rtp_header.sequenceNumber++;
769 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000770 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700771 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000772
Peter Kastingdce40cf2015-08-24 14:52:23 -0700773 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800774 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800775 AudioFrame::SpeechType expected_type[8] = {
776 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
777 AudioFrame::kCNG, AudioFrame::kCNG,
778 AudioFrame::kCNG, AudioFrame::kCNG,
779 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000780 };
781 int expected_timestamp_increment[8] = {
782 -1, // will not be used.
783 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700784 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
785 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000786 50 * kSampleRateKhz, 10 * kSampleRateKhz
787 };
788
henrik.lundin7a926812016-05-12 13:51:28 -0700789 bool muted;
790 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin9a410dd2016-04-06 01:39:22 -0700791 rtc::Optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
792 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000793
henrik.lundin0d96ab72016-04-06 12:28:26 -0700794 // Lambda for verifying the timestamps.
795 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
796 rtc::Optional<uint32_t> ts, size_t i) {
797 if (expected_timestamp_increment[i] == -1) {
798 // Expect to get an empty timestamp value during CNG and PLC.
799 EXPECT_FALSE(ts) << "i = " << i;
800 } else {
801 ASSERT_TRUE(ts) << "i = " << i;
802 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
803 << "i = " << i;
804 last_timestamp = ts;
805 }
806 };
807
Peter Kastingdce40cf2015-08-24 14:52:23 -0700808 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800809 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
810 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800811 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700812 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700813 SCOPED_TRACE("");
814 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000815 }
816
817 // Insert third packet, which leaves a gap from last packet.
818 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700819 rtp_header.sequenceNumber += 2;
820 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000821 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700822 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823
Peter Kastingdce40cf2015-08-24 14:52:23 -0700824 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800825 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
826 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800827 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700828 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700829 SCOPED_TRACE("");
830 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000831 }
832
833 // Now check the packet buffer, and make sure it is empty.
834 EXPECT_TRUE(packet_buffer_->Empty());
835
836 EXPECT_CALL(mock_decoder, Die());
837}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000838
839TEST_F(NetEqImplTest, UnsupportedDecoder) {
840 UseNoMocks();
841 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700842 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800843 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000844
845 const uint8_t kPayloadType = 17; // Just an arbitrary number.
846 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
847 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000848
Peter Kastingdce40cf2015-08-24 14:52:23 -0700849 const size_t kPayloadLengthSamples =
850 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000851 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700852 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200853 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700854 RTPHeader rtp_header;
855 rtp_header.payloadType = kPayloadType;
856 rtp_header.sequenceNumber = 0x1234;
857 rtp_header.timestamp = 0x12345678;
858 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000859
ossu61a208b2016-09-20 01:38:00 -0700860 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000861
862 const uint8_t kFirstPayloadValue = 1;
863 const uint8_t kSecondPayloadValue = 2;
864
ossu61a208b2016-09-20 01:38:00 -0700865 EXPECT_CALL(decoder,
866 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
867 .Times(AtLeast(1))
868 .WillRepeatedly(Return(kNetEqMaxFrameSize + 1));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000869
ossu61a208b2016-09-20 01:38:00 -0700870 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000871 .Times(0);
872
ossu61a208b2016-09-20 01:38:00 -0700873 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
874 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000875 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700876 .WillOnce(DoAll(
877 SetArrayArgument<3>(dummy_output,
878 dummy_output + kPayloadLengthSamples * kChannels),
879 SetArgPointee<4>(AudioDecoder::kSpeech),
880 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000881
ossu61a208b2016-09-20 01:38:00 -0700882 EXPECT_CALL(decoder,
883 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
884 .Times(AtLeast(1))
885 .WillRepeatedly(Return(kNetEqMaxFrameSize));
886
887 EXPECT_CALL(decoder, SampleRateHz())
888 .WillRepeatedly(Return(kSampleRateHz));
889
890 EXPECT_CALL(decoder, Channels())
891 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000892
kwibergee1879c2015-10-29 06:20:28 -0700893 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700894 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700895 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897 // Insert one packet.
898 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
899 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700900 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000901
902 // Insert another packet.
903 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905 // The second timestamp needs to be at least 30 ms after the first to make
906 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700907 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000908 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700909 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700912 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800913 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200914 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700915 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 // Output size and number of channels should be correct.
917 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
918 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
919 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000920
henrik.lundin6d8e0112016-03-04 10:34:21 -0800921 // Second call to GetAudio will decode the packet that is ok. No errors are
922 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700923 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800924 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
925 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700926
927 // Die isn't called through NiceMock (since it's called by the
928 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
929 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000930}
931
henrik.lundin116c84e2015-08-27 13:14:48 -0700932// This test inserts packets until the buffer is flushed. After that, it asks
933// NetEq for the network statistics. The purpose of the test is to make sure
934// that even though the buffer size increment is negative (which it becomes when
935// the packet causing a flush is inserted), the packet length stored in the
936// decision logic remains valid.
937TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
938 UseNoMocks();
939 CreateInstance();
940
941 const size_t kPayloadLengthSamples = 80;
942 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
943 const uint8_t kPayloadType = 17; // Just an arbitrary number.
944 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
945 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700946 RTPHeader rtp_header;
947 rtp_header.payloadType = kPayloadType;
948 rtp_header.sequenceNumber = 0x1234;
949 rtp_header.timestamp = 0x12345678;
950 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700951
kwibergee1879c2015-10-29 06:20:28 -0700952 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800953 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700954
955 // Insert packets until the buffer flushes.
956 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
957 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
958 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700959 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
960 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
961 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700962 }
963 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
964
965 // Ask for network statistics. This should not crash.
966 NetEqNetworkStatistics stats;
967 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
968}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200969
970TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
971 UseNoMocks();
972 CreateInstance();
973
974 const uint8_t kPayloadType = 17; // Just an arbitrary number.
975 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
976 const int kSampleRateHz = 8000;
977 const size_t kPayloadLengthSamples =
978 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
979 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
980 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700981 RTPHeader rtp_header;
982 rtp_header.payloadType = kPayloadType;
983 rtp_header.sequenceNumber = 0x1234;
984 rtp_header.timestamp = 0x12345678;
985 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200986
987 // Create a mock decoder object.
988 MockAudioDecoder mock_decoder;
989 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700990 EXPECT_CALL(mock_decoder, SampleRateHz())
991 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200992 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
993 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
994 .WillRepeatedly(Return(0));
995 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
996 .WillRepeatedly(Return(kPayloadLengthSamples));
997 int16_t dummy_output[kPayloadLengthSamples] = {0};
998 // The below expectation will make the mock decoder write
999 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1000 // speech. That is, the decoded length is 5 samples shorter than the expected.
1001 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001002 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001003 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001004 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001005 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001006 SetArgPointee<4>(AudioDecoder::kSpeech),
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007 Return(kPayloadLengthSamples - 5)));
kwibergee1879c2015-10-29 06:20:28 -07001008 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1009 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001010 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001011
1012 // Insert one packet.
1013 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001014 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001015
1016 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1017
1018 // Pull audio once.
1019 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001020 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001021 bool muted;
1022 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001023 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1024 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001025 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001026
1027 EXPECT_CALL(mock_decoder, Die());
1028}
minyuel6d92bf52015-09-23 15:20:39 +02001029
1030// This test checks the behavior of NetEq when audio decoder fails.
1031TEST_F(NetEqImplTest, DecodingError) {
1032 UseNoMocks();
1033 CreateInstance();
1034
1035 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1036 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1037 const int kSampleRateHz = 8000;
1038 const int kDecoderErrorCode = -97; // Any negative number.
1039
1040 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1041 const size_t kFrameLengthSamples =
1042 static_cast<size_t>(5 * kSampleRateHz / 1000);
1043
1044 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1045
1046 uint8_t payload[kPayloadLengthBytes] = {0};
1047
henrik.lundin246ef3e2017-04-24 09:14:32 -07001048 RTPHeader rtp_header;
1049 rtp_header.payloadType = kPayloadType;
1050 rtp_header.sequenceNumber = 0x1234;
1051 rtp_header.timestamp = 0x12345678;
1052 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001053
1054 // Create a mock decoder object.
1055 MockAudioDecoder mock_decoder;
1056 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001057 EXPECT_CALL(mock_decoder, SampleRateHz())
1058 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001059 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1060 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1061 .WillRepeatedly(Return(0));
1062 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1063 .WillRepeatedly(Return(kFrameLengthSamples));
1064 EXPECT_CALL(mock_decoder, ErrorCode())
1065 .WillOnce(Return(kDecoderErrorCode));
1066 EXPECT_CALL(mock_decoder, HasDecodePlc())
1067 .WillOnce(Return(false));
1068 int16_t dummy_output[kFrameLengthSamples] = {0};
1069
1070 {
1071 InSequence sequence; // Dummy variable.
1072 // Mock decoder works normally the first time.
1073 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001074 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001075 .Times(3)
1076 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001077 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001078 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001079 SetArgPointee<4>(AudioDecoder::kSpeech),
minyuel6d92bf52015-09-23 15:20:39 +02001080 Return(kFrameLengthSamples)))
1081 .RetiresOnSaturation();
1082
1083 // Then mock decoder fails. A common reason for failure can be buffer being
1084 // too short
1085 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001086 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001087 .WillOnce(Return(-1))
1088 .RetiresOnSaturation();
1089
1090 // Mock decoder finally returns to normal.
1091 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001092 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001093 .Times(2)
1094 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001095 DoAll(SetArrayArgument<3>(dummy_output,
1096 dummy_output + kFrameLengthSamples),
1097 SetArgPointee<4>(AudioDecoder::kSpeech),
minyuel6d92bf52015-09-23 15:20:39 +02001098 Return(kFrameLengthSamples)));
1099 }
1100
kwibergee1879c2015-10-29 06:20:28 -07001101 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1102 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001103 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001104
1105 // Insert packets.
1106 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001107 rtp_header.sequenceNumber += 1;
1108 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001109 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001110 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001111 }
1112
1113 // Pull audio.
1114 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001115 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001116 bool muted;
1117 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001118 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1119 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001120 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001121
1122 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001123 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001124 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1125 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001126 // We are not expecting anything for output.speech_type_, since an error was
1127 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001128
1129 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001130 EXPECT_EQ(NetEq::kOK, 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 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001134
1135 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001136 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001137 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1138 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001139 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001140
1141 EXPECT_CALL(mock_decoder, Die());
1142}
1143
1144// This test checks the behavior of NetEq when audio decoder fails during CNG.
1145TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1146 UseNoMocks();
1147 CreateInstance();
1148
1149 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1150 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1151 const int kSampleRateHz = 8000;
1152 const int kDecoderErrorCode = -97; // Any negative number.
1153
1154 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1155 const size_t kFrameLengthSamples =
1156 static_cast<size_t>(5 * kSampleRateHz / 1000);
1157
1158 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1159
1160 uint8_t payload[kPayloadLengthBytes] = {0};
1161
henrik.lundin246ef3e2017-04-24 09:14:32 -07001162 RTPHeader rtp_header;
1163 rtp_header.payloadType = kPayloadType;
1164 rtp_header.sequenceNumber = 0x1234;
1165 rtp_header.timestamp = 0x12345678;
1166 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001167
1168 // Create a mock decoder object.
1169 MockAudioDecoder mock_decoder;
1170 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001171 EXPECT_CALL(mock_decoder, SampleRateHz())
1172 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001173 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1174 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1175 .WillRepeatedly(Return(0));
1176 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1177 .WillRepeatedly(Return(kFrameLengthSamples));
1178 EXPECT_CALL(mock_decoder, ErrorCode())
1179 .WillOnce(Return(kDecoderErrorCode));
1180 int16_t dummy_output[kFrameLengthSamples] = {0};
1181
1182 {
1183 InSequence sequence; // Dummy variable.
1184 // Mock decoder works normally the first 2 times.
1185 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001186 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001187 .Times(2)
1188 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001189 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001190 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001191 SetArgPointee<4>(AudioDecoder::kComfortNoise),
minyuel6d92bf52015-09-23 15:20:39 +02001192 Return(kFrameLengthSamples)))
1193 .RetiresOnSaturation();
1194
1195 // Then mock decoder fails. A common reason for failure can be buffer being
1196 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001197 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001198 .WillOnce(Return(-1))
1199 .RetiresOnSaturation();
1200
1201 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001202 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001203 .Times(2)
1204 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001205 DoAll(SetArrayArgument<3>(dummy_output,
1206 dummy_output + kFrameLengthSamples),
1207 SetArgPointee<4>(AudioDecoder::kComfortNoise),
minyuel6d92bf52015-09-23 15:20:39 +02001208 Return(kFrameLengthSamples)));
1209 }
1210
kwibergee1879c2015-10-29 06:20:28 -07001211 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1212 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001213 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001214
1215 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1216 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001217 rtp_header.sequenceNumber += 1;
1218 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001219 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001220 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001221 }
1222
1223 // Pull audio.
1224 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001225 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001226 bool muted;
1227 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001228 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1229 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001230 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001231
1232 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001233 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001234 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1235 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001236 // We are not expecting anything for output.speech_type_, since an error was
1237 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001238
1239 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001240 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001241 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1242 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001243 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001244
1245 EXPECT_CALL(mock_decoder, Die());
1246}
1247
henrik.lundind89814b2015-11-23 06:49:25 -08001248// Tests that the return value from last_output_sample_rate_hz() is equal to the
1249// configured inital sample rate.
1250TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1251 UseNoMocks();
1252 config_.sample_rate_hz = 48000;
1253 CreateInstance();
1254 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1255}
1256
henrik.lundined497212016-04-25 10:11:38 -07001257TEST_F(NetEqImplTest, TickTimerIncrement) {
1258 UseNoMocks();
1259 CreateInstance();
1260 ASSERT_TRUE(tick_timer_);
1261 EXPECT_EQ(0u, tick_timer_->ticks());
1262 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001263 bool muted;
1264 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001265 EXPECT_EQ(1u, tick_timer_->ticks());
1266}
1267
henrik.lundin114c1b32017-04-26 07:47:32 -07001268TEST_F(NetEqImplTest, TargetDelayMs) {
1269 UseNoMocks();
1270 use_mock_delay_manager_ = true;
1271 CreateInstance();
1272 // Let the dummy target delay be 17 packets.
1273 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1274 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1275 .WillOnce(Return(kTargetLevelPacketsQ8));
1276 // Default packet size before any packet has been decoded is 30 ms, so we are
1277 // expecting 17 * 30 = 510 ms target delay.
1278 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1279}
1280
henrik.lundinb8c55b12017-05-10 07:38:01 -07001281TEST_F(NetEqImplTest, InsertEmptyPacket) {
1282 UseNoMocks();
1283 use_mock_delay_manager_ = true;
1284 CreateInstance();
1285
1286 RTPHeader rtp_header;
1287 rtp_header.payloadType = 17;
1288 rtp_header.sequenceNumber = 0x1234;
1289 rtp_header.timestamp = 0x12345678;
1290 rtp_header.ssrc = 0x87654321;
1291
1292 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1293 neteq_->InsertEmptyPacket(rtp_header);
1294}
1295
minyue5bd33972016-05-02 04:46:11 -07001296class Decoder120ms : public AudioDecoder {
1297 public:
kwiberg347d3512016-06-16 01:59:09 -07001298 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1299 : sample_rate_hz_(sample_rate_hz),
1300 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001301 speech_type_(speech_type) {}
1302
1303 int DecodeInternal(const uint8_t* encoded,
1304 size_t encoded_len,
1305 int sample_rate_hz,
1306 int16_t* decoded,
1307 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001308 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001309 size_t decoded_len =
1310 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1311 for (size_t i = 0; i < decoded_len; ++i) {
1312 decoded[i] = next_value_++;
1313 }
1314 *speech_type = speech_type_;
1315 return decoded_len;
1316 }
1317
1318 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001319 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001320 size_t Channels() const override { return 2; }
1321
1322 private:
kwiberg347d3512016-06-16 01:59:09 -07001323 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001324 int16_t next_value_;
1325 SpeechType speech_type_;
1326};
1327
1328class NetEqImplTest120ms : public NetEqImplTest {
1329 protected:
1330 NetEqImplTest120ms() : NetEqImplTest() {}
1331 virtual ~NetEqImplTest120ms() {}
1332
1333 void CreateInstanceNoMocks() {
1334 UseNoMocks();
1335 CreateInstance();
1336 }
1337
1338 void CreateInstanceWithDelayManagerMock() {
1339 UseNoMocks();
1340 use_mock_delay_manager_ = true;
1341 CreateInstance();
1342 }
1343
1344 uint32_t timestamp_diff_between_packets() const {
1345 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1346 }
1347
1348 uint32_t first_timestamp() const { return 10u; }
1349
1350 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001351 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001352 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001353 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1354 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001355 }
1356 }
1357
1358 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001359 RTPHeader rtp_header;
1360 rtp_header.payloadType = kPayloadType;
1361 rtp_header.sequenceNumber = sequence_number_;
1362 rtp_header.timestamp = timestamp;
1363 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001364 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1365 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001366 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001367 sequence_number_++;
1368 }
1369
1370 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
kwiberg347d3512016-06-16 01:59:09 -07001371 decoder_.reset(new Decoder120ms(kSamplingFreq_, speech_type));
minyue5bd33972016-05-02 04:46:11 -07001372 ASSERT_EQ(2u, decoder_->Channels());
1373 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1374 decoder_.get(), NetEqDecoder::kDecoderOpus_2ch,
kwiberg342f7402016-06-16 03:18:00 -07001375 "120ms codec", kPayloadType));
minyue5bd33972016-05-02 04:46:11 -07001376 }
1377
1378 std::unique_ptr<Decoder120ms> decoder_;
1379 AudioFrame output_;
1380 const uint32_t kPayloadType = 17;
1381 const uint32_t kSamplingFreq_ = 48000;
1382 uint16_t sequence_number_ = 1;
1383};
1384
1385TEST_F(NetEqImplTest120ms, AudioRepetition) {
1386 config_.playout_mode = kPlayoutFax;
1387 CreateInstanceNoMocks();
1388 Register120msCodec(AudioDecoder::kSpeech);
1389
1390 InsertPacket(first_timestamp());
1391 GetFirstPacket();
1392
henrik.lundin7a926812016-05-12 13:51:28 -07001393 bool muted;
1394 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001395 EXPECT_EQ(kAudioRepetition, neteq_->last_operation_for_test());
1396}
1397
1398TEST_F(NetEqImplTest120ms, AlternativePlc) {
1399 config_.playout_mode = kPlayoutOff;
1400 CreateInstanceNoMocks();
1401 Register120msCodec(AudioDecoder::kSpeech);
1402
1403 InsertPacket(first_timestamp());
1404 GetFirstPacket();
1405
henrik.lundin7a926812016-05-12 13:51:28 -07001406 bool muted;
1407 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001408 EXPECT_EQ(kAlternativePlc, neteq_->last_operation_for_test());
1409}
1410
1411TEST_F(NetEqImplTest120ms, CodecInternalCng) {
1412 CreateInstanceNoMocks();
1413 Register120msCodec(AudioDecoder::kComfortNoise);
1414
1415 InsertPacket(first_timestamp());
1416 GetFirstPacket();
1417
henrik.lundin7a926812016-05-12 13:51:28 -07001418 bool muted;
1419 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001420 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1421}
1422
1423TEST_F(NetEqImplTest120ms, Normal) {
1424 CreateInstanceNoMocks();
1425 Register120msCodec(AudioDecoder::kSpeech);
1426
1427 InsertPacket(first_timestamp());
1428 GetFirstPacket();
1429
1430 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1431}
1432
1433TEST_F(NetEqImplTest120ms, Merge) {
1434 CreateInstanceWithDelayManagerMock();
1435
1436 Register120msCodec(AudioDecoder::kSpeech);
1437 InsertPacket(first_timestamp());
1438
1439 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001440 bool muted;
1441 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001442
1443 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1444
1445 // Delay manager reports a target level which should cause a Merge.
1446 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1447
henrik.lundin7a926812016-05-12 13:51:28 -07001448 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001449 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1450}
1451
1452TEST_F(NetEqImplTest120ms, Expand) {
1453 CreateInstanceNoMocks();
1454 Register120msCodec(AudioDecoder::kSpeech);
1455
1456 InsertPacket(first_timestamp());
1457 GetFirstPacket();
1458
henrik.lundin7a926812016-05-12 13:51:28 -07001459 bool muted;
1460 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001461 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1462}
1463
1464TEST_F(NetEqImplTest120ms, FastAccelerate) {
1465 CreateInstanceWithDelayManagerMock();
1466 Register120msCodec(AudioDecoder::kSpeech);
1467
1468 InsertPacket(first_timestamp());
1469 GetFirstPacket();
1470 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1471
1472 // Delay manager report buffer limit which should cause a FastAccelerate.
1473 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1474 .Times(1)
1475 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1476
henrik.lundin7a926812016-05-12 13:51:28 -07001477 bool muted;
1478 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001479 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1480}
1481
1482TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
1483 CreateInstanceWithDelayManagerMock();
1484 Register120msCodec(AudioDecoder::kSpeech);
1485
1486 InsertPacket(first_timestamp());
1487 GetFirstPacket();
1488
1489 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1490
1491 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1492 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1493 .Times(1)
1494 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1495
henrik.lundin7a926812016-05-12 13:51:28 -07001496 bool muted;
1497 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001498 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1499}
1500
1501TEST_F(NetEqImplTest120ms, Accelerate) {
1502 CreateInstanceWithDelayManagerMock();
1503 Register120msCodec(AudioDecoder::kSpeech);
1504
1505 InsertPacket(first_timestamp());
1506 GetFirstPacket();
1507
1508 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1509
1510 // Delay manager report buffer limit which should cause a Accelerate.
1511 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1512 .Times(1)
1513 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1514
henrik.lundin7a926812016-05-12 13:51:28 -07001515 bool muted;
1516 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001517 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1518}
1519
minyuel6d92bf52015-09-23 15:20:39 +02001520}// namespace webrtc