changeset 2539:ba933dfa4833 libavformat

Properly set RTP and NTP timestamps in RTCP SR packets
author lucabe
date Mon, 10 Sep 2007 06:58:19 +0000
parents 0d837f926052
children ca3cba9c641f
files rtp.c
diffstat 1 files changed, 14 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/rtp.c	Sun Sep 09 17:54:26 2007 +0000
+++ b/rtp.c	Mon Sep 10 06:58:19 2007 +0000
@@ -739,6 +739,7 @@
     s->timestamp = s->base_timestamp;
     s->ssrc = 0; /* FIXME: was random(), what should this be? */
     s->first_packet = 1;
+    s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
 
     max_packet_size = url_fget_max_packet_size(&s1->pb);
     if (max_packet_size <= 12)
@@ -762,6 +763,9 @@
         s->buf_ptr = s->buf;
         break;
     default:
+        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
+            av_set_pts_info(st, 32, 1, st->codec->sample_rate);
+        }
         s->buf_ptr = s->buf;
         break;
     }
@@ -773,15 +777,22 @@
 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
 {
     RTPDemuxContext *s = s1->priv_data;
+    uint32_t rtp_ts;
+
 #if defined(DEBUG)
     printf("RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
 #endif
+
+    if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) s->first_rtcp_ntp_time = ntp_time;
+    rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, AV_TIME_BASE_Q,
+                          s1->streams[0]->time_base) + s->base_timestamp;
     put_byte(&s1->pb, (RTP_VERSION << 6));
     put_byte(&s1->pb, 200);
     put_be16(&s1->pb, 6); /* length in words - 1 */
     put_be32(&s1->pb, s->ssrc);
-    put_be64(&s1->pb, ntp_time);
-    put_be32(&s1->pb, s->timestamp);
+    put_be32(&s1->pb, ntp_time / 1000000);
+    put_be32(&s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
+    put_be32(&s1->pb, rtp_ts);
     put_be32(&s1->pb, s->packet_count);
     put_be32(&s1->pb, s->octet_count);
     put_flush_packet(&s1->pb);
@@ -956,7 +967,6 @@
     RTPDemuxContext *s = s1->priv_data;
     AVStream *st = s1->streams[0];
     int rtcp_bytes;
-    int64_t ntp_time;
     int size= pkt->size;
     uint8_t *buf1= pkt->data;
 
@@ -968,10 +978,7 @@
     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
         RTCP_TX_RATIO_DEN;
     if (s->first_packet || rtcp_bytes >= 28) {
-        /* compute NTP time */
-        /* XXX: 90 kHz timestamp hardcoded */
-        ntp_time = (pkt->pts << 28) / 5625;
-        rtcp_send_sr(s1, ntp_time);
+        rtcp_send_sr(s1, av_gettime());
         s->last_octet_count = s->octet_count;
         s->first_packet = 0;
     }