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;
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;
88 vlc_mutex_init( &rtsp->lock );
90 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
91 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
92 if( rtsp->psz_path == NULL )
95 msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
96 url->psz_host, rtsp->port, rtsp->psz_path );
98 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
100 if( rtsp->host == NULL )
103 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
105 if( rtsp->url == NULL )
108 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
109 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
110 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
111 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
112 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
114 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
123 void RtspUnsetup( rtsp_stream_t *rtsp )
125 while( rtsp->sessionc > 0 )
126 RtspClientDel( rtsp, rtsp->sessionv[0] );
129 httpd_UrlDelete( rtsp->url );
132 httpd_HostDelete( rtsp->host );
134 free( rtsp->psz_path );
135 vlc_mutex_destroy( &rtsp->lock );
141 struct rtsp_stream_id_t
143 rtsp_stream_t *stream;
144 sout_stream_id_t *sout_id;
150 uint16_t loport, hiport;
154 typedef struct rtsp_strack_t rtsp_strack_t;
156 /* For unicast streaming */
157 struct rtsp_session_t
159 rtsp_stream_t *stream;
162 /* output (id-access) */
164 rtsp_strack_t *trackv;
168 /* Unicast session track */
171 rtsp_stream_id_t *id;
177 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
179 const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
183 if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
189 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
191 /* Multicast stuff - TODO: cleanup */
192 const char *dst, int ttl,
193 unsigned loport, unsigned hiport )
196 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
204 id->track_id = rtsp->track_id;
206 /* TODO: can we assume that this need not be strdup'd? */
208 if( id->dst != NULL )
215 urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
222 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
223 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
232 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
233 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
234 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
235 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
236 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
237 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
245 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
247 vlc_mutex_lock( &rtsp->lock );
248 for( int i = 0; i < rtsp->sessionc; i++ )
250 rtsp_session_t *ses = rtsp->sessionv[i];
252 for( int j = 0; j < ses->trackc; j++ )
254 if( ses->trackv[j].id == id )
256 rtsp_strack_t *tr = ses->trackv + j;
257 rtp_del_sink( tr->id->sout_id, tr->fd );
258 REMOVE_ELEM( ses->trackv, ses->trackc, j );
263 vlc_mutex_unlock( &rtsp->lock );
264 httpd_UrlDelete( id->url );
269 /** rtsp must be locked */
271 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
273 rtsp_session_t *s = malloc( sizeof( *s ) );
278 vlc_rand_bytes (&s->id, sizeof (s->id));
282 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
288 /** rtsp must be locked */
290 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
300 id = strtoull( name, &end, 0x10 );
304 /* FIXME: use a hash/dictionary */
305 for( i = 0; i < rtsp->sessionc; i++ )
307 if( rtsp->sessionv[i]->id == id )
308 return rtsp->sessionv[i];
314 /** rtsp must be locked */
316 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
319 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
321 for( i = 0; i < session->trackc; i++ )
322 rtp_del_sink( session->trackv[i].id->sout_id, session->trackv[i].fd );
324 free( session->trackv );
329 /** Finds the next transport choice */
330 static inline const char *transport_next( const char *str )
332 /* Looks for comma */
333 str = strchr( str, ',' );
335 return NULL; /* No more transport options */
337 str++; /* skips comma */
338 while( strchr( "\r\n\t ", *str ) )
341 return (*str) ? str : NULL;
345 /** Finds the next transport parameter */
346 static inline const char *parameter_next( const char *str )
348 while( strchr( ",;", *str ) == NULL )
351 return (*str == ';') ? (str + 1) : NULL;
355 /** RTSP requests handler
356 * @param id selected track for non-aggregate URLs,
357 * NULL for aggregate URLs
359 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
361 httpd_message_t *answer,
362 const httpd_message_t *query )
364 sout_stream_t *p_stream = rtsp->owner;
366 const char *psz_session = NULL, *psz;
367 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
368 + strlen( rtsp->psz_path )];
373 if( answer == NULL || query == NULL || cl == NULL )
377 /* Build self-referential control URL */
378 char ip[NI_MAXNUMERICHOST], *ptr;
380 httpd_ServerIP( cl, ip );
381 ptr = strchr( ip, '%' );
385 if( strchr( ip, ':' ) != NULL )
386 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
389 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
394 answer->i_proto = HTTPD_PROTO_RTSP;
395 answer->i_version= 0;
396 answer->i_type = HTTPD_MSG_ANSWER;
398 answer->p_body = NULL;
400 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
402 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
404 if (gmtime_r (&now, &ut) != NULL)
405 { /* RFC1123 format, GMT is mandatory */
406 static const char wdays[7][4] = {
407 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
408 static const char mons[12][4] = {
409 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
410 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
411 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
412 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
413 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
416 if( query->i_proto != HTTPD_PROTO_RTSP )
418 answer->i_status = 505;
421 if( httpd_MsgGet( query, "Require" ) != NULL )
423 answer->i_status = 551;
424 httpd_MsgAdd( answer, "Unsupported", "%s",
425 httpd_MsgGet( query, "Require" ) );
428 switch( query->i_type )
430 case HTTPD_MSG_DESCRIBE:
431 { /* Aggregate-only */
434 answer->i_status = 460;
438 answer->i_status = 200;
439 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
440 httpd_MsgAdd( answer, "Content-Base", "%s", control );
441 answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
442 if( answer->p_body != NULL )
443 answer->i_body = strlen( (char *)answer->p_body );
445 answer->i_status = 500;
449 case HTTPD_MSG_SETUP:
450 /* Non-aggregate-only */
453 answer->i_status = 459;
457 psz_session = httpd_MsgGet( query, "Session" );
458 answer->i_status = 461;
460 for( const char *tpt = httpd_MsgGet( query, "Transport" );
462 tpt = transport_next( tpt ) )
464 bool b_multicast = true, b_unsupp = false;
465 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
467 /* Check transport protocol. */
468 /* Currently, we only support RTP/AVP over UDP */
469 if( strncmp( tpt, "RTP/AVP", 7 ) )
472 if( strncmp( tpt, "/UDP", 4 ) == 0 )
474 if( strchr( ";,", *tpt ) == NULL )
477 /* Parse transport options */
478 for( const char *opt = parameter_next( tpt );
480 opt = parameter_next( opt ) )
482 if( strncmp( opt, "multicast", 9 ) == 0)
485 if( strncmp( opt, "unicast", 7 ) == 0 )
488 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
492 if( strncmp( opt, "mode=", 5 ) == 0 )
494 if( strncasecmp( opt + 5, "play", 4 )
495 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
503 if( strncmp( opt,"destination=", 12 ) == 0 )
505 answer->i_status = 403;
511 * Every other option is unsupported:
513 * "source" and "append" are invalid (server-only);
514 * "ssrc" also (as clarified per RFC2326bis).
516 * For multicast, "port", "layers", "ttl" are set by the
517 * stream output configuration.
519 * For unicast, we want to decide "server_port" values.
521 * "interleaved" is not implemented.
533 const char *dst = id->dst;
537 if( psz_session == NULL )
539 /* Create a dummy session ID */
540 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
542 psz_session = psz_sesbuf;
544 answer->i_status = 200;
546 httpd_MsgAdd( answer, "Transport",
547 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
549 dst, id->loport, id->hiport,
550 ( id->ttl > 0 ) ? id->ttl : 1 );
554 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
555 rtsp_session_t *ses = NULL;
556 rtsp_strack_t track = { id, -1, false };
559 if( httpd_ClientIP( cl, ip ) == NULL )
561 answer->i_status = 500;
565 track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
570 "cannot create RTP socket for %s port %u",
572 answer->i_status = 500;
576 /* Ignore any unexpected incoming packet */
577 setsockopt (track.fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
579 net_GetSockAddress( track.fd, src, &sport );
581 vlc_mutex_lock( &rtsp->lock );
582 if( psz_session == NULL )
584 ses = RtspClientNew( rtsp );
585 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
587 psz_session = psz_sesbuf;
591 /* FIXME: we probably need to remove an access out,
592 * if there is already one for the same ID */
593 ses = RtspClientGet( rtsp, psz_session );
596 answer->i_status = 454;
597 vlc_mutex_unlock( &rtsp->lock );
602 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
604 vlc_mutex_unlock( &rtsp->lock );
606 httpd_ServerIP( cl, ip );
608 if( strcmp( src, ip ) )
610 /* Specify source IP if it is different from the RTSP
611 * control connection server address */
612 char *ptr = strchr( src, '%' );
613 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
615 httpd_MsgAdd( answer, "Transport",
616 "RTP/AVP/UDP;unicast;source=%s;"
617 "client_port=%u-%u;server_port=%u-%u;"
618 "ssrc=%08X;mode=play",
619 src, loport, loport + 1, sport,
620 sport + 1, id->ssrc );
624 httpd_MsgAdd( answer, "Transport",
625 "RTP/AVP/UDP;unicast;"
626 "client_port=%u-%u;server_port=%u-%u;"
627 "ssrc=%08X;mode=play",
628 loport, loport + 1, sport, sport + 1,
632 answer->i_status = 200;
641 answer->i_status = 200;
643 psz_session = httpd_MsgGet( query, "Session" );
644 const char *range = httpd_MsgGet (query, "Range");
645 if (range && strncmp (range, "npt=", 4))
647 answer->i_status = 501;
651 vlc_mutex_lock( &rtsp->lock );
652 ses = RtspClientGet( rtsp, psz_session );
655 /* The "trackID" part must match what is done in
656 * RtspAppendTrackPath() */
657 /* FIXME: we really need to limit the number of tracks... */
658 char info[ses->trackc * ( strlen( control )
659 + sizeof("url=/trackID=123;seq=65535;"
660 "rtptime=4294967295, ") ) + 1];
662 int64_t ts = rtp_get_ts( rtsp->owner );
664 for( int i = 0; i < ses->trackc; i++ )
666 rtsp_strack_t *tr = ses->trackv + i;
667 if( ( id == NULL ) || ( tr->id == id ) )
673 rtp_add_sink( tr->id->sout_id, tr->fd, false,
677 seq = rtp_get_seq( tr->id->sout_id );
678 char *url = RtspAppendTrackPath( tr->id, control );
679 infolen += sprintf( info + infolen,
680 "url=%s;seq=%u;rtptime=%u, ",
681 url != NULL ? url : "", seq,
682 rtp_compute_ts( tr->id->sout_id, ts ) );
688 info[infolen - 2] = '\0'; /* remove trailing ", " */
689 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
692 vlc_mutex_unlock( &rtsp->lock );
694 if( httpd_MsgGet( query, "Scale" ) != NULL )
695 httpd_MsgAdd( answer, "Scale", "1." );
699 case HTTPD_MSG_PAUSE:
700 answer->i_status = 405;
701 httpd_MsgAdd( answer, "Allow",
702 "%s, TEARDOWN, PLAY, GET_PARAMETER",
703 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
706 case HTTPD_MSG_GETPARAMETER:
707 if( query->i_body > 0 )
709 answer->i_status = 451;
713 psz_session = httpd_MsgGet( query, "Session" );
714 answer->i_status = 200;
717 case HTTPD_MSG_TEARDOWN:
721 answer->i_status = 200;
723 psz_session = httpd_MsgGet( query, "Session" );
725 vlc_mutex_lock( &rtsp->lock );
726 ses = RtspClientGet( rtsp, psz_session );
729 if( id == NULL ) /* Delete the entire session */
730 RtspClientDel( rtsp, ses );
731 else /* Delete one track from the session */
732 for( int i = 0; i < ses->trackc; i++ )
734 if( ses->trackv[i].id == id )
736 rtp_del_sink( id->sout_id, ses->trackv[i].fd );
737 REMOVE_ELEM( ses->trackv, ses->trackc, i );
741 vlc_mutex_unlock( &rtsp->lock );
750 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
752 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
753 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
755 psz = httpd_MsgGet( query, "Cseq" );
757 httpd_MsgAdd( answer, "Cseq", "%s", psz );
758 psz = httpd_MsgGet( query, "Timestamp" );
760 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
766 /** Aggregate RTSP callback */
767 static int RtspCallback( httpd_callback_sys_t *p_args,
769 httpd_message_t *answer,
770 const httpd_message_t *query )
772 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
776 /** Non-aggregate RTSP callback */
777 static int RtspCallbackId( httpd_callback_sys_t *p_args,
779 httpd_message_t *answer,
780 const httpd_message_t *query )
782 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
783 return RtspHandler( id->stream, id, cl, answer, query );