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;
69 rtsp_session_t **sessionv;
76 static int RtspCallback( httpd_callback_sys_t *p_args,
77 httpd_client_t *cl, httpd_message_t *answer,
78 const httpd_message_t *query );
79 static int RtspCallbackId( httpd_callback_sys_t *p_args,
80 httpd_client_t *cl, httpd_message_t *answer,
81 const httpd_message_t *query );
82 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
84 static void RtspTimeOut( void *data );
86 rtsp_stream_t *RtspSetup( vlc_object_t *owner, vod_media_t *media,
87 const vlc_url_t *url )
89 rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
91 if( rtsp == NULL || ( url->i_port > 99999 ) )
98 rtsp->vod_media = media;
100 rtsp->sessionv = NULL;
103 rtsp->psz_path = NULL;
105 vlc_mutex_init( &rtsp->lock );
107 rtsp->timeout = var_InheritInteger(owner, "rtsp-timeout");
108 if (rtsp->timeout > 0)
110 if (vlc_timer_create(&rtsp->timer, RtspTimeOut, rtsp))
114 rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
115 if( rtsp->psz_path == NULL )
118 msg_Dbg( owner, "RTSP stream at %s", rtsp->psz_path );
120 rtsp->host = vlc_rtsp_HostNew( VLC_OBJECT(owner) );
121 if( rtsp->host == NULL )
124 rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
126 if( rtsp->url == NULL )
129 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
130 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
131 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
132 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
133 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
135 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
144 void RtspUnsetup( rtsp_stream_t *rtsp )
147 httpd_UrlDelete( rtsp->url );
150 httpd_HostDelete( rtsp->host );
152 while( rtsp->sessionc > 0 )
153 RtspClientDel( rtsp, rtsp->sessionv[0] );
155 if (rtsp->timeout > 0)
156 vlc_timer_destroy(rtsp->timer);
158 free( rtsp->psz_path );
159 vlc_mutex_destroy( &rtsp->lock );
165 struct rtsp_stream_id_t
167 rtsp_stream_t *stream;
168 sout_stream_id_t *sout_id;
172 unsigned clock_rate; /* needed to compute rtptime in RTP-Info */
177 typedef struct rtsp_strack_t rtsp_strack_t;
179 /* For unicast streaming */
180 struct rtsp_session_t
182 rtsp_stream_t *stream;
184 mtime_t last_seen; /* for timeouts */
186 /* output (id-access) */
188 rtsp_strack_t *trackv;
192 /* Unicast session track */
195 rtsp_stream_id_t *id;
196 sout_stream_id_t *sout_id;
197 int setup_fd; /* socket created by the SETUP request */
198 int rtp_fd; /* socket used by the RTP output, when playing */
203 static void RtspTrackClose( rtsp_strack_t *tr );
205 #define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
207 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
209 const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
213 if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
219 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
220 uint32_t ssrc, unsigned clock_rate,
223 if (rtsp->track_id > 999)
225 msg_Err(rtsp->owner, "RTSP: too many IDs!");
230 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
238 id->track_id = rtsp->track_id;
240 id->clock_rate = clock_rate;
241 id->mcast_fd = mcast_fd;
243 urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
250 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
251 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
260 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
261 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
262 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
263 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
264 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
265 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
273 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
275 httpd_UrlDelete( id->url );
277 vlc_mutex_lock( &rtsp->lock );
278 for( int i = 0; i < rtsp->sessionc; i++ )
280 rtsp_session_t *ses = rtsp->sessionv[i];
282 for( int j = 0; j < ses->trackc; j++ )
284 if( ses->trackv[j].id == id )
286 rtsp_strack_t *tr = ses->trackv + j;
287 RtspTrackClose( tr );
288 REMOVE_ELEM( ses->trackv, ses->trackc, j );
293 vlc_mutex_unlock( &rtsp->lock );
298 /** rtsp must be locked */
299 static void RtspUpdateTimer( rtsp_stream_t *rtsp )
301 if (rtsp->timeout <= 0)
305 for (int i = 0; i < rtsp->sessionc; i++)
307 if (timeout == 0 || rtsp->sessionv[i]->last_seen < timeout)
308 timeout = rtsp->sessionv[i]->last_seen;
311 timeout += rtsp->timeout * CLOCK_FREQ;
312 vlc_timer_schedule(rtsp->timer, true, timeout, 0);
316 static void RtspTimeOut( void *data )
318 rtsp_stream_t *rtsp = data;
320 vlc_mutex_lock(&rtsp->lock);
321 mtime_t now = mdate();
322 for (int i = rtsp->sessionc - 1; i >= 0; i--)
324 if (rtsp->sessionv[i]->last_seen + rtsp->timeout * CLOCK_FREQ < now)
326 if (rtsp->vod_media != NULL)
329 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
330 rtsp->sessionv[i]->id );
331 vod_stop(rtsp->vod_media, psz_sesbuf);
333 RtspClientDel(rtsp, rtsp->sessionv[i]);
336 RtspUpdateTimer(rtsp);
337 vlc_mutex_unlock(&rtsp->lock);
341 /** rtsp must be locked */
343 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
345 rtsp_session_t *s = malloc( sizeof( *s ) );
350 vlc_rand_bytes (&s->id, sizeof (s->id));
354 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
360 /** rtsp must be locked */
362 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
372 id = strtoull( name, &end, 0x10 );
376 /* FIXME: use a hash/dictionary */
377 for( i = 0; i < rtsp->sessionc; i++ )
379 if( rtsp->sessionv[i]->id == id )
380 return rtsp->sessionv[i];
386 /** rtsp must be locked */
388 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
391 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
393 for( i = 0; i < session->trackc; i++ )
394 RtspTrackClose( &session->trackv[i] );
396 free( session->trackv );
401 /** rtsp must be locked */
402 static void RtspClientAlive( rtsp_session_t *session )
404 if (session->stream->timeout <= 0)
407 session->last_seen = mdate();
408 RtspUpdateTimer(session->stream);
411 static int dup_socket(int oldfd)
414 #if !defined(WIN32) || defined(UNDER_CE)
415 newfd = vlc_dup(oldfd);
417 WSAPROTOCOL_INFO info;
418 WSADuplicateSocket (oldfd, GetCurrentProcessId (), &info);
419 newfd = WSASocket (info.iAddressFamily, info.iSocketType,
420 info.iProtocol, &info, 0, 0);
425 /* Attach a starting VoD RTP id to its RTSP track, and let it
426 * initialize with the parameters of the SETUP request */
427 int RtspTrackAttach( rtsp_stream_t *rtsp, const char *name,
428 rtsp_stream_id_t *id, sout_stream_id_t *sout_id,
429 uint32_t *ssrc, uint16_t *seq_init )
431 int val = VLC_EGENERIC;
432 rtsp_session_t *session;
434 vlc_mutex_lock(&rtsp->lock);
435 session = RtspClientGet(rtsp, name);
440 rtsp_strack_t *tr = NULL;
441 for (int i = 0; i < session->trackc; i++)
443 if (session->trackv[i].id == id)
445 tr = session->trackv + i;
452 tr->sout_id = sout_id;
453 tr->rtp_fd = dup_socket(tr->setup_fd);
457 /* The track was not SETUP. We still create one because we'll
458 * need the sout_id if we set it up later. */
459 rtsp_strack_t track = { .id = id, .sout_id = sout_id,
460 .setup_fd = -1, .rtp_fd = -1 };
461 vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
462 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
464 INSERT_ELEM(session->trackv, session->trackc, session->trackc, track);
465 tr = session->trackv + session->trackc - 1;
468 *ssrc = ntohl(tr->ssrc);
469 *seq_init = tr->seq_init;
471 if (tr->rtp_fd != -1)
474 rtp_add_sink(tr->sout_id, tr->rtp_fd, false, &seq);
475 /* To avoid race conditions, sout_id->i_seq_sent_next must
476 * be set here and now. Make sure the caller did its job
477 * properly when passing seq_init. */
478 assert(tr->seq_init == seq);
483 vlc_mutex_unlock(&rtsp->lock);
488 /* Remove references to the RTP id when it is stopped */
489 void RtspTrackDetach( rtsp_stream_t *rtsp, const char *name,
490 sout_stream_id_t *sout_id )
492 rtsp_session_t *session;
494 vlc_mutex_lock(&rtsp->lock);
495 session = RtspClientGet(rtsp, name);
500 for (int i = 0; i < session->trackc; i++)
502 rtsp_strack_t *tr = session->trackv + i;
503 if (tr->sout_id == sout_id)
505 if (tr->setup_fd == -1)
507 /* No (more) SETUP information: better get rid of the
508 * track so that we can have new random ssrc and
509 * seq_init next time. */
510 REMOVE_ELEM( session->trackv, session->trackc, i );
513 /* We keep the SETUP information of the track, but stop it */
514 if (tr->rtp_fd != -1)
516 rtp_del_sink(tr->sout_id, tr->rtp_fd);
525 vlc_mutex_unlock(&rtsp->lock);
529 /** rtsp must be locked */
530 static void RtspTrackClose( rtsp_strack_t *tr )
532 if (tr->setup_fd != -1)
534 if (tr->rtp_fd != -1)
536 rtp_del_sink(tr->sout_id, tr->rtp_fd);
539 net_Close(tr->setup_fd);
545 /** Finds the next transport choice */
546 static inline const char *transport_next( const char *str )
548 /* Looks for comma */
549 str = strchr( str, ',' );
551 return NULL; /* No more transport options */
553 str++; /* skips comma */
554 while( strchr( "\r\n\t ", *str ) )
557 return (*str) ? str : NULL;
561 /** Finds the next transport parameter */
562 static inline const char *parameter_next( const char *str )
564 while( strchr( ",;", *str ) == NULL )
567 return (*str == ';') ? (str + 1) : NULL;
571 static int64_t ParseNPT (const char *str)
573 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
574 locale_t oldloc = uselocale (loc);
578 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
579 sec += ((hour * 60) + min) * 60;
581 if (sscanf (str, "%f", &sec) != 1)
584 if (loc != (locale_t)0)
589 return sec < 0 ? -1 : sec * CLOCK_FREQ;
593 /** RTSP requests handler
594 * @param id selected track for non-aggregate URLs,
595 * NULL for aggregate URLs
597 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
599 httpd_message_t *answer,
600 const httpd_message_t *query )
602 vlc_object_t *owner = rtsp->owner;
604 const char *psz_session = NULL, *psz;
605 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
606 + strlen( rtsp->psz_path )];
607 bool vod = rtsp->vod_media != NULL;
612 if( answer == NULL || query == NULL || cl == NULL )
616 /* Build self-referential control URL */
617 char ip[NI_MAXNUMERICHOST], *ptr;
620 httpd_ServerIP( cl, ip, &port );
621 ptr = strchr( ip, '%' );
625 if( strchr( ip, ':' ) != NULL )
626 sprintf( control, "rtsp://[%s]:%d%s", ip, port, rtsp->psz_path );
628 sprintf( control, "rtsp://%s:%d%s", ip, port, rtsp->psz_path );
632 answer->i_proto = HTTPD_PROTO_RTSP;
633 answer->i_version= 0;
634 answer->i_type = HTTPD_MSG_ANSWER;
636 answer->p_body = NULL;
638 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
640 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
642 if (gmtime_r (&now, &ut) != NULL)
643 { /* RFC1123 format, GMT is mandatory */
644 static const char wdays[7][4] = {
645 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
646 static const char mons[12][4] = {
647 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
648 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
649 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
650 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
651 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
654 if( query->i_proto != HTTPD_PROTO_RTSP )
656 answer->i_status = 505;
659 if( httpd_MsgGet( query, "Require" ) != NULL )
661 answer->i_status = 551;
662 httpd_MsgAdd( answer, "Unsupported", "%s",
663 httpd_MsgGet( query, "Require" ) );
666 switch( query->i_type )
668 case HTTPD_MSG_DESCRIBE:
669 { /* Aggregate-only */
672 answer->i_status = 460;
676 answer->i_status = 200;
677 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
678 httpd_MsgAdd( answer, "Content-Base", "%s", control );
680 answer->p_body = (uint8_t *) ( vod ?
681 SDPGenerateVoD( rtsp->vod_media, control ) :
682 SDPGenerate( (sout_stream_t *)owner, control ) );
683 if( answer->p_body != NULL )
684 answer->i_body = strlen( (char *)answer->p_body );
686 answer->i_status = 500;
690 case HTTPD_MSG_SETUP:
691 /* Non-aggregate-only */
694 answer->i_status = 459;
698 psz_session = httpd_MsgGet( query, "Session" );
699 answer->i_status = 461;
701 for( const char *tpt = httpd_MsgGet( query, "Transport" );
703 tpt = transport_next( tpt ) )
705 bool b_multicast = true, b_unsupp = false;
706 unsigned loport = 5004, hiport; /* from RFC3551 */
708 /* Check transport protocol. */
709 /* Currently, we only support RTP/AVP over UDP */
710 if( strncmp( tpt, "RTP/AVP", 7 ) )
713 if( strncmp( tpt, "/UDP", 4 ) == 0 )
715 if( strchr( ";,", *tpt ) == NULL )
718 /* Parse transport options */
719 for( const char *opt = parameter_next( tpt );
721 opt = parameter_next( opt ) )
723 if( strncmp( opt, "multicast", 9 ) == 0)
726 if( strncmp( opt, "unicast", 7 ) == 0 )
729 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
733 if( strncmp( opt, "mode=", 5 ) == 0 )
735 if( strncasecmp( opt + 5, "play", 4 )
736 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
744 if( strncmp( opt,"destination=", 12 ) == 0 )
746 answer->i_status = 403;
752 * Every other option is unsupported:
754 * "source" and "append" are invalid (server-only);
755 * "ssrc" also (as clarified per RFC2326bis).
757 * For multicast, "port", "layers", "ttl" are set by the
758 * stream output configuration.
760 * For unicast, we want to decide "server_port" values.
762 * "interleaved" is not implemented.
774 char dst[NI_MAXNUMERICHOST];
776 if( id->mcast_fd == -1 )
779 net_GetPeerAddress(id->mcast_fd, dst, &dport);
781 ttl = var_InheritInteger(owner, "ttl");
783 /* FIXME: the TTL is left to the OS default, we can
784 * only guess that it's 1. */
787 if( psz_session == NULL )
789 /* Create a dummy session ID */
790 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%lu",
792 psz_session = psz_sesbuf;
794 answer->i_status = 200;
796 httpd_MsgAdd( answer, "Transport",
797 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
799 dst, dport, dport + 1, ttl );
800 /* FIXME: this doesn't work with RTP + RTCP mux */
804 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
805 rtsp_session_t *ses = NULL;
809 if( httpd_ClientIP( cl, ip, NULL ) == NULL )
811 answer->i_status = 500;
815 fd = net_ConnectDgram( owner, ip, loport, -1,
820 "cannot create RTP socket for %s port %u",
822 answer->i_status = 500;
826 /* Ignore any unexpected incoming packet */
827 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
829 net_GetSockAddress( fd, src, &sport );
831 vlc_mutex_lock( &rtsp->lock );
832 if( psz_session == NULL )
834 ses = RtspClientNew( rtsp );
835 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
837 psz_session = psz_sesbuf;
841 ses = RtspClientGet( rtsp, psz_session );
844 answer->i_status = 454;
845 vlc_mutex_unlock( &rtsp->lock );
850 RtspClientAlive(ses);
852 rtsp_strack_t *tr = NULL;
853 for (int i = 0; i < ses->trackc; i++)
855 if (ses->trackv[i].id == id)
857 tr = ses->trackv + i;
864 /* Set up a new track */
865 rtsp_strack_t track = { .id = id,
866 .sout_id = id->sout_id,
872 vlc_rand_bytes (&track.seq_init,
873 sizeof (track.seq_init));
874 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
880 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
883 else if (tr->setup_fd == -1)
885 /* The track was not SETUP, but it exists
886 * because there is a sout_id running for it */
892 /* The track is already set up, and we don't
893 * support changing the transport parameters on
895 vlc_mutex_unlock( &rtsp->lock );
896 answer->i_status = 455;
900 vlc_mutex_unlock( &rtsp->lock );
902 httpd_ServerIP( cl, ip, NULL );
904 /* Specify source IP only if it is different from the
905 * RTSP control connection server address */
906 if( strcmp( src, ip ) )
908 char *ptr = strchr( src, '%' );
909 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
914 httpd_MsgAdd( answer, "Transport",
915 "RTP/AVP/UDP;unicast%s%s;"
916 "client_port=%u-%u;server_port=%u-%u;"
917 "ssrc=%08X;mode=play",
918 src[0] ? ";source=" : "", src,
919 loport, loport + 1, sport, sport + 1, ssrc );
921 answer->i_status = 200;
930 answer->i_status = 200;
932 psz_session = httpd_MsgGet( query, "Session" );
933 int64_t start = -1, end = -1, npt;
934 const char *range = httpd_MsgGet (query, "Range");
937 if (strncmp (range, "npt=", 4))
939 answer->i_status = 501;
943 start = ParseNPT (range + 4);
944 range = strchr(range, '-');
945 if (range != NULL && *(range + 1))
946 end = ParseNPT (range + 1);
948 if (end >= 0 && end < start)
950 answer->i_status = 457;
956 if (vod_check_range(rtsp->vod_media, psz_session,
957 start, end) != VLC_SUCCESS)
959 answer->i_status = 457;
963 /* We accept start times of 0 even for broadcast streams
964 * that already started */
965 else if (start > 0 || end >= 0)
967 answer->i_status = 456;
971 vlc_mutex_lock( &rtsp->lock );
972 ses = RtspClientGet( rtsp, psz_session );
975 char info[ses->trackc * ( strlen( control ) + TRACK_PATH_SIZE
976 + sizeof("url=;seq=65535;rtptime=4294967295, ")
979 RtspClientAlive(ses);
981 sout_stream_id_t *sout_id = NULL;
984 /* We don't keep a reference to the sout_stream_t,
985 * so we check if a sout_id is available instead. */
986 for (int i = 0; i < ses->trackc; i++)
988 sout_id = ses->trackv[i].sout_id;
993 int64_t ts = rtp_get_ts(vod ? NULL : (sout_stream_t *)owner,
994 sout_id, rtsp->vod_media, psz_session,
997 for( int i = 0; i < ses->trackc; i++ )
999 rtsp_strack_t *tr = ses->trackv + i;
1000 if( ( id == NULL ) || ( tr->id == id ) )
1002 if (tr->setup_fd == -1)
1003 /* Track not SETUP */
1007 if( tr->rtp_fd == -1 )
1009 /* Track not PLAYing yet */
1010 if (tr->sout_id == NULL)
1011 /* Instance not running yet (VoD) */
1015 /* Instance running, add a sink to it */
1016 tr->rtp_fd = dup_socket(tr->setup_fd);
1017 if (tr->rtp_fd == -1)
1020 rtp_add_sink( tr->sout_id, tr->rtp_fd,
1026 /* Track already playing */
1027 assert( tr->sout_id != NULL );
1028 seq = rtp_get_seq( tr->sout_id );
1030 char *url = RtspAppendTrackPath( tr->id, control );
1031 infolen += sprintf( info + infolen,
1032 "url=%s;seq=%u;rtptime=%u, ",
1033 url != NULL ? url : "", seq,
1034 rtp_compute_ts( tr->id->clock_rate, ts ) );
1040 info[infolen - 2] = '\0'; /* remove trailing ", " */
1041 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
1045 vod_play(rtsp->vod_media, psz_session, &start, end);
1049 vlc_mutex_unlock( &rtsp->lock );
1053 double f_npt = (double) npt / CLOCK_FREQ;
1054 httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
1057 if( httpd_MsgGet( query, "Scale" ) != NULL )
1058 httpd_MsgAdd( answer, "Scale", "1." );
1062 case HTTPD_MSG_PAUSE:
1064 if (id == NULL && !vod)
1066 answer->i_status = 405;
1067 httpd_MsgAdd( answer, "Allow",
1068 "%s, TEARDOWN, PLAY, GET_PARAMETER",
1069 ( id != NULL ) ? "SETUP" : "DESCRIBE" );
1073 rtsp_session_t *ses;
1074 answer->i_status = 200;
1075 psz_session = httpd_MsgGet( query, "Session" );
1076 vlc_mutex_lock( &rtsp->lock );
1077 ses = RtspClientGet( rtsp, psz_session );
1084 vod_pause(rtsp->vod_media, psz_session, &npt);
1085 double f_npt = (double) npt / CLOCK_FREQ;
1086 httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
1088 else /* "Mute" the selected track */
1091 for (int i = 0; i < ses->trackc; i++)
1093 rtsp_strack_t *tr = ses->trackv + i;;
1096 if (tr->setup_fd == -1)
1100 if (tr->rtp_fd != -1)
1102 rtp_del_sink(tr->sout_id, tr->rtp_fd);
1109 answer->i_status = 455;
1111 RtspClientAlive(ses);
1113 vlc_mutex_unlock( &rtsp->lock );
1117 case HTTPD_MSG_GETPARAMETER:
1118 if( query->i_body > 0 )
1120 answer->i_status = 451;
1124 psz_session = httpd_MsgGet( query, "Session" );
1125 answer->i_status = 200;
1126 vlc_mutex_lock( &rtsp->lock );
1127 rtsp_session_t *ses = RtspClientGet( rtsp, psz_session );
1129 RtspClientAlive(ses);
1130 vlc_mutex_unlock( &rtsp->lock );
1133 case HTTPD_MSG_TEARDOWN:
1135 rtsp_session_t *ses;
1137 answer->i_status = 200;
1139 psz_session = httpd_MsgGet( query, "Session" );
1141 vlc_mutex_lock( &rtsp->lock );
1142 ses = RtspClientGet( rtsp, psz_session );
1145 if( id == NULL ) /* Delete the entire session */
1147 RtspClientDel( rtsp, ses );
1149 vod_stop(rtsp->vod_media, psz_session);
1150 RtspUpdateTimer(rtsp);
1152 else /* Delete one track from the session */
1154 for( int i = 0; i < ses->trackc; i++ )
1156 if( ses->trackv[i].id == id )
1158 RtspTrackClose( &ses->trackv[i] );
1159 /* Keep VoD tracks whose instance is still
1161 if (!(vod && ses->trackv[i].sout_id != NULL))
1162 REMOVE_ELEM( ses->trackv, ses->trackc, i );
1165 RtspClientAlive(ses);
1168 vlc_mutex_unlock( &rtsp->lock );
1173 return VLC_EGENERIC;
1178 if (rtsp->timeout > 0)
1179 httpd_MsgAdd( answer, "Session", "%s;timeout=%d", psz_session,
1182 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1185 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1186 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
1188 psz = httpd_MsgGet( query, "Cseq" );
1190 httpd_MsgAdd( answer, "Cseq", "%s", psz );
1191 psz = httpd_MsgGet( query, "Timestamp" );
1193 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
1199 /** Aggregate RTSP callback */
1200 static int RtspCallback( httpd_callback_sys_t *p_args,
1202 httpd_message_t *answer,
1203 const httpd_message_t *query )
1205 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
1209 /** Non-aggregate RTSP callback */
1210 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1212 httpd_message_t *answer,
1213 const httpd_message_t *query )
1215 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
1216 return RtspHandler( id->stream, id, cl, answer, query );