]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtp.c
rtp sout: fix DCCP socket leak
[vlc] / modules / stream_out / rtp.c
index 0e65085cf2a9b7cf79f63a2d9f08aa5d6a83ab72..b19c02bd55fd0fc1a9044768ab17e2ac10e911fe 100644 (file)
@@ -49,8 +49,6 @@
 #ifdef HAVE_UNISTD_H
 #   include <sys/types.h>
 #   include <unistd.h>
-#   include <fcntl.h>
-#   include <sys/stat.h>
 #endif
 #ifdef HAVE_ARPA_INET_H
 #   include <arpa/inet.h>
@@ -275,6 +273,12 @@ struct sout_stream_sys_t
     /* RTSP */
     rtsp_stream_t *rtsp;
 
+    /* RTSP NPT and timestamp computations */
+    mtime_t      i_npt_zero;    /* when NPT=0 packet is sent */
+    int64_t      i_pts_zero;    /* predicts PTS of NPT=0 packet */
+    int64_t      i_pts_offset;  /* matches actual PTS to prediction */
+    vlc_mutex_t  lock_ts;
+
     /* */
     char     *psz_destination;
     uint32_t  payload_bitmap;
@@ -313,6 +317,8 @@ struct sout_stream_id_t
     /* rtp field */
     uint16_t    i_sequence;
     uint8_t     i_payload_type;
+    bool        b_ts_init;
+    uint32_t    i_ts_offset;
     uint8_t     ssrc[4];
 
     /* for rtsp */
@@ -451,14 +457,22 @@ static int Open( vlc_object_t *p_this )
     if( p_sys->i_ttl == -1 )
     {
         /* Normally, we should let the default hop limit up to the core,
-         * but we have to know it to build our SDP properly, which is why
-         * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
-         * ttl are set. */
-        p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
+         * but we have to know it to write our RTSP headers properly,
+         * which is why we ask the core. FIXME: broken when neither
+         * sout-rtp-ttl nor ttl are set. */
+        p_sys->i_ttl = var_InheritInteger( p_stream, "ttl" );
     }
 
     p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
 
+    /* NPT=0 time will be determined when we packetize the first packet
+     * (of any ES). But we want to be able to report rtptime in RTSP
+     * without waiting. So until then, we use an arbitrary reference
+     * PTS for timestamp computations, and then actual PTS will catch
+     * up using offsets. */
+    p_sys->i_npt_zero = VLC_TS_INVALID;
+    p_sys->i_pts_zero = mdate(); /* arbitrary value, could probably be
+                                  * random */
     p_sys->payload_bitmap = 0;
     p_sys->i_es = 0;
     p_sys->es   = NULL;
@@ -475,6 +489,7 @@ static int Open( vlc_object_t *p_this )
     p_stream->p_sys     = p_sys;
 
     vlc_mutex_init( &p_sys->lock_sdp );
+    vlc_mutex_init( &p_sys->lock_ts );
     vlc_mutex_init( &p_sys->lock_es );
 
     psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
@@ -606,6 +621,7 @@ static void Close( vlc_object_t * p_this )
         RtspUnsetup( p_sys->rtsp );
 
     vlc_mutex_destroy( &p_sys->lock_sdp );
+    vlc_mutex_destroy( &p_sys->lock_ts );
     vlc_mutex_destroy( &p_sys->lock_es );
 
     if( p_sys->p_httpd_file )
@@ -665,7 +681,7 @@ static void SDPHandleUrl( sout_stream_t *p_stream, const char *psz_url )
         if( p_sys->p_mux != NULL )
         {
             sout_stream_id_t *id = p_sys->es[0];
-            id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0, GetDWBE( id->ssrc ),
+            id->rtsp_id = RtspAddId( p_sys->rtsp, id, GetDWBE( id->ssrc ),
                                      p_sys->psz_destination, p_sys->i_ttl,
                                      id->i_port, id->i_port + 1 );
         }
@@ -747,13 +763,18 @@ char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
     {
         inclport = 0;
 
+        /* Check against URL format rtsp://[<ipv6>]:<port>/<path> */
+        bool ipv6 = rtsp_url != NULL && strlen( rtsp_url ) > 7
+                    && rtsp_url[7] == '[';
+
         /* Dummy destination address for RTSP */
-        memset (&dst, 0, sizeof( struct sockaddr_in ) );
-        dst.ss_family = AF_INET;
+        dstlen = ipv6 ? sizeof( struct sockaddr_in6 )
+                      : sizeof( struct sockaddr_in );
+        memset (&dst, 0, dstlen);
+        dst.ss_family = ipv6 ? AF_INET6 : AF_INET;
 #ifdef HAVE_SA_LEN
-        dst.ss_len =
+        dst.ss_len = dstlen;
 #endif
-        dstlen = sizeof( struct sockaddr_in );
     }
 
     psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_stream ), SOUT_CFG_PREFIX,
@@ -768,12 +789,29 @@ char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
     if( rtsp_url != NULL )
         sdp_AddAttribute ( &psz_sdp, "control", "%s", rtsp_url );
 
+    const char *proto = "RTP/AVP"; /* protocol */
+    if( rtsp_url == NULL )
+    {
+        switch( p_sys->proto )
+        {
+            case IPPROTO_UDP:
+                break;
+            case IPPROTO_TCP:
+                proto = "TCP/RTP/AVP";
+                break;
+            case IPPROTO_DCCP:
+                proto = "DCCP/RTP/AVP";
+                break;
+            case IPPROTO_UDPLITE:
+                return psz_sdp;
+        }
+    }
+
     /* FIXME: locking?! */
     for( i = 0; i < p_sys->i_es; i++ )
     {
         sout_stream_id_t *id = p_sys->es[i];
         const char *mime_major; /* major MIME type */
-        const char *proto = "RTP/AVP"; /* protocol */
 
         switch( id->i_cat )
         {
@@ -790,23 +828,6 @@ char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
                 continue;
         }
 
-        if( rtsp_url == NULL )
-        {
-            switch( p_sys->proto )
-            {
-                case IPPROTO_UDP:
-                    break;
-                case IPPROTO_TCP:
-                    proto = "TCP/RTP/AVP";
-                    break;
-                case IPPROTO_DCCP:
-                    proto = "DCCP/RTP/AVP";
-                    break;
-                case IPPROTO_UDPLITE:
-                    continue;
-            }
-        }
-
         sdp_AddMedia( &psz_sdp, mime_major, proto, inclport * id->i_port,
                       id->i_payload_type, false, id->i_bitrate,
                       id->psz_enc, id->i_clock_rate, id->i_channels,
@@ -817,11 +838,12 @@ char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
 
         if( rtsp_url != NULL )
         {
-            assert( strlen( rtsp_url ) > 0 );
-            bool addslash = ( rtsp_url[strlen( rtsp_url ) - 1] != '/' );
-            sdp_AddAttribute ( &psz_sdp, "control",
-                               addslash ? "%s/trackID=%u" : "%strackID=%u",
-                               rtsp_url, i );
+            char *track_url = RtspAppendTrackPath( id->rtsp_id, rtsp_url );
+            if( track_url != NULL )
+            {
+                sdp_AddAttribute ( &psz_sdp, "control", "%s", track_url );
+                free( track_url );
+            }
         }
         else
         {
@@ -870,6 +892,13 @@ rtp_set_ptime (sout_stream_id_t *id, unsigned ptime_ms, size_t bytes)
         id->i_mtu = 12 + (((id->i_mtu - 12) / bytes) * bytes);
 }
 
+uint32_t rtp_compute_ts( const sout_stream_id_t *id, int64_t i_pts )
+{
+    /* NOTE: this plays nice with offsets because the calculations are
+     * linear. */
+    return i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
+}
+
 /** Add an ES as a new RTP stream */
 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 {
@@ -954,7 +983,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         id->i_bitrate = 0;
     }
 
-    id->i_mtu = config_GetInt( p_stream, "mtu" );
+    id->i_mtu = var_InheritInteger( p_stream, "mtu" );
     if( id->i_mtu <= 12 + 16 )
         id->i_mtu = 576 - 20 - 8; /* pessimistic */
     msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
@@ -1212,12 +1241,11 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
         case VLC_CODEC_MP4V:
         {
-            char hexa[2*p_fmt->i_extra +1];
-
             id->psz_enc = "MP4V-ES";
             id->pf_packetize = rtp_packetize_split;
             if( p_fmt->i_extra > 0 )
             {
+                char hexa[2*p_fmt->i_extra +1];
                 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
                 if( asprintf( &id->psz_fmtp,
                               "profile-level-id=3; config=%s;", hexa ) == -1 )
@@ -1307,8 +1335,14 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
 #endif
 
+    vlc_mutex_lock( &p_sys->lock_ts );
+    id->b_ts_init = ( p_sys->i_npt_zero != VLC_TS_INVALID );
+    vlc_mutex_unlock( &p_sys->lock_ts );
+    if( id->b_ts_init )
+        id->i_ts_offset = rtp_compute_ts( id, p_sys->i_pts_offset );
+
     if( p_sys->rtsp != NULL )
-        id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
+        id->rtsp_id = RtspAddId( p_sys->rtsp, id,
                                  GetDWBE( id->ssrc ),
                                  p_sys->psz_destination,
                                  p_sys->i_ttl, id->i_port, id->i_port + 1 );
@@ -1366,14 +1400,16 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
 
     if( id->rtsp_id )
         RtspDelId( p_sys->rtsp, id->rtsp_id );
-    if( id->sinkc > 0 )
-        rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
     if( id->listen.fd != NULL )
     {
         vlc_cancel( id->listen.thread );
         vlc_join( id->listen.thread, NULL );
         net_ListenClose( id->listen.fd );
     }
+    /* Delete remaining sinks (incoming connections or explicit
+     * outgoing dst=) */
+    while( id->sinkc > 0 )
+        rtp_del_sink( id, id->sinkv[0].rtp_fd );
 #ifdef HAVE_SRTP
     if( id->srtp != NULL )
         srtp_destroy( id->srtp );
@@ -1385,7 +1421,6 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
     if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
     if( p_sys->psz_sdp_file != NULL ) FileSetup( p_stream );
 
-    vlc_object_detach( id );
     vlc_object_release( id );
     return VLC_SUCCESS;
 }
@@ -1689,29 +1724,50 @@ uint16_t rtp_get_seq( sout_stream_id_t *id )
     return seq;
 }
 
-/* FIXME: this is pretty bad - if we remove and then insert an ES
- * the number will get unsynched from inside RTSP */
-unsigned rtp_get_num( const sout_stream_id_t *id )
+/* Return a timestamp corresponding to packets being sent now, and that
+ * can be passed to rtp_compute_ts() to get rtptime values for each ES. */
+int64_t rtp_get_ts( const sout_stream_t *p_stream )
 {
-    sout_stream_sys_t *p_sys = id->p_stream->p_sys;
-    int i;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+    mtime_t i_npt_zero;
+    vlc_mutex_lock( &p_sys->lock_ts );
+    i_npt_zero = p_sys->i_npt_zero;
+    vlc_mutex_unlock( &p_sys->lock_ts );
 
-    vlc_mutex_lock( &p_sys->lock_es );
-    for( i = 0; i < p_sys->i_es; i++ )
-    {
-        if( id == p_sys->es[i] )
-            break;
-    }
-    vlc_mutex_unlock( &p_sys->lock_es );
+    if( i_npt_zero == VLC_TS_INVALID )
+        return p_sys->i_pts_zero;
 
-    return i;
-}
+    mtime_t now = mdate();
+    if( now < i_npt_zero )
+        return p_sys->i_pts_zero;
 
+    return p_sys->i_pts_zero + (now - i_npt_zero); 
+}
 
 void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
                            int b_marker, int64_t i_pts )
 {
-    uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
+    if( !id->b_ts_init )
+    {
+        sout_stream_sys_t *p_sys = id->p_stream->p_sys;
+        vlc_mutex_lock( &p_sys->lock_ts );
+        if( p_sys->i_npt_zero == VLC_TS_INVALID )
+        {
+            /* This is the first packet of any ES. We initialize the
+             * NPT=0 time reference, and the offset to match the
+             * arbitrary PTS reference. */
+            p_sys->i_npt_zero = i_pts + id->i_caching;
+            p_sys->i_pts_offset = p_sys->i_pts_zero - i_pts;
+        }
+        vlc_mutex_unlock( &p_sys->lock_ts );
+
+        /* And in any case this is the first packet of this ES, so we
+         * initialize the offset for this ES. */
+        id->i_ts_offset = rtp_compute_ts( id, p_sys->i_pts_offset );
+        id->b_ts_init = true;
+    }
+
+    uint32_t i_timestamp = rtp_compute_ts( id, i_pts ) + id->i_ts_offset;
 
     out->p_buffer[0] = 0x80;
     out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;