-
Notifications
You must be signed in to change notification settings - Fork 5
/
inter_arrival_unittest.cpp
308 lines (253 loc) · 11.5 KB
/
inter_arrival_unittest.cpp
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
/*****************************************************************
* Copyright (C) 2020 Zuoyebang.com, Inc. All Rights Reserved.
*
* @file inter_arrival_unittest.cpp
* @author yujitai([email protected])
* @date 2020/03/14
* @brief
*****************************************************************/
#include <iostream>
using namespace std;
#include "inter_arrival.h"
namespace webrtc {
enum {
kTimestampGroupLengthUs = 5000,
kMinStep = 20,
kTriggerNewGroupUs = kTimestampGroupLengthUs + kMinStep,
kBurstThresholdMs = 5,
kAbsSendTimeFraction = 18,
kAbsSendTimeInterArrivalUpshift = 8,
kInterArrivalShift = kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift,
};
const double kRtpTimestampToMs = 1.0 / 90.0;
const double kAstToMs = 1000.0 / static_cast<double>(1 << kInterArrivalShift);
void TestInterArrival01() {
InterArrival* inter_arrival = new InterArrival(kTimestampGroupLengthUs / 1000, 1.0, true);
cout << "==========初始化InterArrival==========" << endl;
cout << "包组大小(ms)=" << kTimestampGroupLengthUs / 1000 << " 时间戳转换系数=1.0" << " 开启burst" << endl;
const size_t kPacketSize = 1000;
uint32_t send_time_ms = 10000;
int64_t arrival_time_ms = 20000;
int64_t system_time_ms = 30000;
uint32_t send_delta;
int64_t arrival_delta;
int size_delta;
bool calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
const int kTimeDeltaMs = 30;
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs + InterArrival::kArrivalTimeOffsetThresholdMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
// The previous arrival time jump should now be detected and cause a reset.
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
// The two next packets will not give a valid delta since we're in the initial state.
for (int i = 0; i < 2; i++) {
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
}
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
delete inter_arrival;
}
void TestInterArrival02() {
InterArrival* inter_arrival = new InterArrival(kTimestampGroupLengthUs / 1000, 1.0, true);
cout << "==========初始化InterArrival";
cout << " 包组大小(ms)=" << kTimestampGroupLengthUs / 1000;
cout << " 时间戳转换系数=1.0" <<
cout << " 开启burst==========" << endl;
// group1
const size_t kPacketSize = 1000;
uint32_t send_time_ms = 10000;
int64_t arrival_time_ms = 20000;
int64_t system_time_ms = 30000;
uint32_t send_delta;
int64_t arrival_delta;
int size_delta;
bool calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
// group2
const int kTimeDeltaMs = 30;
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
// group3
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
cout << "==========突发数据与到达时间乱序测试==========" << endl;
// Three out of order will fail, after that we will be reset and two more will
// fail before we get our first valid delta after the reset.
// arrival_time_ms += 3000; // 测试到达时间相较于系统时间跳变
arrival_time_ms -= 1000; // 测试到达时间乱序,到达时间间隔为负
// group4~group9
for (int i = 0; i < InterArrival::kReorderedResetThreshold + 3; ++i) {
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
// The previous arrival time jump should now be detected and cause a reset.
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
}
// group10
send_time_ms += kTimeDeltaMs;
arrival_time_ms += kTimeDeltaMs;
system_time_ms += kTimeDeltaMs;
calculated_deltas = inter_arrival->ComputeDeltas(send_time_ms, arrival_time_ms, system_time_ms, kPacketSize,
&send_delta, &arrival_delta, &size_delta);
delete inter_arrival;
}
static uint32_t MakeRtpTimestamp(int64_t us) {
return static_cast<uint32_t>(static_cast<uint64_t>(us * 90 + 500) / 1000);
}
void TestInterArrival03() {
InterArrival* inter_arrival_rtp = new InterArrival(MakeRtpTimestamp(kTimestampGroupLengthUs), kRtpTimestampToMs, true);
// group1
int64_t arrival_time = 17;
int64_t timestamp = 0;
uint32_t delta_timestamp = 101;
int64_t delta_arrival_time_ms = 303;
int delta_packet_size = 909;
bool computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
int64_t g1_timestamp = timestamp;
int64_t g1_arrival_time = arrival_time;
// group2
arrival_time += 11;
timestamp += kTriggerNewGroupUs;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 2,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
for (int i = 0; i < 10; ++i) {
arrival_time += kBurstThresholdMs + 1;
timestamp += kMinStep;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
}
int64_t g2_timestamp = timestamp;
int64_t g2_arrival_time = arrival_time;
// This packet is out of order and should be dropped.
arrival_time = 281;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(g1_timestamp), arrival_time, arrival_time, 100,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
// group3
arrival_time = 500;
timestamp = 2 * kTriggerNewGroupUs;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 100,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
delete inter_arrival_rtp;
}
void TestInterArrival04() {
InterArrival* inter_arrival_rtp = new InterArrival(MakeRtpTimestamp(kTimestampGroupLengthUs), kRtpTimestampToMs, true);
int64_t arrival_time = 17;
int64_t timestamp = 0;
uint32_t delta_timestamp = 101;
int64_t delta_arrival_time_ms = 303;
int delta_packet_size = 909;
bool computed = inter_arrival_rtp->ComputeDeltas(
timestamp, arrival_time, arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
int64_t g1_timestamp = timestamp;
int64_t g1_arrival_time = arrival_time;
timestamp += kTriggerNewGroupUs;
arrival_time += 11;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 2,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
timestamp += 10 * kMinStep;
int64_t g2_timestamp = timestamp;
for (int i = 0; i < 10; ++i) {
// These packets arrive with timestamps in decreasing order but are
// nevertheless accumulated to group because their timestamps are higher
// than the initial timestamp of the group.
arrival_time += kBurstThresholdMs + 1;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
timestamp -= kMinStep;
}
int64_t g2_arrival_time = arrival_time;
// However, this packet is deemed out of order and should be dropped.
// 因为时间戳小于当前包组首包时间戳
arrival_time = 281;
timestamp = g1_timestamp;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 100,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
// group3
timestamp = 2 * kTriggerNewGroupUs;
arrival_time = 500;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 100,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
delete inter_arrival_rtp;
}
void TestInterArrival05() {
InterArrival* inter_arrival_rtp = new InterArrival(MakeRtpTimestamp(kTimestampGroupLengthUs), kRtpTimestampToMs, true);
// group1
int64_t g1_arrival_time = 17;
uint32_t delta_timestamp = 101;
int64_t delta_arrival_time_ms = 303;
int delta_packet_size = 909;
bool computed = inter_arrival_rtp->ComputeDeltas(
0, g1_arrival_time, g1_arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
// group2
int64_t timestamp = kTriggerNewGroupUs;
int64_t arrival_time = 100; // Simulate no packets arriving for 100 ms.
for (int i = 0; i < 10; ++i) {
// A bunch of packets arriving in one burst (within 5 ms apart).
timestamp += 30000;
arrival_time += kBurstThresholdMs;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 1,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
}
int64_t g2_arrival_time = arrival_time;
int64_t g2_timestamp = timestamp;
// group3
timestamp += 30000;
arrival_time += kBurstThresholdMs + 1;
computed = inter_arrival_rtp->ComputeDeltas(
MakeRtpTimestamp(timestamp), arrival_time, arrival_time, 100,
&delta_timestamp, &delta_arrival_time_ms, &delta_packet_size);
delete inter_arrival_rtp;
}
} // namespace webrtc
int main() {
// PositiveArrivalTimeJump
// webrtc::TestInterArrival01();
// NegativeArrivalTimeJump
webrtc::TestInterArrival02();
// OutOfOrderPacket
// webrtc::TestInterArrival03();
// OutOfOrderWithinGroup
// webrtc::TestInterArrival04();
// TwoBursts
// webrtc::TestInterArrival05();
return 0;
}