#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>
sout_stream_t *owner;
httpd_host_t *host;
httpd_url_t *url;
- char *psz_control;
char *psz_path;
+ unsigned port;
int sessionc;
rtsp_session_t **sessionv;
{
rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
- if( rtsp == NULL )
+ if( rtsp == NULL || ( url->i_port > 99999 ) )
return NULL;
rtsp->owner = p_stream;
msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n",
url->psz_host, url->i_port, url->psz_path );
- rtsp->psz_path = strdup( url->psz_path ? url->psz_path : "/" );
- if( rtsp->psz_path == NULL )
- goto error;
-
- if( asprintf( &rtsp->psz_control, "rtsp://%s:%d%s",
- url->psz_host, url->i_port > 0 ? url->i_port : 554,
- rtsp->psz_path ) == -1 )
- {
- rtsp->psz_control = NULL;
- goto error;
- }
+ rtsp->port = (url->i_port > 0) ? url->i_port : 554;
+ if( url->psz_path != NULL )
+ rtsp->psz_path = strdup( url->psz_path + 1 );
+ else
+ rtsp->psz_path = NULL;
rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
- url->i_port > 0 ? url->i_port : 554 );
+ rtsp->port );
if( rtsp->host == NULL )
goto error;
- rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path, NULL, NULL,
- NULL );
+ rtsp->url = httpd_UrlNewUnique( rtsp->host,
+ url->psz_path ? url->psz_path : "/", NULL,
+ NULL, NULL );
if( rtsp->url == NULL )
goto error;
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;
struct rtsp_strack_t
{
sout_stream_id_t *id;
- sout_access_out_t *access;
+ int fd;
vlc_bool_t playing;
};
const char *dst, int ttl,
unsigned loport, unsigned hiport )
{
- char urlbuf[strlen( rtsp->psz_control ) + 1 + 10];
+ char urlbuf[sizeof( "//trackID=123" ) + strlen( rtsp->psz_path )];
rtsp_stream_id_t *id = malloc( sizeof( *id ) );
httpd_url_t *url;
id->hiport = hiport;
}
- sprintf( urlbuf, "%s/trackID=%d", rtsp->psz_path, num );
+ snprintf( urlbuf, sizeof( urlbuf ), "/%s/trackID=%u", rtsp->psz_path,
+ num );
msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
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 );
return id;
if( ses->trackv[j].id == id->sout_id )
{
rtsp_strack_t *tr = ses->trackv + j;
- sout_AccessOutDelete( tr->access );
+ net_Close( tr->fd );
REMOVE_ELEM( ses->trackv, ses->trackc, j );
- /* FIXME: are we supposed to notify the client? */
}
}
}
return NULL;
s->stream = rtsp;
- s->id = rand(); /* FIXME: not enough entropy */
+ vlc_rand_bytes (&s->id, sizeof (s->id));
s->trackc = 0;
s->trackv = NULL;
TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
for( i = 0; i < session->trackc; i++ )
- {
- rtp_del_sink( session->trackv[i].id, session->trackv[i].access );
- sout_AccessOutDelete( session->trackv[i].access );
- }
+ rtp_del_sink( session->trackv[i].id, session->trackv[i].fd );
free( session->trackv );
free( session );
}
-/** 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 )
-{
- rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
- const char *psz_session = NULL, *psz;
-
- if( answer == NULL || query == NULL )
- {
- return VLC_SUCCESS;
- }
- //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;
-
- if( httpd_MsgGet( query, "Require" ) != NULL )
- {
- answer->i_status = 551;
- httpd_MsgAdd( answer, "Unsupported", "%s",
- httpd_MsgGet( query, "Require" ) );
- }
- else
- switch( query->i_type )
- {
- case HTTPD_MSG_DESCRIBE:
- {
- char *psz_sdp = SDPGenerate( rtsp->owner, rtsp->psz_control );
-
- answer->i_status = 200;
- httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
- httpd_MsgAdd( answer, "Content-Base", "%s", rtsp->psz_control );
- answer->p_body = (uint8_t *)psz_sdp;
- answer->i_body = strlen( psz_sdp );
- break;
- }
-
- case HTTPD_MSG_SETUP:
- answer->i_status = 459;
- break;
-
- case HTTPD_MSG_PLAY:
- {
- rtsp_session_t *ses;
- answer->i_status = 200;
-
- psz_session = httpd_MsgGet( query, "Session" );
- if( httpd_MsgGet( query, "Range" ) != NULL )
- {
- answer->i_status = 456; /* cannot seek */
- break;
- }
-
- vlc_mutex_lock( &rtsp->lock );
- ses = RtspClientGet( rtsp, psz_session );
- if( ses != NULL )
- {
- for( int i = 0; i < ses->trackc; i++ )
- {
- rtsp_strack_t *tr = ses->trackv + i;
- if( !tr->playing )
- {
- tr->playing = VLC_TRUE;
- rtp_add_sink( tr->id, tr->access );
- }
- }
- }
- vlc_mutex_unlock( &rtsp->lock );
-
- if( httpd_MsgGet( query, "Scale" ) != NULL )
- httpd_MsgAdd( answer, "Scale", "1." );
- break;
- }
-
- case HTTPD_MSG_PAUSE:
- answer->i_status = 405;
- httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
- break;
-
- case HTTPD_MSG_TEARDOWN:
- {
- rtsp_session_t *ses;
-
- /* for now only multicast so easy again */
- answer->i_status = 200;
-
- psz_session = httpd_MsgGet( query, "Session" );
-
- vlc_mutex_lock( &rtsp->lock );
- ses = RtspClientGet( rtsp, psz_session );
- if( ses != NULL )
- RtspClientDel( rtsp, ses );
- vlc_mutex_unlock( &rtsp->lock );
- break;
- }
-
- default:
- return VLC_EGENERIC;
- }
-
- httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
- httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
-
- 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 );
-
- httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
-
- if( psz_session )
- httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
- return VLC_SUCCESS;
-}
-
-
/** Finds the next transport choice */
static inline const char *transport_next( const char *str )
{
}
-/** 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 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 )
{
- rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
- rtsp_stream_t *rtsp = id->stream;
- sout_stream_t *p_stream = id->stream->owner;
+ sout_stream_t *p_stream = rtsp->owner;
char psz_sesbuf[17];
- const char *psz_session, *psz;
+ const char *psz_session = NULL, *psz;
- if( answer == NULL || query == NULL )
+ if( answer == NULL || query == NULL || cl == NULL )
return VLC_SUCCESS;
/* */
answer->i_body = 0;
answer->p_body = NULL;
- psz_session = httpd_MsgGet( query, "Session" );
-
if( httpd_MsgGet( query, "Require" ) != NULL )
{
answer->i_status = 551;
else
switch( query->i_type )
{
+ case HTTPD_MSG_DESCRIBE:
+ { /* Aggregate-only */
+ if( id != NULL )
+ {
+ answer->i_status = 460;
+ break;
+ }
+
+ char ip[NI_MAXNUMERICHOST], *ptr;
+ char control[sizeof("rtsp://[]:12345/") + sizeof( ip )
+ + strlen( rtsp->psz_path )];
+
+ /* Build self-referential URL */
+ 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 != NULL ) ? rtsp->psz_path : "" );
+ else
+ sprintf( control, "rtsp://%s:%u/%s", ip, rtsp->port,
+ ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
+
+ ptr = SDPGenerate( rtsp->owner, control );
+
+ answer->i_status = 200;
+ httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
+ httpd_MsgAdd( answer, "Content-Base", "%s", control );
+ answer->p_body = (uint8_t *)ptr;
+ answer->i_body = strlen( ptr );
+ 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" );
if( strncmp( opt, "unicast", 7 ) == 0 )
b_multicast = VLC_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 )
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",
}
else
{
- char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
- static const char access[] = "udp{raw,rtcp}";
+ char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
rtsp_session_t *ses = NULL;
- rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
+ rtsp_strack_t track = { id->sout_id, -1, VLC_FALSE };
+ int sport;
if( httpd_ClientIP( cl, ip ) == NULL )
{
continue;
}
- snprintf( url, sizeof( url ),
- ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
- ip, loport );
-
- track.access = sout_AccessOutNew( p_stream->p_sout,
- access, url );
- if( track.access == NULL )
+ track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
+ IPPROTO_UDP );
+ if( track.fd == -1 )
{
msg_Err( p_stream,
- "cannot create access output for %s://%s",
- access, url );
+ "cannot create RTP socket for %s port %u",
+ ip, loport );
answer->i_status = 500;
continue;
}
- char *src = var_GetNonEmptyString( track.access, "src-addr" );
- int sport = var_GetInteger( track.access, "src-port" );
+ net_GetSockAddress( track.fd, src, &sport );
vlc_mutex_lock( &rtsp->lock );
if( psz_session == NULL )
}
}
- INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
+ 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 */
"RTP/AVP/UDP;unicast;source=%s;"
"client_port=%u-%u;server_port=%u-%u;"
"mode=play",
- src, loport, loport + 1, sport, sport + 1 );
+ src, loport, loport + 1, sport,
+ sport + 1 );
}
else
{
}
answer->i_status = 200;
- free( src );
}
break;
}
break;
- }
case HTTPD_MSG_PLAY:
{
for( int i = 0; i < ses->trackc; i++ )
{
rtsp_strack_t *tr = ses->trackv + i;
- if( !tr->playing && ( tr->id == id->sout_id ) )
+ if( !tr->playing
+ && ( ( id == NULL ) || ( tr->id == id->sout_id ) ) )
{
tr->playing = VLC_TRUE;
- rtp_add_sink( tr->id, tr->access );
+ rtp_add_sink( tr->id, tr->fd );
}
}
}
case HTTPD_MSG_PAUSE:
answer->i_status = 405;
- httpd_MsgAdd( answer, "Allow", "SETUP, PLAY, TEARDOWN" );
+ httpd_MsgAdd( answer, "Allow",
+ "%s, TEARDOWN, PLAY, GET_PARAMETER",
+ ( id != NULL ) ? "SETUP" : "DESCRIBE" );
+ break;
+
+ 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:
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->sout_id )
{
- rtp_del_sink( id->sout_id, ses->trackv[i].access );
- sout_AccessOutDelete( ses->trackv[i].access );
+ rtp_del_sink( id->sout_id, ses->trackv[i].fd );
REMOVE_ELEM( ses->trackv, ses->trackc, i );
}
}
}
default:
- answer->i_status = 460;
- break;
+ return VLC_EGENERIC;
}
+ httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
+ 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 );
- httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
- httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
- httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
- if( psz_session )
- httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
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 );
+}