1 /*****************************************************************************
2 * rtsp.c: RTSP support for RTP stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
5 * Copyright © 2007 Rémi Denis-Courmont
7 * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
9 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc_httpd.h>
34 #include <vlc_network.h>
42 typedef struct rtsp_session_t rtsp_session_t;
51 const char *track_fmt;
55 rtsp_session_t **sessionv;
59 static int RtspCallback( httpd_callback_sys_t *p_args,
60 httpd_client_t *cl, httpd_message_t *answer,
61 const httpd_message_t *query );
62 static int RtspCallbackId( httpd_callback_sys_t *p_args,
63 httpd_client_t *cl, httpd_message_t *answer,
64 const httpd_message_t *query );
65 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
67 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
69 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
71 if( rtsp == NULL || ( url->i_port > 99999 ) )
77 rtsp->owner = p_stream;
79 rtsp->sessionv = NULL;
82 rtsp->psz_path = NULL;
83 vlc_mutex_init( p_stream, &rtsp->lock );
85 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
86 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
87 if( rtsp->psz_path == NULL )
90 assert( strlen( rtsp->psz_path ) > 0 );
91 if( rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' )
92 rtsp->track_fmt = "%strackID=%u";
94 rtsp->track_fmt = "%s/trackID=%u";
96 msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
97 url->psz_host, rtsp->port, rtsp->psz_path );
99 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
101 if( rtsp->host == NULL )
104 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
106 if( rtsp->url == NULL )
109 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
110 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
111 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
112 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
113 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
115 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
124 void RtspUnsetup( rtsp_stream_t *rtsp )
126 while( rtsp->sessionc > 0 )
127 RtspClientDel( rtsp, rtsp->sessionv[0] );
130 httpd_UrlDelete( rtsp->url );
133 httpd_HostDelete( rtsp->host );
135 free( rtsp->psz_path );
136 vlc_mutex_destroy( &rtsp->lock );
140 struct rtsp_stream_id_t
142 rtsp_stream_t *stream;
143 sout_stream_id_t *sout_id;
147 unsigned loport, hiport;
151 typedef struct rtsp_strack_t rtsp_strack_t;
153 /* For unicast streaming */
154 struct rtsp_session_t
156 rtsp_stream_t *stream;
159 /* output (id-access) */
161 rtsp_strack_t *trackv;
165 /* Unicast session track */
168 sout_stream_id_t *id;
174 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
176 /* Multicast stuff - TODO: cleanup */
177 const char *dst, int ttl,
178 unsigned loport, unsigned hiport )
180 char urlbuf[sizeof( "/trackID=123" ) + strlen( rtsp->psz_path )];
181 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
189 /* TODO: can we assume that this need not be strdup'd? */
191 if( id->dst != NULL )
198 snprintf( urlbuf, sizeof( urlbuf ), rtsp->track_fmt, rtsp->psz_path,
200 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
201 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
209 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
210 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
211 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
212 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
213 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
214 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
220 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
222 vlc_mutex_lock( &rtsp->lock );
223 for( int i = 0; i < rtsp->sessionc; i++ )
225 rtsp_session_t *ses = rtsp->sessionv[i];
227 for( int j = 0; j < ses->trackc; j++ )
229 if( ses->trackv[j].id == id->sout_id )
231 rtsp_strack_t *tr = ses->trackv + j;
233 REMOVE_ELEM( ses->trackv, ses->trackc, j );
238 vlc_mutex_unlock( &rtsp->lock );
239 httpd_UrlDelete( id->url );
244 /** rtsp must be locked */
246 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
248 rtsp_session_t *s = malloc( sizeof( *s ) );
253 vlc_rand_bytes (&s->id, sizeof (s->id));
257 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
263 /** rtsp must be locked */
265 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
275 id = strtoull( name, &end, 0x10 );
279 /* FIXME: use a hash/dictionary */
280 for( i = 0; i < rtsp->sessionc; i++ )
282 if( rtsp->sessionv[i]->id == id )
283 return rtsp->sessionv[i];
289 /** rtsp must be locked */
291 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
294 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
296 for( i = 0; i < session->trackc; i++ )
297 rtp_del_sink( session->trackv[i].id, session->trackv[i].fd );
299 free( session->trackv );
304 /** Finds the next transport choice */
305 static inline const char *transport_next( const char *str )
307 /* Looks for comma */
308 str = strchr( str, ',' );
310 return NULL; /* No more transport options */
312 str++; /* skips comma */
313 while( strchr( "\r\n\t ", *str ) )
316 return (*str) ? str : NULL;
320 /** Finds the next transport parameter */
321 static inline const char *parameter_next( const char *str )
323 while( strchr( ",;", *str ) == NULL )
326 return (*str == ';') ? (str + 1) : NULL;
330 /** RTSP requests handler
331 * @param id selected track for non-aggregate URLs,
332 * NULL for aggregate URLs
334 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
336 httpd_message_t *answer,
337 const httpd_message_t *query )
339 sout_stream_t *p_stream = rtsp->owner;
341 const char *psz_session = NULL, *psz;
342 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
343 + strlen( rtsp->psz_path )];
345 if( answer == NULL || query == NULL || cl == NULL )
349 /* Build self-referential control URL */
350 char ip[NI_MAXNUMERICHOST], *ptr;
352 httpd_ServerIP( cl, ip );
353 ptr = strchr( ip, '%' );
357 if( strchr( ip, ':' ) != NULL )
358 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
361 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
366 answer->i_proto = HTTPD_PROTO_RTSP;
367 answer->i_version= 0;
368 answer->i_type = HTTPD_MSG_ANSWER;
370 answer->p_body = NULL;
372 if( query->i_proto != HTTPD_PROTO_RTSP )
374 answer->i_status = 505;
377 if( httpd_MsgGet( query, "Require" ) != NULL )
379 answer->i_status = 551;
380 httpd_MsgAdd( answer, "Unsupported", "%s",
381 httpd_MsgGet( query, "Require" ) );
384 switch( query->i_type )
386 case HTTPD_MSG_DESCRIBE:
387 { /* Aggregate-only */
390 answer->i_status = 460;
394 answer->i_status = 200;
395 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
396 httpd_MsgAdd( answer, "Content-Base", "%s", control );
397 answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
398 if( answer->p_body != NULL )
399 answer->i_body = strlen( (char *)answer->p_body );
401 answer->i_status = 500;
405 case HTTPD_MSG_SETUP:
406 /* Non-aggregate-only */
409 answer->i_status = 459;
413 psz_session = httpd_MsgGet( query, "Session" );
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 )
448 if( strncmp( opt, "mode=", 5 ) == 0 )
450 if( strncasecmp( opt + 5, "play", 4 )
451 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
459 if( strncmp( opt,"destination=", 12 ) == 0 )
461 answer->i_status = 403;
467 * Every other option is unsupported:
469 * "source" and "append" are invalid (server-only);
470 * "ssrc" also (as clarified per RFC2326bis).
472 * For multicast, "port", "layers", "ttl" are set by the
473 * stream output configuration.
475 * For unicast, we want to decide "server_port" values.
477 * "interleaved" is not implemented.
489 const char *dst = id->dst;
493 if( psz_session == NULL )
495 /* Create a dummy session ID */
496 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
498 psz_session = psz_sesbuf;
500 answer->i_status = 200;
502 httpd_MsgAdd( answer, "Transport",
503 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
505 dst, id->loport, id->hiport,
506 ( id->ttl > 0 ) ? id->ttl : 1 );
510 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
511 rtsp_session_t *ses = NULL;
512 rtsp_strack_t track = { id->sout_id, -1, VLC_FALSE };
515 if( httpd_ClientIP( cl, ip ) == NULL )
517 answer->i_status = 500;
521 track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
526 "cannot create RTP socket for %s port %u",
528 answer->i_status = 500;
532 net_GetSockAddress( track.fd, src, &sport );
534 vlc_mutex_lock( &rtsp->lock );
535 if( psz_session == NULL )
537 ses = RtspClientNew( rtsp );
538 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
540 psz_session = psz_sesbuf;
544 /* FIXME: we probably need to remove an access out,
545 * if there is already one for the same ID */
546 ses = RtspClientGet( rtsp, psz_session );
549 answer->i_status = 454;
550 vlc_mutex_unlock( &rtsp->lock );
555 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
557 vlc_mutex_unlock( &rtsp->lock );
559 httpd_ServerIP( cl, ip );
561 if( strcmp( src, ip ) )
563 /* Specify source IP if it is different from the RTSP
564 * control connection server address */
565 char *ptr = strchr( src, '%' );
566 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
568 httpd_MsgAdd( answer, "Transport",
569 "RTP/AVP/UDP;unicast;source=%s;"
570 "client_port=%u-%u;server_port=%u-%u;"
572 src, loport, loport + 1, sport,
577 httpd_MsgAdd( answer, "Transport",
578 "RTP/AVP/UDP;unicast;"
579 "client_port=%u-%u;server_port=%u-%u;"
581 loport, loport + 1, sport, sport + 1 );
584 answer->i_status = 200;
593 answer->i_status = 200;
595 psz_session = httpd_MsgGet( query, "Session" );
596 if( httpd_MsgGet( query, "Range" ) != NULL )
598 answer->i_status = 456; /* cannot seek */
602 vlc_mutex_lock( &rtsp->lock );
603 ses = RtspClientGet( rtsp, psz_session );
606 for( int i = 0; i < ses->trackc; i++ )
608 rtsp_strack_t *tr = ses->trackv + i;
610 && ( ( id == NULL ) || ( tr->id == id->sout_id ) ) )
612 tr->playing = VLC_TRUE;
613 rtp_add_sink( tr->id, tr->fd, VLC_FALSE );
617 vlc_mutex_unlock( &rtsp->lock );
619 if( httpd_MsgGet( query, "Scale" ) != NULL )
620 httpd_MsgAdd( answer, "Scale", "1." );
624 case HTTPD_MSG_PAUSE:
625 answer->i_status = 405;
626 httpd_MsgAdd( answer, "Allow",
627 "%s, TEARDOWN, PLAY, GET_PARAMETER",
628 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
631 case HTTPD_MSG_GETPARAMETER:
632 if( query->i_body > 0 )
634 answer->i_status = 451;
638 psz_session = httpd_MsgGet( query, "Session" );
639 answer->i_status = 200;
642 case HTTPD_MSG_TEARDOWN:
646 answer->i_status = 200;
648 psz_session = httpd_MsgGet( query, "Session" );
650 vlc_mutex_lock( &rtsp->lock );
651 ses = RtspClientGet( rtsp, psz_session );
654 if( id == NULL ) /* Delete the entire session */
655 RtspClientDel( rtsp, ses );
656 else /* Delete one track from the session */
657 for( int i = 0; i < ses->trackc; i++ )
659 if( ses->trackv[i].id == id->sout_id )
661 rtp_del_sink( id->sout_id, ses->trackv[i].fd );
662 REMOVE_ELEM( ses->trackv, ses->trackc, i );
666 vlc_mutex_unlock( &rtsp->lock );
674 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
676 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
678 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
679 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
681 psz = httpd_MsgGet( query, "Cseq" );
683 httpd_MsgAdd( answer, "Cseq", "%s", psz );
684 psz = httpd_MsgGet( query, "Timestamp" );
686 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
692 /** Aggregate RTSP callback */
693 static int RtspCallback( httpd_callback_sys_t *p_args,
695 httpd_message_t *answer,
696 const httpd_message_t *query )
698 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
702 /** Non-aggregate RTSP callback */
703 static int RtspCallbackId( httpd_callback_sys_t *p_args,
705 httpd_message_t *answer,
706 const httpd_message_t *query )
708 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
709 return RtspHandler( id->stream, id, cl, answer, query );