[vlc-devel] [PATCH 2/2] rtp sout: implement rtptime parameter

Pierre Ynard linkfanel at yahoo.fr
Sat Dec 12 03:55:04 CET 2009


This adds support for the rtptime parameter in the RTP-Info RTSP header.


diff --git a/modules/stream_out/rtp.c b/modules/stream_out/rtp.c
index 0e65085..8f2fde7 100644
--- a/modules/stream_out/rtp.c
+++ b/modules/stream_out/rtp.c
@@ -317,7 +317,9 @@
     uint8_t     ssrc[4];
 
     /* for rtsp */
+    uint32_t    i_ts_sent_now;
     uint16_t    i_seq_sent_next;
+    bool        b_ts_init;
 
     /* for sdp */
     const char  *psz_enc;
@@ -930,10 +932,11 @@
     assert (id->i_payload_type < 128);
 
     vlc_rand_bytes (&id->i_sequence, sizeof (id->i_sequence));
-    vlc_rand_bytes (&id->i_ts_offset, sizeof (id->i_ts_offset));
+    vlc_rand_bytes (&id->i_ts_sent_now, sizeof (id->i_ts_sent_now));
     vlc_rand_bytes (id->ssrc, sizeof (id->ssrc));
 
     id->i_seq_sent_next = id->i_sequence;
+    id->b_ts_init = false;
 
     id->psz_enc    = NULL;
     id->psz_fmtp   = NULL;
@@ -1607,6 +1610,7 @@
             deadv[deadc++] = id->sinkv[i].rtp_fd;
         }
         id->i_seq_sent_next = ntohs(((uint16_t *) out->p_buffer)[1]) + 1;
+        id->i_ts_sent_now = ntohl(((uint32_t *) out->p_buffer)[1]);
         vlc_mutex_unlock( &id->lock_sink );
         block_Release( out );
 
@@ -1642,7 +1646,8 @@
 }
 
 
-int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux, uint16_t *seq )
+int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux,
+                  rtpinfo_t *rtpinfo )
 {
     rtp_sink_t sink = { fd, NULL };
     sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP,
@@ -1652,8 +1657,11 @@
 
     vlc_mutex_lock( &id->lock_sink );
     INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
-    if( seq != NULL )
-        *seq = id->i_seq_sent_next;
+    if( rtpinfo != NULL )
+    {
+        rtpinfo->seq = id->i_seq_sent_next;
+        rtpinfo->rtptime = id->i_ts_sent_now;
+    }
     vlc_mutex_unlock( &id->lock_sink );
     return VLC_SUCCESS;
 }
@@ -1679,16 +1687,14 @@
     net_Close( sink.rtp_fd );
 }
 
-uint16_t rtp_get_seq( sout_stream_id_t *id )
+void rtp_get_rtpinfo( sout_stream_id_t *id, rtpinfo_t *rtpinfo )
 {
-    /* This will return values for the next packet. */
-    uint16_t seq;
+    assert( rtpinfo != NULL );
 
     vlc_mutex_lock( &id->lock_sink );
-    seq = id->i_seq_sent_next;
+    rtpinfo->seq = id->i_seq_sent_next;
+    rtpinfo->rtptime = id->i_ts_sent_now;
     vlc_mutex_unlock( &id->lock_sink );
-
-    return seq;
 }
 
 /* FIXME: this is pretty bad - if we remove and then insert an ES
@@ -1715,6 +1721,15 @@
 {
     uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
 
+    if( !id->b_ts_init )
+    {
+        /* We already set a value for the first timestamp before the
+         * stream started, so that we could report it in the RTSP
+         * without waiting for the first packet. Initialize the offset
+         * so that this first timestamp matches this value. */
+        id->i_ts_offset = id->i_ts_sent_now - i_timestamp;
+        id->b_ts_init = true;
+    }
     i_timestamp += id->i_ts_offset;
 
     out->p_buffer[0] = 0x80;
diff --git a/modules/stream_out/rtp.h b/modules/stream_out/rtp.h
index 5372146..a98f1b6 100644
--- a/modules/stream_out/rtp.h
+++ b/modules/stream_out/rtp.h
@@ -36,9 +36,16 @@ void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t * );
 
 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url );
 
-int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux, uint16_t *seq );
+typedef struct rtpinfo_t
+{
+    uint32_t rtptime;
+    uint16_t seq;
+} rtpinfo_t;
+
+int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux,
+                  rtpinfo_t *rtpinfo );
 void rtp_del_sink( sout_stream_id_t *id, int fd );
-uint16_t rtp_get_seq( sout_stream_id_t *id );
+void rtp_get_rtpinfo( sout_stream_id_t *id, rtpinfo_t *rtpinfo );
 unsigned rtp_get_num( const sout_stream_id_t *id );
 
 /* RTP packetization */
diff --git a/modules/stream_out/rtsp.c b/modules/stream_out/rtsp.c
index a8e3027..a74e375 100644
--- a/modules/stream_out/rtsp.c
+++ b/modules/stream_out/rtsp.c
@@ -638,7 +638,8 @@ 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("url=/trackID=123;seq=65535, ") ) + 1];
+                              + sizeof("url=/trackID=123;seq=65535;"
+                                       "rtptime=4294967295, ") ) + 1];
                 size_t infolen = 0;
 
                 for( int i = 0; i < ses->trackc; i++ )
@@ -646,19 +647,19 @@ static int RtspHandler( rtsp_stream_t *rtsp, rtsp_stream_id_t *id,
                     rtsp_strack_t *tr = ses->trackv + i;
                     if( ( id == NULL ) || ( tr->id == id->sout_id ) )
                     {
-                        uint16_t seq;
+                        rtpinfo_t rtpinfo;
                         if( !tr->playing )
                         {
                             tr->playing = true;
-                            rtp_add_sink( tr->id, tr->fd, false, &seq );
+                            rtp_add_sink( tr->id, tr->fd, false, &rtpinfo );
                         }
                         else
-                            seq = rtp_get_seq( tr->id );
+                            rtp_get_rtpinfo( tr->id, &rtpinfo );
                         infolen += sprintf( info + infolen,
-                                            "url=%s/trackID=%u;seq=%u, ",
+                                    "url=%s/trackID=%u;seq=%u;rtptime=%u, ",
                                             control,
                                             rtp_get_num( tr->id ),
-                                            seq );
+                                            rtpinfo.seq, rtpinfo.rtptime );
                     }
                 }
                 if( infolen > 0 )


-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."



More information about the vlc-devel mailing list