blob: 44660fc483053b7e11ba98cd1bd3135e561acb8e [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
Alessio Bazzica8f319a32019-07-24 16:47:02 +000014#include <utility>
15#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010018#include "api/neteq/default_neteq_controller_factory.h"
19#include "api/neteq/neteq.h"
20#include "api/neteq/neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/neteq/accelerate.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020022#include "modules/audio_coding/neteq/decision_logic.h"
Ivo Creusen39cf3c72019-11-28 14:07:14 +010023#include "modules/audio_coding/neteq/default_neteq_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010025#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020029#include "modules/audio_coding/neteq/mock/mock_neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
31#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010033#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/audio_coding/neteq/sync_buffer.h"
35#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000037#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010038#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010039#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "test/gmock.h"
41#include "test/gtest.h"
42#include "test/mock_audio_decoder.h"
43#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010046using ::testing::AtLeast;
47using ::testing::DoAll;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000048using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049using ::testing::InSequence;
50using ::testing::Invoke;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000051using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000052using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010053using ::testing::Pointee;
54using ::testing::Return;
55using ::testing::ReturnNull;
56using ::testing::SetArgPointee;
57using ::testing::SetArrayArgument;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000058using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010059using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
61namespace webrtc {
62
63// This function is called when inserting a packet list into the mock packet
64// buffer. The purpose is to delete all inserted packets properly, to avoid
65// memory leaks in the test.
66int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070067 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 return PacketBuffer::kOK;
69}
70
71class NetEqImplTest : public ::testing::Test {
72 protected:
Alessio Bazzica8f319a32019-07-24 16:47:02 +000073 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000074
Niels Möllera0f44302018-11-30 10:45:12 +010075 void CreateInstance(
76 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
77 ASSERT_TRUE(decoder_factory);
Ivo Creusen3ce44a32019-10-31 14:38:11 +010078 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory,
79 DefaultNetEqControllerFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070080
81 // Get a local pointer to NetEq's TickTimer object.
82 tick_timer_ = deps.tick_timer.get();
83
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000084 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070085 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
86 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000087 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
88 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000090 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000092
henrik.lundin1d9061e2016-04-26 12:19:34 -070093 if (use_mock_dtmf_buffer_) {
94 std::unique_ptr<MockDtmfBuffer> mock(
95 new MockDtmfBuffer(config_.sample_rate_hz));
96 mock_dtmf_buffer_ = mock.get();
97 deps.dtmf_buffer = std::move(mock);
98 }
99 dtmf_buffer_ = deps.dtmf_buffer.get();
100
101 if (use_mock_dtmf_tone_generator_) {
102 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
103 mock_dtmf_tone_generator_ = mock.get();
104 deps.dtmf_tone_generator = std::move(mock);
105 }
106 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
107
108 if (use_mock_packet_buffer_) {
109 std::unique_ptr<MockPacketBuffer> mock(
110 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
111 mock_packet_buffer_ = mock.get();
112 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700113 }
114 packet_buffer_ = deps.packet_buffer.get();
115
Ivo Creusen53a31f72019-10-24 15:20:39 +0200116 if (use_mock_neteq_controller_) {
117 std::unique_ptr<MockNetEqController> mock(new MockNetEqController());
118 mock_neteq_controller_ = mock.get();
119 deps.neteq_controller = std::move(mock);
120 } else {
121 deps.stats = std::make_unique<StatisticsCalculator>();
122 NetEqController::Config controller_config;
123 controller_config.tick_timer = tick_timer_;
124 controller_config.base_min_delay_ms = config_.min_delay_ms;
125 controller_config.enable_rtx_handling = config_.enable_rtx_handling;
126 controller_config.allow_time_stretching = true;
127 controller_config.max_packets_in_buffer = config_.max_packets_in_buffer;
Ivo Creusen88636c62020-01-24 11:04:56 +0100128 controller_config.clock = &clock_;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200129 deps.neteq_controller =
130 std::make_unique<DecisionLogic>(std::move(controller_config));
131 }
132 neteq_controller_ = deps.neteq_controller.get();
133
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700135 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700137 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700138 }
ossua70695a2016-09-22 02:06:28 -0700139 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700140
141 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
142 new TimestampScaler(*deps.decoder_database.get()));
143
144 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000145 ASSERT_TRUE(neteq_ != NULL);
146 }
147
Niels Möllera0f44302018-11-30 10:45:12 +0100148 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
149
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000150 void UseNoMocks() {
151 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000152 use_mock_decoder_database_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200153 use_mock_neteq_controller_ = false;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000154 use_mock_dtmf_buffer_ = false;
155 use_mock_dtmf_tone_generator_ = false;
156 use_mock_packet_buffer_ = false;
157 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158 }
159
160 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000161 if (use_mock_decoder_database_) {
162 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
163 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200164 if (use_mock_neteq_controller_) {
165 EXPECT_CALL(*mock_neteq_controller_, Die()).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000166 }
167 if (use_mock_dtmf_buffer_) {
168 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
169 }
170 if (use_mock_dtmf_tone_generator_) {
171 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
172 }
173 if (use_mock_packet_buffer_) {
174 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
175 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176 }
177
Niels Möller05543682019-01-10 16:55:06 +0100178 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800179 const size_t kPayloadLength = 4;
180 const uint8_t kPayloadType = 110;
solenberg2779bab2016-11-17 04:45:19 -0800181 const int kSampleRateHz = 16000;
182 config_.sample_rate_hz = kSampleRateHz;
183 UseNoMocks();
184 CreateInstance();
185 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200186 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700187 RTPHeader rtp_header;
188 rtp_header.payloadType = kPayloadType;
189 rtp_header.sequenceNumber = 0x1234;
190 rtp_header.timestamp = 0x12345678;
191 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800192
Niels Möller05543682019-01-10 16:55:06 +0100193 EXPECT_TRUE(neteq_->RegisterPayloadType(
194 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800195
196 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200197 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
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
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000210 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
211 // returned by |GetAudio()|.
212 EXPECT_THAT(output.packet_infos_, IsEmpty());
213
solenberg2779bab2016-11-17 04:45:19 -0800214 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200215 const std::vector<int16_t> kOutput(
216 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
218 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
219 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
220 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
221 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
222 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800223 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700224 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800225 }
226
henrik.lundin1d9061e2016-04-26 12:19:34 -0700227 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000228 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000229 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 TickTimer* tick_timer_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700231 MockDecoderDatabase* mock_decoder_database_ = nullptr;
232 DecoderDatabase* decoder_database_ = nullptr;
233 bool use_mock_decoder_database_ = true;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200234 MockNetEqController* mock_neteq_controller_ = nullptr;
235 NetEqController* neteq_controller_ = nullptr;
236 bool use_mock_neteq_controller_ = true;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700237 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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251// 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;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000255 SimulatedClock clock(0);
Ivo Creusen39cf3c72019-11-28 14:07:14 +0100256 auto decoder_factory = CreateBuiltinAudioDecoderFactory();
257 std::unique_ptr<NetEq> neteq =
258 DefaultNetEqFactory().CreateNetEq(config, decoder_factory, &clock);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259}
260
kwiberg5adaf732016-10-04 09:33:27 -0700261TEST_F(NetEqImplTest, RegisterPayloadType) {
262 CreateInstance();
263 constexpr int rtp_payload_type = 0;
264 const SdpAudioFormat format("pcmu", 8000, 1);
265 EXPECT_CALL(*mock_decoder_database_,
266 RegisterPayload(rtp_payload_type, format));
267 neteq_->RegisterPayloadType(rtp_payload_type, format);
268}
269
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000271 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000273 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200275 // Check that kOK is returned when database returns kDecoderNotFound, because
276 // removing a payload type that was never registered is not an error.
277 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278}
279
kwiberg6b19b562016-09-20 04:02:25 -0700280TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
281 CreateInstance();
282 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
283 neteq_->RemoveAllPayloadTypes();
284}
285
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000287 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000288 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 const uint8_t kPayloadType = 0;
290 const uint16_t kFirstSequenceNumber = 0x1234;
291 const uint32_t kFirstTimestamp = 0x12345678;
292 const uint32_t kSsrc = 0x87654321;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700294 RTPHeader rtp_header;
295 rtp_header.payloadType = kPayloadType;
296 rtp_header.sequenceNumber = kFirstSequenceNumber;
297 rtp_header.timestamp = kFirstTimestamp;
298 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700299 Packet fake_packet;
300 fake_packet.payload_type = kPayloadType;
301 fake_packet.sequence_number = kFirstSequenceNumber;
302 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303
kwibergc0f2dcf2016-05-31 06:28:03 -0700304 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
305 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100306 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800307 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200308 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800309 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700310 EXPECT_EQ("pcmu", format.name);
311
312 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
313 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
314 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
kwibergc0f2dcf2016-05-31 06:28:03 -0700315 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
316
317 *dec = std::move(mock_decoder);
318 }));
Niels Möller72899062019-01-11 09:36:13 +0100319 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
320 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321
322 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000323 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324 .WillRepeatedly(Return(&info));
325
326 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000327 EXPECT_CALL(*mock_packet_buffer_, Empty())
328 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200329 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200330 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100332 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
333 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
335 // index) is a pointer, and the variable pointed to is set to kPayloadType.
336 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
337 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700338 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000339 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700340 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341
342 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200343 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344
345 // Expectations for delay manager.
346 {
347 // All expectations within this block must be called in this specific order.
348 InSequence sequence; // Dummy variable.
349 // Expectations when the first packet is inserted.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200350 EXPECT_CALL(*mock_neteq_controller_,
351 PacketArrived(/*last_cng_or_dtmf*/ false,
352 /*packet_length_samples*/ _,
353 /*should_update_stats*/ _,
354 /*main_sequence_number*/ kFirstSequenceNumber,
355 /*main_timestamp*/ kFirstTimestamp,
356 /*fs_hz*/ 8000));
357 EXPECT_CALL(*mock_neteq_controller_,
358 PacketArrived(/*last_cng_or_dtmf*/ false,
359 /*packet_length_samples*/ _,
360 /*should_update_stats*/ _,
361 /*main_sequence_number*/ kFirstSequenceNumber + 1,
362 /*main_timestamp*/ kFirstTimestamp + 160,
363 /*fs_hz*/ 8000));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 }
365
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200367 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000368
369 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700370 rtp_header.timestamp += 160;
371 rtp_header.sequenceNumber += 1;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200372 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373}
374
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000375TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
376 UseNoMocks();
377 CreateInstance();
378
379 const int kPayloadLengthSamples = 80;
380 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200381 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000382 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700383 RTPHeader rtp_header;
384 rtp_header.payloadType = kPayloadType;
385 rtp_header.sequenceNumber = 0x1234;
386 rtp_header.timestamp = 0x12345678;
387 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000388
Niels Möller05543682019-01-10 16:55:06 +0100389 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
390 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000391
392 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700393 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
Karl Wiberg45eb1352019-10-10 14:23:00 +0200394 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700395 rtp_header.timestamp += kPayloadLengthSamples;
396 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000397 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
398 }
399
400 // Insert one more packet and make sure the buffer got flushed. That is, it
401 // should only hold one single packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200402 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700403 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700404 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700405 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
406 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000407}
408
solenberg2779bab2016-11-17 04:45:19 -0800409TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100410 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800411}
solenberg99df6c02016-10-11 04:35:34 -0700412
solenberg2779bab2016-11-17 04:45:19 -0800413TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100414 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800415}
solenberg99df6c02016-10-11 04:35:34 -0700416
solenberg2779bab2016-11-17 04:45:19 -0800417TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100418 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800419}
solenberg99df6c02016-10-11 04:35:34 -0700420
solenberg2779bab2016-11-17 04:45:19 -0800421TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100422 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700423}
424
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000425// This test verifies that timestamps propagate from the incoming packets
426// through to the sync buffer and to the playout timestamp.
427TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000428 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000429 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700430 const size_t kPayloadLengthSamples =
431 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000432 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
433 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700434 RTPHeader rtp_header;
435 rtp_header.payloadType = kPayloadType;
436 rtp_header.sequenceNumber = 0x1234;
437 rtp_header.timestamp = 0x12345678;
438 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000439 rtp_header.numCSRCs = 3;
440 rtp_header.arrOfCSRCs[0] = 43;
441 rtp_header.arrOfCSRCs[1] = 65;
442 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000443
444 // This is a dummy decoder that produces as many output samples as the input
445 // has bytes. The output is an increasing series, starting at 1 for the first
446 // sample, and then increasing by 1 for each sample.
447 class CountingSamplesDecoder : public AudioDecoder {
448 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000449 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000450
451 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100452 int DecodeInternal(const uint8_t* encoded,
453 size_t encoded_len,
454 int /* sample_rate_hz */,
455 int16_t* decoded,
456 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000457 for (size_t i = 0; i < encoded_len; ++i) {
458 decoded[i] = next_value_++;
459 }
460 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200461 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000462 }
463
Karl Wiberg43766482015-08-27 15:22:11 +0200464 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000465
kwiberg347d3512016-06-16 01:59:09 -0700466 int SampleRateHz() const override { return kSampleRateHz; }
467
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000468 size_t Channels() const override { return 1; }
469
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000470 uint16_t next_value() const { return next_value_; }
471
472 private:
473 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000474 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475
Niels Möllerb7180c02018-12-06 13:07:11 +0100476 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
477 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
478
479 UseNoMocks();
480 CreateInstance(decoder_factory);
481
482 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100483 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000484
485 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000486 clock_.AdvanceTimeMilliseconds(123456);
487 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200488 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489
490 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700491 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800492 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700493 bool muted;
494 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
495 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800496 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
497 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800498 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000499
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000500 // Verify |output.packet_infos_|.
501 ASSERT_THAT(output.packet_infos_, SizeIs(1));
502 {
503 const auto& packet_info = output.packet_infos_[0];
504 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
505 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
506 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
507 EXPECT_FALSE(packet_info.audio_level().has_value());
508 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
509 }
510
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000511 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700512 EXPECT_EQ(kPayloadLengthSamples,
513 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000514
515 // The value of the last of the output samples is the same as the number of
516 // samples played from the decoded packet. Thus, this number + the RTP
517 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200518 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700519 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200520 absl::optional<uint32_t>(rtp_header.timestamp +
521 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700522 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000523
524 // Check the timestamp for the last value in the sync buffer. This should
525 // be one full frame length ahead of the RTP timestamp.
526 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
527 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700528 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000529 sync_buffer->end_timestamp());
530
531 // Check that the number of samples still to play from the sync buffer add
532 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800533 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700534 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800535 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000536}
537
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000538TEST_F(NetEqImplTest, ReorderedPacket) {
539 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100540 // Create a mock decoder object.
541 MockAudioDecoder mock_decoder;
542
543 CreateInstance(
544 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000545
546 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000547 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;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000557 rtp_header.extension.hasAudioLevel = true;
558 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000559
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.lundin034154b2016-04-27 06:11:50 -0700564 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200565 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000566 int16_t dummy_output[kPayloadLengthSamples] = {0};
567 // The below expectation will make the mock decoder write
568 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100569 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
570 kSampleRateHz, _, _))
571 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000572 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100573 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200574 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100575 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
576 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000577
578 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000579 clock_.AdvanceTimeMilliseconds(123456);
580 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200581 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000582
583 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700584 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800585 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700586 bool muted;
587 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800588 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
589 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800590 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000591
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000592 // Verify |output.packet_infos_|.
593 ASSERT_THAT(output.packet_infos_, SizeIs(1));
594 {
595 const auto& packet_info = output.packet_infos_[0];
596 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
597 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
598 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
599 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
600 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
601 }
602
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 // Insert two more packets. The first one is out of order, and is already too
604 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 rtp_header.sequenceNumber -= 1;
606 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000607 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000609 clock_.AdvanceTimeMilliseconds(1000);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200610 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700611 rtp_header.sequenceNumber += 2;
612 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000613 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000614 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000615 clock_.AdvanceTimeMilliseconds(2000);
616 expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200617 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000618
619 // Expect only the second packet to be decoded (the one with "2" as the first
620 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100621 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
622 kSampleRateHz, _, _))
623 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000624 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100625 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200626 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000627
628 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700629 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800630 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
631 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800632 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000633
634 // Now check the packet buffer, and make sure it is empty, since the
635 // out-of-order packet should have been discarded.
636 EXPECT_TRUE(packet_buffer_->Empty());
637
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000638 // Verify |output.packet_infos_|. Expect to only see the second packet.
639 ASSERT_THAT(output.packet_infos_, SizeIs(1));
640 {
641 const auto& packet_info = output.packet_infos_[0];
642 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
643 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
644 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
645 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
646 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
647 }
648
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000649 EXPECT_CALL(mock_decoder, Die());
650}
651
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000652// This test verifies that NetEq can handle the situation where the first
653// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000654TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000655 UseNoMocks();
656 CreateInstance();
657
658 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000659 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700660 const size_t kPayloadLengthSamples =
661 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700662 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000663 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700664 RTPHeader rtp_header;
665 rtp_header.payloadType = kPayloadType;
666 rtp_header.sequenceNumber = 0x1234;
667 rtp_header.timestamp = 0x12345678;
668 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000669
670 // Insert one packet. Note that we have not registered any payload type, so
671 // this packet will be rejected.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200672 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000673
674 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800676 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700677 bool muted;
678 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::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000683 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000684
685 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100686 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
687 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000688
689 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700690 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700691 rtp_header.sequenceNumber++;
692 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200693 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000694 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
695 }
696
697 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700698 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700699 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800700 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
701 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
702 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800703 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000704 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000705 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000706 }
707}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000708
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200709// This test verifies that audio interruption is not logged for the initial
710// PLC period before the first packet is deocoded.
711// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
Henrik Lundinfe047752019-11-19 12:58:11 +0100712// Make the test parametrized, so that we can test with different initial
713// sample rates in NetEq.
714class NetEqImplTestSampleRateParameter
715 : public NetEqImplTest,
716 public testing::WithParamInterface<int> {
717 protected:
718 NetEqImplTestSampleRateParameter()
719 : NetEqImplTest(), initial_sample_rate_hz_(GetParam()) {
720 config_.sample_rate_hz = initial_sample_rate_hz_;
721 }
722
723 const int initial_sample_rate_hz_;
724};
725
726// This test does the following:
727// 0. Set up NetEq with initial sample rate given by test parameter, and a codec
728// sample rate of 16000.
729// 1. Start calling GetAudio before inserting any encoded audio. The audio
730// produced will be PLC.
731// 2. Insert a number of encoded audio packets.
732// 3. Keep calling GetAudio and verify that no audio interruption was logged.
733// Call GetAudio until NetEq runs out of data again; PLC starts.
734// 4. Insert one more packet.
735// 5. Call GetAudio until that packet is decoded and the PLC ends.
736
737TEST_P(NetEqImplTestSampleRateParameter,
738 NoAudioInterruptionLoggedBeforeFirstDecode) {
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200739 UseNoMocks();
740 CreateInstance();
741
742 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundinfe047752019-11-19 12:58:11 +0100743 const int kPayloadSampleRateHz = 16000;
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200744 const size_t kPayloadLengthSamples =
Henrik Lundinfe047752019-11-19 12:58:11 +0100745 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200746 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
747 uint8_t payload[kPayloadLengthBytes] = {0};
748 RTPHeader rtp_header;
749 rtp_header.payloadType = kPayloadType;
750 rtp_header.sequenceNumber = 0x1234;
751 rtp_header.timestamp = 0x12345678;
752 rtp_header.ssrc = 0x87654321;
753
754 // Register the payload type.
Henrik Lundinfe047752019-11-19 12:58:11 +0100755 EXPECT_TRUE(neteq_->RegisterPayloadType(
756 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200757
758 // Pull audio several times. No packets have been inserted yet.
Henrik Lundinfe047752019-11-19 12:58:11 +0100759 const size_t initial_output_size =
760 static_cast<size_t>(10 * initial_sample_rate_hz_ / 1000); // 10 ms
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200761 AudioFrame output;
762 bool muted;
763 for (int i = 0; i < 100; ++i) {
764 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Henrik Lundinfe047752019-11-19 12:58:11 +0100765 EXPECT_EQ(initial_output_size, output.samples_per_channel_);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200766 EXPECT_EQ(1u, output.num_channels_);
767 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000768 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200769 }
770
Henrik Lundinfe047752019-11-19 12:58:11 +0100771 // Lambda for inserting packets.
772 auto insert_packet = [&]() {
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200773 rtp_header.sequenceNumber++;
774 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200775 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundinfe047752019-11-19 12:58:11 +0100776 };
777 // Insert 10 packets.
778 for (size_t i = 0; i < 10; ++i) {
779 insert_packet();
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200780 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
781 }
782
783 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Henrik Lundinfe047752019-11-19 12:58:11 +0100784 constexpr size_t kOutputSize =
785 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200786 for (size_t i = 0; i < 3; ++i) {
787 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Henrik Lundinfe047752019-11-19 12:58:11 +0100788 EXPECT_EQ(kOutputSize, output.samples_per_channel_);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200789 EXPECT_EQ(1u, output.num_channels_);
790 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
791 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000792 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200793 }
794
Henrik Lundinfe047752019-11-19 12:58:11 +0100795 // Verify that no interruption was logged.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200796 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200797 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundinfe047752019-11-19 12:58:11 +0100798
799 // Keep pulling audio data until a new PLC period is started.
800 size_t count_loops = 0;
801 while (output.speech_type_ == AudioFrame::kNormalSpeech) {
802 // Make sure we don't hang the test if we never go to PLC.
803 ASSERT_LT(++count_loops, 100u);
804 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
805 }
806
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200807 // Insert a few packets to avoid postpone decoding after expand.
808 for (size_t i = 0; i < 5; ++i) {
809 insert_packet();
810 }
Henrik Lundinfe047752019-11-19 12:58:11 +0100811
812 // Pull audio until the newly inserted packet is decoded and the PLC ends.
813 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
814 // Make sure we don't hang the test if we never go to PLC.
815 ASSERT_LT(++count_loops, 100u);
816 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
817 }
818
819 // Verify that no interruption was logged.
820 lifetime_stats = neteq_->GetLifetimeStatistics();
821 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200822}
823
Henrik Lundinfe047752019-11-19 12:58:11 +0100824// This test does the following:
825// 0. Set up NetEq with initial sample rate given by test parameter, and a codec
826// sample rate of 16000.
827// 1. Insert a number of encoded audio packets.
828// 2. Call GetAudio and verify that decoded audio is produced.
829// 3. Keep calling GetAudio until NetEq runs out of data; PLC starts.
830// 4. Keep calling GetAudio until PLC has been produced for at least 150 ms.
831// 5. Insert one more packet.
832// 6. Call GetAudio until that packet is decoded and the PLC ends.
833// 7. Verify that an interruption was logged.
834
835TEST_P(NetEqImplTestSampleRateParameter, AudioInterruptionLogged) {
836 UseNoMocks();
837 CreateInstance();
838
839 const uint8_t kPayloadType = 17; // Just an arbitrary number.
840 const int kPayloadSampleRateHz = 16000;
841 const size_t kPayloadLengthSamples =
842 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
843 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
844 uint8_t payload[kPayloadLengthBytes] = {0};
845 RTPHeader rtp_header;
846 rtp_header.payloadType = kPayloadType;
847 rtp_header.sequenceNumber = 0x1234;
848 rtp_header.timestamp = 0x12345678;
849 rtp_header.ssrc = 0x87654321;
850
851 // Register the payload type.
852 EXPECT_TRUE(neteq_->RegisterPayloadType(
853 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
854
855 // Lambda for inserting packets.
856 auto insert_packet = [&]() {
857 rtp_header.sequenceNumber++;
858 rtp_header.timestamp += kPayloadLengthSamples;
859 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
860 };
861 // Insert 10 packets.
862 for (size_t i = 0; i < 10; ++i) {
863 insert_packet();
864 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
865 }
866
867 AudioFrame output;
868 bool muted;
869 // Keep pulling audio data until a new PLC period is started.
870 size_t count_loops = 0;
871 do {
872 // Make sure we don't hang the test if we never go to PLC.
873 ASSERT_LT(++count_loops, 100u);
874 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
875 } while (output.speech_type_ == AudioFrame::kNormalSpeech);
876
877 // Pull audio 15 times, which produces 150 ms of output audio. This should
878 // all be produced as PLC. The total length of the gap will then be 150 ms
879 // plus an initial fraction of 10 ms at the start and the end of the PLC
880 // period. In total, less than 170 ms.
881 for (size_t i = 0; i < 15; ++i) {
882 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
883 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
884 }
885
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200886 // Insert a few packets to avoid postpone decoding after expand.
887 for (size_t i = 0; i < 5; ++i) {
888 insert_packet();
889 }
Henrik Lundinfe047752019-11-19 12:58:11 +0100890
891 // Pull audio until the newly inserted packet is decoded and the PLC ends.
892 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
893 // Make sure we don't hang the test if we never go to PLC.
894 ASSERT_LT(++count_loops, 100u);
895 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
896 }
897
898 // Verify that the interruption was logged.
899 auto lifetime_stats = neteq_->GetLifetimeStatistics();
900 EXPECT_EQ(1, lifetime_stats.interruption_count);
901 EXPECT_GT(lifetime_stats.total_interruption_duration_ms, 150);
902 EXPECT_LT(lifetime_stats.total_interruption_duration_ms, 170);
903}
904
905INSTANTIATE_TEST_SUITE_P(SampleRates,
906 NetEqImplTestSampleRateParameter,
907 testing::Values(8000, 16000, 32000, 48000));
908
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000909// This test verifies that NetEq can handle comfort noise and enters/quits codec
910// internal CNG mode properly.
911TEST_F(NetEqImplTest, CodecInternalCng) {
912 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100913 // Create a mock decoder object.
914 MockAudioDecoder mock_decoder;
915 CreateInstance(
916 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000917
918 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000919 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700920 const size_t kPayloadLengthSamples =
921 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
922 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000923 uint8_t payload[kPayloadLengthBytes] = {0};
924 int16_t dummy_output[kPayloadLengthSamples] = {0};
925
henrik.lundin246ef3e2017-04-24 09:14:32 -0700926 RTPHeader rtp_header;
927 rtp_header.payloadType = kPayloadType;
928 rtp_header.sequenceNumber = 0x1234;
929 rtp_header.timestamp = 0x12345678;
930 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000931
Karl Wiberg43766482015-08-27 15:22:11 +0200932 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700933 EXPECT_CALL(mock_decoder, SampleRateHz())
934 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000935 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700936 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200937 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700938 // Packed duration when asking the decoder for more CNG data (without a new
939 // packet).
940 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200941 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000942
943 // Pointee(x) verifies that first byte of the payload equals x, this makes it
944 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100945 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
946 kSampleRateKhz * 1000, _, _))
947 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000948 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100949 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200950 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000951
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100952 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
953 kSampleRateKhz * 1000, _, _))
954 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000955 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100956 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200957 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000958
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100959 EXPECT_CALL(mock_decoder,
960 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
961 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000962 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100963 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200964 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000965
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100966 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
967 kSampleRateKhz * 1000, _, _))
968 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000969 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100970 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200971 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000972
Niels Möller50b66d52018-12-11 14:43:21 +0100973 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
974 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000975
976 // Insert one packet (decoder will return speech).
Karl Wiberg45eb1352019-10-10 14:23:00 +0200977 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000978
979 // Insert second packet (decoder will return CNG).
980 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700981 rtp_header.sequenceNumber++;
982 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200983 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000984
Peter Kastingdce40cf2015-08-24 14:52:23 -0700985 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800986 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800987 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200988 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
989 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
990 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000991 int expected_timestamp_increment[8] = {
992 -1, // will not be used.
993 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200994 -1,
995 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
996 -1,
997 -1,
998 50 * kSampleRateKhz,
999 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001000
henrik.lundin7a926812016-05-12 13:51:28 -07001001 bool muted;
1002 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +02001003 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -07001004 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001005
henrik.lundin0d96ab72016-04-06 12:28:26 -07001006 // Lambda for verifying the timestamps.
1007 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +02001008 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -07001009 if (expected_timestamp_increment[i] == -1) {
1010 // Expect to get an empty timestamp value during CNG and PLC.
1011 EXPECT_FALSE(ts) << "i = " << i;
1012 } else {
1013 ASSERT_TRUE(ts) << "i = " << i;
1014 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
1015 << "i = " << i;
1016 last_timestamp = ts;
1017 }
1018 };
1019
Peter Kastingdce40cf2015-08-24 14:52:23 -07001020 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001021 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1022 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001023 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -07001024 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -07001025 SCOPED_TRACE("");
1026 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001027 }
1028
1029 // Insert third packet, which leaves a gap from last packet.
1030 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -07001031 rtp_header.sequenceNumber += 2;
1032 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001033 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001034
Peter Kastingdce40cf2015-08-24 14:52:23 -07001035 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001036 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1037 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001038 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -07001039 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -07001040 SCOPED_TRACE("");
1041 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +00001042 }
1043
1044 // Now check the packet buffer, and make sure it is empty.
1045 EXPECT_TRUE(packet_buffer_->Empty());
1046
1047 EXPECT_CALL(mock_decoder, Die());
1048}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001049
1050TEST_F(NetEqImplTest, UnsupportedDecoder) {
1051 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001052 ::testing::NiceMock<MockAudioDecoder> decoder;
1053
1054 CreateInstance(
1055 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -07001056 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -08001057 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001058
1059 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001060 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001061
Peter Kastingdce40cf2015-08-24 14:52:23 -07001062 const size_t kPayloadLengthSamples =
1063 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001064 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -07001065 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +02001066 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001067 RTPHeader rtp_header;
1068 rtp_header.payloadType = kPayloadType;
1069 rtp_header.sequenceNumber = 0x1234;
1070 rtp_header.timestamp = 0x12345678;
1071 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001072
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001073 const uint8_t kFirstPayloadValue = 1;
1074 const uint8_t kSecondPayloadValue = 2;
1075
ossu61a208b2016-09-20 01:38:00 -07001076 EXPECT_CALL(decoder,
1077 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
1078 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001079 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001080
ossu61a208b2016-09-20 01:38:00 -07001081 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001082 .Times(0);
1083
ossu61a208b2016-09-20 01:38:00 -07001084 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
1085 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001086 .Times(1)
ossu61a208b2016-09-20 01:38:00 -07001087 .WillOnce(DoAll(
1088 SetArrayArgument<3>(dummy_output,
1089 dummy_output + kPayloadLengthSamples * kChannels),
1090 SetArgPointee<4>(AudioDecoder::kSpeech),
1091 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001092
ossu61a208b2016-09-20 01:38:00 -07001093 EXPECT_CALL(decoder,
1094 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1095 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001096 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -07001097
Jonas Olssona4d87372019-07-05 19:08:33 +02001098 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -07001099
Jonas Olssona4d87372019-07-05 19:08:33 +02001100 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001101
Niels Möllera1eb9c72018-12-07 15:24:42 +01001102 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1103 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001104
1105 // Insert one packet.
1106 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001107 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001108
1109 // Insert another packet.
1110 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001111 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001112 // The second timestamp needs to be at least 30 ms after the first to make
1113 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001114 rtp_header.timestamp += 3 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001115 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001116
henrik.lundin6d8e0112016-03-04 10:34:21 -08001117 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001118 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001119 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001120 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001121 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001122 // Output size and number of channels should be correct.
1123 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1124 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1125 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001126 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001127
henrik.lundin6d8e0112016-03-04 10:34:21 -08001128 // Second call to GetAudio will decode the packet that is ok. No errors are
1129 // expected.
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(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1132 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001133 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001134
1135 // Die isn't called through NiceMock (since it's called by the
1136 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1137 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001138}
1139
henrik.lundin116c84e2015-08-27 13:14:48 -07001140// This test inserts packets until the buffer is flushed. After that, it asks
1141// NetEq for the network statistics. The purpose of the test is to make sure
1142// that even though the buffer size increment is negative (which it becomes when
1143// the packet causing a flush is inserted), the packet length stored in the
1144// decision logic remains valid.
1145TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1146 UseNoMocks();
1147 CreateInstance();
1148
1149 const size_t kPayloadLengthSamples = 80;
1150 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1151 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin116c84e2015-08-27 13:14:48 -07001152 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001153 RTPHeader rtp_header;
1154 rtp_header.payloadType = kPayloadType;
1155 rtp_header.sequenceNumber = 0x1234;
1156 rtp_header.timestamp = 0x12345678;
1157 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001158
Niels Möller05543682019-01-10 16:55:06 +01001159 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1160 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001161
1162 // Insert packets until the buffer flushes.
1163 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1164 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
Karl Wiberg45eb1352019-10-10 14:23:00 +02001165 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -07001166 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1167 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001168 }
1169 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1170
1171 // Ask for network statistics. This should not crash.
1172 NetEqNetworkStatistics stats;
1173 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1174}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001175
1176TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1177 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001178 // Create a mock decoder object.
1179 MockAudioDecoder mock_decoder;
1180
1181 CreateInstance(
1182 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001183
1184 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001185 const int kSampleRateHz = 8000;
1186 const size_t kPayloadLengthSamples =
1187 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1188 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1189 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001190 RTPHeader rtp_header;
1191 rtp_header.payloadType = kPayloadType;
1192 rtp_header.sequenceNumber = 0x1234;
1193 rtp_header.timestamp = 0x12345678;
1194 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001195
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001196 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001197 EXPECT_CALL(mock_decoder, SampleRateHz())
1198 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001199 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001200 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001201 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001202 int16_t dummy_output[kPayloadLengthSamples] = {0};
1203 // The below expectation will make the mock decoder write
1204 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1205 // speech. That is, the decoded length is 5 samples shorter than the expected.
1206 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001207 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001208 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001209 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001210 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001211 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001212 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001213 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1214 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001215
1216 // Insert one packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001217 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001218
1219 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1220
1221 // Pull audio once.
1222 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001223 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001224 bool muted;
1225 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001226 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1227 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001228 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001229 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001230
1231 EXPECT_CALL(mock_decoder, Die());
1232}
minyuel6d92bf52015-09-23 15:20:39 +02001233
1234// This test checks the behavior of NetEq when audio decoder fails.
1235TEST_F(NetEqImplTest, DecodingError) {
1236 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001237 // Create a mock decoder object.
1238 MockAudioDecoder mock_decoder;
1239
1240 CreateInstance(
1241 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001242
1243 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001244 const int kSampleRateHz = 8000;
1245 const int kDecoderErrorCode = -97; // Any negative number.
1246
1247 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1248 const size_t kFrameLengthSamples =
1249 static_cast<size_t>(5 * kSampleRateHz / 1000);
1250
1251 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1252
1253 uint8_t payload[kPayloadLengthBytes] = {0};
1254
henrik.lundin246ef3e2017-04-24 09:14:32 -07001255 RTPHeader rtp_header;
1256 rtp_header.payloadType = kPayloadType;
1257 rtp_header.sequenceNumber = 0x1234;
1258 rtp_header.timestamp = 0x12345678;
1259 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001260
minyuel6d92bf52015-09-23 15:20:39 +02001261 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001262 EXPECT_CALL(mock_decoder, SampleRateHz())
1263 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001264 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001265 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001266 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001267 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1268 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001269 int16_t dummy_output[kFrameLengthSamples] = {0};
1270
1271 {
1272 InSequence sequence; // Dummy variable.
1273 // Mock decoder works normally the first time.
1274 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001275 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001276 .Times(3)
1277 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001278 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001279 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001280 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001281 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001282 .RetiresOnSaturation();
1283
1284 // Then mock decoder fails. A common reason for failure can be buffer being
1285 // too short
1286 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001287 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001288 .WillOnce(Return(-1))
1289 .RetiresOnSaturation();
1290
1291 // Mock decoder finally returns to normal.
1292 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001293 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001294 .Times(2)
1295 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001296 DoAll(SetArrayArgument<3>(dummy_output,
1297 dummy_output + kFrameLengthSamples),
1298 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001299 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001300 }
1301
Niels Möllera1eb9c72018-12-07 15:24:42 +01001302 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1303 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001304
1305 // Insert packets.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +02001306 for (int i = 0; i < 20; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001307 rtp_header.sequenceNumber += 1;
1308 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001309 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001310 }
1311
1312 // Pull audio.
1313 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001314 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001315 bool muted;
1316 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001317 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1318 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001319 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001320 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001321
1322 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001323 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001324 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1325 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001326 // We are not expecting anything for output.speech_type_, since an error was
1327 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001328
1329 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001330 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001331 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1332 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001333 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001334 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001335
1336 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001337 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001338 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1339 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001340 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001341 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001342
1343 EXPECT_CALL(mock_decoder, Die());
1344}
1345
1346// This test checks the behavior of NetEq when audio decoder fails during CNG.
1347TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1348 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001349
1350 // Create a mock decoder object.
1351 MockAudioDecoder mock_decoder;
1352 CreateInstance(
1353 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001354
1355 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001356 const int kSampleRateHz = 8000;
1357 const int kDecoderErrorCode = -97; // Any negative number.
1358
1359 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1360 const size_t kFrameLengthSamples =
1361 static_cast<size_t>(5 * kSampleRateHz / 1000);
1362
1363 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1364
1365 uint8_t payload[kPayloadLengthBytes] = {0};
1366
henrik.lundin246ef3e2017-04-24 09:14:32 -07001367 RTPHeader rtp_header;
1368 rtp_header.payloadType = kPayloadType;
1369 rtp_header.sequenceNumber = 0x1234;
1370 rtp_header.timestamp = 0x12345678;
1371 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001372
minyuel6d92bf52015-09-23 15:20:39 +02001373 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001374 EXPECT_CALL(mock_decoder, SampleRateHz())
1375 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001376 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001377 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001378 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001379 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001380 int16_t dummy_output[kFrameLengthSamples] = {0};
1381
1382 {
1383 InSequence sequence; // Dummy variable.
1384 // Mock decoder works normally the first 2 times.
1385 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001386 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001387 .Times(2)
1388 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001389 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001390 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001391 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001392 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001393 .RetiresOnSaturation();
1394
1395 // Then mock decoder fails. A common reason for failure can be buffer being
1396 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001397 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001398 .WillOnce(Return(-1))
1399 .RetiresOnSaturation();
1400
1401 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001402 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001403 .Times(2)
1404 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001405 DoAll(SetArrayArgument<3>(dummy_output,
1406 dummy_output + kFrameLengthSamples),
1407 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001408 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001409 }
1410
Niels Möller50b66d52018-12-11 14:43:21 +01001411 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1412 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001413
1414 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1415 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001416 rtp_header.sequenceNumber += 1;
1417 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001418 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001419 }
1420
1421 // Pull audio.
1422 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001423 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001424 bool muted;
1425 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001426 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1427 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001428 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001429
1430 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001431 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001432 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1433 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001434 // We are not expecting anything for output.speech_type_, since an error was
1435 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001436
1437 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001438 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001439 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1440 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001441 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001442
1443 EXPECT_CALL(mock_decoder, Die());
1444}
1445
henrik.lundind89814b2015-11-23 06:49:25 -08001446// Tests that the return value from last_output_sample_rate_hz() is equal to the
1447// configured inital sample rate.
1448TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1449 UseNoMocks();
1450 config_.sample_rate_hz = 48000;
1451 CreateInstance();
1452 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1453}
1454
henrik.lundined497212016-04-25 10:11:38 -07001455TEST_F(NetEqImplTest, TickTimerIncrement) {
1456 UseNoMocks();
1457 CreateInstance();
1458 ASSERT_TRUE(tick_timer_);
1459 EXPECT_EQ(0u, tick_timer_->ticks());
1460 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001461 bool muted;
1462 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001463 EXPECT_EQ(1u, tick_timer_->ticks());
1464}
1465
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001466TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1467 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001468 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001469 CreateInstance();
1470
Ivo Creusen53a31f72019-10-24 15:20:39 +02001471 EXPECT_CALL(*mock_neteq_controller_, SetBaseMinimumDelay(_))
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001472 .WillOnce(Return(true))
1473 .WillOnce(Return(false));
1474
1475 const int delay_ms = 200;
1476
1477 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1478 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1479}
1480
1481TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1482 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001483 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001484 CreateInstance();
1485
1486 const int delay_ms = 200;
1487
Ivo Creusen53a31f72019-10-24 15:20:39 +02001488 EXPECT_CALL(*mock_neteq_controller_, GetBaseMinimumDelay())
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001489 .WillOnce(Return(delay_ms));
1490
1491 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1492}
1493
henrik.lundin114c1b32017-04-26 07:47:32 -07001494TEST_F(NetEqImplTest, TargetDelayMs) {
1495 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001496 use_mock_neteq_controller_ = true;
henrik.lundin114c1b32017-04-26 07:47:32 -07001497 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001498 constexpr int kTargetLevelMs = 510;
1499 EXPECT_CALL(*mock_neteq_controller_, TargetLevelMs())
1500 .WillOnce(Return(kTargetLevelMs));
1501 EXPECT_EQ(510, neteq_->TargetDelayMs());
henrik.lundin114c1b32017-04-26 07:47:32 -07001502}
1503
henrik.lundinb8c55b12017-05-10 07:38:01 -07001504TEST_F(NetEqImplTest, InsertEmptyPacket) {
1505 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001506 use_mock_neteq_controller_ = true;
henrik.lundinb8c55b12017-05-10 07:38:01 -07001507 CreateInstance();
1508
1509 RTPHeader rtp_header;
1510 rtp_header.payloadType = 17;
1511 rtp_header.sequenceNumber = 0x1234;
1512 rtp_header.timestamp = 0x12345678;
1513 rtp_header.ssrc = 0x87654321;
1514
Ivo Creusen53a31f72019-10-24 15:20:39 +02001515 EXPECT_CALL(*mock_neteq_controller_, RegisterEmptyPacket());
henrik.lundinb8c55b12017-05-10 07:38:01 -07001516 neteq_->InsertEmptyPacket(rtp_header);
1517}
1518
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001519TEST_F(NetEqImplTest, EnableRtxHandling) {
1520 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001521 use_mock_neteq_controller_ = true;
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001522 config_.enable_rtx_handling = true;
1523 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001524 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001525 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001526 .WillOnce(Return(NetEq::Operation::kNormal));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001527
1528 const int kPayloadLengthSamples = 80;
1529 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1530 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001531 uint8_t payload[kPayloadLengthBytes] = {0};
1532 RTPHeader rtp_header;
1533 rtp_header.payloadType = kPayloadType;
1534 rtp_header.sequenceNumber = 0x1234;
1535 rtp_header.timestamp = 0x12345678;
1536 rtp_header.ssrc = 0x87654321;
1537
Niels Möller05543682019-01-10 16:55:06 +01001538 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1539 SdpAudioFormat("l16", 8000, 1)));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001540 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001541 AudioFrame output;
1542 bool muted;
1543 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1544
1545 // Insert second packet that was sent before the first packet.
1546 rtp_header.sequenceNumber -= 1;
1547 rtp_header.timestamp -= kPayloadLengthSamples;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001548 EXPECT_CALL(*mock_neteq_controller_,
1549 PacketArrived(
1550 /*last_cng_or_dtmf*/ _,
1551 /*packet_length_samples*/ kPayloadLengthSamples,
1552 /*should_update_stats*/ _,
1553 /*main_sequence_number*/ rtp_header.sequenceNumber,
1554 /*main_timestamp*/ rtp_header.timestamp,
1555 /*fs_hz*/ 8000));
1556
Karl Wiberg45eb1352019-10-10 14:23:00 +02001557 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001558}
1559
minyue5bd33972016-05-02 04:46:11 -07001560class Decoder120ms : public AudioDecoder {
1561 public:
kwiberg347d3512016-06-16 01:59:09 -07001562 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1563 : sample_rate_hz_(sample_rate_hz),
1564 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001565 speech_type_(speech_type) {}
1566
1567 int DecodeInternal(const uint8_t* encoded,
1568 size_t encoded_len,
1569 int sample_rate_hz,
1570 int16_t* decoded,
1571 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001572 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001573 size_t decoded_len =
1574 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1575 for (size_t i = 0; i < decoded_len; ++i) {
1576 decoded[i] = next_value_++;
1577 }
1578 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001579 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001580 }
1581
1582 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001583 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001584 size_t Channels() const override { return 2; }
1585
1586 private:
kwiberg347d3512016-06-16 01:59:09 -07001587 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001588 int16_t next_value_;
1589 SpeechType speech_type_;
1590};
1591
1592class NetEqImplTest120ms : public NetEqImplTest {
1593 protected:
1594 NetEqImplTest120ms() : NetEqImplTest() {}
1595 virtual ~NetEqImplTest120ms() {}
1596
1597 void CreateInstanceNoMocks() {
1598 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001599 CreateInstance(decoder_factory_);
1600 EXPECT_TRUE(neteq_->RegisterPayloadType(
1601 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001602 }
1603
1604 void CreateInstanceWithDelayManagerMock() {
1605 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001606 use_mock_neteq_controller_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001607 CreateInstance(decoder_factory_);
1608 EXPECT_TRUE(neteq_->RegisterPayloadType(
1609 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001610 }
1611
1612 uint32_t timestamp_diff_between_packets() const {
1613 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1614 }
1615
1616 uint32_t first_timestamp() const { return 10u; }
1617
1618 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001619 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001620 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001621 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1622 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001623 }
1624 }
1625
1626 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001627 RTPHeader rtp_header;
1628 rtp_header.payloadType = kPayloadType;
1629 rtp_header.sequenceNumber = sequence_number_;
1630 rtp_header.timestamp = timestamp;
1631 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001632 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1633 uint8_t payload[kPayloadLengthBytes] = {0};
Karl Wiberg45eb1352019-10-10 14:23:00 +02001634 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue5bd33972016-05-02 04:46:11 -07001635 sequence_number_++;
1636 }
1637
1638 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001639 const uint32_t sampling_freq = kSamplingFreq_;
1640 decoder_factory_ =
1641 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1642 [sampling_freq, speech_type]() {
1643 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001644 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001645 RTC_CHECK_EQ(2, decoder->Channels());
1646 return decoder;
1647 });
minyue5bd33972016-05-02 04:46:11 -07001648 }
1649
Niels Möllera0f44302018-11-30 10:45:12 +01001650 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001651 AudioFrame output_;
1652 const uint32_t kPayloadType = 17;
1653 const uint32_t kSamplingFreq_ = 48000;
1654 uint16_t sequence_number_ = 1;
1655};
1656
minyue5bd33972016-05-02 04:46:11 -07001657TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001658 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001659 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001660
1661 InsertPacket(first_timestamp());
1662 GetFirstPacket();
1663
henrik.lundin7a926812016-05-12 13:51:28 -07001664 bool muted;
1665 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001666 EXPECT_EQ(NetEq::Operation::kCodecInternalCng,
1667 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001668}
1669
1670TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001671 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001672 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001673
1674 InsertPacket(first_timestamp());
1675 GetFirstPacket();
1676
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001677 EXPECT_EQ(NetEq::Operation::kNormal, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001678}
1679
1680TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001681 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001682 CreateInstanceWithDelayManagerMock();
1683
Ivo Creusen53a31f72019-10-24 15:20:39 +02001684 EXPECT_CALL(*mock_neteq_controller_, CngOff()).WillRepeatedly(Return(true));
minyue5bd33972016-05-02 04:46:11 -07001685 InsertPacket(first_timestamp());
1686
1687 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001688 bool muted;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001689 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001690 .WillOnce(Return(NetEq::Operation::kExpand));
henrik.lundin7a926812016-05-12 13:51:28 -07001691 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001692
1693 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1694
Ivo Creusen53a31f72019-10-24 15:20:39 +02001695 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001696 .WillOnce(Return(NetEq::Operation::kMerge));
minyue5bd33972016-05-02 04:46:11 -07001697
henrik.lundin7a926812016-05-12 13:51:28 -07001698 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001699 EXPECT_EQ(NetEq::Operation::kMerge, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001700}
1701
1702TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001703 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001704 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001705
1706 InsertPacket(first_timestamp());
1707 GetFirstPacket();
1708
henrik.lundin7a926812016-05-12 13:51:28 -07001709 bool muted;
1710 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001711 EXPECT_EQ(NetEq::Operation::kExpand, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001712}
1713
1714TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001715 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001716 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001717
1718 InsertPacket(first_timestamp());
1719 GetFirstPacket();
1720 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1721
Ivo Creusen53a31f72019-10-24 15:20:39 +02001722 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001723 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001724 .WillOnce(Return(NetEq::Operation::kFastAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001725
henrik.lundin7a926812016-05-12 13:51:28 -07001726 bool muted;
1727 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001728 EXPECT_EQ(NetEq::Operation::kFastAccelerate,
1729 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001730}
1731
1732TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001733 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001734 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001735
1736 InsertPacket(first_timestamp());
1737 GetFirstPacket();
1738
1739 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1740
Ivo Creusen53a31f72019-10-24 15:20:39 +02001741 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001742 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001743 .WillOnce(Return(NetEq::Operation::kPreemptiveExpand));
minyue5bd33972016-05-02 04:46:11 -07001744
henrik.lundin7a926812016-05-12 13:51:28 -07001745 bool muted;
1746 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001747 EXPECT_EQ(NetEq::Operation::kPreemptiveExpand,
1748 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001749}
1750
1751TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001752 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001753 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001754
1755 InsertPacket(first_timestamp());
1756 GetFirstPacket();
1757
1758 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1759
Ivo Creusen53a31f72019-10-24 15:20:39 +02001760 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001761 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001762 .WillOnce(Return(NetEq::Operation::kAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001763
henrik.lundin7a926812016-05-12 13:51:28 -07001764 bool muted;
1765 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001766 EXPECT_EQ(NetEq::Operation::kAccelerate, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001767}
1768
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001769} // namespace webrtc