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 int RtspSetupId( sout_stream_t *p_stream, sout_stream_id_t *id )
94 sout_stream_sys_t *p_sys = p_stream->p_sys;
95 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
97 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
98 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
99 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
103 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void*)id );
104 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
105 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallbackId, (void*)id );
106 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallbackId, (void*)id );
107 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void*)id );
114 void RtspUnsetup( sout_stream_t *p_stream )
116 sout_stream_sys_t *p_sys = p_stream->p_sys;
117 while( p_sys->i_rtsp > 0 )
118 RtspClientDel( p_stream, p_sys->rtsp[0] );
122 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
124 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
126 rtsp->psz_session = strdup( psz_session );
128 rtsp->b_playing = VLC_FALSE;
134 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
140 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
144 if( !psz_session ) return NULL;
146 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
148 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
150 return p_stream->p_sys->rtsp[i];
157 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
160 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
162 for( i = 0; i < rtsp->i_access; i++ )
164 sout_AccessOutDelete( rtsp->access[i] );
166 if( rtsp->id ) free( rtsp->id );
167 if( rtsp->access ) free( rtsp->access );
169 free( rtsp->psz_session );
174 /** Aggregate RTSP callback */
175 static int RtspCallback( httpd_callback_sys_t *p_args,
177 httpd_message_t *answer, httpd_message_t *query )
179 sout_stream_t *p_stream = (sout_stream_t*)p_args;
180 sout_stream_sys_t *p_sys = p_stream->p_sys;
181 char *psz_destination = p_sys->psz_destination;
182 const char *psz_session = NULL;
183 const char *psz_cseq = NULL;
185 if( answer == NULL || query == NULL )
189 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
191 answer->i_proto = HTTPD_PROTO_RTSP;
192 answer->i_version= query->i_version;
193 answer->i_type = HTTPD_MSG_ANSWER;
195 answer->p_body = NULL;
197 if( httpd_MsgGet( query, "Require" ) != NULL )
199 answer->i_status = 551;
200 httpd_MsgAdd( query, "Unsupported", "%s",
201 httpd_MsgGet( query, "Require" ) );
204 switch( query->i_type )
206 case HTTPD_MSG_DESCRIBE:
208 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
210 answer->i_status = 200;
211 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
212 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
213 answer->p_body = (uint8_t *)psz_sdp;
214 answer->i_body = strlen( psz_sdp );
218 case HTTPD_MSG_SETUP:
219 answer->i_status = 459;
225 answer->i_status = 200;
227 psz_session = httpd_MsgGet( query, "Session" );
228 rtsp = RtspClientGet( p_stream, psz_session );
229 if( rtsp && !rtsp->b_playing )
233 rtsp->b_playing = VLC_TRUE;
235 vlc_mutex_lock( &p_sys->lock_es );
236 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
238 sout_stream_id_t *id = rtsp->id[i_id];
241 for( i = 0; i < p_sys->i_es; i++ )
243 if( id == p_sys->es[i] )
246 if( i >= p_sys->i_es ) continue;
248 rtp_add_sink( id, rtsp->access[i_id] );
250 vlc_mutex_unlock( &p_sys->lock_es );
255 case HTTPD_MSG_PAUSE:
256 answer->i_status = 405;
257 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
260 case HTTPD_MSG_TEARDOWN:
264 /* for now only multicast so easy again */
265 answer->i_status = 200;
267 psz_session = httpd_MsgGet( query, "Session" );
268 rtsp = RtspClientGet( p_stream, psz_session );
273 vlc_mutex_lock( &p_sys->lock_es );
274 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
276 sout_stream_id_t *id = rtsp->id[i_id];
279 for( i = 0; i < p_sys->i_es; i++ )
281 if( id == p_sys->es[i] )
284 if( i >= p_sys->i_es ) continue;
286 rtp_del_sink( id, rtsp->access[i_id] );
288 vlc_mutex_unlock( &p_sys->lock_es );
290 RtspClientDel( p_stream, rtsp );
299 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
300 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
301 psz_cseq = httpd_MsgGet( query, "Cseq" );
303 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
304 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
307 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
312 /** Finds the next transport choice */
313 static inline const char *transport_next( const char *str )
315 /* Looks for comma */
316 str = strchr( str, ',' );
318 return NULL; /* No more transport options */
320 str++; /* skips comma */
321 while( strchr( "\r\n\t ", *str ) )
324 return (*str) ? str : NULL;
328 /** Finds the next transport parameter */
329 static inline const char *parameter_next( const char *str )
331 while( strchr( ",;", *str ) == NULL )
334 return (*str == ';') ? (str + 1) : NULL;
338 /** Non-aggregate RTSP callback */
339 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
341 httpd_message_t *answer, httpd_message_t *query )
343 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
344 sout_stream_t *p_stream = id->p_stream;
345 sout_stream_sys_t *p_sys = p_stream->p_sys;
346 char psz_session_init[21];
347 const char *psz_session;
348 const char *psz_cseq;
350 if( answer == NULL || query == NULL )
352 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
355 answer->i_proto = HTTPD_PROTO_RTSP;
356 answer->i_version= query->i_version;
357 answer->i_type = HTTPD_MSG_ANSWER;
359 answer->p_body = NULL;
361 /* Create new session ID if needed */
362 psz_session = httpd_MsgGet( query, "Session" );
363 if( psz_session == NULL )
365 /* FIXME: should be somewhat secure randomness */
366 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
367 NTPtime64() + rand() );
370 if( httpd_MsgGet( query, "Require" ) != NULL )
372 answer->i_status = 551;
373 httpd_MsgAdd( query, "Unsupported", "%s",
374 httpd_MsgGet( query, "Require" ) );
377 switch( query->i_type )
379 case HTTPD_MSG_SETUP:
381 answer->i_status = 461;
383 for( const char *tpt = httpd_MsgGet( query, "Transport" );
385 tpt = transport_next( tpt ) )
387 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
388 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
390 /* Check transport protocol. */
391 /* Currently, we only support RTP/AVP over UDP */
392 if( strncmp( tpt, "RTP/AVP", 7 ) )
395 if( strncmp( tpt, "/UDP", 4 ) == 0 )
397 if( strchr( ";,", *tpt ) == NULL )
400 /* Parse transport options */
401 for( const char *opt = parameter_next( tpt );
403 opt = parameter_next( opt ) )
405 if( strncmp( opt, "multicast", 9 ) == 0)
406 b_multicast = VLC_TRUE;
408 if( strncmp( opt, "unicast", 7 ) == 0 )
409 b_multicast = VLC_FALSE;
411 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
414 if( strncmp( opt, "mode=", 5 ) == 0 )
416 if( strncasecmp( opt + 5, "play", 4 )
417 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
427 * Every other option is unsupported:
429 * "source" and "append" are invalid.
431 * For multicast, "port", "layers", "ttl" are set by the
432 * stream output configuration.
434 * For unicast, we do not allow "destination" as it
435 * carries a DoS risk, and we decide on "server_port".
437 * "interleaved" and "ssrc" are not implemented.
449 if( p_sys->psz_destination == NULL )
452 answer->i_status = 200;
454 httpd_MsgAdd( answer, "Transport",
455 "RTP/AVP/UDP;destination=%s;port=%d-%d;"
457 p_sys->psz_destination, id->i_port, id->i_port+1,
458 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
462 char ip[NI_MAXNUMERICHOST], psz_access[22],
463 url[NI_MAXNUMERICHOST + 8];
464 sout_access_out_t *p_access;
465 rtsp_client_t *rtsp = NULL;
467 if( ( hiport - loport ) > 1 )
470 if( psz_session == NULL )
472 psz_session = psz_session_init;
473 rtsp = RtspClientNew( p_stream, psz_session );
477 /* FIXME: we probably need to remove an access out,
478 * if there is already one for the same ID */
479 rtsp = RtspClientGet( p_stream, psz_session );
482 answer->i_status = 454;
487 if( httpd_ClientIP( cl, ip ) == NULL )
489 answer->i_status = 500;
493 if( p_sys->i_ttl > 0 )
494 snprintf( psz_access, sizeof( psz_access ),
495 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
497 strcpy( psz_access, "udp{raw,rtcp}" );
499 snprintf( url, sizeof( url ),
500 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
503 p_access = sout_AccessOutNew( p_stream->p_sout,
505 if( p_access == NULL )
508 "cannot create access output for %s://%s",
510 answer->i_status = 500;
514 TAB_APPEND( rtsp->i_id, rtsp->id, id );
515 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
517 char *src = var_GetNonEmptyString (p_access, "src-addr");
518 int sport = var_GetInteger (p_access, "src-port");
520 httpd_ServerIP( cl, ip );
522 if( ( src != NULL ) && strcmp( src, ip ) )
524 /* Specify source IP if it is different from the RTSP
525 * control connection server address */
526 char *ptr = strchr( src, '%' );
527 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
529 httpd_MsgAdd( answer, "Transport",
530 "RTP/AVP/UDP;unicast;source=%s;"
531 "client_port=%u-%u;server_port=%u-%u;"
533 src, loport, hiport, sport, sport + 1 );
537 httpd_MsgAdd( answer, "Transport",
538 "RTP/AVP/UDP;unicast;"
539 "client_port=%u-%u;server_port=%u-%u;"
541 loport, hiport, sport, sport + 1 );
544 answer->i_status = 200;
553 answer->i_status = 460;
557 psz_cseq = httpd_MsgGet( query, "Cseq" );
559 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
560 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
561 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
562 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
565 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );