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 struct sout_stream_sys_t
60 char *psz_session_name;
61 char *psz_session_description;
62 char *psz_session_url;
63 char *psz_session_email;
66 vlc_bool_t b_export_sdp_file;
69 vlc_bool_t b_export_sap;
70 session_descriptor_t *p_session;
72 httpd_host_t *p_httpd_host;
73 httpd_file_t *p_httpd_file;
75 httpd_host_t *p_rtsp_host;
76 httpd_url_t *p_rtsp_url;
77 char *psz_rtsp_control;
81 char *psz_destination;
88 /* when need to use a private one or when using muxer */
91 /* in case we do TS/PS over rtp */
93 sout_access_out_t *p_access;
95 sout_access_out_t *p_grab;
97 uint32_t i_timestamp_start;
104 sout_stream_id_t **es;
108 rtsp_client_t **rtsp;
112 static int RtspCallback( httpd_callback_sys_t *p_args,
114 httpd_message_t *answer, httpd_message_t *query );
118 int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
120 sout_stream_sys_t *p_sys = p_stream->p_sys;
122 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
124 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
125 if( p_sys->p_rtsp_host == NULL )
130 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
131 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
132 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
133 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
135 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
136 if( p_sys->p_rtsp_url == 0 )
140 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
141 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
142 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
143 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
144 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
150 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
152 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
154 rtsp->psz_session = strdup( psz_session );
156 rtsp->b_playing = VLC_FALSE;
162 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
168 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
172 if( !psz_session ) return NULL;
174 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
176 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
178 return p_stream->p_sys->rtsp[i];
185 /*static*/ void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
188 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
190 for( i = 0; i < rtsp->i_access; i++ )
192 sout_AccessOutDelete( rtsp->access[i] );
194 if( rtsp->id ) free( rtsp->id );
195 if( rtsp->access ) free( rtsp->access );
197 free( rtsp->psz_session );
202 /** Aggregate RTSP callback */
203 static int RtspCallback( httpd_callback_sys_t *p_args,
205 httpd_message_t *answer, httpd_message_t *query )
207 sout_stream_t *p_stream = (sout_stream_t*)p_args;
208 sout_stream_sys_t *p_sys = p_stream->p_sys;
209 char *psz_destination = p_sys->psz_destination;
210 const char *psz_session = NULL;
211 const char *psz_cseq = NULL;
213 if( answer == NULL || query == NULL )
217 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
219 answer->i_proto = HTTPD_PROTO_RTSP;
220 answer->i_version= query->i_version;
221 answer->i_type = HTTPD_MSG_ANSWER;
223 answer->p_body = NULL;
225 if( httpd_MsgGet( query, "Require" ) != NULL )
227 answer->i_status = 551;
228 httpd_MsgAdd( query, "Unsupported", "%s",
229 httpd_MsgGet( query, "Require" ) );
232 switch( query->i_type )
234 case HTTPD_MSG_DESCRIBE:
236 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
238 answer->i_status = 200;
239 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
240 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
241 answer->p_body = (uint8_t *)psz_sdp;
242 answer->i_body = strlen( psz_sdp );
246 case HTTPD_MSG_SETUP:
247 answer->i_status = 459;
253 /* for now only multicast so easy */
254 answer->i_status = 200;
256 psz_session = httpd_MsgGet( query, "Session" );
257 rtsp = RtspClientGet( p_stream, psz_session );
258 if( rtsp && !rtsp->b_playing )
262 rtsp->b_playing = VLC_TRUE;
264 vlc_mutex_lock( &p_sys->lock_es );
265 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
267 sout_stream_id_t *id = rtsp->id[i_id];
270 for( i = 0; i < p_sys->i_es; i++ )
272 if( id == p_sys->es[i] )
275 if( i >= p_sys->i_es ) continue;
277 vlc_mutex_lock( &id->lock_rtsp );
278 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
279 vlc_mutex_unlock( &id->lock_rtsp );
281 vlc_mutex_unlock( &p_sys->lock_es );
286 case HTTPD_MSG_PAUSE:
287 answer->i_status = 405;
288 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
291 case HTTPD_MSG_TEARDOWN:
295 /* for now only multicast so easy again */
296 answer->i_status = 200;
298 psz_session = httpd_MsgGet( query, "Session" );
299 rtsp = RtspClientGet( p_stream, psz_session );
304 vlc_mutex_lock( &p_sys->lock_es );
305 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
307 sout_stream_id_t *id = rtsp->id[i_id];
310 for( i = 0; i < p_sys->i_es; i++ )
312 if( id == p_sys->es[i] )
315 if( i >= p_sys->i_es ) continue;
317 vlc_mutex_lock( &id->lock_rtsp );
318 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
319 vlc_mutex_unlock( &id->lock_rtsp );
321 vlc_mutex_unlock( &p_sys->lock_es );
323 RtspClientDel( p_stream, rtsp );
332 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
333 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
334 psz_cseq = httpd_MsgGet( query, "Cseq" );
336 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
337 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
340 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
345 /** Finds the next transport choice */
346 static inline const char *transport_next( const char *str )
348 /* Looks for comma */
349 str = strchr( str, ',' );
351 return NULL; /* No more transport options */
353 str++; /* skips comma */
354 while( strchr( "\r\n\t ", *str ) )
357 return (*str) ? str : NULL;
361 /** Finds the next transport parameter */
362 static inline const char *parameter_next( const char *str )
364 while( strchr( ",;", *str ) == NULL )
367 return (*str == ';') ? (str + 1) : NULL;
371 /** Non-aggregate RTSP callback */
372 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
374 httpd_message_t *answer, httpd_message_t *query )
376 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
377 sout_stream_t *p_stream = id->p_stream;
378 sout_stream_sys_t *p_sys = p_stream->p_sys;
379 char psz_session_init[21];
380 const char *psz_session;
381 const char *psz_cseq;
383 if( answer == NULL || query == NULL )
385 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
388 answer->i_proto = HTTPD_PROTO_RTSP;
389 answer->i_version= query->i_version;
390 answer->i_type = HTTPD_MSG_ANSWER;
392 answer->p_body = NULL;
394 /* Create new session ID if needed */
395 psz_session = httpd_MsgGet( query, "Session" );
396 if( psz_session == NULL )
398 /* FIXME: should be somewhat secure randomness */
399 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
400 NTPtime64() + rand() );
403 if( httpd_MsgGet( query, "Require" ) != NULL )
405 answer->i_status = 551;
406 httpd_MsgAdd( query, "Unsupported", "%s",
407 httpd_MsgGet( query, "Require" ) );
410 switch( query->i_type )
412 case HTTPD_MSG_SETUP:
414 answer->i_status = 461;
416 for( const char *tpt = httpd_MsgGet( query, "Transport" );
418 tpt = transport_next( tpt ) )
420 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
421 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
423 /* Check transport protocol. */
424 /* Currently, we only support RTP/AVP over UDP */
425 if( strncmp( tpt, "RTP/AVP", 7 ) )
428 if( strncmp( tpt, "/UDP", 4 ) == 0 )
430 if( strchr( ";,", *tpt ) == NULL )
433 /* Parse transport options */
434 for( const char *opt = parameter_next( tpt );
436 opt = parameter_next( opt ) )
438 if( strncmp( opt, "multicast", 9 ) == 0)
439 b_multicast = VLC_TRUE;
441 if( strncmp( opt, "unicast", 7 ) == 0 )
442 b_multicast = VLC_FALSE;
444 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
447 if( strncmp( opt, "mode=", 5 ) == 0 )
449 if( strncasecmp( opt + 5, "play", 4 )
450 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
460 * Every other option is unsupported:
462 * "source" and "append" are invalid.
464 * For multicast, "port", "layers", "ttl" are set by the
465 * stream output configuration.
467 * For unicast, we do not allow "destination" as it
468 * carries a DoS risk, and we decide on "server_port".
470 * "interleaved" and "ssrc" are not implemented.
482 if( id->psz_destination == NULL )
485 answer->i_status = 200;
487 httpd_MsgAdd( answer, "Transport",
488 "RTP/AVP/UDP;destination=%s;port=%d-%d;"
490 id->psz_destination, id->i_port, id->i_port+1,
491 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
495 char ip[NI_MAXNUMERICHOST], psz_access[22],
496 url[NI_MAXNUMERICHOST + 8];
497 sout_access_out_t *p_access;
498 rtsp_client_t *rtsp = NULL;
500 if( ( hiport - loport ) > 1 )
503 if( psz_session == NULL )
505 psz_session = psz_session_init;
506 rtsp = RtspClientNew( p_stream, psz_session );
510 /* FIXME: we probably need to remove an access out,
511 * if there is already one for the same ID */
512 rtsp = RtspClientGet( p_stream, psz_session );
515 answer->i_status = 454;
520 if( httpd_ClientIP( cl, ip ) == NULL )
522 answer->i_status = 500;
527 snprintf( psz_access, sizeof( psz_access ),
528 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
530 strcpy( psz_access, "udp{raw,rtcp}" );
532 snprintf( url, sizeof( url ),
533 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
536 p_access = sout_AccessOutNew( p_stream->p_sout,
538 if( p_access == NULL )
541 "cannot create access output for %s://%s",
543 answer->i_status = 500;
547 TAB_APPEND( rtsp->i_id, rtsp->id, id );
548 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
550 char *src = var_GetNonEmptyString (p_access, "src-addr");
551 int sport = var_GetInteger (p_access, "src-port");
553 httpd_ServerIP( cl, ip );
555 if( ( src != NULL ) && strcmp( src, ip ) )
557 /* Specify source IP if it is different from the RTSP
558 * control connection server address */
559 char *ptr = strchr( src, '%' );
560 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
562 httpd_MsgAdd( answer, "Transport",
563 "RTP/AVP/UDP;unicast;source=%s;"
564 "client_port=%u-%u;server_port=%u-%u;"
566 src, loport, hiport, sport, sport + 1 );
570 httpd_MsgAdd( answer, "Transport",
571 "RTP/AVP/UDP;unicast;"
572 "client_port=%u-%u;server_port=%u-%u;"
574 loport, hiport, sport, sport + 1 );
577 answer->i_status = 200;
586 answer->i_status = 460;
590 psz_cseq = httpd_MsgGet( query, "Cseq" );
592 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
593 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
594 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
595 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
598 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );