Browse code

rtpdec: Emit timestamps for packets before the first RTCP packet, too

Emitted timestamps in each stream start from 0, for the first received
RTP packet. Once an RTCP packet is received, that one is used for
sync, emitting timestamps that fit seamlessly into the earlier ones.

Originally committed as revision 26187 to svn://svn.ffmpeg.org/ffmpeg/trunk

Martin Storsjö authored on 2011/01/02 07:27:16
Showing 4 changed files
... ...
@@ -123,9 +123,13 @@ static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int l
123 123
             payload_len = (AV_RB16(buf + 2) + 1) * 4;
124 124
 
125 125
             s->last_rtcp_ntp_time = AV_RB64(buf + 8);
126
-            if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
127
-                s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
128 126
             s->last_rtcp_timestamp = AV_RB32(buf + 16);
127
+            if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
128
+                s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
129
+                if (!s->base_timestamp)
130
+                    s->base_timestamp = s->last_rtcp_timestamp;
131
+                s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
132
+            }
129 133
 
130 134
             buf += payload_len;
131 135
             len -= payload_len;
... ...
@@ -440,8 +444,15 @@ static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestam
440 440
         delta_timestamp = timestamp - s->last_rtcp_timestamp;
441 441
         /* convert to the PTS timebase */
442 442
         addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
443
-        pkt->pts = s->range_start_offset + addend + delta_timestamp;
443
+        pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
444
+                   delta_timestamp;
445
+        return;
444 446
     }
447
+    if (timestamp == RTP_NOTS_VALUE)
448
+        return;
449
+    if (!s->base_timestamp)
450
+        s->base_timestamp = timestamp;
451
+    pkt->pts = s->range_start_offset + timestamp - s->base_timestamp;
445 452
 }
446 453
 
447 454
 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
... ...
@@ -172,6 +172,7 @@ struct RTPDemuxContext {
172 172
     int64_t last_rtcp_ntp_time;    // TODO: move into statistics
173 173
     int64_t first_rtcp_ntp_time;   // TODO: move into statistics
174 174
     uint32_t last_rtcp_timestamp;  // TODO: move into statistics
175
+    int64_t rtcp_ts_offset;
175 176
 
176 177
     /* rtcp sender statistics */
177 178
     unsigned int packet_count;     // TODO: move into statistics (outgoing)
... ...
@@ -1617,11 +1617,21 @@ int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
1617 1617
                  * in order to map their timestamp origin to the same ntp time
1618 1618
                  * as this one. */
1619 1619
                 int i;
1620
+                AVStream *st = NULL;
1621
+                if (rtsp_st->stream_index >= 0)
1622
+                    st = s->streams[rtsp_st->stream_index];
1620 1623
                 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1621 1624
                     RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
1622
-                    if (rtpctx2 &&
1623
-                        rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE)
1625
+                    AVStream *st2 = NULL;
1626
+                    if (rt->rtsp_streams[i]->stream_index >= 0)
1627
+                        st2 = s->streams[rt->rtsp_streams[i]->stream_index];
1628
+                    if (rtpctx2 && st && st2 &&
1629
+                        rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
1624 1630
                         rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
1631
+                        rtpctx2->rtcp_ts_offset = av_rescale_q(
1632
+                            rtpctx->rtcp_ts_offset, st->time_base,
1633
+                            st2->time_base);
1634
+                    }
1625 1635
                 }
1626 1636
             }
1627 1637
             if (ret == -RTCP_BYE) {
... ...
@@ -67,6 +67,8 @@ static int rtsp_read_play(AVFormatContext *s)
67 67
                 if (reply->range_start != AV_NOPTS_VALUE) {
68 68
                     rtpctx->last_rtcp_ntp_time  = AV_NOPTS_VALUE;
69 69
                     rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
70
+                    rtpctx->base_timestamp      = 0;
71
+                    rtpctx->rtcp_ts_offset      = 0;
70 72
                     if (st)
71 73
                         rtpctx->range_start_offset =
72 74
                             av_rescale_q(reply->range_start, AV_TIME_BASE_Q,