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 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <vlc_httpd.h>
38 #include <vlc_network.h>
46 typedef struct rtsp_session_t rtsp_session_t;
55 const char *track_sep;
60 rtsp_session_t **sessionv;
64 static int RtspCallback( httpd_callback_sys_t *p_args,
65 httpd_client_t *cl, httpd_message_t *answer,
66 const httpd_message_t *query );
67 static int RtspCallbackId( httpd_callback_sys_t *p_args,
68 httpd_client_t *cl, httpd_message_t *answer,
69 const httpd_message_t *query );
70 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
72 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
74 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
76 if( rtsp == NULL || ( url->i_port > 99999 ) )
82 rtsp->owner = p_stream;
84 rtsp->sessionv = NULL;
87 rtsp->psz_path = NULL;
89 vlc_mutex_init( &rtsp->lock );
91 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
92 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
93 if( rtsp->psz_path == NULL )
96 assert( strlen( rtsp->psz_path ) > 0 );
97 rtsp->track_sep = rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' ?
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 );
146 struct rtsp_stream_id_t
148 rtsp_stream_t *stream;
149 sout_stream_id_t *sout_id;
155 uint16_t loport, hiport;
159 typedef struct rtsp_strack_t rtsp_strack_t;
161 /* For unicast streaming */
162 struct rtsp_session_t
164 rtsp_stream_t *stream;
167 /* output (id-access) */
169 rtsp_strack_t *trackv;
173 /* Unicast session track */
176 rtsp_stream_id_t *id;
182 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
184 /* Multicast stuff - TODO: cleanup */
185 const char *dst, int ttl,
186 unsigned loport, unsigned hiport )
188 char urlbuf[sizeof( "/trackID=123" ) + strlen( rtsp->psz_path )];
189 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
197 id->track_id = rtsp->track_id;
199 /* TODO: can we assume that this need not be strdup'd? */
201 if( id->dst != NULL )
208 snprintf( urlbuf, sizeof( urlbuf ), "%s%strackID=%u", rtsp->psz_path,
209 rtsp->track_sep, id->track_id );
210 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
211 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
219 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
220 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
221 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
222 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
223 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
224 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
232 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
234 vlc_mutex_lock( &rtsp->lock );
235 for( int i = 0; i < rtsp->sessionc; i++ )
237 rtsp_session_t *ses = rtsp->sessionv[i];
239 for( int j = 0; j < ses->trackc; j++ )
241 if( ses->trackv[j].id == id )
243 rtsp_strack_t *tr = ses->trackv + j;
245 REMOVE_ELEM( ses->trackv, ses->trackc, j );
250 vlc_mutex_unlock( &rtsp->lock );
251 httpd_UrlDelete( id->url );
256 /** rtsp must be locked */
258 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
260 rtsp_session_t *s = malloc( sizeof( *s ) );
265 vlc_rand_bytes (&s->id, sizeof (s->id));
269 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
275 /** rtsp must be locked */
277 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
287 id = strtoull( name, &end, 0x10 );
291 /* FIXME: use a hash/dictionary */
292 for( i = 0; i < rtsp->sessionc; i++ )
294 if( rtsp->sessionv[i]->id == id )
295 return rtsp->sessionv[i];
301 /** rtsp must be locked */
303 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
306 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
308 for( i = 0; i < session->trackc; i++ )
309 rtp_del_sink( session->trackv[i].id->sout_id, session->trackv[i].fd );
311 free( session->trackv );
316 /** Finds the next transport choice */
317 static inline const char *transport_next( const char *str )
319 /* Looks for comma */
320 str = strchr( str, ',' );
322 return NULL; /* No more transport options */
324 str++; /* skips comma */
325 while( strchr( "\r\n\t ", *str ) )
328 return (*str) ? str : NULL;
332 /** Finds the next transport parameter */
333 static inline const char *parameter_next( const char *str )
335 while( strchr( ",;", *str ) == NULL )
338 return (*str == ';') ? (str + 1) : NULL;
342 /** RTSP requests handler
343 * @param id selected track for non-aggregate URLs,
344 * NULL for aggregate URLs
346 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
348 httpd_message_t *answer,
349 const httpd_message_t *query )
351 sout_stream_t *p_stream = rtsp->owner;
353 const char *psz_session = NULL, *psz;
354 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
355 + strlen( rtsp->psz_path )];
360 if( answer == NULL || query == NULL || cl == NULL )
364 /* Build self-referential control URL */
365 char ip[NI_MAXNUMERICHOST], *ptr;
367 httpd_ServerIP( cl, ip );
368 ptr = strchr( ip, '%' );
372 if( strchr( ip, ':' ) != NULL )
373 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
376 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
381 answer->i_proto = HTTPD_PROTO_RTSP;
382 answer->i_version= 0;
383 answer->i_type = HTTPD_MSG_ANSWER;
385 answer->p_body = NULL;
387 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
389 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
391 if (gmtime_r (&now, &ut) != NULL)
392 { /* RFC1123 format, GMT is mandatory */
393 static const char wdays[7][4] = {
394 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
395 static const char mons[12][4] = {
396 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
397 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
398 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
399 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
400 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
403 if( query->i_proto != HTTPD_PROTO_RTSP )
405 answer->i_status = 505;
408 if( httpd_MsgGet( query, "Require" ) != NULL )
410 answer->i_status = 551;
411 httpd_MsgAdd( answer, "Unsupported", "%s",
412 httpd_MsgGet( query, "Require" ) );
415 switch( query->i_type )
417 case HTTPD_MSG_DESCRIBE:
418 { /* Aggregate-only */
421 answer->i_status = 460;
425 answer->i_status = 200;
426 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
427 httpd_MsgAdd( answer, "Content-Base", "%s", control );
428 answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
429 if( answer->p_body != NULL )
430 answer->i_body = strlen( (char *)answer->p_body );
432 answer->i_status = 500;
436 case HTTPD_MSG_SETUP:
437 /* Non-aggregate-only */
440 answer->i_status = 459;
444 psz_session = httpd_MsgGet( query, "Session" );
445 answer->i_status = 461;
447 for( const char *tpt = httpd_MsgGet( query, "Transport" );
449 tpt = transport_next( tpt ) )
451 bool b_multicast = true, b_unsupp = false;
452 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
454 /* Check transport protocol. */
455 /* Currently, we only support RTP/AVP over UDP */
456 if( strncmp( tpt, "RTP/AVP", 7 ) )
459 if( strncmp( tpt, "/UDP", 4 ) == 0 )
461 if( strchr( ";,", *tpt ) == NULL )
464 /* Parse transport options */
465 for( const char *opt = parameter_next( tpt );
467 opt = parameter_next( opt ) )
469 if( strncmp( opt, "multicast", 9 ) == 0)
472 if( strncmp( opt, "unicast", 7 ) == 0 )
475 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
479 if( strncmp( opt, "mode=", 5 ) == 0 )
481 if( strncasecmp( opt + 5, "play", 4 )
482 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
490 if( strncmp( opt,"destination=", 12 ) == 0 )
492 answer->i_status = 403;
498 * Every other option is unsupported:
500 * "source" and "append" are invalid (server-only);
501 * "ssrc" also (as clarified per RFC2326bis).
503 * For multicast, "port", "layers", "ttl" are set by the
504 * stream output configuration.
506 * For unicast, we want to decide "server_port" values.
508 * "interleaved" is not implemented.
520 const char *dst = id->dst;
524 if( psz_session == NULL )
526 /* Create a dummy session ID */
527 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
529 psz_session = psz_sesbuf;
531 answer->i_status = 200;
533 httpd_MsgAdd( answer, "Transport",
534 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
536 dst, id->loport, id->hiport,
537 ( id->ttl > 0 ) ? id->ttl : 1 );
541 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
542 rtsp_session_t *ses = NULL;
543 rtsp_strack_t track = { id, -1, false };
546 if( httpd_ClientIP( cl, ip ) == NULL )
548 answer->i_status = 500;
552 track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
557 "cannot create RTP socket for %s port %u",
559 answer->i_status = 500;
563 /* Ignore any unexpected incoming packet */
564 setsockopt (track.fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
566 net_GetSockAddress( track.fd, src, &sport );
568 vlc_mutex_lock( &rtsp->lock );
569 if( psz_session == NULL )
571 ses = RtspClientNew( rtsp );
572 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
574 psz_session = psz_sesbuf;
578 /* FIXME: we probably need to remove an access out,
579 * if there is already one for the same ID */
580 ses = RtspClientGet( rtsp, psz_session );
583 answer->i_status = 454;
584 vlc_mutex_unlock( &rtsp->lock );
589 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
591 vlc_mutex_unlock( &rtsp->lock );
593 httpd_ServerIP( cl, ip );
595 if( strcmp( src, ip ) )
597 /* Specify source IP if it is different from the RTSP
598 * control connection server address */
599 char *ptr = strchr( src, '%' );
600 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
602 httpd_MsgAdd( answer, "Transport",
603 "RTP/AVP/UDP;unicast;source=%s;"
604 "client_port=%u-%u;server_port=%u-%u;"
605 "ssrc=%08X;mode=play",
606 src, loport, loport + 1, sport,
607 sport + 1, id->ssrc );
611 httpd_MsgAdd( answer, "Transport",
612 "RTP/AVP/UDP;unicast;"
613 "client_port=%u-%u;server_port=%u-%u;"
614 "ssrc=%08X;mode=play",
615 loport, loport + 1, sport, sport + 1,
619 answer->i_status = 200;
628 answer->i_status = 200;
630 psz_session = httpd_MsgGet( query, "Session" );
631 const char *range = httpd_MsgGet (query, "Range");
632 if (range && strncmp (range, "npt=", 4))
634 answer->i_status = 501;
638 vlc_mutex_lock( &rtsp->lock );
639 ses = RtspClientGet( rtsp, psz_session );
642 /* FIXME: we really need to limit the number of tracks... */
643 char info[ses->trackc * ( strlen( control )
644 + sizeof("url=/trackID=123;seq=65535;"
645 "rtptime=4294967295, ") ) + 1];
647 int64_t ts = rtp_get_ts( rtsp->owner );
649 for( int i = 0; i < ses->trackc; i++ )
651 rtsp_strack_t *tr = ses->trackv + i;
652 if( ( id == NULL ) || ( tr->id == id ) )
658 rtp_add_sink( tr->id->sout_id, tr->fd, false,
662 seq = rtp_get_seq( tr->id->sout_id );
663 infolen += sprintf( info + infolen,
664 "url=%s%strackID=%u;seq=%u;rtptime=%u, ",
665 control, rtsp->track_sep,
666 tr->id->track_id, seq,
667 rtp_compute_ts( tr->id->sout_id, ts ) );
672 info[infolen - 2] = '\0'; /* remove trailing ", " */
673 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
676 vlc_mutex_unlock( &rtsp->lock );
678 if( httpd_MsgGet( query, "Scale" ) != NULL )
679 httpd_MsgAdd( answer, "Scale", "1." );
683 case HTTPD_MSG_PAUSE:
684 answer->i_status = 405;
685 httpd_MsgAdd( answer, "Allow",
686 "%s, TEARDOWN, PLAY, GET_PARAMETER",
687 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
690 case HTTPD_MSG_GETPARAMETER:
691 if( query->i_body > 0 )
693 answer->i_status = 451;
697 psz_session = httpd_MsgGet( query, "Session" );
698 answer->i_status = 200;
701 case HTTPD_MSG_TEARDOWN:
705 answer->i_status = 200;
707 psz_session = httpd_MsgGet( query, "Session" );
709 vlc_mutex_lock( &rtsp->lock );
710 ses = RtspClientGet( rtsp, psz_session );
713 if( id == NULL ) /* Delete the entire session */
714 RtspClientDel( rtsp, ses );
715 else /* Delete one track from the session */
716 for( int i = 0; i < ses->trackc; i++ )
718 if( ses->trackv[i].id == id )
720 rtp_del_sink( id->sout_id, ses->trackv[i].fd );
721 REMOVE_ELEM( ses->trackv, ses->trackc, i );
725 vlc_mutex_unlock( &rtsp->lock );
734 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
736 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
737 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
739 psz = httpd_MsgGet( query, "Cseq" );
741 httpd_MsgAdd( answer, "Cseq", "%s", psz );
742 psz = httpd_MsgGet( query, "Timestamp" );
744 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
750 /** Aggregate RTSP callback */
751 static int RtspCallback( httpd_callback_sys_t *p_args,
753 httpd_message_t *answer,
754 const httpd_message_t *query )
756 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
760 /** Non-aggregate RTSP callback */
761 static int RtspCallbackId( httpd_callback_sys_t *p_args,
763 httpd_message_t *answer,
764 const httpd_message_t *query )
766 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
767 return RtspHandler( id->stream, id, cl, answer, query );