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_fmt;
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 if( rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' )
98 rtsp->track_fmt = "%strackID=%u";
100 rtsp->track_fmt = "%s/trackID=%u";
102 msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
103 url->psz_host, rtsp->port, rtsp->psz_path );
105 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
107 if( rtsp->host == NULL )
110 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
112 if( rtsp->url == NULL )
115 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
116 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
117 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
118 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
119 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
121 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
130 void RtspUnsetup( rtsp_stream_t *rtsp )
132 while( rtsp->sessionc > 0 )
133 RtspClientDel( rtsp, rtsp->sessionv[0] );
136 httpd_UrlDelete( rtsp->url );
139 httpd_HostDelete( rtsp->host );
141 free( rtsp->psz_path );
142 vlc_mutex_destroy( &rtsp->lock );
148 struct rtsp_stream_id_t
150 rtsp_stream_t *stream;
151 sout_stream_id_t *sout_id;
157 uint16_t loport, hiport;
161 typedef struct rtsp_strack_t rtsp_strack_t;
163 /* For unicast streaming */
164 struct rtsp_session_t
166 rtsp_stream_t *stream;
169 /* output (id-access) */
171 rtsp_strack_t *trackv;
175 /* Unicast session track */
178 rtsp_stream_id_t *id;
184 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
186 /* Multicast stuff - TODO: cleanup */
187 const char *dst, int ttl,
188 unsigned loport, unsigned hiport )
190 char urlbuf[sizeof( "/trackID=123" ) + strlen( rtsp->psz_path )];
191 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
199 id->track_id = rtsp->track_id;
201 /* TODO: can we assume that this need not be strdup'd? */
203 if( id->dst != NULL )
210 snprintf( urlbuf, sizeof( urlbuf ), rtsp->track_fmt, rtsp->psz_path,
212 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
213 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
221 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
222 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
223 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
224 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
225 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
226 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
234 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
236 vlc_mutex_lock( &rtsp->lock );
237 for( int i = 0; i < rtsp->sessionc; i++ )
239 rtsp_session_t *ses = rtsp->sessionv[i];
241 for( int j = 0; j < ses->trackc; j++ )
243 if( ses->trackv[j].id == id )
245 rtsp_strack_t *tr = ses->trackv + j;
247 REMOVE_ELEM( ses->trackv, ses->trackc, j );
252 vlc_mutex_unlock( &rtsp->lock );
253 httpd_UrlDelete( id->url );
258 /** rtsp must be locked */
260 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
262 rtsp_session_t *s = malloc( sizeof( *s ) );
267 vlc_rand_bytes (&s->id, sizeof (s->id));
271 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
277 /** rtsp must be locked */
279 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
289 id = strtoull( name, &end, 0x10 );
293 /* FIXME: use a hash/dictionary */
294 for( i = 0; i < rtsp->sessionc; i++ )
296 if( rtsp->sessionv[i]->id == id )
297 return rtsp->sessionv[i];
303 /** rtsp must be locked */
305 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
308 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
310 for( i = 0; i < session->trackc; i++ )
311 rtp_del_sink( session->trackv[i].id->sout_id, session->trackv[i].fd );
313 free( session->trackv );
318 /** Finds the next transport choice */
319 static inline const char *transport_next( const char *str )
321 /* Looks for comma */
322 str = strchr( str, ',' );
324 return NULL; /* No more transport options */
326 str++; /* skips comma */
327 while( strchr( "\r\n\t ", *str ) )
330 return (*str) ? str : NULL;
334 /** Finds the next transport parameter */
335 static inline const char *parameter_next( const char *str )
337 while( strchr( ",;", *str ) == NULL )
340 return (*str == ';') ? (str + 1) : NULL;
344 /** RTSP requests handler
345 * @param id selected track for non-aggregate URLs,
346 * NULL for aggregate URLs
348 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
350 httpd_message_t *answer,
351 const httpd_message_t *query )
353 sout_stream_t *p_stream = rtsp->owner;
355 const char *psz_session = NULL, *psz;
356 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
357 + strlen( rtsp->psz_path )];
362 if( answer == NULL || query == NULL || cl == NULL )
366 /* Build self-referential control URL */
367 char ip[NI_MAXNUMERICHOST], *ptr;
369 httpd_ServerIP( cl, ip );
370 ptr = strchr( ip, '%' );
374 if( strchr( ip, ':' ) != NULL )
375 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
378 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
383 answer->i_proto = HTTPD_PROTO_RTSP;
384 answer->i_version= 0;
385 answer->i_type = HTTPD_MSG_ANSWER;
387 answer->p_body = NULL;
389 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
391 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
393 if (gmtime_r (&now, &ut) != NULL)
394 { /* RFC1123 format, GMT is mandatory */
395 static const char wdays[7][4] = {
396 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
397 static const char mons[12][4] = {
398 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
399 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
400 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
401 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
402 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
405 if( query->i_proto != HTTPD_PROTO_RTSP )
407 answer->i_status = 505;
410 if( httpd_MsgGet( query, "Require" ) != NULL )
412 answer->i_status = 551;
413 httpd_MsgAdd( answer, "Unsupported", "%s",
414 httpd_MsgGet( query, "Require" ) );
417 switch( query->i_type )
419 case HTTPD_MSG_DESCRIBE:
420 { /* Aggregate-only */
423 answer->i_status = 460;
427 answer->i_status = 200;
428 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
429 httpd_MsgAdd( answer, "Content-Base", "%s", control );
430 answer->p_body = (uint8_t *)SDPGenerate( rtsp->owner, control );
431 if( answer->p_body != NULL )
432 answer->i_body = strlen( (char *)answer->p_body );
434 answer->i_status = 500;
438 case HTTPD_MSG_SETUP:
439 /* Non-aggregate-only */
442 answer->i_status = 459;
446 psz_session = httpd_MsgGet( query, "Session" );
447 answer->i_status = 461;
449 for( const char *tpt = httpd_MsgGet( query, "Transport" );
451 tpt = transport_next( tpt ) )
453 bool b_multicast = true, b_unsupp = false;
454 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
456 /* Check transport protocol. */
457 /* Currently, we only support RTP/AVP over UDP */
458 if( strncmp( tpt, "RTP/AVP", 7 ) )
461 if( strncmp( tpt, "/UDP", 4 ) == 0 )
463 if( strchr( ";,", *tpt ) == NULL )
466 /* Parse transport options */
467 for( const char *opt = parameter_next( tpt );
469 opt = parameter_next( opt ) )
471 if( strncmp( opt, "multicast", 9 ) == 0)
474 if( strncmp( opt, "unicast", 7 ) == 0 )
477 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
481 if( strncmp( opt, "mode=", 5 ) == 0 )
483 if( strncasecmp( opt + 5, "play", 4 )
484 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
492 if( strncmp( opt,"destination=", 12 ) == 0 )
494 answer->i_status = 403;
500 * Every other option is unsupported:
502 * "source" and "append" are invalid (server-only);
503 * "ssrc" also (as clarified per RFC2326bis).
505 * For multicast, "port", "layers", "ttl" are set by the
506 * stream output configuration.
508 * For unicast, we want to decide "server_port" values.
510 * "interleaved" is not implemented.
522 const char *dst = id->dst;
526 if( psz_session == NULL )
528 /* Create a dummy session ID */
529 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%d",
531 psz_session = psz_sesbuf;
533 answer->i_status = 200;
535 httpd_MsgAdd( answer, "Transport",
536 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
538 dst, id->loport, id->hiport,
539 ( id->ttl > 0 ) ? id->ttl : 1 );
543 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
544 rtsp_session_t *ses = NULL;
545 rtsp_strack_t track = { id, -1, false };
548 if( httpd_ClientIP( cl, ip ) == NULL )
550 answer->i_status = 500;
554 track.fd = net_ConnectDgram( p_stream, ip, loport, -1,
559 "cannot create RTP socket for %s port %u",
561 answer->i_status = 500;
565 /* Ignore any unexpected incoming packet */
566 setsockopt (track.fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
568 net_GetSockAddress( track.fd, src, &sport );
570 vlc_mutex_lock( &rtsp->lock );
571 if( psz_session == NULL )
573 ses = RtspClientNew( rtsp );
574 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
576 psz_session = psz_sesbuf;
580 /* FIXME: we probably need to remove an access out,
581 * if there is already one for the same ID */
582 ses = RtspClientGet( rtsp, psz_session );
585 answer->i_status = 454;
586 vlc_mutex_unlock( &rtsp->lock );
591 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
593 vlc_mutex_unlock( &rtsp->lock );
595 httpd_ServerIP( cl, ip );
597 if( strcmp( src, ip ) )
599 /* Specify source IP if it is different from the RTSP
600 * control connection server address */
601 char *ptr = strchr( src, '%' );
602 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
604 httpd_MsgAdd( answer, "Transport",
605 "RTP/AVP/UDP;unicast;source=%s;"
606 "client_port=%u-%u;server_port=%u-%u;"
607 "ssrc=%08X;mode=play",
608 src, loport, loport + 1, sport,
609 sport + 1, id->ssrc );
613 httpd_MsgAdd( answer, "Transport",
614 "RTP/AVP/UDP;unicast;"
615 "client_port=%u-%u;server_port=%u-%u;"
616 "ssrc=%08X;mode=play",
617 loport, loport + 1, sport, sport + 1,
621 answer->i_status = 200;
630 answer->i_status = 200;
632 psz_session = httpd_MsgGet( query, "Session" );
633 const char *range = httpd_MsgGet (query, "Range");
634 if (range && strncmp (range, "npt=", 4))
636 answer->i_status = 501;
640 vlc_mutex_lock( &rtsp->lock );
641 ses = RtspClientGet( rtsp, psz_session );
644 /* FIXME: we really need to limit the number of tracks... */
645 char info[ses->trackc * ( strlen( control )
646 + sizeof("url=/trackID=123;seq=65535;"
647 "rtptime=4294967295, ") ) + 1];
649 int64_t ts = rtp_get_ts( rtsp->owner );
651 for( int i = 0; i < ses->trackc; i++ )
653 rtsp_strack_t *tr = ses->trackv + i;
654 if( ( id == NULL ) || ( tr->id == id ) )
660 rtp_add_sink( tr->id->sout_id, tr->fd, false,
664 seq = rtp_get_seq( tr->id->sout_id );
665 infolen += sprintf( info + infolen,
666 "url=%s/trackID=%u;seq=%u;rtptime=%u, ",
667 control, tr->id->track_id, seq,
668 rtp_compute_ts( tr->id->sout_id, ts ) );
673 info[infolen - 2] = '\0'; /* remove trailing ", " */
674 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
677 vlc_mutex_unlock( &rtsp->lock );
679 if( httpd_MsgGet( query, "Scale" ) != NULL )
680 httpd_MsgAdd( answer, "Scale", "1." );
684 case HTTPD_MSG_PAUSE:
685 answer->i_status = 405;
686 httpd_MsgAdd( answer, "Allow",
687 "%s, TEARDOWN, PLAY, GET_PARAMETER",
688 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
691 case HTTPD_MSG_GETPARAMETER:
692 if( query->i_body > 0 )
694 answer->i_status = 451;
698 psz_session = httpd_MsgGet( query, "Session" );
699 answer->i_status = 200;
702 case HTTPD_MSG_TEARDOWN:
706 answer->i_status = 200;
708 psz_session = httpd_MsgGet( query, "Session" );
710 vlc_mutex_lock( &rtsp->lock );
711 ses = RtspClientGet( rtsp, psz_session );
714 if( id == NULL ) /* Delete the entire session */
715 RtspClientDel( rtsp, ses );
716 else /* Delete one track from the session */
717 for( int i = 0; i < ses->trackc; i++ )
719 if( ses->trackv[i].id == id )
721 rtp_del_sink( id->sout_id, ses->trackv[i].fd );
722 REMOVE_ELEM( ses->trackv, ses->trackc, i );
726 vlc_mutex_unlock( &rtsp->lock );
735 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
737 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
738 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
740 psz = httpd_MsgGet( query, "Cseq" );
742 httpd_MsgAdd( answer, "Cseq", "%s", psz );
743 psz = httpd_MsgGet( query, "Timestamp" );
745 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
751 /** Aggregate RTSP callback */
752 static int RtspCallback( httpd_callback_sys_t *p_args,
754 httpd_message_t *answer,
755 const httpd_message_t *query )
757 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
761 /** Non-aggregate RTSP callback */
762 static int RtspCallbackId( httpd_callback_sys_t *p_args,
764 httpd_message_t *answer,
765 const httpd_message_t *query )
767 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
768 return RtspHandler( id->stream, id, cl, answer, query );