]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtp.c
rtp sout: factor out some SDP code
[vlc] / modules / stream_out / rtp.c
index 837270b5f04eb713689d9f6c1dc54c11ac2298d3..a6b4fd647f26570d1cd5d7d7f959abe3c7a47874 100644 (file)
@@ -49,8 +49,9 @@
 #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>
 #endif
 #ifdef HAVE_LINUX_DCCP_H
 #   include <linux/dccp.h>
     "This sends and receives RTCP packet multiplexed over the same port " \
     "as RTP packets." )
 
+#define CACHING_TEXT N_("Caching value (ms)")
+#define CACHING_LONGTEXT N_( \
+    "Default caching value for outbound RTP streams. This " \
+    "value should be set in milliseconds." )
+
 #define PROTO_TEXT N_("Transport protocol")
 #define PROTO_LONGTEXT N_( \
     "This selects which transport protocol to use for RTP." )
@@ -202,6 +208,8 @@ vlc_module_begin ()
                  TTL_LONGTEXT, true )
     add_bool( SOUT_CFG_PREFIX "rtcp-mux", false, NULL,
               RTCP_MUX_TEXT, RTCP_MUX_LONGTEXT, false )
+    add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
+                 CACHING_TEXT, CACHING_LONGTEXT, true )
 
 #ifdef HAVE_SRTP
     add_string( SOUT_CFG_PREFIX "key", "", NULL,
@@ -222,7 +230,7 @@ vlc_module_end ()
 static const char *const ppsz_sout_options[] = {
     "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
     "sap", "description", "url", "email", "phone",
-    "proto", "rtcp-mux", "key", "salt",
+    "proto", "rtcp-mux", "caching", "key", "salt",
     "mp4a-latm", NULL
 };
 
@@ -265,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;
@@ -303,8 +317,13 @@ 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 */
+    uint16_t    i_seq_sent_next;
+
     /* for sdp */
     const char  *psz_enc;
     char        *psz_fmtp;
@@ -438,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;
@@ -462,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" );
@@ -593,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 )
@@ -652,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 );
         }
@@ -755,12 +784,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 )
         {
@@ -777,23 +823,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,
@@ -804,11 +833,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
         {
@@ -857,6 +887,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 )
 {
@@ -918,6 +955,8 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     vlc_rand_bytes (&id->i_sequence, sizeof (id->i_sequence));
     vlc_rand_bytes (id->ssrc, sizeof (id->ssrc));
 
+    id->i_seq_sent_next = id->i_sequence;
+
     id->psz_enc    = NULL;
     id->psz_fmtp   = NULL;
     id->i_clock_rate = 90000; /* most common case for video */
@@ -939,7 +978,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 );
@@ -1026,7 +1065,11 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
                     msg_Err( p_stream, "cannot create RTP socket" );
                     goto error;
                 }
-                rtp_add_sink( id, fd, p_sys->rtcp_mux );
+                /* Ignore any unexpected incoming packet (including RTCP-RR
+                 * packets in case of rtcp-mux) */
+                setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
+                            sizeof (int));
+                rtp_add_sink( id, fd, p_sys->rtcp_mux, NULL );
             }
         }
 
@@ -1193,12 +1236,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 )
@@ -1288,8 +1330,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 );
@@ -1585,6 +1633,7 @@ static void* ThreadSend( vlc_object_t *p_this )
 
             deadv[deadc++] = id->sinkv[i].rtp_fd;
         }
+        id->i_seq_sent_next = ntohs(((uint16_t *) out->p_buffer)[1]) + 1;
         vlc_mutex_unlock( &id->lock_sink );
         block_Release( out );
 
@@ -1612,7 +1661,7 @@ static void *rtp_listen_thread( void *data )
         if( fd == -1 )
             continue;
         int canc = vlc_savecancel( );
-        rtp_add_sink( id, fd, true );
+        rtp_add_sink( id, fd, true, NULL );
         vlc_restorecancel( canc );
     }
 
@@ -1620,7 +1669,7 @@ static void *rtp_listen_thread( void *data )
 }
 
 
-int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux )
+int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux, uint16_t *seq )
 {
     rtp_sink_t sink = { fd, NULL };
     sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP,
@@ -1630,6 +1679,8 @@ int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux )
 
     vlc_mutex_lock( &id->lock_sink );
     INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
+    if( seq != NULL )
+        *seq = id->i_seq_sent_next;
     vlc_mutex_unlock( &id->lock_sink );
     return VLC_SUCCESS;
 }
@@ -1655,36 +1706,62 @@ void rtp_del_sink( sout_stream_id_t *id, int fd )
     net_Close( sink.rtp_fd );
 }
 
-uint16_t rtp_get_seq( const sout_stream_id_t *id )
+uint16_t rtp_get_seq( sout_stream_id_t *id )
 {
-    /* This will return values for the next packet.
-     * Accounting for caching would not be totally trivial. */
-    return id->i_sequence;
+    /* This will return values for the next packet. */
+    uint16_t seq;
+
+    vlc_mutex_lock( &id->lock_sink );
+    seq = id->i_seq_sent_next;
+    vlc_mutex_unlock( &id->lock_sink );
+
+    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;