1 /*****************************************************************************
2 * rtsp.c: RTSP support for RTP stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
5 * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc_httpd.h>
32 #include <vlc_network.h>
36 /* For unicast/interleaved streaming */
40 int64_t i_last; /* for timeout */
42 /* is it in "play" state */
45 /* output (id-access) */
47 sout_stream_id_t **id;
49 sout_access_out_t **access;
52 static int RtspCallback( httpd_callback_sys_t *p_args,
54 httpd_message_t *answer, httpd_message_t *query );
58 int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
60 sout_stream_sys_t *p_sys = p_stream->p_sys;
62 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
64 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
65 if( p_sys->p_rtsp_host == NULL )
70 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
71 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
72 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
73 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
75 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
76 if( p_sys->p_rtsp_url == 0 )
80 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
81 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
82 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
83 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
84 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
90 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
92 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
94 rtsp->psz_session = strdup( psz_session );
96 rtsp->b_playing = VLC_FALSE;
102 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
108 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
112 if( !psz_session ) return NULL;
114 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
116 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
118 return p_stream->p_sys->rtsp[i];
125 /*static*/ void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
128 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
130 for( i = 0; i < rtsp->i_access; i++ )
132 sout_AccessOutDelete( rtsp->access[i] );
134 if( rtsp->id ) free( rtsp->id );
135 if( rtsp->access ) free( rtsp->access );
137 free( rtsp->psz_session );
142 /** Aggregate RTSP callback */
143 static int RtspCallback( httpd_callback_sys_t *p_args,
145 httpd_message_t *answer, httpd_message_t *query )
147 sout_stream_t *p_stream = (sout_stream_t*)p_args;
148 sout_stream_sys_t *p_sys = p_stream->p_sys;
149 char *psz_destination = p_sys->psz_destination;
150 const char *psz_session = NULL;
151 const char *psz_cseq = NULL;
153 if( answer == NULL || query == NULL )
157 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
159 answer->i_proto = HTTPD_PROTO_RTSP;
160 answer->i_version= query->i_version;
161 answer->i_type = HTTPD_MSG_ANSWER;
163 answer->p_body = NULL;
165 if( httpd_MsgGet( query, "Require" ) != NULL )
167 answer->i_status = 551;
168 httpd_MsgAdd( query, "Unsupported", "%s",
169 httpd_MsgGet( query, "Require" ) );
172 switch( query->i_type )
174 case HTTPD_MSG_DESCRIBE:
176 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
178 answer->i_status = 200;
179 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
180 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
181 answer->p_body = (uint8_t *)psz_sdp;
182 answer->i_body = strlen( psz_sdp );
186 case HTTPD_MSG_SETUP:
187 answer->i_status = 459;
193 answer->i_status = 200;
195 psz_session = httpd_MsgGet( query, "Session" );
196 rtsp = RtspClientGet( p_stream, psz_session );
197 if( rtsp && !rtsp->b_playing )
201 rtsp->b_playing = VLC_TRUE;
203 vlc_mutex_lock( &p_sys->lock_es );
204 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
206 sout_stream_id_t *id = rtsp->id[i_id];
209 for( i = 0; i < p_sys->i_es; i++ )
211 if( id == p_sys->es[i] )
214 if( i >= p_sys->i_es ) continue;
216 rtp_add_sink( id, rtsp->access[i_id] );
218 vlc_mutex_unlock( &p_sys->lock_es );
223 case HTTPD_MSG_PAUSE:
224 answer->i_status = 405;
225 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
228 case HTTPD_MSG_TEARDOWN:
232 /* for now only multicast so easy again */
233 answer->i_status = 200;
235 psz_session = httpd_MsgGet( query, "Session" );
236 rtsp = RtspClientGet( p_stream, psz_session );
241 vlc_mutex_lock( &p_sys->lock_es );
242 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
244 sout_stream_id_t *id = rtsp->id[i_id];
247 for( i = 0; i < p_sys->i_es; i++ )
249 if( id == p_sys->es[i] )
252 if( i >= p_sys->i_es ) continue;
254 rtp_del_sink( id, rtsp->access[i_id] );
256 vlc_mutex_unlock( &p_sys->lock_es );
258 RtspClientDel( p_stream, rtsp );
267 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
268 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
269 psz_cseq = httpd_MsgGet( query, "Cseq" );
271 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
272 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
275 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
280 /** Finds the next transport choice */
281 static inline const char *transport_next( const char *str )
283 /* Looks for comma */
284 str = strchr( str, ',' );
286 return NULL; /* No more transport options */
288 str++; /* skips comma */
289 while( strchr( "\r\n\t ", *str ) )
292 return (*str) ? str : NULL;
296 /** Finds the next transport parameter */
297 static inline const char *parameter_next( const char *str )
299 while( strchr( ",;", *str ) == NULL )
302 return (*str == ';') ? (str + 1) : NULL;
306 /** Non-aggregate RTSP callback */
307 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
309 httpd_message_t *answer, httpd_message_t *query )
311 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
312 sout_stream_t *p_stream = id->p_stream;
313 sout_stream_sys_t *p_sys = p_stream->p_sys;
314 char psz_session_init[21];
315 const char *psz_session;
316 const char *psz_cseq;
318 if( answer == NULL || query == NULL )
320 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
323 answer->i_proto = HTTPD_PROTO_RTSP;
324 answer->i_version= query->i_version;
325 answer->i_type = HTTPD_MSG_ANSWER;
327 answer->p_body = NULL;
329 /* Create new session ID if needed */
330 psz_session = httpd_MsgGet( query, "Session" );
331 if( psz_session == NULL )
333 /* FIXME: should be somewhat secure randomness */
334 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
335 NTPtime64() + rand() );
338 if( httpd_MsgGet( query, "Require" ) != NULL )
340 answer->i_status = 551;
341 httpd_MsgAdd( query, "Unsupported", "%s",
342 httpd_MsgGet( query, "Require" ) );
345 switch( query->i_type )
347 case HTTPD_MSG_SETUP:
349 answer->i_status = 461;
351 for( const char *tpt = httpd_MsgGet( query, "Transport" );
353 tpt = transport_next( tpt ) )
355 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
356 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
358 /* Check transport protocol. */
359 /* Currently, we only support RTP/AVP over UDP */
360 if( strncmp( tpt, "RTP/AVP", 7 ) )
363 if( strncmp( tpt, "/UDP", 4 ) == 0 )
365 if( strchr( ";,", *tpt ) == NULL )
368 /* Parse transport options */
369 for( const char *opt = parameter_next( tpt );
371 opt = parameter_next( opt ) )
373 if( strncmp( opt, "multicast", 9 ) == 0)
374 b_multicast = VLC_TRUE;
376 if( strncmp( opt, "unicast", 7 ) == 0 )
377 b_multicast = VLC_FALSE;
379 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
382 if( strncmp( opt, "mode=", 5 ) == 0 )
384 if( strncasecmp( opt + 5, "play", 4 )
385 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
395 * Every other option is unsupported:
397 * "source" and "append" are invalid.
399 * For multicast, "port", "layers", "ttl" are set by the
400 * stream output configuration.
402 * For unicast, we do not allow "destination" as it
403 * carries a DoS risk, and we decide on "server_port".
405 * "interleaved" and "ssrc" are not implemented.
417 if( p_sys->psz_destination == NULL )
420 answer->i_status = 200;
422 httpd_MsgAdd( answer, "Transport",
423 "RTP/AVP/UDP;destination=%s;port=%d-%d;"
425 p_sys->psz_destination, id->i_port, id->i_port+1,
426 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
430 char ip[NI_MAXNUMERICHOST], psz_access[22],
431 url[NI_MAXNUMERICHOST + 8];
432 sout_access_out_t *p_access;
433 rtsp_client_t *rtsp = NULL;
435 if( ( hiport - loport ) > 1 )
438 if( psz_session == NULL )
440 psz_session = psz_session_init;
441 rtsp = RtspClientNew( p_stream, psz_session );
445 /* FIXME: we probably need to remove an access out,
446 * if there is already one for the same ID */
447 rtsp = RtspClientGet( p_stream, psz_session );
450 answer->i_status = 454;
455 if( httpd_ClientIP( cl, ip ) == NULL )
457 answer->i_status = 500;
461 if( p_sys->i_ttl > 0 )
462 snprintf( psz_access, sizeof( psz_access ),
463 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
465 strcpy( psz_access, "udp{raw,rtcp}" );
467 snprintf( url, sizeof( url ),
468 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
471 p_access = sout_AccessOutNew( p_stream->p_sout,
473 if( p_access == NULL )
476 "cannot create access output for %s://%s",
478 answer->i_status = 500;
482 TAB_APPEND( rtsp->i_id, rtsp->id, id );
483 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
485 char *src = var_GetNonEmptyString (p_access, "src-addr");
486 int sport = var_GetInteger (p_access, "src-port");
488 httpd_ServerIP( cl, ip );
490 if( ( src != NULL ) && strcmp( src, ip ) )
492 /* Specify source IP if it is different from the RTSP
493 * control connection server address */
494 char *ptr = strchr( src, '%' );
495 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
497 httpd_MsgAdd( answer, "Transport",
498 "RTP/AVP/UDP;unicast;source=%s;"
499 "client_port=%u-%u;server_port=%u-%u;"
501 src, loport, hiport, sport, sport + 1 );
505 httpd_MsgAdd( answer, "Transport",
506 "RTP/AVP/UDP;unicast;"
507 "client_port=%u-%u;server_port=%u-%u;"
509 loport, hiport, sport, sport + 1 );
512 answer->i_status = 200;
521 answer->i_status = 460;
525 psz_cseq = httpd_MsgGet( query, "Cseq" );
527 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
528 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
529 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
530 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
533 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );