comparison libmpdemux/realrtsp/rtsp.c @ 9922:6cb7a295ab0e

Real rstp:// streaming support, ported from xine
author rtognimp
date Thu, 17 Apr 2003 20:39:41 +0000
parents
children 6b88f67564ce
comparison
equal deleted inserted replaced
9921:61057de81510 9922:6cb7a295ab0e
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
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netdb.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <time.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43
44 #include "rtsp.h"
45
46 /*
47 #define LOG
48 */
49
50 #define BUF_SIZE 4096
51 #define HEADER_SIZE 1024
52 #define MAX_FIELDS 256
53
54 struct rtsp_s {
55
56 int s;
57
58 char *host;
59 int port;
60 char *path;
61 char *mrl;
62 char *user_agent;
63
64 char *server;
65 unsigned int server_state;
66 uint32_t server_caps;
67
68 char buffer[BUF_SIZE]; /* scratch buffer */
69
70 unsigned int cseq;
71 char *session;
72
73 char *answers[MAX_FIELDS]; /* data of last message */
74 char *scheduled[MAX_FIELDS]; /* will be sent with next message */
75 };
76
77 /*
78 * constants
79 */
80
81 const char rtsp_protocol_version[]="RTSP/1.0";
82
83 /* server states */
84 #define RTSP_CONNECTED 1
85 #define RTSP_INIT 2
86 #define RTSP_READY 4
87 #define RTSP_PLAYING 8
88 #define RTSP_RECORDING 16
89
90 /* server capabilities */
91 #define RTSP_OPTIONS 0x001
92 #define RTSP_DESCRIBE 0x002
93 #define RTSP_ANNOUNCE 0x004
94 #define RTSP_SETUP 0x008
95 #define RTSP_GET_PARAMETER 0x010
96 #define RTSP_SET_PARAMETER 0x020
97 #define RTSP_TEARDOWN 0x040
98 #define RTSP_PLAY 0x080
99 #define RTSP_RECORD 0x100
100
101 /*
102 * network utilities
103 */
104
105 static int host_connect_attempt(struct in_addr ia, int port) {
106
107 int s;
108 struct sockaddr_in sin;
109
110 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
111 if (s == -1) {
112 printf ("rtsp: socket(): %s\n", strerror(errno));
113 return -1;
114 }
115
116 sin.sin_family = AF_INET;
117 sin.sin_addr = ia;
118 sin.sin_port = htons(port);
119
120 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1
121 && errno != EINPROGRESS) {
122 printf ("rtsp: connect(): %s\n", strerror(errno));
123 close(s);
124 return -1;
125 }
126
127 return s;
128 }
129
130 static int host_connect(const char *host, int port) {
131
132 struct hostent *h;
133 int i, s;
134
135 h = gethostbyname(host);
136 if (h == NULL) {
137 printf ("rtsp: unable to resolve '%s'.\n", host);
138 return -1;
139 }
140
141 for (i = 0; h->h_addr_list[i]; i++) {
142 struct in_addr ia;
143
144 memcpy (&ia, h->h_addr_list[i], 4);
145 s = host_connect_attempt(ia, port);
146 if(s != -1)
147 return s;
148 }
149 printf ("rtsp: unable to connect to '%s'.\n", host);
150 return -1;
151 }
152
153 static int write_stream(int s, const char *buf, int len) {
154 int total, timeout;
155
156 total = 0; timeout = 30;
157 while (total < len){
158 int n;
159
160 n = write (s, &buf[total], len - total);
161
162 if (n > 0)
163 total += n;
164 else if (n < 0) {
165 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) {
166 sleep (1); timeout--;
167 } else
168 return -1;
169 }
170 }
171
172 return total;
173 }
174
175 static ssize_t read_stream(int fd, void *buf, size_t count) {
176
177 ssize_t ret, total;
178
179 total = 0;
180
181 while (total < count) {
182
183 ret=read (fd, ((uint8_t*)buf)+total, count-total);
184
185 if (ret<0) {
186 if(errno == EAGAIN) {
187 fd_set rset;
188 struct timeval timeout;
189
190 FD_ZERO (&rset);
191 FD_SET (fd, &rset);
192
193 timeout.tv_sec = 30;
194 timeout.tv_usec = 0;
195
196 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) {
197 return -1;
198 }
199 continue;
200 }
201
202 printf ("rtsp: read error.\n");
203 return ret;
204 } else
205 total += ret;
206
207 /* end of stream */
208 if (!ret) break;
209 }
210
211 return total;
212 }
213
214 /*
215 * debugging utilities
216 */
217 #if 0
218 static void hexdump (char *buf, int length) {
219
220 int i;
221
222 printf ("rtsp: ascii>");
223 for (i = 0; i < length; i++) {
224 unsigned char c = buf[i];
225
226 if ((c >= 32) && (c <= 128))
227 printf ("%c", c);
228 else
229 printf (".");
230 }
231 printf ("\n");
232
233 printf ("rtsp: hexdump> ");
234 for (i = 0; i < length; i++) {
235 unsigned char c = buf[i];
236
237 printf ("%02x", c);
238
239 if ((i % 16) == 15)
240 printf ("\nrtsp: ");
241
242 if ((i % 2) == 1)
243 printf (" ");
244
245 }
246 printf ("\n");
247 }
248 #endif
249
250 /*
251 * rtsp_get gets a line from stream
252 * and returns a null terminated string.
253 */
254
255 static char *rtsp_get(rtsp_t *s) {
256
257 int n=0;
258 char *string;
259
260 while (n<BUF_SIZE) {
261 read_stream(s->s, &s->buffer[n], 1);
262 if ((s->buffer[n-1]==0x0d)&&(s->buffer[n]==0x0a)) break;
263 n++;
264 }
265
266 if (n>=BUF_SIZE) {
267 printf("librtsp: buffer overflow in rtsp_get\n");
268 exit(1);
269 }
270 string=malloc(sizeof(char)*n);
271 memcpy(string,s->buffer,n-1);
272 string[n-1]=0;
273
274 #ifdef LOG
275 printf("librtsp: << '%s'\n", string);
276 #endif
277
278
279 return string;
280 }
281
282 /*
283 * rtsp_put puts a line on stream
284 */
285
286 static void rtsp_put(rtsp_t *s, const char *string) {
287
288 int len=strlen(string);
289 char *buf=malloc(sizeof(char)*len+2);
290
291 #ifdef LOG
292 printf("librtsp: >> '%s'", string);
293 #endif
294
295 memcpy(buf,string,len);
296 buf[len]=0x0d;
297 buf[len+1]=0x0a;
298
299 write_stream(s->s, buf, len+2);
300
301 #ifdef LOG
302 printf(" done.\n");
303 #endif
304
305 free(buf);
306 }
307
308 /*
309 * extract server status code
310 */
311
312 static int rtsp_get_code(const char *string) {
313
314 char buf[4];
315 int code=0;
316
317 if (!strncmp(string, rtsp_protocol_version, strlen(rtsp_protocol_version)))
318 {
319 memcpy(buf, string+strlen(rtsp_protocol_version)+1, 3);
320 buf[3]=0;
321 code=atoi(buf);
322 } else if (!strncmp(string, "SET_PARAMETER",8))
323 {
324 return RTSP_STATUS_SET_PARAMETER;
325 }
326
327 if(code != 200) printf("librtsp: server responds: '%s'\n",string);
328
329 return code;
330 }
331
332 /*
333 * send a request
334 */
335
336 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) {
337
338 char **payload=s->scheduled;
339 sprintf(s->buffer,"%s %s %s",type, what, rtsp_protocol_version);
340 rtsp_put(s,s->buffer);
341 if (payload)
342 while (*payload) {
343 rtsp_put(s,*payload);
344 payload++;
345 }
346 rtsp_put(s,"");
347 rtsp_unschedule_all(s);
348 }
349
350 /*
351 * schedule standard fields
352 */
353
354 static void rtsp_schedule_standard(rtsp_t *s) {
355
356 sprintf(s->buffer, "Cseq: %u", s->cseq);
357 rtsp_schedule_field(s, s->buffer);
358 if (s->session) {
359 sprintf(s->buffer, "Session: %s", s->session);
360 rtsp_schedule_field(s, s->buffer);
361 }
362 }
363 /*
364 * get the answers, if server responses with something != 200, return NULL
365 */
366
367 static int rtsp_get_answers(rtsp_t *s) {
368
369 char *answer=NULL;
370 unsigned int answer_seq;
371 char **answer_ptr=s->answers;
372 int code;
373
374 answer=rtsp_get(s);
375 code=rtsp_get_code(answer);
376 free(answer);
377
378 rtsp_free_answers(s);
379
380 do { /* while we get answer lines */
381
382 answer=rtsp_get(s);
383
384 if (!strncmp(answer,"Cseq:",5)) {
385 sscanf(answer,"Cseq: %u",&answer_seq);
386 if (s->cseq != answer_seq) {
387 #ifdef LOG
388 printf("librtsp: warning: Cseq mismatch. got %u, assumed %u", answer_seq, s->cseq);
389 #endif
390 s->cseq=answer_seq;
391 }
392 }
393 if (!strncmp(answer,"Server:",7)) {
394 sscanf(answer,"Server: %s",s->buffer);
395 if (s->server) free(s->server);
396 s->server=strdup(s->buffer);
397 }
398 if (!strncmp(answer,"Session:",8)) {
399 memset(s->buffer,0, BUF_SIZE);
400 sscanf(answer,"Session: %s",s->buffer);
401 if (s->session) {
402 if (strcmp(s->buffer, s->session)) {
403 printf("rtsp: warning: setting NEW session: %s\n", s->buffer);
404 free(s->session);
405 s->session=strdup(s->buffer);
406 }
407 } else
408 {
409 #ifdef LOG
410 printf("rtsp: setting session id to: %s\n", s->buffer);
411 #endif
412 s->session=strdup(s->buffer);
413 }
414 }
415 *answer_ptr=answer;
416 answer_ptr++;
417 } while (strlen(answer)!=0);
418
419 s->cseq++;
420
421 *answer_ptr=NULL;
422 rtsp_schedule_standard(s);
423
424 return code;
425 }
426
427 /*
428 * send an ok message
429 */
430
431 int rtsp_send_ok(rtsp_t *s) {
432 char cseq[16];
433
434 rtsp_put(s, "RTSP/1.0 200 OK");
435 sprintf(cseq,"CSeq: %u", s->cseq);
436 rtsp_put(s, cseq);
437 rtsp_put(s, "");
438 return 0;
439 }
440
441 /*
442 * implementation of must-have rtsp requests; functions return
443 * server status code.
444 */
445
446 int rtsp_request_options(rtsp_t *s, const char *what) {
447
448 char *buf;
449
450 if (what) {
451 buf=strdup(what);
452 } else
453 {
454 buf=malloc(sizeof(char)*(strlen(s->host)+16));
455 sprintf(buf,"rtsp://%s:%i", s->host, s->port);
456 }
457 rtsp_send_request(s,"OPTIONS",buf);
458 free(buf);
459
460 return rtsp_get_answers(s);
461 }
462
463 int rtsp_request_describe(rtsp_t *s, const char *what) {
464
465 char *buf;
466
467 if (what) {
468 buf=strdup(what);
469 } else
470 {
471 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
472 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
473 }
474 rtsp_send_request(s,"DESCRIBE",buf);
475 free(buf);
476
477 return rtsp_get_answers(s);
478 }
479
480 int rtsp_request_setup(rtsp_t *s, const char *what) {
481
482 rtsp_send_request(s,"SETUP",what);
483
484 return rtsp_get_answers(s);
485 }
486
487 int rtsp_request_setparameter(rtsp_t *s, const char *what) {
488
489 char *buf;
490
491 if (what) {
492 buf=strdup(what);
493 } else
494 {
495 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
496 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
497 }
498 rtsp_send_request(s,"SET_PARAMETER",buf);
499 free(buf);
500
501 return rtsp_get_answers(s);
502 }
503
504 int rtsp_request_play(rtsp_t *s, const char *what) {
505
506 char *buf;
507
508 if (what) {
509 buf=strdup(what);
510 } else
511 {
512 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16));
513 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
514 }
515 rtsp_send_request(s,"PLAY",buf);
516 free(buf);
517
518 return rtsp_get_answers(s);
519 }
520
521 int rtsp_request_tearoff(rtsp_t *s, const char *what) {
522
523 rtsp_send_request(s,"TEAROFF",what);
524
525 return rtsp_get_answers(s);
526 }
527
528 /*
529 * read opaque data from stream
530 */
531
532 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) {
533
534 int i,seq;
535
536 if (size>=4) {
537 i=read_stream(s->s, buffer, 4);
538 if (i<4) return i;
539 if ((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_'))
540 {
541 char *rest=rtsp_get(s);
542 /* a real server wanna play table tennis? */
543 memcpy(s->buffer, buffer, 4);
544 strcpy(s->buffer+4, rest);
545 seq=-1;
546 do {
547 free(rest);
548 rest=rtsp_get(s);
549 if (!strncmp(rest,"Cseq:",5))
550 sscanf(rest,"Cseq: %u",&seq);
551 } while (strlen(rest)!=0);
552 free(rest);
553 if (seq<0) {
554 #ifdef LOG
555 printf("rtsp: warning: cseq not recognized!\n");
556 #endif
557 seq=1;
558 }
559 /* lets make the server happy */
560 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood");
561 rest=malloc(sizeof(char)*16);
562 sprintf(rest,"CSeq: %u", seq);
563 rtsp_put(s, rest);
564 rtsp_put(s, "");
565 i=read_stream(s->s, buffer, size);
566 } else
567 {
568 i=read_stream(s->s, buffer+4, size-4);
569 i+=4;
570 }
571 } else
572 i=read_stream(s->s, buffer, size);
573 #ifdef LOG
574 printf("librtsp: << %d of %d bytes\n", i, size);
575 #endif
576
577 return i;
578 }
579
580 /*
581 * connect to a rtsp server
582 */
583
584 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) {
585 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) {
586
587 rtsp_t *s=malloc(sizeof(rtsp_t));
588 int i;
589
590 for (i=0; i<MAX_FIELDS; i++) {
591 s->answers[i]=NULL;
592 s->scheduled[i]=NULL;
593 }
594
595 s->server=NULL;
596 s->server_state=0;
597 s->server_caps=0;
598
599 s->cseq=0;
600 s->session=NULL;
601
602 if (user_agent)
603 s->user_agent=strdup(user_agent);
604 else
605 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)");
606
607 s->mrl = strdup(mrl);
608 s->host = strdup(host);
609 s->port = port;
610 s->path = strdup(path);
611 s->s = fd;
612
613 if (s->s < 0) {
614 printf ("rtsp: failed to connect to '%s'\n", s->host);
615 rtsp_close(s);
616 return NULL;
617 }
618
619 s->server_state=RTSP_CONNECTED;
620
621 /* now lets send an options request. */
622 rtsp_schedule_field(s, "CSeq: 1");
623 rtsp_schedule_field(s, s->user_agent);
624 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7");
625 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]");
626 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw==");
627 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000");
628 rtsp_schedule_field(s, "RegionData: 0");
629 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
630 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/
631 rtsp_request_options(s, NULL);
632
633 return s;
634 }
635
636
637 /*
638 * closes an rtsp connection
639 */
640
641 void rtsp_close(rtsp_t *s) {
642
643 if (s->server_state) close(s->s); /* TODO: send a TEAROFF */
644 if (s->path) free(s->path);
645 if (s->host) free(s->host);
646 if (s->mrl) free(s->mrl);
647 if (s->session) free(s->session);
648 if (s->user_agent) free(s->user_agent);
649 rtsp_free_answers(s);
650 rtsp_unschedule_all(s);
651 free(s);
652 }
653
654 /*
655 * search in answers for tags. returns a pointer to the content
656 * after the first matched tag. returns NULL if no match found.
657 */
658
659 char *rtsp_search_answers(rtsp_t *s, const char *tag) {
660
661 char **answer;
662 char *ptr;
663
664 if (!s->answers) return NULL;
665 answer=s->answers;
666
667 while (*answer) {
668 if (!strncasecmp(*answer,tag,strlen(tag))) {
669 ptr=strchr(*answer,':');
670 ptr++;
671 while(*ptr==' ') ptr++;
672 return ptr;
673 }
674 answer++;
675 }
676
677 return NULL;
678 }
679
680 /*
681 * session id management
682 */
683
684 void rtsp_set_session(rtsp_t *s, const char *id) {
685
686 if (s->session) free(s->session);
687
688 s->session=strdup(id);
689
690 }
691
692 char *rtsp_get_session(rtsp_t *s) {
693
694 return s->session;
695
696 }
697
698 char *rtsp_get_mrl(rtsp_t *s) {
699
700 return s->mrl;
701
702 }
703
704 /*
705 * schedules a field for transmission
706 */
707
708 void rtsp_schedule_field(rtsp_t *s, const char *string) {
709
710 int i=0;
711
712 if (!string) return;
713
714 while(s->scheduled[i]) {
715 i++;
716 }
717 s->scheduled[i]=strdup(string);
718 }
719
720 /*
721 * removes the first scheduled field which prefix matches string.
722 */
723
724 void rtsp_unschedule_field(rtsp_t *s, const char *string) {
725
726 char **ptr=s->scheduled;
727
728 if (!string) return;
729
730 while(*ptr) {
731 if (!strncmp(*ptr, string, strlen(string)))
732 break;
733 }
734 if (*ptr) free(*ptr);
735 ptr++;
736 do {
737 *(ptr-1)=*ptr;
738 } while(*ptr);
739 }
740
741 /*
742 * unschedule all fields
743 */
744
745 void rtsp_unschedule_all(rtsp_t *s) {
746
747 char **ptr;
748
749 if (!s->scheduled) return;
750 ptr=s->scheduled;
751
752 while (*ptr) {
753 free(*ptr);
754 *ptr=NULL;
755 ptr++;
756 }
757 }
758 /*
759 * free answers
760 */
761
762 void rtsp_free_answers(rtsp_t *s) {
763
764 char **answer;
765
766 if (!s->answers) return;
767 answer=s->answers;
768
769 while (*answer) {
770 free(*answer);
771 *answer=NULL;
772 answer++;
773 }
774 }