-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtsp_server.c
407 lines (351 loc) · 11.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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <libavformat/avformat.h>
#include <libavutil/avstring.h>
#include "camera.h"
#include "event_loop.h"
#include "file_reader.h"
#include "screen.h"
#include "string_utils.h"
#define SERVER_PORT 8554
enum RTSPStatusCode {
RTSP_STATUS_OK =200, /**< OK */
RTSP_STATUS_METHOD =405, /**< Method Not Allowed */
RTSP_STATUS_BANDWIDTH =453, /**< Not Enough Bandwidth */
RTSP_STATUS_SESSION =454, /**< Session Not Found */
RTSP_STATUS_STATE =455, /**< Method Not Valid in This State */
RTSP_STATUS_AGGREGATE =459, /**< Aggregate operation not allowed */
RTSP_STATUS_ONLY_AGGREGATE =460, /**< Only aggregate operation allowed */
RTSP_STATUS_TRANSPORT =461, /**< Unsupported transport */
RTSP_STATUS_INTERNAL =500, /**< Internal Server Error */
RTSP_STATUS_SERVICE =503, /**< Service Unavailable */
RTSP_STATUS_VERSION =505, /**< RTSP Version not supported */
};
enum RTSPMethod {
DESCRIBE,
ANNOUNCE,
OPTIONS,
SETUP,
PLAY,
PAUSE,
TEARDOWN,
GET_PARAMETER,
SET_PARAMETER,
REDIRECT,
RECORD,
UNKNOWN = -1,
};
struct rtsp_session {
char *session_id;
struct screen *screen;
struct client *client;
};
extern int rtsp_server_fd;
extern l1 *screens;
extern pthread_mutex_t screens_lock;
static int rtsp_session_find_func(void *value, void *arg);
static void rtsp_reply_header(struct client *client, int CSeq, enum RTSPStatusCode error_number);
static void rtsp_reply_error(struct client *client, int CSeq, enum RTSPStatusCode error_number);
void rtsp_remove_session_by_client(struct client *client);
static l1 *sessions = NULL;
static pthread_mutex_t sessions_lock;
void rtsp_server_start() {
int socket_fd, flags, on = 1;
struct sockaddr_in address;
int address_length = sizeof(struct sockaddr_in);
memset(&address, 0, address_length);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(SERVER_PORT);
if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
error("socket");
if(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
error("setsockopt");
if((flags = fcntl(socket_fd, F_GETFL, 0)) < 0)
error("fcntl");
if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) < 0)
error("fcntl");
if(bind(socket_fd, (struct sockaddr *)&address, address_length) < 0)
error("bind");
if(listen(socket_fd, 5) < 0)
error("listen");
rtsp_server_fd = socket_fd;
}
void parse_rtsp_request(struct client* client) {
const char *p = client->buffer, *p1, *p2;
char cmd[32];
char url[256];
char path1[128];
char protocol[32];
char screen_id[16];
char session_id[64];
char transport_protocol[16];
char lower_transport[16] = "UDP";
char profile[16];
char parameter[16];
char line[1024];
const char *l;
char *path = path1;
int client_port_min=0, client_port_max=0;
int method, CSeq = 0;
int ret;
int len;
int rcvd = strlen(client->buffer);
char out[128];
char *sdp = NULL;
struct rtsp_session *session = NULL;
struct screen *screen = NULL;
//printf("------->\n");
//printf("%s", client->buffer);
//printf("<-------\n");
get_word(cmd, sizeof(cmd), &p);
get_word(url, sizeof(url), &p);
get_word(protocol, sizeof(protocol), &p);
if (strcmp(protocol, "RTSP/1.0") != 0)
rtsp_reply_error(client, CSeq, RTSP_STATUS_VERSION);
session_id[0] = '\0';
transport_protocol[0] = '\0';
profile[0] = '\0';
/* parse each header line */
/* skip to next line */
while (*p != '\n' && *p != '\0') p++;
if (*p == '\n') p++;
while (*p != '\0') {
p1 = memchr(p, '\n', rcvd - (p - client->buffer));
if (!p1)
break;
p2 = p1;
if (p2 > p && p2[-1] == '\r')
p2--;
/* skip empty line */
if (p2 == p) {
p = p1 + 1;
continue;
}
len = p2 - p;
if (len > sizeof(line) - 1)
len = sizeof(line) - 1;
memcpy(line, p, len);
line[len] = '\0';
l = (const char *)line;
//printf("Parse header line: <%s>\n", line);
if(av_stristart(l, "Session:", &l)) {
get_word_sep(session_id, sizeof(session_id), ";", &l);
} else if (av_stristart(l, "Transport:", &l)) {
// parse first transport
while(av_isspace(*l) && *l != '\0') l++;
if(*l != '\0') {
get_word_sep(transport_protocol, sizeof(transport_protocol), "/", &l);
get_word_sep(profile, sizeof(profile), "/;,", &l);
if(*l == '/')
get_word_sep(lower_transport, sizeof(lower_transport), ";,", &l);
if(*l == ';')
l++;
while(*l != '\0' && *l != ',') {
get_word_sep(parameter, sizeof(parameter), "=;,", &l);
if(!strcmp(parameter, "client_port")) {
if(*l == '=') {
l++;
parse_range(&client_port_min, &client_port_max, &l);
}
}
while(*l != ';' && *l != '\0' && *l != ',') l++;
if(*l == ';') l++;
}
}
} else if (av_stristart(l, "CSeq:", &l)) {
CSeq = strtol(l, NULL, 10);
}
p = p1 + 1;
}
screen_id[0] = '\0';
av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path, sizeof(path1), url);
if(*path == '/') {
path++;
get_word_sep(screen_id, sizeof(screen_id), "/?", (const char **)&path);
}
printf("url: <%s>, path: <%s>, screen_id: <%s>\n", url, path1, screen_id);
printf("transport_protocol: <%s>, profile: <%s>, lower_transport: <%s> client_port: %d-%d\n",
transport_protocol, profile, lower_transport, client_port_min, client_port_max);
if(screen_id[0] != '\0') {
screen = (struct screen *) l1_find(&screens, &screens_lock, &screen_find_func, screen_id);
if(screen == NULL) {
printf("Screen not found\n");
rtsp_reply_error(client, CSeq, RTSP_STATUS_SERVICE);
return;
}
}
if(session_id[0] != '\0') {
session = (struct rtsp_session *) l1_find(&sessions, &sessions_lock, &rtsp_session_find_func, session_id);
if(session == NULL)
printf("Session <%s> not found\n", session_id);
else
printf("Session <%s> found. Session screen: <%s>\n", session->session_id, session->screen->screen_id);
}
if (!strcmp(cmd, "DESCRIBE"))
method = DESCRIBE;
else if (!strcmp(cmd, "OPTIONS"))
method = OPTIONS;
else if (!strcmp(cmd, "SETUP"))
method = SETUP;
else if (!strcmp(cmd, "PLAY"))
method = PLAY;
else if (!strcmp(cmd, "PAUSE"))
method = PAUSE;
else if (!strcmp(cmd, "TEARDOWN"))
method = TEARDOWN;
else {
rtsp_reply_error(client, CSeq, RTSP_STATUS_METHOD);
return;
}
switch(method) {
case OPTIONS:
rtsp_reply_header(client, CSeq, RTSP_STATUS_OK);
snd(client, "Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\r\n");
snd(client, "\r\n");
break;
case DESCRIBE:
rtsp_reply_header(client, CSeq, RTSP_STATUS_OK);
sprintf(out, "Content-Base: %s\r\n", url);
snd(client, out);
snd(client, "Content-Type: application/sdp\r\n");
sdp = screen_create_sdp(screen);
sprintf(out, "Content-Length: %d\r\n", strlen(sdp));
snd(client, out);
snd(client, "\r\n");
snd(client, sdp);
free(sdp);
break;
case SETUP:
/*
if(av_strcasecmp(transport_protocol, "RTP") != 0 ||
av_strcasecmp(lower_transport, "UDP") != 0 ) {
rtsp_reply_error(client, CSeq, RTSP_STATUS_TRANSPORT);
return;
}
*/
sprintf(screen->rtp_context->filename, "rtp://%s:%d", inet_ntoa(client->from_addr.sin_addr), client_port_min);
printf("Streaming to <%s>\n", screen->rtp_context->filename);
if((ret = avio_open(&screen->rtp_context->pb, screen->rtp_context->filename, AVIO_FLAG_WRITE)) < 0) {
avformat_free_context(screen->rtp_context);
av_err_msg("avio_open", ret);
rtsp_reply_error(client, CSeq, RTSP_STATUS_SESSION);
return;
}
session = (struct rtsp_session *)malloc(sizeof(struct rtsp_session));
session->session_id = random_string(8);
session->screen = screen;
session->client = client;
l1_insert(&sessions, &sessions_lock, session);
rtsp_reply_header(client, CSeq, RTSP_STATUS_OK);
sprintf(out, "Session: %s\r\n", session->session_id);
snd(client, out);
sprintf(out, "Transport: RTP/AVP/UDP;unicast;client_port=%d-%d\r\n", client_port_min, client_port_max);
snd(client, out);
snd(client, "\r\n");
break;
case PLAY:
if(session == NULL || session->screen != screen) {
rtsp_reply_error(client, CSeq, RTSP_STATUS_SESSION);
return;
}
if((ret = avformat_write_header(screen->rtp_context, NULL)) < 0) {
avformat_free_context(screen->rtp_context);
av_err_msg("avformat_write_header", ret);
return;
}
screen->active = 1;
if(screen->type == ARCHIVE) {
if(pthread_create(&screen->worker_thread, NULL, copy_input_to_output, screen->io) < 0)
error("pthread_create");
} else if(screen->tmpl_size > 1) {
if(pthread_create(&screen->worker_thread, NULL, multiple_cameras_thread, screen) < 0)
error("pthread_create");
}
rtsp_reply_header(client, CSeq, RTSP_STATUS_OK);
snd(client, "\r\n");
break;
case TEARDOWN:
rtsp_remove_session_by_client(client);
rtsp_reply_header(client, CSeq, RTSP_STATUS_OK);
snd(client, "\r\n");
break;
}
}
static int rtsp_session_find_func(void *value, void *arg) {
struct rtsp_session *session = (struct rtsp_session *)value;
char* session_id = (char *)arg;
if(strcmp(session->session_id, session_id) == 0)
return 1;
else
return 0;
}
static void rtsp_reply_header(struct client *client, int CSeq, enum RTSPStatusCode error_number) {
const char *str;
time_t ti;
struct tm *tm;
char out[128];
char buf2[32];
switch(error_number) {
case RTSP_STATUS_OK:
str = "OK";
break;
case RTSP_STATUS_METHOD:
str = "Method Not Allowed";
break;
case RTSP_STATUS_SESSION:
str = "Session Not Found";
break;
case RTSP_STATUS_STATE:
str = "Method Not Valid in This State";
break;
case RTSP_STATUS_TRANSPORT:
str = "Unsupported transport";
break;
case RTSP_STATUS_INTERNAL:
str = "Internal Server Error";
break;
case RTSP_STATUS_VERSION:
str = "RTSP Version not supported";
break;
default:
str = "Unknown Error";
break;
}
sprintf(out, "RTSP/1.0 %d %s\r\n", error_number, str);
snd(client, out);
sprintf(out, "CSeq: %d\r\n", CSeq);
snd(client, out);
/* output GMT time */
ti = time(NULL);
tm = gmtime(&ti);
strftime(buf2, sizeof(buf2), "%a, %d %b %Y %H:%M:%S", tm);
sprintf(out, "Date: %s GMT\r\n", buf2);
snd(client, out);
}
static void rtsp_reply_error(struct client *client, int CSeq, enum RTSPStatusCode error_number) {
rtsp_reply_header(client, CSeq, error_number);
snd(client, "\r\n");
}
static int session_filter_func(void *value, void *arg) {
struct rtsp_session *session = (struct rtsp_session *)value;
if(session->client == (struct client *)arg) {
printf("removing session: %s\n", session->session_id);
if(session->screen != NULL)
printf("removing screen ncams: %d\n", session->screen->ncams);
screen_remove(session->screen);
return 0;
}
return 1;
}
void rtsp_remove_session_by_client(struct client *client) {
printf("rtsp_remove_session_by_client\n");
l1_filter(&sessions, &sessions_lock, &session_filter_func, client);
}