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 );
58 int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
60 sout_stream_sys_t *p_sys = p_stream->p_sys;
62 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
64 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
65 if( p_sys->p_rtsp_host == NULL )
70 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
71 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
72 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
73 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
75 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
76 if( p_sys->p_rtsp_url == 0 )
80 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
81 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
82 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
83 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
84 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
90 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
92 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
94 rtsp->psz_session = strdup( psz_session );
96 rtsp->b_playing = VLC_FALSE;
102 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
108 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
112 if( !psz_session ) return NULL;
114 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
116 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
118 return p_stream->p_sys->rtsp[i];
125 /*static*/ void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
128 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
130 for( i = 0; i < rtsp->i_access; i++ )
132 sout_AccessOutDelete( rtsp->access[i] );
134 if( rtsp->id ) free( rtsp->id );
135 if( rtsp->access ) free( rtsp->access );
137 free( rtsp->psz_session );
142 /** Aggregate RTSP callback */
143 static int RtspCallback( httpd_callback_sys_t *p_args,
145 httpd_message_t *answer, httpd_message_t *query )
147 sout_stream_t *p_stream = (sout_stream_t*)p_args;
148 sout_stream_sys_t *p_sys = p_stream->p_sys;
149 char *psz_destination = p_sys->psz_destination;
150 const char *psz_session = NULL;
151 const char *psz_cseq = NULL;
153 if( answer == NULL || query == NULL )
157 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
159 answer->i_proto = HTTPD_PROTO_RTSP;
160 answer->i_version= query->i_version;
161 answer->i_type = HTTPD_MSG_ANSWER;
163 answer->p_body = NULL;
165 if( httpd_MsgGet( query, "Require" ) != NULL )
167 answer->i_status = 551;
168 httpd_MsgAdd( query, "Unsupported", "%s",
169 httpd_MsgGet( query, "Require" ) );
172 switch( query->i_type )
174 case HTTPD_MSG_DESCRIBE:
176 char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
178 answer->i_status = 200;
179 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
180 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
181 answer->p_body = (uint8_t *)psz_sdp;
182 answer->i_body = strlen( psz_sdp );
186 case HTTPD_MSG_SETUP:
187 answer->i_status = 459;
193 answer->i_status = 200;
195 psz_session = httpd_MsgGet( query, "Session" );
196 rtsp = RtspClientGet( p_stream, psz_session );
197 if( rtsp && !rtsp->b_playing )
201 rtsp->b_playing = VLC_TRUE;
203 vlc_mutex_lock( &p_sys->lock_es );
204 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
206 sout_stream_id_t *id = rtsp->id[i_id];
209 for( i = 0; i < p_sys->i_es; i++ )
211 if( id == p_sys->es[i] )
214 if( i >= p_sys->i_es ) continue;
216 vlc_mutex_lock( &id->lock_rtsp );
217 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
218 vlc_mutex_unlock( &id->lock_rtsp );
220 vlc_mutex_unlock( &p_sys->lock_es );
225 case HTTPD_MSG_PAUSE:
226 answer->i_status = 405;
227 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
230 case HTTPD_MSG_TEARDOWN:
234 /* for now only multicast so easy again */
235 answer->i_status = 200;
237 psz_session = httpd_MsgGet( query, "Session" );
238 rtsp = RtspClientGet( p_stream, psz_session );
243 vlc_mutex_lock( &p_sys->lock_es );
244 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
246 sout_stream_id_t *id = rtsp->id[i_id];
249 for( i = 0; i < p_sys->i_es; i++ )
251 if( id == p_sys->es[i] )
254 if( i >= p_sys->i_es ) continue;
256 vlc_mutex_lock( &id->lock_rtsp );
257 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
258 vlc_mutex_unlock( &id->lock_rtsp );
260 vlc_mutex_unlock( &p_sys->lock_es );
262 RtspClientDel( p_stream, rtsp );
271 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
272 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
273 psz_cseq = httpd_MsgGet( query, "Cseq" );
275 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
276 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
279 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
284 /** Finds the next transport choice */
285 static inline const char *transport_next( const char *str )
287 /* Looks for comma */
288 str = strchr( str, ',' );
290 return NULL; /* No more transport options */
292 str++; /* skips comma */
293 while( strchr( "\r\n\t ", *str ) )
296 return (*str) ? str : NULL;
300 /** Finds the next transport parameter */
301 static inline const char *parameter_next( const char *str )
303 while( strchr( ",;", *str ) == NULL )
306 return (*str == ';') ? (str + 1) : NULL;
310 /** Non-aggregate RTSP callback */
311 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
313 httpd_message_t *answer, httpd_message_t *query )
315 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
316 sout_stream_t *p_stream = id->p_stream;
317 sout_stream_sys_t *p_sys = p_stream->p_sys;
318 char psz_session_init[21];
319 const char *psz_session;
320 const char *psz_cseq;
322 if( answer == NULL || query == NULL )
324 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
327 answer->i_proto = HTTPD_PROTO_RTSP;
328 answer->i_version= query->i_version;
329 answer->i_type = HTTPD_MSG_ANSWER;
331 answer->p_body = NULL;
333 /* Create new session ID if needed */
334 psz_session = httpd_MsgGet( query, "Session" );
335 if( psz_session == NULL )
337 /* FIXME: should be somewhat secure randomness */
338 snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
339 NTPtime64() + rand() );
342 if( httpd_MsgGet( query, "Require" ) != NULL )
344 answer->i_status = 551;
345 httpd_MsgAdd( query, "Unsupported", "%s",
346 httpd_MsgGet( query, "Require" ) );
349 switch( query->i_type )
351 case HTTPD_MSG_SETUP:
353 answer->i_status = 461;
355 for( const char *tpt = httpd_MsgGet( query, "Transport" );
357 tpt = transport_next( tpt ) )
359 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
360 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
362 /* Check transport protocol. */
363 /* Currently, we only support RTP/AVP over UDP */
364 if( strncmp( tpt, "RTP/AVP", 7 ) )
367 if( strncmp( tpt, "/UDP", 4 ) == 0 )
369 if( strchr( ";,", *tpt ) == NULL )
372 /* Parse transport options */
373 for( const char *opt = parameter_next( tpt );
375 opt = parameter_next( opt ) )
377 if( strncmp( opt, "multicast", 9 ) == 0)
378 b_multicast = VLC_TRUE;
380 if( strncmp( opt, "unicast", 7 ) == 0 )
381 b_multicast = VLC_FALSE;
383 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
386 if( strncmp( opt, "mode=", 5 ) == 0 )
388 if( strncasecmp( opt + 5, "play", 4 )
389 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
399 * Every other option is unsupported:
401 * "source" and "append" are invalid.
403 * For multicast, "port", "layers", "ttl" are set by the
404 * stream output configuration.
406 * For unicast, we do not allow "destination" as it
407 * carries a DoS risk, and we decide on "server_port".
409 * "interleaved" and "ssrc" are not implemented.
421 if( id->psz_destination == NULL )
424 answer->i_status = 200;
426 httpd_MsgAdd( answer, "Transport",
427 "RTP/AVP/UDP;destination=%s;port=%d-%d;"
429 id->psz_destination, id->i_port, id->i_port+1,
430 ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
434 char ip[NI_MAXNUMERICHOST], psz_access[22],
435 url[NI_MAXNUMERICHOST + 8];
436 sout_access_out_t *p_access;
437 rtsp_client_t *rtsp = NULL;
439 if( ( hiport - loport ) > 1 )
442 if( psz_session == NULL )
444 psz_session = psz_session_init;
445 rtsp = RtspClientNew( p_stream, psz_session );
449 /* FIXME: we probably need to remove an access out,
450 * if there is already one for the same ID */
451 rtsp = RtspClientGet( p_stream, psz_session );
454 answer->i_status = 454;
459 if( httpd_ClientIP( cl, ip ) == NULL )
461 answer->i_status = 500;
466 snprintf( psz_access, sizeof( psz_access ),
467 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
469 strcpy( psz_access, "udp{raw,rtcp}" );
471 snprintf( url, sizeof( url ),
472 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
475 p_access = sout_AccessOutNew( p_stream->p_sout,
477 if( p_access == NULL )
480 "cannot create access output for %s://%s",
482 answer->i_status = 500;
486 TAB_APPEND( rtsp->i_id, rtsp->id, id );
487 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
489 char *src = var_GetNonEmptyString (p_access, "src-addr");
490 int sport = var_GetInteger (p_access, "src-port");
492 httpd_ServerIP( cl, ip );
494 if( ( src != NULL ) && strcmp( src, ip ) )
496 /* Specify source IP if it is different from the RTSP
497 * control connection server address */
498 char *ptr = strchr( src, '%' );
499 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
501 httpd_MsgAdd( answer, "Transport",
502 "RTP/AVP/UDP;unicast;source=%s;"
503 "client_port=%u-%u;server_port=%u-%u;"
505 src, loport, hiport, sport, sport + 1 );
509 httpd_MsgAdd( answer, "Transport",
510 "RTP/AVP/UDP;unicast;"
511 "client_port=%u-%u;server_port=%u-%u;"
513 loport, hiport, sport, sport + 1 );
516 answer->i_status = 200;
525 answer->i_status = 460;
529 psz_cseq = httpd_MsgGet( query, "Cseq" );
531 httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
532 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
533 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
534 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
537 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );