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;
54 rtsp_session_t **sessionv;
58 static int RtspCallback( httpd_callback_sys_t *p_args,
59 httpd_client_t *cl, httpd_message_t *answer,
60 const httpd_message_t *query );
61 static int RtspCallbackId( httpd_callback_sys_t *p_args,
62 httpd_client_t *cl, httpd_message_t *answer,
63 const httpd_message_t *query );
64 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
66 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
68 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
70 if( rtsp == NULL || ( url->i_port > 99999 ) )
73 rtsp->owner = p_stream;
75 rtsp->sessionv = NULL;
76 vlc_mutex_init( p_stream, &rtsp->lock );
78 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n",
79 url->psz_host, url->i_port, url->psz_path );
81 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
82 if( url->psz_path != NULL )
83 rtsp->psz_path = strdup( url->psz_path + 1 );
85 rtsp->psz_path = NULL;
88 if( asprintf( &rtsp->psz_control, "rtsp://%s:%d%s",
89 url->psz_host, url->i_port > 0 ? url->i_port : 554,
90 rtsp->psz_path ) == -1 )
92 rtsp->psz_control = NULL;
97 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
99 if( rtsp->host == NULL )
102 rtsp->url = httpd_UrlNewUnique( rtsp->host,
103 url->psz_path ? url->psz_path : "/", NULL,
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 vlc_mutex_destroy( &rtsp->lock );
138 struct rtsp_stream_id_t
140 rtsp_stream_t *stream;
141 sout_stream_id_t *sout_id;
145 unsigned loport, hiport;
149 typedef struct rtsp_strack_t rtsp_strack_t;
151 /* For unicast streaming */
152 struct rtsp_session_t
154 rtsp_stream_t *stream;
157 /* output (id-access) */
159 rtsp_strack_t *trackv;
163 /* Unicast session track */
166 sout_stream_id_t *id;
167 sout_access_out_t *access;
172 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
174 /* Multicast stuff - TODO: cleanup */
175 const char *dst, int ttl,
176 unsigned loport, unsigned hiport )
178 char urlbuf[sizeof( "//trackID=123" ) + strlen( rtsp->psz_path )];
179 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
187 /* TODO: can we assume that this need not be strdup'd? */
189 if( id->dst != NULL )
196 snprintf( urlbuf, sizeof( urlbuf ), "/%s/trackID=%u", rtsp->psz_path,
198 msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
199 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
207 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
208 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
209 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
210 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
211 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
212 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
218 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
220 vlc_mutex_lock( &rtsp->lock );
221 for( int i = 0; i < rtsp->sessionc; i++ )
223 rtsp_session_t *ses = rtsp->sessionv[i];
225 for( int j = 0; j < ses->trackc; j++ )
227 if( ses->trackv[j].id == id->sout_id )
229 rtsp_strack_t *tr = ses->trackv + j;
230 sout_AccessOutDelete( tr->access );
231 REMOVE_ELEM( ses->trackv, ses->trackc, j );
232 /* FIXME: are we supposed to notify the client? */
237 vlc_mutex_unlock( &rtsp->lock );
238 httpd_UrlDelete( id->url );
243 /** rtsp must be locked */
245 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
247 rtsp_session_t *s = malloc( sizeof( *s ) );
252 vlc_rand_bytes (&s->id, sizeof (s->id));
256 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
262 /** rtsp must be locked */
264 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
274 id = strtoull( name, &end, 0x10 );
278 /* FIXME: use a hash/dictionary */
279 for( i = 0; i < rtsp->sessionc; i++ )
281 if( rtsp->sessionv[i]->id == id )
282 return rtsp->sessionv[i];
288 /** rtsp must be locked */
290 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
293 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
295 for( i = 0; i < session->trackc; i++ )
297 rtp_del_sink( session->trackv[i].id, session->trackv[i].access );
298 sout_AccessOutDelete( session->trackv[i].access );
301 free( session->trackv );
306 /** Aggregate RTSP callback */
307 static int RtspCallback( httpd_callback_sys_t *p_args,
309 httpd_message_t *answer,
310 const httpd_message_t *query )
312 rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
313 const char *psz_session = NULL, *psz;
315 if( answer == NULL || query == NULL )
320 answer->i_proto = HTTPD_PROTO_RTSP;
321 answer->i_version= query->i_version;
322 answer->i_type = HTTPD_MSG_ANSWER;
324 answer->p_body = NULL;
326 if( httpd_MsgGet( query, "Require" ) != NULL )
328 answer->i_status = 551;
329 httpd_MsgAdd( answer, "Unsupported", "%s",
330 httpd_MsgGet( query, "Require" ) );
333 switch( query->i_type )
335 case HTTPD_MSG_DESCRIBE:
337 char ip[NI_MAXNUMERICHOST], *ptr;
338 char control[sizeof("rtsp://[]:12345/") + sizeof( ip )
339 + strlen( rtsp->psz_path )];
341 /* Build self-referential URL */
342 httpd_ServerIP( cl, ip );
343 ptr = strchr( ip, '%' );
347 if( strchr( ip, ':' ) != NULL )
348 sprintf( control, "rtsp://[%s]:%u/%s", ip, rtsp->port,
349 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
351 sprintf( control, "rtsp://%s:%u/%s", ip, rtsp->port,
352 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
354 ptr = SDPGenerate( rtsp->owner, control );
356 answer->i_status = 200;
357 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
358 httpd_MsgAdd( answer, "Content-Base", "%s", control );
359 answer->p_body = (uint8_t *)ptr;
360 answer->i_body = strlen( ptr );
364 case HTTPD_MSG_SETUP:
365 answer->i_status = 459;
371 answer->i_status = 200;
373 psz_session = httpd_MsgGet( query, "Session" );
374 if( httpd_MsgGet( query, "Range" ) != NULL )
376 answer->i_status = 456; /* cannot seek */
380 vlc_mutex_lock( &rtsp->lock );
381 ses = RtspClientGet( rtsp, psz_session );
384 for( int i = 0; i < ses->trackc; i++ )
386 rtsp_strack_t *tr = ses->trackv + i;
389 tr->playing = VLC_TRUE;
390 rtp_add_sink( tr->id, tr->access );
394 vlc_mutex_unlock( &rtsp->lock );
396 if( httpd_MsgGet( query, "Scale" ) != NULL )
397 httpd_MsgAdd( answer, "Scale", "1." );
401 case HTTPD_MSG_PAUSE:
402 answer->i_status = 405;
403 httpd_MsgAdd( answer, "Allow",
404 "DESCRIBE, TEARDOWN, PLAY, GET_PARAMETER" );
407 case HTTPD_MSG_GETPARAMETER:
408 if( query->i_body > 0 )
410 answer->i_status = 451;
414 answer->i_status = 200;
417 case HTTPD_MSG_TEARDOWN:
421 /* for now only multicast so easy again */
422 answer->i_status = 200;
424 psz_session = httpd_MsgGet( query, "Session" );
426 vlc_mutex_lock( &rtsp->lock );
427 ses = RtspClientGet( rtsp, psz_session );
429 RtspClientDel( rtsp, ses );
430 vlc_mutex_unlock( &rtsp->lock );
438 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
439 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
441 psz = httpd_MsgGet( query, "Cseq" );
443 httpd_MsgAdd( answer, "Cseq", "%s", psz );
444 psz = httpd_MsgGet( query, "Timestamp" );
446 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
448 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
451 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
456 /** Finds the next transport choice */
457 static inline const char *transport_next( const char *str )
459 /* Looks for comma */
460 str = strchr( str, ',' );
462 return NULL; /* No more transport options */
464 str++; /* skips comma */
465 while( strchr( "\r\n\t ", *str ) )
468 return (*str) ? str : NULL;
472 /** Finds the next transport parameter */
473 static inline const char *parameter_next( const char *str )
475 while( strchr( ",;", *str ) == NULL )
478 return (*str == ';') ? (str + 1) : NULL;
482 /** Non-aggregate RTSP callback */
483 static int RtspCallbackId( httpd_callback_sys_t *p_args,
485 httpd_message_t *answer,
486 const httpd_message_t *query )
488 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
489 rtsp_stream_t *rtsp = id->stream;
490 sout_stream_t *p_stream = id->stream->owner;
492 const char *psz_session, *psz;
494 if( answer == NULL || query == NULL )
498 answer->i_proto = HTTPD_PROTO_RTSP;
499 answer->i_version= query->i_version;
500 answer->i_type = HTTPD_MSG_ANSWER;
502 answer->p_body = NULL;
504 psz_session = httpd_MsgGet( query, "Session" );
506 if( httpd_MsgGet( query, "Require" ) != NULL )
508 answer->i_status = 551;
509 httpd_MsgAdd( answer, "Unsupported", "%s",
510 httpd_MsgGet( query, "Require" ) );
513 switch( query->i_type )
515 case HTTPD_MSG_SETUP:
517 answer->i_status = 461;
519 for( const char *tpt = httpd_MsgGet( query, "Transport" );
521 tpt = transport_next( tpt ) )
523 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
524 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
526 /* Check transport protocol. */
527 /* Currently, we only support RTP/AVP over UDP */
528 if( strncmp( tpt, "RTP/AVP", 7 ) )
531 if( strncmp( tpt, "/UDP", 4 ) == 0 )
533 if( strchr( ";,", *tpt ) == NULL )
536 /* Parse transport options */
537 for( const char *opt = parameter_next( tpt );
539 opt = parameter_next( opt ) )
541 if( strncmp( opt, "multicast", 9 ) == 0)
542 b_multicast = VLC_TRUE;
544 if( strncmp( opt, "unicast", 7 ) == 0 )
545 b_multicast = VLC_FALSE;
547 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
551 if( strncmp( opt, "mode=", 5 ) == 0 )
553 if( strncasecmp( opt + 5, "play", 4 )
554 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
562 if( strncmp( opt,"destination=", 12 ) == 0 )
564 answer->i_status = 403;
570 * Every other option is unsupported:
572 * "source" and "append" are invalid (server-only);
573 * "ssrc" also (as clarified per RFC2326bis).
575 * For multicast, "port", "layers", "ttl" are set by the
576 * stream output configuration.
578 * For unicast, we want to decide "server_port" values.
580 * "interleaved" is not implemented.
592 const char *dst = id->dst;
596 answer->i_status = 200;
598 httpd_MsgAdd( answer, "Transport",
599 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
601 dst, id->loport, id->hiport,
602 ( id->ttl > 0 ) ? id->ttl : 1 );
606 char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
607 static const char access[] = "udp{raw,rtcp}";
608 rtsp_session_t *ses = NULL;
609 rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
611 if( httpd_ClientIP( cl, ip ) == NULL )
613 answer->i_status = 500;
617 snprintf( url, sizeof( url ),
618 ( strchr( ip, ':' ) != NULL )
619 ? "[%s]:%d" : "%s:%d",
622 track.access = sout_AccessOutNew( p_stream->p_sout,
624 if( track.access == NULL )
627 "cannot create access output for %s://%s",
629 answer->i_status = 500;
633 char *src = var_GetNonEmptyString( track.access,
635 int sport = var_GetInteger( track.access, "src-port" );
637 vlc_mutex_lock( &rtsp->lock );
638 if( psz_session == NULL )
640 ses = RtspClientNew( rtsp );
641 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
643 psz_session = psz_sesbuf;
647 /* FIXME: we probably need to remove an access out,
648 * if there is already one for the same ID */
649 ses = RtspClientGet( rtsp, psz_session );
652 answer->i_status = 454;
653 vlc_mutex_unlock( &rtsp->lock );
658 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
659 vlc_mutex_unlock( &rtsp->lock );
661 httpd_ServerIP( cl, ip );
663 if( ( src != NULL ) && strcmp( src, ip ) )
665 /* Specify source IP if it is different from the RTSP
666 * control connection server address */
667 char *ptr = strchr( src, '%' );
668 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
670 httpd_MsgAdd( answer, "Transport",
671 "RTP/AVP/UDP;unicast;source=%s;"
672 "client_port=%u-%u;server_port=%u-%u;"
674 src, loport, loport + 1, sport,
679 httpd_MsgAdd( answer, "Transport",
680 "RTP/AVP/UDP;unicast;"
681 "client_port=%u-%u;server_port=%u-%u;"
683 loport, loport + 1, sport, sport + 1 );
686 answer->i_status = 200;
697 answer->i_status = 200;
699 psz_session = httpd_MsgGet( query, "Session" );
700 if( httpd_MsgGet( query, "Range" ) != NULL )
702 answer->i_status = 456; /* cannot seek */
706 vlc_mutex_lock( &rtsp->lock );
707 ses = RtspClientGet( rtsp, psz_session );
710 for( int i = 0; i < ses->trackc; i++ )
712 rtsp_strack_t *tr = ses->trackv + i;
713 if( !tr->playing && ( tr->id == id->sout_id ) )
715 tr->playing = VLC_TRUE;
716 rtp_add_sink( tr->id, tr->access );
720 vlc_mutex_unlock( &rtsp->lock );
722 if( httpd_MsgGet( query, "Scale" ) != NULL )
723 httpd_MsgAdd( answer, "Scale", "1." );
727 case HTTPD_MSG_PAUSE:
728 answer->i_status = 405;
729 httpd_MsgAdd( answer, "Allow",
730 "SETUP, TEARDOWN, PLAY, GET_PARAMETER" );
733 case HTTPD_MSG_GETPARAMETER:
734 if( query->i_body > 0 )
736 answer->i_status = 451;
740 answer->i_status = 200;
743 case HTTPD_MSG_TEARDOWN:
747 answer->i_status = 200;
749 psz_session = httpd_MsgGet( query, "Session" );
751 vlc_mutex_lock( &rtsp->lock );
752 ses = RtspClientGet( rtsp, psz_session );
755 for( int i = 0; i < ses->trackc; i++ )
757 if( ses->trackv[i].id == id->sout_id )
759 rtp_del_sink( id->sout_id, ses->trackv[i].access );
760 sout_AccessOutDelete( ses->trackv[i].access );
761 REMOVE_ELEM( ses->trackv, ses->trackc, i );
765 vlc_mutex_unlock( &rtsp->lock );
770 answer->i_status = 460;
774 psz = httpd_MsgGet( query, "Cseq" );
776 httpd_MsgAdd( answer, "Cseq", "%s", psz );
777 psz = httpd_MsgGet( query, "Timestamp" );
779 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
780 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
781 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
782 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
785 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );