]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtsp.c
avcodec: remove support for old versions
[vlc] / modules / stream_out / rtsp.c
index c463746c478ce64c80349f267bc2bc4c98c076cc..cece5bc85e94565deb5b2a66f52a1b5b0e30e019 100644 (file)
@@ -43,6 +43,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <time.h>
 
 #ifndef WIN32
 # include <locale.h>
@@ -64,7 +65,6 @@ struct rtsp_stream_t
     httpd_url_t    *url;
     char           *psz_path;
     unsigned        track_id;
-    unsigned        port;
 
     int             sessionc;
     rtsp_session_t **sessionv;
@@ -85,11 +85,11 @@ static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
 static void RtspTimeOut( void *data );
 
 rtsp_stream_t *RtspSetup( vlc_object_t *owner, vod_media_t *media,
-                          const vlc_url_t *url )
+                          const char *path )
 {
     rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
 
-    if( rtsp == NULL || ( url->i_port > 99999 ) )
+    if( rtsp == NULL )
     {
         free( rtsp );
         return NULL;
@@ -112,21 +112,22 @@ rtsp_stream_t *RtspSetup( vlc_object_t *owner, vod_media_t *media,
             goto error;
     }
 
-    rtsp->port = (url->i_port > 0) ? url->i_port : 554;
-    rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
+    rtsp->psz_path = strdup( (path != NULL) ? path : "/" );
     if( rtsp->psz_path == NULL )
         goto error;
 
-    msg_Dbg( owner, "RTSP stream: host %s port %d at %s",
-             url->psz_host, rtsp->port, rtsp->psz_path );
+    msg_Dbg( owner, "RTSP stream at %s", rtsp->psz_path );
 
-    rtsp->host = httpd_HostNew( VLC_OBJECT(owner), url->psz_host,
-                                rtsp->port );
+    rtsp->host = vlc_rtsp_HostNew( VLC_OBJECT(owner) );
     if( rtsp->host == NULL )
         goto error;
 
-    rtsp->url = httpd_UrlNewUnique( rtsp->host, rtsp->psz_path,
-                                    NULL, NULL, NULL );
+    char *user = var_InheritString(owner, "sout-rtsp-user");
+    char *pwd = var_InheritString(owner, "sout-rtsp-pwd");
+
+    rtsp->url = httpd_UrlNew( rtsp->host, rtsp->psz_path, user, pwd );
+    free(user);
+    free(pwd);
     if( rtsp->url == NULL )
         goto error;
 
@@ -170,13 +171,11 @@ struct rtsp_stream_id_t
 {
     rtsp_stream_t    *stream;
     sout_stream_id_t *sout_id;
-    unsigned          clock_rate; /* needed to compute rtptime in RTP-Info */
     httpd_url_t      *url;
-    const char       *dst;
-    int               ttl;
     unsigned          track_id;
     uint32_t          ssrc;
-    uint16_t          loport, hiport;
+    unsigned          clock_rate; /* needed to compute rtptime in RTP-Info */
+    int               mcast_fd;
 };
 
 
@@ -188,7 +187,6 @@ struct rtsp_session_t
     rtsp_stream_t *stream;
     uint64_t       id;
     mtime_t        last_seen; /* for timeouts */
-    bool           vod_started; /* true if the VoD media instance was created */
 
     /* output (id-access) */
     int            trackc;
@@ -201,15 +199,16 @@ struct rtsp_strack_t
 {
     rtsp_stream_id_t  *id;
     sout_stream_id_t  *sout_id;
-    int          setup_fd;       /* socket created by the SETUP request */
-    int          rtp_fd;         /* socket used by the RTP output */
+    int          setup_fd;  /* socket created by the SETUP request */
+    int          rtp_fd;    /* socket used by the RTP output, when playing */
     uint32_t     ssrc;
     uint16_t     seq_init;
-    bool         playing;
 };
 
 static void RtspTrackClose( rtsp_strack_t *tr );
 
+#define TRACK_PATH_SIZE (sizeof("/trackID=999") - 1)
+
 char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
 {
     const char *sep = strlen( base ) > 0 && base[strlen( base ) - 1] == '/' ?
@@ -224,10 +223,14 @@ char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
 
 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
                              uint32_t ssrc, unsigned clock_rate,
-                             /* Multicast stuff - TODO: cleanup */
-                             const char *dst, int ttl,
-                             unsigned loport, unsigned hiport )
+                             int mcast_fd)
 {
+    if (rtsp->track_id > 999)
+    {
+        msg_Err(rtsp->owner, "RTSP: too many IDs!");
+        return NULL;
+    }
+
     char *urlbuf;
     rtsp_stream_id_t *id = malloc( sizeof( *id ) );
     httpd_url_t *url;
@@ -240,14 +243,7 @@ rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
     id->track_id = rtsp->track_id;
     id->ssrc = ssrc;
     id->clock_rate = clock_rate;
-    /* TODO: can we assume that this need not be strdup'd? */
-    id->dst = dst;
-    if( id->dst != NULL )
-    {
-        id->ttl = ttl;
-        id->loport = loport;
-        id->hiport = hiport;
-    }
+    id->mcast_fd = mcast_fd;
 
     urlbuf = RtspAppendTrackPath( id, rtsp->psz_path );
     if( urlbuf == NULL )
@@ -257,7 +253,13 @@ rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
     }
 
     msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
-    url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
+
+    char *user = var_InheritString(rtsp->owner, "sout-rtsp-user");
+    char *pwd = var_InheritString(rtsp->owner, "sout-rtsp-pwd");
+
+    url = id->url = httpd_UrlNew( rtsp->host, urlbuf, user, pwd );
+    free( user );
+    free( pwd );
     free( urlbuf );
 
     if( url == NULL )
@@ -357,7 +359,6 @@ rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
 
     s->stream = rtsp;
     vlc_rand_bytes (&s->id, sizeof (s->id));
-    s->vod_started = false;
     s->trackc = 0;
     s->trackv = NULL;
 
@@ -418,6 +419,19 @@ static void RtspClientAlive( rtsp_session_t *session )
     RtspUpdateTimer(session->stream);
 }
 
+static int dup_socket(int oldfd)
+{
+    int newfd;
+#ifndef WIN32
+    newfd = vlc_dup(oldfd);
+#else
+    WSAPROTOCOL_INFO info;
+    WSADuplicateSocket (oldfd, GetCurrentProcessId (), &info);
+    newfd = WSASocket (info.iAddressFamily, info.iSocketType,
+                       info.iProtocol, &info, 0, 0);
+#endif
+    return newfd;
+}
 
 /* Attach a starting VoD RTP id to its RTSP track, and let it
  * initialize with the parameters of the SETUP request */
@@ -434,46 +448,48 @@ int RtspTrackAttach( rtsp_stream_t *rtsp, const char *name,
     if (session == NULL)
         goto out;
 
-    for (int i = 0; session->trackc; i++)
+    rtsp_strack_t *tr = NULL;
+    for (int i = 0; i < session->trackc; i++)
     {
-        rtsp_strack_t *tr = session->trackv + i;
-        if (tr->id == id)
+        if (session->trackv[i].id == id)
         {
-            int rtp_fd;
-#if !defined(WIN32) || defined(UNDER_CE)
-            rtp_fd = vlc_dup(tr->setup_fd);
-#else
-            WSAPROTOCOL_INFO info;
-            WSADuplicateSocket (tr->setup_fd, GetCurrentProcessId (), &info);
-            rtp_fd = WSASocket (info.iAddressFamily, info.iSocketType,
-                                info.iProtocol, &info, 0, 0);
-#endif
-            if (rtp_fd == -1)
-                break;
-
-            /* Ignore any unexpected incoming packet */
-            /* XXX: is this needed again? */
-            setsockopt (rtp_fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
-                        sizeof (int));
-
-            uint16_t seq;
-            *ssrc = ntohl(tr->ssrc);
-            *seq_init = tr->seq_init;
-            rtp_add_sink(sout_id, rtp_fd, false, &seq);
-            /* To avoid race conditions, sout_id->i_seq_sent_next must
-             * be set here and now. Make sure the caller did its job
-             * properly when passing seq_init. */
-            assert(tr->seq_init == seq);
-
-            tr->rtp_fd = rtp_fd;
-            tr->sout_id = sout_id;
-            tr->playing = true;
-
-            val = VLC_SUCCESS;
+            tr = session->trackv + i;
             break;
         }
     }
 
+    if (tr != NULL)
+    {
+        tr->sout_id = sout_id;
+        tr->rtp_fd = dup_socket(tr->setup_fd);
+    }
+    else
+    {
+        /* The track was not SETUP. We still create one because we'll
+         * need the sout_id if we set it up later. */
+        rtsp_strack_t track = { .id = id, .sout_id = sout_id,
+                                .setup_fd = -1, .rtp_fd = -1 };
+        vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
+        vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
+
+        INSERT_ELEM(session->trackv, session->trackc, session->trackc, track);
+        tr = session->trackv + session->trackc - 1;
+    }
+
+    *ssrc = ntohl(tr->ssrc);
+    *seq_init = tr->seq_init;
+
+    if (tr->rtp_fd != -1)
+    {
+        uint16_t seq;
+        rtp_add_sink(tr->sout_id, tr->rtp_fd, false, &seq);
+        /* To avoid race conditions, sout_id->i_seq_sent_next must
+         * be set here and now. Make sure the caller did its job
+         * properly when passing seq_init. */
+        assert(tr->seq_init == seq);
+    }
+
+    val = VLC_SUCCESS;
 out:
     vlc_mutex_unlock(&rtsp->lock);
     return val;
@@ -492,14 +508,26 @@ void RtspTrackDetach( rtsp_stream_t *rtsp, const char *name,
     if (session == NULL)
         goto out;
 
-    for (int i = 0; session->trackc; i++)
+    for (int i = 0; i < session->trackc; i++)
     {
         rtsp_strack_t *tr = session->trackv + i;
         if (tr->sout_id == sout_id)
         {
+            if (tr->setup_fd == -1)
+            {
+                /* No (more) SETUP information: better get rid of the
+                 * track so that we can have new random ssrc and
+                 * seq_init next time. */
+                REMOVE_ELEM( session->trackv, session->trackc, i );
+                break;
+            }
+            /* We keep the SETUP information of the track, but stop it */
+            if (tr->rtp_fd != -1)
+            {
+                rtp_del_sink(tr->sout_id, tr->rtp_fd);
+                tr->rtp_fd = -1;
+            }
             tr->sout_id = NULL;
-            tr->playing = false;
-            rtp_del_sink(sout_id, tr->rtp_fd);
             break;
         }
     }
@@ -512,11 +540,16 @@ out:
 /** rtsp must be locked */
 static void RtspTrackClose( rtsp_strack_t *tr )
 {
-    if (tr->sout_id != NULL)
-        rtp_del_sink(tr->sout_id, tr->rtp_fd);
-    /* rtp_fd is duplicated from setup_fd only in VoD mode. */
-    if (tr->id->stream->vod_media != NULL)
+    if (tr->setup_fd != -1)
+    {
+        if (tr->rtp_fd != -1)
+        {
+            rtp_del_sink(tr->sout_id, tr->rtp_fd);
+            tr->rtp_fd = -1;
+        }
         net_Close(tr->setup_fd);
+        tr->setup_fd = -1;
+    }
 }
 
 
@@ -557,14 +590,14 @@ static int64_t ParseNPT (const char *str)
         sec += ((hour * 60) + min) * 60;
     else
     if (sscanf (str, "%f", &sec) != 1)
-        sec = 0.;
+        sec = -1;
 
     if (loc != (locale_t)0)
     {
         uselocale (oldloc);
         freelocale (loc);
     }
-    return sec * CLOCK_FREQ;
+    return sec < 0 ? -1 : sec * CLOCK_FREQ;
 }
 
 
@@ -593,18 +626,17 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
     {
         /* Build self-referential control URL */
         char ip[NI_MAXNUMERICHOST], *ptr;
+        int port;
 
-        httpd_ServerIP( cl, ip );
+        httpd_ServerIP( cl, ip, &port );
         ptr = strchr( ip, '%' );
         if( ptr != NULL )
             *ptr = '\0';
 
         if( strchr( ip, ':' ) != NULL )
-            sprintf( control, "rtsp://[%s]:%u%s", ip, rtsp->port,
-                     rtsp->psz_path );
+            sprintf( control, "rtsp://[%s]:%d%s", ip, port, rtsp->psz_path );
         else
-            sprintf( control, "rtsp://%s:%u%s", ip, rtsp->port,
-                     rtsp->psz_path );
+            sprintf( control, "rtsp://%s:%d%s", ip, port, rtsp->psz_path );
     }
 
     /* */
@@ -682,7 +714,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                  tpt = transport_next( tpt ) )
             {
                 bool b_multicast = true, b_unsupp = false;
-                unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
+                unsigned loport = 5004, hiport; /* from RFC3551 */
 
                 /* Check transport protocol. */
                 /* Currently, we only support RTP/AVP over UDP */
@@ -750,10 +782,19 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
 
                 if( b_multicast )
                 {
-                    const char *dst = id->dst;
-                    if( dst == NULL )
+                    char dst[NI_MAXNUMERICHOST];
+                    int dport, ttl;
+                    if( id->mcast_fd == -1 )
                         continue;
 
+                    net_GetPeerAddress(id->mcast_fd, dst, &dport);
+
+                    ttl = var_InheritInteger(owner, "ttl");
+                    if (ttl <= 0)
+                    /* FIXME: the TTL is left to the OS default, we can
+                     * only guess that it's 1. */
+                        ttl = 1;
+
                     if( psz_session == NULL )
                     {
                         /* Create a dummy session ID */
@@ -766,8 +807,8 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     httpd_MsgAdd( answer, "Transport",
                                   "RTP/AVP/UDP;destination=%s;port=%u-%u;"
                                   "ttl=%d;mode=play",
-                                  dst, id->loport, id->hiport,
-                                  ( id->ttl > 0 ) ? id->ttl : 1 );
+                                  dst, dport, dport + 1, ttl );
+                     /* FIXME: this doesn't work with RTP + RTCP mux */
                 }
                 else
                 {
@@ -776,7 +817,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     int fd, sport;
                     uint32_t ssrc;
 
-                    if( httpd_ClientIP( cl, ip ) == NULL )
+                    if( httpd_ClientIP( cl, ip, NULL ) == NULL )
                     {
                         answer->i_status = 500;
                         continue;
@@ -798,21 +839,6 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                                 sizeof (int));
                     net_GetSockAddress( fd, src, &sport );
 
-                    rtsp_strack_t track = { .id = id, .sout_id = id->sout_id,
-                                            .setup_fd = fd, .playing = false };
-
-                    if (vod)
-                    {
-                        vlc_rand_bytes (&track.seq_init, sizeof (track.seq_init));
-                        vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
-                        ssrc = track.ssrc;
-                    }
-                    else
-                    {
-                        track.rtp_fd = track.setup_fd;
-                        ssrc = id->ssrc;
-                    }
-
                     vlc_mutex_lock( &rtsp->lock );
                     if( psz_session == NULL )
                     {
@@ -834,55 +860,74 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     }
                     RtspClientAlive(ses);
 
-                    /* Bail if the track is already set up: we don't
-                     * support changing the transport parameters on the
-                     * fly */
-                    bool setup = false;
+                    rtsp_strack_t *tr = NULL;
                     for (int i = 0; i < ses->trackc; i++)
                     {
                         if (ses->trackv[i].id == id)
                         {
-                            setup = true;
+                            tr = ses->trackv + i;
                             break;
                         }
                     }
-                    if (setup)
+
+                    if (tr == NULL)
+                    {
+                        /* Set up a new track */
+                        rtsp_strack_t track = { .id = id,
+                                                .sout_id = id->sout_id,
+                                                .setup_fd = fd,
+                                                .rtp_fd = -1 };
+
+                        if (vod)
+                        {
+                            vlc_rand_bytes (&track.seq_init,
+                                            sizeof (track.seq_init));
+                            vlc_rand_bytes (&track.ssrc, sizeof (track.ssrc));
+                            ssrc = track.ssrc;
+                        }
+                        else
+                            ssrc = id->ssrc;
+
+                        INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
+                                     track );
+                    }
+                    else if (tr->setup_fd == -1)
                     {
+                        /* The track was not SETUP, but it exists
+                         * because there is a sout_id running for it */
+                        tr->setup_fd = fd;
+                        ssrc = tr->ssrc;
+                    }
+                    else
+                    {
+                        /* The track is already set up, and we don't
+                         * support changing the transport parameters on
+                         * the fly */
                         vlc_mutex_unlock( &rtsp->lock );
                         answer->i_status = 455;
                         net_Close( fd );
                         break;
                     }
-
-                    INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc,
-                                 track );
                     vlc_mutex_unlock( &rtsp->lock );
 
-                    httpd_ServerIP( cl, ip );
+                    httpd_ServerIP( cl, ip, NULL );
 
+                    /* Specify source IP only if it is different from the
+                     * RTSP control connection server address */
                     if( strcmp( src, ip ) )
                     {
-                        /* Specify source IP if it is different from the RTSP
-                         * control connection server address */
                         char *ptr = strchr( src, '%' );
                         if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
-
-                        httpd_MsgAdd( answer, "Transport",
-                                      "RTP/AVP/UDP;unicast;source=%s;"
-                                      "client_port=%u-%u;server_port=%u-%u;"
-                                      "ssrc=%08X;mode=play",
-                                      src, loport, loport + 1, sport,
-                                      sport + 1, ssrc );
                     }
                     else
-                    {
-                        httpd_MsgAdd( answer, "Transport",
-                                      "RTP/AVP/UDP;unicast;"
-                                      "client_port=%u-%u;server_port=%u-%u;"
-                                      "ssrc=%08X;mode=play",
-                                      loport, loport + 1, sport, sport + 1,
-                                      ssrc );
-                    }
+                        src[0] = '\0';
+
+                    httpd_MsgAdd( answer, "Transport",
+                                  "RTP/AVP/UDP;unicast%s%s;"
+                                  "client_port=%u-%u;server_port=%u-%u;"
+                                  "ssrc=%08X;mode=play",
+                                  src[0] ? ";source=" : "", src,
+                                  loport, loport + 1, sport, sport + 1, ssrc );
 
                     answer->i_status = 200;
                 }
@@ -896,23 +941,51 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
             answer->i_status = 200;
 
             psz_session = httpd_MsgGet( query, "Session" );
+            int64_t start = -1, end = -1, npt;
             const char *range = httpd_MsgGet (query, "Range");
-            if (range != NULL && strncmp (range, "npt=", 4))
+            if (range != NULL)
             {
-                answer->i_status = 501;
-                break;
-            }
+                if (strncmp (range, "npt=", 4))
+                {
+                    answer->i_status = 501;
+                    break;
+                }
+
+                start = ParseNPT (range + 4);
+                range = strchr(range, '-');
+                if (range != NULL && *(range + 1))
+                    end = ParseNPT (range + 1);
 
+                if (end >= 0 && end < start)
+                {
+                    answer->i_status = 457;
+                    break;
+                }
+
+                if (vod)
+                {
+                    if (vod_check_range(rtsp->vod_media, psz_session,
+                                        start, end) != VLC_SUCCESS)
+                    {
+                        answer->i_status = 457;
+                        break;
+                    }
+                }
+                /* We accept start times of 0 even for broadcast streams
+                 * that already started */
+                else if (start > 0 || end >= 0)
+                {
+                    answer->i_status = 456;
+                    break;
+                }
+            }
             vlc_mutex_lock( &rtsp->lock );
             ses = RtspClientGet( rtsp, psz_session );
             if( ses != NULL )
             {
-                /* The "trackID" part must match what is done in
-                 * RtspAppendTrackPath() */
-                /* FIXME: we really need to limit the number of tracks... */
-                char info[ses->trackc * ( strlen( control )
-                              + sizeof("url=/trackID=123;seq=65535;"
-                                       "rtptime=4294967295, ") ) + 1];
+                char info[ses->trackc * ( strlen( control ) + TRACK_PATH_SIZE
+                          + sizeof("url=;seq=65535;rtptime=4294967295, ")
+                                          - 1 ) + 1];
                 size_t infolen = 0;
                 RtspClientAlive(ses);
 
@@ -920,10 +993,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                 if (vod)
                 {
                     /* We don't keep a reference to the sout_stream_t,
-                     * so we check if a sout_id is available instead.
-                     * FIXME: this is broken if the stream is still
-                     * running but with no track set up; but this case
-                     * is already broken anyway (see below). */
+                     * so we check if a sout_id is available instead. */
                     for (int i = 0; i < ses->trackc; i++)
                     {
                         sout_id = ses->trackv[i].sout_id;
@@ -932,31 +1002,39 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     }
                 }
                 int64_t ts = rtp_get_ts(vod ? NULL : (sout_stream_t *)owner,
-                                        sout_id, rtsp->vod_media, psz_session);
+                                        sout_id, rtsp->vod_media, psz_session,
+                                        vod ? NULL : &npt);
 
                 for( int i = 0; i < ses->trackc; i++ )
                 {
                     rtsp_strack_t *tr = ses->trackv + i;
                     if( ( id == NULL ) || ( tr->id == id ) )
                     {
+                        if (tr->setup_fd == -1)
+                            /* Track not SETUP */
+                            continue;
+
                         uint16_t seq;
-                        if( !tr->playing )
+                        if( tr->rtp_fd == -1 )
                         {
-                            if (vod)
-                                /* TODO: if the RTP stream output is already
-                                 * started, it won't pick up newly set-up
-                                 * tracks, so we need to call rtp_add_sink()
-                                 * or something. */
+                            /* Track not PLAYing yet */
+                            if (tr->sout_id == NULL)
+                                /* Instance not running yet (VoD) */
                                 seq = tr->seq_init;
                             else
                             {
-                                tr->playing = true;
+                                /* Instance running, add a sink to it */
+                                tr->rtp_fd = dup_socket(tr->setup_fd);
+                                if (tr->rtp_fd == -1)
+                                    continue;
+
                                 rtp_add_sink( tr->sout_id, tr->rtp_fd,
                                               false, &seq );
                             }
                         }
                         else
                         {
+                            /* Track already playing */
                             assert( tr->sout_id != NULL );
                             seq = rtp_get_seq( tr->sout_id );
                         }
@@ -973,27 +1051,20 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     info[infolen - 2] = '\0'; /* remove trailing ", " */
                     httpd_MsgAdd( answer, "RTP-Info", "%s", info );
                 }
+            }
+            vlc_mutex_unlock( &rtsp->lock );
+
+            if (ses != NULL)
+            {
                 if (vod)
                 {
-                    /* TODO: fix that crap, this is barely RTSP */
-                    if (!ses->vod_started)
-                    {
-                        vod_start(rtsp->vod_media, psz_session);
-                        ses->vod_started = true;
-                    }
-                    else
-                    {
-                        if (range != NULL)
-                        {
-                            int64_t time = ParseNPT (range + 4);
-                            vod_seek(rtsp->vod_media, psz_session, time);
-                        }
-                        /* This is the thing to do to unpause... */
-                        vod_start(rtsp->vod_media, psz_session);
-                    }
+                    vod_play(rtsp->vod_media, psz_session, &start, end);
+                    npt = start;
                 }
+
+                double f_npt = (double) npt / CLOCK_FREQ;
+                httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
             }
-            vlc_mutex_unlock( &rtsp->lock );
 
             if( httpd_MsgGet( query, "Scale" ) != NULL )
                 httpd_MsgAdd( answer, "Scale", "1." );
@@ -1002,7 +1073,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
 
         case HTTPD_MSG_PAUSE:
         {
-            if (!vod)
+            if (id == NULL && !vod)
             {
                 answer->i_status = 405;
                 httpd_MsgAdd( answer, "Allow",
@@ -1018,10 +1089,41 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
             ses = RtspClientGet( rtsp, psz_session );
             if (ses != NULL)
             {
-                vod_pause(rtsp->vod_media, psz_session);
+                if (id != NULL) /* "Mute" the selected track */
+                {
+                    bool found = false;
+                    for (int i = 0; i < ses->trackc; i++)
+                    {
+                        rtsp_strack_t *tr = ses->trackv + i;;
+                        if (tr->id == id)
+                        {
+                            if (tr->setup_fd == -1)
+                                break;
+
+                            found = true;
+                            if (tr->rtp_fd != -1)
+                            {
+                                rtp_del_sink(tr->sout_id, tr->rtp_fd);
+                                tr->rtp_fd = -1;
+                            }
+                            break;
+                        }
+                    }
+                    if (!found)
+                        answer->i_status = 455;
+                }
                 RtspClientAlive(ses);
             }
             vlc_mutex_unlock( &rtsp->lock );
+
+            if (ses != NULL && id == NULL)
+            {
+                assert(vod);
+                int64_t npt = 0;
+                vod_pause(rtsp->vod_media, psz_session, &npt);
+                double f_npt = (double) npt / CLOCK_FREQ;
+                httpd_MsgAdd( answer, "Range", "npt=%f-", f_npt );
+            }
             break;
         }
 
@@ -1067,7 +1169,10 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                         if( ses->trackv[i].id == id )
                         {
                             RtspTrackClose( &ses->trackv[i] );
-                            REMOVE_ELEM( ses->trackv, ses->trackc, i );
+                            /* Keep VoD tracks whose instance is still
+                             * running */
+                            if (!(vod && ses->trackv[i].sout_id != NULL))
+                                REMOVE_ELEM( ses->trackv, ses->trackc, i );
                         }
                     }
                     RtspClientAlive(ses);