]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtsp.c
rtp sout: fix RTSP track numbering in SDP
[vlc] / modules / stream_out / rtsp.c
index f5872afa7f19011ac20acad232fc4ce8052e5ea5..1bc02bc6c381d5b27d760bb2326a6ee3d0186fe4 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2003-2004 the VideoLAN team
  * Copyright © 2007 Rémi Denis-Courmont
  *
- * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
@@ -30,7 +30,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_sout.h>
 
 #include <vlc_httpd.h>
@@ -52,7 +52,8 @@ struct rtsp_stream_t
     httpd_host_t   *host;
     httpd_url_t    *url;
     char           *psz_path;
-    const char     *track_fmt;
+    const char     *track_sep;
+    unsigned        track_id;
     unsigned        port;
 
     int             sessionc;
@@ -84,7 +85,8 @@ rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
     rtsp->host = NULL;
     rtsp->url = NULL;
     rtsp->psz_path = NULL;
-    vlc_mutex_init( p_stream, &rtsp->lock );
+    rtsp->track_id = 0;
+    vlc_mutex_init( &rtsp->lock );
 
     rtsp->port = (url->i_port > 0) ? url->i_port : 554;
     rtsp->psz_path = strdup( ( url->psz_path != NULL ) ? url->psz_path : "/" );
@@ -92,10 +94,8 @@ rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
         goto error;
 
     assert( strlen( rtsp->psz_path ) > 0 );
-    if( rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' )
-        rtsp->track_fmt = "%strackID=%u";
-    else
-        rtsp->track_fmt = "%s/trackID=%u";
+    rtsp->track_sep = rtsp->psz_path[strlen( rtsp->psz_path ) - 1] == '/' ?
+                      "" : "/";
 
     msg_Dbg( p_stream, "RTSP stream: host %s port %d at %s",
              url->psz_host, rtsp->port, rtsp->psz_path );
@@ -138,6 +138,8 @@ void RtspUnsetup( rtsp_stream_t *rtsp )
 
     free( rtsp->psz_path );
     vlc_mutex_destroy( &rtsp->lock );
+
+    free( rtsp );
 }
 
 
@@ -148,6 +150,7 @@ struct rtsp_stream_id_t
     httpd_url_t      *url;
     const char       *dst;
     int               ttl;
+    unsigned          track_id;
     uint32_t          ssrc;
     uint16_t          loport, hiport;
 };
@@ -170,14 +173,27 @@ struct rtsp_session_t
 /* Unicast session track */
 struct rtsp_strack_t
 {
-    sout_stream_id_t  *id;
+    rtsp_stream_id_t  *id;
     int                fd;
-    vlc_bool_t         playing;
+    bool         playing;
 };
 
 
+char *RtspAppendTrackPath( rtsp_stream_id_t *id, const char *base )
+{
+    assert( ( strlen( base ) > 0 && base[strlen( base ) - 1] == '/' )
+            ^ ( id->stream->track_sep[0] == '/' ) );
+
+    char *url;
+    if( asprintf( &url, "%s%strackID=%u", base, id->stream->track_sep,
+                  id->track_id ) == -1 )
+        url = NULL;
+    return url;
+}
+
+
 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
-                             unsigned num, uint32_t ssrc,
+                             uint32_t ssrc,
                              /* Multicast stuff - TODO: cleanup */
                              const char *dst, int ttl,
                              unsigned loport, unsigned hiport )
@@ -191,6 +207,7 @@ rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
 
     id->stream = rtsp;
     id->sout_id = sid;
+    id->track_id = rtsp->track_id;
     id->ssrc = ssrc;
     /* TODO: can we assume that this need not be strdup'd? */
     id->dst = dst;
@@ -201,9 +218,8 @@ rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
         id->hiport = hiport;
     }
 
-    /* FIXME: num screws up if any ES has been removed and re-added */
-    snprintf( urlbuf, sizeof( urlbuf ), rtsp->track_fmt, rtsp->psz_path,
-              num );
+    snprintf( urlbuf, sizeof( urlbuf ), "%s%strackID=%u", rtsp->psz_path,
+              rtsp->track_sep, id->track_id );
     msg_Dbg( rtsp->owner, "RTSP: adding %s", urlbuf );
     url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
 
@@ -220,6 +236,8 @@ rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
     httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
     httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
 
+    rtsp->track_id++;
+
     return id;
 }
 
@@ -233,7 +251,7 @@ void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
 
         for( int j = 0; j < ses->trackc; j++ )
         {
-            if( ses->trackv[j].id == id->sout_id )
+            if( ses->trackv[j].id == id )
             {
                 rtsp_strack_t *tr = ses->trackv + j;
                 net_Close( tr->fd );
@@ -301,7 +319,7 @@ void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
     TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
 
     for( i = 0; i < session->trackc; i++ )
-        rtp_del_sink( session->trackv[i].id, session->trackv[i].fd );
+        rtp_del_sink( session->trackv[i].id->sout_id, session->trackv[i].fd );
 
     free( session->trackv );
     free( session );
@@ -348,6 +366,9 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
     const char *psz_session = NULL, *psz;
     char control[sizeof("rtsp://[]:12345") + NI_MAXNUMERICHOST
                   + strlen( rtsp->psz_path )];
+    time_t now;
+
+    time (&now);
 
     if( answer == NULL || query == NULL || cl == NULL )
         return VLC_SUCCESS;
@@ -376,6 +397,22 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
     answer->i_body = 0;
     answer->p_body = NULL;
 
+    httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
+
+    /* Date: is always allowed, and sometimes mandatory with RTSP/2.0. */
+    struct tm ut;
+    if (gmtime_r (&now, &ut) != NULL)
+    {   /* RFC1123 format, GMT is mandatory */
+        static const char wdays[7][4] = {
+            "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+        static const char mons[12][4] = {
+            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+        httpd_MsgAdd (answer, "Date", "%s, %02u %s %04u %02u:%02u:%02u GMT",
+                      wdays[ut.tm_wday], ut.tm_mday, mons[ut.tm_mon],
+                      1900 + ut.tm_year, ut.tm_hour, ut.tm_min, ut.tm_sec);
+    }
+
     if( query->i_proto != HTTPD_PROTO_RTSP )
     {
         answer->i_status = 505;
@@ -424,7 +461,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                  tpt != NULL;
                  tpt = transport_next( tpt ) )
             {
-                vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
+                bool b_multicast = true, b_unsupp = false;
                 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
 
                 /* Check transport protocol. */
@@ -443,10 +480,10 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                      opt = parameter_next( opt ) )
                 {
                     if( strncmp( opt, "multicast", 9 ) == 0)
-                        b_multicast = VLC_TRUE;
+                        b_multicast = true;
                     else
                     if( strncmp( opt, "unicast", 7 ) == 0 )
-                        b_multicast = VLC_FALSE;
+                        b_multicast = false;
                     else
                     if( sscanf( opt, "client_port=%u-%u", &loport, &hiport )
                                 == 2 )
@@ -458,7 +495,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                          && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
                         {
                             /* Not playing?! */
-                            b_unsupp = VLC_TRUE;
+                            b_unsupp = true;
                             break;
                         }
                     }
@@ -466,7 +503,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     if( strncmp( opt,"destination=", 12 ) == 0 )
                     {
                         answer->i_status = 403;
-                        b_unsupp = VLC_TRUE;
+                        b_unsupp = true;
                     }
                     else
                     {
@@ -483,7 +520,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                      *
                      * "interleaved" is not implemented.
                      */
-                        b_unsupp = VLC_TRUE;
+                        b_unsupp = true;
                         break;
                     }
                 }
@@ -516,7 +553,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                 {
                     char ip[NI_MAXNUMERICHOST], src[NI_MAXNUMERICHOST];
                     rtsp_session_t *ses = NULL;
-                    rtsp_strack_t track = { id->sout_id, -1, VLC_FALSE };
+                    rtsp_strack_t track = { id, -1, false };
                     int sport;
 
                     if( httpd_ClientIP( cl, ip ) == NULL )
@@ -536,13 +573,16 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                         continue;
                     }
 
+                    /* Ignore any unexpected incoming packet */
+                    setsockopt (track.fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
+                                sizeof (int));
                     net_GetSockAddress( track.fd, src, &sport );
 
                     vlc_mutex_lock( &rtsp->lock );
                     if( psz_session == NULL )
                     {
                         ses = RtspClientNew( rtsp );
-                        snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
+                        snprintf( psz_sesbuf, sizeof( psz_sesbuf ), "%"PRIx64,
                                   ses->id );
                         psz_session = psz_sesbuf;
                     }
@@ -601,9 +641,10 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
             answer->i_status = 200;
 
             psz_session = httpd_MsgGet( query, "Session" );
-            if( httpd_MsgGet( query, "Range" ) != NULL )
+            const char *range = httpd_MsgGet (query, "Range");
+            if (range && strncmp (range, "npt=", 4))
             {
-                answer->i_status = 456; /* cannot seek */
+                answer->i_status = 501;
                 break;
             }
 
@@ -613,23 +654,30 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
             {
                 /* FIXME: we really need to limit the number of tracks... */
                 char info[ses->trackc * ( strlen( control )
-                                  + sizeof("/trackID=123;seq=65535, ") ) + 1];
+                              + sizeof("url=/trackID=123;seq=65535;"
+                                       "rtptime=4294967295, ") ) + 1];
                 size_t infolen = 0;
+                int64_t ts = rtp_get_ts( rtsp->owner );
 
                 for( int i = 0; i < ses->trackc; i++ )
                 {
                     rtsp_strack_t *tr = ses->trackv + i;
-                    if( ( id == NULL ) || ( tr->id == id->sout_id ) )
+                    if( ( id == NULL ) || ( tr->id == id ) )
                     {
+                        uint16_t seq;
                         if( !tr->playing )
                         {
-                            tr->playing = VLC_TRUE;
-                            rtp_add_sink( tr->id, tr->fd, VLC_FALSE );
+                            tr->playing = true;
+                            rtp_add_sink( tr->id->sout_id, tr->fd, false,
+                                          &seq );
                         }
+                        else
+                            seq = rtp_get_seq( tr->id->sout_id );
                         infolen += sprintf( info + infolen,
-                                            "%s/trackID=%u;seq=%u, ", control,
-                                            rtp_get_num( tr->id ),
-                                            rtp_get_seq( tr->id ) );
+                                    "url=%s%strackID=%u;seq=%u;rtptime=%u, ",
+                                    control, rtsp->track_sep,
+                                    tr->id->track_id, seq,
+                                    rtp_compute_ts( tr->id->sout_id, ts ) );
                     }
                 }
                 if( infolen > 0 )
@@ -680,7 +728,7 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                 else /* Delete one track from the session */
                 for( int i = 0; i < ses->trackc; i++ )
                 {
-                    if( ses->trackv[i].id == id->sout_id )
+                    if( ses->trackv[i].id == id )
                     {
                         rtp_del_sink( id->sout_id, ses->trackv[i].fd );
                         REMOVE_ELEM( ses->trackv, ses->trackc, i );
@@ -695,7 +743,6 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
             return VLC_EGENERIC;
     }
 
-    httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
     if( psz_session )
         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );