File: kplay-test2.c

package info (click to toggle)
rtpengine 13.5.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,775; perl: 59,422; python: 3,193; sh: 1,037; makefile: 687; asm: 211
file content (166 lines) | stat: -rw-r--r-- 4,802 bytes parent folder | download | duplicates (2)
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
#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>
#include <sys/mman.h>

#define atomic64 uint64_t
#include "../kernel-module/xt_RTPENGINE.h"

#define PAGE_SIZE 4096

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 global_stats_counter *rtpe_stats = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	assert(rtpe_stats != NULL && rtpe_stats != MAP_FAILED);

	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_DEL_TARGET] = sizeof(struct rtpengine_command_del_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),
		},
		.rtpe_stats = rtpe_stats,
	};

	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 = 40960,
	};
	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");

	unsigned play_idx[4096];
	const unsigned int num_plays = sizeof(play_idx)/sizeof(*play_idx);

	struct {
		struct interface_stats_block iface_stats[num_plays];
		struct stream_stats stream_stats[num_plays];
		struct ssrc_stats ssrc_stats[num_plays];
	} *all_stats;
	const unsigned int map_size = PAGE_SIZE * 512;
	assert(sizeof(*all_stats) <= map_size);

	all_stats = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	assert(all_stats != NULL && all_stats != MAP_FAILED);

	for (unsigned int i = 0; i < num_plays; i++) {
		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 + i,
				},
				.dst_addr = {
					.family = AF_INET,
					.u = {
						.ipv4 = inet_addr("192.168.1.66"),
					},
					.port = 9999,
				},
				.pt = 8,
				.ssrc = 0x12345678 + i,
				.ts = 76543210 + i,
				.seq = 5432 + i,
				.encrypt = {
					.cipher = REC_NULL,
					.hmac = REH_NULL,
				},
				.packet_stream_idx = gps.packet_stream_idx,
				.repeat = 50,
				.remove_at_end = false,
				.iface_stats = &all_stats->iface_stats[i],
				.stats = &all_stats->stream_stats[i],
				.ssrc_stats = &all_stats->ssrc_stats[i],
			},
		};
		ret = read(fd, &ps, sizeof(ps));
		assert(ret == sizeof(ps));
		printf("play stream idx %u\n", ps.play_idx);
		play_idx[i] = ps.play_idx;

		usleep(50000);
	}

	printf("sleep\n");
	sleep(350);

	printf("close fd, sleep\n");
	sleep(10);
	close(fd);
	munmap(rtpe_stats, PAGE_SIZE);
	munmap(all_stats, map_size);

	printf("del table\n");
	fd = open("/proc/rtpengine/control", O_WRONLY);
	assert(fd >= 0);
	ret = write(fd, "del 0\n", 6);
	assert(ret == 6);
	close(fd);

	return 0;
}