From 49867bd432352d19172ab26cf873bd5651e69e25 Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 31 Jul 2006 17:39:17 +0000 Subject: introduce new 'stream' directory for all stream layer related components and split them from libmpdemux git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@19277 b3059339-0415-0410-9bf9-f77b7e298cf2 --- stream/librtsp/rtsp.c | 804 ++++++++++++++++++++++++++++++++++++++++++ stream/librtsp/rtsp.h | 84 +++++ stream/librtsp/rtsp_rtp.c | 700 ++++++++++++++++++++++++++++++++++++ stream/librtsp/rtsp_rtp.h | 41 +++ stream/librtsp/rtsp_session.c | 269 ++++++++++++++ stream/librtsp/rtsp_session.h | 42 +++ 6 files changed, 1940 insertions(+) create mode 100644 stream/librtsp/rtsp.c create mode 100644 stream/librtsp/rtsp.h create mode 100644 stream/librtsp/rtsp_rtp.c create mode 100644 stream/librtsp/rtsp_rtp.h create mode 100644 stream/librtsp/rtsp_session.c create mode 100644 stream/librtsp/rtsp_session.h (limited to 'stream/librtsp') diff --git a/stream/librtsp/rtsp.c b/stream/librtsp/rtsp.c new file mode 100644 index 0000000000..16a77f77d5 --- /dev/null +++ b/stream/librtsp/rtsp.c @@ -0,0 +1,804 @@ +/* + * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48 + */ + +/* + * Copyright (C) 2000-2002 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * + * a minimalistic implementation of rtsp protocol, + * *not* RFC 2326 compilant yet. + * + * 2006, Benjamin Zores and Vincent Mussard + * fixed a lot of RFC compliance issues. + */ + +#include +#include +#include +#include "config.h" +#ifndef HAVE_WINSOCK2 +#define closesocket close +#include +#include +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mp_msg.h" +#include "rtsp.h" +#include "rtsp_session.h" +#include "osdep/timer.h" + +/* +#define LOG +*/ + +#define BUF_SIZE 4096 +#define HEADER_SIZE 1024 +#define MAX_FIELDS 256 + +struct rtsp_s { + + int s; + + char *host; + int port; + char *path; + char *param; + char *mrl; + char *user_agent; + + char *server; + unsigned int server_state; + uint32_t server_caps; + + unsigned int cseq; + char *session; + + char *answers[MAX_FIELDS]; /* data of last message */ + char *scheduled[MAX_FIELDS]; /* will be sent with next message */ +}; + +/* + * constants + */ + +#define RTSP_PROTOCOL_VERSION "RTSP/1.0" + +/* server states */ +#define RTSP_CONNECTED 1 +#define RTSP_INIT 2 +#define RTSP_READY 4 +#define RTSP_PLAYING 8 +#define RTSP_RECORDING 16 + +/* server capabilities */ +#define RTSP_OPTIONS 0x001 +#define RTSP_DESCRIBE 0x002 +#define RTSP_ANNOUNCE 0x004 +#define RTSP_SETUP 0x008 +#define RTSP_GET_PARAMETER 0x010 +#define RTSP_SET_PARAMETER 0x020 +#define RTSP_TEARDOWN 0x040 +#define RTSP_PLAY 0x080 +#define RTSP_RECORD 0x100 + +/* + * network utilities + */ + +static int write_stream(int s, const char *buf, int len) { + int total, timeout; + + total = 0; timeout = 30; + while (total < len){ + int n; + + n = send (s, &buf[total], len - total, 0); + + if (n > 0) + total += n; + else if (n < 0) { +#ifndef HAVE_WINSOCK2 + if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { +#else + if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { +#endif + usec_sleep (1000000); timeout--; + } else + return -1; + } + } + + return total; +} + +static ssize_t read_stream(int fd, void *buf, size_t count) { + + ssize_t ret, total; + + total = 0; + + while (total < count) { + + ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); + + if (ret<0) { + if(errno == EAGAIN) { + fd_set rset; + struct timeval timeout; + + FD_ZERO (&rset); + FD_SET (fd, &rset); + + timeout.tv_sec = 30; + timeout.tv_usec = 0; + + if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { + return -1; + } + continue; + } + + mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: read error.\n"); + return ret; + } else + total += ret; + + /* end of stream */ + if (!ret) break; + } + + return total; +} + +/* + * rtsp_get gets a line from stream + * and returns a null terminated string. + */ + +static char *rtsp_get(rtsp_t *s) { + + int n=1; + char *buffer = malloc(BUF_SIZE); + char *string = NULL; + + read_stream(s->s, buffer, 1); + while (ns, &(buffer[n]), 1); + if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; + n++; + } + + if (n>=BUF_SIZE) { + mp_msg(MSGT_OPEN, MSGL_FATAL, "librtsp: buffer overflow in rtsp_get\n"); + exit(1); + } + string=malloc(n); + memcpy(string,buffer,n-1); + string[n-1]=0; + +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << '%s'\n", string); +#endif + + + free(buffer); + return string; +} + +/* + * rtsp_put puts a line on stream + */ + +static void rtsp_put(rtsp_t *s, const char *string) { + + int len=strlen(string); + char *buf=malloc(len+2); + +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: >> '%s'", string); +#endif + + memcpy(buf,string,len); + buf[len]=0x0d; + buf[len+1]=0x0a; + + write_stream(s->s, buf, len+2); + +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_INFO, " done.\n"); +#endif + + free(buf); +} + +/* + * extract server status code + */ + +static int rtsp_get_code(const char *string) { + + char buf[4]; + int code=0; + + if (!strncmp(string, RTSP_PROTOCOL_VERSION, strlen(RTSP_PROTOCOL_VERSION))) + { + memcpy(buf, string+strlen(RTSP_PROTOCOL_VERSION)+1, 3); + buf[3]=0; + code=atoi(buf); + } else if (!strncmp(string, RTSP_METHOD_SET_PARAMETER,8)) + { + return RTSP_STATUS_SET_PARAMETER; + } + + if(code != RTSP_STATUS_OK) mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: server responds: '%s'\n",string); + + return code; +} + +/* + * send a request + */ + +static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { + + char **payload=s->scheduled; + char *buf; + + buf = malloc(strlen(type)+strlen(what)+strlen(RTSP_PROTOCOL_VERSION)+3); + + sprintf(buf,"%s %s %s",type, what, RTSP_PROTOCOL_VERSION); + rtsp_put(s,buf); + free(buf); + if (payload) + while (*payload) { + rtsp_put(s,*payload); + payload++; + } + rtsp_put(s,""); + rtsp_unschedule_all(s); +} + +/* + * schedule standard fields + */ + +static void rtsp_schedule_standard(rtsp_t *s) { + + char tmp[17]; + + snprintf(tmp, 17, "CSeq: %u", s->cseq); + rtsp_schedule_field(s, tmp); + + if (s->session) { + char *buf; + buf = malloc(strlen(s->session)+15); + sprintf(buf, "Session: %s", s->session); + rtsp_schedule_field(s, buf); + free(buf); + } +} +/* + * get the answers, if server responses with something != 200, return NULL + */ + +static int rtsp_get_answers(rtsp_t *s) { + + char *answer=NULL; + unsigned int answer_seq; + char **answer_ptr=s->answers; + int code; + int ans_count = 0; + + answer=rtsp_get(s); + if (!answer) + return 0; + code=rtsp_get_code(answer); + free(answer); + + rtsp_free_answers(s); + + do { /* while we get answer lines */ + + answer=rtsp_get(s); + if (!answer) + return 0; + + if (!strncasecmp(answer,"CSeq:",5)) { + sscanf(answer,"%*s %u",&answer_seq); + if (s->cseq != answer_seq) { +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_WARN, "librtsp: warning: CSeq mismatch. got %u, assumed %u", answer_seq, s->cseq); +#endif + s->cseq=answer_seq; + } + } + if (!strncasecmp(answer,"Server:",7)) { + char *buf = malloc(strlen(answer)); + sscanf(answer,"%*s %s",buf); + if (s->server) free(s->server); + s->server=strdup(buf); + free(buf); + } + if (!strncasecmp(answer,"Session:",8)) { + char *buf = calloc(1, strlen(answer)); + sscanf(answer,"%*s %s",buf); + if (s->session) { + if (strcmp(buf, s->session)) { + mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: setting NEW session: %s\n", buf); + free(s->session); + s->session=strdup(buf); + } + } else + { +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_INFO, "rtsp: setting session id to: %s\n", buf); +#endif + s->session=strdup(buf); + } + free(buf); + } + *answer_ptr=answer; + answer_ptr++; + } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS)); + + s->cseq++; + + *answer_ptr=NULL; + rtsp_schedule_standard(s); + + return code; +} + +/* + * send an ok message + */ + +int rtsp_send_ok(rtsp_t *s) { + char cseq[16]; + + rtsp_put(s, "RTSP/1.0 200 OK"); + sprintf(cseq,"CSeq: %u", s->cseq); + rtsp_put(s, cseq); + rtsp_put(s, ""); + return 0; +} + +/* + * implementation of must-have rtsp requests; functions return + * server status code. + */ + +int rtsp_request_options(rtsp_t *s, const char *what) { + + char *buf; + + if (what) { + buf=strdup(what); + } else + { + buf=malloc(strlen(s->host)+16); + sprintf(buf,"rtsp://%s:%i", s->host, s->port); + } + rtsp_send_request(s,RTSP_METHOD_OPTIONS,buf); + free(buf); + + return rtsp_get_answers(s); +} + +int rtsp_request_describe(rtsp_t *s, const char *what) { + + char *buf; + + if (what) { + buf=strdup(what); + } else + { + buf=malloc(strlen(s->host)+strlen(s->path)+16); + sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); + } + rtsp_send_request(s,RTSP_METHOD_DESCRIBE,buf); + free(buf); + + return rtsp_get_answers(s); +} + +int rtsp_request_setup(rtsp_t *s, const char *what, char *control) { + + char *buf = NULL; + + if (what) + buf = strdup (what); + else + { + int len = strlen (s->host) + strlen (s->path) + 16; + if (control) + len += strlen (control) + 1; + + buf = malloc (len); + sprintf (buf, "rtsp://%s:%i/%s%s%s", s->host, s->port, s->path, + control ? "/" : "", control ? control : ""); + } + + rtsp_send_request (s, RTSP_METHOD_SETUP, buf); + free (buf); + return rtsp_get_answers (s); +} + +int rtsp_request_setparameter(rtsp_t *s, const char *what) { + + char *buf; + + if (what) { + buf=strdup(what); + } else + { + buf=malloc(strlen(s->host)+strlen(s->path)+16); + sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); + } + rtsp_send_request(s,RTSP_METHOD_SET_PARAMETER,buf); + free(buf); + + return rtsp_get_answers(s); +} + +int rtsp_request_play(rtsp_t *s, const char *what) { + + char *buf; + int ret; + + if (what) { + buf=strdup(what); + } else + { + buf=malloc(strlen(s->host)+strlen(s->path)+16); + sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); + } + rtsp_send_request(s,RTSP_METHOD_PLAY,buf); + free(buf); + + ret = rtsp_get_answers (s); + if (ret == RTSP_STATUS_OK) + s->server_state = RTSP_PLAYING; + + return ret; +} + +int rtsp_request_teardown(rtsp_t *s, const char *what) { + + char *buf; + + if (what) + buf = strdup (what); + else + { + buf = + malloc (strlen (s->host) + strlen (s->path) + 16); + sprintf (buf, "rtsp://%s:%i/%s", s->host, s->port, s->path); + } + rtsp_send_request (s, RTSP_METHOD_TEARDOWN, buf); + free (buf); + + /* after teardown we're done with RTSP streaming, no need to get answer as + reading more will only result to garbage and buffer overflow */ + return RTSP_STATUS_OK; +} + +/* + * read opaque data from stream + */ + +int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { + + int i,seq; + + if (size>=4) { + i=read_stream(s->s, buffer, 4); + if (i<4) return i; + if (((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) || + ((buffer[0]=='O')&&(buffer[1]=='P')&&(buffer[2]=='T')&&(buffer[3]=='I'))) // OPTIONS + { + char *rest=rtsp_get(s); + if (!rest) + return -1; + + seq=-1; + do { + free(rest); + rest=rtsp_get(s); + if (!rest) + return -1; + if (!strncasecmp(rest,"CSeq:",5)) + sscanf(rest,"%*s %u",&seq); + } while (strlen(rest)!=0); + free(rest); + if (seq<0) { +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_WARN, "rtsp: warning: CSeq not recognized!\n"); +#endif + seq=1; + } + /* let's make the server happy */ + rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); + rest=malloc(17); + sprintf(rest,"CSeq: %u", seq); + rtsp_put(s, rest); + free(rest); + rtsp_put(s, ""); + i=read_stream(s->s, buffer, size); + } else + { + i=read_stream(s->s, buffer+4, size-4); + i+=4; + } + } else + i=read_stream(s->s, buffer, size); +#ifdef LOG + mp_msg(MSGT_OPEN, MSGL_INFO, "librtsp: << %d of %d bytes\n", i, size); +#endif + + return i; +} + +/* + * connect to a rtsp server + */ + +//rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { +rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { + + rtsp_t *s=malloc(sizeof(rtsp_t)); + int i; + + for (i=0; ianswers[i]=NULL; + s->scheduled[i]=NULL; + } + + s->server=NULL; + s->server_state=0; + s->server_caps=0; + + s->cseq=0; + s->session=NULL; + + if (user_agent) + s->user_agent=strdup(user_agent); + else + s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); + + s->mrl = strdup(mrl); + s->host = strdup(host); + s->port = port; + s->path = strdup(path); + while (*path == '/') + path++; + if ((s->param = strchr(s->path, '?')) != NULL) + s->param++; + //mp_msg(MSGT_OPEN, MSGL_INFO, "path=%s\n", s->path); + //mp_msg(MSGT_OPEN, MSGL_INFO, "param=%s\n", s->param ? s->param : "NULL"); + s->s = fd; + + if (s->s < 0) { + mp_msg(MSGT_OPEN, MSGL_ERR, "rtsp: failed to connect to '%s'\n", s->host); + rtsp_close(s); + return NULL; + } + + s->server_state=RTSP_CONNECTED; + + /* now let's send an options request. */ + rtsp_schedule_field(s, "CSeq: 1"); + rtsp_schedule_field(s, s->user_agent); + rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); + rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); + rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); + rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); + rtsp_schedule_field(s, "RegionData: 0"); + rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); + /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ + rtsp_request_options(s, NULL); + + return s; +} + + +/* + * closes an rtsp connection + */ + +void rtsp_close(rtsp_t *s) { + + if (s->server_state) + { + if (s->server_state == RTSP_PLAYING) + rtsp_request_teardown (s, NULL); + closesocket (s->s); + } + + if (s->path) free(s->path); + if (s->host) free(s->host); + if (s->mrl) free(s->mrl); + if (s->session) free(s->session); + if (s->user_agent) free(s->user_agent); + rtsp_free_answers(s); + rtsp_unschedule_all(s); + free(s); +} + +/* + * search in answers for tags. returns a pointer to the content + * after the first matched tag. returns NULL if no match found. + */ + +char *rtsp_search_answers(rtsp_t *s, const char *tag) { + + char **answer; + char *ptr; + + if (!s->answers) return NULL; + answer=s->answers; + + while (*answer) { + if (!strncasecmp(*answer,tag,strlen(tag))) { + ptr=strchr(*answer,':'); + if (!ptr) return NULL; + ptr++; + while(*ptr==' ') ptr++; + return ptr; + } + answer++; + } + + return NULL; +} + +/* + * session id management + */ + +void rtsp_set_session(rtsp_t *s, const char *id) { + + if (s->session) free(s->session); + + s->session=strdup(id); + +} + +char *rtsp_get_session(rtsp_t *s) { + + return s->session; + +} + +char *rtsp_get_mrl(rtsp_t *s) { + + return s->mrl; + +} + +char *rtsp_get_param(rtsp_t *s, const char *p) { + int len; + char *param; + if (!s->param) + return NULL; + if (!p) + return strdup(s->param); + len = strlen(p); + param = s->param; + while (param && *param) { + char *nparam = strchr(param, '&'); + if (strncmp(param, p, len) == 0 && param[len] == '=') { + param += len + 1; + len = nparam ? nparam - param : strlen(param); + nparam = malloc(len + 1); + memcpy(nparam, param, len); + nparam[len] = 0; + return nparam; + } + param = nparam ? nparam + 1 : NULL; + } + return NULL; +} + +/* + * schedules a field for transmission + */ + +void rtsp_schedule_field(rtsp_t *s, const char *string) { + + int i=0; + + if (!string) return; + + while(s->scheduled[i]) { + i++; + } + s->scheduled[i]=strdup(string); +} + +/* + * removes the first scheduled field which prefix matches string. + */ + +void rtsp_unschedule_field(rtsp_t *s, const char *string) { + + char **ptr=s->scheduled; + + if (!string) return; + + while(*ptr) { + if (!strncmp(*ptr, string, strlen(string))) + break; + else + ptr++; + } + if (*ptr) free(*ptr); + ptr++; + do { + *(ptr-1)=*ptr; + } while(*ptr); +} + +/* + * unschedule all fields + */ + +void rtsp_unschedule_all(rtsp_t *s) { + + char **ptr; + + if (!s->scheduled) return; + ptr=s->scheduled; + + while (*ptr) { + free(*ptr); + *ptr=NULL; + ptr++; + } +} +/* + * free answers + */ + +void rtsp_free_answers(rtsp_t *s) { + + char **answer; + + if (!s->answers) return; + answer=s->answers; + + while (*answer) { + free(*answer); + *answer=NULL; + answer++; + } +} diff --git a/stream/librtsp/rtsp.h b/stream/librtsp/rtsp.h new file mode 100644 index 0000000000..eefdd79d51 --- /dev/null +++ b/stream/librtsp/rtsp.h @@ -0,0 +1,84 @@ +/* + * This file was ported to MPlayer from xine CVS rtsp.h,v 1.2 2002/12/16 21:50:55 + */ + +/* + * Copyright (C) 2002 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * + * a minimalistic implementation of rtsp protocol, + * *not* RFC 2326 compilant yet. + * + * 2006, Benjamin Zores and Vincent Mussard + * fixed a lot of RFC compliance issues. + */ + +#ifndef HAVE_RTSP_H +#define HAVE_RTSP_H + + +/* some codes returned by rtsp_request_* functions */ + +#define RTSP_STATUS_SET_PARAMETER 10 +#define RTSP_STATUS_OK 200 + +#define RTSP_METHOD_OPTIONS "OPTIONS" +#define RTSP_METHOD_DESCRIBE "DESCRIBE" +#define RTSP_METHOD_SETUP "SETUP" +#define RTSP_METHOD_PLAY "PLAY" +#define RTSP_METHOD_TEARDOWN "TEARDOWN" +#define RTSP_METHOD_SET_PARAMETER "SET_PARAMETER" + +typedef struct rtsp_s rtsp_t; + +rtsp_t* rtsp_connect (int fd, char *mrl, char *path, char *host, int port, char *user_agent); + +int rtsp_request_options(rtsp_t *s, const char *what); +int rtsp_request_describe(rtsp_t *s, const char *what); +int rtsp_request_setup(rtsp_t *s, const char *what, char *control); +int rtsp_request_setparameter(rtsp_t *s, const char *what); +int rtsp_request_play(rtsp_t *s, const char *what); +int rtsp_request_teardown(rtsp_t *s, const char *what); + +int rtsp_send_ok(rtsp_t *s); + +int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size); + +char* rtsp_search_answers(rtsp_t *s, const char *tag); +void rtsp_add_to_payload(char **payload, const char *string); + +void rtsp_free_answers(rtsp_t *this); + +int rtsp_read (rtsp_t *this, char *data, int len); +void rtsp_close (rtsp_t *this); + +void rtsp_set_session(rtsp_t *s, const char *id); +char *rtsp_get_session(rtsp_t *s); + +char *rtsp_get_mrl(rtsp_t *s); +char *rtsp_get_param(rtsp_t *s, const char *param); + +/*int rtsp_peek_header (rtsp_t *this, char *data); */ + +void rtsp_schedule_field(rtsp_t *s, const char *string); +void rtsp_unschedule_field(rtsp_t *s, const char *string); +void rtsp_unschedule_all(rtsp_t *s); + +#endif + diff --git a/stream/librtsp/rtsp_rtp.c b/stream/librtsp/rtsp_rtp.c new file mode 100644 index 0000000000..b6cfd065a1 --- /dev/null +++ b/stream/librtsp/rtsp_rtp.c @@ -0,0 +1,700 @@ +/* + * Copyright (C) 2006 Benjamin Zores + * based on the Freebox patch for xine by Vincent Mussard + * but with many enhancements for better RTSP RFC compliance. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#ifndef HAVE_WINSOCK2 +#include +#include +#include +#include +#else +#include +#include +#endif + +#include "mp_msg.h" +#include "rtsp.h" +#include "rtsp_rtp.h" +#include "rtsp_session.h" +#include "../freesdp/common.h" +#include "../freesdp/parser.h" + +#define RTSP_DEFAULT_PORT 31336 +#define MAX_LENGTH 256 + +#define RTSP_ACCEPT_SDP "Accept: application/sdp" +#define RTSP_CONTENT_LENGTH "Content-length" +#define RTSP_CONTENT_TYPE "Content-Type" +#define RTSP_APPLICATION_SDP "application/sdp" +#define RTSP_RANGE "Range: " +#define RTSP_NPT_NOW "npt=now-" +#define RTSP_MEDIA_CONTAINER_MPEG_TS "33" +#define RTSP_TRANSPORT_REQUEST "Transport: RTP/AVP;%s;%s%i-%i;mode=\"PLAY\"" + +#define RTSP_TRANSPORT_MULTICAST "multicast" +#define RTSP_TRANSPORT_UNICAST "unicast" + +#define RTSP_MULTICAST_PORT "port=" +#define RTSP_UNICAST_CLIENT_PORT "client_port=" +#define RTSP_UNICAST_SERVER_PORT "server_port=" +#define RTSP_SETUP_DESTINATION "destination=" + +#define RTSP_SESSION "Session" +#define RTSP_TRANSPORT "Transport" + +/* hardcoded RTCP RR - this is _NOT_ RFC compliant */ +#define RTCP_RR_SIZE 32 +#define RTCP_RR "\201\311\0\7(.JD\31+\306\343\0\0\0\0\0\0/E\0\0\2&\0\0\0\0\0\0\0\0\201" +#define RTCP_SEND_FREQUENCY 1024 + +int rtsp_port = 0; +char *rtsp_destination = NULL; + +void +rtcp_send_rr (rtsp_t *s, struct rtp_rtsp_session_t *st) +{ + if (st->rtcp_socket == -1) + return; + + /* send RTCP RR every RTCP_SEND_FREQUENCY packets + * FIXME : NOT CORRECT, HARDCODED, BUT MAKES SOME SERVERS HAPPY + * not rfc compliant + * http://www.faqs.org/rfcs/rfc1889.html chapter 6 for RTCP + */ + + if (st->count == RTCP_SEND_FREQUENCY) + { + char rtcp_content[RTCP_RR_SIZE]; + strcpy (rtcp_content, RTCP_RR); + send (st->rtcp_socket, rtcp_content, RTCP_RR_SIZE, 0); + + /* ping RTSP server to keep connection alive. + we use OPTIONS instead of PING as not all servers support it */ + rtsp_request_options (s, "*"); + st->count = 0; + } + else + st->count++; +} + +static struct rtp_rtsp_session_t * +rtp_session_new (void) +{ + struct rtp_rtsp_session_t *st = NULL; + + st = malloc (sizeof (struct rtp_rtsp_session_t)); + + st->rtp_socket = -1; + st->rtcp_socket = -1; + st->control_url = NULL; + st->count = 0; + + return st; +} + +void +rtp_session_free (struct rtp_rtsp_session_t *st) +{ + if (!st) + return; + + if (st->rtp_socket != -1) + close (st->rtp_socket); + if (st->rtcp_socket != -1) + close (st->rtcp_socket); + + if (st->control_url) + free (st->control_url); + free (st); +} + +static void +rtp_session_set_fd (struct rtp_rtsp_session_t *st, + int rtp_sock, int rtcp_sock) +{ + if (!st) + return; + + st->rtp_socket = rtp_sock; + st->rtcp_socket = rtcp_sock; +} + +static int +parse_port (const char *line, const char *param, + int *rtp_port, int *rtcp_port) +{ + char *parse1; + char *parse2; + char *parse3; + + char *line_copy = strdup (line); + + parse1 = strstr (line_copy, param); + + if (parse1) + { + parse2 = strstr (parse1, "-"); + + if (parse2) + { + parse3 = strstr (parse2, ";"); + + if (parse3) + parse3[0] = 0; + + parse2[0] = 0; + } + else + { + free (line_copy); + return 0; + } + } + else + { + free (line_copy); + return 0; + } + + *rtp_port = atoi (parse1 + strlen (param)); + *rtcp_port = atoi (parse2 + 1); + + free (line_copy); + + return 1; +} + +static char * +parse_destination (const char *line) +{ + char *parse1; + char *parse2; + + char *dest = NULL; + char *line_copy = strdup (line); + int len; + + parse1 = strstr (line_copy, RTSP_SETUP_DESTINATION); + if (!parse1) + { + free (line_copy); + return NULL; + } + + parse2 = strstr (parse1, ";"); + if (!parse2) + { + free (line_copy); + return NULL; + } + + len = strlen (parse1) - strlen (parse2) + - strlen (RTSP_SETUP_DESTINATION) + 1; + dest = (char *) malloc (len + 1); + snprintf (dest, len, parse1 + strlen (RTSP_SETUP_DESTINATION)); + free (line_copy); + + return dest; +} + +static int +rtcp_connect (int client_port, int server_port, const char* server_hostname) +{ + struct sockaddr_in sin; + struct hostent *hp; + int s; + + if (client_port <= 1023) + return -1; + + s = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (s == -1) + return -1; + + hp = gethostbyname (server_hostname); + if (!hp) + { + close (s); + return -1; + } + + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_port = htons (client_port); + + if (bind (s, (struct sockaddr *) &sin, sizeof (sin))) + { +#ifndef HAVE_WINSOCK2 + if (errno != EINPROGRESS) +#else + if (WSAGetLastError() != WSAEINPROGRESS) +#endif + { + close (s); + return -1; + } + } + + sin.sin_family = AF_INET; + memcpy (&(sin.sin_addr.s_addr), hp->h_addr, sizeof (hp->h_addr)); + sin.sin_port = htons (server_port); + + /* datagram socket */ + if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0) + { + close (s); + return -1; + } + + return s; +} + +static int +rtp_connect (char *hostname, int port) +{ + struct sockaddr_in sin; + struct timeval tv; + int err, err_len; + int rxsockbufsz; + int s; + fd_set set; + + if (port <= 1023) + return -1; + + s = socket (PF_INET, SOCK_DGRAM, 0); + if (s == -1) + return -1; + + sin.sin_family = AF_INET; + if (!hostname || !strcmp (hostname, "0.0.0.0")) + sin.sin_addr.s_addr = htonl (INADDR_ANY); + else +#ifndef HAVE_WINSOCK2 +#ifdef USE_ATON + inet_aton (hostname, &sin.sin_addr); +#else + inet_pton (AF_INET, hostname, &sin.sin_addr); +#endif +#else + sin.sin_addr.s_addr = htonl (INADDR_ANY); +#endif + sin.sin_port = htons (port); + + /* Increase the socket rx buffer size to maximum -- this is UDP */ + rxsockbufsz = 240 * 1024; + if (setsockopt (s, SOL_SOCKET, SO_RCVBUF, + &rxsockbufsz, sizeof (rxsockbufsz))) + mp_msg (MSGT_OPEN, MSGL_ERR, "Couldn't set receive socket buffer size\n"); + + /* if multicast address, add membership */ + if ((ntohl (sin.sin_addr.s_addr) >> 28) == 0xe) + { + struct ip_mreq mcast; + mcast.imr_multiaddr.s_addr = sin.sin_addr.s_addr; + mcast.imr_interface.s_addr = 0; + + if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof (mcast))) + { + mp_msg (MSGT_OPEN, MSGL_ERR, "IP_ADD_MEMBERSHIP failed\n"); + close (s); + return -1; + } + } + + /* datagram socket */ + if (bind (s, (struct sockaddr *) &sin, sizeof (sin))) + { +#ifndef HAVE_WINSOCK2 + if (errno != EINPROGRESS) +#else + if (WSAGetLastError() != WSAEINPROGRESS) +#endif + { + mp_msg (MSGT_OPEN, MSGL_ERR, "bind: %s\n", strerror (errno)); + close (s); + return -1; + } + } + + tv.tv_sec = 0; + tv.tv_usec = (1 * 1000000); /* 1 second timeout */ + + FD_ZERO (&set); + FD_SET (s, &set); + + err = select (s + 1, &set, NULL, NULL, &tv); + if (err < 0) + { + mp_msg (MSGT_OPEN, MSGL_ERR, "Select failed: %s\n", strerror (errno)); + close (s); + return -1; + } + else if (err == 0) + { + mp_msg (MSGT_OPEN, MSGL_ERR, "Timeout! No data from host %s\n", hostname); + close (s); + return -1; + } + + err_len = sizeof (err); + getsockopt (s, SOL_SOCKET, SO_ERROR, &err, (socklen_t *) &err_len); + if (err) + { + mp_msg (MSGT_OPEN, MSGL_ERR, "Socket error: %d\n", err); + close (s); + return -1; + } + + return s; +} + +static int +is_multicast_address (char *addr) +{ + struct sockaddr_in sin; + + if (!addr) + return -1; + + sin.sin_family = AF_INET; + +#ifndef HAVE_WINSOCK2 +#ifdef USE_ATON + inet_aton (addr, &sin.sin_addr); +#else + inet_pton (AF_INET, addr, &sin.sin_addr); +#endif +#else + sin.sin_addr.s_addr = htonl (INADDR_ANY); +#endif + + if ((ntohl (sin.sin_addr.s_addr) >> 28) == 0xe) + return 1; + + return 0; +} + +struct rtp_rtsp_session_t * +rtp_setup_and_play (rtsp_t *rtsp_session) +{ + struct rtp_rtsp_session_t* rtp_session = NULL; + const fsdp_media_description_t *med_dsc = NULL; + char temp_buf[MAX_LENGTH + 1]; + char npt[256]; + + char* answer; + char* sdp; + char *server_addr = NULL; + char *destination = NULL; + + int statut; + int content_length = 0; + int is_multicast = 0; + + fsdp_description_t *dsc = NULL; + fsdp_error_t result; + + int client_rtp_port = -1; + int client_rtcp_port = -1; + int server_rtp_port = -1; + int server_rtcp_port = -1; + int rtp_sock = -1; + int rtcp_sock = -1; + + /* 1. send a RTSP DESCRIBE request to server */ + rtsp_schedule_field (rtsp_session, RTSP_ACCEPT_SDP); + statut = rtsp_request_describe (rtsp_session, NULL); + if (statut < 200 || statut > 299) + return NULL; + + answer = rtsp_search_answers (rtsp_session, RTSP_CONTENT_LENGTH); + if (answer) + content_length = atoi (answer); + else + return NULL; + + answer = rtsp_search_answers (rtsp_session, RTSP_CONTENT_TYPE); + if (!answer || !strstr (answer, RTSP_APPLICATION_SDP)) + return NULL; + + /* 2. read SDP message from server */ + sdp = (char *) malloc (content_length + 1); + if (rtsp_read_data (rtsp_session, sdp, content_length) <= 0) + { + free (sdp); + return NULL; + } + sdp[content_length] = 0; + + /* 3. parse SDP message */ + dsc = fsdp_description_new (); + result = fsdp_parse (sdp, dsc); + if (result != FSDPE_OK) + { + free (sdp); + fsdp_description_delete (dsc); + return NULL; + } + mp_msg (MSGT_OPEN, MSGL_V, "SDP:\n%s\n", sdp); + free (sdp); + + /* 4. check for number of media streams: only one is supported */ + if (fsdp_get_media_count (dsc) != 1) + { + mp_msg (MSGT_OPEN, MSGL_ERR, + "A single media stream only is supported atm.\n"); + fsdp_description_delete (dsc); + return NULL; + } + + /* 5. set the Normal Play Time parameter + * use range provided by server in SDP or start now if empty */ + sprintf (npt, RTSP_RANGE); + if (fsdp_get_range (dsc)) + strcat (npt, fsdp_get_range (dsc)); + else + strcat (npt, RTSP_NPT_NOW); + + /* 5. check for a valid media stream */ + med_dsc = fsdp_get_media (dsc, 0); + if (!med_dsc) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* 6. parse the `m= ' line */ + + /* check for an A/V media */ + if (fsdp_get_media_type (med_dsc) != FSDP_MEDIA_VIDEO && + fsdp_get_media_type (med_dsc) != FSDP_MEDIA_AUDIO) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* only RTP/AVP transport method is supported right now */ + if (fsdp_get_media_transport_protocol (med_dsc) != FSDP_TP_RTP_AVP) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* only MPEG-TS is supported at the moment */ + if (!strstr (fsdp_get_media_format (med_dsc, 0), + RTSP_MEDIA_CONTAINER_MPEG_TS)) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* get client port (if any) advised by server */ + client_rtp_port = fsdp_get_media_port (med_dsc); + if (client_rtp_port == -1) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* if client_rtp_port = 0 => let client randomly pick one */ + if (client_rtp_port == 0) + { + /* TODO: we should check if the port is in use first */ + if (rtsp_port) + client_rtp_port = rtsp_port; + else + client_rtp_port = RTSP_DEFAULT_PORT; + } + + /* RTCP port generally is RTP port + 1 */ + client_rtcp_port = client_rtp_port + 1; + + mp_msg (MSGT_OPEN, MSGL_V, + "RTP Port from SDP appears to be: %d\n", client_rtp_port); + mp_msg (MSGT_OPEN, MSGL_V, + "RTCP Port from SDP appears to be: %d\n", client_rtcp_port); + + /* 7. parse the `c= ' line */ + + /* check for a valid media network type (inet) */ + if (fsdp_get_media_network_type (med_dsc) != FSDP_NETWORK_TYPE_INET) + { + /* no control for media: try global one instead */ + if (fsdp_get_global_conn_network_type (dsc) != FSDP_NETWORK_TYPE_INET) + { + fsdp_description_delete (dsc); + return NULL; + } + } + + /* only IPv4 is supported atm. */ + if (fsdp_get_media_address_type (med_dsc) != FSDP_ADDRESS_TYPE_IPV4) + { + /* no control for media: try global one instead */ + if (fsdp_get_global_conn_address_type (dsc) != FSDP_ADDRESS_TYPE_IPV4) + { + fsdp_description_delete (dsc); + return NULL; + } + } + + /* get the media server address to connect to */ + if (fsdp_get_media_address (med_dsc)) + server_addr = strdup (fsdp_get_media_address (med_dsc)); + else if (fsdp_get_global_conn_address (dsc)) + { + /* no control for media: try global one instead */ + server_addr = strdup (fsdp_get_global_conn_address (dsc)); + } + + if (!server_addr) + { + fsdp_description_delete (dsc); + return NULL; + } + + /* check for a UNICAST or MULTICAST address to connect to */ + is_multicast = is_multicast_address (server_addr); + + /* 8. initiate an RTP session */ + rtp_session = rtp_session_new (); + if (!rtp_session) + { + free (server_addr); + fsdp_description_delete (dsc); + return NULL; + } + + /* get the media control URL */ + if (fsdp_get_media_control (med_dsc, 0)) + rtp_session->control_url = strdup (fsdp_get_media_control (med_dsc, 0)); + fsdp_description_delete (dsc); + if (!rtp_session->control_url) + { + free (server_addr); + rtp_session_free (rtp_session); + return NULL; + } + + /* 9. create the payload for RTSP SETUP request */ + memset (temp_buf, '\0', MAX_LENGTH); + snprintf (temp_buf, MAX_LENGTH, + RTSP_TRANSPORT_REQUEST, + is_multicast ? RTSP_TRANSPORT_MULTICAST : RTSP_TRANSPORT_UNICAST, + is_multicast ? RTSP_MULTICAST_PORT : RTSP_UNICAST_CLIENT_PORT, + client_rtp_port, client_rtcp_port); + mp_msg (MSGT_OPEN, MSGL_V, "RTSP Transport: %s\n", temp_buf); + + rtsp_unschedule_field (rtsp_session, RTSP_SESSION); + rtsp_schedule_field (rtsp_session, temp_buf); + + /* 10. check for the media control URL type and initiate RTSP SETUP */ + if (!strncmp (rtp_session->control_url, "rtsp://", 7)) /* absolute URL */ + statut = rtsp_request_setup (rtsp_session, + rtp_session->control_url, NULL); + else /* relative URL */ + statut = rtsp_request_setup (rtsp_session, + NULL, rtp_session->control_url); + + if (statut < 200 || statut > 299) + { + free (server_addr); + rtp_session_free (rtp_session); + return NULL; + } + + /* 11. parse RTSP SETUP response: we need it to actually determine + * the real address and port to connect to */ + answer = rtsp_search_answers (rtsp_session, RTSP_TRANSPORT); + if (!answer) + { + free (server_addr); + rtp_session_free (rtp_session); + return NULL; + } + + /* check for RTP and RTCP ports to bind according to how request was done */ + is_multicast = 0; + if (strstr (answer, RTSP_TRANSPORT_MULTICAST)) + is_multicast = 1; + + if (is_multicast) + parse_port (answer, RTSP_MULTICAST_PORT, + &client_rtp_port, &client_rtcp_port); + else + { + parse_port (answer, RTSP_UNICAST_CLIENT_PORT, + &client_rtp_port, &client_rtcp_port); + parse_port (answer, RTSP_UNICAST_SERVER_PORT, + &server_rtp_port, &server_rtcp_port); + } + + /* now check network settings as determined by server */ + if (rtsp_destination) + destination = strdup (rtsp_destination); + else + destination = parse_destination (answer); + if (!destination) + destination = strdup (server_addr); + free (server_addr); + + mp_msg (MSGT_OPEN, MSGL_V, "RTSP Destination: %s\n", destination); + mp_msg (MSGT_OPEN, MSGL_V, "Client RTP port : %d\n", client_rtp_port); + mp_msg (MSGT_OPEN, MSGL_V, "Client RTCP port : %d\n", client_rtcp_port); + mp_msg (MSGT_OPEN, MSGL_V, "Server RTP port : %d\n", server_rtp_port); + mp_msg (MSGT_OPEN, MSGL_V, "Server RTCP port : %d\n", server_rtcp_port); + + /* 12. performs RTSP PLAY request */ + rtsp_schedule_field (rtsp_session, npt); + statut = rtsp_request_play (rtsp_session, NULL); + if (statut < 200 || statut > 299) + { + free (destination); + rtp_session_free (rtp_session); + return NULL; + } + + /* 13. create RTP and RTCP connections */ + rtp_sock = rtp_connect (destination, client_rtp_port); + rtcp_sock = rtcp_connect (client_rtcp_port, server_rtcp_port, destination); + rtp_session_set_fd (rtp_session, rtp_sock, rtcp_sock); + free (destination); + + mp_msg (MSGT_OPEN, MSGL_V, "RTP Sock : %d\nRTCP Sock : %d\n", + rtp_session->rtp_socket, rtp_session->rtcp_socket); + + if (rtp_session->rtp_socket == -1) + { + rtp_session_free (rtp_session); + return NULL; + } + + return rtp_session; +} diff --git a/stream/librtsp/rtsp_rtp.h b/stream/librtsp/rtsp_rtp.h new file mode 100644 index 0000000000..117a09e57b --- /dev/null +++ b/stream/librtsp/rtsp_rtp.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2006 Benjamin Zores + * heavily base on the Freebox patch for xine by Vincent Mussard + * but with many enhancements for better RTSP RFC compliance. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _HAVE_RTSP_RTP_H_ +#define _HAVE_RTSP_RTP_H_ + +#include "rtsp.h" + +#define MAX_PREVIEW_SIZE 4096 + +struct rtp_rtsp_session_t { + int rtp_socket; + int rtcp_socket; + char *control_url; + int count; +}; + +struct rtp_rtsp_session_t *rtp_setup_and_play (rtsp_t* rtsp_session); +off_t rtp_read (struct rtp_rtsp_session_t* st, char *buf, off_t length); +void rtp_session_free (struct rtp_rtsp_session_t *st); +void rtcp_send_rr (rtsp_t *s, struct rtp_rtsp_session_t *st); + +#endif /* _HAVE_RTSP_RTP_H_ */ + diff --git a/stream/librtsp/rtsp_session.c b/stream/librtsp/rtsp_session.c new file mode 100644 index 0000000000..fc8aa807f8 --- /dev/null +++ b/stream/librtsp/rtsp_session.c @@ -0,0 +1,269 @@ +/* + * This file was ported to MPlayer from xine CVS rtsp_session.c,v 1.9 2003/02/11 16:20:40 + */ + +/* + * Copyright (C) 2000-2002 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * + * high level interface to rtsp servers. + * + * 2006, Benjamin Zores and Vincent Mussard + * Support for MPEG-TS streaming through RFC compliant RTSP servers + */ + +#include +#include "config.h" +#ifndef HAVE_WINSOCK2 +#include +#include +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include + +#include "mp_msg.h" +#include "../rtp.h" +#include "rtsp.h" +#include "rtsp_rtp.h" +#include "rtsp_session.h" +#include "../realrtsp/real.h" +#include "../realrtsp/rmff.h" +#include "../realrtsp/asmrp.h" +#include "../realrtsp/xbuffer.h" + +/* +#define LOG +*/ + +#define RTSP_OPTIONS_PUBLIC "Public" +#define RTSP_OPTIONS_SERVER "Server" +#define RTSP_OPTIONS_LOCATION "Location" +#define RTSP_OPTIONS_REAL "RealChallenge1" +#define RTSP_SERVER_TYPE_REAL "Real" +#define RTSP_SERVER_TYPE_HELIX "Helix" +#define RTSP_SERVER_TYPE_UNKNOWN "unknown" + +struct rtsp_session_s { + rtsp_t *s; + struct real_rtsp_session_t* real_session; + struct rtp_rtsp_session_t* rtp_session; +}; + +//rtsp_session_t *rtsp_session_start(char *mrl) { +rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host, int port, int *redir, uint32_t bandwidth) { + + rtsp_session_t *rtsp_session = NULL; + char *server; + char *mrl_line = NULL; + rmff_header_t *h; + + rtsp_session = malloc (sizeof (rtsp_session_t)); + rtsp_session->s = NULL; + rtsp_session->real_session = NULL; + rtsp_session->rtp_session = NULL; + +//connect: + *redir = 0; + + /* connect to server */ + rtsp_session->s=rtsp_connect(fd,*mrl,path,host,port,NULL); + if (!rtsp_session->s) + { + mp_msg (MSGT_OPEN, MSGL_ERR,"rtsp_session: failed to connect to server %s\n", path); + free(rtsp_session); + return NULL; + } + + /* looking for server type */ + if (rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_SERVER)) + server=strdup(rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_SERVER)); + else { + if (rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_REAL)) + server=strdup(RTSP_SERVER_TYPE_REAL); + else + server=strdup(RTSP_SERVER_TYPE_UNKNOWN); + } + if (strstr(server,RTSP_SERVER_TYPE_REAL) || strstr(server,RTSP_SERVER_TYPE_HELIX)) + { + /* we are talking to a real server ... */ + + h=real_setup_and_get_header(rtsp_session->s, bandwidth); + if (!h) { + /* got an redirect? */ + if (rtsp_search_answers(rtsp_session->s, RTSP_OPTIONS_LOCATION)) + { + free(mrl_line); + mrl_line=strdup(rtsp_search_answers(rtsp_session->s, RTSP_OPTIONS_LOCATION)); + mp_msg (MSGT_OPEN, MSGL_INFO,"rtsp_session: redirected to %s\n", mrl_line); + rtsp_close(rtsp_session->s); + free(server); + free(*mrl); + free(rtsp_session); + /* tell the caller to redirect, return url to redirect to in mrl */ + *mrl = mrl_line; + *redir = 1; + return NULL; +// goto connect; /* *shudder* i made a design mistake somewhere */ + } else + { + mp_msg (MSGT_OPEN, MSGL_ERR,"rtsp_session: session can not be established.\n"); + rtsp_close(rtsp_session->s); + free (server); + free(rtsp_session); + return NULL; + } + } + + rtsp_session->real_session = init_real_rtsp_session (); + rtsp_session->real_session->header_len = + rmff_dump_header (h, (char *) rtsp_session->real_session->header, 1024); + + rtsp_session->real_session->recv = + xbuffer_copyin (rtsp_session->real_session->recv, 0, + rtsp_session->real_session->header, + rtsp_session->real_session->header_len); + + rtsp_session->real_session->recv_size = + rtsp_session->real_session->header_len; + rtsp_session->real_session->recv_read = 0; + } else /* not a Real server : try RTP instead */ + { + char *public = NULL; + + /* look for the Public: field in response to RTSP OPTIONS */ + public = strdup (rtsp_search_answers (rtsp_session->s, + RTSP_OPTIONS_PUBLIC)); + if (!public) + { + rtsp_close (rtsp_session->s); + free (server); + free (mrl_line); + free (rtsp_session); + return NULL; + } + + /* check for minimalistic RTSP RFC compliance */ + if (!strstr (public, RTSP_METHOD_DESCRIBE) + || !strstr (public, RTSP_METHOD_SETUP) + || !strstr (public, RTSP_METHOD_PLAY) + || !strstr (public, RTSP_METHOD_TEARDOWN)) + { + free (public); + mp_msg (MSGT_OPEN, MSGL_ERR, + "Remote server does not meet minimal RTSP 1.0 compliance.\n"); + rtsp_close (rtsp_session->s); + free (server); + free (mrl_line); + free (rtsp_session); + return NULL; + } + + free (public); + rtsp_session->rtp_session = rtp_setup_and_play (rtsp_session->s); + + /* neither a Real or an RTP server */ + if (!rtsp_session->rtp_session) + { + mp_msg (MSGT_OPEN, MSGL_ERR, "rtsp_session: unsupported RTSP server. "); + mp_msg (MSGT_OPEN, MSGL_ERR, "Server type is '%s'.\n", server); + rtsp_close (rtsp_session->s); + free (server); + free (mrl_line); + free (rtsp_session); + return NULL; + } + } + free(server); + + return rtsp_session; +} + +int rtsp_session_read (rtsp_session_t *this, char *data, int len) { + + if (this->real_session) { + int to_copy=len; + char *dest=data; + char *source = + (char *) (this->real_session->recv + this->real_session->recv_read); + int fill = this->real_session->recv_size - this->real_session->recv_read; + + if (len < 0) return 0; + while (to_copy > fill) { + + memcpy(dest, source, fill); + to_copy -= fill; + dest += fill; + this->real_session->recv_read = 0; + this->real_session->recv_size = + real_get_rdt_chunk (this->s, (char **)&(this->real_session->recv)); + if (this->real_session->recv_size < 0) + return -1; + source = (char *) this->real_session->recv; + fill = this->real_session->recv_size; + + if (this->real_session->recv_size == 0) { +#ifdef LOG + mp_msg (MSGT_OPEN, MSGL_INFO, "librtsp: %d of %d bytes provided\n", len-to_copy, len); +#endif + return len-to_copy; + } + } + + memcpy(dest, source, to_copy); + this->real_session->recv_read += to_copy; + +#ifdef LOG + mp_msg (MSGT_OPEN, MSGL_INFO, "librtsp: %d bytes provided\n", len); +#endif + + return len; + } + else if (this->rtp_session) + { + int l = 0; + + l = read_rtp_from_server (this->rtp_session->rtp_socket, data, len); + /* send RTSP and RTCP keepalive */ + rtcp_send_rr (this->s, this->rtp_session); + + if (l == 0) + rtsp_session_end (this); + + return l; + } + + return 0; +} + +void rtsp_session_end(rtsp_session_t *session) { + + rtsp_close(session->s); + if (session->real_session) + free_real_rtsp_session (session->real_session); + if (session->rtp_session) + rtp_session_free (session->rtp_session); + free(session); +} diff --git a/stream/librtsp/rtsp_session.h b/stream/librtsp/rtsp_session.h new file mode 100644 index 0000000000..e955e939b1 --- /dev/null +++ b/stream/librtsp/rtsp_session.h @@ -0,0 +1,42 @@ +/* + * This file was ported to MPlayer from xine CVS rtsp_session.h,v 1.4 2003/01/31 14:06:18 + */ + +/* + * Copyright (C) 2000-2002 the xine project + * + * This file is part of xine, a free video player. + * + * xine is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * xine is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * + * high level interface to rtsp servers. + * + * 2006, Benjamin Zores and Vincent Mussard + * Support for MPEG-TS streaming through RFC compliant RTSP servers + */ + +#ifndef HAVE_RTSP_SESSION_H +#define HAVE_RTSP_SESSION_H + +typedef struct rtsp_session_s rtsp_session_t; + +rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host, int port, int *redir, uint32_t bandwidth); + +int rtsp_session_read(rtsp_session_t *session, char *data, int len); + +void rtsp_session_end(rtsp_session_t *session); + +#endif -- cgit v1.2.3