-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtpgen.c
380 lines (352 loc) · 10.5 KB
/
rtpgen.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//============================================================================
// RTP packet stream generator
//
// Summary:
// This tool sends an MPEG-1 RTP stream to the indicated address.
// Most header bits are zeroed, only Sequence number and timestamp are
// updated.
//
// Compilation for Windows:
// cc -Wall -o rtpgen.exe rtpgen.c -D WIN32 -lwsock32
//
// Compilation for UNIX:
// make (requires included Makefile)
// OR
// cc -Wall -g -o rtpgen -lrt rtpgen.c
//
// Compilation for OS X:
// make osx (requires included Makefile)
// OR
// cc -Wall -g -o rtpgen rtpgen.c
//
// Example usage:
// ./rtpgen -a 127.0.0.1 -p 9999 -r 30
//
// Author: Kevan Ahlquist
//
// License: All Rights Reserved
//
//============================================================================
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef WIN32
# include <windows.h>
# include <Winsock.h>
# include <Winsock2.h>
# include <Ws2tcpip.h>
# else
# include <arpa/inet.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/stat.h>
# include <sys/types.h>
#endif
#ifdef __MACH__
# include <mach/clock.h>
# include <mach/mach.h>
#endif
//============================================================================
char address[16];
int DEBUG;
int servPort;
float sendRate;
#ifdef WIN32
WSADATA wsaData;
SOCKET sock;
#else
int sock;
struct timespec to_sleep = {0, 0};
#endif
// 16-bit RTP header field, Version 2, P: 0, X: 0, CSRC: 0, M: 0, PT: 32 (MPV)
uint16_t RtpHeader = 0x8020;
uint16_t networkRtpHeader;
// 32-bit Mpeg Video-specific header (RFC 2250)
uint32_t mpegVideoHeader = 0x00000000;
uint32_t networkMpegVideoHeader;
uint16_t sequenceNumber; // RTP Sequence number
uint16_t networkSequenceNumber;
uint32_t rtpTimestamp;
uint32_t networkRtpTimestamp;
uint32_t ssrc;
uint64_t unixTimestamp;
uint64_t networkUnixTimestamp;
const char ttl = 64;
unsigned char msgLength = 0x3D;
unsigned char packetBuffer[1500];
char *testMessage = "TEST PAYLOAD";
char payload[1484];
int payloadLength;
int fdin;
struct sockaddr_in servaddr;
//============================================================================
// FUNCTIONS
uint64_t getUnixTimestamp(void);
uint32_t getRtpTimestamp(float rate);
void help(void);
uint64_t htonll(uint64_t num);
void makePacket(unsigned char *buff);
int sysIsBigEndian(void);
int udpInit(void);
int udpSendPacket(const char * packet);
//--------------------------------------------------
// Closes UDP socket before exiting
void exitProgram() {
#ifdef WIN32
closesocket(sock);
WSACleanup();
#else
close(sock);
#endif
//printf("Exiting now...\n");
exit(0);
}
//--------------------------------------------------
// Returns an RTP timestamp based on the current framerate
// Approximates 90,000Hz RTP clock frequency for video
// Normally this value is generated at frame capture time.
// This timestamp is arbitrary.
uint32_t getRtpTimestamp(float rate) {
static uint32_t rtpTime = 0;
rtpTime += (uint32_t)(90000/rate);
return rtpTime;
}
//--------------------------------------------------
// Returns the current Unix timestamp in microseconds
uint64_t getUnixTimestamp(void) {
#ifdef WIN32
SYSTEMTIME st, epochs;
FILETIME ft, epochf;
ULARGE_INTEGER epoch, now;
GetSystemTime(&st);
epochs.wYear = 1970;
epochs.wMonth = 1;
epochs.wDay = 1;
epochs.wHour = 0;
epochs.wMinute = 0;
epochs.wSecond = 0;
epochs.wMilliseconds = 0;
SystemTimeToFileTime(&st, &ft);
SystemTimeToFileTime(&epochs, &epochf);
memcpy(&epoch, &epochf, sizeof(epochf));
memcpy(&now, &ft, sizeof(ft));
if (now.QuadPart > epoch.QuadPart) {
return (uint64_t)((now.QuadPart - epoch.QuadPart) / 10);
}
else return 0;
#elif defined __gnu_linux__
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ((((uint64_t)ts.tv_sec * 1000000)) + (((uint64_t)ts.tv_nsec / 1000)));
#elif (defined __APPLE__) && (defined __MACH__)
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
return (uint64_t)((ts.tv_sec * 10^9) + ts.tv_nsec);
#endif
}
//--------------------------------------------------
// Displays help information for the tool
void help(void) {
printf("Usage: rtpgen ...\n");
printf(" -a or --address <address>\n\tDestination address in dotted quad notation (e.g. 127.0.0.1)\n\tDefault: 127.0.0.1\n");
printf(" -p or --port <port>\n\tThe port to send packets to\n\tDefault: 9000\n");
printf(" -r or --rate <rate>\n\tPackets per second (e.g. rate 30, 30 packets sent per second)\n\tDefault: 1\n");
printf(" -c or --payload <file>\n\tLoad packet payload from file \n\tDefault:\"TEST PAYLOAD\"\n");
}
//--------------------------------------------------
// Convert ints of type uint64_t to network order, checks if conversion is needed.
uint64_t htonll(uint64_t num) {
if (sysIsBigEndian()) return num;
else return (((num & 0xFFULL) << 56) | ((num & 0xFF00000000000000ULL) >> 56) |
((num & 0xFF00ULL) << 40) | ((num & 0x00FF000000000000ULL) >> 40) |
((num & 0xFF0000ULL) << 24) | ((num & 0x0000FF0000000000ULL) >> 24) |
((num & 0xFF000000ULL) << 8) | ((num & 0x000000FF00000000ULL) >> 8));
}
//--------------------------------------------------
// Assemble packet in the given buffer
void makePacket(unsigned char *buff) {
// Copy all fields into the packet buffer
memcpy(&buff[0], &networkRtpHeader, 2);
memcpy(&buff[2], &networkSequenceNumber, 2);
memcpy(&buff[4], &networkRtpTimestamp, 4);
memcpy(&buff[8], &ssrc, 4);
memcpy(&buff[12], &networkMpegVideoHeader, 4);
//Copy Packet payload
memcpy(&buff[16], &payload, payloadLength);
return;
}
//--------------------------------------------------
// Check if the system is big endian or not.
int sysIsBigEndian(void) {
union {
uint32_t i;
char ch[4];
} tmp = {0x01020304};
return tmp.ch[0] == 1;
}
//--------------------------------------------------
// Initialize UDP socket
int udpInit(void) {
//printf("udpInit: servPort: %d\n", servPort);
#ifdef WIN32
int error;
error = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (error != 0) {
perror("Unable to initialize WinSock DLL");
return 1;
}
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
//printf("Unable to create socket.");
perror("Unable to create socket.");
return -1;
}
#else
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Unable to create socket.");
return -1;
}
#endif
// if (setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) != 0) {
// perror("Unable to set TTL for socket");
// }
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(servPort);
//printf("Current servPort: %d, sin_port: %d\n", servPort, servaddr.sin_port);
return 0;
}
//--------------------------------------------------
// Sends the contents of the given packet
int udpSendPacket(const char * packet) {
if (sendto(sock, packet, payloadLength+16, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
//perror("Error sending socket message");
return -1;
}
return 1;
}
//============================================================================
int main(int argc, char *argv[]) {
#ifndef WIN32
signal(SIGINT, exitProgram);
signal(SIGTERM, exitProgram);
signal(SIGHUP, exitProgram);
signal(SIGKILL, exitProgram);
#endif
unixTimestamp = getUnixTimestamp();
networkRtpHeader = htons(RtpHeader);
networkMpegVideoHeader = htonl(mpegVideoHeader);
int i;
// Set Default values
sendRate = 1.0;
strcpy(address, "127.0.0.1");
servPort = 9000;
DEBUG = 0;
srand(time(0));
ssrc = rand();
memcpy(payload, testMessage, strlen(testMessage)+1);
payloadLength = strlen(testMessage)+1;
printf("\nRTP Stream Generator, Version 0.1\nKevan Ahlquist\nAll Rights Reserved\n\n");
// read user options
int option_index = 0;
int optc;
static struct option long_options[] =
{
{"address", required_argument, 0, 'a'},
{"port", required_argument, 0, 'p'},
{"rate", required_argument, 0, 'r'},
{"payload", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
while (( optc = getopt_long(argc, argv, "a:p:r:c:hv", long_options, &option_index)) != -1) {
switch(optc) {
case 'a':
strncpy(address, optarg, 16);
address[15] = '\0'; // Prevent buffer overrun
printf("Address received: %s\n", address);
break;
case 'p':
servPort = atol(optarg);
printf("Port received: %d\n", servPort);
break;
case 'r':
sendRate = atof(optarg);
printf("Rate received: %f\n", sendRate);
if (sendRate > 10000) {
printf("Values greater than 10000 packets per second are not supported\n");
exit(0);
}
break;
case 'c':
// Open file
fdin = open(optarg, O_RDONLY);
if (fdin < 0) {
perror("Error opening payload file");
exit(1);
}
payloadLength = read(fdin, payload, 1484);
if (payloadLength < 0) {
perror("Error reading from payload file");
exit(1);
}
close(fdin);
break;
case 'h':
help();
exit(0);
break;
case 'v':
exit(0);
break;
default:
printf("Usage: udpout -a <address>:<port> -r <rate> -c <payload>\n");
printf("For help use option -h or --help\n");
exit(0);
}
}
if (udpInit() == -1) exit(-1);
while (1) {
networkUnixTimestamp = htonll(getUnixTimestamp());
networkRtpTimestamp = htonl(getRtpTimestamp(sendRate));
networkSequenceNumber = htons(++sequenceNumber); // Sequence number is unique for each packet, while timestamp may remain the same
makePacket(packetBuffer);
udpSendPacket((const char *)packetBuffer);
if (DEBUG) {
printf("Packet contents:\n");
for (i = 0; i < 80; ++i) {
printf("%2X ", packetBuffer[i]);
if ((i == 16) || (i == 26) || (i == 40) || (i == 54) ||
(i == 60) || (i == 66) || (i == 70) || (i == 73)) {
printf("\n");
}
}
}
#ifdef WIN32
Sleep((1 / sendRate) * 1000);
#else
long long int sleep;
sleep = 1000000000 / sendRate;
to_sleep.tv_sec = (time_t) (sleep / 1000000000);
to_sleep.tv_nsec = (long) (sleep % 1000000000);
while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR));
#endif
}
}