1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
* Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/pacing/prioritized_packet_queue.h"
#include <utility>
#include "api/units/time_delta.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
#include "rtc_base/checks.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
constexpr uint32_t kDefaultSsrc = 123;
constexpr int kDefaultPayloadSize = 789;
std::unique_ptr<RtpPacketToSend> CreatePacket(RtpPacketMediaType type,
uint16_t sequence_number,
uint32_t ssrc = kDefaultSsrc) {
auto packet = std::make_unique<RtpPacketToSend>(/*extensions=*/nullptr);
packet->set_packet_type(type);
packet->SetSsrc(ssrc);
packet->SetSequenceNumber(sequence_number);
packet->SetPayloadSize(kDefaultPayloadSize);
return packet;
}
} // namespace
TEST(PrioritizedPacketQueue, ReturnsPacketsInPrioritizedOrder) {
Timestamp now = Timestamp::Zero();
PrioritizedPacketQueue queue(now);
// Add packets in low to high packet order.
queue.Push(now, CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/1));
queue.Push(now, CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/2));
queue.Push(now, CreatePacket(RtpPacketMediaType::kForwardErrorCorrection,
/*seq=*/3));
queue.Push(now, CreatePacket(RtpPacketMediaType::kRetransmission, /*seq=*/4));
queue.Push(now, CreatePacket(RtpPacketMediaType::kAudio, /*seq=*/5));
// Packets should be returned in high to low order.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 5);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 4);
// Video and FEC prioritized equally - but video was enqueued first.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 2);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 3);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 1);
}
TEST(PrioritizedPacketQueue, ReturnsEqualPrioPacketsInRoundRobinOrder) {
Timestamp now = Timestamp::Zero();
PrioritizedPacketQueue queue(now);
// Insert video packets (prioritized equally), simulating a simulcast-type use
// case.
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/1, /*ssrc=*/100));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/2, /*ssrc=*/101));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/3, /*ssrc=*/101));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/4, /*ssrc=*/102));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/5, /*ssrc=*/102));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/6, /*ssrc=*/102));
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/7, /*ssrc=*/102));
// First packet from each SSRC.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 1);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 2);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 4);
// Second packets from streams that have packets left.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 3);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 5);
// Only packets from last stream remaining.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 6);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 7);
}
TEST(PrioritizedPacketQueue, ReportsSizeInPackets) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.SizeInPackets(), 0);
queue.Push(/*enqueue_time=*/Timestamp::Zero(),
CreatePacket(RtpPacketMediaType::kVideo,
/*seq_no=*/1));
EXPECT_EQ(queue.SizeInPackets(), 1);
queue.Pop();
EXPECT_EQ(queue.SizeInPackets(), 0);
}
TEST(PrioritizedPacketQueue, ReportsPayloadSize) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.SizeInPayloadBytes(), DataSize::Zero());
queue.Push(/*enqueue_time=*/Timestamp::Zero(),
CreatePacket(RtpPacketMediaType::kVideo,
/*seq_no=*/1));
EXPECT_EQ(queue.SizeInPayloadBytes(), DataSize::Bytes(kDefaultPayloadSize));
queue.Pop();
EXPECT_EQ(queue.SizeInPayloadBytes(), DataSize::Zero());
}
TEST(PrioritizedPacketQueue, ReportsPaddingSize) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.SizeInPayloadBytes(), DataSize::Zero());
static constexpr DataSize kPaddingSize = DataSize::Bytes(190);
auto packet = std::make_unique<RtpPacketToSend>(/*extensions=*/nullptr);
packet->set_packet_type(RtpPacketMediaType::kPadding);
packet->SetSsrc(kDefaultSsrc);
packet->SetSequenceNumber(/*seq=*/1);
packet->SetPadding(kPaddingSize.bytes());
queue.Push(/*enqueue_time=*/Timestamp::Zero(), std::move(packet));
EXPECT_EQ(queue.SizeInPayloadBytes(), kPaddingSize);
queue.Pop();
EXPECT_EQ(queue.SizeInPayloadBytes(), DataSize::Zero());
}
TEST(PrioritizedPacketQueue, ReportsOldestEnqueueTime) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.OldestEnqueueTime(), Timestamp::MinusInfinity());
// Add three packets, with the middle packet having higher prio.
queue.Push(Timestamp::Millis(10),
CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/1));
queue.Push(Timestamp::Millis(20),
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/2));
queue.Push(Timestamp::Millis(30),
CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/3));
EXPECT_EQ(queue.OldestEnqueueTime(), Timestamp::Millis(10));
queue.Pop(); // Pop packet with enqueue time 20.
EXPECT_EQ(queue.OldestEnqueueTime(), Timestamp::Millis(10));
queue.Pop(); // Pop packet with enqueue time 10.
EXPECT_EQ(queue.OldestEnqueueTime(), Timestamp::Millis(30));
queue.Pop(); // Pop packet with enqueue time 30, queue empty again.
EXPECT_EQ(queue.OldestEnqueueTime(), Timestamp::MinusInfinity());
}
TEST(PrioritizedPacketQueue, ReportsAverageQueueTime) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Zero());
// Add three packets, with the middle packet having higher prio.
queue.Push(Timestamp::Millis(10),
CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/1));
queue.Push(Timestamp::Millis(20),
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/2));
queue.Push(Timestamp::Millis(30),
CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/3));
queue.UpdateAverageQueueTime(Timestamp::Millis(40));
// Packets have waited 30, 20, 10 ms -> average = 20ms.
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(20));
queue.Pop(); // Pop packet with enqueue time 20.
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(20));
queue.Pop(); // Pop packet with enqueue time 10.
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(10));
queue.Pop(); // Pop packet with enqueue time 30, queue empty again.
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Zero());
}
TEST(PrioritizedPacketQueue, SubtractsPusedTimeFromAverageQueueTime) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Zero());
// Add a packet and then enable paused state.
queue.Push(Timestamp::Millis(100),
CreatePacket(RtpPacketMediaType::kPadding, /*seq=*/1));
queue.SetPauseState(true, Timestamp::Millis(600));
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(500));
// Enqueue a packet 500ms into the paused state. Queue time of
// original packet is still seen as 500ms and new one has 0ms giving
// an average of 250ms.
queue.Push(Timestamp::Millis(1100),
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/2));
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(250));
// Unpause some time later, queue time still unchanged.
queue.SetPauseState(false, Timestamp::Millis(1600));
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(250));
// Update queue time 500ms after pause state ended.
queue.UpdateAverageQueueTime(Timestamp::Millis(2100));
EXPECT_EQ(queue.AverageQueueTime(), TimeDelta::Millis(750));
}
TEST(PrioritizedPacketQueue, ReportsLeadingPacketEnqueueTime) {
PrioritizedPacketQueue queue(/*creation_time=*/Timestamp::Zero());
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio),
Timestamp::MinusInfinity());
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kVideo),
Timestamp::MinusInfinity());
queue.Push(Timestamp::Millis(10),
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/1));
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio),
Timestamp::MinusInfinity());
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kVideo),
Timestamp::Millis(10));
queue.Push(Timestamp::Millis(20),
CreatePacket(RtpPacketMediaType::kAudio, /*seq=*/2));
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio),
Timestamp::Millis(20));
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kVideo),
Timestamp::Millis(10));
queue.Pop(); // Pop audio packet.
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio),
Timestamp::MinusInfinity());
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kVideo),
Timestamp::Millis(10));
queue.Pop(); // Pop video packet.
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio),
Timestamp::MinusInfinity());
EXPECT_EQ(queue.LeadingPacketEnqueueTime(RtpPacketMediaType::kVideo),
Timestamp::MinusInfinity());
}
TEST(PrioritizedPacketQueue,
PushAndPopUpdatesSizeInPacketsPerRtpPacketMediaType) {
Timestamp now = Timestamp::Zero();
PrioritizedPacketQueue queue(now);
// Initially all sizes are zero.
for (size_t i = 0; i < kNumMediaTypes; ++i) {
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[i], 0);
}
// Push packets.
queue.Push(now, CreatePacket(RtpPacketMediaType::kAudio, 1));
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
RtpPacketMediaType::kAudio)],
1);
queue.Push(now, CreatePacket(RtpPacketMediaType::kVideo, 2));
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
RtpPacketMediaType::kVideo)],
1);
queue.Push(now, CreatePacket(RtpPacketMediaType::kRetransmission, 3));
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
RtpPacketMediaType::kRetransmission)],
1);
queue.Push(now, CreatePacket(RtpPacketMediaType::kForwardErrorCorrection, 4));
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
RtpPacketMediaType::kForwardErrorCorrection)],
1);
queue.Push(now, CreatePacket(RtpPacketMediaType::kPadding, 5));
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
RtpPacketMediaType::kPadding)],
1);
// Now all sizes are 1.
for (size_t i = 0; i < kNumMediaTypes; ++i) {
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[i], 1);
}
// Popping happens in a priority order based on media type. This test does not
// assert what this order is, only that the counter for the popped packet's
// media type is decremented.
for (size_t i = 0; i < kNumMediaTypes; ++i) {
auto popped_packet = queue.Pop();
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[static_cast<size_t>(
popped_packet->packet_type().value())],
0);
}
// We've popped all packets, so all sizes are zero.
for (size_t i = 0; i < kNumMediaTypes; ++i) {
EXPECT_EQ(queue.SizeInPacketsPerRtpPacketMediaType()[i], 0);
}
}
TEST(PrioritizedPacketQueue, ClearsPackets) {
Timestamp now = Timestamp::Zero();
PrioritizedPacketQueue queue(now);
const uint32_t kSsrc = 1;
// Add two packets of each type, all using the same SSRC.
int sequence_number = 0;
for (size_t i = 0; i < kNumMediaTypes; ++i) {
queue.Push(now, CreatePacket(static_cast<RtpPacketMediaType>(i),
sequence_number++, kSsrc));
queue.Push(now, CreatePacket(static_cast<RtpPacketMediaType>(i),
sequence_number++, kSsrc));
}
EXPECT_EQ(queue.SizeInPackets(), 2 * int{kNumMediaTypes});
// Remove all of them.
queue.RemovePacketsForSsrc(kSsrc);
EXPECT_TRUE(queue.Empty());
}
TEST(PrioritizedPacketQueue, ClearPacketsAffectsOnlySpecifiedSsrc) {
Timestamp now = Timestamp::Zero();
PrioritizedPacketQueue queue(now);
const uint32_t kRemovingSsrc = 1;
const uint32_t kStayingSsrc = 2;
// Add an audio packet and a retransmission for the SSRC we will remove,
// ensuring they are first in line.
queue.Push(
now, CreatePacket(RtpPacketMediaType::kAudio, /*seq=*/1, kRemovingSsrc));
queue.Push(now, CreatePacket(RtpPacketMediaType::kRetransmission, /*seq=*/2,
kRemovingSsrc));
// Add a video packet and a retransmission for the SSRC that will remain.
// The retransmission packets now both have pointers to their respective qeues
// from the same prio level.
queue.Push(now,
CreatePacket(RtpPacketMediaType::kVideo, /*seq=*/3, kStayingSsrc));
queue.Push(now, CreatePacket(RtpPacketMediaType::kRetransmission, /*seq=*/4,
kStayingSsrc));
EXPECT_EQ(queue.SizeInPackets(), 4);
// Clear the first two packets.
queue.RemovePacketsForSsrc(kRemovingSsrc);
EXPECT_EQ(queue.SizeInPackets(), 2);
// We should get the single remaining retransmission first, then the video
// packet.
EXPECT_EQ(queue.Pop()->SequenceNumber(), 4);
EXPECT_EQ(queue.Pop()->SequenceNumber(), 3);
EXPECT_TRUE(queue.Empty());
}
} // namespace webrtc
|