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>
37 typedef struct rtsp_session_t rtsp_session_t;
49 rtsp_session_t **sessionv;
53 static int RtspCallback( httpd_callback_sys_t *p_args,
55 httpd_message_t *answer, httpd_message_t *query );
56 static int RtspCallbackId( httpd_callback_sys_t *p_args,
58 httpd_message_t *answer, httpd_message_t *query );
59 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
61 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
63 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
68 rtsp->owner = p_stream;
70 rtsp->sessionv = NULL;
71 vlc_mutex_init( p_stream, &rtsp->lock );
73 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n",
74 url->psz_host, url->i_port, url->psz_path );
76 rtsp->psz_path = strdup( url->psz_path ? url->psz_path : "/" );
77 if( rtsp->psz_path == NULL )
80 if( asprintf( &rtsp->psz_control, "rtsp://%s:%d%s",
81 url->psz_host, url->i_port > 0 ? url->i_port : 554,
82 rtsp->psz_path ) == -1 )
84 rtsp->psz_control = NULL;
88 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
89 url->i_port > 0 ? url->i_port : 554 );
90 if( rtsp->host == NULL )
93 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path, NULL, NULL,
95 if( rtsp->url == NULL )
98 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
99 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
100 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
101 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
102 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
111 void RtspUnsetup( rtsp_stream_t *rtsp )
113 while( rtsp->sessionc > 0 )
114 RtspClientDel( rtsp, rtsp->sessionv[0] );
117 httpd_UrlDelete( rtsp->url );
120 httpd_HostDelete( rtsp->host );
122 vlc_mutex_destroy( &rtsp->lock );
126 struct rtsp_stream_id_t
128 rtsp_stream_t *stream;
129 sout_stream_id_t *sout_id;
133 unsigned loport, hiport;
137 /* For unicast streaming */
138 struct rtsp_session_t
140 rtsp_stream_t *stream;
142 /* is it in "play" state */
143 vlc_bool_t b_playing;
145 /* output (id-access) */
147 sout_stream_id_t **id;
149 sout_access_out_t **access;
155 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
157 /* Multicast stuff - TODO: cleanup */
158 const char *dst, int ttl,
159 unsigned loport, unsigned hiport )
161 char urlbuf[strlen( rtsp->psz_control ) + 1 + 10];
162 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
170 /* TODO: can we assume that this need not be strdup'd? */
172 if( id->dst != NULL )
179 sprintf( urlbuf, "%s/trackID=%d", rtsp->psz_path, num );
180 msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
181 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
189 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
190 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
191 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
192 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
193 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
199 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
201 vlc_mutex_lock( &rtsp->lock );
202 for( int i = 0; i < rtsp->sessionc; i++ )
204 rtsp_session_t *ses = rtsp->sessionv[i];
206 for( int j = 0; j < ses->i_id; j++ )
208 if( ses->id[j] == id->sout_id )
210 REMOVE_ELEM( ses->id, ses->i_id, j );
212 assert( ses->access[j] != NULL );
213 sout_AccessOutDelete( ses->access[j] );
214 REMOVE_ELEM( ses->access, ses->i_access, j );
215 /* FIXME: are we supposed to notify the client? */
220 vlc_mutex_unlock( &rtsp->lock );
221 httpd_UrlDelete( id->url );
226 /** rtsp must be locked */
228 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp, const char *name )
230 rtsp_session_t *s = malloc( sizeof( *s ) + strlen( name ) + 1 );
233 s->b_playing = VLC_FALSE;
234 s->i_id = s->i_access = 0;
237 strcpy( s->name, name );
239 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
245 /** rtsp must be locked */
247 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
254 /* FIXME: use a hash/dictionary */
255 for( i = 0; i < rtsp->sessionc; i++ )
257 if( !strcmp( rtsp->sessionv[i]->name, name ) )
258 return rtsp->sessionv[i];
264 /** rtsp must be locked */
266 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
269 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
271 for( i = 0; i < session->i_access; i++ )
273 rtp_del_sink( session->id[i], session->access[i] );
274 sout_AccessOutDelete( session->access[i] );
278 free( session->access );
283 /** Aggregate RTSP callback */
284 static int RtspCallback( httpd_callback_sys_t *p_args,
286 httpd_message_t *answer, httpd_message_t *query )
288 rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
289 const char *psz_session = NULL, *psz_cseq;
291 if( answer == NULL || query == NULL )
295 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
297 answer->i_proto = HTTPD_PROTO_RTSP;
298 answer->i_version= query->i_version;
299 answer->i_type = HTTPD_MSG_ANSWER;
301 answer->p_body = NULL;
303 if( httpd_MsgGet( query, "Require" ) != NULL )
305 answer->i_status = 551;
306 httpd_MsgAdd( query, "Unsupported", "%s",
307 httpd_MsgGet( query, "Require" ) );
310 switch( query->i_type )
312 case HTTPD_MSG_DESCRIBE:
314 char *psz_sdp = SDPGenerate( rtsp->owner, rtsp->psz_control );
316 answer->i_status = 200;
317 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
318 httpd_MsgAdd( answer, "Content-Base", "%s", rtsp->psz_control );
319 answer->p_body = (uint8_t *)psz_sdp;
320 answer->i_body = strlen( psz_sdp );
324 case HTTPD_MSG_SETUP:
325 answer->i_status = 459;
331 answer->i_status = 200;
333 psz_session = httpd_MsgGet( query, "Session" );
335 vlc_mutex_lock( &rtsp->lock );
336 ses = RtspClientGet( rtsp, psz_session );
337 if( ( ses != NULL ) && !ses->b_playing )
340 ses->b_playing = VLC_TRUE;
342 for( int i_id = 0; i_id < ses->i_id; i_id++ )
343 rtp_add_sink( ses->id[i_id], ses->access[i_id] );
345 vlc_mutex_unlock( &rtsp->lock );
349 case HTTPD_MSG_PAUSE:
350 answer->i_status = 405;
351 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
354 case HTTPD_MSG_TEARDOWN:
358 /* for now only multicast so easy again */
359 answer->i_status = 200;
361 psz_session = httpd_MsgGet( query, "Session" );
363 vlc_mutex_lock( &rtsp->lock );
364 ses = RtspClientGet( rtsp, psz_session );
366 RtspClientDel( rtsp, ses );
367 vlc_mutex_unlock( &rtsp->lock );
375 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
376 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
377 psz_cseq = httpd_MsgGet( query, "Cseq" );
379 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
380 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
383 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
388 /** Finds the next transport choice */
389 static inline const char *transport_next( const char *str )
391 /* Looks for comma */
392 str = strchr( str, ',' );
394 return NULL; /* No more transport options */
396 str++; /* skips comma */
397 while( strchr( "\r\n\t ", *str ) )
400 return (*str) ? str : NULL;
404 /** Finds the next transport parameter */
405 static inline const char *parameter_next( const char *str )
407 while( strchr( ",;", *str ) == NULL )
410 return (*str == ';') ? (str + 1) : NULL;
414 /** Non-aggregate RTSP callback */
415 static int RtspCallbackId( httpd_callback_sys_t *p_args,
417 httpd_message_t *answer, httpd_message_t *query )
419 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
420 sout_stream_t *p_stream = id->stream->owner;
421 sout_stream_id_t *sid = id->sout_id;
422 char psz_session_init[21];
423 const char *psz_session;
424 const char *psz_cseq;
426 if( answer == NULL || query == NULL )
428 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
431 answer->i_proto = HTTPD_PROTO_RTSP;
432 answer->i_version= query->i_version;
433 answer->i_type = HTTPD_MSG_ANSWER;
435 answer->p_body = NULL;
437 /* Create new session ID if needed */
438 psz_session = httpd_MsgGet( query, "Session" );
439 if( psz_session == NULL )
441 /* FIXME: should be somewhat secure randomness */
442 snprintf( psz_session_init, sizeof(psz_session_init), I64Fu,
443 NTPtime64() + rand() );
446 if( httpd_MsgGet( query, "Require" ) != NULL )
448 answer->i_status = 551;
449 httpd_MsgAdd( query, "Unsupported", "%s",
450 httpd_MsgGet( query, "Require" ) );
453 switch( query->i_type )
455 case HTTPD_MSG_SETUP:
457 answer->i_status = 461;
459 for( const char *tpt = httpd_MsgGet( query, "Transport" );
461 tpt = transport_next( tpt ) )
463 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
464 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
466 /* Check transport protocol. */
467 /* Currently, we only support RTP/AVP over UDP */
468 if( strncmp( tpt, "RTP/AVP", 7 ) )
471 if( strncmp( tpt, "/UDP", 4 ) == 0 )
473 if( strchr( ";,", *tpt ) == NULL )
476 /* Parse transport options */
477 for( const char *opt = parameter_next( tpt );
479 opt = parameter_next( opt ) )
481 if( strncmp( opt, "multicast", 9 ) == 0)
482 b_multicast = VLC_TRUE;
484 if( strncmp( opt, "unicast", 7 ) == 0 )
485 b_multicast = VLC_FALSE;
487 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
490 if( strncmp( opt, "mode=", 5 ) == 0 )
492 if( strncasecmp( opt + 5, "play", 4 )
493 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
503 * Every other option is unsupported:
505 * "source" and "append" are invalid.
507 * For multicast, "port", "layers", "ttl" are set by the
508 * stream output configuration.
510 * For unicast, we do not allow "destination" as it
511 * carries a DoS risk, and we decide on "server_port".
513 * "interleaved" and "ssrc" are not implemented.
525 const char *dst = id->dst;
529 answer->i_status = 200;
531 httpd_MsgAdd( answer, "Transport",
532 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
534 dst, id->loport, id->hiport,
535 ( id->ttl > 0 ) ? id->ttl : 1 );
539 char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
540 static const char access[] = "udp{raw,rtcp}";
541 sout_access_out_t *p_access;
542 rtsp_session_t *ses = NULL;
544 if( httpd_ClientIP( cl, ip ) == NULL )
546 answer->i_status = 500;
550 snprintf( url, sizeof( url ),
551 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
554 p_access = sout_AccessOutNew( p_stream->p_sout, access,
556 if( p_access == NULL )
559 "cannot create access output for %s://%s",
561 answer->i_status = 500;
565 vlc_mutex_lock( &id->stream->lock );
566 if( psz_session == NULL )
568 psz_session = psz_session_init;
569 ses = RtspClientNew( id->stream, psz_session );
573 /* FIXME: we probably need to remove an access out,
574 * if there is already one for the same ID */
575 ses = RtspClientGet( id->stream, psz_session );
578 answer->i_status = 454;
579 vlc_mutex_unlock( &id->stream->lock );
584 assert( ses->i_id == ses->i_access );
585 TAB_APPEND( ses->i_id, ses->id, sid );
586 TAB_APPEND( ses->i_access, ses->access, p_access );
587 assert( ses->i_id == ses->i_access );
588 vlc_mutex_unlock( &id->stream->lock );
590 char *src = var_GetNonEmptyString (p_access, "src-addr");
591 int sport = var_GetInteger (p_access, "src-port");
593 httpd_ServerIP( cl, ip );
595 if( ( src != NULL ) && strcmp( src, ip ) )
597 /* Specify source IP if it is different from the RTSP
598 * control connection server address */
599 char *ptr = strchr( src, '%' );
600 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
602 httpd_MsgAdd( answer, "Transport",
603 "RTP/AVP/UDP;unicast;source=%s;"
604 "client_port=%u-%u;server_port=%u-%u;"
606 src, loport, loport + 1, sport, sport + 1 );
610 httpd_MsgAdd( answer, "Transport",
611 "RTP/AVP/UDP;unicast;"
612 "client_port=%u-%u;server_port=%u-%u;"
614 loport, loport + 1, sport, sport + 1 );
617 answer->i_status = 200;
626 answer->i_status = 460;
630 psz_cseq = httpd_MsgGet( query, "Cseq" );
632 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
633 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
634 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
635 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
638 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );