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 );
55 static int RtspCallbackId( httpd_callback_sys_t *p_args,
57 httpd_message_t *answer, httpd_message_t *query );
58 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp );
60 int RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
62 sout_stream_sys_t *p_sys = p_stream->p_sys;
64 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
66 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
67 if( p_sys->p_rtsp_host == NULL )
72 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
73 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
74 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
75 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
77 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
78 if( p_sys->p_rtsp_url == NULL )
82 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
83 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
84 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
85 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
86 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
92 void RtspUnsetup( sout_stream_t *p_stream )
94 sout_stream_sys_t *p_sys = p_stream->p_sys;
95 while( p_sys->i_rtsp > 0 )
96 RtspClientDel( p_stream, p_sys->rtsp[0] );
98 if( p_sys->p_rtsp_url )
99 httpd_UrlDelete( p_sys->p_rtsp_url );
101 if( p_sys->p_rtsp_host )
102 httpd_HostDelete( p_sys->p_rtsp_host );
106 int RtspAddId( sout_stream_t *p_stream, sout_stream_id_t *id )
108 sout_stream_sys_t *p_sys = p_stream->p_sys;
109 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
111 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
112 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
113 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
117 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void*)id );
118 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
119 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallbackId, (void*)id );
120 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallbackId, (void*)id );
121 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void*)id );
128 void RtspDelId( sout_stream_t *p_stream, sout_stream_id_t *id )
130 httpd_UrlDelete( id->p_rtsp_url );
134 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
136 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
138 rtsp->psz_session = strdup( psz_session );
140 rtsp->b_playing = VLC_FALSE;
146 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
152 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
156 if( !psz_session ) return NULL;
158 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
160 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
162 return p_stream->p_sys->rtsp[i];
169 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
172 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
174 for( i = 0; i < rtsp->i_access; i++ )
176 sout_AccessOutDelete( rtsp->access[i] );
178 if( rtsp->id ) free( rtsp->id );
179 if( rtsp->access ) free( rtsp->access );
181 free( rtsp->psz_session );
186 /** Aggregate RTSP callback */
187 static int RtspCallback( httpd_callback_sys_t *p_args,
189 httpd_message_t *answer, httpd_message_t *query )
191 sout_stream_t *p_stream = (sout_stream_t*)p_args;
192 sout_stream_sys_t *p_sys = p_stream->p_sys;
193 char *psz_destination = p_sys->psz_destination;
194 const char *psz_session = NULL;
195 const char *psz_cseq = NULL;
197 if( answer == NULL || query == NULL )
201 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
203 answer->i_proto = HTTPD_PROTO_RTSP;
204 answer->i_version= query->i_version;
205 answer->i_type = HTTPD_MSG_ANSWER;
207 answer->p_body = NULL;
209 if( httpd_MsgGet( query, "Require" ) != NULL )
211 answer->i_status = 551;
212 httpd_MsgAdd( query, "Unsupported", "%s",
213 httpd_MsgGet( query, "Require" ) );
216 switch( query->i_type )
218 case HTTPD_MSG_DESCRIBE:
220 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
222 answer->i_status = 200;
223 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
224 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
225 answer->p_body = (uint8_t *)psz_sdp;
226 answer->i_body = strlen( psz_sdp );
230 case HTTPD_MSG_SETUP:
231 answer->i_status = 459;
237 answer->i_status = 200;
239 psz_session = httpd_MsgGet( query, "Session" );
240 rtsp = RtspClientGet( p_stream, psz_session );
241 if( rtsp && !rtsp->b_playing )
245 rtsp->b_playing = VLC_TRUE;
247 vlc_mutex_lock( &p_sys->lock_es );
248 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
250 sout_stream_id_t *id = rtsp->id[i_id];
253 for( i = 0; i < p_sys->i_es; i++ )
255 if( id == p_sys->es[i] )
258 if( i >= p_sys->i_es ) continue;
260 rtp_add_sink( id, rtsp->access[i_id] );
262 vlc_mutex_unlock( &p_sys->lock_es );
267 case HTTPD_MSG_PAUSE:
268 answer->i_status = 405;
269 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
272 case HTTPD_MSG_TEARDOWN:
276 /* for now only multicast so easy again */
277 answer->i_status = 200;
279 psz_session = httpd_MsgGet( query, "Session" );
280 rtsp = RtspClientGet( p_stream, psz_session );
285 vlc_mutex_lock( &p_sys->lock_es );
286 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
288 sout_stream_id_t *id = rtsp->id[i_id];
291 for( i = 0; i < p_sys->i_es; i++ )
293 if( id == p_sys->es[i] )
296 if( i >= p_sys->i_es ) continue;
298 rtp_del_sink( id, rtsp->access[i_id] );
300 vlc_mutex_unlock( &p_sys->lock_es );
302 RtspClientDel( p_stream, rtsp );
311 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
312 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
313 psz_cseq = httpd_MsgGet( query, "Cseq" );
315 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
316 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
319 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
324 /** Finds the next transport choice */
325 static inline const char *transport_next( const char *str )
327 /* Looks for comma */
328 str = strchr( str, ',' );
330 return NULL; /* No more transport options */
332 str++; /* skips comma */
333 while( strchr( "\r\n\t ", *str ) )
336 return (*str) ? str : NULL;
340 /** Finds the next transport parameter */
341 static inline const char *parameter_next( const char *str )
343 while( strchr( ",;", *str ) == NULL )
346 return (*str == ';') ? (str + 1) : NULL;
350 /** Non-aggregate RTSP callback */
351 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
353 httpd_message_t *answer, httpd_message_t *query )
355 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
356 sout_stream_t *p_stream = id->p_stream;
357 sout_stream_sys_t *p_sys = p_stream->p_sys;
358 char psz_session_init[21];
359 const char *psz_session;
360 const char *psz_cseq;
362 if( answer == NULL || query == NULL )
364 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
367 answer->i_proto = HTTPD_PROTO_RTSP;
368 answer->i_version= query->i_version;
369 answer->i_type = HTTPD_MSG_ANSWER;
371 answer->p_body = NULL;
373 /* Create new session ID if needed */
374 psz_session = httpd_MsgGet( query, "Session" );
375 if( psz_session == NULL )
377 /* FIXME: should be somewhat secure randomness */
378 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
379 NTPtime64() + rand() );
382 if( httpd_MsgGet( query, "Require" ) != NULL )
384 answer->i_status = 551;
385 httpd_MsgAdd( query, "Unsupported", "%s",
386 httpd_MsgGet( query, "Require" ) );
389 switch( query->i_type )
391 case HTTPD_MSG_SETUP:
393 answer->i_status = 461;
395 for( const char *tpt = httpd_MsgGet( query, "Transport" );
397 tpt = transport_next( tpt ) )
399 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
400 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
402 /* Check transport protocol. */
403 /* Currently, we only support RTP/AVP over UDP */
404 if( strncmp( tpt, "RTP/AVP", 7 ) )
407 if( strncmp( tpt, "/UDP", 4 ) == 0 )
409 if( strchr( ";,", *tpt ) == NULL )
412 /* Parse transport options */
413 for( const char *opt = parameter_next( tpt );
415 opt = parameter_next( opt ) )
417 if( strncmp( opt, "multicast", 9 ) == 0)
418 b_multicast = VLC_TRUE;
420 if( strncmp( opt, "unicast", 7 ) == 0 )
421 b_multicast = VLC_FALSE;
423 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
426 if( strncmp( opt, "mode=", 5 ) == 0 )
428 if( strncasecmp( opt + 5, "play", 4 )
429 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
439 * Every other option is unsupported:
441 * "source" and "append" are invalid.
443 * For multicast, "port", "layers", "ttl" are set by the
444 * stream output configuration.
446 * For unicast, we do not allow "destination" as it
447 * carries a DoS risk, and we decide on "server_port".
449 * "interleaved" and "ssrc" are not implemented.
461 if( p_sys->psz_destination == NULL )
464 answer->i_status = 200;
466 httpd_MsgAdd( answer, "Transport",
467 "RTP/AVP/UDP;destination=%s;port=%d-%d;"
469 p_sys->psz_destination, id->i_port, id->i_port+1,
470 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
474 char ip[NI_MAXNUMERICHOST], psz_access[22],
475 url[NI_MAXNUMERICHOST + 8];
476 sout_access_out_t *p_access;
477 rtsp_client_t *rtsp = NULL;
479 if( ( hiport - loport ) > 1 )
482 if( psz_session == NULL )
484 psz_session = psz_session_init;
485 rtsp = RtspClientNew( p_stream, psz_session );
489 /* FIXME: we probably need to remove an access out,
490 * if there is already one for the same ID */
491 rtsp = RtspClientGet( p_stream, psz_session );
494 answer->i_status = 454;
499 if( httpd_ClientIP( cl, ip ) == NULL )
501 answer->i_status = 500;
505 if( p_sys->i_ttl > 0 )
506 snprintf( psz_access, sizeof( psz_access ),
507 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
509 strcpy( psz_access, "udp{raw,rtcp}" );
511 snprintf( url, sizeof( url ),
512 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
515 p_access = sout_AccessOutNew( p_stream->p_sout,
517 if( p_access == NULL )
520 "cannot create access output for %s://%s",
522 answer->i_status = 500;
526 TAB_APPEND( rtsp->i_id, rtsp->id, id );
527 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
529 char *src = var_GetNonEmptyString (p_access, "src-addr");
530 int sport = var_GetInteger (p_access, "src-port");
532 httpd_ServerIP( cl, ip );
534 if( ( src != NULL ) && strcmp( src, ip ) )
536 /* Specify source IP if it is different from the RTSP
537 * control connection server address */
538 char *ptr = strchr( src, '%' );
539 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
541 httpd_MsgAdd( answer, "Transport",
542 "RTP/AVP/UDP;unicast;source=%s;"
543 "client_port=%u-%u;server_port=%u-%u;"
545 src, loport, hiport, sport, sport + 1 );
549 httpd_MsgAdd( answer, "Transport",
550 "RTP/AVP/UDP;unicast;"
551 "client_port=%u-%u;server_port=%u-%u;"
553 loport, hiport, sport, sport + 1 );
556 answer->i_status = 200;
565 answer->i_status = 460;
569 psz_cseq = httpd_MsgGet( query, "Cseq" );
571 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
572 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
573 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
574 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
577 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );