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 struct rtsp_stream_id_t
108 sout_stream_t *sout_stream;
109 sout_stream_id_t *sout_id;
111 unsigned loport, hiport;
114 rtsp_stream_id_t *RtspAddId( sout_stream_t *p_stream, sout_stream_id_t *sid,
115 unsigned loport, unsigned hiport )
117 sout_stream_sys_t *p_sys = p_stream->p_sys;
118 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
119 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
125 id->sout_stream = p_stream;
130 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
131 msg_Dbg( p_stream, "RTSP: adding %s\n", psz_urlc );
133 httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
141 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
142 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
143 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
144 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
145 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
151 void RtspDelId( sout_stream_t *p_stream, rtsp_stream_id_t *id )
153 httpd_UrlDelete( id->url );
158 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
160 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
162 rtsp->psz_session = strdup( psz_session );
164 rtsp->b_playing = VLC_FALSE;
170 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
176 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
180 if( !psz_session ) return NULL;
182 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
184 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
186 return p_stream->p_sys->rtsp[i];
193 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
196 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
198 for( i = 0; i < rtsp->i_access; i++ )
200 sout_AccessOutDelete( rtsp->access[i] );
202 if( rtsp->id ) free( rtsp->id );
203 if( rtsp->access ) free( rtsp->access );
205 free( rtsp->psz_session );
210 /** Aggregate RTSP callback */
211 static int RtspCallback( httpd_callback_sys_t *p_args,
213 httpd_message_t *answer, httpd_message_t *query )
215 sout_stream_t *p_stream = (sout_stream_t*)p_args;
216 sout_stream_sys_t *p_sys = p_stream->p_sys;
217 char *psz_destination = p_sys->psz_destination;
218 const char *psz_session = NULL;
219 const char *psz_cseq = NULL;
221 if( answer == NULL || query == NULL )
225 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
227 answer->i_proto = HTTPD_PROTO_RTSP;
228 answer->i_version= query->i_version;
229 answer->i_type = HTTPD_MSG_ANSWER;
231 answer->p_body = NULL;
233 if( httpd_MsgGet( query, "Require" ) != NULL )
235 answer->i_status = 551;
236 httpd_MsgAdd( query, "Unsupported", "%s",
237 httpd_MsgGet( query, "Require" ) );
240 switch( query->i_type )
242 case HTTPD_MSG_DESCRIBE:
244 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
246 answer->i_status = 200;
247 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
248 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
249 answer->p_body = (uint8_t *)psz_sdp;
250 answer->i_body = strlen( psz_sdp );
254 case HTTPD_MSG_SETUP:
255 answer->i_status = 459;
261 answer->i_status = 200;
263 psz_session = httpd_MsgGet( query, "Session" );
264 rtsp = RtspClientGet( p_stream, psz_session );
265 if( rtsp && !rtsp->b_playing )
269 rtsp->b_playing = VLC_TRUE;
271 vlc_mutex_lock( &p_sys->lock_es );
272 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
274 sout_stream_id_t *id = rtsp->id[i_id];
277 for( i = 0; i < p_sys->i_es; i++ )
279 if( id == p_sys->es[i] )
282 if( i >= p_sys->i_es ) continue;
284 rtp_add_sink( id, rtsp->access[i_id] );
286 vlc_mutex_unlock( &p_sys->lock_es );
291 case HTTPD_MSG_PAUSE:
292 answer->i_status = 405;
293 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
296 case HTTPD_MSG_TEARDOWN:
300 /* for now only multicast so easy again */
301 answer->i_status = 200;
303 psz_session = httpd_MsgGet( query, "Session" );
304 rtsp = RtspClientGet( p_stream, psz_session );
309 vlc_mutex_lock( &p_sys->lock_es );
310 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
312 sout_stream_id_t *id = rtsp->id[i_id];
315 for( i = 0; i < p_sys->i_es; i++ )
317 if( id == p_sys->es[i] )
320 if( i >= p_sys->i_es ) continue;
322 rtp_del_sink( id, rtsp->access[i_id] );
324 vlc_mutex_unlock( &p_sys->lock_es );
326 RtspClientDel( p_stream, rtsp );
335 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
336 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
337 psz_cseq = httpd_MsgGet( query, "Cseq" );
339 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
340 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
343 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
348 /** Finds the next transport choice */
349 static inline const char *transport_next( const char *str )
351 /* Looks for comma */
352 str = strchr( str, ',' );
354 return NULL; /* No more transport options */
356 str++; /* skips comma */
357 while( strchr( "\r\n\t ", *str ) )
360 return (*str) ? str : NULL;
364 /** Finds the next transport parameter */
365 static inline const char *parameter_next( const char *str )
367 while( strchr( ",;", *str ) == NULL )
370 return (*str == ';') ? (str + 1) : NULL;
374 /** Non-aggregate RTSP callback */
375 static int RtspCallbackId( httpd_callback_sys_t *p_args,
377 httpd_message_t *answer, httpd_message_t *query )
379 rtsp_stream_id_t *id = (rtsp_stream_id_t*)p_args;
380 sout_stream_t *p_stream = id->sout_stream;
381 sout_stream_id_t *sid = id->sout_id;
382 sout_stream_sys_t *p_sys = p_stream->p_sys;
383 char psz_session_init[21];
384 const char *psz_session;
385 const char *psz_cseq;
387 if( answer == NULL || query == NULL )
389 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
392 answer->i_proto = HTTPD_PROTO_RTSP;
393 answer->i_version= query->i_version;
394 answer->i_type = HTTPD_MSG_ANSWER;
396 answer->p_body = NULL;
398 /* Create new session ID if needed */
399 psz_session = httpd_MsgGet( query, "Session" );
400 if( psz_session == NULL )
402 /* FIXME: should be somewhat secure randomness */
403 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
404 NTPtime64() + rand() );
407 if( httpd_MsgGet( query, "Require" ) != NULL )
409 answer->i_status = 551;
410 httpd_MsgAdd( query, "Unsupported", "%s",
411 httpd_MsgGet( query, "Require" ) );
414 switch( query->i_type )
416 case HTTPD_MSG_SETUP:
418 answer->i_status = 461;
420 for( const char *tpt = httpd_MsgGet( query, "Transport" );
422 tpt = transport_next( tpt ) )
424 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
425 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
427 /* Check transport protocol. */
428 /* Currently, we only support RTP/AVP over UDP */
429 if( strncmp( tpt, "RTP/AVP", 7 ) )
432 if( strncmp( tpt, "/UDP", 4 ) == 0 )
434 if( strchr( ";,", *tpt ) == NULL )
437 /* Parse transport options */
438 for( const char *opt = parameter_next( tpt );
440 opt = parameter_next( opt ) )
442 if( strncmp( opt, "multicast", 9 ) == 0)
443 b_multicast = VLC_TRUE;
445 if( strncmp( opt, "unicast", 7 ) == 0 )
446 b_multicast = VLC_FALSE;
448 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
451 if( strncmp( opt, "mode=", 5 ) == 0 )
453 if( strncasecmp( opt + 5, "play", 4 )
454 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
464 * Every other option is unsupported:
466 * "source" and "append" are invalid.
468 * For multicast, "port", "layers", "ttl" are set by the
469 * stream output configuration.
471 * For unicast, we do not allow "destination" as it
472 * carries a DoS risk, and we decide on "server_port".
474 * "interleaved" and "ssrc" are not implemented.
486 if( p_sys->psz_destination == NULL )
489 answer->i_status = 200;
491 httpd_MsgAdd( answer, "Transport",
492 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
494 p_sys->psz_destination, id->loport,
496 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
500 char ip[NI_MAXNUMERICHOST], psz_access[22],
501 url[NI_MAXNUMERICHOST + 8];
502 sout_access_out_t *p_access;
503 rtsp_client_t *rtsp = NULL;
505 if( ( hiport - loport ) != ( id->hiport - id->loport ) )
508 if( psz_session == NULL )
510 psz_session = psz_session_init;
511 rtsp = RtspClientNew( p_stream, psz_session );
515 /* FIXME: we probably need to remove an access out,
516 * if there is already one for the same ID */
517 rtsp = RtspClientGet( p_stream, psz_session );
520 answer->i_status = 454;
525 if( httpd_ClientIP( cl, ip ) == NULL )
527 answer->i_status = 500;
531 if( p_sys->i_ttl > 0 )
532 snprintf( psz_access, sizeof( psz_access ),
533 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
535 strcpy( psz_access, "udp{raw,rtcp}" );
537 snprintf( url, sizeof( url ),
538 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
541 p_access = sout_AccessOutNew( p_stream->p_sout,
543 if( p_access == NULL )
546 "cannot create access output for %s://%s",
548 answer->i_status = 500;
552 TAB_APPEND( rtsp->i_id, rtsp->id, sid );
553 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
555 char *src = var_GetNonEmptyString (p_access, "src-addr");
556 int sport = var_GetInteger (p_access, "src-port");
558 httpd_ServerIP( cl, ip );
560 if( ( src != NULL ) && strcmp( src, ip ) )
562 /* Specify source IP if it is different from the RTSP
563 * control connection server address */
564 char *ptr = strchr( src, '%' );
565 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
567 httpd_MsgAdd( answer, "Transport",
568 "RTP/AVP/UDP;unicast;source=%s;"
569 "client_port=%u-%u;server_port=%u-%u;"
571 src, loport, hiport, sport, sport + 1 );
575 httpd_MsgAdd( answer, "Transport",
576 "RTP/AVP/UDP;unicast;"
577 "client_port=%u-%u;server_port=%u-%u;"
579 loport, hiport, sport, sport + 1 );
582 answer->i_status = 200;
591 answer->i_status = 460;
595 psz_cseq = httpd_MsgGet( query, "Cseq" );
597 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
598 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
599 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
600 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
603 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );