rtp: Use running_time instead of PTS for config-interval calculations

PTS can start again from a different offset while the running time is
increasing. The only thing that matters here is the running time.

https://bugzilla.gnome.org/show_bug.cgi?id=796807
diff --git a/gst/rtp/gstrtpgstpay.c b/gst/rtp/gstrtpgstpay.c
index f5511f4..f5de410 100644
--- a/gst/rtp/gstrtpgstpay.c
+++ b/gst/rtp/gstrtpgstpay.c
@@ -573,7 +573,8 @@
 }
 
 static void
-gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
+gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay,
+    GstClockTime running_time)
 {
   GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
   GstCaps *caps = NULL;
@@ -601,7 +602,7 @@
     gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
     gst_caps_unref (caps);
   }
-  rtpgstpay->last_config = timestamp;
+  rtpgstpay->last_config = running_time;
 }
 
 static GstFlowReturn
@@ -610,25 +611,28 @@
 {
   GstFlowReturn ret;
   GstRtpGSTPay *rtpgstpay;
-  GstClockTime timestamp;
+  GstClockTime timestamp, running_time;
 
   rtpgstpay = GST_RTP_GST_PAY (basepayload);
 
   timestamp = GST_BUFFER_PTS (buffer);
+  running_time =
+      gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+      timestamp);
 
   /* check if we need to send the caps and taglist now */
   if (rtpgstpay->config_interval > 0) {
     GST_DEBUG_OBJECT (rtpgstpay,
-        "timestamp %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
-        GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpgstpay->last_config));
+        "running time %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (running_time), GST_TIME_ARGS (rtpgstpay->last_config));
 
-    if (timestamp != GST_CLOCK_TIME_NONE &&
+    if (running_time != GST_CLOCK_TIME_NONE &&
         rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
       guint64 diff;
 
       /* calculate diff between last SPS/PPS in milliseconds */
-      if (timestamp > rtpgstpay->last_config)
-        diff = timestamp - rtpgstpay->last_config;
+      if (running_time > rtpgstpay->last_config)
+        diff = running_time - rtpgstpay->last_config;
       else
         diff = 0;
 
@@ -637,9 +641,9 @@
 
       /* bigger than interval, queue SPS/PPS */
       if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
-        gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
+        gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
     } else {
-      gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
+      gst_rtp_gst_pay_send_config (rtpgstpay, running_time);
     }
   }
 
diff --git a/gst/rtp/gstrtph264pay.c b/gst/rtp/gstrtph264pay.c
index 73f080a..6e18894 100644
--- a/gst/rtp/gstrtph264pay.c
+++ b/gst/rtp/gstrtph264pay.c
@@ -717,7 +717,9 @@
 
     /* remember when we last saw SPS */
     if (updated && pts != -1)
-      payloader->last_spspps = pts;
+      payloader->last_spspps =
+          gst_segment_to_running_time (&GST_RTP_BASE_PAYLOAD_CAST
+          (payloader)->segment, GST_FORMAT_TIME, pts);
   } else {
     GST_DEBUG ("NAL: %x %x %x Len = %u", (header >> 7),
         (header >> 5) & 3, type, size);
@@ -769,7 +771,9 @@
   }
 
   if (pts != -1 && sent_all_sps_pps)
-    rtph264pay->last_spspps = pts;
+    rtph264pay->last_spspps =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        pts);
 
   return ret;
 }
@@ -816,14 +820,18 @@
   if (nalType == IDR_TYPE_ID && rtph264pay->spspps_interval > 0) {
     if (rtph264pay->last_spspps != -1) {
       guint64 diff;
+      GstClockTime running_time =
+          gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+          pts);
 
       GST_LOG_OBJECT (rtph264pay,
           "now %" GST_TIME_FORMAT ", last SPS/PPS %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (pts), GST_TIME_ARGS (rtph264pay->last_spspps));
+          GST_TIME_ARGS (running_time),
+          GST_TIME_ARGS (rtph264pay->last_spspps));
 
       /* calculate diff between last SPS/PPS in milliseconds */
-      if (pts > rtph264pay->last_spspps)
-        diff = pts - rtph264pay->last_spspps;
+      if (running_time > rtph264pay->last_spspps)
+        diff = running_time - rtph264pay->last_spspps;
       else
         diff = 0;
 
diff --git a/gst/rtp/gstrtph265pay.c b/gst/rtp/gstrtph265pay.c
index 9a597e0..1f366b7 100644
--- a/gst/rtp/gstrtph265pay.c
+++ b/gst/rtp/gstrtph265pay.c
@@ -802,7 +802,9 @@
 
     /* remember when we last saw VPS */
     if (updated && pts != -1)
-      payloader->last_vps_sps_pps = pts;
+      payloader->last_vps_sps_pps =
+          gst_segment_to_running_time (&GST_RTP_BASE_PAYLOAD_CAST
+          (payloader)->segment, GST_FORMAT_TIME, pts);
   } else {
     GST_DEBUG_OBJECT (payloader, "NALU type 0x%x, size %u", type, size);
   }
@@ -856,7 +858,9 @@
   }
 
   if (pts != -1 && sent_all_vps_sps_pps)
-    rtph265pay->last_vps_sps_pps = pts;
+    rtph265pay->last_vps_sps_pps =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        pts);
 
   return ret;
 }
@@ -929,15 +933,18 @@
       if (rtph265pay->vps_sps_pps_interval > 0) {
         if (rtph265pay->last_vps_sps_pps != -1) {
           guint64 diff;
+          GstClockTime running_time =
+              gst_segment_to_running_time (&basepayload->segment,
+              GST_FORMAT_TIME, pts);
 
           GST_LOG_OBJECT (rtph265pay,
               "now %" GST_TIME_FORMAT ", last VPS/SPS/PPS %" GST_TIME_FORMAT,
-              GST_TIME_ARGS (pts),
+              GST_TIME_ARGS (running_time),
               GST_TIME_ARGS (rtph265pay->last_vps_sps_pps));
 
           /* calculate diff between last SPS/PPS in milliseconds */
-          if (pts > rtph265pay->last_vps_sps_pps)
-            diff = pts - rtph265pay->last_vps_sps_pps;
+          if (running_time > rtph265pay->last_vps_sps_pps)
+            diff = running_time - rtph265pay->last_vps_sps_pps;
           else
             diff = 0;
 
diff --git a/gst/rtp/gstrtpmp4vpay.c b/gst/rtp/gstrtpmp4vpay.c
index 5e89fd8..7e76b23 100644
--- a/gst/rtp/gstrtpmp4vpay.c
+++ b/gst/rtp/gstrtpmp4vpay.c
@@ -472,23 +472,32 @@
 
       size = gst_buffer_get_size (buffer);
     } else {
+      GstClockTime running_time =
+          gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+          timestamp);
+
       GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
-      rtpmp4vpay->last_config = timestamp;
+      rtpmp4vpay->last_config = running_time;
     }
   }
 
   /* there is a config request, see if we need to insert it */
   if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
+    GstClockTime running_time =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        timestamp);
+
     if (rtpmp4vpay->last_config != -1) {
       guint64 diff;
 
       GST_LOG_OBJECT (rtpmp4vpay,
           "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
+          GST_TIME_ARGS (running_time),
+          GST_TIME_ARGS (rtpmp4vpay->last_config));
 
       /* calculate diff between last config in milliseconds */
-      if (timestamp > rtpmp4vpay->last_config) {
-        diff = timestamp - rtpmp4vpay->last_config;
+      if (running_time > rtpmp4vpay->last_config) {
+        diff = running_time - rtpmp4vpay->last_config;
       } else {
         diff = 0;
       }
@@ -497,7 +506,6 @@
           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
 
       /* bigger than interval, queue config */
-      /* FIXME should convert timestamps to running time */
       if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
         GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
         send_config = TRUE;
@@ -518,8 +526,8 @@
       GST_BUFFER_PTS (buffer) = timestamp;
       size = gst_buffer_get_size (buffer);
 
-      if (timestamp != -1) {
-        rtpmp4vpay->last_config = timestamp;
+      if (running_time != -1) {
+        rtpmp4vpay->last_config = running_time;
       }
     }
   }
diff --git a/gst/rtp/gstrtptheorapay.c b/gst/rtp/gstrtptheorapay.c
index f9839bc..61fb90b 100644
--- a/gst/rtp/gstrtptheorapay.c
+++ b/gst/rtp/gstrtptheorapay.c
@@ -819,17 +819,21 @@
   if (keyframe && (rtptheorapay->config_interval > 0) &&
       rtptheorapay->config_data) {
     gboolean send_config = FALSE;
+    GstClockTime running_time =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        timestamp);
 
     if (rtptheorapay->last_config != -1) {
       guint64 diff;
 
       GST_LOG_OBJECT (rtptheorapay,
           "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtptheorapay->last_config));
+          GST_TIME_ARGS (running_time),
+          GST_TIME_ARGS (rtptheorapay->last_config));
 
       /* calculate diff between last config in milliseconds */
-      if (timestamp > rtptheorapay->last_config) {
-        diff = timestamp - rtptheorapay->last_config;
+      if (running_time > rtptheorapay->last_config) {
+        diff = running_time - rtptheorapay->last_config;
       } else {
         diff = 0;
       }
@@ -838,7 +842,6 @@
           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
 
       /* bigger than interval, queue config */
-      /* FIXME should convert timestamps to running time */
       if (GST_TIME_AS_SECONDS (diff) >= rtptheorapay->config_interval) {
         GST_DEBUG_OBJECT (rtptheorapay, "time to send config");
         send_config = TRUE;
@@ -856,8 +859,8 @@
           NULL, rtptheorapay->config_data, rtptheorapay->config_size,
           timestamp, GST_CLOCK_TIME_NONE, rtptheorapay->config_extra_len);
 
-      if (timestamp != -1) {
-        rtptheorapay->last_config = timestamp;
+      if (running_time != -1) {
+        rtptheorapay->last_config = running_time;
       }
     }
   }
diff --git a/gst/rtp/gstrtpvorbispay.c b/gst/rtp/gstrtpvorbispay.c
index b6bb507..9220f37 100644
--- a/gst/rtp/gstrtpvorbispay.c
+++ b/gst/rtp/gstrtpvorbispay.c
@@ -829,17 +829,21 @@
   /* there is a config request, see if we need to insert it */
   if (rtpvorbispay->config_interval > 0 && rtpvorbispay->config_data) {
     gboolean send_config = FALSE;
+    GstClockTime running_time =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        timestamp);
 
     if (rtpvorbispay->last_config != -1) {
       guint64 diff;
 
       GST_LOG_OBJECT (rtpvorbispay,
           "now %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpvorbispay->last_config));
+          GST_TIME_ARGS (running_time),
+          GST_TIME_ARGS (rtpvorbispay->last_config));
 
       /* calculate diff between last config in milliseconds */
-      if (timestamp > rtpvorbispay->last_config) {
-        diff = timestamp - rtpvorbispay->last_config;
+      if (running_time > rtpvorbispay->last_config) {
+        diff = running_time - rtpvorbispay->last_config;
       } else {
         diff = 0;
       }
@@ -848,7 +852,6 @@
           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
 
       /* bigger than interval, queue config */
-      /* FIXME should convert timestamps to running time */
       if (GST_TIME_AS_SECONDS (diff) >= rtpvorbispay->config_interval) {
         GST_DEBUG_OBJECT (rtpvorbispay, "time to send config");
         send_config = TRUE;
@@ -866,8 +869,8 @@
           NULL, rtpvorbispay->config_data, rtpvorbispay->config_size,
           timestamp, GST_CLOCK_TIME_NONE, rtpvorbispay->config_extra_len);
 
-      if (timestamp != -1) {
-        rtpvorbispay->last_config = timestamp;
+      if (running_time != -1) {
+        rtpvorbispay->last_config = running_time;
       }
     }
   }