1 /* RTP Retransmission receiver 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-rtprtxreceive
26 * @see_also: rtprtxsend, rtpsession, rtpjitterbuffer
28 * The receiver will listen to the custom retransmission events from the
29 * downstream jitterbuffer and will remember the SSRC1 of the stream and
30 * seqnum that was requested. When it sees a packet with one of the stored
31 * seqnum, it associates the SSRC2 of the stream with the SSRC1 of the
32 * master stream. From then it knows that SSRC2 is the retransmission
33 * stream of SSRC1. This algorithm is stated in RFC 4588. For this
34 * algorithm to work, RFC4588 also states that no two pending retransmission
35 * requests can exist for the same seqnum and different SSRCs or else it
36 * would be impossible to associate the retransmission with the original
38 * When the RTX receiver has associated the retransmission packets,
39 * it can depayload and forward them to the source pad of the element.
40 * RTX is SSRC-multiplexed. See #GstRtpRtxSend
43 * <title>Example pipelines</title>
45 * gst-launch-1.0 rtpsession name=rtpsession \
46 * audiotestsrc ! speexenc ! rtpspeexpay pt=97 ! rtprtxsend rtx-payload-type=99 ! \
47 * identity drop-probability=0.1 ! rtpsession.send_rtp_sink \
48 * rtpsession.send_rtp_src ! udpsink host="127.0.0.1" port=5000 \
49 * udpsrc port=5001 ! rtpsession.recv_rtcp_sink \
50 * rtpsession.send_rtcp_src ! udpsink host="127.0.0.1" port=5002 sync=false async=false
51 * ]| Send audio stream through port 5000. (5001 and 5002 are just the rtcp link with the receiver)
53 * gst-launch-1.0 rtpsession name=rtpsession \
54 * udpsrc port=5000 caps="application/x-rtp,media=(string)audio,clock-rate=(int)44100,encoding-name=(string)SPEEX,encoding-params=(string)1,octet-align=(string)1" ! \
55 * rtpsession.recv_rtp_sink \
56 * rtpsession.recv_rtp_src ! rtprtxreceive rtx-payload-types="99" ! rtpjitterbuffer do-retransmission=true ! rtpspeexdepay ! \
57 * speexdec ! audioconvert ! autoaudiosink \
58 * rtpsession.send_rtcp_src ! udpsink host="127.0.0.1" port=5001 \
59 * udpsrc port=5002 ! rtpsession.recv_rtcp_sink sync=fakse async=false
60 * ]| Receive audio stream from port 5000. (5001 and 5002 are just the rtcp link with the sender)
61 * On sender side make sure to use a different payload type for the stream and
62 * its associated retransmission stream (see #GstRtpRtxSend). Note that several retransmission streams can
63 * have the same payload type so this is not deterministic. Actually the
64 * rtprtxreceiver element does the association using seqnum values.
65 * On receiver side set all the retransmission payload types (Those informations are retrieve
67 * You should still hear a clear sound when setting drop-probability to something greater than 0.
68 * The rtpjitterbuffer will generate a custom upstream event GstRTPRetransmissionRequest when
69 * it assumes that one packet is missing. Then this request is translated to a FB NACK in the rtcp link
70 * Finally the rtpsession of the sender side re-convert it in a GstRTPRetransmissionRequest that will
71 * be handle by rtprtxsend.
72 * When increasing this value it may be possible that even the retransmission stream would be dropped
73 * so the receiver will ask to resend the packets again and again until it actually receive them.
74 * If the value is too high the rtprtxsend will not be able to retrieve the packet in its list of
75 * stored packets. For learning purpose you could try to increase the max-size-packets or max-size-time
76 * rtprtxsender's properties.
77 * Also note that you should use rtprtxsend through rtpbin and its set-aux-send property. See #GstRtpBin.
79 * gst-launch-1.0 rtpsession name=rtpsession0 \
80 * audiotestsrc wave=0 ! speexenc ! rtpspeexpay pt=97 ! rtprtxsend rtx-payload-type=99 seqnum-offset=1 ! \
81 * identity drop-probability=0.1 ! rtpsession0.send_rtp_sink \
82 * rtpsession0.send_rtp_src ! udpsink host="127.0.0.1" port=5000 \
83 * udpsrc port=5001 ! rtpsession0.recv_rtcp_sink \
84 * rtpsession0.send_rtcp_src ! udpsink host="127.0.0.1" port=5002 sync=false async=false \
85 * rtpsession name=rtpsession1 \
86 * audiotestsrc wave=0 ! speexenc ! rtpspeexpay pt=97 ! rtprtxsend rtx-payload-type=99 seqnum-offset=10 ! \
87 * identity drop-probability=0.1 ! rtpsession1.send_rtp_sink \
88 * rtpsession1.send_rtp_src ! udpsink host="127.0.0.1" port=5000 \
89 * udpsrc port=5004 ! rtpsession1.recv_rtcp_sink \
90 * rtpsession1.send_rtcp_src ! udpsink host="127.0.0.1" port=5002 sync=false async=false
91 * ]| Send two audio streams to port 5000.
93 * gst-launch-1.0 rtpsession name=rtpsession
94 * udpsrc port=5000 caps="application/x-rtp,media=(string)audio,clock-rate=(int)44100,encoding-name=(string)SPEEX,encoding-params=(string)1,octet-align=(string)1" ! \
95 * rtpsession.recv_rtp_sink \
96 * rtpsession.recv_rtp_src ! rtprtxreceive rtx-payload-types="99" ! rtpssrcdemux name=demux \
97 * demux. ! queue ! rtpjitterbuffer do-retransmission=true ! rtpspeexdepay ! speexdec ! audioconvert ! autoaudiosink \
98 * demux. ! queue ! rtpjitterbuffer do-retransmission=true ! rtpspeexdepay ! speexdec ! audioconvert ! autoaudiosink \
99 * rtpsession.send_rtcp_src ! ! tee name=t ! queue ! udpsink host="127.0.0.1" port=5001 t. ! queue ! udpsink host="127.0.0.1" port=5004 \
100 * udpsrc port=5002 ! rtpsession.recv_rtcp_sink sync=fakse async=false
101 * ]| Receive audio stream from port 5000.
102 * On sender side the two streams have the same payload type for master streams, Same about retransmission streams.
103 * The streams are sent to the network through two distincts sessions.
104 * But we need to set a different seqnum-offset to make sure their seqnum navigate at a different rate like in concrete cases.
105 * We could also choose the same seqnum offset but we would require to set a different initial seqnum value.
106 * This is also why the rtprtxreceive can succeed to do the association between master and retransmission stream.
107 * On receiver side the same session is used to receive the two streams. So the rtpssrcdemux is here to demultiplex
108 * those two streams. The rtprtxreceive is responsible for reconstructing the original packets from the two retransmission streams.
109 * You can play with the drop-probability value for one or both streams.
110 * You should hear a clear sound. (after a few seconds the two streams wave feel synchronized)
119 #include <gst/rtp/gstrtpbuffer.h>
123 #include "gstrtprtxreceive.h"
125 #define ASSOC_TIMEOUT (GST_SECOND)
127 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_receive_debug);
128 #define GST_CAT_DEFAULT gst_rtp_rtx_receive_debug
133 PROP_PAYLOAD_TYPE_MAP,
134 PROP_NUM_RTX_REQUESTS,
135 PROP_NUM_RTX_PACKETS,
136 PROP_NUM_RTX_ASSOC_PACKETS
139 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
142 GST_STATIC_CAPS ("application/x-rtp")
145 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
148 GST_STATIC_CAPS ("application/x-rtp")
151 static gboolean gst_rtp_rtx_receive_src_event (GstPad * pad, GstObject * parent,
153 static GstFlowReturn gst_rtp_rtx_receive_chain (GstPad * pad,
154 GstObject * parent, GstBuffer * buffer);
156 static GstStateChangeReturn gst_rtp_rtx_receive_change_state (GstElement *
157 element, GstStateChange transition);
159 static void gst_rtp_rtx_receive_set_property (GObject * object, guint prop_id,
160 const GValue * value, GParamSpec * pspec);
161 static void gst_rtp_rtx_receive_get_property (GObject * object, guint prop_id,
162 GValue * value, GParamSpec * pspec);
163 static void gst_rtp_rtx_receive_finalize (GObject * object);
165 G_DEFINE_TYPE (GstRtpRtxReceive, gst_rtp_rtx_receive, GST_TYPE_ELEMENT);
168 gst_rtp_rtx_receive_class_init (GstRtpRtxReceiveClass * klass)
170 GObjectClass *gobject_class;
171 GstElementClass *gstelement_class;
173 gobject_class = (GObjectClass *) klass;
174 gstelement_class = (GstElementClass *) klass;
176 gobject_class->get_property = gst_rtp_rtx_receive_get_property;
177 gobject_class->set_property = gst_rtp_rtx_receive_set_property;
178 gobject_class->finalize = gst_rtp_rtx_receive_finalize;
180 g_object_class_install_property (gobject_class, PROP_PAYLOAD_TYPE_MAP,
181 g_param_spec_boxed ("payload-type-map", "Payload Type Map",
182 "Map of original payload types to their retransmission payload types",
183 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS,
186 g_param_spec_uint ("num-rtx-requests", "Num RTX Requests",
187 "Number of retransmission events received", 0, G_MAXUINT,
188 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
190 g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS,
191 g_param_spec_uint ("num-rtx-packets", "Num RTX Packets",
192 " Number of retransmission packets received", 0, G_MAXUINT,
193 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (gobject_class, PROP_NUM_RTX_ASSOC_PACKETS,
196 g_param_spec_uint ("num-rtx-assoc-packets",
197 "Num RTX Associated Packets", "Number of retransmission packets "
198 "correctly associated with retransmission requests", 0, G_MAXUINT,
199 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
202 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
204 gst_element_class_set_static_metadata (gstelement_class,
205 "RTP Retransmission receiver", "Codec",
206 "Receive retransmitted RTP packets according to RFC4588",
207 "Julien Isorce <julien.isorce@collabora.co.uk>");
209 gstelement_class->change_state =
210 GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_change_state);
214 gst_rtp_rtx_receive_reset (GstRtpRtxReceive * rtx)
216 GST_OBJECT_LOCK (rtx);
217 g_hash_table_remove_all (rtx->ssrc2_ssrc1_map);
218 g_hash_table_remove_all (rtx->seqnum_ssrc1_map);
219 rtx->num_rtx_requests = 0;
220 rtx->num_rtx_packets = 0;
221 rtx->num_rtx_assoc_packets = 0;
222 GST_OBJECT_UNLOCK (rtx);
226 gst_rtp_rtx_receive_finalize (GObject * object)
228 GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
230 g_hash_table_unref (rtx->ssrc2_ssrc1_map);
231 g_hash_table_unref (rtx->seqnum_ssrc1_map);
232 g_hash_table_unref (rtx->rtx_pt_map);
233 if (rtx->rtx_pt_map_structure)
234 gst_structure_free (rtx->rtx_pt_map_structure);
236 G_OBJECT_CLASS (gst_rtp_rtx_receive_parent_class)->finalize (object);
246 ssrc_assoc_new (guint32 ssrc, GstClockTime time)
248 SsrcAssoc *assoc = g_slice_new (SsrcAssoc);
257 ssrc_assoc_free (SsrcAssoc * assoc)
259 g_slice_free (SsrcAssoc, assoc);
263 gst_rtp_rtx_receive_init (GstRtpRtxReceive * rtx)
265 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
268 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
270 GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
271 GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
272 gst_pad_set_event_function (rtx->srcpad,
273 GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_src_event));
274 gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
277 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
279 GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
280 GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
281 gst_pad_set_chain_function (rtx->sinkpad,
282 GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_chain));
283 gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
285 rtx->ssrc2_ssrc1_map = g_hash_table_new (g_direct_hash, g_direct_equal);
286 rtx->seqnum_ssrc1_map = g_hash_table_new_full (g_direct_hash, g_direct_equal,
287 NULL, (GDestroyNotify) ssrc_assoc_free);
289 rtx->rtx_pt_map = g_hash_table_new (g_direct_hash, g_direct_equal);
293 gst_rtp_rtx_receive_src_event (GstPad * pad, GstObject * parent,
296 GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (parent);
299 switch (GST_EVENT_TYPE (event)) {
300 case GST_EVENT_CUSTOM_UPSTREAM:
302 const GstStructure *s = gst_event_get_structure (event);
304 /* This event usually comes from the downstream gstrtpjitterbuffer */
305 if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
310 /* retrieve seqnum of the packet that need to be retransmitted */
311 if (!gst_structure_get_uint (s, "seqnum", &seqnum))
314 /* retrieve ssrc of the packet that need to be retransmitted
315 * it's useful when reconstructing the original packet from the rtx packet */
316 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
319 GST_DEBUG_OBJECT (rtx,
320 "request seqnum: %" G_GUINT32_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
323 GST_OBJECT_LOCK (rtx);
325 /* increase number of seen requests for our statistics */
326 ++rtx->num_rtx_requests;
328 /* First, we lookup in our map to see if we have already associate this
329 * master stream ssrc with its retransmitted stream.
330 * Every ssrc are unique so we can use the same hash table
331 * for both retrieving the ssrc1 from ssrc2 and also ssrc2 from ssrc1
333 if (g_hash_table_lookup_extended (rtx->ssrc2_ssrc1_map,
334 GUINT_TO_POINTER (ssrc), NULL, &ssrc2)
335 && GPOINTER_TO_UINT (ssrc2) != GPOINTER_TO_UINT (ssrc)) {
336 GST_DEBUG_OBJECT (rtx, "Retransmited stream %" G_GUINT32_FORMAT
337 " already associated to its master", GPOINTER_TO_UINT (ssrc2));
341 /* not already associated but also we have to check that we have not
342 * already considered this request.
344 if (g_hash_table_lookup_extended (rtx->seqnum_ssrc1_map,
345 GUINT_TO_POINTER (seqnum), NULL, (gpointer *) & assoc)) {
346 if (assoc->ssrc == ssrc) {
347 /* do nothing because we have already considered this request
348 * The jitter may be too impatient of the rtx packet has been
350 * It does not mean we reject the event, we still want to forward
351 * the request to the gstrtpsession to be translater into a FB NACK
353 GST_DEBUG_OBJECT (rtx, "Duplicated request seqnum: %"
354 G_GUINT32_FORMAT ", ssrc1: %" G_GUINT32_FORMAT, seqnum, ssrc);
357 /* If the association attempt is larger than ASSOC_TIMEOUT,
358 * then we give up on it, and try this one.
360 if (!GST_CLOCK_TIME_IS_VALID (rtx->last_time) ||
361 !GST_CLOCK_TIME_IS_VALID (assoc->time) ||
362 assoc->time + ASSOC_TIMEOUT < rtx->last_time) {
364 * the receiver MUST NOT have two outstanding requests for the
365 * same packet sequence number in two different original streams
366 * before the association is resolved. Otherwise it's impossible
367 * to associate a rtx stream and its master stream
370 /* remove seqnum in order to reuse the spot */
371 g_hash_table_remove (rtx->seqnum_ssrc1_map,
372 GUINT_TO_POINTER (seqnum));
375 GST_DEBUG_OBJECT (rtx,
376 "reject request for seqnum %" G_GUINT32_FORMAT
377 " of master stream %" G_GUINT32_FORMAT, seqnum, ssrc);
379 /* do not forward the event as we are rejecting this request */
380 GST_OBJECT_UNLOCK (rtx);
381 gst_event_unref (event);
387 /* the request has not been already considered
388 * insert it for the first time */
389 g_hash_table_insert (rtx->seqnum_ssrc1_map,
390 GUINT_TO_POINTER (seqnum),
391 ssrc_assoc_new (ssrc, rtx->last_time));
395 GST_DEBUG_OBJECT (rtx,
396 "packet number %" G_GUINT32_FORMAT " of master stream %"
397 G_GUINT32_FORMAT " needs to be retransmitted", seqnum, ssrc);
399 GST_OBJECT_UNLOCK (rtx);
402 /* Transfer event upstream so that the request can acutally by translated
403 * through gstrtpsession through the network */
404 res = gst_pad_event_default (pad, parent, event);
408 res = gst_pad_event_default (pad, parent, event);
414 /* Copy fixed header and extension. Replace current ssrc by ssrc1,
415 * remove OSN and replace current seq num by OSN.
416 * Copy memory to avoid to manually copy each rtp buffer field.
419 _gst_rtp_buffer_new_from_rtx (GstRTPBuffer * rtp, guint32 ssrc1,
420 guint16 orign_seqnum, guint8 origin_payload_type)
422 GstMemory *mem = NULL;
423 GstRTPBuffer new_rtp = GST_RTP_BUFFER_INIT;
424 GstBuffer *new_buffer = gst_buffer_new ();
426 guint payload_len = 0;
428 /* copy fixed header */
429 mem = gst_memory_copy (rtp->map[0].memory,
430 (guint8 *) rtp->data[0] - rtp->map[0].data, rtp->size[0]);
431 gst_buffer_append_memory (new_buffer, mem);
433 /* copy extension if any */
435 mem = gst_memory_copy (rtp->map[1].memory,
436 (guint8 *) rtp->data[1] - rtp->map[1].data, rtp->size[1]);
437 gst_buffer_append_memory (new_buffer, mem);
440 /* copy payload and remove OSN */
441 payload_len = rtp->size[2] - 2;
442 mem = gst_allocator_alloc (NULL, payload_len, NULL);
444 gst_memory_map (mem, &map, GST_MAP_WRITE);
446 memcpy (map.data, (guint8 *) rtp->data[2] + 2, payload_len);
447 gst_memory_unmap (mem, &map);
448 gst_buffer_append_memory (new_buffer, mem);
450 /* the sender always constructs rtx packets without padding,
451 * But the receiver can still receive rtx packets with padding.
455 guint pad_len = rtp->size[3];
457 mem = gst_allocator_alloc (NULL, pad_len, NULL);
459 gst_memory_map (mem, &map, GST_MAP_WRITE);
460 map.data[pad_len - 1] = pad_len;
461 gst_memory_unmap (mem, &map);
463 gst_buffer_append_memory (new_buffer, mem);
466 /* set ssrc and seq num */
467 gst_rtp_buffer_map (new_buffer, GST_MAP_WRITE, &new_rtp);
468 gst_rtp_buffer_set_ssrc (&new_rtp, ssrc1);
469 gst_rtp_buffer_set_seq (&new_rtp, orign_seqnum);
470 gst_rtp_buffer_set_payload_type (&new_rtp, origin_payload_type);
471 gst_rtp_buffer_unmap (&new_rtp);
473 gst_buffer_copy_into (new_buffer, rtp->buffer,
474 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
475 GST_BUFFER_FLAG_SET (new_buffer, GST_RTP_BUFFER_FLAG_RETRANSMISSION);
481 gst_rtp_rtx_receive_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
483 GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (parent);
484 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
485 GstFlowReturn ret = GST_FLOW_OK;
486 GstBuffer *new_buffer = NULL;
491 guint16 orign_seqnum = 0;
492 guint8 payload_type = 0;
493 guint8 origin_payload_type = 0;
495 gboolean drop = FALSE;
497 /* map current rtp packet to parse its header */
498 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
499 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
500 seqnum = gst_rtp_buffer_get_seq (&rtp);
501 payload_type = gst_rtp_buffer_get_payload_type (&rtp);
503 /* check if we have a retransmission packet (this information comes from SDP) */
504 GST_OBJECT_LOCK (rtx);
506 rtx->last_time = GST_BUFFER_PTS (buffer);
508 if (g_hash_table_size (rtx->seqnum_ssrc1_map) > 0) {
512 g_hash_table_iter_init (&iter, rtx->seqnum_ssrc1_map);
513 while (g_hash_table_iter_next (&iter, &key, &value)) {
514 SsrcAssoc *assoc = value;
516 /* remove association request if it is too old */
517 if (GST_CLOCK_TIME_IS_VALID (rtx->last_time) &&
518 GST_CLOCK_TIME_IS_VALID (assoc->time) &&
519 assoc->time + ASSOC_TIMEOUT < rtx->last_time) {
520 g_hash_table_iter_remove (&iter);
526 g_hash_table_lookup_extended (rtx->rtx_pt_map,
527 GUINT_TO_POINTER (payload_type), NULL, NULL);
529 /* if the current packet is from a retransmission stream */
531 /* increase our statistic */
532 ++rtx->num_rtx_packets;
534 /* read OSN in the rtx payload */
535 orign_seqnum = GST_READ_UINT16_BE (gst_rtp_buffer_get_payload (&rtp));
536 origin_payload_type =
537 GPOINTER_TO_UINT (g_hash_table_lookup (rtx->rtx_pt_map,
538 GUINT_TO_POINTER (payload_type)));
540 /* first we check if we already have associated this retransmission stream
541 * to a master stream */
542 if (g_hash_table_lookup_extended (rtx->ssrc2_ssrc1_map,
543 GUINT_TO_POINTER (ssrc), NULL, &ssrc1)) {
544 GST_DEBUG_OBJECT (rtx,
545 "packet is from retransmission stream %" G_GUINT32_FORMAT
546 " already associated to master stream %" G_GUINT32_FORMAT, ssrc,
547 GPOINTER_TO_UINT (ssrc1));
552 /* the current retransmitted packet has its rtx stream not already
553 * associated to a master stream, so retrieve it from our request
555 if (g_hash_table_lookup_extended (rtx->seqnum_ssrc1_map,
556 GUINT_TO_POINTER (orign_seqnum), NULL, (gpointer *) & assoc)) {
557 GST_DEBUG_OBJECT (rtx,
558 "associate retransmitted stream %" G_GUINT32_FORMAT
559 " to master stream %" G_GUINT32_FORMAT " thanks to packet %"
560 G_GUINT16_FORMAT "", ssrc, assoc->ssrc, orign_seqnum);
561 ssrc1 = GUINT_TO_POINTER (assoc->ssrc);
564 /* just put a guard */
565 if (GPOINTER_TO_UINT (ssrc1) == ssrc2)
566 GST_WARNING_OBJECT (rtx, "RTX receiver ssrc2_ssrc1_map bad state, "
567 "ssrc %" G_GUINT32_FORMAT " are the same\n", ssrc);
569 /* free the spot so that this seqnum can be used to do another
571 g_hash_table_remove (rtx->seqnum_ssrc1_map,
572 GUINT_TO_POINTER (orign_seqnum));
574 /* actually do the association between rtx stream and master stream */
575 g_hash_table_insert (rtx->ssrc2_ssrc1_map, GUINT_TO_POINTER (ssrc2),
578 /* also do the association between master stream and rtx stream
579 * every ssrc are unique so we can use the same hash table
580 * for both retrieving the ssrc1 from ssrc2 and also ssrc2 from ssrc1
582 g_hash_table_insert (rtx->ssrc2_ssrc1_map, ssrc1,
583 GUINT_TO_POINTER (ssrc2));
586 /* we are not able to associate this rtx packet with a master stream */
587 GST_DEBUG_OBJECT (rtx,
588 "drop rtx packet because its orign_seqnum %" G_GUINT16_FORMAT
589 " is not in pending retransmission requests", orign_seqnum);
595 /* if not dropped the packet was successfully associated */
597 ++rtx->num_rtx_assoc_packets;
599 GST_OBJECT_UNLOCK (rtx);
601 /* just drop the packet if the association could not have been made */
603 gst_rtp_buffer_unmap (&rtp);
604 gst_buffer_unref (buffer);
608 /* create the retransmission packet */
611 _gst_rtp_buffer_new_from_rtx (&rtp, GPOINTER_TO_UINT (ssrc1),
612 orign_seqnum, origin_payload_type);
614 gst_rtp_buffer_unmap (&rtp);
616 /* push the packet */
618 gst_buffer_unref (buffer);
619 GST_LOG_OBJECT (rtx, "push packet seqnum:%" G_GUINT16_FORMAT
620 " from a restransmission stream ssrc2:%" G_GUINT32_FORMAT " (src %"
621 G_GUINT32_FORMAT ")", orign_seqnum, ssrc2, GPOINTER_TO_UINT (ssrc1));
622 ret = gst_pad_push (rtx->srcpad, new_buffer);
624 GST_LOG_OBJECT (rtx, "push packet seqnum:%" G_GUINT16_FORMAT
625 " from a master stream ssrc: %" G_GUINT32_FORMAT, seqnum, ssrc);
626 ret = gst_pad_push (rtx->srcpad, buffer);
633 gst_rtp_rtx_receive_get_property (GObject * object,
634 guint prop_id, GValue * value, GParamSpec * pspec)
636 GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
639 case PROP_PAYLOAD_TYPE_MAP:
640 GST_OBJECT_LOCK (rtx);
641 g_value_set_boxed (value, rtx->rtx_pt_map_structure);
642 GST_OBJECT_UNLOCK (rtx);
644 case PROP_NUM_RTX_REQUESTS:
645 GST_OBJECT_LOCK (rtx);
646 g_value_set_uint (value, rtx->num_rtx_requests);
647 GST_OBJECT_UNLOCK (rtx);
649 case PROP_NUM_RTX_PACKETS:
650 GST_OBJECT_LOCK (rtx);
651 g_value_set_uint (value, rtx->num_rtx_packets);
652 GST_OBJECT_UNLOCK (rtx);
654 case PROP_NUM_RTX_ASSOC_PACKETS:
655 GST_OBJECT_LOCK (rtx);
656 g_value_set_uint (value, rtx->num_rtx_assoc_packets);
657 GST_OBJECT_UNLOCK (rtx);
660 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
666 structure_to_hash_table_inv (GQuark field_id, const GValue * value,
669 const gchar *field_str;
673 field_str = g_quark_to_string (field_id);
674 field_uint = atoi (field_str);
675 value_uint = g_value_get_uint (value);
676 g_hash_table_insert ((GHashTable *) hash, GUINT_TO_POINTER (value_uint),
677 GUINT_TO_POINTER (field_uint));
683 gst_rtp_rtx_receive_set_property (GObject * object,
684 guint prop_id, const GValue * value, GParamSpec * pspec)
686 GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
689 case PROP_PAYLOAD_TYPE_MAP:
690 GST_OBJECT_LOCK (rtx);
691 if (rtx->rtx_pt_map_structure)
692 gst_structure_free (rtx->rtx_pt_map_structure);
693 rtx->rtx_pt_map_structure = g_value_dup_boxed (value);
694 g_hash_table_remove_all (rtx->rtx_pt_map);
695 gst_structure_foreach (rtx->rtx_pt_map_structure,
696 structure_to_hash_table_inv, rtx->rtx_pt_map);
697 GST_OBJECT_UNLOCK (rtx);
700 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
705 static GstStateChangeReturn
706 gst_rtp_rtx_receive_change_state (GstElement * element,
707 GstStateChange transition)
709 GstStateChangeReturn ret;
710 GstRtpRtxReceive *rtx;
712 rtx = GST_RTP_RTX_RECEIVE (element);
714 switch (transition) {
720 GST_ELEMENT_CLASS (gst_rtp_rtx_receive_parent_class)->change_state
721 (element, transition);
723 switch (transition) {
724 case GST_STATE_CHANGE_PAUSED_TO_READY:
725 gst_rtp_rtx_receive_reset (rtx);
735 gst_rtp_rtx_receive_plugin_init (GstPlugin * plugin)
737 GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_receive_debug, "rtprtxreceive", 0,
738 "rtp retransmission receiver");
740 return gst_element_register (plugin, "rtprtxreceive", GST_RANK_NONE,
741 GST_TYPE_RTP_RTX_RECEIVE);