rtpbin: Get and use the NTP time when receiving RTCP
authorWim Taymans <wim.taymans@collabora.co.uk>
Wed, 2 Feb 2011 17:21:26 +0000 (18:21 +0100)
committerWim Taymans <wim.taymans@collabora.co.uk>
Wed, 2 Feb 2011 17:30:46 +0000 (18:30 +0100)
When we receive an RTCP packet, get the current NTP time in nanseconds so that
we can correctly calculate the round-trip time.

gst/rtpmanager/gstrtpsession.c
gst/rtpmanager/rtpsession.c
gst/rtpmanager/rtpsession.h
gst/rtpmanager/rtpsource.c
gst/rtpmanager/rtpsource.h
gst/rtpmanager/rtpstats.h

index b1223d6..64abc94 100644 (file)
@@ -1581,6 +1581,7 @@ gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
   GstRtpSession *rtpsession;
   GstRtpSessionPrivate *priv;
   GstClockTime current_time;
+  guint64 ntpnstime;
   GstFlowReturn ret;
 
   rtpsession = GST_RTP_SESSION (gst_pad_get_parent (pad));
@@ -1589,7 +1590,10 @@ gst_rtp_session_chain_recv_rtcp (GstPad * pad, GstBuffer * buffer)
   GST_LOG_OBJECT (rtpsession, "received RTCP packet");
 
   current_time = gst_clock_get_time (priv->sysclock);
-  ret = rtp_session_process_rtcp (priv->session, buffer, current_time);
+  get_current_times (rtpsession, NULL, &ntpnstime);
+
+  ret =
+      rtp_session_process_rtcp (priv->session, buffer, current_time, ntpnstime);
 
   gst_object_unref (rtpsession);
 
index 49ce69b..a455ad3 100644 (file)
@@ -1598,11 +1598,12 @@ rtp_session_create_source (RTPSession * sess)
 static void
 update_arrival_stats (RTPSession * sess, RTPArrivalStats * arrival,
     gboolean rtp, GstBuffer * buffer, GstClockTime current_time,
-    GstClockTime running_time)
+    GstClockTime running_time, guint64 ntpnstime)
 {
   /* get time of arrival */
   arrival->current_time = current_time;
   arrival->running_time = running_time;
+  arrival->ntpnstime = ntpnstime;
 
   /* get packet size including header overhead */
   arrival->bytes = GST_BUFFER_SIZE (buffer) + sess->header_len;
@@ -1657,7 +1658,7 @@ rtp_session_process_rtp (RTPSession * sess, GstBuffer * buffer,
   RTP_SESSION_LOCK (sess);
   /* update arrival stats */
   update_arrival_stats (sess, &arrival, TRUE, buffer, current_time,
-      running_time);
+      running_time, -1);
 
   /* ignore more RTP packets when we left the session */
   if (sess->source->received_bye)
@@ -1778,7 +1779,7 @@ rtp_session_process_rb (RTPSession * sess, RTPSource * source,
       /* only deal with report blocks for our session, we update the stats of
        * the sender of the RTCP message. We could also compare our stats against
        * the other sender to see if we are better or worse. */
-      rtp_source_process_rb (source, arrival->current_time, fractionlost,
+      rtp_source_process_rb (source, arrival->ntpnstime, fractionlost,
           packetslost, exthighestseq, jitter, lsr, dlsr);
     }
   }
@@ -2153,6 +2154,7 @@ rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
  * @sess: and #RTPSession
  * @buffer: an RTCP buffer
  * @current_time: the current system time
+ * @ntpnstime: the current NTP time in nanoseconds
  *
  * Process an RTCP buffer in the session manager. This function takes ownership
  * of @buffer.
@@ -2161,7 +2163,7 @@ rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
  */
 GstFlowReturn
 rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
-    GstClockTime current_time)
+    GstClockTime current_time, guint64 ntpnstime)
 {
   GstRTCPPacket packet;
   gboolean more, is_bye = FALSE, do_sync = FALSE;
@@ -2178,7 +2180,8 @@ rtp_session_process_rtcp (RTPSession * sess, GstBuffer * buffer,
 
   RTP_SESSION_LOCK (sess);
   /* update arrival stats */
-  update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1);
+  update_arrival_stats (sess, &arrival, FALSE, buffer, current_time, -1,
+      ntpnstime);
 
   if (sess->sent_bye)
     goto ignore;
index 3268490..6dee1cb 100644 (file)
@@ -326,7 +326,8 @@ GstFlowReturn   rtp_session_process_rtp            (RTPSession *sess, GstBuffer
                                                     GstClockTime current_time,
                                                    GstClockTime running_time);
 GstFlowReturn   rtp_session_process_rtcp           (RTPSession *sess, GstBuffer *buffer,
-                                                    GstClockTime current_time);
+                                                    GstClockTime current_time,
+                                                    guint64 ntpnstime);
 
 /* processing packets for sending */
 GstFlowReturn   rtp_session_send_rtp               (RTPSession *sess, gpointer data, gboolean is_list,
index 423d2b6..e7872d8 100644 (file)
@@ -1350,7 +1350,7 @@ rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
 /**
  * rtp_source_process_rb:
  * @src: an #RTPSource
- * @time: the current time in nanoseconds since 1970
+ * @ntpnstime: the current time in nanoseconds since 1970
  * @fractionlost: fraction lost since last SR/RR
  * @packetslost: the cumululative number of packets lost
  * @exthighestseq: the extended last sequence number received
@@ -1361,13 +1361,14 @@ rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
  * Update the report block in @src.
  */
 void
-rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
-    gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
-    guint32 dlsr)
+rtp_source_process_rb (RTPSource * src, GstClockTime ntpnstime,
+    guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
+    guint32 jitter, guint32 lsr, guint32 dlsr)
 {
   RTPReceiverReport *curr;
   gint curridx;
   guint32 ntp, A;
+  guint64 f_ntp;
 
   g_return_if_fail (RTP_IS_SOURCE (src));
 
@@ -1388,8 +1389,11 @@ rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
   curr->lsr = lsr;
   curr->dlsr = dlsr;
 
+  /* convert the NTP time in nanoseconds to 32.32 fixed point */
+  f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
   /* calculate round trip, round the time up */
-  ntp = ((gst_rtcp_unix_to_ntp (time) + 0xffff) >> 16) & 0xffffffff;
+  ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
+
   A = dlsr + lsr;
   if (A > 0 && ntp > A)
     A = ntp - A;
index fadbe30..6db0a6a 100644 (file)
@@ -220,7 +220,7 @@ GstFlowReturn   rtp_source_send_rtp            (RTPSource *src, gpointer data, g
 void            rtp_source_process_bye         (RTPSource *src, const gchar *reason);
 void            rtp_source_process_sr          (RTPSource *src, GstClockTime time, guint64 ntptime,
                                                 guint32 rtptime, guint32 packet_count, guint32 octet_count);
-void            rtp_source_process_rb          (RTPSource *src, GstClockTime time, guint8 fractionlost,
+void            rtp_source_process_rb          (RTPSource *src, guint64 ntpnstime, guint8 fractionlost,
                                                 gint32 packetslost, guint32 exthighestseq, guint32 jitter,
                                                 guint32 lsr, guint32 dlsr);
 
index 643da00..4560595 100644 (file)
@@ -58,6 +58,7 @@ typedef struct {
  * RTPArrivalStats:
  * @current_time: current time according to the system clock
  * @running_time: arrival time of a packet as buffer running_time
+ * @ntpnstime: arrival time of a packet NTP time in nanoseconds
  * @have_address: if the @address field contains a valid address
  * @address: address of the sender of the packet
  * @bytes: bytes of the packet including lowlevel overhead
@@ -68,6 +69,7 @@ typedef struct {
 typedef struct {
   GstClockTime  current_time;
   GstClockTime  running_time;
+  guint64       ntpnstime;
   gboolean      have_address;
   GstNetAddress address;
   guint         bytes;