1 /*****************************************************************************
2 * rtsp.c: RTSP support for RTP stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2010 the VideoLAN team
5 * Copyright © 2007 Rémi Denis-Courmont
9 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
37 #include <vlc_httpd.h>
39 #include <vlc_charset.h>
41 #include <vlc_network.h>
56 typedef struct rtsp_session_t rtsp_session_t;
62 vod_media_t *vod_media;
70 rtsp_session_t **sessionv;
77 static int RtspCallback( httpd_callback_sys_t *p_args,
78 httpd_client_t *cl, httpd_message_t *answer,
79 const httpd_message_t *query );
80 static int RtspCallbackId( httpd_callback_sys_t *p_args,
81 httpd_client_t *cl, httpd_message_t *answer,
82 const httpd_message_t *query );
83 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
85 static void RtspTimeOut( void *data );
87 rtsp_stream_t *RtspSetup( vlc_object_t *owner, vod_media_t *media,
88 const vlc_url_t *url )
90 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
92 if( rtsp == NULL || ( url->i_port > 99999 ) )
99 rtsp->vod_media = media;
101 rtsp->sessionv = NULL;
104 rtsp->psz_path = NULL;
106 vlc_mutex_init( &rtsp->lock );
108 rtsp->timeout = var_InheritInteger(owner, "rtsp-timeout");
109 if (rtsp->timeout > 0)
111 if (vlc_timer_create(&rtsp->timer, RtspTimeOut, rtsp))
115 rtsp->port = (url->i_port > 0) ? url->i_port : 554;
116 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
117 if( rtsp->psz_path == NULL )
120 msg_Dbg( owner, "RTSP stream: host %s port %d at %s",
121 url->psz_host, rtsp->port, rtsp->psz_path );
123 rtsp->host = httpd_HostNew( VLC_OBJECT(owner), url->psz_host,
125 if( rtsp->host == NULL )
128 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
130 if( rtsp->url == NULL )
133 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
134 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
135 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
136 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
137 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
139 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
148 void RtspUnsetup( rtsp_stream_t *rtsp )
151 httpd_UrlDelete( rtsp->url );
154 httpd_HostDelete( rtsp->host );
156 while( rtsp->sessionc > 0 )
157 RtspClientDel( rtsp, rtsp->sessionv[0] );
159 if (rtsp->timeout > 0)
160 vlc_timer_destroy(rtsp->timer);
162 free( rtsp->psz_path );
163 vlc_mutex_destroy( &rtsp->lock );
169 struct rtsp_stream_id_t
171 rtsp_stream_t *stream;
172 sout_stream_id_t *sout_id;
176 unsigned clock_rate; /* needed to compute rtptime in RTP-Info */
181 typedef struct rtsp_strack_t rtsp_strack_t;
183 /* For unicast streaming */
184 struct rtsp_session_t
186 rtsp_stream_t *stream;
188 mtime_t last_seen; /* for timeouts */
190 /* output (id-access) */
192 rtsp_strack_t *trackv;
196 /* Unicast session track */
199 rtsp_stream_id_t *id;
200 sout_stream_id_t *sout_id;
201 int setup_fd; /* socket created by the SETUP request */
202 int rtp_fd; /* socket used by the RTP output, when playing */
207 static void RtspTrackClose( rtsp_strack_t *tr );
209 #define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
211 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
213 const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
217 if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
223 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
224 uint32_t ssrc, unsigned clock_rate,
227 if (rtsp->track_id > 999)
229 msg_Err(rtsp->owner, "RTSP: too many IDs!");
234 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
242 id->track_id = rtsp->track_id;
244 id->clock_rate = clock_rate;
245 id->mcast_fd = mcast_fd;
247 urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
254 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
255 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
264 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
265 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
266 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
267 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
268 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
269 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
277 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
279 httpd_UrlDelete( id->url );
281 vlc_mutex_lock( &rtsp->lock );
282 for( int i = 0; i < rtsp->sessionc; i++ )
284 rtsp_session_t *ses = rtsp->sessionv[i];
286 for( int j = 0; j < ses->trackc; j++ )
288 if( ses->trackv[j].id == id )
290 rtsp_strack_t *tr = ses->trackv + j;
291 RtspTrackClose( tr );
292 REMOVE_ELEM( ses->trackv, ses->trackc, j );
297 vlc_mutex_unlock( &rtsp->lock );
302 /** rtsp must be locked */
303 static void RtspUpdateTimer( rtsp_stream_t *rtsp )
305 if (rtsp->timeout <= 0)
309 for (int i = 0; i < rtsp->sessionc; i++)
311 if (timeout == 0 || rtsp->sessionv[i]->last_seen < timeout)
312 timeout = rtsp->sessionv[i]->last_seen;
315 timeout += rtsp->timeout * CLOCK_FREQ;
316 vlc_timer_schedule(rtsp->timer, true, timeout, 0);
320 static void RtspTimeOut( void *data )
322 rtsp_stream_t *rtsp = data;
324 vlc_mutex_lock(&rtsp->lock);
325 mtime_t now = mdate();
326 for (int i = rtsp->sessionc - 1; i >= 0; i--)
328 if (rtsp->sessionv[i]->last_seen + rtsp->timeout * CLOCK_FREQ < now)
330 if (rtsp->vod_media != NULL)
333 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
334 rtsp->sessionv[i]->id );
335 vod_stop(rtsp->vod_media, psz_sesbuf);
337 RtspClientDel(rtsp, rtsp->sessionv[i]);
340 RtspUpdateTimer(rtsp);
341 vlc_mutex_unlock(&rtsp->lock);
345 /** rtsp must be locked */
347 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
349 rtsp_session_t *s = malloc( sizeof( *s ) );
354 vlc_rand_bytes (&s->id, sizeof (s->id));
358 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
364 /** rtsp must be locked */
366 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
376 id = strtoull( name, &end, 0x10 );
380 /* FIXME: use a hash/dictionary */
381 for( i = 0; i < rtsp->sessionc; i++ )
383 if( rtsp->sessionv[i]->id == id )
384 return rtsp->sessionv[i];
390 /** rtsp must be locked */
392 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
395 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
397 for( i = 0; i < session->trackc; i++ )
398 RtspTrackClose( &session->trackv[i] );
400 free( session->trackv );
405 /** rtsp must be locked */
406 static void RtspClientAlive( rtsp_session_t *session )
408 if (session->stream->timeout <= 0)
411 session->last_seen = mdate();
412 RtspUpdateTimer(session->stream);
415 static int dup_socket(int oldfd)
418 #if !defined(WIN32) || defined(UNDER_CE)
419 newfd = vlc_dup(oldfd);
421 WSAPROTOCOL_INFO info;
422 WSADuplicateSocket (oldfd, GetCurrentProcessId (), &info);
423 newfd = WSASocket (info.iAddressFamily, info.iSocketType,
424 info.iProtocol, &info, 0, 0);
429 /* Attach a starting VoD RTP id to its RTSP track, and let it
430 * initialize with the parameters of the SETUP request */
431 int RtspTrackAttach( rtsp_stream_t *rtsp, const char *name,
432 rtsp_stream_id_t *id, sout_stream_id_t *sout_id,
433 uint32_t *ssrc, uint16_t *seq_init )
435 int val = VLC_EGENERIC;
436 rtsp_session_t *session;
438 vlc_mutex_lock(&rtsp->lock);
439 session = RtspClientGet(rtsp, name);
444 rtsp_strack_t *tr = NULL;
445 for (int i = 0; session->trackc; i++)
447 if (session->trackv[i].id == id)
449 tr = session->trackv + i;
456 tr->sout_id = sout_id;
457 tr->rtp_fd = dup_socket(tr->setup_fd);
461 /* The track was not SETUP. We still create one because we'll
462 * need the sout_id if we set it up later. */
463 rtsp_strack_t track = { .id = id, .sout_id = sout_id,
464 .setup_fd = -1, .rtp_fd = -1 };
465 vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
466 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
468 INSERT_ELEM(session->trackv, session->trackc, session->trackc, track);
471 *ssrc = ntohl(tr->ssrc);
472 *seq_init = tr->seq_init;
474 if (tr->rtp_fd != -1)
477 rtp_add_sink(tr->sout_id, tr->rtp_fd, false, &seq);
478 /* To avoid race conditions, sout_id->i_seq_sent_next must
479 * be set here and now. Make sure the caller did its job
480 * properly when passing seq_init. */
481 assert(tr->seq_init == seq);
486 vlc_mutex_unlock(&rtsp->lock);
491 /* Remove references to the RTP id when it is stopped */
492 void RtspTrackDetach( rtsp_stream_t *rtsp, const char *name,
493 sout_stream_id_t *sout_id )
495 rtsp_session_t *session;
497 vlc_mutex_lock(&rtsp->lock);
498 session = RtspClientGet(rtsp, name);
503 for (int i = 0; session->trackc; i++)
505 rtsp_strack_t *tr = session->trackv + i;
506 if (tr->sout_id == sout_id)
508 if (tr->setup_fd == -1)
510 /* No (more) SETUP information: better get rid of the
511 * track so that we can have new random ssrc and
512 * seq_init next time. */
513 REMOVE_ELEM( session->trackv, session->trackc, i );
516 /* We keep the SETUP information of the track, but stop it */
517 if (tr->rtp_fd != -1)
519 rtp_del_sink(tr->sout_id, tr->rtp_fd);
528 vlc_mutex_unlock(&rtsp->lock);
532 /** rtsp must be locked */
533 static void RtspTrackClose( rtsp_strack_t *tr )
535 if (tr->setup_fd != -1)
537 if (tr->rtp_fd != -1)
539 rtp_del_sink(tr->sout_id, tr->rtp_fd);
542 net_Close(tr->setup_fd);
548 /** Finds the next transport choice */
549 static inline const char *transport_next( const char *str )
551 /* Looks for comma */
552 str = strchr( str, ',' );
554 return NULL; /* No more transport options */
556 str++; /* skips comma */
557 while( strchr( "\r\n\t ", *str ) )
560 return (*str) ? str : NULL;
564 /** Finds the next transport parameter */
565 static inline const char *parameter_next( const char *str )
567 while( strchr( ",;", *str ) == NULL )
570 return (*str == ';') ? (str + 1) : NULL;
574 static int64_t ParseNPT (const char *str)
576 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
577 locale_t oldloc = uselocale (loc);
581 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
582 sec += ((hour * 60) + min) * 60;
584 if (sscanf (str, "%f", &sec) != 1)
587 if (loc != (locale_t)0)
592 return sec < 0 ? -1 : sec * CLOCK_FREQ;
596 /** RTSP requests handler
597 * @param id selected track for non-aggregate URLs,
598 * NULL for aggregate URLs
600 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
602 httpd_message_t *answer,
603 const httpd_message_t *query )
605 vlc_object_t *owner = rtsp->owner;
607 const char *psz_session = NULL, *psz;
608 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
609 + strlen( rtsp->psz_path )];
610 bool vod = rtsp->vod_media != NULL;
615 if( answer == NULL || query == NULL || cl == NULL )
619 /* Build self-referential control URL */
620 char ip[NI_MAXNUMERICHOST], *ptr;
622 httpd_ServerIP( cl, ip );
623 ptr = strchr( ip, '%' );
627 if( strchr( ip, ':' ) != NULL )
628 sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
631 sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
636 answer->i_proto = HTTPD_PROTO_RTSP;
637 answer->i_version= 0;
638 answer->i_type = HTTPD_MSG_ANSWER;
640 answer->p_body = NULL;
642 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
644 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
646 if (gmtime_r (&now, &ut) != NULL)
647 { /* RFC1123 format, GMT is mandatory */
648 static const char wdays[7][4] = {
649 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
650 static const char mons[12][4] = {
651 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
652 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
653 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
654 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
655 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
658 if( query->i_proto != HTTPD_PROTO_RTSP )
660 answer->i_status = 505;
663 if( httpd_MsgGet( query, "Require" ) != NULL )
665 answer->i_status = 551;
666 httpd_MsgAdd( answer, "Unsupported", "%s",
667 httpd_MsgGet( query, "Require" ) );
670 switch( query->i_type )
672 case HTTPD_MSG_DESCRIBE:
673 { /* Aggregate-only */
676 answer->i_status = 460;
680 answer->i_status = 200;
681 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
682 httpd_MsgAdd( answer, "Content-Base", "%s", control );
684 answer->p_body = (uint8_t *) ( vod ?
685 SDPGenerateVoD( rtsp->vod_media, control ) :
686 SDPGenerate( (sout_stream_t *)owner, control ) );
687 if( answer->p_body != NULL )
688 answer->i_body = strlen( (char *)answer->p_body );
690 answer->i_status = 500;
694 case HTTPD_MSG_SETUP:
695 /* Non-aggregate-only */
698 answer->i_status = 459;
702 psz_session = httpd_MsgGet( query, "Session" );
703 answer->i_status = 461;
705 for( const char *tpt = httpd_MsgGet( query, "Transport" );
707 tpt = transport_next( tpt ) )
709 bool b_multicast = true, b_unsupp = false;
710 unsigned loport = 5004, hiport; /* from RFC3551 */
712 /* Check transport protocol. */
713 /* Currently, we only support RTP/AVP over UDP */
714 if( strncmp( tpt, "RTP/AVP", 7 ) )
717 if( strncmp( tpt, "/UDP", 4 ) == 0 )
719 if( strchr( ";,", *tpt ) == NULL )
722 /* Parse transport options */
723 for( const char *opt = parameter_next( tpt );
725 opt = parameter_next( opt ) )
727 if( strncmp( opt, "multicast", 9 ) == 0)
730 if( strncmp( opt, "unicast", 7 ) == 0 )
733 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
737 if( strncmp( opt, "mode=", 5 ) == 0 )
739 if( strncasecmp( opt + 5, "play", 4 )
740 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
748 if( strncmp( opt,"destination=", 12 ) == 0 )
750 answer->i_status = 403;
756 * Every other option is unsupported:
758 * "source" and "append" are invalid (server-only);
759 * "ssrc" also (as clarified per RFC2326bis).
761 * For multicast, "port", "layers", "ttl" are set by the
762 * stream output configuration.
764 * For unicast, we want to decide "server_port" values.
766 * "interleaved" is not implemented.
778 char dst[NI_MAXNUMERICHOST];
780 if( id->mcast_fd == -1 )
783 net_GetPeerAddress(id->mcast_fd, dst, &dport);
785 ttl = var_InheritInteger(owner, "ttl");
787 /* FIXME: the TTL is left to the OS default, we can
788 * only guess that it's 1. */
791 if( psz_session == NULL )
793 /* Create a dummy session ID */
794 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%lu",
796 psz_session = psz_sesbuf;
798 answer->i_status = 200;
800 httpd_MsgAdd( answer, "Transport",
801 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
803 dst, dport, dport + 1, ttl );
804 /* FIXME: this doesn't work with RTP + RTCP mux */
808 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
809 rtsp_session_t *ses = NULL;
813 if( httpd_ClientIP( cl, ip ) == NULL )
815 answer->i_status = 500;
819 fd = net_ConnectDgram( owner, ip, loport, -1,
824 "cannot create RTP socket for %s port %u",
826 answer->i_status = 500;
830 /* Ignore any unexpected incoming packet */
831 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
833 net_GetSockAddress( fd, src, &sport );
835 vlc_mutex_lock( &rtsp->lock );
836 if( psz_session == NULL )
838 ses = RtspClientNew( rtsp );
839 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
841 psz_session = psz_sesbuf;
845 ses = RtspClientGet( rtsp, psz_session );
848 answer->i_status = 454;
849 vlc_mutex_unlock( &rtsp->lock );
854 RtspClientAlive(ses);
856 rtsp_strack_t *tr = NULL;
857 for (int i = 0; i < ses->trackc; i++)
859 if (ses->trackv[i].id == id)
861 tr = ses->trackv + i;
868 /* Set up a new track */
869 rtsp_strack_t track = { .id = id,
870 .sout_id = id->sout_id,
876 vlc_rand_bytes (&track.seq_init,
877 sizeof (track.seq_init));
878 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
884 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
887 else if (tr->setup_fd == -1)
889 /* The track was not SETUP, but it exists
890 * because there is a sout_id running for it */
896 /* The track is already set up, and we don't
897 * support changing the transport parameters on
899 vlc_mutex_unlock( &rtsp->lock );
900 answer->i_status = 455;
904 vlc_mutex_unlock( &rtsp->lock );
906 httpd_ServerIP( cl, ip );
908 /* Specify source IP only if it is different from the
909 * RTSP control connection server address */
910 if( strcmp( src, ip ) )
912 char *ptr = strchr( src, '%' );
913 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
918 httpd_MsgAdd( answer, "Transport",
919 "RTP/AVP/UDP;unicast%s%s;"
920 "client_port=%u-%u;server_port=%u-%u;"
921 "ssrc=%08X;mode=play",
922 src[0] ? ";source=" : "", src,
923 loport, loport + 1, sport, sport + 1, ssrc );
925 answer->i_status = 200;
934 answer->i_status = 200;
936 psz_session = httpd_MsgGet( query, "Session" );
937 int64_t start = -1, end = -1;
938 const char *range = httpd_MsgGet (query, "Range");
941 if (strncmp (range, "npt=", 4))
943 answer->i_status = 501;
947 start = ParseNPT (range + 4);
948 range = strchr(range, '-');
949 if (range != NULL && *(range + 1))
950 end = ParseNPT (range + 1);
952 if (end >= 0 && end < start)
954 answer->i_status = 457;
960 if (vod_check_range(rtsp->vod_media, psz_session,
961 start, end) != VLC_SUCCESS)
963 answer->i_status = 457;
967 /* We accept start times of 0 even for broadcast streams
968 * that already started */
969 else if (start > 0 || end >= 0)
971 answer->i_status = 456;
975 vlc_mutex_lock( &rtsp->lock );
976 ses = RtspClientGet( rtsp, psz_session );
979 char info[ses->trackc * ( strlen( control ) + TRACK_PATH_SIZE
980 + sizeof("url=;seq=65535;rtptime=4294967295, ")
983 RtspClientAlive(ses);
985 sout_stream_id_t *sout_id = NULL;
988 /* We don't keep a reference to the sout_stream_t,
989 * so we check if a sout_id is available instead. */
990 for (int i = 0; i < ses->trackc; i++)
992 sout_id = ses->trackv[i].sout_id;
997 int64_t ts = rtp_get_ts(vod ? NULL : (sout_stream_t *)owner,
998 sout_id, rtsp->vod_media, psz_session);
1000 for( int i = 0; i < ses->trackc; i++ )
1002 rtsp_strack_t *tr = ses->trackv + i;
1003 if( ( id == NULL ) || ( tr->id == id ) )
1005 if (tr->setup_fd == -1)
1006 /* Track not SETUP */
1010 if( tr->rtp_fd == -1 )
1012 /* Track not PLAYing yet */
1013 if (tr->sout_id == NULL)
1014 /* Instance not running yet (VoD) */
1018 /* Instance running, add a sink to it */
1019 tr->rtp_fd = dup_socket(tr->setup_fd);
1020 if (tr->rtp_fd == -1)
1023 rtp_add_sink( tr->sout_id, tr->rtp_fd,
1029 /* Track already playing */
1030 assert( tr->sout_id != NULL );
1031 seq = rtp_get_seq( tr->sout_id );
1033 char *url = RtspAppendTrackPath( tr->id, control );
1034 infolen += sprintf( info + infolen,
1035 "url=%s;seq=%u;rtptime=%u, ",
1036 url != NULL ? url : "", seq,
1037 rtp_compute_ts( tr->id->clock_rate, ts ) );
1043 info[infolen - 2] = '\0'; /* remove trailing ", " */
1044 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
1048 bool running = (sout_id != NULL);
1049 vod_play(rtsp->vod_media, psz_session, start, end, running);
1052 vlc_mutex_unlock( &rtsp->lock );
1054 if( httpd_MsgGet( query, "Scale" ) != NULL )
1055 httpd_MsgAdd( answer, "Scale", "1." );
1059 case HTTPD_MSG_PAUSE:
1061 if (id == NULL && !vod)
1063 answer->i_status = 405;
1064 httpd_MsgAdd( answer, "Allow",
1065 "%s, TEARDOWN, PLAY, GET_PARAMETER",
1066 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
1070 rtsp_session_t *ses;
1071 answer->i_status = 200;
1072 psz_session = httpd_MsgGet( query, "Session" );
1073 vlc_mutex_lock( &rtsp->lock );
1074 ses = RtspClientGet( rtsp, psz_session );
1080 vod_pause(rtsp->vod_media, psz_session);
1082 else /* "Mute" the selected track */
1085 for (int i = 0; i < ses->trackc; i++)
1087 rtsp_strack_t *tr = ses->trackv + i;;
1090 if (tr->setup_fd == -1)
1094 if (tr->rtp_fd != -1)
1096 rtp_del_sink(tr->sout_id, tr->rtp_fd);
1103 answer->i_status = 455;
1105 RtspClientAlive(ses);
1107 vlc_mutex_unlock( &rtsp->lock );
1111 case HTTPD_MSG_GETPARAMETER:
1112 if( query->i_body > 0 )
1114 answer->i_status = 451;
1118 psz_session = httpd_MsgGet( query, "Session" );
1119 answer->i_status = 200;
1120 vlc_mutex_lock( &rtsp->lock );
1121 rtsp_session_t *ses = RtspClientGet( rtsp, psz_session );
1123 RtspClientAlive(ses);
1124 vlc_mutex_unlock( &rtsp->lock );
1127 case HTTPD_MSG_TEARDOWN:
1129 rtsp_session_t *ses;
1131 answer->i_status = 200;
1133 psz_session = httpd_MsgGet( query, "Session" );
1135 vlc_mutex_lock( &rtsp->lock );
1136 ses = RtspClientGet( rtsp, psz_session );
1139 if( id == NULL ) /* Delete the entire session */
1141 RtspClientDel( rtsp, ses );
1143 vod_stop(rtsp->vod_media, psz_session);
1144 RtspUpdateTimer(rtsp);
1146 else /* Delete one track from the session */
1148 for( int i = 0; i < ses->trackc; i++ )
1150 if( ses->trackv[i].id == id )
1152 RtspTrackClose( &ses->trackv[i] );
1153 /* Keep VoD tracks whose instance is still
1155 if (!(vod && ses->trackv[i].sout_id != NULL))
1156 REMOVE_ELEM( ses->trackv, ses->trackc, i );
1159 RtspClientAlive(ses);
1162 vlc_mutex_unlock( &rtsp->lock );
1167 return VLC_EGENERIC;
1172 if (rtsp->timeout > 0)
1173 httpd_MsgAdd( answer, "Session", "%s;timeout=%d", psz_session,
1176 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1179 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1180 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
1182 psz = httpd_MsgGet( query, "Cseq" );
1184 httpd_MsgAdd( answer, "Cseq", "%s", psz );
1185 psz = httpd_MsgGet( query, "Timestamp" );
1187 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
1193 /** Aggregate RTSP callback */
1194 static int RtspCallback( httpd_callback_sys_t *p_args,
1196 httpd_message_t *answer,
1197 const httpd_message_t *query )
1199 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
1203 /** Non-aggregate RTSP callback */
1204 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1206 httpd_message_t *answer,
1207 const httpd_message_t *query )
1209 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
1210 return RtspHandler( id->stream, id, cl, answer, query );