2003-04-17 20:39:41 +00:00
|
|
|
/*
|
|
|
|
* 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-06-26 21:27:57 +00:00
|
|
|
*
|
|
|
|
* 2006, Benjamin Zores and Vincent Mussard
|
|
|
|
* Support for MPEG-TS streaming through RFC compliant RTSP servers
|
2003-04-17 20:39:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2008-08-31 13:59:54 +00:00
|
|
|
#include "config.h"
|
|
|
|
#ifndef HAVE_WINSOCK2_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-04-17 20:39:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-01-08 22:57:59 +00:00
|
|
|
#include <inttypes.h>
|
2003-04-17 20:39:41 +00:00
|
|
|
|
2006-06-23 19:34:06 +00:00
|
|
|
#include "mp_msg.h"
|
2003-04-17 20:39:41 +00:00
|
|
|
#include "rtsp.h"
|
2006-06-26 21:27:57 +00:00
|
|
|
#include "rtsp_rtp.h"
|
2003-04-17 20:39:41 +00:00
|
|
|
#include "rtsp_session.h"
|
2008-09-01 07:31:57 +00:00
|
|
|
#include "stream/network.h"
|
2008-08-29 22:29:27 +00:00
|
|
|
#include "stream/url.h"
|
|
|
|
#include "stream/rtp.h"
|
|
|
|
#include "stream/realrtsp/real.h"
|
|
|
|
#include "stream/realrtsp/rmff.h"
|
|
|
|
#include "stream/realrtsp/asmrp.h"
|
|
|
|
#include "stream/realrtsp/xbuffer.h"
|
2003-04-17 20:39:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
#define LOG
|
|
|
|
*/
|
|
|
|
|
2006-06-26 21:27:57 +00:00
|
|
|
#define RTSP_OPTIONS_PUBLIC "Public"
|
2006-06-26 17:27:38 +00:00
|
|
|
#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"
|
|
|
|
|
2003-04-17 20:39:41 +00:00
|
|
|
struct rtsp_session_s {
|
|
|
|
rtsp_t *s;
|
2006-06-23 19:30:15 +00:00
|
|
|
struct real_rtsp_session_t* real_session;
|
2006-06-26 21:27:57 +00:00
|
|
|
struct rtp_rtsp_session_t* rtp_session;
|
2003-04-17 20:39:41 +00:00
|
|
|
};
|
|
|
|
|
2008-06-25 09:53:45 +00:00
|
|
|
/*
|
|
|
|
* closes an rtsp connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
static 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);
|
|
|
|
}
|
|
|
|
|
2003-04-17 20:39:41 +00:00
|
|
|
//rtsp_session_t *rtsp_session_start(char *mrl) {
|
2006-10-30 21:55:58 +00:00
|
|
|
rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host,
|
|
|
|
int port, int *redir, uint32_t bandwidth, char *user, char *pass) {
|
2003-04-17 20:39:41 +00:00
|
|
|
|
2006-06-23 19:30:15 +00:00
|
|
|
rtsp_session_t *rtsp_session = NULL;
|
2003-04-17 20:39:41 +00:00
|
|
|
char *server;
|
|
|
|
char *mrl_line = NULL;
|
|
|
|
rmff_header_t *h;
|
|
|
|
|
2006-06-23 19:30:15 +00:00
|
|
|
rtsp_session = malloc (sizeof (rtsp_session_t));
|
|
|
|
rtsp_session->s = NULL;
|
|
|
|
rtsp_session->real_session = NULL;
|
2006-06-26 21:27:57 +00:00
|
|
|
rtsp_session->rtp_session = NULL;
|
2006-06-23 19:30:15 +00:00
|
|
|
|
2003-05-28 21:40:53 +00:00
|
|
|
//connect:
|
|
|
|
*redir = 0;
|
2003-04-17 20:39:41 +00:00
|
|
|
|
|
|
|
/* connect to server */
|
2003-05-28 21:40:53 +00:00
|
|
|
rtsp_session->s=rtsp_connect(fd,*mrl,path,host,port,NULL);
|
2003-04-17 20:39:41 +00:00
|
|
|
if (!rtsp_session->s)
|
|
|
|
{
|
2006-06-23 19:34:06 +00:00
|
|
|
mp_msg (MSGT_OPEN, MSGL_ERR,"rtsp_session: failed to connect to server %s\n", path);
|
2003-04-17 20:39:41 +00:00
|
|
|
free(rtsp_session);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* looking for server type */
|
2006-06-26 17:27:38 +00:00
|
|
|
if (rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_SERVER))
|
|
|
|
server=strdup(rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_SERVER));
|
2003-04-17 20:39:41 +00:00
|
|
|
else {
|
2006-06-26 17:27:38 +00:00
|
|
|
if (rtsp_search_answers(rtsp_session->s,RTSP_OPTIONS_REAL))
|
|
|
|
server=strdup(RTSP_SERVER_TYPE_REAL);
|
2003-04-17 20:39:41 +00:00
|
|
|
else
|
2006-06-26 17:27:38 +00:00
|
|
|
server=strdup(RTSP_SERVER_TYPE_UNKNOWN);
|
2003-04-17 20:39:41 +00:00
|
|
|
}
|
2006-06-26 17:27:38 +00:00
|
|
|
if (strstr(server,RTSP_SERVER_TYPE_REAL) || strstr(server,RTSP_SERVER_TYPE_HELIX))
|
2003-04-17 20:39:41 +00:00
|
|
|
{
|
|
|
|
/* we are talking to a real server ... */
|
|
|
|
|
2006-10-30 21:55:58 +00:00
|
|
|
h=real_setup_and_get_header(rtsp_session->s, bandwidth, user, pass);
|
2003-04-17 20:39:41 +00:00
|
|
|
if (!h) {
|
|
|
|
/* got an redirect? */
|
2006-06-26 17:27:38 +00:00
|
|
|
if (rtsp_search_answers(rtsp_session->s, RTSP_OPTIONS_LOCATION))
|
2003-04-17 20:39:41 +00:00
|
|
|
{
|
|
|
|
free(mrl_line);
|
2006-06-26 17:27:38 +00:00
|
|
|
mrl_line=strdup(rtsp_search_answers(rtsp_session->s, RTSP_OPTIONS_LOCATION));
|
2006-06-23 19:34:06 +00:00
|
|
|
mp_msg (MSGT_OPEN, MSGL_INFO,"rtsp_session: redirected to %s\n", mrl_line);
|
2003-04-17 20:39:41 +00:00
|
|
|
rtsp_close(rtsp_session->s);
|
|
|
|
free(server);
|
2003-05-28 21:40:53 +00:00
|
|
|
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 */
|
2003-04-17 20:39:41 +00:00
|
|
|
} else
|
|
|
|
{
|
2006-06-23 19:34:06 +00:00
|
|
|
mp_msg (MSGT_OPEN, MSGL_ERR,"rtsp_session: session can not be established.\n");
|
2003-04-17 20:39:41 +00:00
|
|
|
rtsp_close(rtsp_session->s);
|
2006-06-23 19:30:15 +00:00
|
|
|
free (server);
|
2003-04-17 20:39:41 +00:00
|
|
|
free(rtsp_session);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-23 19:30:15 +00:00
|
|
|
rtsp_session->real_session = init_real_rtsp_session ();
|
2007-03-18 16:50:26 +00:00
|
|
|
if(!strncmp(h->streams[0]->mime_type, "application/vnd.rn-rmadriver", h->streams[0]->mime_type_size) ||
|
|
|
|
!strncmp(h->streams[0]->mime_type, "application/smil", h->streams[0]->mime_type_size)) {
|
2007-02-18 15:57:50 +00:00
|
|
|
rtsp_session->real_session->header_len = 0;
|
|
|
|
rtsp_session->real_session->recv_size = 0;
|
|
|
|
rtsp_session->real_session->rdt_rawdata = 1;
|
|
|
|
mp_msg(MSGT_OPEN, MSGL_V, "smil-over-realrtsp playlist, switching to raw rdt mode\n");
|
|
|
|
} else {
|
2006-06-23 19:30:15 +00:00
|
|
|
rtsp_session->real_session->header_len =
|
2007-03-25 19:00:39 +00:00
|
|
|
rmff_dump_header (h, (char *) rtsp_session->real_session->header, HEADER_SIZE);
|
|
|
|
|
|
|
|
if (rtsp_session->real_session->header_len < 0) {
|
|
|
|
mp_msg (MSGT_OPEN, MSGL_ERR,"rtsp_session: error while dumping RMFF headers, session can not be established.\n");
|
|
|
|
free_real_rtsp_session(rtsp_session->real_session);
|
|
|
|
rtsp_close(rtsp_session->s);
|
|
|
|
free (server);
|
|
|
|
free (mrl_line);
|
|
|
|
free(rtsp_session);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-06-23 19:30:15 +00:00
|
|
|
|
|
|
|
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;
|
2007-02-18 15:57:50 +00:00
|
|
|
}
|
2006-06-23 19:30:15 +00:00
|
|
|
rtsp_session->real_session->recv_read = 0;
|
2006-06-26 21:27:57 +00:00
|
|
|
} else /* not a Real server : try RTP instead */
|
2003-04-17 20:39:41 +00:00
|
|
|
{
|
2006-06-26 21:27:57 +00:00
|
|
|
char *public = NULL;
|
|
|
|
|
|
|
|
/* look for the Public: field in response to RTSP OPTIONS */
|
2006-08-30 20:18:27 +00:00
|
|
|
if (!(public = rtsp_search_answers (rtsp_session->s, RTSP_OPTIONS_PUBLIC)))
|
2006-06-26 21:27:57 +00:00
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2003-04-17 20:39:41 +00:00
|
|
|
}
|
|
|
|
free(server);
|
|
|
|
|
|
|
|
return rtsp_session;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtsp_session_read (rtsp_session_t *this, char *data, int len) {
|
|
|
|
|
2006-06-23 19:30:15 +00:00
|
|
|
if (this->real_session) {
|
2003-04-17 20:39:41 +00:00
|
|
|
int to_copy=len;
|
|
|
|
char *dest=data;
|
2006-06-23 19:30:15 +00:00
|
|
|
char *source =
|
|
|
|
(char *) (this->real_session->recv + this->real_session->recv_read);
|
|
|
|
int fill = this->real_session->recv_size - this->real_session->recv_read;
|
2003-04-17 20:39:41 +00:00
|
|
|
|
2007-01-09 21:32:25 +00:00
|
|
|
if(this->real_session->rdteof)
|
|
|
|
return -1;
|
2003-04-17 20:39:41 +00:00
|
|
|
if (len < 0) return 0;
|
2007-01-07 19:23:59 +00:00
|
|
|
if (this->real_session->recv_size < 0) return -1;
|
2003-04-17 20:39:41 +00:00
|
|
|
while (to_copy > fill) {
|
|
|
|
|
|
|
|
memcpy(dest, source, fill);
|
|
|
|
to_copy -= fill;
|
|
|
|
dest += fill;
|
2006-06-23 19:30:15 +00:00
|
|
|
this->real_session->recv_read = 0;
|
|
|
|
this->real_session->recv_size =
|
2007-02-18 15:57:50 +00:00
|
|
|
real_get_rdt_chunk (this->s, (char **)&(this->real_session->recv), this->real_session->rdt_rawdata);
|
2007-01-09 21:32:25 +00:00
|
|
|
if (this->real_session->recv_size < 0) {
|
|
|
|
this->real_session->rdteof = 1;
|
|
|
|
this->real_session->recv_size = 0;
|
|
|
|
}
|
2006-06-23 19:30:15 +00:00
|
|
|
source = (char *) this->real_session->recv;
|
|
|
|
fill = this->real_session->recv_size;
|
2003-04-17 20:39:41 +00:00
|
|
|
|
2006-06-23 19:30:15 +00:00
|
|
|
if (this->real_session->recv_size == 0) {
|
2003-04-17 20:39:41 +00:00
|
|
|
#ifdef LOG
|
2006-06-23 19:34:06 +00:00
|
|
|
mp_msg (MSGT_OPEN, MSGL_INFO, "librtsp: %d of %d bytes provided\n", len-to_copy, len);
|
2003-04-17 20:39:41 +00:00
|
|
|
#endif
|
|
|
|
return len-to_copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dest, source, to_copy);
|
2006-06-23 19:30:15 +00:00
|
|
|
this->real_session->recv_read += to_copy;
|
2003-04-17 20:39:41 +00:00
|
|
|
|
|
|
|
#ifdef LOG
|
2006-06-23 19:34:06 +00:00
|
|
|
mp_msg (MSGT_OPEN, MSGL_INFO, "librtsp: %d bytes provided\n", len);
|
2003-04-17 20:39:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return len;
|
2006-06-23 19:30:15 +00:00
|
|
|
}
|
2006-06-26 21:27:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2006-06-23 19:30:15 +00:00
|
|
|
|
|
|
|
return 0;
|
2003-04-17 20:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtsp_session_end(rtsp_session_t *session) {
|
|
|
|
|
|
|
|
rtsp_close(session->s);
|
2006-06-23 19:30:15 +00:00
|
|
|
if (session->real_session)
|
|
|
|
free_real_rtsp_session (session->real_session);
|
2006-06-26 21:27:57 +00:00
|
|
|
if (session->rtp_session)
|
|
|
|
rtp_session_free (session->rtp_session);
|
2003-04-17 20:39:41 +00:00
|
|
|
free(session);
|
|
|
|
}
|