-
Notifications
You must be signed in to change notification settings - Fork 14
/
rtsp_server.c
471 lines (432 loc) · 14.5 KB
/
rtsp_server.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "common.h"
#include "session.h"
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUF_MAX_SIZE (1024 * 1024)
char *mp4Dir = "mp4path/\0"; // MP4文件存放位置
int auth = 1;
#define USER "admin"
#define PASSWORD "123456"
/*doClientThd线程参数*/
struct thd_arg_st
{
int client_sock_fd;
char client_ip[30];
int client_port;
};
int create_rtp_sockets(int *fd1, int *fd2, int *port1, int *port2)
{
struct sockaddr_in addr;
int port = 0;
*fd1 = socket(AF_INET, SOCK_DGRAM, 0);
if (*fd1 < 0)
{
perror("socket");
return -1;
}
*fd2 = socket(AF_INET, SOCK_DGRAM, 0);
if (*fd2 < 0)
{
perror("socket");
close(*fd1);
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
for (port = 1024; port <= 65535; port += 2)
{
addr.sin_port = htons(port);
if (bind(*fd1, (struct sockaddr *)&addr, sizeof(addr)) == 0)
{
addr.sin_port = htons(port + 1);
if (bind(*fd2, (struct sockaddr *)&addr, sizeof(addr)) == 0)
{
*port1 = port;
*port2 = port + 1;
return 0;
}
close(*fd1);
}
}
close(*fd1);
close(*fd2);
return -1;
}
static void generate_session_id(char *session_id, size_t size) {
if (size < 9) {
return;
}
time_t timestamp = time(NULL);
srand((unsigned int)timestamp);
int random_part = rand() % 1000000;
snprintf(session_id, size, "%02ld%06d", timestamp % 100, random_part);
return;
}
/*处理客户端rtsp请求*/
void *doClientThd(void *arg)
{
signal(SIGINT, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGKILL, sig_handler);
struct thd_arg_st *arg_thd = (struct thd_arg_st *)arg;
int client_sock_fd = arg_thd->client_sock_fd;
char *client_ip = arg_thd->client_ip;
int client_port = arg_thd->client_port;
char method[40];
char url[100];
char url_tmp[100];
char filename[100];
char url_setup[100];
char track[1024];
char url_play[1024];
char local_ip[40];
char version[40];
int cseq;
char *buf_ptr;
char *buf_tmp;
char *recv_buf = malloc(BUF_MAX_SIZE);
char *send_buf = malloc(BUF_MAX_SIZE);
char line[400];
// rtp_over_tcp
int sig_0 = -1;
int sig_1 = -1;
int sig_2 = -1;
int sig_3 = -1;
int ture_of_rtp_tcp = 0;
// rtp_over_udp
int client_rtp_port = -1;
int client_rtcp_port = -1;
int client_rtp_port_1 = -1;
int client_rtcp_port_1 = -1;
int server_rtp_port = -1;
int server_rtcp_port = -1;
int server_rtp_port_1 = -1;
int server_rtcp_port_1 = -1;
int server_udp_socket_rtp_fd = -1;
int server_udp_socket_rtcp_fd = -1;
int server_udp_socket_rtp_1_fd = -1;
int server_udp_socket_rtcp_1_fd = -1;
char ch = '/';
int findflag = 0;
char path[100];
memcpy(path, mp4Dir, strlen(mp4Dir));
path[strlen(mp4Dir)] = '\0';
char path_tmp[100];
memcpy(path_tmp, mp4Dir, strlen(mp4Dir));
path_tmp[strlen(mp4Dir)] = '\0';
int fd;
char *realm = "simple-rtsp-server";
char nonce[33] = {0};
generate_nonce(nonce, sizeof(nonce));
char session_id[512];
generate_session_id(session_id, sizeof(session_id));
while (1)
{
int recv_len;
recv_len = recv(client_sock_fd, recv_buf, BUF_MAX_SIZE, 0);
if (recv_len <= 0)
goto out;
recv_buf[recv_len] = '\0';
printf("---------------C->S--------------\n");
printf("%s", recv_buf);
buf_ptr = getLineFromBuf(recv_buf, line);
buf_tmp = buf_ptr;
if (sscanf(line, "%s %s %s\r\n", method, url, version) != 3)
{
printf("parse err\n");
goto out;
}
/*解析序列号*/
while (1)
{
buf_ptr = getLineFromBuf(buf_ptr, line);
if (!strncmp(line, "CSeq:", strlen("CSeq:")))
{
if (sscanf(line, "CSeq: %d\r\n", &cseq) != 1)
{
printf("parse err\n");
goto out;
}
break;
}
}
if(auth == 1){
// authorization
if(!strcmp(method, "SETUP") || !strcmp(method, "DESCRIBE") || !strcmp(method, "PLAY")){
AuthorizationInfo *auth_info = find_authorization(recv_buf);
if(auth_info == NULL){
handleCmd_Unauthorized(send_buf, cseq, realm, nonce);
printf("---------------S->C--------------\n");
printf("%s", send_buf);
send(client_sock_fd, send_buf, strlen(send_buf), 0);
continue;
}
else{
// printf("nonce:%s\n", auth_info->nonce);
// printf("realm:%s\n", auth_info->realm);
// printf("response:%s\n", auth_info->response);
// printf("uri:%s\n", auth_info->uri);
// printf("username:%s\n", auth_info->username);
// 鉴权校验
int ret = authorization_verify(USER, PASSWORD, realm, nonce, auth_info->uri, method, auth_info->response);
free_authorization_info(auth_info);
if(ret < 0){ // 鉴权失败
goto out;
}
}
}
}
/* 如果是SETUP,需要解析是RTP_OVER_TCP还是RTP_OVER_UDP模式 */
if (!strcmp(method, "SETUP"))
{
memset(url_setup, 0, sizeof(url_setup));
memset(track, 0, sizeof(track));
strcpy(url_setup, url);
char *p = strrchr(url_setup, ch);
memcpy(track, p + 1, strlen(p)); // video-track0 audio -track1
while (1)
{
buf_tmp = getLineFromBuf(buf_tmp, line);
if (!buf_tmp)
{
break;
}
if (!strncmp(line, "Transport: RTP/AVP/TCP", strlen("Transport: RTP/AVP/TCP")))
{
if (memcmp(track, "track0", 6) == 0)
{
sscanf(line, "Transport: RTP/AVP/TCP;unicast;interleaved=%d-%d\r\n", &sig_0, &sig_1);
}
else
{
sscanf(line, "Transport: RTP/AVP/TCP;unicast;interleaved=%d-%d\r\n", &sig_2, &sig_3);
}
ture_of_rtp_tcp = 1;
break;
}
if (!strncmp(line, "Transport: RTP/AVP/UDP", strlen("Transport: RTP/AVP/UDP")))
{
if (memcmp(track, "track0", 6) == 0)
{
sscanf(line, "Transport: RTP/AVP/UDP;unicast;client_port=%d-%d\r\n", &client_rtp_port, &client_rtcp_port);
}
else
{
sscanf(line, "Transport: RTP/AVP/UDP;unicast;client_port=%d-%d\r\n", &client_rtp_port_1, &client_rtcp_port_1);
}
break;
}
if (!strncmp(line, "Transport: RTP/AVP", strlen("Transport: RTP/AVP")))
{
if (memcmp(track, "track0", 6) == 0)
{
sscanf(line, "Transport: RTP/AVP;unicast;client_port=%d-%d\r\n", &client_rtp_port, &client_rtcp_port);
}
else
{
sscanf(line, "Transport: RTP/AVP;unicast;client_port=%d-%d\r\n", &client_rtp_port_1, &client_rtcp_port_1);
}
break;
}
}
}
if (!strcmp(method, "OPTIONS"))
{
char *p = strrchr(url, ch);
memcpy(filename, p + 1, strlen(p));
char *tmp = strcat(path_tmp, filename);
findflag = 1;
fd = open(tmp, O_RDONLY);
if (fd < 0) // 请求的资源不存在返回404并关闭客户端文件描述符
{
perror("failed");
handleCmd_404(send_buf, cseq);
send(client_sock_fd, send_buf, strlen(send_buf), 0);
goto out;
}
else
{
close(fd);
if (handleCmd_OPTIONS(send_buf, cseq))
{
printf("failed to handle options\n");
goto out;
}
}
}
else if (!strcmp(method, "DESCRIBE"))
{
if (findflag == 0)
{
char *p = strrchr(url, ch);
memcpy(filename, p + 1, strlen(p));
char *tmp = strcat(path_tmp, filename);
fd = open(tmp, O_RDONLY);
if (fd < 0) // 请求的资源不存在返回404并关闭客户端文件描述符
{
perror("failed");
handleCmd_404(send_buf, cseq);
send(client_sock_fd, send_buf, strlen(send_buf), 0);
goto out;
}
close(fd);
findflag = 1;
}
char sdp[1024];
char localIp[100];
sscanf(url, "rtsp://%[^:]:", localIp);
int ret = generateSDP(path_tmp, localIp, sdp, sizeof(sdp));
if (ret < 0)
{ // mp4文件有问题,或者视频不是H264/H265,音频不是AAC/PCMA
handleCmd_500(send_buf, cseq);
send(client_sock_fd, send_buf, strlen(send_buf), 0);
goto out;
}
if (handleCmd_DESCRIBE(send_buf, cseq, url, sdp))
{
printf("failed to handle describe\n");
goto out;
}
}
else if (!strcmp(method, "SETUP") && ture_of_rtp_tcp == 0) // RTP_OVER_UDP
{
sscanf(url, "rtsp://%[^:]:", local_ip);
if (memcmp(track, "track0", 6) == 0)
{
create_rtp_sockets(&server_udp_socket_rtp_fd, &server_udp_socket_rtcp_fd, &server_rtp_port, &server_rtp_port);
handleCmd_SETUP_UDP(send_buf, cseq, client_rtp_port, server_rtp_port, session_id);
}
else
{
create_rtp_sockets(&server_udp_socket_rtp_1_fd, &server_udp_socket_rtcp_1_fd, &server_rtp_port_1, &server_rtp_port_1);
handleCmd_SETUP_UDP(send_buf, cseq, client_rtp_port_1, server_rtp_port_1, session_id);
}
}
else if (!strcmp(method, "SETUP") && ture_of_rtp_tcp == 1) // RTP_OVER_TCP
{
sscanf(url, "rtsp://%[^:]:", local_ip);
if (memcmp(track, "track0", 6) == 0)
{
handleCmd_SETUP_TCP(send_buf, cseq, local_ip, client_ip, sig_0, session_id);
}
else
{
handleCmd_SETUP_TCP(send_buf, cseq, local_ip, client_ip, sig_2, session_id);
}
}
else if (!strcmp(method, "PLAY"))
{
memset(url_play, 0, sizeof(url_play));
memset(track, 0, sizeof(track));
strcpy(url_play, url);
if (handleCmd_PLAY(send_buf, cseq, url_play, session_id))
{
printf("failed to handle play\n");
goto out;
}
}
else
{
goto out;
}
printf("---------------S->C--------------\n");
printf("%s", send_buf);
send(client_sock_fd, send_buf, strlen(send_buf), 0);
if (!strcmp(method, "PLAY"))
{
struct timeval time_pre, time_now;
gettimeofday(&time_pre, NULL);
char *tmp = strcat(path, filename);
int ret = addClient(tmp, client_sock_fd, sig_0, sig_2, ture_of_rtp_tcp, client_ip, client_rtp_port, client_rtp_port_1,
server_udp_socket_rtp_fd, server_udp_socket_rtcp_fd, server_udp_socket_rtp_1_fd, server_udp_socket_rtcp_1_fd);
if (ret < 0)
goto out;
int sum = getClientNum();
gettimeofday(&time_now, NULL);
int time_handle = 1000 * (time_now.tv_sec - time_pre.tv_sec) + (time_now.tv_usec - time_pre.tv_usec) / 1000;
printf("timeuse:%dms sum_client:%d\n\n", time_handle, sum);
goto over;
}
}
out:
close(client_sock_fd);
free(recv_buf);
free(send_buf);
free(arg);
return NULL;
over:
free(recv_buf);
free(send_buf);
free(arg);
return NULL;
}
int main(int argc, char *argv[])
{
if(argc < 3){
printf("./rtsp_server auth(0-not authentication; 1-authentication) loop(0-not loop 1-loop)\n");
return -1;
}
auth = atoi(argv[1]);
reloop_flag = atoi(argv[2]);
int server_sock_fd;
int ret;
signal(SIGINT, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGFPE, SIG_IGN);
server_sock_fd = createTcpSocket();
if (server_sock_fd < 0)
{
printf("failed to create tcp socket\n");
return -1;
}
ret = bindSocketAddr(server_sock_fd, SERVER_IP, SERVER_PORT);
if (ret < 0)
{
printf("failed to bind addr\n");
return -1;
}
ret = listen(server_sock_fd, 100);
if (ret < 0)
{
printf("failed to listen\n");
return -1;
}
moduleInit();
printf("rtsp://%s:%d/filename\n", SERVER_IP, SERVER_PORT);
while (1)
{
int client_sock_fd;
char client_ip[40];
int client_port;
pthread_t tid;
client_sock_fd = acceptClient(server_sock_fd, client_ip, &client_port);
if (client_sock_fd < 0)
{
printf("failed to accept client\n");
return -1;
}
printf("###########accept client --> client_sock_fd:%d client ip:%s,client port:%d###########\n", client_sock_fd, client_ip, client_port);
struct thd_arg_st *arg;
arg = malloc(sizeof(struct thd_arg_st));
memcpy(arg->client_ip, client_ip, strlen(client_ip));
arg->client_port = client_port;
arg->client_sock_fd = client_sock_fd;
ret = pthread_create(&tid, NULL, doClientThd, (void *)arg);
if (ret < 0)
{
perror("doClientThd pthread_create()");
}
pthread_detach(tid);
}
moduleDel();
close(server_sock_fd);
return 0;
}