📄 rtsp.c
字号:
/* * 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 <unistd.h>#include <stdio.h>#include <assert.h>#include "config.h"#ifndef HAVE_WINSOCK2#define closesocket close#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#else#include <winsock2.h>#endif#include <string.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <stdlib.h>#include <time.h>#include <sys/time.h>#include <sys/types.h>#include <inttypes.h>#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 256struct 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 (n<BUF_SIZE) { read_stream(s->s, &(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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -