1 /* RTP Retransmission sender element for GStreamer
5 * Copyright (C) 2013 Collabora Ltd.
6 * @author Julien Isorce <julien.isorce@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:element-rtprtxsend
27 * See #GstRtpRtxReceive for examples
29 * The purpose of the sender RTX object is to keep a history of RTP packets up
30 * to a configurable limit (max-size-time or max-size-packets). It will listen
31 * for upstream custom retransmission events (GstRTPRetransmissionRequest) that
32 * comes from downstream (#GstRtpSession). When receiving a request it will
33 * look up the requested seqnum in its list of stored packets. If the packet
34 * is available, it will create a RTX packet according to RFC 4588 and send
35 * this as an auxiliary stream. RTX is SSRC-multiplexed
43 #include <gst/rtp/gstrtpbuffer.h>
46 #include "gstrtprtxsend.h"
48 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_send_debug);
49 #define GST_CAT_DEFAULT gst_rtp_rtx_send_debug
51 #define DEFAULT_RTX_PAYLOAD_TYPE 0
52 #define DEFAULT_MAX_SIZE_TIME 0
53 #define DEFAULT_MAX_SIZE_PACKETS 100
58 PROP_RTX_PAYLOAD_TYPE,
60 PROP_MAX_SIZE_PACKETS,
61 PROP_NUM_RTX_REQUESTS,
66 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS ("application/x-rtp")
72 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_STATIC_CAPS ("application/x-rtp")
78 static gboolean gst_rtp_rtx_send_src_event (GstPad * pad, GstObject * parent,
80 static GstFlowReturn gst_rtp_rtx_send_chain (GstPad * pad, GstObject * parent,
83 static GstStateChangeReturn gst_rtp_rtx_send_change_state (GstElement *
84 element, GstStateChange transition);
86 static void gst_rtp_rtx_send_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_rtp_rtx_send_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
90 static void gst_rtp_rtx_send_finalize (GObject * object);
92 G_DEFINE_TYPE (GstRtpRtxSend, gst_rtp_rtx_send, GST_TYPE_ELEMENT);
102 buffer_queue_item_free (BufferQueueItem * item)
104 gst_buffer_unref (item->buffer);
109 gst_rtp_rtx_send_class_init (GstRtpRtxSendClass * klass)
111 GObjectClass *gobject_class;
112 GstElementClass *gstelement_class;
114 gobject_class = (GObjectClass *) klass;
115 gstelement_class = (GstElementClass *) klass;
117 gobject_class->get_property = gst_rtp_rtx_send_get_property;
118 gobject_class->set_property = gst_rtp_rtx_send_set_property;
119 gobject_class->finalize = gst_rtp_rtx_send_finalize;
121 g_object_class_install_property (gobject_class, PROP_RTX_PAYLOAD_TYPE,
122 g_param_spec_uint ("rtx-payload-type", "RTX Payload Type",
123 "Payload type of the retransmission stream (fmtp in SDP)", 0,
124 G_MAXUINT, DEFAULT_RTX_PAYLOAD_TYPE,
125 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
128 g_param_spec_uint ("max-size-time", "Max Size Times",
129 "Amount of ms to queue (0 = unlimited)", 0, G_MAXUINT,
130 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_PACKETS,
133 g_param_spec_uint ("max-size-packets", "Max Size Packets",
134 "Amount of packets to queue (0 = unlimited)", 0, G_MAXUINT,
135 DEFAULT_MAX_SIZE_PACKETS,
136 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS,
139 g_param_spec_uint ("num-rtx-requests", "Num RTX Requests",
140 "Number of retransmission events received", 0, G_MAXUINT,
141 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
143 g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS,
144 g_param_spec_uint ("num-rtx-packets", "Num RTX Packets",
145 " Number of retransmission packets sent", 0, G_MAXUINT,
146 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
148 gst_element_class_add_pad_template (gstelement_class,
149 gst_static_pad_template_get (&src_factory));
150 gst_element_class_add_pad_template (gstelement_class,
151 gst_static_pad_template_get (&sink_factory));
153 gst_element_class_set_static_metadata (gstelement_class,
154 "RTP Retransmission Sender", "Codec",
155 "Retransmit RTP packets when needed, according to RFC4588",
156 "Julien Isorce <julien.isorce@collabora.co.uk>");
158 gstelement_class->change_state =
159 GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_change_state);
163 gst_rtp_rtx_send_reset (GstRtpRtxSend * rtx, gboolean full)
165 g_mutex_lock (&rtx->lock);
166 g_queue_foreach (rtx->queue, (GFunc) buffer_queue_item_free, NULL);
167 g_queue_clear (rtx->queue);
168 g_list_foreach (rtx->pending, (GFunc) gst_buffer_unref, NULL);
169 g_list_free (rtx->pending);
171 rtx->master_ssrc = 0;
172 rtx->next_seqnum = g_random_int_range (0, G_MAXUINT16);
173 rtx->rtx_ssrc = g_random_int ();
174 rtx->num_rtx_requests = 0;
175 rtx->num_rtx_packets = 0;
176 g_mutex_unlock (&rtx->lock);
180 gst_rtp_rtx_send_finalize (GObject * object)
182 GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
184 gst_rtp_rtx_send_reset (rtx, TRUE);
185 g_queue_free (rtx->queue);
186 g_mutex_clear (&rtx->lock);
188 G_OBJECT_CLASS (gst_rtp_rtx_send_parent_class)->finalize (object);
192 gst_rtp_rtx_send_init (GstRtpRtxSend * rtx)
194 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
197 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
199 GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
200 GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
201 gst_pad_set_event_function (rtx->srcpad,
202 GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_src_event));
203 gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
206 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
208 GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
209 GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
210 gst_pad_set_chain_function (rtx->sinkpad,
211 GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_chain));
212 gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
214 rtx->queue = g_queue_new ();
216 g_mutex_init (&rtx->lock);
218 rtx->next_seqnum = g_random_int_range (0, G_MAXUINT16);
219 rtx->rtx_ssrc = g_random_int ();
221 rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
222 rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
226 choose_ssrc (GstRtpRtxSend * rtx)
231 ssrc = g_random_int ();
233 /* make sure to be different than master */
234 if (ssrc != rtx->master_ssrc)
247 /* traverse queue history and try to find the buffer that the
248 * requested seqnum */
250 push_seqnum (BufferQueueItem * item, RTXData * data)
252 GstRtpRtxSend *rtx = data->rtx;
257 /* data->seqnum comes from the request */
258 if (item->seqnum == data->seqnum) {
260 GST_DEBUG_OBJECT (rtx, "found %" G_GUINT16_FORMAT, item->seqnum);
261 rtx->pending = g_list_prepend (rtx->pending, gst_buffer_ref (item->buffer));
266 gst_rtp_rtx_send_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
268 GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
271 switch (GST_EVENT_TYPE (event)) {
272 case GST_EVENT_CUSTOM_UPSTREAM:
274 const GstStructure *s = gst_event_get_structure (event);
276 /* This event usually comes from the downstream gstrtpsession */
277 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
282 /* retrieve seqnum of the packet that need to be restransmisted */
283 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
286 /* retrieve ssrc of the packet that need to be restransmisted */
287 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
290 GST_DEBUG_OBJECT (rtx,
291 "request seqnum: %" G_GUINT16_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
294 g_mutex_lock (&rtx->lock);
295 /* check if request is for us */
296 if (rtx->master_ssrc == ssrc) {
297 ++rtx->num_rtx_requests;
299 data.seqnum = seqnum;
301 /* TODO do a binary search because rtx->queue is sorted by seq num */
302 g_queue_foreach (rtx->queue, (GFunc) push_seqnum, &data);
304 g_mutex_unlock (&rtx->lock);
306 gst_event_unref (event);
309 /* This event usually comes from the downstream gstrtpsession */
310 } else if (gst_structure_has_name (s, "GstRTPCollision")) {
313 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
316 GST_DEBUG_OBJECT (rtx, "collision ssrc: %" G_GUINT32_FORMAT, ssrc);
318 g_mutex_lock (&rtx->lock);
320 /* choose another ssrc for our retransmited stream */
321 if (ssrc == rtx->rtx_ssrc) {
322 rtx->rtx_ssrc = choose_ssrc (rtx);
324 /* clear buffers we already saved */
325 g_queue_foreach (rtx->queue, (GFunc) gst_buffer_unref, NULL);
326 g_queue_clear (rtx->queue);
328 /* clear buffers that are about to be retransmited */
329 g_list_foreach (rtx->pending, (GFunc) gst_buffer_unref, NULL);
330 g_list_free (rtx->pending);
333 g_mutex_unlock (&rtx->lock);
335 /* no need to forward to payloader because we make sure to have
338 gst_event_unref (event);
341 g_mutex_unlock (&rtx->lock);
343 /* forward event to payloader in case collided ssrc is
345 res = gst_pad_event_default (pad, parent, event);
348 res = gst_pad_event_default (pad, parent, event);
353 res = gst_pad_event_default (pad, parent, event);
359 /* Copy fixed header and extension. Add OSN before to copy payload
360 * Copy memory to avoid to manually copy each rtp buffer field.
363 _gst_rtp_rtx_buffer_new (GstBuffer * buffer, guint32 ssrc, guint16 seqnum,
366 GstMemory *mem = NULL;
367 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
368 GstRTPBuffer new_rtp = GST_RTP_BUFFER_INIT;
369 GstBuffer *new_buffer = gst_buffer_new ();
371 guint payload_len = 0;
373 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
375 /* gst_rtp_buffer_map does not map the payload so do it now */
376 gst_rtp_buffer_get_payload (&rtp);
378 /* If payload type is not set through SDP/property then
379 * just bump the value */
381 fmtp = gst_rtp_buffer_get_payload_type (&rtp) + 1;
383 /* copy fixed header */
384 mem = gst_memory_copy (rtp.map[0].memory, 0, rtp.size[0]);
385 gst_buffer_append_memory (new_buffer, mem);
387 /* copy extension if any */
389 mem = gst_memory_copy (rtp.map[1].memory, 0, rtp.size[1]);
390 gst_buffer_append_memory (new_buffer, mem);
393 /* copy payload and add OSN just before */
394 payload_len = 2 + rtp.size[2];
395 mem = gst_allocator_alloc (NULL, payload_len, NULL);
397 gst_memory_map (mem, &map, GST_MAP_WRITE);
398 GST_WRITE_UINT16_BE (map.data, gst_rtp_buffer_get_seq (&rtp));
400 memcpy (map.data + 2, rtp.data[2], rtp.size[2]);
401 gst_memory_unmap (mem, &map);
402 gst_buffer_append_memory (new_buffer, mem);
404 /* everything needed is copied */
405 gst_rtp_buffer_unmap (&rtp);
407 /* set ssrc, seqnum and fmtp */
408 gst_rtp_buffer_map (new_buffer, GST_MAP_WRITE, &new_rtp);
409 gst_rtp_buffer_set_ssrc (&new_rtp, ssrc);
410 gst_rtp_buffer_set_seq (&new_rtp, seqnum);
411 gst_rtp_buffer_set_payload_type (&new_rtp, fmtp);
412 /* RFC 4588: let other elements do the padding, as normal */
413 gst_rtp_buffer_set_padding (&new_rtp, FALSE);
414 gst_rtp_buffer_unmap (&new_rtp);
419 /* psuh pending retransmission packet.
420 * it constructs rtx packet from original paclets */
422 do_push (GstBuffer * buffer, GstRtpRtxSend * rtx)
424 /* RFC4588 two streams multiplexed by sending them in the same session using
425 * different SSRC values, i.e., SSRC-multiplexing. */
426 GST_DEBUG_OBJECT (rtx,
427 "retransmit seqnum: %" G_GUINT16_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
428 rtx->next_seqnum, rtx->rtx_ssrc);
429 gst_pad_push (rtx->srcpad, _gst_rtp_rtx_buffer_new (buffer, rtx->rtx_ssrc,
430 rtx->next_seqnum++, rtx->rtx_payload_type));
434 gst_rtp_rtx_send_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
436 GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
437 GstFlowReturn ret = GST_FLOW_ERROR;
438 GList *pending = NULL;
439 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
440 BufferQueueItem *item;
442 guint32 ssrc, rtptime;
444 rtx = GST_RTP_RTX_SEND (parent);
446 /* read the information we want from the buffer */
447 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
448 seqnum = gst_rtp_buffer_get_seq (&rtp);
449 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
450 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
451 gst_rtp_buffer_unmap (&rtp);
453 g_mutex_lock (&rtx->lock);
455 /* retrieve master stream ssrc */
456 rtx->master_ssrc = ssrc;
457 /* check if our initial aux ssrc is equal to master */
458 if (rtx->rtx_ssrc == rtx->master_ssrc)
461 /* add current rtp buffer to queue history */
462 item = g_new0 (BufferQueueItem, 1);
463 item->seqnum = seqnum;
464 item->timestamp = rtptime;
465 item->buffer = gst_buffer_ref (buffer);
466 g_queue_push_head (rtx->queue, item);
468 /* remove oldest packets from history if they are too many */
469 if (rtx->max_size_packets) {
470 while (g_queue_get_length (rtx->queue) > rtx->max_size_packets)
471 buffer_queue_item_free (g_queue_pop_tail (rtx->queue));
474 /* within lock, get packets that have to be retransmited */
475 pending = rtx->pending;
478 /* update statistics - assume we will succeed to retransmit those packets */
479 rtx->num_rtx_packets += g_list_length (pending);
481 /* transfer payload type while holding the lock */
482 rtx->rtx_payload_type = rtx->rtx_payload_type_pending;
484 /* no need to hold the lock to push rtx packets */
485 g_mutex_unlock (&rtx->lock);
487 /* retransmit requested packets */
488 g_list_foreach (pending, (GFunc) do_push, rtx);
489 g_list_foreach (pending, (GFunc) gst_buffer_unref, NULL);
490 g_list_free (pending);
493 "push seqnum: %" G_GUINT16_FORMAT ", ssrc: %" G_GUINT32_FORMAT, seqnum,
496 /* push current rtp packet */
497 ret = gst_pad_push (rtx->srcpad, buffer);
503 gst_rtp_rtx_send_get_property (GObject * object,
504 guint prop_id, GValue * value, GParamSpec * pspec)
506 GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
509 case PROP_RTX_PAYLOAD_TYPE:
510 g_mutex_lock (&rtx->lock);
511 g_value_set_uint (value, rtx->rtx_payload_type_pending);
512 g_mutex_unlock (&rtx->lock);
514 case PROP_MAX_SIZE_TIME:
515 g_mutex_lock (&rtx->lock);
516 g_value_set_uint (value, rtx->max_size_time);
517 g_mutex_unlock (&rtx->lock);
519 case PROP_MAX_SIZE_PACKETS:
520 g_mutex_lock (&rtx->lock);
521 g_value_set_uint (value, rtx->max_size_packets);
522 g_mutex_unlock (&rtx->lock);
524 case PROP_NUM_RTX_REQUESTS:
525 g_mutex_lock (&rtx->lock);
526 g_value_set_uint (value, rtx->num_rtx_requests);
527 g_mutex_unlock (&rtx->lock);
529 case PROP_NUM_RTX_PACKETS:
530 g_mutex_lock (&rtx->lock);
531 g_value_set_uint (value, rtx->num_rtx_packets);
532 g_mutex_unlock (&rtx->lock);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gst_rtp_rtx_send_set_property (GObject * object,
542 guint prop_id, const GValue * value, GParamSpec * pspec)
544 GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
547 case PROP_RTX_PAYLOAD_TYPE:
548 g_mutex_lock (&rtx->lock);
549 rtx->rtx_payload_type_pending = g_value_get_uint (value);
550 g_mutex_unlock (&rtx->lock);
552 case PROP_MAX_SIZE_TIME:
553 g_mutex_lock (&rtx->lock);
554 rtx->max_size_time = g_value_get_uint (value);
555 g_mutex_unlock (&rtx->lock);
557 case PROP_MAX_SIZE_PACKETS:
558 g_mutex_lock (&rtx->lock);
559 rtx->max_size_packets = g_value_get_uint (value);
560 g_mutex_unlock (&rtx->lock);
563 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
568 static GstStateChangeReturn
569 gst_rtp_rtx_send_change_state (GstElement * element, GstStateChange transition)
571 GstStateChangeReturn ret;
574 rtx = GST_RTP_RTX_SEND (element);
576 switch (transition) {
582 GST_ELEMENT_CLASS (gst_rtp_rtx_send_parent_class)->change_state (element,
585 switch (transition) {
586 case GST_STATE_CHANGE_PAUSED_TO_READY:
587 gst_rtp_rtx_send_reset (rtx, TRUE);
597 gst_rtp_rtx_send_plugin_init (GstPlugin * plugin)
599 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_send_debug, "rtprtxsend", 0,
600 "rtp retransmission sender");
602 return gst_element_register (plugin, "rtprtxsend", GST_RANK_NONE,
603 GST_TYPE_RTP_RTX_SEND);