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>
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_UrlNewUnique( rtsp->host, rtsp->psz_path,
132 if( rtsp->url == NULL )
135 httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
136 httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP, RtspCallback, (void*)rtsp );
137 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY, RtspCallback, (void*)rtsp );
138 httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE, RtspCallback, (void*)rtsp );
139 httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
141 httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
150 void RtspUnsetup( rtsp_stream_t *rtsp )
153 httpd_UrlDelete( rtsp->url );
156 httpd_HostDelete( rtsp->host );
158 while( rtsp->sessionc > 0 )
159 RtspClientDel( rtsp, rtsp->sessionv[0] );
161 if (rtsp->timeout > 0)
162 vlc_timer_destroy(rtsp->timer);
164 free( rtsp->psz_path );
165 vlc_mutex_destroy( &rtsp->lock );
171 struct rtsp_stream_id_t
173 rtsp_stream_t *stream;
174 sout_stream_id_t *sout_id;
178 unsigned clock_rate; /* needed to compute rtptime in RTP-Info */
183 typedef struct rtsp_strack_t rtsp_strack_t;
185 /* For unicast streaming */
186 struct rtsp_session_t
188 rtsp_stream_t *stream;
190 mtime_t last_seen; /* for timeouts */
192 /* output (id-access) */
194 rtsp_strack_t *trackv;
198 /* Unicast session track */
201 rtsp_stream_id_t *id;
202 sout_stream_id_t *sout_id;
203 int setup_fd; /* socket created by the SETUP request */
204 int rtp_fd; /* socket used by the RTP output, when playing */
209 static void RtspTrackClose( rtsp_strack_t *tr );
211 #define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
213 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
215 const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
219 if( asprintf( &url, "%s%strackID=%u", base, sep, id->track_id ) == -1 )
225 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
226 uint32_t ssrc, unsigned clock_rate,
229 if (rtsp->track_id > 999)
231 msg_Err(rtsp->owner, "RTSP: too many IDs!");
236 rtsp_stream_id_t *id = malloc( sizeof( *id ) );
244 id->track_id = rtsp->track_id;
246 id->clock_rate = clock_rate;
247 id->mcast_fd = mcast_fd;
249 urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
256 msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
258 char *user = var_InheritString(rtsp->owner, "sout-rtsp-user");
259 char *pwd = var_InheritString(rtsp->owner, "sout-rtsp-pwd");
261 url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, user, pwd, NULL );
272 httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
273 httpd_UrlCatch( url, HTTPD_MSG_SETUP, RtspCallbackId, (void *)id );
274 httpd_UrlCatch( url, HTTPD_MSG_PLAY, RtspCallbackId, (void *)id );
275 httpd_UrlCatch( url, HTTPD_MSG_PAUSE, RtspCallbackId, (void *)id );
276 httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
277 httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
285 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
287 httpd_UrlDelete( id->url );
289 vlc_mutex_lock( &rtsp->lock );
290 for( int i = 0; i < rtsp->sessionc; i++ )
292 rtsp_session_t *ses = rtsp->sessionv[i];
294 for( int j = 0; j < ses->trackc; j++ )
296 if( ses->trackv[j].id == id )
298 rtsp_strack_t *tr = ses->trackv + j;
299 RtspTrackClose( tr );
300 REMOVE_ELEM( ses->trackv, ses->trackc, j );
305 vlc_mutex_unlock( &rtsp->lock );
310 /** rtsp must be locked */
311 static void RtspUpdateTimer( rtsp_stream_t *rtsp )
313 if (rtsp->timeout <= 0)
317 for (int i = 0; i < rtsp->sessionc; i++)
319 if (timeout == 0 || rtsp->sessionv[i]->last_seen < timeout)
320 timeout = rtsp->sessionv[i]->last_seen;
323 timeout += rtsp->timeout * CLOCK_FREQ;
324 vlc_timer_schedule(rtsp->timer, true, timeout, 0);
328 static void RtspTimeOut( void *data )
330 rtsp_stream_t *rtsp = data;
332 vlc_mutex_lock(&rtsp->lock);
333 mtime_t now = mdate();
334 for (int i = rtsp->sessionc - 1; i >= 0; i--)
336 if (rtsp->sessionv[i]->last_seen + rtsp->timeout * CLOCK_FREQ < now)
338 if (rtsp->vod_media != NULL)
341 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
342 rtsp->sessionv[i]->id );
343 vod_stop(rtsp->vod_media, psz_sesbuf);
345 RtspClientDel(rtsp, rtsp->sessionv[i]);
348 RtspUpdateTimer(rtsp);
349 vlc_mutex_unlock(&rtsp->lock);
353 /** rtsp must be locked */
355 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
357 rtsp_session_t *s = malloc( sizeof( *s ) );
362 vlc_rand_bytes (&s->id, sizeof (s->id));
366 TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
372 /** rtsp must be locked */
374 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
384 id = strtoull( name, &end, 0x10 );
388 /* FIXME: use a hash/dictionary */
389 for( i = 0; i < rtsp->sessionc; i++ )
391 if( rtsp->sessionv[i]->id == id )
392 return rtsp->sessionv[i];
398 /** rtsp must be locked */
400 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
403 TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
405 for( i = 0; i < session->trackc; i++ )
406 RtspTrackClose( &session->trackv[i] );
408 free( session->trackv );
413 /** rtsp must be locked */
414 static void RtspClientAlive( rtsp_session_t *session )
416 if (session->stream->timeout <= 0)
419 session->last_seen = mdate();
420 RtspUpdateTimer(session->stream);
423 static int dup_socket(int oldfd)
426 #if !defined(WIN32) || defined(UNDER_CE)
427 newfd = vlc_dup(oldfd);
429 WSAPROTOCOL_INFO info;
430 WSADuplicateSocket (oldfd, GetCurrentProcessId (), &info);
431 newfd = WSASocket (info.iAddressFamily, info.iSocketType,
432 info.iProtocol, &info, 0, 0);
437 /* Attach a starting VoD RTP id to its RTSP track, and let it
438 * initialize with the parameters of the SETUP request */
439 int RtspTrackAttach( rtsp_stream_t *rtsp, const char *name,
440 rtsp_stream_id_t *id, sout_stream_id_t *sout_id,
441 uint32_t *ssrc, uint16_t *seq_init )
443 int val = VLC_EGENERIC;
444 rtsp_session_t *session;
446 vlc_mutex_lock(&rtsp->lock);
447 session = RtspClientGet(rtsp, name);
452 rtsp_strack_t *tr = NULL;
453 for (int i = 0; i < session->trackc; i++)
455 if (session->trackv[i].id == id)
457 tr = session->trackv + i;
464 tr->sout_id = sout_id;
465 tr->rtp_fd = dup_socket(tr->setup_fd);
469 /* The track was not SETUP. We still create one because we'll
470 * need the sout_id if we set it up later. */
471 rtsp_strack_t track = { .id = id, .sout_id = sout_id,
472 .setup_fd = -1, .rtp_fd = -1 };
473 vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
474 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
476 INSERT_ELEM(session->trackv, session->trackc, session->trackc, track);
477 tr = session->trackv + session->trackc - 1;
480 *ssrc = ntohl(tr->ssrc);
481 *seq_init = tr->seq_init;
483 if (tr->rtp_fd != -1)
486 rtp_add_sink(tr->sout_id, tr->rtp_fd, false, &seq);
487 /* To avoid race conditions, sout_id->i_seq_sent_next must
488 * be set here and now. Make sure the caller did its job
489 * properly when passing seq_init. */
490 assert(tr->seq_init == seq);
495 vlc_mutex_unlock(&rtsp->lock);
500 /* Remove references to the RTP id when it is stopped */
501 void RtspTrackDetach( rtsp_stream_t *rtsp, const char *name,
502 sout_stream_id_t *sout_id )
504 rtsp_session_t *session;
506 vlc_mutex_lock(&rtsp->lock);
507 session = RtspClientGet(rtsp, name);
512 for (int i = 0; i < session->trackc; i++)
514 rtsp_strack_t *tr = session->trackv + i;
515 if (tr->sout_id == sout_id)
517 if (tr->setup_fd == -1)
519 /* No (more) SETUP information: better get rid of the
520 * track so that we can have new random ssrc and
521 * seq_init next time. */
522 REMOVE_ELEM( session->trackv, session->trackc, i );
525 /* We keep the SETUP information of the track, but stop it */
526 if (tr->rtp_fd != -1)
528 rtp_del_sink(tr->sout_id, tr->rtp_fd);
537 vlc_mutex_unlock(&rtsp->lock);
541 /** rtsp must be locked */
542 static void RtspTrackClose( rtsp_strack_t *tr )
544 if (tr->setup_fd != -1)
546 if (tr->rtp_fd != -1)
548 rtp_del_sink(tr->sout_id, tr->rtp_fd);
551 net_Close(tr->setup_fd);
557 /** Finds the next transport choice */
558 static inline const char *transport_next( const char *str )
560 /* Looks for comma */
561 str = strchr( str, ',' );
563 return NULL; /* No more transport options */
565 str++; /* skips comma */
566 while( strchr( "\r\n\t ", *str ) )
569 return (*str) ? str : NULL;
573 /** Finds the next transport parameter */
574 static inline const char *parameter_next( const char *str )
576 while( strchr( ",;", *str ) == NULL )
579 return (*str == ';') ? (str + 1) : NULL;
583 static int64_t ParseNPT (const char *str)
585 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
586 locale_t oldloc = uselocale (loc);
590 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
591 sec += ((hour * 60) + min) * 60;
593 if (sscanf (str, "%f", &sec) != 1)
596 if (loc != (locale_t)0)
601 return sec < 0 ? -1 : sec * CLOCK_FREQ;
605 /** RTSP requests handler
606 * @param id selected track for non-aggregate URLs,
607 * NULL for aggregate URLs
609 static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
611 httpd_message_t *answer,
612 const httpd_message_t *query )
614 vlc_object_t *owner = rtsp->owner;
616 const char *psz_session = NULL, *psz;
617 char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
618 + strlen( rtsp->psz_path )];
619 bool vod = rtsp->vod_media != NULL;
624 if( answer == NULL || query == NULL || cl == NULL )
628 /* Build self-referential control URL */
629 char ip[NI_MAXNUMERICHOST], *ptr;
632 httpd_ServerIP( cl, ip, &port );
633 ptr = strchr( ip, '%' );
637 if( strchr( ip, ':' ) != NULL )
638 sprintf( control, "rtsp://[%s]:%d%s", ip, port, rtsp->psz_path );
640 sprintf( control, "rtsp://%s:%d%s", ip, port, rtsp->psz_path );
644 answer->i_proto = HTTPD_PROTO_RTSP;
645 answer->i_version= 0;
646 answer->i_type = HTTPD_MSG_ANSWER;
648 answer->p_body = NULL;
650 httpd_MsgAdd( answer, "Server", "VLC/%s", VERSION );
652 /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
654 if (gmtime_r (&now, &ut) != NULL)
655 { /* RFC1123 format, GMT is mandatory */
656 static const char wdays[7][4] = {
657 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
658 static const char mons[12][4] = {
659 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
660 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
661 httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
662 wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
663 1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
666 if( query->i_proto != HTTPD_PROTO_RTSP )
668 answer->i_status = 505;
671 if( httpd_MsgGet( query, "Require" ) != NULL )
673 answer->i_status = 551;
674 httpd_MsgAdd( answer, "Unsupported", "%s",
675 httpd_MsgGet( query, "Require" ) );
678 switch( query->i_type )
680 case HTTPD_MSG_DESCRIBE:
681 { /* Aggregate-only */
684 answer->i_status = 460;
688 answer->i_status = 200;
689 httpd_MsgAdd( answer, "Content-Type", "%s", "application/sdp" );
690 httpd_MsgAdd( answer, "Content-Base", "%s", control );
692 answer->p_body = (uint8_t *) ( vod ?
693 SDPGenerateVoD( rtsp->vod_media, control ) :
694 SDPGenerate( (sout_stream_t *)owner, control ) );
695 if( answer->p_body != NULL )
696 answer->i_body = strlen( (char *)answer->p_body );
698 answer->i_status = 500;
702 case HTTPD_MSG_SETUP:
703 /* Non-aggregate-only */
706 answer->i_status = 459;
710 psz_session = httpd_MsgGet( query, "Session" );
711 answer->i_status = 461;
713 for( const char *tpt = httpd_MsgGet( query, "Transport" );
715 tpt = transport_next( tpt ) )
717 bool b_multicast = true, b_unsupp = false;
718 unsigned loport = 5004, hiport; /* from RFC3551 */
720 /* Check transport protocol. */
721 /* Currently, we only support RTP/AVP over UDP */
722 if( strncmp( tpt, "RTP/AVP", 7 ) )
725 if( strncmp( tpt, "/UDP", 4 ) == 0 )
727 if( strchr( ";,", *tpt ) == NULL )
730 /* Parse transport options */
731 for( const char *opt = parameter_next( tpt );
733 opt = parameter_next( opt ) )
735 if( strncmp( opt, "multicast", 9 ) == 0)
738 if( strncmp( opt, "unicast", 7 ) == 0 )
741 if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
745 if( strncmp( opt, "mode=", 5 ) == 0 )
747 if( strncasecmp( opt + 5, "play", 4 )
748 && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
756 if( strncmp( opt,"destination=", 12 ) == 0 )
758 answer->i_status = 403;
764 * Every other option is unsupported:
766 * "source" and "append" are invalid (server-only);
767 * "ssrc" also (as clarified per RFC2326bis).
769 * For multicast, "port", "layers", "ttl" are set by the
770 * stream output configuration.
772 * For unicast, we want to decide "server_port" values.
774 * "interleaved" is not implemented.
786 char dst[NI_MAXNUMERICHOST];
788 if( id->mcast_fd == -1 )
791 net_GetPeerAddress(id->mcast_fd, dst, &dport);
793 ttl = var_InheritInteger(owner, "ttl");
795 /* FIXME: the TTL is left to the OS default, we can
796 * only guess that it's 1. */
799 if( psz_session == NULL )
801 /* Create a dummy session ID */
802 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%lu",
804 psz_session = psz_sesbuf;
806 answer->i_status = 200;
808 httpd_MsgAdd( answer, "Transport",
809 "RTP/AVP/UDP;destination=%s;port=%u-%u;"
811 dst, dport, dport + 1, ttl );
812 /* FIXME: this doesn't work with RTP + RTCP mux */
816 char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
817 rtsp_session_t *ses = NULL;
821 if( httpd_ClientIP( cl, ip, NULL ) == NULL )
823 answer->i_status = 500;
827 fd = net_ConnectDgram( owner, ip, loport, -1,
832 "cannot create RTP socket for %s port %u",
834 answer->i_status = 500;
838 /* Ignore any unexpected incoming packet */
839 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
841 net_GetSockAddress( fd, src, &sport );
843 vlc_mutex_lock( &rtsp->lock );
844 if( psz_session == NULL )
846 ses = RtspClientNew( rtsp );
847 snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
849 psz_session = psz_sesbuf;
853 ses = RtspClientGet( rtsp, psz_session );
856 answer->i_status = 454;
857 vlc_mutex_unlock( &rtsp->lock );
862 RtspClientAlive(ses);
864 rtsp_strack_t *tr = NULL;
865 for (int i = 0; i < ses->trackc; i++)
867 if (ses->trackv[i].id == id)
869 tr = ses->trackv + i;
876 /* Set up a new track */
877 rtsp_strack_t track = { .id = id,
878 .sout_id = id->sout_id,
884 vlc_rand_bytes (&track.seq_init,
885 sizeof (track.seq_init));
886 vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
892 INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
895 else if (tr->setup_fd == -1)
897 /* The track was not SETUP, but it exists
898 * because there is a sout_id running for it */
904 /* The track is already set up, and we don't
905 * support changing the transport parameters on
907 vlc_mutex_unlock( &rtsp->lock );
908 answer->i_status = 455;
912 vlc_mutex_unlock( &rtsp->lock );
914 httpd_ServerIP( cl, ip, NULL );
916 /* Specify source IP only if it is different from the
917 * RTSP control connection server address */
918 if( strcmp( src, ip ) )
920 char *ptr = strchr( src, '%' );
921 if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
926 httpd_MsgAdd( answer, "Transport",
927 "RTP/AVP/UDP;unicast%s%s;"
928 "client_port=%u-%u;server_port=%u-%u;"
929 "ssrc=%08X;mode=play",
930 src[0] ? ";source=" : "", src,
931 loport, loport + 1, sport, sport + 1, ssrc );
933 answer->i_status = 200;
942 answer->i_status = 200;
944 psz_session = httpd_MsgGet( query, "Session" );
945 int64_t start = -1, end = -1, npt;
946 const char *range = httpd_MsgGet (query, "Range");
949 if (strncmp (range, "npt=", 4))
951 answer->i_status = 501;
955 start = ParseNPT (range + 4);
956 range = strchr(range, '-');
957 if (range != NULL && *(range + 1))
958 end = ParseNPT (range + 1);
960 if (end >= 0 && end < start)
962 answer->i_status = 457;
968 if (vod_check_range(rtsp->vod_media, psz_session,
969 start, end) != VLC_SUCCESS)
971 answer->i_status = 457;
975 /* We accept start times of 0 even for broadcast streams
976 * that already started */
977 else if (start > 0 || end >= 0)
979 answer->i_status = 456;
983 vlc_mutex_lock( &rtsp->lock );
984 ses = RtspClientGet( rtsp, psz_session );
987 char info[ses->trackc * ( strlen( control ) + TRACK_PATH_SIZE
988 + sizeof("url=;seq=65535;rtptime=4294967295, ")
991 RtspClientAlive(ses);
993 sout_stream_id_t *sout_id = NULL;
996 /* We don't keep a reference to the sout_stream_t,
997 * so we check if a sout_id is available instead. */
998 for (int i = 0; i < ses->trackc; i++)
1000 sout_id = ses->trackv[i].sout_id;
1001 if (sout_id != NULL)
1005 int64_t ts = rtp_get_ts(vod ? NULL : (sout_stream_t *)owner,
1006 sout_id, rtsp->vod_media, psz_session,
1009 for( int i = 0; i < ses->trackc; i++ )
1011 rtsp_strack_t *tr = ses->trackv + i;
1012 if( ( id == NULL ) || ( tr->id == id ) )
1014 if (tr->setup_fd == -1)
1015 /* Track not SETUP */
1019 if( tr->rtp_fd == -1 )
1021 /* Track not PLAYing yet */
1022 if (tr->sout_id == NULL)
1023 /* Instance not running yet (VoD) */
1027 /* Instance running, add a sink to it */
1028 tr->rtp_fd = dup_socket(tr->setup_fd);
1029 if (tr->rtp_fd == -1)
1032 rtp_add_sink( tr->sout_id, tr->rtp_fd,
1038 /* Track already playing */
1039 assert( tr->sout_id != NULL );
1040 seq = rtp_get_seq( tr->sout_id );
1042 char *url = RtspAppendTrackPath( tr->id, control );
1043 infolen += sprintf( info + infolen,
1044 "url=%s;seq=%u;rtptime=%u, ",
1045 url != NULL ? url : "", seq,
1046 rtp_compute_ts( tr->id->clock_rate, ts ) );
1052 info[infolen - 2] = '\0'; /* remove trailing ", " */
1053 httpd_MsgAdd( answer, "RTP-Info", "%s", info );
1057 vod_play(rtsp->vod_media, psz_session, &start, end);
1061 vlc_mutex_unlock( &rtsp->lock );
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 );
1096 vod_pause(rtsp->vod_media, psz_session, &npt);
1097 double f_npt = (double) npt / CLOCK_FREQ;
1098 httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
1100 else /* "Mute" the selected track */
1103 for (int i = 0; i < ses->trackc; i++)
1105 rtsp_strack_t *tr = ses->trackv + i;;
1108 if (tr->setup_fd == -1)
1112 if (tr->rtp_fd != -1)
1114 rtp_del_sink(tr->sout_id, tr->rtp_fd);
1121 answer->i_status = 455;
1123 RtspClientAlive(ses);
1125 vlc_mutex_unlock( &rtsp->lock );
1129 case HTTPD_MSG_GETPARAMETER:
1130 if( query->i_body > 0 )
1132 answer->i_status = 451;
1136 psz_session = httpd_MsgGet( query, "Session" );
1137 answer->i_status = 200;
1138 vlc_mutex_lock( &rtsp->lock );
1139 rtsp_session_t *ses = RtspClientGet( rtsp, psz_session );
1141 RtspClientAlive(ses);
1142 vlc_mutex_unlock( &rtsp->lock );
1145 case HTTPD_MSG_TEARDOWN:
1147 rtsp_session_t *ses;
1149 answer->i_status = 200;
1151 psz_session = httpd_MsgGet( query, "Session" );
1153 vlc_mutex_lock( &rtsp->lock );
1154 ses = RtspClientGet( rtsp, psz_session );
1157 if( id == NULL ) /* Delete the entire session */
1159 RtspClientDel( rtsp, ses );
1161 vod_stop(rtsp->vod_media, psz_session);
1162 RtspUpdateTimer(rtsp);
1164 else /* Delete one track from the session */
1166 for( int i = 0; i < ses->trackc; i++ )
1168 if( ses->trackv[i].id == id )
1170 RtspTrackClose( &ses->trackv[i] );
1171 /* Keep VoD tracks whose instance is still
1173 if (!(vod && ses->trackv[i].sout_id != NULL))
1174 REMOVE_ELEM( ses->trackv, ses->trackc, i );
1177 RtspClientAlive(ses);
1180 vlc_mutex_unlock( &rtsp->lock );
1185 return VLC_EGENERIC;
1190 if (rtsp->timeout > 0)
1191 httpd_MsgAdd( answer, "Session", "%s;timeout=%d", psz_session,
1194 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1197 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1198 httpd_MsgAdd( answer, "Cache-Control", "no-cache" );
1200 psz = httpd_MsgGet( query, "Cseq" );
1202 httpd_MsgAdd( answer, "Cseq", "%s", psz );
1203 psz = httpd_MsgGet( query, "Timestamp" );
1205 httpd_MsgAdd( answer, "Timestamp", "%s", psz );
1211 /** Aggregate RTSP callback */
1212 static int RtspCallback( httpd_callback_sys_t *p_args,
1214 httpd_message_t *answer,
1215 const httpd_message_t *query )
1217 return RtspHandler( (rtsp_stream_t *)p_args, NULL, cl, answer, query );
1221 /** Non-aggregate RTSP callback */
1222 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1224 httpd_message_t *answer,
1225 const httpd_message_t *query )
1227 rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
1228 return RtspHandler( id->stream, id, cl, answer, query );