forked from thestr4ng3r/chiaki
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtakion.c
203 lines (174 loc) · 5.95 KB
/
takion.c
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
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <munit.h>
#include <chiaki/takion.h>
#include <chiaki/seqnum.h>
#include <chiaki/base64.h>
#define CHIAKI_UNIT_TEST
#include "../lib/src/takionsendbuffer.c"
#include "test_log.h"
static MunitResult test_av_packet_parse(const MunitParameter params[], void *user)
{
uint8_t packet[] = {
0x2, 0x0, 0x2d, 0x0, 0x5, 0x0, 0xc0, 0x1c, 0x1, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xe4, 0x10, 0x3, 0x67, 0x0, 0x29, 0xf3, 0x2f, 0x98, 0xf6, 0x99, 0x82, 0x83, 0x78, 0xdb, 0x29,
0x43, 0xa9, 0xe5, 0x88, 0xf2, 0x11, 0x4, 0x20, 0xe6, 0x20, 0x96, 0xe9, 0x6, 0xee, 0xd, 0x27,
0xa1, 0x83, 0x82, 0x88, 0xe6, 0x21, 0x49, 0x2, 0x75, 0x74, 0x32, 0x5b, 0xf6, 0xe9, 0xdc, 0x93,
0xea, 0x31, 0x88, 0xd, 0x2b, 0x4b, 0x34, 0xf9, 0xec, 0x1b, 0x26, 0xcc, 0xbb, 0xbb, 0x81, 0xf2,
0xd9, 0x2d, 0x8e, 0xa1, 0xb9, 0xe2, 0xb3, 0xca, 0xb2, 0x7d, 0xa3, 0x31, 0xf0, 0x42, 0xb7, 0xb6,
0x1e, 0x8f, 0x6d, 0xa2, 0x70, 0x46, 0xfd, 0x7e, 0x9b, 0x60, 0x85, 0xb0, 0xed, 0x4f, 0x20, 0xb5,
0x1, 0x71, 0xa9, 0xaa, 0x18, 0x6b, 0x2a, 0x90, 0xf3, 0xa7, 0x84, 0x36, 0xfd, 0x6d, 0x14, 0x83,
0x68, 0xa3, 0x9b, 0x3a, 0xc8, 0xd4, 0x3a, 0x31, 0xa0, 0x9b, 0x61, 0xde, 0xa7, 0xed, 0x46, 0xb4,
0xa3, 0xdf, 0x3f, 0x44, 0x8f, 0xad, 0x64, 0x9, 0xfc, 0x7a, 0xe7, 0x24, 0xf0, 0xd2, 0x42, 0xd3,
0x57, 0x5a, 0x76, 0x0, 0xc5, 0xe0, 0x93, 0xa9, 0xf5, 0x32, 0x5d, 0xee, 0xf7, 0x9d
};
ChiakiTakionAVPacket av_packet;
ChiakiErrorCode err = chiaki_takion_v9_av_packet_parse(&av_packet, packet, sizeof(packet));
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
munit_assert(av_packet.is_video);
munit_assert_uint16(av_packet.packet_index, ==, 45);
munit_assert_uint16(av_packet.frame_index, ==, 5);
// TODO: uses_nalu_info_structs
munit_assert_uint16(av_packet.unit_index, ==, 6);
munit_assert_uint16(av_packet.units_in_frame_total, ==, 8);
munit_assert_uint16(av_packet.units_in_frame_fec, ==, 1);
munit_assert_uint32(av_packet.codec, ==, 3);
// munit_assert_uint16(av_packet.word_at_0x18, ==, 871);
munit_assert_uint8(av_packet.adaptive_stream_index, ==, 0);
// munit_assert_uint8(av_packet.byte_at_0x2c, ==, 0);
munit_assert_ptr_equal(av_packet.data, packet + 0x15);
munit_assert_size(av_packet.data_size, ==, 0x99);
return MUNIT_OK;
}
static MunitResult test_av_packet_parse_real_video(const MunitParameter params[], void *user)
{
#include "takion_av_packet_parse_real_video.inl"
return MUNIT_OK;
}
static void random_seqnums(ChiakiSeqNum32 *nums, size_t count)
{
for(size_t i=0; i<count; i++)
{
ChiakiSeqNum32 seqnum;
retry:
seqnum = munit_rand_uint32();
for(size_t j=0; j<i; j++)
{
if(nums[j] == seqnum)
goto retry;
}
nums[i] = seqnum;
}
}
static bool check_send_buffer_contents(ChiakiTakionSendBuffer *send_buffer, const ChiakiSeqNum32 *nums_expected, size_t nums_expected_count)
{
// nums_expected must be unique
if(chiaki_mutex_lock(&send_buffer->mutex) != CHIAKI_ERR_SUCCESS)
return false;
if(send_buffer->packets_count != nums_expected_count)
goto fail;
for(size_t i=0; i<nums_expected_count; i++)
{
bool found = false;
for(size_t j=0; j<send_buffer->packets_count; j++)
{
if(send_buffer->packets[j].seq_num == nums_expected[i])
{
found = true;
break;
}
}
if(!found)
goto fail;
}
chiaki_mutex_unlock(&send_buffer->mutex);
return true;
fail:
chiaki_mutex_unlock(&send_buffer->mutex);
return false;
}
static void seqnums_ack(ChiakiSeqNum32 *nums, size_t *nums_count, ChiakiSeqNum32 ack_num)
{
// simulate ack of ack_num
for(size_t i=0; i<*nums_count; i++)
{
if(nums[i] == ack_num || chiaki_seq_num_32_lt(nums[i], ack_num))
{
for(size_t j=i+1; j<*nums_count; j++)
nums[j-1] = nums[j];
(*nums_count)--;
i--;
}
}
}
static MunitResult test_takion_send_buffer(const MunitParameter params[], void *user)
{
#define nums_count 0x30
ChiakiTakionSendBuffer send_buffer;
ChiakiErrorCode err = chiaki_takion_send_buffer_init(&send_buffer, NULL, nums_count);
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
send_buffer.log = get_test_log();
ChiakiSeqNum32 nums_expected[nums_count + 1];
random_seqnums(nums_expected, nums_count + 1);
for(size_t i=0; i<nums_count; i++)
{
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[i], malloc(8), 8);
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
}
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[nums_count], malloc(8), 8);
munit_assert_int(err, ==, CHIAKI_ERR_OVERFLOW);
size_t nums_count_cur = nums_count;
while(nums_count_cur > 0)
{
ChiakiSeqNum32 ack_num = nums_expected[nums_count_cur - 1]
+ munit_rand_int_range(-1, 1) * munit_rand_int_range(1, 32);
chiaki_takion_send_buffer_ack(&send_buffer, ack_num, NULL, NULL); // TODO: test acked seqnums params
seqnums_ack(nums_expected, &nums_count_cur, ack_num);
bool correct = check_send_buffer_contents(&send_buffer, nums_expected, nums_count_cur);
munit_assert(correct);
}
chiaki_takion_send_buffer_fini(&send_buffer);
return MUNIT_OK;
#undef nums_count
}
MunitTest tests_takion[] = {
{
"/av_packet_parse",
test_av_packet_parse,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
"/av_packet_parse_real_video",
test_av_packet_parse_real_video,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
"/send_buffer",
test_takion_send_buffer,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};