/*****************************************************************************
* rtsp.c: RTSP support for RTP stream output module
*****************************************************************************
- * Copyright (C) 2003-2007 the VideoLAN team
- * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
+ * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright © 2007 Rémi Denis-Courmont
+ *
+ * $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
#include <vlc_sout.h>
#include <vlc_httpd.h>
#include <vlc_url.h>
#include <vlc_network.h>
+#include <vlc_rand.h>
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
#include "rtp.h"
-/* For unicast/interleaved streaming */
-struct rtsp_client_t
-{
- char *psz_session;
- int64_t i_last; /* for timeout */
-
- /* is it in "play" state */
- vlc_bool_t b_playing;
+typedef struct rtsp_session_t rtsp_session_t;
- /* output (id-access) */
- int i_id;
- sout_stream_id_t **id;
- int i_access;
- sout_access_out_t **access;
+struct rtsp_stream_t
+{
+ vlc_mutex_t lock;
+ sout_stream_t *owner;
+ httpd_host_t *host;
+ httpd_url_t *url;
+ char *psz_path;
+ unsigned track_id;
+ unsigned port;
+
+ int sessionc;
+ rtsp_session_t **sessionv;
};
-static int RtspCallback( httpd_callback_sys_t *p_args,
- httpd_client_t *cl,
- httpd_message_t *answer, httpd_message_t *query );
-
-
-int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
+static int RtspCallback( httpd_callback_sys_t *p_args,
+ httpd_client_t *cl, httpd_message_t *answer,
+ const httpd_message_t *query );
+static int RtspCallbackId( httpd_callback_sys_t *p_args,
+ httpd_client_t *cl, httpd_message_t *answer,
+ const httpd_message_t *query );
+static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
+
+rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
{
- sout_stream_sys_t *p_sys = p_stream->p_sys;
+ rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
- msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
-
- p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
- if( p_sys->p_rtsp_host == NULL )
+ if( rtsp == NULL || ( url->i_port > 99999 ) )
{
- return VLC_EGENERIC;
+ free( rtsp );
+ return NULL;
}
- p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
- p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
- sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
- url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
-
- p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
- if( p_sys->p_rtsp_url == 0 )
- {
- return VLC_EGENERIC;
- }
- httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
- httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
- httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
- httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
- httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
+ rtsp->owner = p_stream;
+ rtsp->sessionc = 0;
+ rtsp->sessionv = NULL;
+ rtsp->host = NULL;
+ rtsp->url = NULL;
+ rtsp->psz_path = NULL;
+ rtsp->track_id = 0;
+ vlc_mutex_init( &rtsp->lock );
+
+ rtsp->port = (url->i_port > 0) ? url->i_port : 554;
+ rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
+ if( rtsp->psz_path == NULL )
+ goto error;
+
+ msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
+ url->psz_host, rtsp->port, rtsp->psz_path );
+
+ rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
+ rtsp->port );
+ if( rtsp->host == NULL )
+ goto error;
+
+ rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
+ NULL, NULL, NULL );
+ if( rtsp->url == NULL )
+ goto error;
+
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
+ (void*)rtsp );
+ httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
+ return rtsp;
- return VLC_SUCCESS;
+error:
+ RtspUnsetup( rtsp );
+ return NULL;
}
-static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
+void RtspUnsetup( rtsp_stream_t *rtsp )
{
- rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
+ if( rtsp->url )
+ httpd_UrlDelete( rtsp->url );
- rtsp->psz_session = strdup( psz_session );
- rtsp->i_last = 0;
- rtsp->b_playing = VLC_FALSE;
- rtsp->i_id = 0;
- rtsp->id = NULL;
- rtsp->i_access = 0;
- rtsp->access = NULL;
+ while( rtsp->sessionc > 0 )
+ RtspClientDel( rtsp, rtsp->sessionv[0] );
- TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
+ if( rtsp->host )
+ httpd_HostDelete( rtsp->host );
- return rtsp;
+ free( rtsp->psz_path );
+ vlc_mutex_destroy( &rtsp->lock );
+
+ free( rtsp );
}
-static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
+struct rtsp_stream_id_t
{
- int i;
+ rtsp_stream_t *stream;
+ sout_stream_id_t *sout_id;
+ httpd_url_t *url;
+ const char *dst;
+ int ttl;
+ unsigned track_id;
+ uint32_t ssrc;
+ uint16_t loport, hiport;
+};
- if( !psz_session ) return NULL;
- for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
- {
- if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
- {
- return p_stream->p_sys->rtsp[i];
- }
- }
- return NULL;
-}
+typedef struct rtsp_strack_t rtsp_strack_t;
+
+/* For unicast streaming */
+struct rtsp_session_t
+{
+ rtsp_stream_t *stream;
+ uint64_t id;
+
+ /* output (id-access) */
+ int trackc;
+ rtsp_strack_t *trackv;
+};
-/*static*/ void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
+/* Unicast session track */
+struct rtsp_strack_t
{
- int i;
- TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
+ rtsp_stream_id_t *id;
+ int fd;
+ bool playing;
+};
- for( i = 0; i < rtsp->i_access; i++ )
- {
- sout_AccessOutDelete( rtsp->access[i] );
- }
- if( rtsp->id ) free( rtsp->id );
- if( rtsp->access ) free( rtsp->access );
- free( rtsp->psz_session );
- free( rtsp );
+char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
+{
+ const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
+ "" : "/";
+ char *url;
+
+ if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
+ url = NULL;
+ return url;
}
-/** Aggregate RTSP callback */
-static int RtspCallback( httpd_callback_sys_t *p_args,
- httpd_client_t *cl,
- httpd_message_t *answer, httpd_message_t *query )
+rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
+ uint32_t ssrc,
+ /* Multicast stuff - TODO: cleanup */
+ const char *dst, int ttl,
+ unsigned loport, unsigned hiport )
{
- sout_stream_t *p_stream = (sout_stream_t*)p_args;
- sout_stream_sys_t *p_sys = p_stream->p_sys;
- char *psz_destination = p_sys->psz_destination;
- const char *psz_session = NULL;
- const char *psz_cseq = NULL;
+ char *urlbuf;
+ rtsp_stream_id_t *id = malloc( sizeof( *id ) );
+ httpd_url_t *url;
+
+ if( id == NULL )
+ return NULL;
+
+ id->stream = rtsp;
+ id->sout_id = sid;
+ id->track_id = rtsp->track_id;
+ id->ssrc = ssrc;
+ /* TODO: can we assume that this need not be strdup'd? */
+ id->dst = dst;
+ if( id->dst != NULL )
+ {
+ id->ttl = ttl;
+ id->loport = loport;
+ id->hiport = hiport;
+ }
- if( answer == NULL || query == NULL )
+ urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
+ if( urlbuf == NULL )
{
- return VLC_SUCCESS;
+ free( id );
+ return NULL;
}
- //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
- answer->i_proto = HTTPD_PROTO_RTSP;
- answer->i_version= query->i_version;
- answer->i_type = HTTPD_MSG_ANSWER;
- answer->i_body = 0;
- answer->p_body = NULL;
+ msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
+ url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
+ free( urlbuf );
- if( httpd_MsgGet( query, "Require" ) != NULL )
+ if( url == NULL )
{
- answer->i_status = 551;
- httpd_MsgAdd( query, "Unsupported", "%s",
- httpd_MsgGet( query, "Require" ) );
+ free( id );
+ return NULL;
}
- else
- switch( query->i_type )
- {
- case HTTPD_MSG_DESCRIBE:
- {
- char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
- answer->i_status = 200;
- httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
- httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
- answer->p_body = (uint8_t *)psz_sdp;
- answer->i_body = strlen( psz_sdp );
- break;
- }
+ httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
+ httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
+ httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
+ httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
+ httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
+ httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
- case HTTPD_MSG_SETUP:
- answer->i_status = 459;
- break;
+ rtsp->track_id++;
- case HTTPD_MSG_PLAY:
- {
- rtsp_client_t *rtsp;
- answer->i_status = 200;
+ return id;
+}
- psz_session = httpd_MsgGet( query, "Session" );
- rtsp = RtspClientGet( p_stream, psz_session );
- if( rtsp && !rtsp->b_playing )
- {
- int i_id;
- /* FIXME */
- rtsp->b_playing = VLC_TRUE;
- vlc_mutex_lock( &p_sys->lock_es );
- for( i_id = 0; i_id < rtsp->i_id; i_id++ )
- {
- sout_stream_id_t *id = rtsp->id[i_id];
- int i;
+void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
+{
+ httpd_UrlDelete( id->url );
- for( i = 0; i < p_sys->i_es; i++ )
- {
- if( id == p_sys->es[i] )
- break;
- }
- if( i >= p_sys->i_es ) continue;
+ vlc_mutex_lock( &rtsp->lock );
+ for( int i = 0; i < rtsp->sessionc; i++ )
+ {
+ rtsp_session_t *ses = rtsp->sessionv[i];
- rtp_add_sink( id, rtsp->access[i_id] );
- }
- vlc_mutex_unlock( &p_sys->lock_es );
+ for( int j = 0; j < ses->trackc; j++ )
+ {
+ if( ses->trackv[j].id == id )
+ {
+ rtsp_strack_t *tr = ses->trackv + j;
+ rtp_del_sink( tr->id->sout_id, tr->fd );
+ REMOVE_ELEM( ses->trackv, ses->trackc, j );
}
- break;
}
+ }
- case HTTPD_MSG_PAUSE:
- answer->i_status = 405;
- httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
- break;
+ vlc_mutex_unlock( &rtsp->lock );
+ free( id );
+}
- case HTTPD_MSG_TEARDOWN:
- {
- rtsp_client_t *rtsp;
- /* for now only multicast so easy again */
- answer->i_status = 200;
+/** rtsp must be locked */
+static
+rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
+{
+ rtsp_session_t *s = malloc( sizeof( *s ) );
+ if( s == NULL )
+ return NULL;
- psz_session = httpd_MsgGet( query, "Session" );
- rtsp = RtspClientGet( p_stream, psz_session );
- if( rtsp )
- {
- int i_id;
+ s->stream = rtsp;
+ vlc_rand_bytes (&s->id, sizeof (s->id));
+ s->trackc = 0;
+ s->trackv = NULL;
- vlc_mutex_lock( &p_sys->lock_es );
- for( i_id = 0; i_id < rtsp->i_id; i_id++ )
- {
- sout_stream_id_t *id = rtsp->id[i_id];
- int i;
+ TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
- for( i = 0; i < p_sys->i_es; i++ )
- {
- if( id == p_sys->es[i] )
- break;
- }
- if( i >= p_sys->i_es ) continue;
+ return s;
+}
- rtp_del_sink( id, rtsp->access[i_id] );
- }
- vlc_mutex_unlock( &p_sys->lock_es );
- RtspClientDel( p_stream, rtsp );
- }
- break;
- }
+/** rtsp must be locked */
+static
+rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
+{
+ char *end;
+ uint64_t id;
+ int i;
- default:
- return VLC_EGENERIC;
+ if( name == NULL )
+ return NULL;
+
+ errno = 0;
+ id = strtoull( name, &end, 0x10 );
+ if( errno || *end )
+ return NULL;
+
+ /* FIXME: use a hash/dictionary */
+ for( i = 0; i < rtsp->sessionc; i++ )
+ {
+ if( rtsp->sessionv[i]->id == id )
+ return rtsp->sessionv[i];
}
+ return NULL;
+}
- httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
- httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
- psz_cseq = httpd_MsgGet( query, "Cseq" );
- if( psz_cseq )
- httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
- httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
- if( psz_session )
- httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
- return VLC_SUCCESS;
+/** rtsp must be locked */
+static
+void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
+{
+ int i;
+ TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
+
+ for( i = 0; i < session->trackc; i++ )
+ rtp_del_sink( session->trackv[i].id->sout_id, session->trackv[i].fd );
+
+ free( session->trackv );
+ free( session );
}
}
-/** Non-aggregate RTSP callback */
-/*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
- httpd_client_t *cl,
- httpd_message_t *answer, httpd_message_t *query )
+/** RTSP requests handler
+ * @param id selected track for non-aggregate URLs,
+ * NULL for aggregate URLs
+ */
+static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
+ httpd_client_t *cl,
+ httpd_message_t *answer,
+ const httpd_message_t *query )
{
- sout_stream_id_t *id = (sout_stream_id_t*)p_args;
- sout_stream_t *p_stream = id->p_stream;
- sout_stream_sys_t *p_sys = p_stream->p_sys;
- char psz_session_init[21];
- const char *psz_session;
- const char *psz_cseq;
-
- if( answer == NULL || query == NULL )
+ sout_stream_t *p_stream = rtsp->owner;
+ char psz_sesbuf[17];
+ const char *psz_session = NULL, *psz;
+ char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
+ + strlen( rtsp->psz_path )];
+ time_t now;
+
+ time (&now);
+
+ if( answer == NULL || query == NULL || cl == NULL )
return VLC_SUCCESS;
- //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
+ else
+ {
+ /* Build self-referential control URL */
+ char ip[NI_MAXNUMERICHOST], *ptr;
+
+ httpd_ServerIP( cl, ip );
+ ptr = strchr( ip, '%' );
+ if( ptr != NULL )
+ *ptr = '\0';
+
+ if( strchr( ip, ':' ) != NULL )
+ sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
+ rtsp->psz_path );
+ else
+ sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
+ rtsp->psz_path );
+ }
/* */
answer->i_proto = HTTPD_PROTO_RTSP;
- answer->i_version= query->i_version;
+ answer->i_version= 0;
answer->i_type = HTTPD_MSG_ANSWER;
answer->i_body = 0;
answer->p_body = NULL;
- /* Create new session ID if needed */
- psz_session = httpd_MsgGet( query, "Session" );
- if( psz_session == NULL )
- {
- /* FIXME: should be somewhat secure randomness */
- snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
- NTPtime64() + rand() );
+ httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
+
+ /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
+ struct tm ut;
+ if (gmtime_r (&now, &ut) != NULL)
+ { /* RFC1123 format, GMT is mandatory */
+ static const char wdays[7][4] = {
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+ static const char mons[12][4] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+ httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
+ wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
+ 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
}
+ if( query->i_proto != HTTPD_PROTO_RTSP )
+ {
+ answer->i_status = 505;
+ }
+ else
if( httpd_MsgGet( query, "Require" ) != NULL )
{
answer->i_status = 551;
- httpd_MsgAdd( query, "Unsupported", "%s",
+ httpd_MsgAdd( answer, "Unsupported", "%s",
httpd_MsgGet( query, "Require" ) );
}
else
switch( query->i_type )
{
+ case HTTPD_MSG_DESCRIBE:
+ { /* Aggregate-only */
+ if( id != NULL )
+ {
+ answer->i_status = 460;
+ break;
+ }
+
+ answer->i_status = 200;
+ httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
+ httpd_MsgAdd( answer, "Content-Base", "%s", control );
+ answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
+ if( answer->p_body != NULL )
+ answer->i_body = strlen( (char *)answer->p_body );
+ else
+ answer->i_status = 500;
+ break;
+ }
+
case HTTPD_MSG_SETUP:
- {
+ /* Non-aggregate-only */
+ if( id == NULL )
+ {
+ answer->i_status = 459;
+ break;
+ }
+
+ psz_session = httpd_MsgGet( query, "Session" );
answer->i_status = 461;
for( const char *tpt = httpd_MsgGet( query, "Transport" );
tpt != NULL;
tpt = transport_next( tpt ) )
{
- vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
+ bool b_multicast = true, b_unsupp = false;
unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
/* Check transport protocol. */
opt = parameter_next( opt ) )
{
if( strncmp( opt, "multicast", 9 ) == 0)
- b_multicast = VLC_TRUE;
+ b_multicast = true;
else
if( strncmp( opt, "unicast", 7 ) == 0 )
- b_multicast = VLC_FALSE;
+ b_multicast = false;
else
- if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
+ if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
+ == 2 )
;
else
if( strncmp( opt, "mode=", 5 ) == 0 )
&& strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
{
/* Not playing?! */
- b_unsupp = VLC_TRUE;
+ b_unsupp = true;
break;
}
}
else
+ if( strncmp( opt,"destination=", 12 ) == 0 )
+ {
+ answer->i_status = 403;
+ b_unsupp = true;
+ }
+ else
{
/*
* Every other option is unsupported:
*
- * "source" and "append" are invalid.
+ * "source" and "append" are invalid (server-only);
+ * "ssrc" also (as clarified per RFC2326bis).
*
* For multicast, "port", "layers", "ttl" are set by the
* stream output configuration.
*
- * For unicast, we do not allow "destination" as it
- * carries a DoS risk, and we decide on "server_port".
+ * For unicast, we want to decide "server_port" values.
*
- * "interleaved" and "ssrc" are not implemented.
+ * "interleaved" is not implemented.
*/
- b_unsupp = VLC_TRUE;
+ b_unsupp = true;
break;
}
}
if( b_multicast )
{
- if( p_sys->psz_destination == NULL )
+ const char *dst = id->dst;
+ if( dst == NULL )
continue;
+ if( psz_session == NULL )
+ {
+ /* Create a dummy session ID */
+ snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
+ rand() );
+ psz_session = psz_sesbuf;
+ }
answer->i_status = 200;
httpd_MsgAdd( answer, "Transport",
- "RTP/AVP/UDP;destination=%s;port=%d-%d;"
+ "RTP/AVP/UDP;destination=%s;port=%u-%u;"
"ttl=%d;mode=play",
- p_sys->psz_destination, id->i_port, id->i_port+1,
- ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
+ dst, id->loport, id->hiport,
+ ( id->ttl > 0 ) ? id->ttl : 1 );
}
else
{
- char ip[NI_MAXNUMERICHOST], psz_access[22],
- url[NI_MAXNUMERICHOST + 8];
- sout_access_out_t *p_access;
- rtsp_client_t *rtsp = NULL;
+ char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
+ rtsp_session_t *ses = NULL;
+ rtsp_strack_t track = { id, -1, false };
+ int sport;
+
+ if( httpd_ClientIP( cl, ip ) == NULL )
+ {
+ answer->i_status = 500;
+ continue;
+ }
- if( ( hiport - loport ) > 1 )
+ track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
+ IPPROTO_UDP );
+ if( track.fd == -1 )
+ {
+ msg_Err( p_stream,
+ "cannot create RTP socket for %s port %u",
+ ip, loport );
+ answer->i_status = 500;
continue;
+ }
+ /* Ignore any unexpected incoming packet */
+ setsockopt (track.fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
+ sizeof (int));
+ net_GetSockAddress( track.fd, src, &sport );
+
+ vlc_mutex_lock( &rtsp->lock );
if( psz_session == NULL )
{
- psz_session = psz_session_init;
- rtsp = RtspClientNew( p_stream, psz_session );
+ ses = RtspClientNew( rtsp );
+ snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
+ ses->id );
+ psz_session = psz_sesbuf;
}
else
{
/* FIXME: we probably need to remove an access out,
* if there is already one for the same ID */
- rtsp = RtspClientGet( p_stream, psz_session );
- if( rtsp == NULL )
+ ses = RtspClientGet( rtsp, psz_session );
+ if( ses == NULL )
{
answer->i_status = 454;
+ vlc_mutex_unlock( &rtsp->lock );
continue;
}
}
- if( httpd_ClientIP( cl, ip ) == NULL )
- {
- answer->i_status = 500;
- continue;
- }
-
- if( p_sys->i_ttl > 0 )
- snprintf( psz_access, sizeof( psz_access ),
- "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
- else
- strcpy( psz_access, "udp{raw,rtcp}" );
-
- snprintf( url, sizeof( url ),
- ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
- ip, loport );
-
- p_access = sout_AccessOutNew( p_stream->p_sout,
- psz_access, url );
- if( p_access == NULL )
- {
- msg_Err( p_stream,
- "cannot create access output for %s://%s",
- psz_access, url );
- answer->i_status = 500;
- break;
- }
-
- TAB_APPEND( rtsp->i_id, rtsp->id, id );
- TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
-
- char *src = var_GetNonEmptyString (p_access, "src-addr");
- int sport = var_GetInteger (p_access, "src-port");
+ INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
+ track );
+ vlc_mutex_unlock( &rtsp->lock );
httpd_ServerIP( cl, ip );
- if( ( src != NULL ) && strcmp( src, ip ) )
+ if( strcmp( src, ip ) )
{
/* Specify source IP if it is different from the RTSP
* control connection server address */
httpd_MsgAdd( answer, "Transport",
"RTP/AVP/UDP;unicast;source=%s;"
"client_port=%u-%u;server_port=%u-%u;"
- "mode=play",
- src, loport, hiport, sport, sport + 1 );
+ "ssrc=%08X;mode=play",
+ src, loport, loport + 1, sport,
+ sport + 1, id->ssrc );
}
else
{
httpd_MsgAdd( answer, "Transport",
"RTP/AVP/UDP;unicast;"
"client_port=%u-%u;server_port=%u-%u;"
- "mode=play",
- loport, hiport, sport, sport + 1 );
+ "ssrc=%08X;mode=play",
+ loport, loport + 1, sport, sport + 1,
+ id->ssrc );
}
answer->i_status = 200;
- free( src );
}
break;
}
break;
+
+ case HTTPD_MSG_PLAY:
+ {
+ rtsp_session_t *ses;
+ answer->i_status = 200;
+
+ psz_session = httpd_MsgGet( query, "Session" );
+ const char *range = httpd_MsgGet (query, "Range");
+ if (range && strncmp (range, "npt=", 4))
+ {
+ answer->i_status = 501;
+ break;
+ }
+
+ vlc_mutex_lock( &rtsp->lock );
+ ses = RtspClientGet( rtsp, psz_session );
+ if( ses != NULL )
+ {
+ /* The "trackID" part must match what is done in
+ * RtspAppendTrackPath() */
+ /* FIXME: we really need to limit the number of tracks... */
+ char info[ses->trackc * ( strlen( control )
+ + sizeof("url=/trackID=123;seq=65535;"
+ "rtptime=4294967295, ") ) + 1];
+ size_t infolen = 0;
+ int64_t ts = rtp_get_ts( rtsp->owner );
+
+ for( int i = 0; i < ses->trackc; i++ )
+ {
+ rtsp_strack_t *tr = ses->trackv + i;
+ if( ( id == NULL ) || ( tr->id == id ) )
+ {
+ uint16_t seq;
+ if( !tr->playing )
+ {
+ tr->playing = true;
+ rtp_add_sink( tr->id->sout_id, tr->fd, false,
+ &seq );
+ }
+ else
+ seq = rtp_get_seq( tr->id->sout_id );
+ char *url = RtspAppendTrackPath( tr->id, control );
+ infolen += sprintf( info + infolen,
+ "url=%s;seq=%u;rtptime=%u, ",
+ url != NULL ? url : "", seq,
+ rtp_compute_ts( tr->id->sout_id, ts ) );
+ free( url );
+ }
+ }
+ if( infolen > 0 )
+ {
+ info[infolen - 2] = '\0'; /* remove trailing ", " */
+ httpd_MsgAdd( answer, "RTP-Info", "%s", info );
+ }
+ }
+ vlc_mutex_unlock( &rtsp->lock );
+
+ if( httpd_MsgGet( query, "Scale" ) != NULL )
+ httpd_MsgAdd( answer, "Scale", "1." );
+ break;
}
- default:
- answer->i_status = 460;
+ case HTTPD_MSG_PAUSE:
+ answer->i_status = 405;
+ httpd_MsgAdd( answer, "Allow",
+ "%s, TEARDOWN, PLAY, GET_PARAMETER",
+ ( id != NULL ) ? "SETUP" : "DESCRIBE" );
break;
- }
- psz_cseq = httpd_MsgGet( query, "Cseq" );
- if( psz_cseq )
- httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
- httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
- httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
- httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
+ case HTTPD_MSG_GETPARAMETER:
+ if( query->i_body > 0 )
+ {
+ answer->i_status = 451;
+ break;
+ }
+
+ psz_session = httpd_MsgGet( query, "Session" );
+ answer->i_status = 200;
+ break;
+
+ case HTTPD_MSG_TEARDOWN:
+ {
+ rtsp_session_t *ses;
+
+ answer->i_status = 200;
+
+ psz_session = httpd_MsgGet( query, "Session" );
+
+ vlc_mutex_lock( &rtsp->lock );
+ ses = RtspClientGet( rtsp, psz_session );
+ if( ses != NULL )
+ {
+ if( id == NULL ) /* Delete the entire session */
+ RtspClientDel( rtsp, ses );
+ else /* Delete one track from the session */
+ for( int i = 0; i < ses->trackc; i++ )
+ {
+ if( ses->trackv[i].id == id )
+ {
+ rtp_del_sink( id->sout_id, ses->trackv[i].fd );
+ REMOVE_ELEM( ses->trackv, ses->trackc, i );
+ }
+ }
+ }
+ vlc_mutex_unlock( &rtsp->lock );
+ break;
+ }
+
+ default:
+ return VLC_EGENERIC;
+ }
if( psz_session )
httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
+
+ httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
+ httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
+
+ psz = httpd_MsgGet( query, "Cseq" );
+ if( psz != NULL )
+ httpd_MsgAdd( answer, "Cseq", "%s", psz );
+ psz = httpd_MsgGet( query, "Timestamp" );
+ if( psz != NULL )
+ httpd_MsgAdd( answer, "Timestamp", "%s", psz );
+
return VLC_SUCCESS;
}
+
+
+/** Aggregate RTSP callback */
+static int RtspCallback( httpd_callback_sys_t *p_args,
+ httpd_client_t *cl,
+ httpd_message_t *answer,
+ const httpd_message_t *query )
+{
+ return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
+}
+
+
+/** Non-aggregate RTSP callback */
+static int RtspCallbackId( httpd_callback_sys_t *p_args,
+ httpd_client_t *cl,
+ httpd_message_t *answer,
+ const httpd_message_t *query )
+{
+ rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
+ return RtspHandler( id->stream, id, cl, answer, query );
+}