rtpmanager: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtprtxqueue.c
index cdf3b03..97678eb 100644 (file)
 
 /**
  * SECTION:element-rtprtxqueue
+ * @title: rtprtxqueue
+ *
+ * rtprtxqueue maintains a queue of transmitted RTP packets, up to a
+ * configurable limit (see #GstRTPRtxQueue:max-size-time,
+ * #GstRTPRtxQueue:max-size-packets), and retransmits them upon request
+ * from the downstream rtpsession (GstRTPRetransmissionRequest event).
+ *
+ * This element is similar to rtprtxsend, but it has differences:
+ * - Retransmission from rtprtxqueue is not RFC 4588 compliant. The
+ * retransmitted packets have the same ssrc and payload type as the original
+ * stream.
+ * - As a side-effect of the above, rtprtxqueue does not require the use of
+ * rtprtxreceive on the receiving end. rtpjitterbuffer alone is able to
+ * reconstruct the stream.
+ * - Retransmission from rtprtxqueue happens as soon as the next regular flow
+ * packet is chained, while rtprtxsend retransmits as soon as the retransmission
+ * event is received, using a helper thread.
+ * - rtprtxqueue can be used with rtpbin without the need of hooking to its
+ * #GstRtpBin::request-aux-sender signal, which means it can be used with
+ * rtpbin using gst-launch.
+ *
+ * See also #GstRtpRtxSend, #GstRtpRtxReceive
+ *
+ * # Example pipelines
+ *
+ * |[
+ * gst-launch-1.0 rtpbin name=b rtp-profile=avpf \
+ *    audiotestsrc is-live=true ! opusenc ! rtpopuspay pt=96 ! rtprtxqueue ! b.send_rtp_sink_0 \
+ *    b.send_rtp_src_0 ! identity drop-probability=0.01 ! udpsink host="127.0.0.1" port=5000 \
+ *    udpsrc port=5001 ! b.recv_rtcp_sink_0 \
+ *    b.send_rtcp_src_0 ! udpsink host="127.0.0.1" port=5002 sync=false async=false
+ * ]|
+ * Sender pipeline
+ *
+ * |[
+ * gst-launch-1.0 rtpbin name=b rtp-profile=avpf do-retransmission=true \
+ *    udpsrc port=5000 caps="application/x-rtp,media=(string)audio,clock-rate=(int)48000,encoding-name=(string)OPUS,payload=(int)96" ! \
+ *        b.recv_rtp_sink_0 \
+ *    b. ! rtpopusdepay ! opusdec ! audioconvert ! audioresample ! autoaudiosink \
+ *    udpsrc port=5002 ! b.recv_rtcp_sink_0 \
+ *    b.send_rtcp_src_0 ! udpsink host="127.0.0.1" port=5001 sync=false async=false
+ * ]|
+ * Receiver pipeline
  */
 
 #ifdef HAVE_CONFIG_H
@@ -63,6 +106,8 @@ static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
 
 static gboolean gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent,
     GstEvent * event);
+static gboolean gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
+    GstEvent * event);
 static GstFlowReturn gst_rtp_rtx_queue_chain (GstPad * pad, GstObject * parent,
     GstBuffer * buffer);
 static GstFlowReturn gst_rtp_rtx_queue_chain_list (GstPad * pad,
@@ -170,6 +215,8 @@ gst_rtp_rtx_queue_init (GstRTPRtxQueue * rtx)
           "sink"), "sink");
   GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
   GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
+  gst_pad_set_event_function (rtx->sinkpad,
+      GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_sink_event));
   gst_pad_set_chain_function (rtx->sinkpad,
       GST_DEBUG_FUNCPTR (gst_rtp_rtx_queue_chain));
   gst_pad_set_chain_list_function (rtx->sinkpad,
@@ -200,7 +247,8 @@ push_seqnum (GstBuffer * buffer, RTXData * data)
   if (data->found)
     return;
 
-  if (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
+  if (!GST_IS_BUFFER (buffer) ||
+      !gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer))
     return;
 
   seqnum = gst_rtp_buffer_get_seq (&rtpbuffer);
@@ -256,6 +304,29 @@ gst_rtp_rtx_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
   return res;
 }
 
+static gboolean
+gst_rtp_rtx_queue_sink_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
+{
+  GstRTPRtxQueue *rtx = GST_RTP_RTX_QUEUE (parent);
+  gboolean res;
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_SEGMENT:
+    {
+      g_mutex_lock (&rtx->lock);
+      gst_event_copy_segment (event, &rtx->head_segment);
+      g_queue_push_head (rtx->queue, gst_event_ref (event));
+      g_mutex_unlock (&rtx->lock);
+      /* fall through */
+    }
+    default:
+      res = gst_pad_event_default (pad, parent, event);
+      break;
+  }
+  return res;
+}
+
 static void
 do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
 {
@@ -263,6 +334,38 @@ do_push (GstBuffer * buffer, GstRTPRtxQueue * rtx)
   gst_pad_push (rtx->srcpad, buffer);
 }
 
+static guint32
+get_ts_diff (GstRTPRtxQueue * rtx)
+{
+  GstClockTime high_ts, low_ts;
+  GstClockTimeDiff result;
+  GstBuffer *high_buf, *low_buf;
+
+  high_buf = g_queue_peek_head (rtx->queue);
+
+  while (GST_IS_EVENT ((low_buf = g_queue_peek_tail (rtx->queue)))) {
+    GstEvent *event = g_queue_pop_tail (rtx->queue);
+    gst_event_copy_segment (event, &rtx->tail_segment);
+    gst_event_unref (event);
+  }
+
+  if (!high_buf || !low_buf || high_buf == low_buf)
+    return 0;
+
+  high_ts = GST_BUFFER_TIMESTAMP (high_buf);
+  low_ts = GST_BUFFER_TIMESTAMP (low_buf);
+
+  high_ts = gst_segment_to_running_time (&rtx->head_segment, GST_FORMAT_TIME,
+      high_ts);
+  low_ts = gst_segment_to_running_time (&rtx->tail_segment, GST_FORMAT_TIME,
+      low_ts);
+
+  result = high_ts - low_ts;
+
+  /* return value in ms instead of ns */
+  return (guint32) gst_util_uint64_scale_int (result, 1, GST_MSECOND);
+}
+
 /* Must be called with rtx->lock */
 static void
 shrink_queue (GstRTPRtxQueue * rtx)
@@ -271,6 +374,10 @@ shrink_queue (GstRTPRtxQueue * rtx)
     while (g_queue_get_length (rtx->queue) > rtx->max_size_packets)
       gst_buffer_unref (g_queue_pop_tail (rtx->queue));
   }
+  if (rtx->max_size_time) {
+    while (get_ts_diff (rtx) > rtx->max_size_time)
+      gst_buffer_unref (g_queue_pop_tail (rtx->queue));
+  }
 }
 
 static GstFlowReturn