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
|
#include <stdio.h>
#include <stdlib.h>
#include "stellar/stellar.h"
#include "stellar/module_manager.h"
#include "stellar/session.h"
#include "stellar/utils.h"
#include "stellar/rtp.h"
#include <gtest/gtest.h>
#include "cjson/cJSON.h"
#define RTP_TEST_MODULE_NAME "RTP_TEST_MODULE"
#define RTP_TEST_RESULT_EXPECT_ENV "RTP_TEST_RESULT_EXPECT"
#define RTP_TEST_STELLAR_CONFIG_FILE "RTP_TEST_STELLAR_CONFIG"
struct rtp_test_result {
cJSON *test_json;
cJSON *expect_json;
int count;
};
struct rtp_test_module_ctx {
struct rtp_test_result *result;
// depends a single session pcap
int rtp_count;
};
static void rtp_test_result_add(struct rtp_test_result *result, cJSON *json)
{
cJSON_AddItemToArray(result->test_json, json);
}
static void rtp_test_result_compare(struct rtp_test_result *result)
{
EXPECT_TRUE(result->expect_json != NULL && result->test_json != NULL);
int i;
int test_result_count, expect_result_count;
char *test_str, *expect_str;
cJSON *tmp_test, *tmp_expect;
/*
expect_str = cJSON_Print(result->expect_json);
test_str = cJSON_Print(result->test_json);
printf("LOAD Raw:\n%s\n", expect_str);
printf("TEST Raw:\n%s\n", test_str);
*/
test_result_count = cJSON_GetArraySize(result->test_json);
expect_result_count = cJSON_GetArraySize(result->expect_json);
EXPECT_EQ(test_result_count, expect_result_count);
for (i = 0; i < MIN(test_result_count, expect_result_count); i++) {
tmp_test = cJSON_GetArrayItem(result->test_json, i);
tmp_expect = cJSON_GetArrayItem(result->expect_json, i);
expect_str = cJSON_Print(tmp_expect);
test_str = cJSON_Print(tmp_test);
EXPECT_STREQ(expect_str, test_str);
free(expect_str);
free(test_str);
break;
}
}
static void rtp_test_result_exit(struct rtp_test_result *result)
{
if (result->expect_json) {
cJSON_Delete(result->expect_json);
}
if (result->test_json) {
cJSON_Delete(result->test_json);
}
}
static struct rtp_test_result * rtp_test_result_init(void)
{
long filesize;
char *buffer;
FILE *file;
struct rtp_test_result *result;
result = (struct rtp_test_result *)calloc(1, sizeof(struct rtp_test_result));
file = fopen(getenv(RTP_TEST_RESULT_EXPECT_ENV), "rb");
if (file) {
fseek(file, 0, SEEK_END);
filesize = ftell(file);
rewind(file);
buffer = (char *)calloc(filesize + 1, 1);
fread(buffer, 1, filesize, file);
result->expect_json = cJSON_Parse(buffer);
free(buffer);
fclose(file);
}
result->test_json = cJSON_CreateArray();
return result;
}
static const char* rtp_test_rtp_payload_to_string(unsigned char payload_type)
{
switch (payload_type) {
case PT_PCMU:
return "PCMU";
case PT_1016:
return "1016";
case PT_G721:
return "G721";
case PT_GSM:
return "GSM";
case PT_G723:
return "G723";
case PT_DVI4_8000:
return "DVI4_8000";
case PT_DVI4_16000:
return "DVI4_16000";
case PT_LPC:
return "LPC";
case PT_PCMA:
return "PCMA";
case PT_G722:
return "G722";
case PT_L16_STEREO:
return "L16_STEREO";
case PT_L16_MONO:
return "L16_MONO";
case PT_QCELP:
return "QCELP";
case PT_CN:
return "CN";
case PT_MPA:
return "MPA";
case PT_G728:
return "G728";
case PT_DVI4_11025:
return "DVI4_11025";
case PT_DVI4_22050:
return "DVI4_22050";
case PT_G729:
return "G729";
case PT_CN_OLD:
return "CN_OLD";
case PT_CELB:
return "CELB";
case PT_JPEG:
return "JPEG";
case PT_NV:
return "NV";
case PT_H261:
return "H261";
case PT_MPV:
return "MPV";
case PT_MP2T:
return "MP2T";
case PT_H263:
return "H263";
case PT_UNDF_96:
return "UNDF_96";
case PT_UNDF_97:
return "UNDF_97";
case PT_UNDF_98:
return "UNDF_98";
case PT_UNDF_99:
return "UNDF_99";
case PT_UNDF_100:
return "UNDF_100";
case PT_UNDF_101:
return "UNDF_101";
case PT_UNDF_102:
return "UNDF_102";
case PT_UNDF_103:
return "UNDF_103";
case PT_UNDF_104:
return "UNDF_104";
case PT_UNDF_105:
return "UNDF_105";
case PT_UNDF_106:
return "UNDF_106";
case PT_UNDF_107:
return "UNDF_107";
case PT_UNDF_108:
return "UNDF_108";
case PT_UNDF_109:
return "UNDF_109";
case PT_UNDF_110:
return "UNDF_110";
case PT_UNDF_111:
return "UNDF_111";
case PT_UNDF_112:
return "UNDF_112";
case PT_UNDF_113:
return "UNDF_113";
case PT_UNDF_114:
return "UNDF_114";
case PT_UNDF_115:
return "UNDF_115";
case PT_UNDF_116:
return "UNDF_116";
case PT_UNDF_117:
return "UNDF_117";
case PT_UNDF_118:
return "UNDF_118";
case PT_UNDF_119:
return "UNDF_119";
case PT_UNDF_120:
return "UNDF_120";
case PT_UNDF_121:
return "UNDF_121";
case PT_UNDF_122:
return "UNDF_122";
case PT_UNDF_123:
return "UNDF_123";
case PT_UNDF_124:
return "UNDF_124";
case PT_UNDF_125:
return "UNDF_125";
case PT_UNDF_126:
return "UNDF_126";
case PT_UNDF_127:
return "UNDF_127";
default:
return "UNKNOWN";
}
return NULL;
}
static void rtp_test_rtp_callback(struct session *sess, const struct rtp_header *hdr, const char *payload, size_t payload_len, void *arg)
{
(void)(sess);
(void)(payload);
struct rtp_test_module_ctx *mod_ctx = (struct rtp_test_module_ctx *)arg;
cJSON *json = cJSON_CreateObject();
cJSON_AddNumberToObject(json, "CSRC_LEN", hdr->csrc_len);
cJSON_AddNumberToObject(json, "EXTENSION", hdr->extension);
cJSON_AddNumberToObject(json, "PADDING", hdr->padding);
cJSON_AddNumberToObject(json, "VERSION", hdr->version);
cJSON_AddStringToObject(json, "PAYLOAD_TYPE", rtp_test_rtp_payload_to_string(hdr->payload_type));
cJSON_AddNumberToObject(json, "MARKER", hdr->marker);
cJSON_AddNumberToObject(json, "SEQ", ntohs(hdr->seq));
cJSON_AddNumberToObject(json, "TIMESTAMP", ntohl(hdr->timestamp));
cJSON_AddNumberToObject(json, "SSRC", ntohl(hdr->ssrc));
cJSON_AddNumberToObject(json, "PAYLOAD_LEN", payload_len);
cJSON_AddNumberToObject(json, "PAYLOAD_SEQ", mod_ctx->rtp_count++);
rtp_test_result_add(mod_ctx->result, json);
return;
}
extern "C" void rtp_test_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod)
{
struct rtp_test_module_ctx *mod_ctx;
if (mod_mgr && mod) {
mod_ctx = (struct rtp_test_module_ctx *)stellar_module_get_ctx(mod);
if (mod_ctx) {
rtp_test_result_compare(mod_ctx->result);
rtp_test_result_exit(mod_ctx->result);
free(mod_ctx);
}
stellar_module_free(mod);
}
}
extern "C" struct stellar_module *rtp_test_init(struct stellar_module_manager *mod_mgr)
{
int ret;
struct stellar_module *mod;
struct rtp_test_module_ctx *mod_ctx;
struct session_manager *sess_mgr;
mod_ctx = (struct rtp_test_module_ctx *)calloc(1, sizeof(struct rtp_test_module_ctx));
mod = stellar_module_new(RTP_TEST_MODULE_NAME, mod_ctx);
sess_mgr = stellar_module_get_session_manager(mod_mgr);
if (mod_mgr == NULL || sess_mgr == NULL) {
goto exit;
}
mod_ctx->result = rtp_test_result_init();
if (mod_ctx->result == NULL) {
goto exit;
}
ret = rtp_subscribe(mod_mgr, rtp_test_rtp_callback, mod_ctx);
if (ret < 0) {
goto exit;
}
return mod;
exit:
printf("rtp_test module init failed!\n");
rtp_test_exit(mod_mgr, mod);
return NULL;
}
TEST(rtp, rtp_module)
{
struct stellar *st;
setenv(RTP_TEST_RESULT_EXPECT_ENV, "./rtp_result.json", 0);
st = stellar_new(getenv(RTP_TEST_STELLAR_CONFIG_FILE));
stellar_run(st);
stellar_free(st);
}
int main(int argc, char ** argv)
{
setenv(RTP_TEST_STELLAR_CONFIG_FILE, "./stellar.toml", 0);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|