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>
41 typedef struct rtsp_session_t rtsp_session_t;
53 rtsp_session_t **sessionv;
57 static int RtspCallback( httpd_callback_sys_t *p_args,
58 httpd_client_t *cl, httpd_message_t *answer,
59 const httpd_message_t *query );
60 static int RtspCallbackId( httpd_callback_sys_t *p_args,
61 httpd_client_t *cl, httpd_message_t *answer,
62 const httpd_message_t *query );
63 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
65 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
67 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
69 if( rtsp == NULL || ( url->i_port > 99999 ) )
72 rtsp->owner = p_stream;
74 rtsp->sessionv = NULL;
75 vlc_mutex_init( p_stream, &rtsp->lock );
77 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n",
78 url->psz_host, url->i_port, url->psz_path );
80 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
81 if( url->psz_path != NULL )
82 rtsp->psz_path = strdup( url->psz_path + 1 );
84 rtsp->psz_path = NULL;
87 if( asprintf( &rtsp->psz_control, "rtsp://%s:%d%s",
88 url->psz_host, url->i_port > 0 ? url->i_port : 554,
89 rtsp->psz_path ) == -1 )
91 rtsp->psz_control = NULL;
96 rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
98 if( rtsp->host == NULL )
101 rtsp->url = httpd_UrlNewUnique( rtsp->host,
102 url->psz_path ? url->psz_path : "/", NULL,
104 if( rtsp->url == NULL )
107 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
108 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
109 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
110 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
111 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
120 void RtspUnsetup( rtsp_stream_t *rtsp )
122 while( rtsp->sessionc > 0 )
123 RtspClientDel( rtsp, rtsp->sessionv[0] );
126 httpd_UrlDelete( rtsp->url );
129 httpd_HostDelete( rtsp->host );
131 vlc_mutex_destroy( &rtsp->lock );
135 struct rtsp_stream_id_t
137 rtsp_stream_t *stream;
138 sout_stream_id_t *sout_id;
142 unsigned loport, hiport;
146 typedef struct rtsp_strack_t rtsp_strack_t;
148 /* For unicast streaming */
149 struct rtsp_session_t
151 rtsp_stream_t *stream;
154 /* output (id-access) */
156 rtsp_strack_t *trackv;
160 /* Unicast session track */
163 sout_stream_id_t *id;
164 sout_access_out_t *access;
169 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
171 /* Multicast stuff - TODO: cleanup */
172 const char *dst, int ttl,
173 unsigned loport, unsigned hiport )
175 char urlbuf[sizeof( "//trackID=123" ) + strlen( rtsp->psz_path )];
176 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
184 /* TODO: can we assume that this need not be strdup'd? */
186 if( id->dst != NULL )
193 snprintf( urlbuf, sizeof( urlbuf ), "/%s/trackID=%u", rtsp->psz_path,
195 msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
196 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
204 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
205 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
206 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
207 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
208 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
214 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
216 vlc_mutex_lock( &rtsp->lock );
217 for( int i = 0; i < rtsp->sessionc; i++ )
219 rtsp_session_t *ses = rtsp->sessionv[i];
221 for( int j = 0; j < ses->trackc; j++ )
223 if( ses->trackv[j].id == id->sout_id )
225 rtsp_strack_t *tr = ses->trackv + j;
226 sout_AccessOutDelete( tr->access );
227 REMOVE_ELEM( ses->trackv, ses->trackc, j );
228 /* FIXME: are we supposed to notify the client? */
233 vlc_mutex_unlock( &rtsp->lock );
234 httpd_UrlDelete( id->url );
239 /** rtsp must be locked */
241 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
243 rtsp_session_t *s = malloc( sizeof( *s ) );
248 s->id = rand(); /* FIXME: not enough entropy */
252 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
258 /** rtsp must be locked */
260 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
270 id = strtoull( name, &end, 0x10 );
274 /* FIXME: use a hash/dictionary */
275 for( i = 0; i < rtsp->sessionc; i++ )
277 if( rtsp->sessionv[i]->id == id )
278 return rtsp->sessionv[i];
284 /** rtsp must be locked */
286 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
289 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
291 for( i = 0; i < session->trackc; i++ )
293 rtp_del_sink( session->trackv[i].id, session->trackv[i].access );
294 sout_AccessOutDelete( session->trackv[i].access );
297 free( session->trackv );
302 /** Aggregate RTSP callback */
303 static int RtspCallback( httpd_callback_sys_t *p_args,
305 httpd_message_t *answer,
306 const httpd_message_t *query )
308 rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
309 const char *psz_session = NULL, *psz;
311 if( answer == NULL || query == NULL )
316 answer->i_proto = HTTPD_PROTO_RTSP;
317 answer->i_version= query->i_version;
318 answer->i_type = HTTPD_MSG_ANSWER;
320 answer->p_body = NULL;
322 if( httpd_MsgGet( query, "Require" ) != NULL )
324 answer->i_status = 551;
325 httpd_MsgAdd( answer, "Unsupported", "%s",
326 httpd_MsgGet( query, "Require" ) );
329 switch( query->i_type )
331 case HTTPD_MSG_DESCRIBE:
333 char ip[NI_MAXNUMERICHOST], *ptr;
334 char control[sizeof("rtsp://[]:12345/") + sizeof( ip )
335 + strlen( rtsp->psz_path )];
337 /* Build self-referential URL */
338 httpd_ServerIP( cl, ip );
339 ptr = strchr( ip, '%' );
343 if( strchr( ip, ':' ) != NULL )
344 sprintf( control, "rtsp://[%s]:%u/%s", ip, rtsp->port,
345 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
347 sprintf( control, "rtsp://%s:%u/%s", ip, rtsp->port,
348 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
350 ptr = SDPGenerate( rtsp->owner, control );
352 answer->i_status = 200;
353 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
354 httpd_MsgAdd( answer, "Content-Base", "%s", control );
355 answer->p_body = (uint8_t *)ptr;
356 answer->i_body = strlen( ptr );
360 case HTTPD_MSG_SETUP:
361 answer->i_status = 459;
367 answer->i_status = 200;
369 psz_session = httpd_MsgGet( query, "Session" );
370 if( httpd_MsgGet( query, "Range" ) != NULL )
372 answer->i_status = 456; /* cannot seek */
376 vlc_mutex_lock( &rtsp->lock );
377 ses = RtspClientGet( rtsp, psz_session );
380 for( int i = 0; i < ses->trackc; i++ )
382 rtsp_strack_t *tr = ses->trackv + i;
385 tr->playing = VLC_TRUE;
386 rtp_add_sink( tr->id, tr->access );
390 vlc_mutex_unlock( &rtsp->lock );
392 if( httpd_MsgGet( query, "Scale" ) != NULL )
393 httpd_MsgAdd( answer, "Scale", "1." );
397 case HTTPD_MSG_PAUSE:
398 answer->i_status = 405;
399 httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
402 case HTTPD_MSG_TEARDOWN:
406 /* for now only multicast so easy again */
407 answer->i_status = 200;
409 psz_session = httpd_MsgGet( query, "Session" );
411 vlc_mutex_lock( &rtsp->lock );
412 ses = RtspClientGet( rtsp, psz_session );
414 RtspClientDel( rtsp, ses );
415 vlc_mutex_unlock( &rtsp->lock );
423 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
424 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
426 psz = httpd_MsgGet( query, "Cseq" );
428 httpd_MsgAdd( answer, "Cseq", "%s", psz );
429 psz = httpd_MsgGet( query, "Timestamp" );
431 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
433 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
436 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
441 /** Finds the next transport choice */
442 static inline const char *transport_next( const char *str )
444 /* Looks for comma */
445 str = strchr( str, ',' );
447 return NULL; /* No more transport options */
449 str++; /* skips comma */
450 while( strchr( "\r\n\t ", *str ) )
453 return (*str) ? str : NULL;
457 /** Finds the next transport parameter */
458 static inline const char *parameter_next( const char *str )
460 while( strchr( ",;", *str ) == NULL )
463 return (*str == ';') ? (str + 1) : NULL;
467 /** Non-aggregate RTSP callback */
468 static int RtspCallbackId( httpd_callback_sys_t *p_args,
470 httpd_message_t *answer,
471 const httpd_message_t *query )
473 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
474 rtsp_stream_t *rtsp = id->stream;
475 sout_stream_t *p_stream = id->stream->owner;
477 const char *psz_session, *psz;
479 if( answer == NULL || query == NULL )
483 answer->i_proto = HTTPD_PROTO_RTSP;
484 answer->i_version= query->i_version;
485 answer->i_type = HTTPD_MSG_ANSWER;
487 answer->p_body = NULL;
489 psz_session = httpd_MsgGet( query, "Session" );
491 if( httpd_MsgGet( query, "Require" ) != NULL )
493 answer->i_status = 551;
494 httpd_MsgAdd( answer, "Unsupported", "%s",
495 httpd_MsgGet( query, "Require" ) );
498 switch( query->i_type )
500 case HTTPD_MSG_SETUP:
502 answer->i_status = 461;
504 for( const char *tpt = httpd_MsgGet( query, "Transport" );
506 tpt = transport_next( tpt ) )
508 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
509 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
511 /* Check transport protocol. */
512 /* Currently, we only support RTP/AVP over UDP */
513 if( strncmp( tpt, "RTP/AVP", 7 ) )
516 if( strncmp( tpt, "/UDP", 4 ) == 0 )
518 if( strchr( ";,", *tpt ) == NULL )
521 /* Parse transport options */
522 for( const char *opt = parameter_next( tpt );
524 opt = parameter_next( opt ) )
526 if( strncmp( opt, "multicast", 9 ) == 0)
527 b_multicast = VLC_TRUE;
529 if( strncmp( opt, "unicast", 7 ) == 0 )
530 b_multicast = VLC_FALSE;
532 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
535 if( strncmp( opt, "mode=", 5 ) == 0 )
537 if( strncasecmp( opt + 5, "play", 4 )
538 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
546 if( strncmp( opt,"destination=", 12 ) == 0 )
548 answer->i_status = 403;
554 * Every other option is unsupported:
556 * "source" and "append" are invalid (server-only);
557 * "ssrc" also (as clarified per RFC2326bis).
559 * For multicast, "port", "layers", "ttl" are set by the
560 * stream output configuration.
562 * For unicast, we want to decide "server_port" values.
564 * "interleaved" is not implemented.
576 const char *dst = id->dst;
580 answer->i_status = 200;
582 httpd_MsgAdd( answer, "Transport",
583 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
585 dst, id->loport, id->hiport,
586 ( id->ttl > 0 ) ? id->ttl : 1 );
590 char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
591 static const char access[] = "udp{raw,rtcp}";
592 rtsp_session_t *ses = NULL;
593 rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
595 if( httpd_ClientIP( cl, ip ) == NULL )
597 answer->i_status = 500;
601 snprintf( url, sizeof( url ),
602 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
605 track.access = sout_AccessOutNew( p_stream->p_sout,
607 if( track.access == NULL )
610 "cannot create access output for %s://%s",
612 answer->i_status = 500;
616 char *src = var_GetNonEmptyString( track.access, "src-addr" );
617 int sport = var_GetInteger( track.access, "src-port" );
619 vlc_mutex_lock( &rtsp->lock );
620 if( psz_session == NULL )
622 ses = RtspClientNew( rtsp );
623 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
625 psz_session = psz_sesbuf;
629 /* FIXME: we probably need to remove an access out,
630 * if there is already one for the same ID */
631 ses = RtspClientGet( rtsp, psz_session );
634 answer->i_status = 454;
635 vlc_mutex_unlock( &rtsp->lock );
640 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
641 vlc_mutex_unlock( &rtsp->lock );
643 httpd_ServerIP( cl, ip );
645 if( ( src != NULL ) && strcmp( src, ip ) )
647 /* Specify source IP if it is different from the RTSP
648 * control connection server address */
649 char *ptr = strchr( src, '%' );
650 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
652 httpd_MsgAdd( answer, "Transport",
653 "RTP/AVP/UDP;unicast;source=%s;"
654 "client_port=%u-%u;server_port=%u-%u;"
656 src, loport, loport + 1, sport, sport + 1 );
660 httpd_MsgAdd( answer, "Transport",
661 "RTP/AVP/UDP;unicast;"
662 "client_port=%u-%u;server_port=%u-%u;"
664 loport, loport + 1, sport, sport + 1 );
667 answer->i_status = 200;
678 answer->i_status = 200;
680 psz_session = httpd_MsgGet( query, "Session" );
681 if( httpd_MsgGet( query, "Range" ) != NULL )
683 answer->i_status = 456; /* cannot seek */
687 vlc_mutex_lock( &rtsp->lock );
688 ses = RtspClientGet( rtsp, psz_session );
691 for( int i = 0; i < ses->trackc; i++ )
693 rtsp_strack_t *tr = ses->trackv + i;
694 if( !tr->playing && ( tr->id == id->sout_id ) )
696 tr->playing = VLC_TRUE;
697 rtp_add_sink( tr->id, tr->access );
701 vlc_mutex_unlock( &rtsp->lock );
703 if( httpd_MsgGet( query, "Scale" ) != NULL )
704 httpd_MsgAdd( answer, "Scale", "1." );
708 case HTTPD_MSG_PAUSE:
709 answer->i_status = 405;
710 httpd_MsgAdd( answer, "Allow", "SETUP, PLAY, TEARDOWN" );
713 case HTTPD_MSG_TEARDOWN:
717 answer->i_status = 200;
719 psz_session = httpd_MsgGet( query, "Session" );
721 vlc_mutex_lock( &rtsp->lock );
722 ses = RtspClientGet( rtsp, psz_session );
725 for( int i = 0; i < ses->trackc; i++ )
727 if( ses->trackv[i].id == id->sout_id )
729 rtp_del_sink( id->sout_id, ses->trackv[i].access );
730 sout_AccessOutDelete( ses->trackv[i].access );
731 REMOVE_ELEM( ses->trackv, ses->trackc, i );
735 vlc_mutex_unlock( &rtsp->lock );
740 answer->i_status = 460;
744 psz = httpd_MsgGet( query, "Cseq" );
746 httpd_MsgAdd( answer, "Cseq", "%s", psz );
747 psz = httpd_MsgGet( query, "Timestamp" );
749 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
750 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
751 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
752 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
755 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );