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
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 *****************************************************************************/
36 #include <vlc_httpd.h>
38 #include <vlc_network.h>
46 typedef struct rtsp_session_t rtsp_session_t;
55 const char *track_fmt;
59 rtsp_session_t **sessionv;
63 static int RtspCallback( httpd_callback_sys_t *p_args,
64 httpd_client_t *cl, httpd_message_t *answer,
65 const httpd_message_t *query );
66 static int RtspCallbackId( httpd_callback_sys_t *p_args,
67 httpd_client_t *cl, httpd_message_t *answer,
68 const httpd_message_t *query );
69 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
71 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
73 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
75 if( rtsp == NULL || ( url->i_port > 99999 ) )
81 rtsp->owner = p_stream;
83 rtsp->sessionv = NULL;
86 rtsp->psz_path = NULL;
87 vlc_mutex_init( p_stream, &rtsp->lock );
89 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
90 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
91 if( rtsp->psz_path == NULL )
94 assert( strlen( rtsp->psz_path ) > 0 );
95 if( rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' )
96 rtsp->track_fmt = "%strackID=%u";
98 rtsp->track_fmt = "%s/trackID=%u";
100 msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
101 url->psz_host, rtsp->port, rtsp->psz_path );
103 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
105 if( rtsp->host == NULL )
108 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
110 if( rtsp->url == NULL )
113 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
114 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
115 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
116 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
117 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
119 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
128 void RtspUnsetup( rtsp_stream_t *rtsp )
130 while( rtsp->sessionc > 0 )
131 RtspClientDel( rtsp, rtsp->sessionv[0] );
134 httpd_UrlDelete( rtsp->url );
137 httpd_HostDelete( rtsp->host );
139 free( rtsp->psz_path );
140 vlc_mutex_destroy( &rtsp->lock );
144 struct rtsp_stream_id_t
146 rtsp_stream_t *stream;
147 sout_stream_id_t *sout_id;
152 uint16_t loport, hiport;
156 typedef struct rtsp_strack_t rtsp_strack_t;
158 /* For unicast streaming */
159 struct rtsp_session_t
161 rtsp_stream_t *stream;
164 /* output (id-access) */
166 rtsp_strack_t *trackv;
170 /* Unicast session track */
173 sout_stream_id_t *id;
179 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
180 unsigned num, uint32_t ssrc,
181 /* Multicast stuff - TODO: cleanup */
182 const char *dst, int ttl,
183 unsigned loport, unsigned hiport )
185 char urlbuf[sizeof( "/trackID=123" ) + strlen( rtsp->psz_path )];
186 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
195 /* TODO: can we assume that this need not be strdup'd? */
197 if( id->dst != NULL )
204 /* FIXME: num screws up if any ES has been removed and re-added */
205 snprintf( urlbuf, sizeof( urlbuf ), rtsp->track_fmt, rtsp->psz_path,
207 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
208 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
216 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
217 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
218 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
219 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
220 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
221 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
227 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
229 vlc_mutex_lock( &rtsp->lock );
230 for( int i = 0; i < rtsp->sessionc; i++ )
232 rtsp_session_t *ses = rtsp->sessionv[i];
234 for( int j = 0; j < ses->trackc; j++ )
236 if( ses->trackv[j].id == id->sout_id )
238 rtsp_strack_t *tr = ses->trackv + j;
240 REMOVE_ELEM( ses->trackv, ses->trackc, j );
245 vlc_mutex_unlock( &rtsp->lock );
246 httpd_UrlDelete( id->url );
251 /** rtsp must be locked */
253 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
255 rtsp_session_t *s = malloc( sizeof( *s ) );
260 vlc_rand_bytes (&s->id, sizeof (s->id));
264 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
270 /** rtsp must be locked */
272 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
282 id = strtoull( name, &end, 0x10 );
286 /* FIXME: use a hash/dictionary */
287 for( i = 0; i < rtsp->sessionc; i++ )
289 if( rtsp->sessionv[i]->id == id )
290 return rtsp->sessionv[i];
296 /** rtsp must be locked */
298 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
301 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
303 for( i = 0; i < session->trackc; i++ )
304 rtp_del_sink( session->trackv[i].id, session->trackv[i].fd );
306 free( session->trackv );
311 /** Finds the next transport choice */
312 static inline const char *transport_next( const char *str )
314 /* Looks for comma */
315 str = strchr( str, ',' );
317 return NULL; /* No more transport options */
319 str++; /* skips comma */
320 while( strchr( "\r\n\t ", *str ) )
323 return (*str) ? str : NULL;
327 /** Finds the next transport parameter */
328 static inline const char *parameter_next( const char *str )
330 while( strchr( ",;", *str ) == NULL )
333 return (*str == ';') ? (str + 1) : NULL;
337 /** RTSP requests handler
338 * @param id selected track for non-aggregate URLs,
339 * NULL for aggregate URLs
341 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
343 httpd_message_t *answer,
344 const httpd_message_t *query )
346 sout_stream_t *p_stream = rtsp->owner;
348 const char *psz_session = NULL, *psz;
349 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
350 + strlen( rtsp->psz_path )];
352 if( answer == NULL || query == NULL || cl == NULL )
356 /* Build self-referential control URL */
357 char ip[NI_MAXNUMERICHOST], *ptr;
359 httpd_ServerIP( cl, ip );
360 ptr = strchr( ip, '%' );
364 if( strchr( ip, ':' ) != NULL )
365 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
368 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
373 answer->i_proto = HTTPD_PROTO_RTSP;
374 answer->i_version= 0;
375 answer->i_type = HTTPD_MSG_ANSWER;
377 answer->p_body = NULL;
379 if( query->i_proto != HTTPD_PROTO_RTSP )
381 answer->i_status = 505;
384 if( httpd_MsgGet( query, "Require" ) != NULL )
386 answer->i_status = 551;
387 httpd_MsgAdd( answer, "Unsupported", "%s",
388 httpd_MsgGet( query, "Require" ) );
391 switch( query->i_type )
393 case HTTPD_MSG_DESCRIBE:
394 { /* Aggregate-only */
397 answer->i_status = 460;
401 answer->i_status = 200;
402 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
403 httpd_MsgAdd( answer, "Content-Base", "%s", control );
404 answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
405 if( answer->p_body != NULL )
406 answer->i_body = strlen( (char *)answer->p_body );
408 answer->i_status = 500;
412 case HTTPD_MSG_SETUP:
413 /* Non-aggregate-only */
416 answer->i_status = 459;
420 psz_session = httpd_MsgGet( query, "Session" );
421 answer->i_status = 461;
423 for( const char *tpt = httpd_MsgGet( query, "Transport" );
425 tpt = transport_next( tpt ) )
427 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
428 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
430 /* Check transport protocol. */
431 /* Currently, we only support RTP/AVP over UDP */
432 if( strncmp( tpt, "RTP/AVP", 7 ) )
435 if( strncmp( tpt, "/UDP", 4 ) == 0 )
437 if( strchr( ";,", *tpt ) == NULL )
440 /* Parse transport options */
441 for( const char *opt = parameter_next( tpt );
443 opt = parameter_next( opt ) )
445 if( strncmp( opt, "multicast", 9 ) == 0)
446 b_multicast = VLC_TRUE;
448 if( strncmp( opt, "unicast", 7 ) == 0 )
449 b_multicast = VLC_FALSE;
451 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
455 if( strncmp( opt, "mode=", 5 ) == 0 )
457 if( strncasecmp( opt + 5, "play", 4 )
458 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
466 if( strncmp( opt,"destination=", 12 ) == 0 )
468 answer->i_status = 403;
474 * Every other option is unsupported:
476 * "source" and "append" are invalid (server-only);
477 * "ssrc" also (as clarified per RFC2326bis).
479 * For multicast, "port", "layers", "ttl" are set by the
480 * stream output configuration.
482 * For unicast, we want to decide "server_port" values.
484 * "interleaved" is not implemented.
496 const char *dst = id->dst;
500 if( psz_session == NULL )
502 /* Create a dummy session ID */
503 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
505 psz_session = psz_sesbuf;
507 answer->i_status = 200;
509 httpd_MsgAdd( answer, "Transport",
510 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
512 dst, id->loport, id->hiport,
513 ( id->ttl > 0 ) ? id->ttl : 1 );
517 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
518 rtsp_session_t *ses = NULL;
519 rtsp_strack_t track = { id->sout_id, -1, VLC_FALSE };
522 if( httpd_ClientIP( cl, ip ) == NULL )
524 answer->i_status = 500;
528 track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
533 "cannot create RTP socket for %s port %u",
535 answer->i_status = 500;
539 net_GetSockAddress( track.fd, src, &sport );
541 vlc_mutex_lock( &rtsp->lock );
542 if( psz_session == NULL )
544 ses = RtspClientNew( rtsp );
545 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
547 psz_session = psz_sesbuf;
551 /* FIXME: we probably need to remove an access out,
552 * if there is already one for the same ID */
553 ses = RtspClientGet( rtsp, psz_session );
556 answer->i_status = 454;
557 vlc_mutex_unlock( &rtsp->lock );
562 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
564 vlc_mutex_unlock( &rtsp->lock );
566 httpd_ServerIP( cl, ip );
568 if( strcmp( src, ip ) )
570 /* Specify source IP if it is different from the RTSP
571 * control connection server address */
572 char *ptr = strchr( src, '%' );
573 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
575 httpd_MsgAdd( answer, "Transport",
576 "RTP/AVP/UDP;unicast;source=%s;"
577 "client_port=%u-%u;server_port=%u-%u;"
578 "ssrc=%08X;mode=play",
579 src, loport, loport + 1, sport,
580 sport + 1, id->ssrc );
584 httpd_MsgAdd( answer, "Transport",
585 "RTP/AVP/UDP;unicast;"
586 "client_port=%u-%u;server_port=%u-%u;"
587 "ssrc=%08X;mode=play",
588 loport, loport + 1, sport, sport + 1,
592 answer->i_status = 200;
601 answer->i_status = 200;
603 psz_session = httpd_MsgGet( query, "Session" );
604 if( httpd_MsgGet( query, "Range" ) != NULL )
606 answer->i_status = 456; /* cannot seek */
610 vlc_mutex_lock( &rtsp->lock );
611 ses = RtspClientGet( rtsp, psz_session );
614 /* FIXME: we really need to limit the number of tracks... */
615 char info[ses->trackc * ( strlen( control )
616 + sizeof("/trackID=123;seq=65535, ") ) + 1];
619 for( int i = 0; i < ses->trackc; i++ )
621 rtsp_strack_t *tr = ses->trackv + i;
622 if( ( id == NULL ) || ( tr->id == id->sout_id ) )
626 tr->playing = VLC_TRUE;
627 rtp_add_sink( tr->id, tr->fd, VLC_FALSE );
629 infolen += sprintf( info + infolen,
630 "%s/trackID=%u;seq=%u, ", control,
631 rtp_get_num( tr->id ),
632 rtp_get_seq( tr->id ) );
637 info[infolen - 2] = '\0'; /* remove trailing ", " */
638 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
641 vlc_mutex_unlock( &rtsp->lock );
643 if( httpd_MsgGet( query, "Scale" ) != NULL )
644 httpd_MsgAdd( answer, "Scale", "1." );
648 case HTTPD_MSG_PAUSE:
649 answer->i_status = 405;
650 httpd_MsgAdd( answer, "Allow",
651 "%s, TEARDOWN, PLAY, GET_PARAMETER",
652 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
655 case HTTPD_MSG_GETPARAMETER:
656 if( query->i_body > 0 )
658 answer->i_status = 451;
662 psz_session = httpd_MsgGet( query, "Session" );
663 answer->i_status = 200;
666 case HTTPD_MSG_TEARDOWN:
670 answer->i_status = 200;
672 psz_session = httpd_MsgGet( query, "Session" );
674 vlc_mutex_lock( &rtsp->lock );
675 ses = RtspClientGet( rtsp, psz_session );
678 if( id == NULL ) /* Delete the entire session */
679 RtspClientDel( rtsp, ses );
680 else /* Delete one track from the session */
681 for( int i = 0; i < ses->trackc; i++ )
683 if( ses->trackv[i].id == id->sout_id )
685 rtp_del_sink( id->sout_id, ses->trackv[i].fd );
686 REMOVE_ELEM( ses->trackv, ses->trackc, i );
690 vlc_mutex_unlock( &rtsp->lock );
698 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
700 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
702 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
703 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
705 psz = httpd_MsgGet( query, "Cseq" );
707 httpd_MsgAdd( answer, "Cseq", "%s", psz );
708 psz = httpd_MsgGet( query, "Timestamp" );
710 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
716 /** Aggregate RTSP callback */
717 static int RtspCallback( httpd_callback_sys_t *p_args,
719 httpd_message_t *answer,
720 const httpd_message_t *query )
722 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
726 /** Non-aggregate RTSP callback */
727 static int RtspCallbackId( httpd_callback_sys_t *p_args,
729 httpd_message_t *answer,
730 const httpd_message_t *query )
732 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
733 return RtspHandler( id->stream, id, cl, answer, query );