1 /*****************************************************************************
2 * rtsp.c: RTSP support for RTP stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2010 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
57 typedef struct rtsp_session_t rtsp_session_t;
63 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,
90 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
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->psz_path = strdup( (path != NULL) ? path : "/" );
116 if( rtsp->psz_path == NULL )
119 msg_Dbg( owner, "RTSP stream at %s", rtsp->psz_path );
121 rtsp->host = vlc_rtsp_HostNew( VLC_OBJECT(owner) );
122 if( rtsp->host == NULL )
125 char *user = var_InheritString(owner, "sout-rtsp-user");
126 char *pwd = var_InheritString(owner, "sout-rtsp-pwd");
128 rtsp->url = httpd_UrlNew( rtsp->host, rtsp->psz_path, user, pwd );
131 if( rtsp->url == NULL )
134 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
135 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
136 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
137 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
138 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
140 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
149 void RtspUnsetup( rtsp_stream_t *rtsp )
152 httpd_UrlDelete( rtsp->url );
155 httpd_HostDelete( rtsp->host );
157 while( rtsp->sessionc > 0 )
158 RtspClientDel( rtsp, rtsp->sessionv[0] );
160 if (rtsp->timeout > 0)
161 vlc_timer_destroy(rtsp->timer);
163 free( rtsp->psz_path );
164 vlc_mutex_destroy( &rtsp->lock );
170 struct rtsp_stream_id_t
172 rtsp_stream_t *stream;
173 sout_stream_id_t *sout_id;
177 unsigned clock_rate; /* needed to compute rtptime in RTP-Info */
182 typedef struct rtsp_strack_t rtsp_strack_t;
184 /* For unicast streaming */
185 struct rtsp_session_t
187 rtsp_stream_t *stream;
189 mtime_t last_seen; /* for timeouts */
191 /* output (id-access) */
193 rtsp_strack_t *trackv;
197 /* Unicast session track */
200 rtsp_stream_id_t *id;
201 sout_stream_id_t *sout_id;
202 int setup_fd; /* socket created by the SETUP request */
203 int rtp_fd; /* socket used by the RTP output, when playing */
208 static void RtspTrackClose( rtsp_strack_t *tr );
210 #define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
212 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
214 const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
218 if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
224 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
225 uint32_t ssrc, unsigned clock_rate,
228 if (rtsp->track_id > 999)
230 msg_Err(rtsp->owner, "RTSP: too many IDs!");
235 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
243 id->track_id = rtsp->track_id;
245 id->clock_rate = clock_rate;
246 id->mcast_fd = mcast_fd;
248 urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
255 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
257 char *user = var_InheritString(rtsp->owner, "sout-rtsp-user");
258 char *pwd = var_InheritString(rtsp->owner, "sout-rtsp-pwd");
260 url = id->url = httpd_UrlNew( rtsp->host, urlbuf, user, pwd );
271 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
272 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
273 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
274 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
275 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
276 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
284 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
286 httpd_UrlDelete( id->url );
288 vlc_mutex_lock( &rtsp->lock );
289 for( int i = 0; i < rtsp->sessionc; i++ )
291 rtsp_session_t *ses = rtsp->sessionv[i];
293 for( int j = 0; j < ses->trackc; j++ )
295 if( ses->trackv[j].id == id )
297 rtsp_strack_t *tr = ses->trackv + j;
298 RtspTrackClose( tr );
299 REMOVE_ELEM( ses->trackv, ses->trackc, j );
304 vlc_mutex_unlock( &rtsp->lock );
309 /** rtsp must be locked */
310 static void RtspUpdateTimer( rtsp_stream_t *rtsp )
312 if (rtsp->timeout <= 0)
316 for (int i = 0; i < rtsp->sessionc; i++)
318 if (timeout == 0 || rtsp->sessionv[i]->last_seen < timeout)
319 timeout = rtsp->sessionv[i]->last_seen;
322 timeout += rtsp->timeout * CLOCK_FREQ;
323 vlc_timer_schedule(rtsp->timer, true, timeout, 0);
327 static void RtspTimeOut( void *data )
329 rtsp_stream_t *rtsp = data;
331 vlc_mutex_lock(&rtsp->lock);
332 mtime_t now = mdate();
333 for (int i = rtsp->sessionc - 1; i >= 0; i--)
335 if (rtsp->sessionv[i]->last_seen + rtsp->timeout * CLOCK_FREQ < now)
337 if (rtsp->vod_media != NULL)
340 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
341 rtsp->sessionv[i]->id );
342 vod_stop(rtsp->vod_media, psz_sesbuf);
344 RtspClientDel(rtsp, rtsp->sessionv[i]);
347 RtspUpdateTimer(rtsp);
348 vlc_mutex_unlock(&rtsp->lock);
352 /** rtsp must be locked */
354 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
356 rtsp_session_t *s = malloc( sizeof( *s ) );
361 vlc_rand_bytes (&s->id, sizeof (s->id));
365 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
371 /** rtsp must be locked */
373 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
383 id = strtoull( name, &end, 0x10 );
387 /* FIXME: use a hash/dictionary */
388 for( i = 0; i < rtsp->sessionc; i++ )
390 if( rtsp->sessionv[i]->id == id )
391 return rtsp->sessionv[i];
397 /** rtsp must be locked */
399 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
402 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
404 for( i = 0; i < session->trackc; i++ )
405 RtspTrackClose( &session->trackv[i] );
407 free( session->trackv );
412 /** rtsp must be locked */
413 static void RtspClientAlive( rtsp_session_t *session )
415 if (session->stream->timeout <= 0)
418 session->last_seen = mdate();
419 RtspUpdateTimer(session->stream);
422 static int dup_socket(int oldfd)
426 newfd = vlc_dup(oldfd);
428 WSAPROTOCOL_INFO info;
429 WSADuplicateSocket (oldfd, GetCurrentProcessId (), &info);
430 newfd = WSASocket (info.iAddressFamily, info.iSocketType,
431 info.iProtocol, &info, 0, 0);
436 /* Attach a starting VoD RTP id to its RTSP track, and let it
437 * initialize with the parameters of the SETUP request */
438 int RtspTrackAttach( rtsp_stream_t *rtsp, const char *name,
439 rtsp_stream_id_t *id, sout_stream_id_t *sout_id,
440 uint32_t *ssrc, uint16_t *seq_init )
442 int val = VLC_EGENERIC;
443 rtsp_session_t *session;
445 vlc_mutex_lock(&rtsp->lock);
446 session = RtspClientGet(rtsp, name);
451 rtsp_strack_t *tr = NULL;
452 for (int i = 0; i < session->trackc; i++)
454 if (session->trackv[i].id == id)
456 tr = session->trackv + i;
463 tr->sout_id = sout_id;
464 tr->rtp_fd = dup_socket(tr->setup_fd);
468 /* The track was not SETUP. We still create one because we'll
469 * need the sout_id if we set it up later. */
470 rtsp_strack_t track = { .id = id, .sout_id = sout_id,
471 .setup_fd = -1, .rtp_fd = -1 };
472 vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
473 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
475 INSERT_ELEM(session->trackv, session->trackc, session->trackc, track);
476 tr = session->trackv + session->trackc - 1;
479 *ssrc = ntohl(tr->ssrc);
480 *seq_init = tr->seq_init;
482 if (tr->rtp_fd != -1)
485 rtp_add_sink(tr->sout_id, tr->rtp_fd, false, &seq);
486 /* To avoid race conditions, sout_id->i_seq_sent_next must
487 * be set here and now. Make sure the caller did its job
488 * properly when passing seq_init. */
489 assert(tr->seq_init == seq);
494 vlc_mutex_unlock(&rtsp->lock);
499 /* Remove references to the RTP id when it is stopped */
500 void RtspTrackDetach( rtsp_stream_t *rtsp, const char *name,
501 sout_stream_id_t *sout_id )
503 rtsp_session_t *session;
505 vlc_mutex_lock(&rtsp->lock);
506 session = RtspClientGet(rtsp, name);
511 for (int i = 0; i < session->trackc; i++)
513 rtsp_strack_t *tr = session->trackv + i;
514 if (tr->sout_id == sout_id)
516 if (tr->setup_fd == -1)
518 /* No (more) SETUP information: better get rid of the
519 * track so that we can have new random ssrc and
520 * seq_init next time. */
521 REMOVE_ELEM( session->trackv, session->trackc, i );
524 /* We keep the SETUP information of the track, but stop it */
525 if (tr->rtp_fd != -1)
527 rtp_del_sink(tr->sout_id, tr->rtp_fd);
536 vlc_mutex_unlock(&rtsp->lock);
540 /** rtsp must be locked */
541 static void RtspTrackClose( rtsp_strack_t *tr )
543 if (tr->setup_fd != -1)
545 if (tr->rtp_fd != -1)
547 rtp_del_sink(tr->sout_id, tr->rtp_fd);
550 net_Close(tr->setup_fd);
556 /** Finds the next transport choice */
557 static inline const char *transport_next( const char *str )
559 /* Looks for comma */
560 str = strchr( str, ',' );
562 return NULL; /* No more transport options */
564 str++; /* skips comma */
565 while( strchr( "\r\n\t ", *str ) )
568 return (*str) ? str : NULL;
572 /** Finds the next transport parameter */
573 static inline const char *parameter_next( const char *str )
575 while( strchr( ",;", *str ) == NULL )
578 return (*str == ';') ? (str + 1) : NULL;
582 static int64_t ParseNPT (const char *str)
584 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
585 locale_t oldloc = uselocale (loc);
589 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
590 sec += ((hour * 60) + min) * 60;
592 if (sscanf (str, "%f", &sec) != 1)
595 if (loc != (locale_t)0)
600 return sec < 0 ? -1 : sec * CLOCK_FREQ;
604 /** RTSP requests handler
605 * @param id selected track for non-aggregate URLs,
606 * NULL for aggregate URLs
608 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
610 httpd_message_t *answer,
611 const httpd_message_t *query )
613 vlc_object_t *owner = rtsp->owner;
615 const char *psz_session = NULL, *psz;
616 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
617 + strlen( rtsp->psz_path )];
618 bool vod = rtsp->vod_media != NULL;
623 if( answer == NULL || query == NULL || cl == NULL )
627 /* Build self-referential control URL */
628 char ip[NI_MAXNUMERICHOST], *ptr;
631 httpd_ServerIP( cl, ip, &port );
632 ptr = strchr( ip, '%' );
636 if( strchr( ip, ':' ) != NULL )
637 sprintf( control, "rtsp://[%s]:%d%s", ip, port, rtsp->psz_path );
639 sprintf( control, "rtsp://%s:%d%s", ip, port, rtsp->psz_path );
643 answer->i_proto = HTTPD_PROTO_RTSP;
644 answer->i_version= 0;
645 answer->i_type = HTTPD_MSG_ANSWER;
647 answer->p_body = NULL;
649 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
651 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
653 if (gmtime_r (&now, &ut) != NULL)
654 { /* RFC1123 format, GMT is mandatory */
655 static const char wdays[7][4] = {
656 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
657 static const char mons[12][4] = {
658 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
659 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
660 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
661 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
662 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
665 if( query->i_proto != HTTPD_PROTO_RTSP )
667 answer->i_status = 505;
670 if( httpd_MsgGet( query, "Require" ) != NULL )
672 answer->i_status = 551;
673 httpd_MsgAdd( answer, "Unsupported", "%s",
674 httpd_MsgGet( query, "Require" ) );
677 switch( query->i_type )
679 case HTTPD_MSG_DESCRIBE:
680 { /* Aggregate-only */
683 answer->i_status = 460;
687 answer->i_status = 200;
688 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
689 httpd_MsgAdd( answer, "Content-Base", "%s", control );
691 answer->p_body = (uint8_t *) ( vod ?
692 SDPGenerateVoD( rtsp->vod_media, control ) :
693 SDPGenerate( (sout_stream_t *)owner, control ) );
694 if( answer->p_body != NULL )
695 answer->i_body = strlen( (char *)answer->p_body );
697 answer->i_status = 500;
701 case HTTPD_MSG_SETUP:
702 /* Non-aggregate-only */
705 answer->i_status = 459;
709 psz_session = httpd_MsgGet( query, "Session" );
710 answer->i_status = 461;
712 for( const char *tpt = httpd_MsgGet( query, "Transport" );
714 tpt = transport_next( tpt ) )
716 bool b_multicast = true, b_unsupp = false;
717 unsigned loport = 5004, hiport; /* from RFC3551 */
719 /* Check transport protocol. */
720 /* Currently, we only support RTP/AVP over UDP */
721 if( strncmp( tpt, "RTP/AVP", 7 ) )
724 if( strncmp( tpt, "/UDP", 4 ) == 0 )
726 if( strchr( ";,", *tpt ) == NULL )
729 /* Parse transport options */
730 for( const char *opt = parameter_next( tpt );
732 opt = parameter_next( opt ) )
734 if( strncmp( opt, "multicast", 9 ) == 0)
737 if( strncmp( opt, "unicast", 7 ) == 0 )
740 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
744 if( strncmp( opt, "mode=", 5 ) == 0 )
746 if( strncasecmp( opt + 5, "play", 4 )
747 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
755 if( strncmp( opt,"destination=", 12 ) == 0 )
757 answer->i_status = 403;
763 * Every other option is unsupported:
765 * "source" and "append" are invalid (server-only);
766 * "ssrc" also (as clarified per RFC2326bis).
768 * For multicast, "port", "layers", "ttl" are set by the
769 * stream output configuration.
771 * For unicast, we want to decide "server_port" values.
773 * "interleaved" is not implemented.
785 char dst[NI_MAXNUMERICHOST];
787 if( id->mcast_fd == -1 )
790 net_GetPeerAddress(id->mcast_fd, dst, &dport);
792 ttl = var_InheritInteger(owner, "ttl");
794 /* FIXME: the TTL is left to the OS default, we can
795 * only guess that it's 1. */
798 if( psz_session == NULL )
800 /* Create a dummy session ID */
801 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%lu",
803 psz_session = psz_sesbuf;
805 answer->i_status = 200;
807 httpd_MsgAdd( answer, "Transport",
808 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
810 dst, dport, dport + 1, ttl );
811 /* FIXME: this doesn't work with RTP + RTCP mux */
815 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
816 rtsp_session_t *ses = NULL;
820 if( httpd_ClientIP( cl, ip, NULL ) == NULL )
822 answer->i_status = 500;
826 fd = net_ConnectDgram( owner, ip, loport, -1,
831 "cannot create RTP socket for %s port %u",
833 answer->i_status = 500;
837 /* Ignore any unexpected incoming packet */
838 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
840 net_GetSockAddress( fd, src, &sport );
842 vlc_mutex_lock( &rtsp->lock );
843 if( psz_session == NULL )
845 ses = RtspClientNew( rtsp );
846 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
848 psz_session = psz_sesbuf;
852 ses = RtspClientGet( rtsp, psz_session );
855 answer->i_status = 454;
856 vlc_mutex_unlock( &rtsp->lock );
861 RtspClientAlive(ses);
863 rtsp_strack_t *tr = NULL;
864 for (int i = 0; i < ses->trackc; i++)
866 if (ses->trackv[i].id == id)
868 tr = ses->trackv + i;
875 /* Set up a new track */
876 rtsp_strack_t track = { .id = id,
877 .sout_id = id->sout_id,
883 vlc_rand_bytes (&track.seq_init,
884 sizeof (track.seq_init));
885 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
891 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
894 else if (tr->setup_fd == -1)
896 /* The track was not SETUP, but it exists
897 * because there is a sout_id running for it */
903 /* The track is already set up, and we don't
904 * support changing the transport parameters on
906 vlc_mutex_unlock( &rtsp->lock );
907 answer->i_status = 455;
911 vlc_mutex_unlock( &rtsp->lock );
913 httpd_ServerIP( cl, ip, NULL );
915 /* Specify source IP only if it is different from the
916 * RTSP control connection server address */
917 if( strcmp( src, ip ) )
919 char *ptr = strchr( src, '%' );
920 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
925 httpd_MsgAdd( answer, "Transport",
926 "RTP/AVP/UDP;unicast%s%s;"
927 "client_port=%u-%u;server_port=%u-%u;"
928 "ssrc=%08X;mode=play",
929 src[0] ? ";source=" : "", src,
930 loport, loport + 1, sport, sport + 1, ssrc );
932 answer->i_status = 200;
941 answer->i_status = 200;
943 psz_session = httpd_MsgGet( query, "Session" );
944 int64_t start = -1, end = -1, npt;
945 const char *range = httpd_MsgGet (query, "Range");
948 if (strncmp (range, "npt=", 4))
950 answer->i_status = 501;
954 start = ParseNPT (range + 4);
955 range = strchr(range, '-');
956 if (range != NULL && *(range + 1))
957 end = ParseNPT (range + 1);
959 if (end >= 0 && end < start)
961 answer->i_status = 457;
967 if (vod_check_range(rtsp->vod_media, psz_session,
968 start, end) != VLC_SUCCESS)
970 answer->i_status = 457;
974 /* We accept start times of 0 even for broadcast streams
975 * that already started */
976 else if (start > 0 || end >= 0)
978 answer->i_status = 456;
982 vlc_mutex_lock( &rtsp->lock );
983 ses = RtspClientGet( rtsp, psz_session );
986 char info[ses->trackc * ( strlen( control ) + TRACK_PATH_SIZE
987 + sizeof("url=;seq=65535;rtptime=4294967295, ")
990 RtspClientAlive(ses);
992 sout_stream_id_t *sout_id = NULL;
995 /* We don't keep a reference to the sout_stream_t,
996 * so we check if a sout_id is available instead. */
997 for (int i = 0; i < ses->trackc; i++)
999 sout_id = ses->trackv[i].sout_id;
1000 if (sout_id != NULL)
1004 int64_t ts = rtp_get_ts(vod ? NULL : (sout_stream_t *)owner,
1005 sout_id, rtsp->vod_media, psz_session,
1008 for( int i = 0; i < ses->trackc; i++ )
1010 rtsp_strack_t *tr = ses->trackv + i;
1011 if( ( id == NULL ) || ( tr->id == id ) )
1013 if (tr->setup_fd == -1)
1014 /* Track not SETUP */
1018 if( tr->rtp_fd == -1 )
1020 /* Track not PLAYing yet */
1021 if (tr->sout_id == NULL)
1022 /* Instance not running yet (VoD) */
1026 /* Instance running, add a sink to it */
1027 tr->rtp_fd = dup_socket(tr->setup_fd);
1028 if (tr->rtp_fd == -1)
1031 rtp_add_sink( tr->sout_id, tr->rtp_fd,
1037 /* Track already playing */
1038 assert( tr->sout_id != NULL );
1039 seq = rtp_get_seq( tr->sout_id );
1041 char *url = RtspAppendTrackPath( tr->id, control );
1042 infolen += sprintf( info + infolen,
1043 "url=%s;seq=%u;rtptime=%u, ",
1044 url != NULL ? url : "", seq,
1045 rtp_compute_ts( tr->id->clock_rate, ts ) );
1051 info[infolen - 2] = '\0'; /* remove trailing ", " */
1052 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
1055 vlc_mutex_unlock( &rtsp->lock );
1061 vod_play(rtsp->vod_media, psz_session, &start, end);
1065 double f_npt = (double) npt / CLOCK_FREQ;
1066 httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
1069 if( httpd_MsgGet( query, "Scale" ) != NULL )
1070 httpd_MsgAdd( answer, "Scale", "1." );
1074 case HTTPD_MSG_PAUSE:
1076 if (id == NULL && !vod)
1078 answer->i_status = 405;
1079 httpd_MsgAdd( answer, "Allow",
1080 "%s, TEARDOWN, PLAY, GET_PARAMETER",
1081 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
1085 rtsp_session_t *ses;
1086 answer->i_status = 200;
1087 psz_session = httpd_MsgGet( query, "Session" );
1088 vlc_mutex_lock( &rtsp->lock );
1089 ses = RtspClientGet( rtsp, psz_session );
1092 if (id != NULL) /* "Mute" the selected track */
1095 for (int i = 0; i < ses->trackc; i++)
1097 rtsp_strack_t *tr = ses->trackv + i;;
1100 if (tr->setup_fd == -1)
1104 if (tr->rtp_fd != -1)
1106 rtp_del_sink(tr->sout_id, tr->rtp_fd);
1113 answer->i_status = 455;
1115 RtspClientAlive(ses);
1117 vlc_mutex_unlock( &rtsp->lock );
1119 if (ses != NULL && id == NULL)
1123 vod_pause(rtsp->vod_media, psz_session, &npt);
1124 double f_npt = (double) npt / CLOCK_FREQ;
1125 httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
1130 case HTTPD_MSG_GETPARAMETER:
1131 if( query->i_body > 0 )
1133 answer->i_status = 451;
1137 psz_session = httpd_MsgGet( query, "Session" );
1138 answer->i_status = 200;
1139 vlc_mutex_lock( &rtsp->lock );
1140 rtsp_session_t *ses = RtspClientGet( rtsp, psz_session );
1142 RtspClientAlive(ses);
1143 vlc_mutex_unlock( &rtsp->lock );
1146 case HTTPD_MSG_TEARDOWN:
1148 rtsp_session_t *ses;
1150 answer->i_status = 200;
1152 psz_session = httpd_MsgGet( query, "Session" );
1154 vlc_mutex_lock( &rtsp->lock );
1155 ses = RtspClientGet( rtsp, psz_session );
1158 if( id == NULL ) /* Delete the entire session */
1160 RtspClientDel( rtsp, ses );
1162 vod_stop(rtsp->vod_media, psz_session);
1163 RtspUpdateTimer(rtsp);
1165 else /* Delete one track from the session */
1167 for( int i = 0; i < ses->trackc; i++ )
1169 if( ses->trackv[i].id == id )
1171 RtspTrackClose( &ses->trackv[i] );
1172 /* Keep VoD tracks whose instance is still
1174 if (!(vod && ses->trackv[i].sout_id != NULL))
1175 REMOVE_ELEM( ses->trackv, ses->trackc, i );
1178 RtspClientAlive(ses);
1181 vlc_mutex_unlock( &rtsp->lock );
1186 return VLC_EGENERIC;
1191 if (rtsp->timeout > 0)
1192 httpd_MsgAdd( answer, "Session", "%s;timeout=%d", psz_session,
1195 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1198 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1199 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
1201 psz = httpd_MsgGet( query, "Cseq" );
1203 httpd_MsgAdd( answer, "Cseq", "%s", psz );
1204 psz = httpd_MsgGet( query, "Timestamp" );
1206 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
1212 /** Aggregate RTSP callback */
1213 static int RtspCallback( httpd_callback_sys_t *p_args,
1215 httpd_message_t *answer,
1216 const httpd_message_t *query )
1218 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
1222 /** Non-aggregate RTSP callback */
1223 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1225 httpd_message_t *answer,
1226 const httpd_message_t *query )
1228 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
1229 return RtspHandler( id->stream, id, cl, answer, query );