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_GETPARAMETER, RtspCallback,
113 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
122 void RtspUnsetup( rtsp_stream_t *rtsp )
124 while( rtsp->sessionc > 0 )
125 RtspClientDel( rtsp, rtsp->sessionv[0] );
128 httpd_UrlDelete( rtsp->url );
131 httpd_HostDelete( rtsp->host );
133 vlc_mutex_destroy( &rtsp->lock );
137 struct rtsp_stream_id_t
139 rtsp_stream_t *stream;
140 sout_stream_id_t *sout_id;
144 unsigned loport, hiport;
148 typedef struct rtsp_strack_t rtsp_strack_t;
150 /* For unicast streaming */
151 struct rtsp_session_t
153 rtsp_stream_t *stream;
156 /* output (id-access) */
158 rtsp_strack_t *trackv;
162 /* Unicast session track */
165 sout_stream_id_t *id;
166 sout_access_out_t *access;
171 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
173 /* Multicast stuff - TODO: cleanup */
174 const char *dst, int ttl,
175 unsigned loport, unsigned hiport )
177 char urlbuf[sizeof( "//trackID=123" ) + strlen( rtsp->psz_path )];
178 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
186 /* TODO: can we assume that this need not be strdup'd? */
188 if( id->dst != NULL )
195 snprintf( urlbuf, sizeof( urlbuf ), "/%s/trackID=%u", rtsp->psz_path,
197 msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
198 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
206 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
207 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
208 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
209 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
210 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
211 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
217 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
219 vlc_mutex_lock( &rtsp->lock );
220 for( int i = 0; i < rtsp->sessionc; i++ )
222 rtsp_session_t *ses = rtsp->sessionv[i];
224 for( int j = 0; j < ses->trackc; j++ )
226 if( ses->trackv[j].id == id->sout_id )
228 rtsp_strack_t *tr = ses->trackv + j;
229 sout_AccessOutDelete( tr->access );
230 REMOVE_ELEM( ses->trackv, ses->trackc, j );
231 /* FIXME: are we supposed to notify the client? */
236 vlc_mutex_unlock( &rtsp->lock );
237 httpd_UrlDelete( id->url );
242 /** rtsp must be locked */
244 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
246 rtsp_session_t *s = malloc( sizeof( *s ) );
251 s->id = rand(); /* FIXME: not enough entropy */
255 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
261 /** rtsp must be locked */
263 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
273 id = strtoull( name, &end, 0x10 );
277 /* FIXME: use a hash/dictionary */
278 for( i = 0; i < rtsp->sessionc; i++ )
280 if( rtsp->sessionv[i]->id == id )
281 return rtsp->sessionv[i];
287 /** rtsp must be locked */
289 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
292 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
294 for( i = 0; i < session->trackc; i++ )
296 rtp_del_sink( session->trackv[i].id, session->trackv[i].access );
297 sout_AccessOutDelete( session->trackv[i].access );
300 free( session->trackv );
305 /** Aggregate RTSP callback */
306 static int RtspCallback( httpd_callback_sys_t *p_args,
308 httpd_message_t *answer,
309 const httpd_message_t *query )
311 rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
312 const char *psz_session = NULL, *psz;
314 if( answer == NULL || query == NULL )
319 answer->i_proto = HTTPD_PROTO_RTSP;
320 answer->i_version= query->i_version;
321 answer->i_type = HTTPD_MSG_ANSWER;
323 answer->p_body = NULL;
325 if( httpd_MsgGet( query, "Require" ) != NULL )
327 answer->i_status = 551;
328 httpd_MsgAdd( answer, "Unsupported", "%s",
329 httpd_MsgGet( query, "Require" ) );
332 switch( query->i_type )
334 case HTTPD_MSG_DESCRIBE:
336 char ip[NI_MAXNUMERICHOST], *ptr;
337 char control[sizeof("rtsp://[]:12345/") + sizeof( ip )
338 + strlen( rtsp->psz_path )];
340 /* Build self-referential URL */
341 httpd_ServerIP( cl, ip );
342 ptr = strchr( ip, '%' );
346 if( strchr( ip, ':' ) != NULL )
347 sprintf( control, "rtsp://[%s]:%u/%s", ip, rtsp->port,
348 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
350 sprintf( control, "rtsp://%s:%u/%s", ip, rtsp->port,
351 ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
353 ptr = SDPGenerate( rtsp->owner, control );
355 answer->i_status = 200;
356 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
357 httpd_MsgAdd( answer, "Content-Base", "%s", control );
358 answer->p_body = (uint8_t *)ptr;
359 answer->i_body = strlen( ptr );
363 case HTTPD_MSG_SETUP:
364 answer->i_status = 459;
370 answer->i_status = 200;
372 psz_session = httpd_MsgGet( query, "Session" );
373 if( httpd_MsgGet( query, "Range" ) != NULL )
375 answer->i_status = 456; /* cannot seek */
379 vlc_mutex_lock( &rtsp->lock );
380 ses = RtspClientGet( rtsp, psz_session );
383 for( int i = 0; i < ses->trackc; i++ )
385 rtsp_strack_t *tr = ses->trackv + i;
388 tr->playing = VLC_TRUE;
389 rtp_add_sink( tr->id, tr->access );
393 vlc_mutex_unlock( &rtsp->lock );
395 if( httpd_MsgGet( query, "Scale" ) != NULL )
396 httpd_MsgAdd( answer, "Scale", "1." );
400 case HTTPD_MSG_PAUSE:
401 answer->i_status = 405;
402 httpd_MsgAdd( answer, "Allow",
403 "DESCRIBE, TEARDOWN, PLAY, GET_PARAMETER" );
406 case HTTPD_MSG_GETPARAMETER:
407 if( query->i_body > 0 )
409 answer->i_status = 451;
413 answer->i_status = 200;
416 case HTTPD_MSG_TEARDOWN:
420 /* for now only multicast so easy again */
421 answer->i_status = 200;
423 psz_session = httpd_MsgGet( query, "Session" );
425 vlc_mutex_lock( &rtsp->lock );
426 ses = RtspClientGet( rtsp, psz_session );
428 RtspClientDel( rtsp, ses );
429 vlc_mutex_unlock( &rtsp->lock );
437 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
438 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
440 psz = httpd_MsgGet( query, "Cseq" );
442 httpd_MsgAdd( answer, "Cseq", "%s", psz );
443 psz = httpd_MsgGet( query, "Timestamp" );
445 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
447 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
450 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
455 /** Finds the next transport choice */
456 static inline const char *transport_next( const char *str )
458 /* Looks for comma */
459 str = strchr( str, ',' );
461 return NULL; /* No more transport options */
463 str++; /* skips comma */
464 while( strchr( "\r\n\t ", *str ) )
467 return (*str) ? str : NULL;
471 /** Finds the next transport parameter */
472 static inline const char *parameter_next( const char *str )
474 while( strchr( ",;", *str ) == NULL )
477 return (*str == ';') ? (str + 1) : NULL;
481 /** Non-aggregate RTSP callback */
482 static int RtspCallbackId( httpd_callback_sys_t *p_args,
484 httpd_message_t *answer,
485 const httpd_message_t *query )
487 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
488 rtsp_stream_t *rtsp = id->stream;
489 sout_stream_t *p_stream = id->stream->owner;
491 const char *psz_session, *psz;
493 if( answer == NULL || query == NULL )
497 answer->i_proto = HTTPD_PROTO_RTSP;
498 answer->i_version= query->i_version;
499 answer->i_type = HTTPD_MSG_ANSWER;
501 answer->p_body = NULL;
503 psz_session = httpd_MsgGet( query, "Session" );
505 if( httpd_MsgGet( query, "Require" ) != NULL )
507 answer->i_status = 551;
508 httpd_MsgAdd( answer, "Unsupported", "%s",
509 httpd_MsgGet( query, "Require" ) );
512 switch( query->i_type )
514 case HTTPD_MSG_SETUP:
516 answer->i_status = 461;
518 for( const char *tpt = httpd_MsgGet( query, "Transport" );
520 tpt = transport_next( tpt ) )
522 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
523 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
525 /* Check transport protocol. */
526 /* Currently, we only support RTP/AVP over UDP */
527 if( strncmp( tpt, "RTP/AVP", 7 ) )
530 if( strncmp( tpt, "/UDP", 4 ) == 0 )
532 if( strchr( ";,", *tpt ) == NULL )
535 /* Parse transport options */
536 for( const char *opt = parameter_next( tpt );
538 opt = parameter_next( opt ) )
540 if( strncmp( opt, "multicast", 9 ) == 0)
541 b_multicast = VLC_TRUE;
543 if( strncmp( opt, "unicast", 7 ) == 0 )
544 b_multicast = VLC_FALSE;
546 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
549 if( strncmp( opt, "mode=", 5 ) == 0 )
551 if( strncasecmp( opt + 5, "play", 4 )
552 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
560 if( strncmp( opt,"destination=", 12 ) == 0 )
562 answer->i_status = 403;
568 * Every other option is unsupported:
570 * "source" and "append" are invalid (server-only);
571 * "ssrc" also (as clarified per RFC2326bis).
573 * For multicast, "port", "layers", "ttl" are set by the
574 * stream output configuration.
576 * For unicast, we want to decide "server_port" values.
578 * "interleaved" is not implemented.
590 const char *dst = id->dst;
594 answer->i_status = 200;
596 httpd_MsgAdd( answer, "Transport",
597 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
599 dst, id->loport, id->hiport,
600 ( id->ttl > 0 ) ? id->ttl : 1 );
604 char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
605 static const char access[] = "udp{raw,rtcp}";
606 rtsp_session_t *ses = NULL;
607 rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
609 if( httpd_ClientIP( cl, ip ) == NULL )
611 answer->i_status = 500;
615 snprintf( url, sizeof( url ),
616 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
619 track.access = sout_AccessOutNew( p_stream->p_sout,
621 if( track.access == NULL )
624 "cannot create access output for %s://%s",
626 answer->i_status = 500;
630 char *src = var_GetNonEmptyString( track.access, "src-addr" );
631 int sport = var_GetInteger( track.access, "src-port" );
633 vlc_mutex_lock( &rtsp->lock );
634 if( psz_session == NULL )
636 ses = RtspClientNew( rtsp );
637 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
639 psz_session = psz_sesbuf;
643 /* FIXME: we probably need to remove an access out,
644 * if there is already one for the same ID */
645 ses = RtspClientGet( rtsp, psz_session );
648 answer->i_status = 454;
649 vlc_mutex_unlock( &rtsp->lock );
654 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
655 vlc_mutex_unlock( &rtsp->lock );
657 httpd_ServerIP( cl, ip );
659 if( ( src != NULL ) && strcmp( src, ip ) )
661 /* Specify source IP if it is different from the RTSP
662 * control connection server address */
663 char *ptr = strchr( src, '%' );
664 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
666 httpd_MsgAdd( answer, "Transport",
667 "RTP/AVP/UDP;unicast;source=%s;"
668 "client_port=%u-%u;server_port=%u-%u;"
670 src, loport, loport + 1, sport, sport + 1 );
674 httpd_MsgAdd( answer, "Transport",
675 "RTP/AVP/UDP;unicast;"
676 "client_port=%u-%u;server_port=%u-%u;"
678 loport, loport + 1, sport, sport + 1 );
681 answer->i_status = 200;
692 answer->i_status = 200;
694 psz_session = httpd_MsgGet( query, "Session" );
695 if( httpd_MsgGet( query, "Range" ) != NULL )
697 answer->i_status = 456; /* cannot seek */
701 vlc_mutex_lock( &rtsp->lock );
702 ses = RtspClientGet( rtsp, psz_session );
705 for( int i = 0; i < ses->trackc; i++ )
707 rtsp_strack_t *tr = ses->trackv + i;
708 if( !tr->playing && ( tr->id == id->sout_id ) )
710 tr->playing = VLC_TRUE;
711 rtp_add_sink( tr->id, tr->access );
715 vlc_mutex_unlock( &rtsp->lock );
717 if( httpd_MsgGet( query, "Scale" ) != NULL )
718 httpd_MsgAdd( answer, "Scale", "1." );
722 case HTTPD_MSG_PAUSE:
723 answer->i_status = 405;
724 httpd_MsgAdd( answer, "Allow",
725 "SETUP, TEARDOWN, PLAY, GET_PARAMETER" );
728 case HTTPD_MSG_GETPARAMETER:
729 if( query->i_body > 0 )
731 answer->i_status = 451;
735 answer->i_status = 200;
738 case HTTPD_MSG_TEARDOWN:
742 answer->i_status = 200;
744 psz_session = httpd_MsgGet( query, "Session" );
746 vlc_mutex_lock( &rtsp->lock );
747 ses = RtspClientGet( rtsp, psz_session );
750 for( int i = 0; i < ses->trackc; i++ )
752 if( ses->trackv[i].id == id->sout_id )
754 rtp_del_sink( id->sout_id, ses->trackv[i].access );
755 sout_AccessOutDelete( ses->trackv[i].access );
756 REMOVE_ELEM( ses->trackv, ses->trackc, i );
760 vlc_mutex_unlock( &rtsp->lock );
765 answer->i_status = 460;
769 psz = httpd_MsgGet( query, "Cseq" );
771 httpd_MsgAdd( answer, "Cseq", "%s", psz );
772 psz = httpd_MsgGet( query, "Timestamp" );
774 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
775 httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
776 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
777 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
780 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );