-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjitter_reduction_timestamps_tests.cpp
346 lines (255 loc) · 9.39 KB
/
jitter_reduction_timestamps_tests.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
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
//
// Copyright (c) 2023 Native Instruments
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <gtest/gtest.h>
#include <midi/jitter_reduction_timestamps.h>
#include <chrono>
#include <random>
#include <thread>
//-----------------------------------------------
class jr_timestamps : public ::testing::Test
{
public:
};
//-----------------------------------------------
TEST_F(jr_timestamps, jr_timestamp_t_constructor)
{
using namespace midi;
jr_timestamp_t t0;
EXPECT_EQ(0u, t0.value);
jr_timestamp_t t1{ 55u };
EXPECT_EQ(55u, t1.value);
jr_timestamp_t t2{ 17283u };
EXPECT_EQ(17283u, t2.value);
jr_timestamp_t t3{ 0xFFFF };
EXPECT_EQ(0xFFFF, t3.value);
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_timestamp_t_equality)
{
using namespace midi;
jr_timestamp_t t1{ 55u };
jr_timestamp_t t2{ 17283u };
jr_timestamp_t t3{ 0xFFFF };
jr_timestamp_t t4{ 55u };
EXPECT_FALSE(t1 == t2);
EXPECT_TRUE(t1 != t2);
EXPECT_FALSE(t2 == t1);
EXPECT_TRUE(t2 != t1);
EXPECT_FALSE(t2 == t3);
EXPECT_TRUE(t2 != t3);
EXPECT_FALSE(t3 == t2);
EXPECT_TRUE(t3 != t2);
EXPECT_FALSE(t3 == t4);
EXPECT_TRUE(t3 != t4);
EXPECT_FALSE(t4 == t3);
EXPECT_TRUE(t4 != t3);
EXPECT_FALSE(t1 == t3);
EXPECT_TRUE(t1 != t3);
EXPECT_FALSE(t3 == t1);
EXPECT_TRUE(t3 != t1);
EXPECT_FALSE(t2 == t4);
EXPECT_TRUE(t2 != t4);
EXPECT_FALSE(t4 == t2);
EXPECT_TRUE(t4 != t2);
EXPECT_TRUE(t1 == t4);
EXPECT_FALSE(t1 != t4);
EXPECT_TRUE(t4 == t1);
EXPECT_FALSE(t4 != t1);
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_timestamp_t_minus)
{
using namespace midi;
jr_timestamp_t t1{ 55u };
jr_timestamp_t t2{ 17283u };
jr_timestamp_t t3{ 0xFFFF };
jr_timestamp_t t4{ 55u };
const auto t2_minus_t1 = t2 - t1;
const auto t3_minus_t2 = t3 - t2;
const auto t4_minus_t3 = t4 - t3;
const auto t1_minus_t4 = t1 - t4;
EXPECT_EQ(17228, t2_minus_t1.count());
EXPECT_EQ(48252, t3_minus_t2.count());
EXPECT_EQ(56, t4_minus_t3.count());
EXPECT_EQ(0, t1_minus_t4.count());
const auto t1_minus_t2 = t1 - t2;
const auto t2_minus_t3 = t2 - t3;
const auto t3_minus_t4 = t3 - t4;
const auto t4_minus_t1 = t4 - t1;
EXPECT_EQ(48308, t1_minus_t2.count());
EXPECT_EQ(17284, t2_minus_t3.count());
EXPECT_EQ(65480, t3_minus_t4.count());
EXPECT_EQ(0, t4_minus_t1.count());
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_clock)
{
// needs improvement, fails on virtual CI servers
GTEST_SKIP() << "Skipping jr_clock test";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<std::uint32_t> dist(0, 1000);
for (auto i = 0; i < 20; ++i)
{
const auto delay = std::chrono::milliseconds(dist(gen));
const auto one = midi::jr_clock::now();
std::this_thread::sleep_for(delay);
const auto two = midi::jr_clock::now();
const midi::jr_ticks diff = two - one;
EXPECT_GE(diff, delay);
EXPECT_LT(diff, delay + std::chrono::milliseconds(75));
}
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_clock_message)
{
using namespace midi;
// default constructor
{
const jr_clock_message jrc;
EXPECT_EQ(1u, jrc.size());
EXPECT_EQ(midi::packet_type::utility, jrc.type());
EXPECT_EQ(midi::utility_status::jr_clock, jrc.byte2());
EXPECT_EQ(0u, jrc.byte3());
EXPECT_EQ(0u, jrc.byte4());
}
// timestamp constructor
{
const auto ts = jr_clock::now();
const jr_clock_message jrc{ ts };
EXPECT_EQ(1u, jrc.size());
EXPECT_EQ(midi::packet_type::utility, jrc.type());
EXPECT_EQ(midi::utility_status::jr_clock, jrc.byte2());
EXPECT_EQ(ts, jrc.timestamp());
}
// set timestamp
{
jr_clock_message jrc;
jrc.set_timestamp(jr_timestamp_t{ 77 });
EXPECT_EQ(77u, jrc.timestamp().value);
EXPECT_EQ(0u, jrc.byte3());
EXPECT_EQ(77u, jrc.byte4());
jrc.set_timestamp(jr_timestamp_t{ 0xF3F4 });
EXPECT_EQ(jr_timestamp_t{ 0xF3F4 }, jrc.timestamp());
EXPECT_EQ(0xF3u, jrc.byte3());
EXPECT_EQ(0xF4u, jrc.byte4());
}
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_timestamp_message)
{
using namespace midi;
// default constructor
{
const jr_timestamp_message jrts;
EXPECT_EQ(1u, jrts.size());
EXPECT_EQ(midi::packet_type::utility, jrts.type());
EXPECT_EQ(midi::utility_status::jr_timestamp, jrts.byte2());
EXPECT_EQ(0u, jrts.byte3());
EXPECT_EQ(0u, jrts.byte4());
}
// timestamp constructor, default channel group
{
const auto ts = jr_clock::now();
const jr_timestamp_message jrts{ ts };
EXPECT_EQ(1u, jrts.size());
EXPECT_EQ(midi::packet_type::utility, jrts.type());
EXPECT_EQ(midi::utility_status::jr_timestamp, jrts.byte2());
EXPECT_EQ(ts, jrts.timestamp());
}
// set timestamp
{
jr_timestamp_message jrts;
jrts.set_timestamp(jr_timestamp_t{ 18334 });
EXPECT_EQ(18334u, jrts.timestamp().value);
EXPECT_EQ(0x47u, jrts.byte3());
EXPECT_EQ(0x9Eu, jrts.byte4());
jrts.set_timestamp(jr_timestamp_t{ 0xABCD });
EXPECT_EQ(jr_timestamp_t{ 0xABCD }, jrts.timestamp());
EXPECT_EQ(0xABu, jrts.byte3());
EXPECT_EQ(0xCDu, jrts.byte4());
}
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_clock_follower)
{
midi::jr_clock_follower jrcf;
EXPECT_EQ(0u, jrcf.jitter().count());
jrcf.set_security_offset(std::chrono::microseconds(1234));
EXPECT_EQ(std::chrono::microseconds(1234), jrcf.security_offset());
jrcf.set_security_offset(std::chrono::milliseconds(3));
EXPECT_EQ(std::chrono::milliseconds(3), jrcf.security_offset());
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_clock_follower_schedule_message)
{
// needs improvement, fails on virtual CI servers
GTEST_SKIP() << "Skipping jr_clock_follower_schedule_message test";
using namespace midi;
jr_clock_follower jrcf;
unsigned security_offsets[] = { 543, 755, 1033, 2000, 3210 };
std::uint16_t timestamps[] = { 0, 100, 1733, 4822, 10999, 33488, 0xFFFF };
const auto now = jr_clock_follower::system_clock::now();
// no clocks received
for (auto s : security_offsets)
{
const auto offset = std::chrono::microseconds(s);
jrcf.set_security_offset(offset);
for (auto ts : timestamps)
{
const auto at = jrcf.schedule_message(now, jr_timestamp_t{ ts });
// no jitter reduction
EXPECT_EQ(at, now + offset);
}
}
// clock with timestamp 0 received
for (auto s : security_offsets)
{
const auto offset = std::chrono::microseconds(s);
jrcf.reset();
jrcf.set_security_offset(offset);
jrcf.process_clock(now, jr_timestamp_t{ 0 });
for (auto ts : timestamps)
{
const auto expected =
now + offset + std::chrono::duration_cast<jr_clock_follower::duration>(jr_ticks{ ts });
const auto at = jrcf.schedule_message(now, jr_timestamp_t{ ts });
EXPECT_EQ(at, expected);
}
}
}
//-----------------------------------------------
TEST_F(jr_timestamps, jr_clock_follower_process_clock)
{
// needs improvement, fails on virtual CI servers
GTEST_SKIP() << "Skipping jr_clock_follower_process_clock test";
using namespace midi;
jr_clock_follower jrcf;
// TODO: improve this test - use artificial test data dor better test coverage
jrcf.process_clock(jr_clock_follower::system_clock::now(), jr_timestamp_t{ 0 });
jrcf.process_clock(jr_clock_follower::system_clock::now(), jr_timestamp_t{ 24 });
jrcf.process_clock(jr_clock_follower::system_clock::now(), jr_timestamp_t{ 48 });
EXPECT_NE(jr_clock_follower::duration{ 0 }, jrcf.jitter());
jrcf.process_clock(jr_clock_follower::system_clock::now(), jr_timestamp_t{ 456 });
EXPECT_NE(jr_clock_follower::duration{ 0 }, jrcf.jitter());
}
//-----------------------------------------------