18799
|
1 /*
|
|
2 * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * Copyright (C) 2000-2002 the xine project
|
|
7 *
|
|
8 * This file is part of xine, a free video player.
|
|
9 *
|
|
10 * xine is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * xine is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
23 *
|
|
24 *
|
|
25 * a minimalistic implementation of rtsp protocol,
|
|
26 * *not* RFC 2326 compilant yet.
|
|
27 *
|
|
28 * 2006, Benjamin Zores and Vincent Mussard
|
|
29 * fixed a lot of RFC compliance issues.
|
|
30 */
|
|
31
|
|
32 #include <unistd.h>
|
|
33 #include <stdio.h>
|
|
34 #include <assert.h>
|
|
35 #include "config.h"
|
|
36 #ifndef HAVE_WINSOCK2
|
|
37 #define closesocket close
|
|
38 #include <sys/socket.h>
|
|
39 #include <netinet/in.h>
|
|
40 #include <netdb.h>
|
|
41 #else
|
|
42 #include <winsock2.h>
|
|
43 #endif
|
|
44 #include <string.h>
|
|
45 #include <sys/stat.h>
|
|
46 #include <fcntl.h>
|
|
47 #include <errno.h>
|
|
48 #include <stdlib.h>
|
|
49 #include <time.h>
|
|
50 #include <sys/time.h>
|
|
51 #include <sys/types.h>
|
|
52 #include <inttypes.h>
|
|
53
|
|
54 #include "mp_msg.h"
|
|
55 #include "rtsp.h"
|
|
56 #include "rtsp_session.h"
|
|
57 #include "osdep/timer.h"
|
|
58
|
|
59 /*
|
|
60 #define LOG
|
|
61 */
|
|
62
|
|
63 #define BUF_SIZE 4096
|
|
64 #define HEADER_SIZE 1024
|
|
65 #define MAX_FIELDS 256
|
|
66
|
|
67 struct rtsp_s {
|
|
68
|
|
69 int s;
|
|
70
|
|
71 char *host;
|
|
72 int port;
|
|
73 char *path;
|
|
74 char *param;
|
|
75 char *mrl;
|
|
76 char *user_agent;
|
|
77
|
|
78 char *server;
|
|
79 unsigned int server_state;
|
|
80 uint32_t server_caps;
|
|
81
|
|
82 unsigned int cseq;
|
|
83 char *session;
|
|
84
|
|
85 char *answers[MAX_FIELDS]; /* data of last message */
|
|
86 char *scheduled[MAX_FIELDS]; /* will be sent with next message */
|
|
87 };
|
|
88
|
|
89 /*
|
|
90 * constants
|
|
91 */
|
|
92
|
|
93 #define RTSP_PROTOCOL_VERSION "RTSP/1.0"
|
|
94
|
|
95 /* server states */
|
|
96 #define RTSP_CONNECTED 1
|
|
97 #define RTSP_INIT 2
|
|
98 #define RTSP_READY 4
|
|
99 #define RTSP_PLAYING 8
|
|
100 #define RTSP_RECORDING 16
|
|
101
|
|
102 /* server capabilities */
|
|
103 #define RTSP_OPTIONS 0x001
|
|
104 #define RTSP_DESCRIBE 0x002
|
|
105 #define RTSP_ANNOUNCE 0x004
|
|
106 #define RTSP_SETUP 0x008
|
|
107 #define RTSP_GET_PARAMETER 0x010
|
|
108 #define RTSP_SET_PARAMETER 0x020
|
|
109 #define RTSP_TEARDOWN 0x040
|
|
110 #define RTSP_PLAY 0x080
|
|
111 #define RTSP_RECORD 0x100
|
|
112
|
|
113 /*
|
|
114 * network utilities
|
|
115 */
|
|
116
|
|
117 static int host_connect_attempt(struct in_addr ia, int port) {
|
|
118
|
|
119 int s;
|
|
120 struct sockaddr_in sin;
|
|
121
|
|
122 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
123 if (s == -1) {
|
|
124 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: socket(): %s\n", strerror(errno));
|
|
125 return -1;
|
|
126 }
|
|
127
|
|
128 sin.sin_family = AF_INET;
|
|
129 sin.sin_addr = ia;
|
|
130 sin.sin_port = htons(port);
|
|
131
|
|
132 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1
|
|
133 #ifndef HAVE_WINSOCK2
|
|
134 && errno != EINPROGRESS) {
|
|
135 #else
|
|
136 && WSAGetLastError() == WSAEINPROGRESS) {
|
|
137 #endif
|
|
138 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: connect(): %s\n", strerror(errno));
|
|
139 closesocket(s);
|
|
140 return -1;
|
|
141 }
|
|
142
|
|
143 return s;
|
|
144 }
|
|
145
|
|
146 static int host_connect(const char *host, int port) {
|
|
147
|
|
148 struct hostent *h;
|
|
149 int i, s;
|
|
150
|
|
151 h = gethostbyname(host);
|
|
152 if (h == NULL) {
|
|
153 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: unable to resolve '%s'.\n", host);
|
|
154 return -1;
|
|
155 }
|
|
156
|
|
157 for (i = 0; h->h_addr_list[i]; i++) {
|
|
158 struct in_addr ia;
|
|
159
|
|
160 memcpy (&ia, h->h_addr_list[i], 4);
|
|
161 s = host_connect_attempt(ia, port);
|
|
162 if(s != -1)
|
|
163 return s;
|
|
164 }
|
|
165 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: unable to connect to '%s'.\n", host);
|
|
166 return -1;
|
|
167 }
|
|
168
|
|
169 static int write_stream(int s, const char *buf, int len) {
|
|
170 int total, timeout;
|
|
171
|
|
172 total = 0; timeout = 30;
|
|
173 while (total < len){
|
|
174 int n;
|
|
175
|
|
176 n = send (s, &buf[total], len - total, 0);
|
|
177
|
|
178 if (n > 0)
|
|
179 total += n;
|
|
180 else if (n < 0) {
|
|
181 #ifndef HAVE_WINSOCK2
|
|
182 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) {
|
|
183 #else
|
|
184 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) {
|
|
185 #endif
|
|
186 usec_sleep (1000000); timeout--;
|
|
187 } else
|
|
188 return -1;
|
|
189 }
|
|
190 }
|
|
191
|
|
192 return total;
|
|
193 }
|
|
194
|
|
195 static ssize_t read_stream(int fd, void *buf, size_t count) {
|
|
196
|
|
197 ssize_t ret, total;
|
|
198
|
|
199 total = 0;
|
|
200
|
|
201 while (total < count) {
|
|
202
|
|
203 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0);
|
|
204
|
|
205 if (ret<0) {
|
|
206 if(errno == EAGAIN) {
|
|
207 fd_set rset;
|
|
208 struct timeval timeout;
|
|
209
|
|
210 FD_ZERO (&rset);
|
|
211 FD_SET (fd, &rset);
|
|
212
|
|
213 timeout.tv_sec = 30;
|
|
214 timeout.tv_usec = 0;
|
|
215
|
|
216 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) {
|
|
217 return -1;
|
|
218 }
|
|
219 continue;
|
|
220 }
|
|
221
|
|
222 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: read error.\n");
|
|
223 return ret;
|
|
224 } else
|
|
225 total += ret;
|
|
226
|
|
227 /* end of stream */
|
|
228 if (!ret) break;
|
|
229 }
|
|
230
|
|
231 return total;
|
|
232 }
|
|
233
|
|
234 /*
|
|
235 * rtsp_get gets a line from stream
|
|
236 * and returns a null terminated string.
|
|
237 */
|
|
238
|
|
239 static char *rtsp_get(rtsp_t *s) {
|
|
240
|
|
241 int n=1;
|
|
242 char *buffer = malloc(BUF_SIZE);
|
|
243 char *string = NULL;
|
|
244
|
|
245 read_stream(s->s, buffer, 1);
|
|
246 while (n<BUF_SIZE) {
|
|
247 read_stream(s->s, &(buffer[n]), 1);
|
|
248 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break;
|
|
249 n++;
|
|
250 }
|
|
251
|
|
252 if (n>=BUF_SIZE) {
|
|
253 mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n");
|
|
254 exit(1);
|
|
255 }
|
|
256 string=malloc(sizeof(char)*n);
|
|
257 memcpy(string,buffer,n-1);
|
|
258 string[n-1]=0;
|
|
259
|
|
260 #ifdef LOG
|
|
261 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string);
|
|
262 #endif
|
|
263
|
|
264
|
|
265 free(buffer);
|
|
266 return string;
|
|
267 }
|
|
268
|
|
269 /*
|
|
270 * rtsp_put puts a line on stream
|
|
271 */
|
|
272
|
|
273 static void rtsp_put(rtsp_t *s, const char *string) {
|
|
274
|
|
275 int len=strlen(string);
|
|
276 char *buf=malloc(sizeof(char)*len+2);
|
|
277
|
|
278 #ifdef LOG
|
|
279 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string);
|
|
280 #endif
|
|
281
|
|
282 memcpy(buf,string,len);
|
|
283 buf[len]=0x0d;
|
|
284 buf[len+1]=0x0a;
|
|
285
|
|
286 write_stream(s->s, buf, len+2);
|
|
287
|
|
288 #ifdef LOG
|
|
289 mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n");
|
|
290 #endif
|
|
291
|
|
292 free(buf);
|
|
293 }
|
|
294
|
|
295 /*
|
|
296 * extract server status code
|
|
297 */
|
|
298
|
|
299 static int rtsp_get_code(const char *string) {
|
|
300
|
|
301 char buf[4];
|
|
302 int code=0;
|
|
303
|
|
304 if (!strncmp(string, RTSP_PROTOCOL_VERSION, strlen(RTSP_PROTOCOL_VERSION)))
|
|
305 {
|
|
306 memcpy(buf, string+strlen(RTSP_PROTOCOL_VERSION)+1, 3);
|
|
307 buf[3]=0;
|
|
308 code=atoi(buf);
|
|
309 } else if (!strncmp(string, RTSP_METHOD_SET_PARAMETER,8))
|
|
310 {
|
|
311 return RTSP_STATUS_SET_PARAMETER;
|
|
312 }
|
|
313
|
|
314 if(code != RTSP_STATUS_OK) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string);
|
|
315
|
|
316 return code;
|
|
317 }
|
|
318
|
|
319 /*
|
|
320 * send a request
|
|
321 */
|
|
322
|
|
323 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) {
|
|
324
|
|
325 char **payload=s->scheduled;
|
|
326 char *buf;
|
|
327
|
|
328 buf = malloc(strlen(type)+strlen(what)+strlen(RTSP_PROTOCOL_VERSION)+3);
|
|
329
|
|
330 sprintf(buf,"%s %s %s",type, what, RTSP_PROTOCOL_VERSION);
|
|
331 rtsp_put(s,buf);
|
|
332 free(buf);
|
|
333 if (payload)
|
|
334 while (*payload) {
|
|
335 rtsp_put(s,*payload);
|
|
336 payload++;
|
|
337 }
|
|
338 rtsp_put(s,"");
|
|
339 rtsp_unschedule_all(s);
|
|
340 }
|
|
341
|
|
342 /*
|
|
343 * schedule standard fields
|
|
344 */
|
|
345
|
|
346 static void rtsp_schedule_standard(rtsp_t *s) {
|
|
347
|
|
348 char tmp[16];
|
|
349
|
|
350 snprintf(tmp, 16, "CSeq: %u", s->cseq);
|
|
351 rtsp_schedule_field(s, tmp);
|
|
352
|
|
353 if (s->session) {
|
|
354 char *buf;
|
|
355 buf = malloc(strlen(s->session)+15);
|
|
356 sprintf(buf, "Session: %s", s->session);
|
|
357 rtsp_schedule_field(s, buf);
|
|
358 free(buf);
|
|
359 }
|
|
360 }
|
|
361 /*
|
|
362 * get the answers, if server responses with something != 200, return NULL
|
|
363 */
|
|
364
|
|
365 static int rtsp_get_answers(rtsp_t *s) {
|
|
366
|
|
367 char *answer=NULL;
|
|
368 unsigned int answer_seq;
|
|
369 char **answer_ptr=s->answers;
|
|
370 int code;
|
|
371 int ans_count = 0;
|
|
372
|
|
373 answer=rtsp_get(s);
|
|
374 if (!answer)
|
|
375 return 0;
|
|
376 code=rtsp_get_code(answer);
|
|
377 free(answer);
|
|
378
|
|
379 rtsp_free_answers(s);
|
|
380
|
|
381 do { /* while we get answer lines */
|
|
382
|
|
383 answer=rtsp_get(s);
|
|
384 if (!answer)
|
|
385 return 0;
|
|
386
|
|
387 if (!strncmp(answer,"CSeq:",5)) {
|
|
388 sscanf(answer,"CSeq: %u",&answer_seq);
|
|
389 if (s->cseq != answer_seq) {
|
|
390 #ifdef LOG
|
|
391 mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: CSeq mismatch. got %u, assumed %u", answer_seq, s->cseq);
|
|
392 #endif
|
|
393 s->cseq=answer_seq;
|
|
394 }
|
|
395 }
|
|
396 if (!strncmp(answer,"Server:",7)) {
|
|
397 char *buf = malloc(strlen(answer));
|
|
398 sscanf(answer,"Server: %s",buf);
|
|
399 if (s->server) free(s->server);
|
|
400 s->server=strdup(buf);
|
|
401 free(buf);
|
|
402 }
|
|
403 if (!strncmp(answer,"Session:",8)) {
|
|
404 char *buf = calloc(1, strlen(answer));
|
|
405 sscanf(answer,"Session: %s",buf);
|
|
406 if (s->session) {
|
|
407 if (strcmp(buf, s->session)) {
|
|
408 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf);
|
|
409 free(s->session);
|
|
410 s->session=strdup(buf);
|
|
411 }
|
|
412 } else
|
|
413 {
|
|
414 #ifdef LOG
|
|
415 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf);
|
|
416 #endif
|
|
417 s->session=strdup(buf);
|
|
418 }
|
|
419 free(buf);
|
|
420 }
|
|
421 *answer_ptr=answer;
|
|
422 answer_ptr++;
|
|
423 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS));
|
|
424
|
|
425 s->cseq++;
|
|
426
|
|
427 *answer_ptr=NULL;
|
|
428 rtsp_schedule_standard(s);
|
|
429
|
|
430 return code;
|
|
431 }
|
|
432
|
|
433 /*
|
|
434 * send an ok message
|
|
435 */
|
|
436
|
|
437 int rtsp_send_ok(rtsp_t *s) {
|
|
438 char cseq[16];
|
|
439
|
|
440 rtsp_put(s, "RTSP/1.0 200 OK");
|
|
441 sprintf(cseq,"CSeq: %u", s->cseq);
|
|
442 rtsp_put(s, cseq);
|
|
443 rtsp_put(s, "");
|
|
444 return 0;
|
|
445 }
|
|
446
|
|
447 /*
|
|
448 * implementation of must-have rtsp requests; functions return
|
|
449 * server status code.
|
|
450 */
|
|
451
|
|
452 int rtsp_request_options(rtsp_t *s, const char *what) {
|
|
453
|
|
454 char *buf;
|
|
455
|
|
456 if (what) {
|
|
457 buf=strdup(what);
|
|
458 } else
|
|
459 {
|
|
460 buf=malloc(sizeof(char)*(strlen(s->host)+16));
|
|
461 sprintf(buf,"rtsp://%s:%i", s->host, s->port);
|
|
462 }
|
|
463 rtsp_send_request(s,RTSP_METHOD_OPTIONS,buf);
|
|
464 free(buf);
|
|
465
|
|
466 return rtsp_get_answers(s);
|
|
467 }
|
|
468
|
|
469 int rtsp_request_describe(rtsp_t *s, const char *what) {
|
|
470
|
|
471 char *buf;
|
|
472
|
|
473 if (what) {
|
|
474 buf=strdup(what);
|
|
475 } else
|
|
476 {
|
|
477 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
|
|
478 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
|
|
479 }
|
|
480 rtsp_send_request(s,RTSP_METHOD_DESCRIBE,buf);
|
|
481 free(buf);
|
|
482
|
|
483 return rtsp_get_answers(s);
|
|
484 }
|
|
485
|
|
486 int rtsp_request_setup(rtsp_t *s, const char *what, char *control) {
|
|
487
|
|
488 char *buf = NULL;
|
|
489
|
|
490 if (what)
|
|
491 buf = strdup (what);
|
|
492 else
|
|
493 {
|
|
494 int len = strlen (s->host) + strlen (s->path) + 16;
|
|
495 if (control)
|
|
496 len += strlen (control) + 1;
|
|
497
|
|
498 buf = malloc (len);
|
|
499 sprintf (buf, "rtsp://%s:%i/%s%s%s", s->host, s->port, s->path,
|
|
500 control ? "/" : "", control ? control : "");
|
|
501 }
|
|
502
|
|
503 rtsp_send_request (s, RTSP_METHOD_SETUP, buf);
|
|
504 free (buf);
|
|
505 return rtsp_get_answers (s);
|
|
506 }
|
|
507
|
|
508 int rtsp_request_setparameter(rtsp_t *s, const char *what) {
|
|
509
|
|
510 char *buf;
|
|
511
|
|
512 if (what) {
|
|
513 buf=strdup(what);
|
|
514 } else
|
|
515 {
|
|
516 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
|
|
517 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
|
|
518 }
|
|
519 rtsp_send_request(s,RTSP_METHOD_SET_PARAMETER,buf);
|
|
520 free(buf);
|
|
521
|
|
522 return rtsp_get_answers(s);
|
|
523 }
|
|
524
|
|
525 int rtsp_request_play(rtsp_t *s, const char *what) {
|
|
526
|
|
527 char *buf;
|
|
528 int ret;
|
|
529
|
|
530 if (what) {
|
|
531 buf=strdup(what);
|
|
532 } else
|
|
533 {
|
|
534 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
|
|
535 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
|
|
536 }
|
|
537 rtsp_send_request(s,RTSP_METHOD_PLAY,buf);
|
|
538 free(buf);
|
|
539
|
|
540 ret = rtsp_get_answers (s);
|
|
541 if (ret == RTSP_STATUS_OK)
|
|
542 s->server_state = RTSP_PLAYING;
|
|
543
|
|
544 return ret;
|
|
545 }
|
|
546
|
|
547 int rtsp_request_teardown(rtsp_t *s, const char *what) {
|
|
548
|
|
549 char *buf;
|
|
550
|
|
551 if (what)
|
|
552 buf = strdup (what);
|
|
553 else
|
|
554 {
|
|
555 buf =
|
|
556 malloc (strlen (s->host) + strlen (s->path) + 16);
|
|
557 sprintf (buf, "rtsp://%s:%i/%s", s->host, s->port, s->path);
|
|
558 }
|
|
559 rtsp_send_request (s, RTSP_METHOD_TEARDOWN, buf);
|
|
560 free (buf);
|
18801
|
561
|
|
562 /* after teardown we're done with RTSP streaming, no need to get answer as
|
|
563 reading more will only result to garbage and buffer overflow */
|
|
564 return RTSP_STATUS_OK;
|
18799
|
565 }
|
|
566
|
|
567 /*
|
|
568 * read opaque data from stream
|
|
569 */
|
|
570
|
|
571 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) {
|
|
572
|
|
573 int i,seq;
|
|
574
|
|
575 if (size>=4) {
|
|
576 i=read_stream(s->s, buffer, 4);
|
|
577 if (i<4) return i;
|
|
578 if (((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) ||
|
|
579 ((buffer[0]=='O')&&(buffer[1]=='P')&&(buffer[2]=='T')&&(buffer[3]=='I'))) // OPTIONS
|
|
580 {
|
|
581 char *rest=rtsp_get(s);
|
|
582 if (!rest)
|
|
583 return -1;
|
|
584
|
|
585 seq=-1;
|
|
586 do {
|
|
587 free(rest);
|
|
588 rest=rtsp_get(s);
|
|
589 if (!rest)
|
|
590 return -1;
|
|
591 if (!strncmp(rest,"CSeq:",5))
|
|
592 sscanf(rest,"CSeq: %u",&seq);
|
|
593 } while (strlen(rest)!=0);
|
|
594 free(rest);
|
|
595 if (seq<0) {
|
|
596 #ifdef LOG
|
|
597 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: CSeq not recognized!\n");
|
|
598 #endif
|
|
599 seq=1;
|
|
600 }
|
|
601 /* let's make the server happy */
|
|
602 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood");
|
|
603 rest=malloc(sizeof(char)*16);
|
|
604 sprintf(rest,"CSeq: %u", seq);
|
|
605 rtsp_put(s, rest);
|
|
606 rtsp_put(s, "");
|
|
607 i=read_stream(s->s, buffer, size);
|
|
608 } else
|
|
609 {
|
|
610 i=read_stream(s->s, buffer+4, size-4);
|
|
611 i+=4;
|
|
612 }
|
|
613 } else
|
|
614 i=read_stream(s->s, buffer, size);
|
|
615 #ifdef LOG
|
|
616 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size);
|
|
617 #endif
|
|
618
|
|
619 return i;
|
|
620 }
|
|
621
|
|
622 /*
|
|
623 * connect to a rtsp server
|
|
624 */
|
|
625
|
|
626 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) {
|
|
627 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) {
|
|
628
|
|
629 rtsp_t *s=malloc(sizeof(rtsp_t));
|
|
630 int i;
|
|
631
|
|
632 for (i=0; i<MAX_FIELDS; i++) {
|
|
633 s->answers[i]=NULL;
|
|
634 s->scheduled[i]=NULL;
|
|
635 }
|
|
636
|
|
637 s->server=NULL;
|
|
638 s->server_state=0;
|
|
639 s->server_caps=0;
|
|
640
|
|
641 s->cseq=0;
|
|
642 s->session=NULL;
|
|
643
|
|
644 if (user_agent)
|
|
645 s->user_agent=strdup(user_agent);
|
|
646 else
|
|
647 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)");
|
|
648
|
|
649 s->mrl = strdup(mrl);
|
|
650 s->host = strdup(host);
|
|
651 s->port = port;
|
|
652 s->path = strdup(path);
|
|
653 while (*path == '/')
|
|
654 path++;
|
|
655 if ((s->param = strchr(s->path, '?')) != NULL)
|
|
656 s->param++;
|
|
657 //mp_msg(MSGT_OPEN, MSGL_INFO, "path=%s\n", s->path);
|
|
658 //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL");
|
|
659 s->s = fd;
|
|
660
|
|
661 if (s->s < 0) {
|
|
662 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", s->host);
|
|
663 rtsp_close(s);
|
|
664 return NULL;
|
|
665 }
|
|
666
|
|
667 s->server_state=RTSP_CONNECTED;
|
|
668
|
|
669 /* now let's send an options request. */
|
|
670 rtsp_schedule_field(s, "CSeq: 1");
|
|
671 rtsp_schedule_field(s, s->user_agent);
|
|
672 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7");
|
|
673 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]");
|
|
674 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw==");
|
|
675 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000");
|
|
676 rtsp_schedule_field(s, "RegionData: 0");
|
|
677 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
|
|
678 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/
|
|
679 rtsp_request_options(s, NULL);
|
|
680
|
|
681 return s;
|
|
682 }
|
|
683
|
|
684
|
|
685 /*
|
|
686 * closes an rtsp connection
|
|
687 */
|
|
688
|
|
689 void rtsp_close(rtsp_t *s) {
|
|
690
|
|
691 if (s->server_state)
|
|
692 {
|
|
693 if (s->server_state == RTSP_PLAYING)
|
|
694 rtsp_request_teardown (s, NULL);
|
|
695 closesocket (s->s);
|
|
696 }
|
|
697
|
|
698 if (s->path) free(s->path);
|
|
699 if (s->host) free(s->host);
|
|
700 if (s->mrl) free(s->mrl);
|
|
701 if (s->session) free(s->session);
|
|
702 if (s->user_agent) free(s->user_agent);
|
|
703 rtsp_free_answers(s);
|
|
704 rtsp_unschedule_all(s);
|
|
705 free(s);
|
|
706 }
|
|
707
|
|
708 /*
|
|
709 * search in answers for tags. returns a pointer to the content
|
|
710 * after the first matched tag. returns NULL if no match found.
|
|
711 */
|
|
712
|
|
713 char *rtsp_search_answers(rtsp_t *s, const char *tag) {
|
|
714
|
|
715 char **answer;
|
|
716 char *ptr;
|
|
717
|
|
718 if (!s->answers) return NULL;
|
|
719 answer=s->answers;
|
|
720
|
|
721 while (*answer) {
|
|
722 if (!strncasecmp(*answer,tag,strlen(tag))) {
|
|
723 ptr=strchr(*answer,':');
|
|
724 if (!ptr) return NULL;
|
|
725 ptr++;
|
|
726 while(*ptr==' ') ptr++;
|
|
727 return ptr;
|
|
728 }
|
|
729 answer++;
|
|
730 }
|
|
731
|
|
732 return NULL;
|
|
733 }
|
|
734
|
|
735 /*
|
|
736 * session id management
|
|
737 */
|
|
738
|
|
739 void rtsp_set_session(rtsp_t *s, const char *id) {
|
|
740
|
|
741 if (s->session) free(s->session);
|
|
742
|
|
743 s->session=strdup(id);
|
|
744
|
|
745 }
|
|
746
|
|
747 char *rtsp_get_session(rtsp_t *s) {
|
|
748
|
|
749 return s->session;
|
|
750
|
|
751 }
|
|
752
|
|
753 char *rtsp_get_mrl(rtsp_t *s) {
|
|
754
|
|
755 return s->mrl;
|
|
756
|
|
757 }
|
|
758
|
|
759 char *rtsp_get_param(rtsp_t *s, char *p) {
|
|
760 int len;
|
|
761 char *param;
|
|
762 if (!s->param)
|
|
763 return NULL;
|
|
764 if (!p)
|
|
765 return strdup(s->param);
|
|
766 len = strlen(p);
|
|
767 param = s->param;
|
|
768 while (param && *param) {
|
|
769 char *nparam = strchr(param, '&');
|
|
770 if (strncmp(param, p, len) == 0 && param[len] == '=') {
|
|
771 param += len + 1;
|
|
772 len = nparam ? nparam - param : strlen(param);
|
|
773 nparam = malloc(len + 1);
|
|
774 memcpy(nparam, param, len);
|
|
775 nparam[len] = 0;
|
|
776 return nparam;
|
|
777 }
|
|
778 param = nparam ? nparam + 1 : NULL;
|
|
779 }
|
|
780 return NULL;
|
|
781 }
|
|
782
|
|
783 /*
|
|
784 * schedules a field for transmission
|
|
785 */
|
|
786
|
|
787 void rtsp_schedule_field(rtsp_t *s, const char *string) {
|
|
788
|
|
789 int i=0;
|
|
790
|
|
791 if (!string) return;
|
|
792
|
|
793 while(s->scheduled[i]) {
|
|
794 i++;
|
|
795 }
|
|
796 s->scheduled[i]=strdup(string);
|
|
797 }
|
|
798
|
|
799 /*
|
|
800 * removes the first scheduled field which prefix matches string.
|
|
801 */
|
|
802
|
|
803 void rtsp_unschedule_field(rtsp_t *s, const char *string) {
|
|
804
|
|
805 char **ptr=s->scheduled;
|
|
806
|
|
807 if (!string) return;
|
|
808
|
|
809 while(*ptr) {
|
|
810 if (!strncmp(*ptr, string, strlen(string)))
|
|
811 break;
|
|
812 else
|
|
813 ptr++;
|
|
814 }
|
|
815 if (*ptr) free(*ptr);
|
|
816 ptr++;
|
|
817 do {
|
|
818 *(ptr-1)=*ptr;
|
|
819 } while(*ptr);
|
|
820 }
|
|
821
|
|
822 /*
|
|
823 * unschedule all fields
|
|
824 */
|
|
825
|
|
826 void rtsp_unschedule_all(rtsp_t *s) {
|
|
827
|
|
828 char **ptr;
|
|
829
|
|
830 if (!s->scheduled) return;
|
|
831 ptr=s->scheduled;
|
|
832
|
|
833 while (*ptr) {
|
|
834 free(*ptr);
|
|
835 *ptr=NULL;
|
|
836 ptr++;
|
|
837 }
|
|
838 }
|
|
839 /*
|
|
840 * free answers
|
|
841 */
|
|
842
|
|
843 void rtsp_free_answers(rtsp_t *s) {
|
|
844
|
|
845 char **answer;
|
|
846
|
|
847 if (!s->answers) return;
|
|
848 answer=s->answers;
|
|
849
|
|
850 while (*answer) {
|
|
851 free(*answer);
|
|
852 *answer=NULL;
|
|
853 answer++;
|
|
854 }
|
|
855 }
|