100l, do not read probe buffer if it will not be used because a format was forced.
[mplayer/glamo.git] / stream / librtsp / rtsp.c
blob53ed233c53b4db2760b1ecb4a49987867bd80236
1 /*
2 * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48
3 */
5 /*
6 * Copyright (C) 2000-2002 the xine project
8 * This file is part of xine, a free video player.
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.
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.
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
25 * a minimalistic implementation of rtsp protocol,
26 * *not* RFC 2326 compilant yet.
28 * 2006, Benjamin Zores and Vincent Mussard
29 * fixed a lot of RFC compliance issues.
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include "config.h"
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <inttypes.h>
45 #if HAVE_WINSOCK2_H
46 #include <winsock2.h>
47 #else
48 #include <sys/socket.h>
49 #endif
50 #include "mp_msg.h"
51 #include "rtsp.h"
52 #include "rtsp_session.h"
53 #include "osdep/timer.h"
56 #define LOG
60 * network utilities
63 static int write_stream(int s, const char *buf, int len) {
64 int total, timeout;
66 total = 0; timeout = 30;
67 while (total < len){
68 int n;
70 n = send (s, &buf[total], len - total, 0);
72 if (n > 0)
73 total += n;
74 else if (n < 0) {
75 #if !HAVE_WINSOCK2_H
76 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) {
77 #else
78 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) {
79 #endif
80 usec_sleep (1000000); timeout--;
81 } else
82 return -1;
86 return total;
89 static ssize_t read_stream(int fd, void *buf, size_t count) {
91 ssize_t ret, total;
93 total = 0;
95 while (total < count) {
97 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0);
99 if (ret<0) {
100 if(errno == EAGAIN) {
101 fd_set rset;
102 struct timeval timeout;
104 FD_ZERO (&rset);
105 FD_SET (fd, &rset);
107 timeout.tv_sec = 30;
108 timeout.tv_usec = 0;
110 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) {
111 return -1;
113 continue;
116 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: read error.\n");
117 return ret;
118 } else
119 total += ret;
121 /* end of stream */
122 if (!ret) break;
125 return total;
129 * rtsp_get gets a line from stream
130 * and returns a null terminated string.
133 static char *rtsp_get(rtsp_t *s) {
135 int n=1;
136 char *buffer = malloc(BUF_SIZE);
137 char *string = NULL;
139 read_stream(s->s, buffer, 1);
140 while (n<BUF_SIZE) {
141 read_stream(s->s, &(buffer[n]), 1);
142 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break;
143 n++;
146 if (n>=BUF_SIZE) {
147 mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n");
148 exit(1);
150 string=malloc(n);
151 memcpy(string,buffer,n-1);
152 string[n-1]=0;
154 #ifdef LOG
155 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string);
156 #endif
159 free(buffer);
160 return string;
164 * rtsp_put puts a line on stream
167 static void rtsp_put(rtsp_t *s, const char *string) {
169 int len=strlen(string);
170 char *buf=malloc(len+2);
172 #ifdef LOG
173 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string);
174 #endif
176 memcpy(buf,string,len);
177 buf[len]=0x0d;
178 buf[len+1]=0x0a;
180 write_stream(s->s, buf, len+2);
182 #ifdef LOG
183 mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n");
184 #endif
186 free(buf);
190 * extract server status code
193 static int rtsp_get_code(const char *string) {
195 char buf[4];
196 int code=0;
198 if (!strncmp(string, RTSP_PROTOCOL_VERSION, strlen(RTSP_PROTOCOL_VERSION)))
200 memcpy(buf, string+strlen(RTSP_PROTOCOL_VERSION)+1, 3);
201 buf[3]=0;
202 code=atoi(buf);
203 } else if (!strncmp(string, RTSP_METHOD_SET_PARAMETER,8))
205 return RTSP_STATUS_SET_PARAMETER;
208 if(code != RTSP_STATUS_OK) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string);
210 return code;
214 * send a request
217 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) {
219 char **payload=s->scheduled;
220 char *buf;
222 buf = malloc(strlen(type)+strlen(what)+strlen(RTSP_PROTOCOL_VERSION)+3);
224 sprintf(buf,"%s %s %s",type, what, RTSP_PROTOCOL_VERSION);
225 rtsp_put(s,buf);
226 free(buf);
227 if (payload)
228 while (*payload) {
229 rtsp_put(s,*payload);
230 payload++;
232 rtsp_put(s,"");
233 rtsp_unschedule_all(s);
237 * schedule standard fields
240 static void rtsp_schedule_standard(rtsp_t *s) {
242 char tmp[17];
244 snprintf(tmp, 17, "CSeq: %u", s->cseq);
245 rtsp_schedule_field(s, tmp);
247 if (s->session) {
248 char *buf;
249 buf = malloc(strlen(s->session)+15);
250 sprintf(buf, "Session: %s", s->session);
251 rtsp_schedule_field(s, buf);
252 free(buf);
256 * get the answers, if server responses with something != 200, return NULL
259 static int rtsp_get_answers(rtsp_t *s) {
261 char *answer=NULL;
262 unsigned int answer_seq;
263 char **answer_ptr=s->answers;
264 int code;
265 int ans_count = 0;
267 answer=rtsp_get(s);
268 if (!answer)
269 return 0;
270 code=rtsp_get_code(answer);
271 free(answer);
273 rtsp_free_answers(s);
275 do { /* while we get answer lines */
277 answer=rtsp_get(s);
278 if (!answer)
279 return 0;
281 if (!strncasecmp(answer,"CSeq:",5)) {
282 sscanf(answer,"%*s %u",&answer_seq);
283 if (s->cseq != answer_seq) {
284 #ifdef LOG
285 mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: CSeq mismatch. got %u, assumed %u", answer_seq, s->cseq);
286 #endif
287 s->cseq=answer_seq;
290 if (!strncasecmp(answer,"Server:",7)) {
291 char *buf = malloc(strlen(answer));
292 sscanf(answer,"%*s %s",buf);
293 if (s->server) free(s->server);
294 s->server=strdup(buf);
295 free(buf);
297 if (!strncasecmp(answer,"Session:",8)) {
298 char *buf = calloc(1, strlen(answer));
299 sscanf(answer,"%*s %s",buf);
300 if (s->session) {
301 if (strcmp(buf, s->session)) {
302 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf);
303 free(s->session);
304 s->session=strdup(buf);
306 } else
308 #ifdef LOG
309 mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf);
310 #endif
311 s->session=strdup(buf);
313 free(buf);
315 *answer_ptr=answer;
316 answer_ptr++;
317 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS));
319 s->cseq++;
321 *answer_ptr=NULL;
322 rtsp_schedule_standard(s);
324 return code;
328 * send an ok message
331 int rtsp_send_ok(rtsp_t *s) {
332 char cseq[16];
334 rtsp_put(s, "RTSP/1.0 200 OK");
335 sprintf(cseq,"CSeq: %u", s->cseq);
336 rtsp_put(s, cseq);
337 rtsp_put(s, "");
338 return 0;
342 * implementation of must-have rtsp requests; functions return
343 * server status code.
346 int rtsp_request_options(rtsp_t *s, const char *what) {
348 char *buf;
350 if (what) {
351 buf=strdup(what);
352 } else
354 buf=malloc(strlen(s->host)+16);
355 sprintf(buf,"rtsp://%s:%i", s->host, s->port);
357 rtsp_send_request(s,RTSP_METHOD_OPTIONS,buf);
358 free(buf);
360 return rtsp_get_answers(s);
363 int rtsp_request_describe(rtsp_t *s, const char *what) {
365 char *buf;
367 if (what) {
368 buf=strdup(what);
369 } else
371 buf=malloc(strlen(s->host)+strlen(s->path)+16);
372 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
374 rtsp_send_request(s,RTSP_METHOD_DESCRIBE,buf);
375 free(buf);
377 return rtsp_get_answers(s);
380 int rtsp_request_setup(rtsp_t *s, const char *what, char *control) {
382 char *buf = NULL;
384 if (what)
385 buf = strdup (what);
386 else
388 int len = strlen (s->host) + strlen (s->path) + 16;
389 if (control)
390 len += strlen (control) + 1;
392 buf = malloc (len);
393 sprintf (buf, "rtsp://%s:%i/%s%s%s", s->host, s->port, s->path,
394 control ? "/" : "", control ? control : "");
397 rtsp_send_request (s, RTSP_METHOD_SETUP, buf);
398 free (buf);
399 return rtsp_get_answers (s);
402 int rtsp_request_setparameter(rtsp_t *s, const char *what) {
404 char *buf;
406 if (what) {
407 buf=strdup(what);
408 } else
410 buf=malloc(strlen(s->host)+strlen(s->path)+16);
411 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
413 rtsp_send_request(s,RTSP_METHOD_SET_PARAMETER,buf);
414 free(buf);
416 return rtsp_get_answers(s);
419 int rtsp_request_play(rtsp_t *s, const char *what) {
421 char *buf;
422 int ret;
424 if (what) {
425 buf=strdup(what);
426 } else
428 buf=malloc(strlen(s->host)+strlen(s->path)+16);
429 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path);
431 rtsp_send_request(s,RTSP_METHOD_PLAY,buf);
432 free(buf);
434 ret = rtsp_get_answers (s);
435 if (ret == RTSP_STATUS_OK)
436 s->server_state = RTSP_PLAYING;
438 return ret;
441 int rtsp_request_teardown(rtsp_t *s, const char *what) {
443 char *buf;
445 if (what)
446 buf = strdup (what);
447 else
449 buf =
450 malloc (strlen (s->host) + strlen (s->path) + 16);
451 sprintf (buf, "rtsp://%s:%i/%s", s->host, s->port, s->path);
453 rtsp_send_request (s, RTSP_METHOD_TEARDOWN, buf);
454 free (buf);
456 /* after teardown we're done with RTSP streaming, no need to get answer as
457 reading more will only result to garbage and buffer overflow */
458 return RTSP_STATUS_OK;
462 * read opaque data from stream
465 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) {
467 int i,seq;
469 if (size>=4) {
470 i=read_stream(s->s, buffer, 4);
471 if (i<4) return i;
472 if (((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) ||
473 ((buffer[0]=='O')&&(buffer[1]=='P')&&(buffer[2]=='T')&&(buffer[3]=='I'))) // OPTIONS
475 char *rest=rtsp_get(s);
476 if (!rest)
477 return -1;
479 seq=-1;
480 do {
481 free(rest);
482 rest=rtsp_get(s);
483 if (!rest)
484 return -1;
485 if (!strncasecmp(rest,"CSeq:",5))
486 sscanf(rest,"%*s %u",&seq);
487 } while (strlen(rest)!=0);
488 free(rest);
489 if (seq<0) {
490 #ifdef LOG
491 mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: CSeq not recognized!\n");
492 #endif
493 seq=1;
495 /* let's make the server happy */
496 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood");
497 rest=malloc(17);
498 sprintf(rest,"CSeq: %u", seq);
499 rtsp_put(s, rest);
500 free(rest);
501 rtsp_put(s, "");
502 i=read_stream(s->s, buffer, size);
503 } else
505 i=read_stream(s->s, buffer+4, size-4);
506 i+=4;
508 } else
509 i=read_stream(s->s, buffer, size);
510 #ifdef LOG
511 mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size);
512 #endif
514 return i;
518 * connect to a rtsp server
521 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) {
522 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) {
524 rtsp_t *s;
525 int i;
527 if (fd < 0) {
528 mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", host);
529 return NULL;
532 s = malloc(sizeof(rtsp_t));
534 for (i=0; i<MAX_FIELDS; i++) {
535 s->answers[i]=NULL;
536 s->scheduled[i]=NULL;
539 s->s = fd;
540 s->server=NULL;
541 s->server_state=0;
542 s->server_caps=0;
544 s->cseq=0;
545 s->session=NULL;
547 if (user_agent)
548 s->user_agent=strdup(user_agent);
549 else
550 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)");
552 s->mrl = strdup(mrl);
553 s->host = strdup(host);
554 s->port = port;
555 s->path = strdup(path);
556 while (*path == '/')
557 path++;
558 if ((s->param = strchr(s->path, '?')) != NULL)
559 s->param++;
560 //mp_msg(MSGT_OPEN, MSGL_INFO, "path=%s\n", s->path);
561 //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL");
563 s->server_state=RTSP_CONNECTED;
565 /* now let's send an options request. */
566 rtsp_schedule_field(s, "CSeq: 1");
567 rtsp_schedule_field(s, s->user_agent);
568 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7");
569 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]");
570 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw==");
571 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000");
572 rtsp_schedule_field(s, "RegionData: 0");
573 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
574 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/
575 rtsp_request_options(s, NULL);
577 return s;
582 * search in answers for tags. returns a pointer to the content
583 * after the first matched tag. returns NULL if no match found.
586 char *rtsp_search_answers(rtsp_t *s, const char *tag) {
588 char **answer;
589 char *ptr;
591 if (!s->answers) return NULL;
592 answer=s->answers;
594 while (*answer) {
595 if (!strncasecmp(*answer,tag,strlen(tag))) {
596 ptr=strchr(*answer,':');
597 if (!ptr) return NULL;
598 ptr++;
599 while(*ptr==' ') ptr++;
600 return ptr;
602 answer++;
605 return NULL;
609 * session id management
612 void rtsp_set_session(rtsp_t *s, const char *id) {
614 if (s->session) free(s->session);
616 s->session=strdup(id);
620 char *rtsp_get_session(rtsp_t *s) {
622 return s->session;
626 char *rtsp_get_mrl(rtsp_t *s) {
628 return s->mrl;
632 char *rtsp_get_param(rtsp_t *s, const char *p) {
633 int len;
634 char *param;
635 if (!s->param)
636 return NULL;
637 if (!p)
638 return strdup(s->param);
639 len = strlen(p);
640 param = s->param;
641 while (param && *param) {
642 char *nparam = strchr(param, '&');
643 if (strncmp(param, p, len) == 0 && param[len] == '=') {
644 param += len + 1;
645 len = nparam ? nparam - param : strlen(param);
646 nparam = malloc(len + 1);
647 memcpy(nparam, param, len);
648 nparam[len] = 0;
649 return nparam;
651 param = nparam ? nparam + 1 : NULL;
653 return NULL;
657 * schedules a field for transmission
660 void rtsp_schedule_field(rtsp_t *s, const char *string) {
662 int i=0;
664 if (!string) return;
666 while(s->scheduled[i]) {
667 i++;
669 s->scheduled[i]=strdup(string);
673 * removes the first scheduled field which prefix matches string.
676 void rtsp_unschedule_field(rtsp_t *s, const char *string) {
678 char **ptr=s->scheduled;
680 if (!string) return;
682 while(*ptr) {
683 if (!strncmp(*ptr, string, strlen(string)))
684 break;
685 else
686 ptr++;
688 if (*ptr) free(*ptr);
689 ptr++;
690 do {
691 *(ptr-1)=*ptr;
692 } while(*ptr);
696 * unschedule all fields
699 void rtsp_unschedule_all(rtsp_t *s) {
701 char **ptr;
703 if (!s->scheduled) return;
704 ptr=s->scheduled;
706 while (*ptr) {
707 free(*ptr);
708 *ptr=NULL;
709 ptr++;
713 * free answers
716 void rtsp_free_answers(rtsp_t *s) {
718 char **answer;
720 if (!s->answers) return;
721 answer=s->answers;
723 while (*answer) {
724 free(*answer);
725 *answer=NULL;
726 answer++;