rtprtxreceive: use GST_DEBUG_OBJECT / GST_WARNING_OBJECT instead of GST_DEBUG / g_warning
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtprtxreceive.c
1 /* RTP Retransmission receiver element for GStreamer
2  *
3  * gstrtprtxreceive.c:
4  *
5  * Copyright (C) 2013 Collabora Ltd.
6  *   @author Julien Isorce <julien.isorce@collabora.co.uk>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:element-rtprtxreceive
26  * @see_also: rtprtxsend, rtpsession, rtpjitterbuffer
27  *
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
37  * requester SSRC.
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
41  *
42  * <refsect2>
43  * <title>Example pipelines</title>
44  * |[
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)
52  * |[
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
66  * through SDP).
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.
78  * |[
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.
92  * |[
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)
111  * </refsect2>
112  *
113  * Last reviewed on 2013-11-08 (1.x)
114  */
115
116 #ifdef HAVE_CONFIG_H
117 #include "config.h"
118 #endif
119
120 #include <gst/gst.h>
121 #include <gst/rtp/gstrtpbuffer.h>
122 #include <string.h>
123 #include <stdlib.h>
124
125 #include "gstrtprtxreceive.h"
126
127 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_receive_debug);
128 #define GST_CAT_DEFAULT gst_rtp_rtx_receive_debug
129
130 enum
131 {
132   PROP_0,
133   PROP_PAYLOAD_TYPE_MAP,
134   PROP_NUM_RTX_REQUESTS,
135   PROP_NUM_RTX_PACKETS,
136   PROP_NUM_RTX_ASSOC_PACKETS,
137   PROP_LAST
138 };
139
140 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
141     GST_PAD_SRC,
142     GST_PAD_ALWAYS,
143     GST_STATIC_CAPS ("application/x-rtp")
144     );
145
146 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
147     GST_PAD_SINK,
148     GST_PAD_ALWAYS,
149     GST_STATIC_CAPS ("application/x-rtp")
150     );
151
152 static gboolean gst_rtp_rtx_receive_src_event (GstPad * pad, GstObject * parent,
153     GstEvent * event);
154 static GstFlowReturn gst_rtp_rtx_receive_chain (GstPad * pad,
155     GstObject * parent, GstBuffer * buffer);
156
157 static GstStateChangeReturn gst_rtp_rtx_receive_change_state (GstElement *
158     element, GstStateChange transition);
159
160 static void gst_rtp_rtx_receive_set_property (GObject * object, guint prop_id,
161     const GValue * value, GParamSpec * pspec);
162 static void gst_rtp_rtx_receive_get_property (GObject * object, guint prop_id,
163     GValue * value, GParamSpec * pspec);
164 static void gst_rtp_rtx_receive_finalize (GObject * object);
165
166 G_DEFINE_TYPE (GstRtpRtxReceive, gst_rtp_rtx_receive, GST_TYPE_ELEMENT);
167
168 static void
169 gst_rtp_rtx_receive_class_init (GstRtpRtxReceiveClass * klass)
170 {
171   GObjectClass *gobject_class;
172   GstElementClass *gstelement_class;
173
174   gobject_class = (GObjectClass *) klass;
175   gstelement_class = (GstElementClass *) klass;
176
177   gobject_class->get_property = gst_rtp_rtx_receive_get_property;
178   gobject_class->set_property = gst_rtp_rtx_receive_set_property;
179   gobject_class->finalize = gst_rtp_rtx_receive_finalize;
180
181   g_object_class_install_property (gobject_class, PROP_PAYLOAD_TYPE_MAP,
182       g_param_spec_boxed ("payload-type-map", "Payload Type Map",
183           "Map of original payload types to their retransmission payload types",
184           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185
186   g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS,
187       g_param_spec_uint ("num-rtx-requests", "Num RTX Requests",
188           "Number of retransmission events received", 0, G_MAXUINT,
189           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
190
191   g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS,
192       g_param_spec_uint ("num-rtx-packets", "Num RTX Packets",
193           " Number of retransmission packets received", 0, G_MAXUINT,
194           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
195
196   g_object_class_install_property (gobject_class, PROP_NUM_RTX_ASSOC_PACKETS,
197       g_param_spec_uint ("num-rtx-assoc-packets",
198           "Num RTX Associated Packets", "Number of retransmission packets "
199           "correctly associated with retransmission requests", 0, G_MAXUINT,
200           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201
202   gst_element_class_add_pad_template (gstelement_class,
203       gst_static_pad_template_get (&src_factory));
204   gst_element_class_add_pad_template (gstelement_class,
205       gst_static_pad_template_get (&sink_factory));
206
207   gst_element_class_set_static_metadata (gstelement_class,
208       "RTP Retransmission receiver", "Codec",
209       "Receive retransmitted RTP packets according to RFC4588",
210       "Julien Isorce <julien.isorce@collabora.co.uk>");
211
212   gstelement_class->change_state =
213       GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_change_state);
214 }
215
216 static void
217 gst_rtp_rtx_receive_reset (GstRtpRtxReceive * rtx)
218 {
219   GST_OBJECT_LOCK (rtx);
220   g_hash_table_remove_all (rtx->ssrc2_ssrc1_map);
221   g_hash_table_remove_all (rtx->seqnum_ssrc1_map);
222   rtx->num_rtx_requests = 0;
223   rtx->num_rtx_packets = 0;
224   rtx->num_rtx_assoc_packets = 0;
225   GST_OBJECT_UNLOCK (rtx);
226 }
227
228 static void
229 gst_rtp_rtx_receive_finalize (GObject * object)
230 {
231   GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
232
233   g_hash_table_unref (rtx->ssrc2_ssrc1_map);
234   g_hash_table_unref (rtx->seqnum_ssrc1_map);
235   g_hash_table_unref (rtx->rtx_pt_map);
236   if (rtx->rtx_pt_map_structure)
237     gst_structure_free (rtx->rtx_pt_map_structure);
238
239   G_OBJECT_CLASS (gst_rtp_rtx_receive_parent_class)->finalize (object);
240 }
241
242 static void
243 gst_rtp_rtx_receive_init (GstRtpRtxReceive * rtx)
244 {
245   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
246
247   rtx->srcpad =
248       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
249           "src"), "src");
250   GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
251   GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
252   gst_pad_set_event_function (rtx->srcpad,
253       GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_src_event));
254   gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
255
256   rtx->sinkpad =
257       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
258           "sink"), "sink");
259   GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
260   GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
261   gst_pad_set_chain_function (rtx->sinkpad,
262       GST_DEBUG_FUNCPTR (gst_rtp_rtx_receive_chain));
263   gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
264
265   rtx->ssrc2_ssrc1_map = g_hash_table_new (g_direct_hash, g_direct_equal);
266   rtx->seqnum_ssrc1_map = g_hash_table_new (g_direct_hash, g_direct_equal);
267
268   rtx->rtx_pt_map = g_hash_table_new (g_direct_hash, g_direct_equal);
269 }
270
271 static gboolean
272 gst_rtp_rtx_receive_src_event (GstPad * pad, GstObject * parent,
273     GstEvent * event)
274 {
275   GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (parent);
276   gboolean res;
277
278   switch (GST_EVENT_TYPE (event)) {
279     case GST_EVENT_CUSTOM_UPSTREAM:
280     {
281       const GstStructure *s = gst_event_get_structure (event);
282
283       /* This event usually comes from the downstream gstrtpjitterbuffer */
284       if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
285         guint seqnum = 0;
286         guint ssrc = 0;
287         gpointer ssrc1 = 0;
288         gpointer ssrc2 = 0;
289
290         /* retrieve seqnum of the packet that need to be restransmisted */
291         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
292           seqnum = -1;
293
294         /* retrieve ssrc of the packet that need to be restransmisted
295          * it's usefull when reconstructing the original packet from the rtx packet */
296         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
297           ssrc = -1;
298
299         GST_DEBUG_OBJECT (rtx,
300             "request seqnum: %" G_GUINT32_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
301             seqnum, ssrc);
302
303         GST_OBJECT_LOCK (rtx);
304
305         /* increase number of seen requests for our statistics */
306         ++rtx->num_rtx_requests;
307
308         /* First, we lookup in our map to see if we have already associate this
309          * master stream ssrc with its retransmisted stream.
310          * Every ssrc are unique so we can use the same hash table
311          * for both retrieving the ssrc1 from ssrc2 and also ssrc2 from ssrc1
312          */
313         if (g_hash_table_lookup_extended (rtx->ssrc2_ssrc1_map,
314                 GUINT_TO_POINTER (ssrc), NULL, &ssrc2)
315             && GPOINTER_TO_UINT (ssrc2) != GPOINTER_TO_UINT (ssrc)) {
316           GST_DEBUG_OBJECT (rtx, "Retransmited stream %" G_GUINT32_FORMAT
317               " already associated to its master", GPOINTER_TO_UINT (ssrc2));
318         } else {
319           /* not already associated but also we have to check that we have not
320            * already considered this request.
321            */
322           if (g_hash_table_lookup_extended (rtx->seqnum_ssrc1_map,
323                   GUINT_TO_POINTER (seqnum), NULL, &ssrc1)) {
324             if (GPOINTER_TO_UINT (ssrc1) == ssrc) {
325               /* do nothing because we have already considered this request
326                * The jitter may be too impatient of the rtx packet has been
327                * lost too.
328                * It does not mean we reject the event, we still want to forward
329                * the request to the gstrtpsession to be translater into a FB NACK
330                */
331               GST_DEBUG_OBJECT (rtx, "Duplicated request seqnum: %"
332                   G_GUINT32_FORMAT ", ssrc1: %" G_GUINT32_FORMAT, seqnum, ssrc);
333             } else {
334               /* From RFC 4588:
335                * the receiver MUST NOT have two outstanding requests for the
336                * same packet sequence number in two different original streams
337                * before the association is resolved. Otherwise it's impossible
338                * to associate a rtx stream and its master stream
339                */
340               GST_DEBUG_OBJECT (rtx,
341                   "reject request for seqnum %" G_GUINT32_FORMAT
342                   "of master stream %" G_GUINT32_FORMAT, seqnum, ssrc);
343               res = TRUE;
344
345               /* remove seqnum in order to reuse the spot */
346               g_hash_table_remove (rtx->seqnum_ssrc1_map,
347                   GUINT_TO_POINTER (seqnum));
348
349               /* do not forward the event as we are rejecting this request */
350               GST_OBJECT_UNLOCK (rtx);
351               gst_event_unref (event);
352               return res;
353             }
354           } else {
355             /* the request has not been already considered
356              * insert it for the first time */
357             GST_DEBUG_OBJECT (rtx,
358                 "packet number %" G_GUINT32_FORMAT " of master stream %"
359                 G_GUINT32_FORMAT " needs to be retransmited", seqnum, ssrc);
360             g_hash_table_insert (rtx->seqnum_ssrc1_map,
361                 GUINT_TO_POINTER (seqnum), GUINT_TO_POINTER (ssrc));
362           }
363         }
364
365         GST_OBJECT_UNLOCK (rtx);
366       }
367       /* Transfer event upstream so that the request can acutally by translated
368        * through gstrtpsession through the network */
369       res = gst_pad_event_default (pad, parent, event);
370       break;
371     }
372     default:
373       res = gst_pad_event_default (pad, parent, event);
374       break;
375   }
376   return res;
377 }
378
379 /* Copy fixed header and extension. Replace current ssrc by ssrc1,
380  * remove OSN and replace current seq num by OSN.
381  * Copy memory to avoid to manually copy each rtp buffer field.
382  */
383 static GstBuffer *
384 _gst_rtp_buffer_new_from_rtx (GstRTPBuffer * rtp, guint32 ssrc1,
385     guint16 orign_seqnum, guint8 origin_payload_type)
386 {
387   GstMemory *mem = NULL;
388   GstRTPBuffer new_rtp = GST_RTP_BUFFER_INIT;
389   GstBuffer *new_buffer = gst_buffer_new ();
390   GstMapInfo map;
391   guint payload_len = 0;
392
393   /* copy fixed header */
394   mem = gst_memory_copy (rtp->map[0].memory, 0, rtp->size[0]);
395   gst_buffer_append_memory (new_buffer, mem);
396
397   /* copy extension if any */
398   if (rtp->size[1]) {
399     mem = gst_memory_copy (rtp->map[1].memory, 0, rtp->size[1]);
400     gst_buffer_append_memory (new_buffer, mem);
401   }
402
403   /* copy payload and remove OSN */
404   payload_len = rtp->size[2] - 2;
405   mem = gst_allocator_alloc (NULL, payload_len, NULL);
406
407   gst_memory_map (mem, &map, GST_MAP_WRITE);
408   if (rtp->size[2])
409     memcpy (map.data, (guint8 *) rtp->data[2] + 2, payload_len);
410   gst_memory_unmap (mem, &map);
411   gst_buffer_append_memory (new_buffer, mem);
412
413   /* the sender always constructs rtx packets without padding,
414    * But the receiver can still receive rtx packets with padding.
415    * So just copy it.
416    */
417   if (rtp->size[3]) {
418     guint pad_len = rtp->size[3];
419
420     mem = gst_allocator_alloc (NULL, pad_len, NULL);
421
422     gst_memory_map (mem, &map, GST_MAP_WRITE);
423     map.data[pad_len - 1] = pad_len;
424     gst_memory_unmap (mem, &map);
425
426     gst_buffer_append_memory (new_buffer, mem);
427   }
428
429   /* set ssrc and seq num */
430   gst_rtp_buffer_map (new_buffer, GST_MAP_WRITE, &new_rtp);
431   gst_rtp_buffer_set_ssrc (&new_rtp, ssrc1);
432   gst_rtp_buffer_set_seq (&new_rtp, orign_seqnum);
433   gst_rtp_buffer_set_payload_type (&new_rtp, origin_payload_type);
434   gst_rtp_buffer_unmap (&new_rtp);
435
436   return new_buffer;
437 }
438
439 static GstFlowReturn
440 gst_rtp_rtx_receive_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
441 {
442   GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (parent);
443   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
444   GstFlowReturn ret = GST_FLOW_OK;
445   GstBuffer *new_buffer = NULL;
446   guint32 ssrc = 0;
447   gpointer ssrc1 = 0;
448   guint32 ssrc2 = 0;
449   guint16 seqnum = 0;
450   guint16 orign_seqnum = 0;
451   guint8 payload_type = 0;
452   guint8 origin_payload_type = 0;
453   gboolean is_rtx = FALSE;
454   gboolean drop = FALSE;
455
456   /* map current rtp packet to parse its header */
457   gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
458   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
459   seqnum = gst_rtp_buffer_get_seq (&rtp);
460   payload_type = gst_rtp_buffer_get_payload_type (&rtp);
461
462   /* check if we have a retransmission packet (this information comes from SDP) */
463   GST_OBJECT_LOCK (rtx);
464
465   is_rtx =
466       g_hash_table_lookup_extended (rtx->rtx_pt_map,
467       GUINT_TO_POINTER (payload_type), NULL, NULL);
468
469   GST_OBJECT_UNLOCK (rtx);
470
471   if (is_rtx) {
472     /* read OSN in the rtx payload */
473     orign_seqnum = GST_READ_UINT16_BE (gst_rtp_buffer_get_payload (&rtp));
474     origin_payload_type =
475         GPOINTER_TO_UINT (g_hash_table_lookup (rtx->rtx_pt_map,
476             GUINT_TO_POINTER (payload_type)));
477   }
478
479   GST_OBJECT_LOCK (rtx);
480
481   /* if the current packet is from a retransmission stream */
482   if (is_rtx) {
483     /* increase our statistic */
484     ++rtx->num_rtx_packets;
485
486     /* first we check if we already have associated this retransmission stream
487      * to a master stream */
488     if (g_hash_table_lookup_extended (rtx->ssrc2_ssrc1_map,
489             GUINT_TO_POINTER (ssrc), NULL, &ssrc1)) {
490       GST_DEBUG_OBJECT (rtx,
491           "packet is from retransmission stream %" G_GUINT32_FORMAT
492           " already associated to master stream %" G_GUINT32_FORMAT, ssrc,
493           GPOINTER_TO_UINT (ssrc1));
494       ssrc2 = ssrc;
495     } else {
496       /* the current retransmisted packet has its rtx stream not already
497        * associated to a master stream, so retrieve it from our request
498        * history */
499       if (g_hash_table_lookup_extended (rtx->seqnum_ssrc1_map,
500               GUINT_TO_POINTER (orign_seqnum), NULL, &ssrc1)) {
501         GST_DEBUG_OBJECT (rtx,
502             "associate retransmisted stream %" G_GUINT32_FORMAT
503             " to master stream %" G_GUINT32_FORMAT " thanks to packet %"
504             G_GUINT16_FORMAT "", ssrc, GPOINTER_TO_UINT (ssrc1), orign_seqnum);
505         ssrc2 = ssrc;
506
507         /* free the spot so that this seqnum can be used to do another
508          * association */
509         g_hash_table_remove (rtx->seqnum_ssrc1_map,
510             GUINT_TO_POINTER (orign_seqnum));
511
512         /* actually do the association between rtx stream and master stream */
513         g_hash_table_insert (rtx->ssrc2_ssrc1_map, GUINT_TO_POINTER (ssrc2),
514             ssrc1);
515
516         /* just put a guard */
517         if (GPOINTER_TO_UINT (ssrc1) == ssrc2)
518           GST_WARNING_OBJECT (rtx, "RTX receiver ssrc2_ssrc1_map bad state, "
519               "ssrc %" G_GUINT32_FORMAT " are the same\n", ssrc);
520
521         /* also do the association between master stream and rtx stream
522          * every ssrc are unique so we can use the same hash table
523          * for both retrieving the ssrc1 from ssrc2 and also ssrc2 from ssrc1
524          */
525         g_hash_table_insert (rtx->ssrc2_ssrc1_map, ssrc1,
526             GUINT_TO_POINTER (ssrc2));
527       } else {
528         /* we are not able to associate this rtx packet with a master stream */
529         GST_DEBUG_OBJECT (rtx,
530             "drop rtx packet because its orign_seqnum %" G_GUINT16_FORMAT
531             " is not in pending retransmission requests", orign_seqnum);
532         drop = TRUE;
533       }
534     }
535   }
536
537   /* if not dropped the packet was successfully associated */
538   if (is_rtx && !drop)
539     ++rtx->num_rtx_assoc_packets;
540
541   GST_OBJECT_UNLOCK (rtx);
542
543   /* just drop the packet if the association could not have been made */
544   if (drop) {
545     gst_rtp_buffer_unmap (&rtp);
546     gst_buffer_unref (buffer);
547     return GST_FLOW_OK;
548   }
549
550   /* create the retransmission packet */
551   if (is_rtx)
552     new_buffer =
553         _gst_rtp_buffer_new_from_rtx (&rtp, GPOINTER_TO_UINT (ssrc1),
554         orign_seqnum, origin_payload_type);
555
556   gst_rtp_buffer_unmap (&rtp);
557
558   /* push the packet */
559   if (is_rtx) {
560     gst_buffer_unref (buffer);
561     GST_LOG_OBJECT (rtx, "push packet seqnum:%" G_GUINT16_FORMAT
562         " from a restransmission stream ssrc2:%" G_GUINT32_FORMAT " (src %"
563         G_GUINT32_FORMAT ")", orign_seqnum, ssrc2, GPOINTER_TO_UINT (ssrc1));
564     ret = gst_pad_push (rtx->srcpad, new_buffer);
565   } else {
566     GST_LOG_OBJECT (rtx, "push packet seqnum:%" G_GUINT16_FORMAT
567         " from a master stream ssrc: %" G_GUINT32_FORMAT, seqnum, ssrc);
568     ret = gst_pad_push (rtx->srcpad, buffer);
569   }
570
571   return ret;
572 }
573
574 static void
575 gst_rtp_rtx_receive_get_property (GObject * object,
576     guint prop_id, GValue * value, GParamSpec * pspec)
577 {
578   GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
579
580   switch (prop_id) {
581     case PROP_PAYLOAD_TYPE_MAP:
582       GST_OBJECT_LOCK (rtx);
583       g_value_set_boxed (value, rtx->rtx_pt_map_structure);
584       GST_OBJECT_UNLOCK (rtx);
585       break;
586     case PROP_NUM_RTX_REQUESTS:
587       GST_OBJECT_LOCK (rtx);
588       g_value_set_uint (value, rtx->num_rtx_requests);
589       GST_OBJECT_UNLOCK (rtx);
590       break;
591     case PROP_NUM_RTX_PACKETS:
592       GST_OBJECT_LOCK (rtx);
593       g_value_set_uint (value, rtx->num_rtx_packets);
594       GST_OBJECT_UNLOCK (rtx);
595       break;
596     case PROP_NUM_RTX_ASSOC_PACKETS:
597       GST_OBJECT_LOCK (rtx);
598       g_value_set_uint (value, rtx->num_rtx_assoc_packets);
599       GST_OBJECT_UNLOCK (rtx);
600       break;
601     default:
602       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
603       break;
604   }
605 }
606
607 static gboolean
608 structure_to_hash_table_inv (GQuark field_id, const GValue * value,
609     gpointer hash)
610 {
611   const gchar *field_str;
612   guint field_uint;
613   guint value_uint;
614
615   field_str = g_quark_to_string (field_id);
616   field_uint = atoi (field_str);
617   value_uint = g_value_get_uint (value);
618   g_hash_table_insert ((GHashTable *) hash, GUINT_TO_POINTER (value_uint),
619       GUINT_TO_POINTER (field_uint));
620
621   return TRUE;
622 }
623
624 static void
625 gst_rtp_rtx_receive_set_property (GObject * object,
626     guint prop_id, const GValue * value, GParamSpec * pspec)
627 {
628   GstRtpRtxReceive *rtx = GST_RTP_RTX_RECEIVE (object);
629
630   switch (prop_id) {
631     case PROP_PAYLOAD_TYPE_MAP:
632       GST_OBJECT_LOCK (rtx);
633       if (rtx->rtx_pt_map_structure)
634         gst_structure_free (rtx->rtx_pt_map_structure);
635       rtx->rtx_pt_map_structure = g_value_dup_boxed (value);
636       g_hash_table_remove_all (rtx->rtx_pt_map);
637       gst_structure_foreach (rtx->rtx_pt_map_structure,
638           structure_to_hash_table_inv, rtx->rtx_pt_map);
639       GST_OBJECT_UNLOCK (rtx);
640       break;
641     default:
642       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643       break;
644   }
645 }
646
647 static GstStateChangeReturn
648 gst_rtp_rtx_receive_change_state (GstElement * element,
649     GstStateChange transition)
650 {
651   GstStateChangeReturn ret;
652   GstRtpRtxReceive *rtx;
653
654   rtx = GST_RTP_RTX_RECEIVE (element);
655
656   switch (transition) {
657     default:
658       break;
659   }
660
661   ret =
662       GST_ELEMENT_CLASS (gst_rtp_rtx_receive_parent_class)->change_state
663       (element, transition);
664
665   switch (transition) {
666     case GST_STATE_CHANGE_PAUSED_TO_READY:
667       gst_rtp_rtx_receive_reset (rtx);
668       break;
669     default:
670       break;
671   }
672
673   return ret;
674 }
675
676 gboolean
677 gst_rtp_rtx_receive_plugin_init (GstPlugin * plugin)
678 {
679   GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_receive_debug, "rtprtxreceive", 0,
680       "rtp retransmission receiver");
681
682   return gst_element_register (plugin, "rtprtxreceive", GST_RANK_NONE,
683       GST_TYPE_RTP_RTX_RECEIVE);
684 }