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
|
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>
#define atomic64 uint64_t
#include "../kernel-module/xt_RTPENGINE.h"
int main() {
int fd = open("/proc/rtpengine/control", O_WRONLY);
assert(fd >= 0);
ssize_t ret = write(fd, "add 0\n", 6);
assert(ret == 6 || (ret == -1 && errno == EEXIST));
close(fd);
fd = open("/proc/rtpengine/0/control", O_RDWR);
assert(fd >= 0);
struct rtpengine_command_init init = { .cmd = REMG_INIT };
init.init = (struct rtpengine_init_info) {
.last_cmd = __REMG_LAST,
.msg_size = {
[REMG_INIT] = sizeof(struct rtpengine_command_init),
[REMG_ADD_TARGET] = sizeof(struct rtpengine_command_add_target),
[REMG_ADD_DESTINATION] = sizeof(struct rtpengine_command_destination),
[REMG_ADD_CALL] = sizeof(struct rtpengine_command_add_call),
[REMG_DEL_CALL] = sizeof(struct rtpengine_command_del_call),
[REMG_ADD_STREAM] = sizeof(struct rtpengine_command_add_stream),
[REMG_DEL_STREAM] = sizeof(struct rtpengine_command_del_stream),
[REMG_PACKET] = sizeof(struct rtpengine_command_packet),
[REMG_INIT_PLAY_STREAMS] = sizeof(struct rtpengine_command_init_play_streams),
[REMG_GET_PACKET_STREAM] = sizeof(struct rtpengine_command_get_packet_stream),
[REMG_PLAY_STREAM_PACKET] = sizeof(struct rtpengine_command_play_stream_packet),
[REMG_PLAY_STREAM] = sizeof(struct rtpengine_command_play_stream),
[REMG_STOP_STREAM] = sizeof(struct rtpengine_command_stop_stream),
[REMG_FREE_PACKET_STREAM] = sizeof(struct rtpengine_command_free_packet_stream),
},
};
ret = write(fd, &init, sizeof(init));
assert(ret == sizeof(init));
struct rtpengine_command_init_play_streams ips = {
.cmd = REMG_INIT_PLAY_STREAMS,
.num_packet_streams = 100,
.num_play_streams = 1000,
};
ret = write(fd, &ips, sizeof(ips));
assert(ret == sizeof(ips));
struct rtpengine_command_get_packet_stream gps = { .cmd = REMG_GET_PACKET_STREAM };
ret = read(fd, &gps, sizeof(gps));
assert(ret == sizeof(gps));
printf("packet stream idx %u\n", gps.packet_stream_idx);
struct {
struct rtpengine_command_play_stream_packet psp;
char buf[160];
} psp = {
.psp = {
.cmd = REMG_PLAY_STREAM_PACKET,
.play_stream_packet = {
.packet_stream_idx = gps.packet_stream_idx,
},
},
};
for (unsigned int i = 0; i < 256; i++) {
psp.psp.play_stream_packet.delay_ms = i * 20;
psp.psp.play_stream_packet.delay_ts = i * 160;
memset(psp.psp.play_stream_packet.data, i, sizeof(psp.buf));
ret = write(fd, &psp, sizeof(psp));
assert(ret == sizeof(psp));
}
printf("packets ok\n");
struct rtpengine_command_play_stream ps = {
.cmd = REMG_PLAY_STREAM,
.info = {
.src_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.102"),
},
.port = 6666,
},
.dst_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.66"),
},
.port = 9999,
},
.pt = 8,
.ssrc = 0x12345678,
.ts = 76543210,
.seq = 5432,
.encrypt = {
.cipher = REC_NULL,
.hmac = REH_NULL,
},
.packet_stream_idx = 999999,
},
};
ret = read(fd, &ps, sizeof(ps));
assert(ret == -1 && errno == ERANGE);
ps = (__typeof(ps)) {
.cmd = REMG_PLAY_STREAM,
.info = {
.src_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.102"),
},
.port = 6666,
},
.dst_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.66"),
},
.port = 9999,
},
.pt = 8,
.ssrc = 0x12345678,
.ts = 76543210,
.seq = 5432,
.encrypt = {
.cipher = REC_NULL,
.hmac = REH_NULL,
},
.packet_stream_idx = gps.packet_stream_idx + 1,
},
};
ret = read(fd, &ps, sizeof(ps));
assert(ret == -1 && errno == ENOENT);
ps = (__typeof(ps)) {
.cmd = REMG_PLAY_STREAM,
.info = {
.src_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.102"),
},
.port = 6666,
},
.dst_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.66"),
},
.port = 9999,
},
.pt = 8,
.ssrc = 0x12345678,
.ts = 76543210,
.seq = 5432,
.encrypt = {
.cipher = REC_NULL,
.hmac = REH_NULL,
},
.packet_stream_idx = gps.packet_stream_idx,
.repeat = 3,
.remove_at_end = true,
},
};
ret = read(fd, &ps, sizeof(ps));
assert(ret == sizeof(ps));
printf("play stream idx %u\n", ps.play_idx);
struct rtpengine_command_free_packet_stream fps = {
.cmd = REMG_FREE_PACKET_STREAM,
.packet_stream_idx = 9999999,
};
ret = write(fd, &fps, sizeof(fps));
assert(ret == -1 && errno == ERANGE);
printf("ok\n");
fps = (__typeof(fps)) {
.cmd = REMG_FREE_PACKET_STREAM,
.packet_stream_idx = gps.packet_stream_idx + 1,
};
ret = write(fd, &fps, sizeof(fps));
assert(ret == -1 && errno == ENOENT);
printf("ok\n");
// test: remove while in use
// fps = (__typeof(fps)) {
// .cmd = REMG_FREE_PACKET_STREAM,
// .packet_stream_idx = gps.packet_stream_idx,
// };
// ret = write(fd, &fps, sizeof(fps));
// assert(ret == sizeof(fps));
// printf("ok\n");
printf("sleep\n");
sleep(20);
struct rtpengine_command_stop_stream ss = {
.cmd = REMG_STOP_STREAM,
.play_idx = ps.play_idx,
};
ret = read(fd, &ss, sizeof(ss));
assert(ret == -1 && errno == ENOENT);
ps = (__typeof(ps)) {
.cmd = REMG_PLAY_STREAM,
.info = {
.src_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.102"),
},
.port = 6666,
},
.dst_addr = {
.family = AF_INET,
.u = {
.ipv4 = inet_addr("192.168.1.66"),
},
.port = 9999,
},
.pt = 8,
.ssrc = 0x12345678,
.ts = 76543210,
.seq = 5432,
.encrypt = {
.cipher = REC_NULL,
.hmac = REH_NULL,
},
.packet_stream_idx = gps.packet_stream_idx,
},
};
ret = read(fd, &ps, sizeof(ps));
assert(ret == sizeof(ps));
printf("play stream idx %u\n", ps.play_idx);
printf("sleep\n");
sleep(2);
// test: remove while in use
// fps = (__typeof(fps)) {
// .cmd = REMG_FREE_PACKET_STREAM,
// .packet_stream_idx = gps.packet_stream_idx,
// };
// ret = write(fd, &fps, sizeof(fps));
// assert(ret == -1 && errno == EBUSY);
ss = (__typeof(ss)) {
.cmd = REMG_STOP_STREAM,
.play_idx = 999999,
};
ret = read(fd, &ss, sizeof(ss));
assert(ret == -1 && errno == ERANGE);
ss = (__typeof(ss)) {
.cmd = REMG_STOP_STREAM,
.play_idx = ps.play_idx + 1,
};
ret = read(fd, &ss, sizeof(ss));
assert(ret == -1 && errno == ENOENT);
ss = (__typeof(ss)) {
.cmd = REMG_STOP_STREAM,
.play_idx = ps.play_idx,
};
ret = read(fd, &ss, sizeof(ss));
assert(ret == sizeof(ss));
printf("stop ok\n");
ss = (__typeof(ss)) {
.cmd = REMG_STOP_STREAM,
.play_idx = ps.play_idx,
};
ret = read(fd, &ss, sizeof(ss));
assert(ret == -1 && errno == ENOENT);
fps = (__typeof(fps)) {
.cmd = REMG_FREE_PACKET_STREAM,
.packet_stream_idx = gps.packet_stream_idx,
};
ret = write(fd, &fps, sizeof(fps));
printf("%zi %s\n", ret, strerror(errno));
assert(ret == sizeof(fps));
printf("free ok\n");
sleep(3);
fps = (__typeof(fps)) {
.cmd = REMG_FREE_PACKET_STREAM,
.packet_stream_idx = gps.packet_stream_idx,
};
ret = write(fd, &fps, sizeof(fps));
assert(ret == -1 && errno == ENOENT);
sleep(3);
printf("closing fd\n");
close(fd);
sleep(3);
fd = open("/proc/rtpengine/control", O_WRONLY);
assert(fd >= 0);
ret = write(fd, "del 0\n", 6);
assert(ret == 6);
close(fd);
return 0;
}
|