rtprtxsend: run a new GstTask on the src pad
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtprtxsend.c
1 /* RTP Retransmission sender element for GStreamer
2  *
3  * gstrtprtxsend.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-rtprtxsend
26  *
27  * See #GstRtpRtxReceive for examples
28  * 
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
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <gst/gst.h>
43 #include <gst/rtp/gstrtpbuffer.h>
44 #include <string.h>
45 #include <stdlib.h>
46
47 #include "gstrtprtxsend.h"
48
49 GST_DEBUG_CATEGORY_STATIC (gst_rtp_rtx_send_debug);
50 #define GST_CAT_DEFAULT gst_rtp_rtx_send_debug
51
52 #define DEFAULT_RTX_PAYLOAD_TYPE 0
53 #define DEFAULT_MAX_SIZE_TIME    0
54 #define DEFAULT_MAX_SIZE_PACKETS 100
55
56 enum
57 {
58   PROP_0,
59   PROP_SSRC_MAP,
60   PROP_PAYLOAD_TYPE_MAP,
61   PROP_MAX_SIZE_TIME,
62   PROP_MAX_SIZE_PACKETS,
63   PROP_NUM_RTX_REQUESTS,
64   PROP_NUM_RTX_PACKETS,
65   PROP_LAST
66 };
67
68 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
69     GST_PAD_SRC,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("application/x-rtp")
72     );
73
74 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("application/x-rtp")
78     );
79
80 static gboolean gst_rtp_rtx_send_queue_check_full (GstDataQueue * queue,
81     guint visible, guint bytes, guint64 time, gpointer checkdata);
82
83 static gboolean gst_rtp_rtx_send_src_event (GstPad * pad, GstObject * parent,
84     GstEvent * event);
85 static gboolean gst_rtp_rtx_send_sink_event (GstPad * pad, GstObject * parent,
86     GstEvent * event);
87 static GstFlowReturn gst_rtp_rtx_send_chain (GstPad * pad, GstObject * parent,
88     GstBuffer * buffer);
89
90 static void gst_rtp_rtx_send_src_loop (GstRtpRtxSend * rtx);
91 static gboolean gst_rtp_rtx_send_activate_mode (GstPad * pad,
92     GstObject * parent, GstPadMode mode, gboolean active);
93
94 static GstStateChangeReturn gst_rtp_rtx_send_change_state (GstElement *
95     element, GstStateChange transition);
96
97 static void gst_rtp_rtx_send_set_property (GObject * object, guint prop_id,
98     const GValue * value, GParamSpec * pspec);
99 static void gst_rtp_rtx_send_get_property (GObject * object, guint prop_id,
100     GValue * value, GParamSpec * pspec);
101 static void gst_rtp_rtx_send_finalize (GObject * object);
102
103 G_DEFINE_TYPE (GstRtpRtxSend, gst_rtp_rtx_send, GST_TYPE_ELEMENT);
104
105 typedef struct
106 {
107   guint16 seqnum;
108   guint32 timestamp;
109   GstBuffer *buffer;
110 } BufferQueueItem;
111
112 static void
113 buffer_queue_item_free (BufferQueueItem * item)
114 {
115   gst_buffer_unref (item->buffer);
116   g_slice_free (BufferQueueItem, item);
117 }
118
119 typedef struct
120 {
121   guint32 rtx_ssrc;
122   guint16 next_seqnum;
123   gint clock_rate;
124
125   /* history of rtp packets */
126   GSequence *queue;
127 } SSRCRtxData;
128
129 static SSRCRtxData *
130 ssrc_rtx_data_new (guint32 rtx_ssrc)
131 {
132   SSRCRtxData *data = g_slice_new0 (SSRCRtxData);
133
134   data->rtx_ssrc = rtx_ssrc;
135   data->next_seqnum = g_random_int_range (0, G_MAXUINT16);
136   data->queue = g_sequence_new ((GDestroyNotify) buffer_queue_item_free);
137
138   return data;
139 }
140
141 static void
142 ssrc_rtx_data_free (SSRCRtxData * data)
143 {
144   g_sequence_free (data->queue);
145   g_slice_free (SSRCRtxData, data);
146 }
147
148 static void
149 gst_rtp_rtx_send_class_init (GstRtpRtxSendClass * klass)
150 {
151   GObjectClass *gobject_class;
152   GstElementClass *gstelement_class;
153
154   gobject_class = (GObjectClass *) klass;
155   gstelement_class = (GstElementClass *) klass;
156
157   gobject_class->get_property = gst_rtp_rtx_send_get_property;
158   gobject_class->set_property = gst_rtp_rtx_send_set_property;
159   gobject_class->finalize = gst_rtp_rtx_send_finalize;
160
161   g_object_class_install_property (gobject_class, PROP_SSRC_MAP,
162       g_param_spec_boxed ("ssrc-map", "SSRC Map",
163           "Map of SSRCs to their retransmission SSRCs for SSRC-multiplexed mode"
164           " (default = random)", GST_TYPE_STRUCTURE,
165           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
166
167   g_object_class_install_property (gobject_class, PROP_PAYLOAD_TYPE_MAP,
168       g_param_spec_boxed ("payload-type-map", "Payload Type Map",
169           "Map of original payload types to their retransmission payload types",
170           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171
172   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
173       g_param_spec_uint ("max-size-time", "Max Size Time",
174           "Amount of ms to queue (0 = unlimited)", 0, G_MAXUINT,
175           DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176
177   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_PACKETS,
178       g_param_spec_uint ("max-size-packets", "Max Size Packets",
179           "Amount of packets to queue (0 = unlimited)", 0, G_MAXINT16,
180           DEFAULT_MAX_SIZE_PACKETS,
181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183   g_object_class_install_property (gobject_class, PROP_NUM_RTX_REQUESTS,
184       g_param_spec_uint ("num-rtx-requests", "Num RTX Requests",
185           "Number of retransmission events received", 0, G_MAXUINT,
186           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
187
188   g_object_class_install_property (gobject_class, PROP_NUM_RTX_PACKETS,
189       g_param_spec_uint ("num-rtx-packets", "Num RTX Packets",
190           " Number of retransmission packets sent", 0, G_MAXUINT,
191           0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
192
193   gst_element_class_add_pad_template (gstelement_class,
194       gst_static_pad_template_get (&src_factory));
195   gst_element_class_add_pad_template (gstelement_class,
196       gst_static_pad_template_get (&sink_factory));
197
198   gst_element_class_set_static_metadata (gstelement_class,
199       "RTP Retransmission Sender", "Codec",
200       "Retransmit RTP packets when needed, according to RFC4588",
201       "Julien Isorce <julien.isorce@collabora.co.uk>");
202
203   gstelement_class->change_state =
204       GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_change_state);
205 }
206
207 static void
208 gst_rtp_rtx_send_reset (GstRtpRtxSend * rtx)
209 {
210   GST_OBJECT_LOCK (rtx);
211   gst_data_queue_flush (rtx->queue);
212   g_hash_table_remove_all (rtx->ssrc_data);
213   g_hash_table_remove_all (rtx->rtx_ssrcs);
214   rtx->num_rtx_requests = 0;
215   rtx->num_rtx_packets = 0;
216   GST_OBJECT_UNLOCK (rtx);
217 }
218
219 static void
220 gst_rtp_rtx_send_finalize (GObject * object)
221 {
222   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
223
224   g_hash_table_unref (rtx->ssrc_data);
225   g_hash_table_unref (rtx->rtx_ssrcs);
226   if (rtx->external_ssrc_map)
227     gst_structure_free (rtx->external_ssrc_map);
228   g_hash_table_unref (rtx->rtx_pt_map);
229   if (rtx->rtx_pt_map_structure)
230     gst_structure_free (rtx->rtx_pt_map_structure);
231   g_object_unref (rtx->queue);
232
233   G_OBJECT_CLASS (gst_rtp_rtx_send_parent_class)->finalize (object);
234 }
235
236 static void
237 gst_rtp_rtx_send_init (GstRtpRtxSend * rtx)
238 {
239   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtx);
240
241   rtx->srcpad =
242       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
243           "src"), "src");
244   GST_PAD_SET_PROXY_CAPS (rtx->srcpad);
245   GST_PAD_SET_PROXY_ALLOCATION (rtx->srcpad);
246   gst_pad_set_event_function (rtx->srcpad,
247       GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_src_event));
248   gst_pad_set_activatemode_function (rtx->srcpad,
249       GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_activate_mode));
250   gst_element_add_pad (GST_ELEMENT (rtx), rtx->srcpad);
251
252   rtx->sinkpad =
253       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
254           "sink"), "sink");
255   GST_PAD_SET_PROXY_CAPS (rtx->sinkpad);
256   GST_PAD_SET_PROXY_ALLOCATION (rtx->sinkpad);
257   gst_pad_set_event_function (rtx->sinkpad,
258       GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_sink_event));
259   gst_pad_set_chain_function (rtx->sinkpad,
260       GST_DEBUG_FUNCPTR (gst_rtp_rtx_send_chain));
261   gst_element_add_pad (GST_ELEMENT (rtx), rtx->sinkpad);
262
263   rtx->queue = gst_data_queue_new (gst_rtp_rtx_send_queue_check_full, NULL,
264       NULL, rtx);
265   rtx->ssrc_data = g_hash_table_new_full (g_direct_hash, g_direct_equal,
266       NULL, (GDestroyNotify) ssrc_rtx_data_free);
267   rtx->rtx_ssrcs = g_hash_table_new (g_direct_hash, g_direct_equal);
268   rtx->rtx_pt_map = g_hash_table_new (g_direct_hash, g_direct_equal);
269
270   rtx->max_size_time = DEFAULT_MAX_SIZE_TIME;
271   rtx->max_size_packets = DEFAULT_MAX_SIZE_PACKETS;
272 }
273
274 static void
275 gst_rtp_rtx_send_set_flushing (GstRtpRtxSend * rtx, gboolean flush)
276 {
277   GST_OBJECT_LOCK (rtx);
278   gst_data_queue_set_flushing (rtx->queue, flush);
279   gst_data_queue_flush (rtx->queue);
280   GST_OBJECT_UNLOCK (rtx);
281 }
282
283 static gboolean
284 gst_rtp_rtx_send_queue_check_full (GstDataQueue * queue,
285     guint visible, guint bytes, guint64 time, gpointer checkdata)
286 {
287   return FALSE;
288 }
289
290 static void
291 gst_rtp_rtx_data_queue_item_free (gpointer item)
292 {
293   GstDataQueueItem *data = item;
294   if (data->object)
295     gst_mini_object_unref (data->object);
296   g_slice_free (GstDataQueueItem, data);
297 }
298
299 static gboolean
300 gst_rtp_rtx_send_push_out (GstRtpRtxSend * rtx, gpointer object)
301 {
302   GstDataQueueItem *data;
303   gboolean success;
304
305   data = g_slice_new0 (GstDataQueueItem);
306   data->object = GST_MINI_OBJECT (object);
307   data->size = 1;
308   data->duration = 1;
309   data->visible = TRUE;
310   data->destroy = gst_rtp_rtx_data_queue_item_free;
311
312   success = gst_data_queue_push (rtx->queue, data);
313
314   if (!success)
315     data->destroy (data);
316
317   return success;
318 }
319
320 static guint32
321 gst_rtp_rtx_send_choose_ssrc (GstRtpRtxSend * rtx, guint32 choice,
322     gboolean consider_choice)
323 {
324   guint32 ssrc = consider_choice ? choice : g_random_int ();
325
326   /* make sure to be different than any other */
327   while (g_hash_table_contains (rtx->ssrc_data, GUINT_TO_POINTER (ssrc)) ||
328       g_hash_table_contains (rtx->rtx_ssrcs, GUINT_TO_POINTER (ssrc))) {
329     ssrc = g_random_int ();
330   }
331
332   return ssrc;
333 }
334
335 static SSRCRtxData *
336 gst_rtp_rtx_send_get_ssrc_data (GstRtpRtxSend * rtx, guint32 ssrc)
337 {
338   SSRCRtxData *data;
339   guint32 rtx_ssrc;
340   gboolean consider = FALSE;
341
342   if (G_UNLIKELY (!g_hash_table_contains (rtx->ssrc_data,
343               GUINT_TO_POINTER (ssrc)))) {
344     if (rtx->external_ssrc_map) {
345       gchar *ssrc_str;
346       ssrc_str = g_strdup_printf ("%" G_GUINT32_FORMAT, ssrc);
347       consider = gst_structure_get_uint (rtx->external_ssrc_map, ssrc_str,
348           &rtx_ssrc);
349       g_free (ssrc_str);
350     }
351     rtx_ssrc = gst_rtp_rtx_send_choose_ssrc (rtx, rtx_ssrc, consider);
352     data = ssrc_rtx_data_new (rtx_ssrc);
353     g_hash_table_insert (rtx->ssrc_data, GUINT_TO_POINTER (ssrc), data);
354     g_hash_table_insert (rtx->rtx_ssrcs, GUINT_TO_POINTER (rtx_ssrc),
355         GUINT_TO_POINTER (ssrc));
356   } else {
357     data = g_hash_table_lookup (rtx->ssrc_data, GUINT_TO_POINTER (ssrc));
358   }
359   return data;
360 }
361
362 /* Copy fixed header and extension. Add OSN before to copy payload
363  * Copy memory to avoid to manually copy each rtp buffer field.
364  */
365 static GstBuffer *
366 gst_rtp_rtx_buffer_new (GstRtpRtxSend * rtx, GstBuffer * buffer)
367 {
368   GstMemory *mem = NULL;
369   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
370   GstRTPBuffer new_rtp = GST_RTP_BUFFER_INIT;
371   GstBuffer *new_buffer = gst_buffer_new ();
372   GstMapInfo map;
373   guint payload_len = 0;
374   SSRCRtxData *data;
375   guint32 ssrc;
376   guint16 seqnum;
377   guint8 fmtp;
378
379   gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
380
381   /* get needed data from GstRtpRtxSend */
382   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
383   data = gst_rtp_rtx_send_get_ssrc_data (rtx, ssrc);
384   ssrc = data->rtx_ssrc;
385   seqnum = data->next_seqnum++;
386   fmtp = GPOINTER_TO_UINT (g_hash_table_lookup (rtx->rtx_pt_map,
387           GUINT_TO_POINTER (gst_rtp_buffer_get_payload_type (&rtp))));
388
389   GST_DEBUG_OBJECT (rtx,
390       "retransmit seqnum: %" G_GUINT16_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
391       seqnum, ssrc);
392
393   /* gst_rtp_buffer_map does not map the payload so do it now */
394   gst_rtp_buffer_get_payload (&rtp);
395
396   /* copy fixed header */
397   mem = gst_memory_copy (rtp.map[0].memory, 0, rtp.size[0]);
398   gst_buffer_append_memory (new_buffer, mem);
399
400   /* copy extension if any */
401   if (rtp.size[1]) {
402     mem = gst_memory_copy (rtp.map[1].memory, 0, rtp.size[1]);
403     gst_buffer_append_memory (new_buffer, mem);
404   }
405
406   /* copy payload and add OSN just before */
407   payload_len = 2 + rtp.size[2];
408   mem = gst_allocator_alloc (NULL, payload_len, NULL);
409
410   gst_memory_map (mem, &map, GST_MAP_WRITE);
411   GST_WRITE_UINT16_BE (map.data, gst_rtp_buffer_get_seq (&rtp));
412   if (rtp.size[2])
413     memcpy (map.data + 2, rtp.data[2], rtp.size[2]);
414   gst_memory_unmap (mem, &map);
415   gst_buffer_append_memory (new_buffer, mem);
416
417   /* everything needed is copied */
418   gst_rtp_buffer_unmap (&rtp);
419
420   /* set ssrc, seqnum and fmtp */
421   gst_rtp_buffer_map (new_buffer, GST_MAP_WRITE, &new_rtp);
422   gst_rtp_buffer_set_ssrc (&new_rtp, ssrc);
423   gst_rtp_buffer_set_seq (&new_rtp, seqnum);
424   gst_rtp_buffer_set_payload_type (&new_rtp, fmtp);
425   /* RFC 4588: let other elements do the padding, as normal */
426   gst_rtp_buffer_set_padding (&new_rtp, FALSE);
427   gst_rtp_buffer_unmap (&new_rtp);
428
429   return new_buffer;
430 }
431
432 static gint
433 buffer_queue_items_cmp (BufferQueueItem * a, BufferQueueItem * b,
434     gpointer user_data)
435 {
436   /* gst_rtp_buffer_compare_seqnum returns the opposite of what we want,
437    * it returns negative when seqnum1 > seqnum2 and we want negative
438    * when b > a, i.e. a is smaller, so it comes first in the sequence */
439   return gst_rtp_buffer_compare_seqnum (b->seqnum, a->seqnum);
440 }
441
442 static gboolean
443 gst_rtp_rtx_send_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
444 {
445   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
446   gboolean res;
447
448   switch (GST_EVENT_TYPE (event)) {
449     case GST_EVENT_CUSTOM_UPSTREAM:
450     {
451       const GstStructure *s = gst_event_get_structure (event);
452
453       /* This event usually comes from the downstream gstrtpsession */
454       if (gst_structure_has_name (s, "GstRTPRetransmissionRequest")) {
455         guint seqnum = 0;
456         guint ssrc = 0;
457         GstBuffer *rtx_buf = NULL;
458
459         /* retrieve seqnum of the packet that need to be restransmisted */
460         if (!gst_structure_get_uint (s, "seqnum", &seqnum))
461           seqnum = -1;
462
463         /* retrieve ssrc of the packet that need to be restransmisted */
464         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
465           ssrc = -1;
466
467         GST_DEBUG_OBJECT (rtx,
468             "request seqnum: %" G_GUINT32_FORMAT ", ssrc: %" G_GUINT32_FORMAT,
469             seqnum, ssrc);
470
471         GST_OBJECT_LOCK (rtx);
472         /* check if request is for us */
473         if (g_hash_table_contains (rtx->ssrc_data, GUINT_TO_POINTER (ssrc))) {
474           SSRCRtxData *data;
475           GSequenceIter *iter;
476           BufferQueueItem search_item;
477
478           /* update statistics */
479           ++rtx->num_rtx_requests;
480
481           data = gst_rtp_rtx_send_get_ssrc_data (rtx, ssrc);
482
483           search_item.seqnum = seqnum;
484           iter = g_sequence_lookup (data->queue, &search_item,
485               (GCompareDataFunc) buffer_queue_items_cmp, NULL);
486           if (iter) {
487             BufferQueueItem *item = g_sequence_get (iter);
488             GST_DEBUG_OBJECT (rtx, "found %" G_GUINT16_FORMAT, item->seqnum);
489             rtx_buf = gst_rtp_rtx_buffer_new (rtx, item->buffer);
490           }
491         }
492         GST_OBJECT_UNLOCK (rtx);
493
494         if (rtx_buf)
495           gst_rtp_rtx_send_push_out (rtx, rtx_buf);
496
497         gst_event_unref (event);
498         res = TRUE;
499
500         /* This event usually comes from the downstream gstrtpsession */
501       } else if (gst_structure_has_name (s, "GstRTPCollision")) {
502         guint ssrc = 0;
503
504         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
505           ssrc = -1;
506
507         GST_DEBUG_OBJECT (rtx, "collision ssrc: %" G_GUINT32_FORMAT, ssrc);
508
509         GST_OBJECT_LOCK (rtx);
510
511         /* choose another ssrc for our retransmited stream */
512         if (g_hash_table_contains (rtx->rtx_ssrcs, GUINT_TO_POINTER (ssrc))) {
513           guint master_ssrc;
514           SSRCRtxData *data;
515
516           master_ssrc = GPOINTER_TO_UINT (g_hash_table_lookup (rtx->rtx_ssrcs,
517                   GUINT_TO_POINTER (ssrc)));
518           data = gst_rtp_rtx_send_get_ssrc_data (rtx, master_ssrc);
519
520           /* change rtx_ssrc and update the reverse map */
521           data->rtx_ssrc = gst_rtp_rtx_send_choose_ssrc (rtx, 0, FALSE);
522           g_hash_table_remove (rtx->rtx_ssrcs, GUINT_TO_POINTER (ssrc));
523           g_hash_table_insert (rtx->rtx_ssrcs,
524               GUINT_TO_POINTER (data->rtx_ssrc),
525               GUINT_TO_POINTER (master_ssrc));
526
527           GST_OBJECT_UNLOCK (rtx);
528
529           /* no need to forward to payloader because we make sure to have
530            * a different ssrc
531            */
532           gst_event_unref (event);
533           res = TRUE;
534         } else {
535           /* if master ssrc has collided, remove it from our data, as it
536            * is not going to be used any longer */
537           if (g_hash_table_contains (rtx->ssrc_data, GUINT_TO_POINTER (ssrc))) {
538             SSRCRtxData *data;
539             data = gst_rtp_rtx_send_get_ssrc_data (rtx, ssrc);
540             g_hash_table_remove (rtx->rtx_ssrcs,
541                 GUINT_TO_POINTER (data->rtx_ssrc));
542             g_hash_table_remove (rtx->ssrc_data, GUINT_TO_POINTER (ssrc));
543           }
544
545           GST_OBJECT_UNLOCK (rtx);
546
547           /* forward event to payloader in case collided ssrc is
548            * master stream */
549           res = gst_pad_event_default (pad, parent, event);
550         }
551       } else {
552         res = gst_pad_event_default (pad, parent, event);
553       }
554       break;
555     }
556     default:
557       res = gst_pad_event_default (pad, parent, event);
558       break;
559   }
560   return res;
561 }
562
563 static gboolean
564 gst_rtp_rtx_send_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
565 {
566   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
567
568   switch (GST_EVENT_TYPE (event)) {
569     case GST_EVENT_FLUSH_START:
570       gst_pad_push_event (rtx->srcpad, event);
571       gst_rtp_rtx_send_set_flushing (rtx, TRUE);
572       gst_pad_pause_task (rtx->srcpad);
573       return TRUE;
574     case GST_EVENT_FLUSH_STOP:
575       gst_pad_push_event (rtx->srcpad, event);
576       gst_rtp_rtx_send_set_flushing (rtx, FALSE);
577       gst_pad_start_task (rtx->srcpad,
578           (GstTaskFunction) gst_rtp_rtx_send_src_loop, rtx, NULL);
579       return TRUE;
580     case GST_EVENT_CAPS:
581     {
582       GstCaps *caps;
583       GstStructure *s;
584       guint ssrc;
585       SSRCRtxData *data;
586
587       gst_event_parse_caps (event, &caps);
588       g_assert (gst_caps_is_fixed (caps));
589
590       s = gst_caps_get_structure (caps, 0);
591       gst_structure_get_uint (s, "ssrc", &ssrc);
592
593       GST_OBJECT_LOCK (rtx);
594       data = gst_rtp_rtx_send_get_ssrc_data (rtx, ssrc);
595       gst_structure_get_int (s, "clock-rate", &data->clock_rate);
596
597       GST_DEBUG_OBJECT (rtx, "got clock-rate from caps: %d for ssrc: %u",
598           data->clock_rate, ssrc);
599       GST_OBJECT_UNLOCK (rtx);
600       break;
601     }
602     default:
603       break;
604   }
605   return gst_pad_event_default (pad, parent, event);
606 }
607
608 /* like rtp_jitter_buffer_get_ts_diff() */
609 static guint32
610 gst_rtp_rtx_send_get_ts_diff (SSRCRtxData * data)
611 {
612   guint64 high_ts, low_ts;
613   BufferQueueItem *high_buf, *low_buf;
614   guint32 result;
615
616   high_buf =
617       g_sequence_get (g_sequence_iter_prev (g_sequence_get_end_iter
618           (data->queue)));
619   low_buf = g_sequence_get (g_sequence_get_begin_iter (data->queue));
620
621   if (!high_buf || !low_buf || high_buf == low_buf)
622     return 0;
623
624   high_ts = high_buf->timestamp;
625   low_ts = low_buf->timestamp;
626
627   /* it needs to work if ts wraps */
628   if (high_ts >= low_ts) {
629     result = (guint32) (high_ts - low_ts);
630   } else {
631     result = (guint32) (high_ts + G_MAXUINT32 + 1 - low_ts);
632   }
633
634   /* return value in ms instead of clock ticks */
635   return (guint32) gst_util_uint64_scale_int (result, 1000, data->clock_rate);
636 }
637
638 static GstFlowReturn
639 gst_rtp_rtx_send_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
640 {
641   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
642   GstFlowReturn ret = GST_FLOW_ERROR;
643   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
644   BufferQueueItem *item;
645   SSRCRtxData *data;
646   guint16 seqnum;
647   guint8 payload_type;
648   guint32 ssrc, rtptime;
649
650   /* read the information we want from the buffer */
651   gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
652   seqnum = gst_rtp_buffer_get_seq (&rtp);
653   payload_type = gst_rtp_buffer_get_payload_type (&rtp);
654   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
655   rtptime = gst_rtp_buffer_get_timestamp (&rtp);
656   gst_rtp_buffer_unmap (&rtp);
657
658   GST_OBJECT_LOCK (rtx);
659
660   /* do not store the buffer if it's payload type is unknown */
661   if (g_hash_table_contains (rtx->rtx_pt_map, GUINT_TO_POINTER (payload_type))) {
662     data = gst_rtp_rtx_send_get_ssrc_data (rtx, ssrc);
663
664     /* add current rtp buffer to queue history */
665     item = g_slice_new0 (BufferQueueItem);
666     item->seqnum = seqnum;
667     item->timestamp = rtptime;
668     item->buffer = gst_buffer_ref (buffer);
669     g_sequence_append (data->queue, item);
670
671     /* remove oldest packets from history if they are too many */
672     if (rtx->max_size_packets) {
673       while (g_sequence_get_length (data->queue) > rtx->max_size_packets)
674         g_sequence_remove (g_sequence_get_begin_iter (data->queue));
675     }
676     if (rtx->max_size_time) {
677       while (gst_rtp_rtx_send_get_ts_diff (data) > rtx->max_size_time)
678         g_sequence_remove (g_sequence_get_begin_iter (data->queue));
679     }
680   }
681
682   GST_OBJECT_UNLOCK (rtx);
683
684   GST_LOG_OBJECT (rtx,
685       "push seqnum: %" G_GUINT16_FORMAT ", ssrc: %" G_GUINT32_FORMAT, seqnum,
686       ssrc);
687
688   ret = gst_pad_push (rtx->srcpad, buffer);
689
690   return ret;
691 }
692
693 static void
694 gst_rtp_rtx_send_src_loop (GstRtpRtxSend * rtx)
695 {
696   GstDataQueueItem *data;
697
698   if (gst_data_queue_pop (rtx->queue, &data)) {
699     GST_LOG_OBJECT (rtx, "pushing rtx buffer %p", data->object);
700
701     gst_pad_push (rtx->srcpad, GST_BUFFER (data->object));
702
703     GST_OBJECT_LOCK (rtx);
704     rtx->num_rtx_packets++;
705     GST_OBJECT_UNLOCK (rtx);
706
707     data->object = NULL;        /* we no longer own that object */
708     data->destroy (data);
709   } else {
710     GST_LOG_OBJECT (rtx, "flushing");
711     gst_pad_pause_task (rtx->srcpad);
712   }
713 }
714
715 static gboolean
716 gst_rtp_rtx_send_activate_mode (GstPad * pad, GstObject * parent,
717     GstPadMode mode, gboolean active)
718 {
719   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (parent);
720   gboolean ret = FALSE;
721
722   switch (mode) {
723     case GST_PAD_MODE_PUSH:
724       if (active) {
725         gst_rtp_rtx_send_set_flushing (rtx, FALSE);
726         ret = gst_pad_start_task (rtx->srcpad,
727             (GstTaskFunction) gst_rtp_rtx_send_src_loop, rtx, NULL);
728       } else {
729         gst_rtp_rtx_send_set_flushing (rtx, TRUE);
730         ret = gst_pad_stop_task (rtx->srcpad);
731       }
732       GST_INFO_OBJECT (rtx, "activate_mode: active %d, ret %d", active, ret);
733       break;
734     default:
735       break;
736   }
737   return ret;
738 }
739
740 static void
741 gst_rtp_rtx_send_get_property (GObject * object,
742     guint prop_id, GValue * value, GParamSpec * pspec)
743 {
744   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
745
746   switch (prop_id) {
747     case PROP_PAYLOAD_TYPE_MAP:
748       GST_OBJECT_LOCK (rtx);
749       g_value_set_boxed (value, rtx->rtx_pt_map_structure);
750       GST_OBJECT_UNLOCK (rtx);
751       break;
752     case PROP_MAX_SIZE_TIME:
753       GST_OBJECT_LOCK (rtx);
754       g_value_set_uint (value, rtx->max_size_time);
755       GST_OBJECT_UNLOCK (rtx);
756       break;
757     case PROP_MAX_SIZE_PACKETS:
758       GST_OBJECT_LOCK (rtx);
759       g_value_set_uint (value, rtx->max_size_packets);
760       GST_OBJECT_UNLOCK (rtx);
761       break;
762     case PROP_NUM_RTX_REQUESTS:
763       GST_OBJECT_LOCK (rtx);
764       g_value_set_uint (value, rtx->num_rtx_requests);
765       GST_OBJECT_UNLOCK (rtx);
766       break;
767     case PROP_NUM_RTX_PACKETS:
768       GST_OBJECT_LOCK (rtx);
769       g_value_set_uint (value, rtx->num_rtx_packets);
770       GST_OBJECT_UNLOCK (rtx);
771       break;
772     default:
773       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774       break;
775   }
776 }
777
778 static gboolean
779 structure_to_hash_table (GQuark field_id, const GValue * value, gpointer hash)
780 {
781   const gchar *field_str;
782   guint field_uint;
783   guint value_uint;
784
785   field_str = g_quark_to_string (field_id);
786   field_uint = atoi (field_str);
787   value_uint = g_value_get_uint (value);
788   g_hash_table_insert ((GHashTable *) hash, GUINT_TO_POINTER (field_uint),
789       GUINT_TO_POINTER (value_uint));
790
791   return TRUE;
792 }
793
794 static void
795 gst_rtp_rtx_send_set_property (GObject * object,
796     guint prop_id, const GValue * value, GParamSpec * pspec)
797 {
798   GstRtpRtxSend *rtx = GST_RTP_RTX_SEND (object);
799
800   switch (prop_id) {
801     case PROP_SSRC_MAP:
802       GST_OBJECT_LOCK (rtx);
803       if (rtx->external_ssrc_map)
804         gst_structure_free (rtx->external_ssrc_map);
805       rtx->external_ssrc_map = g_value_dup_boxed (value);
806       GST_OBJECT_UNLOCK (rtx);
807       break;
808     case PROP_PAYLOAD_TYPE_MAP:
809       GST_OBJECT_LOCK (rtx);
810       if (rtx->rtx_pt_map_structure)
811         gst_structure_free (rtx->rtx_pt_map_structure);
812       rtx->rtx_pt_map_structure = g_value_dup_boxed (value);
813       g_hash_table_remove_all (rtx->rtx_pt_map);
814       gst_structure_foreach (rtx->rtx_pt_map_structure, structure_to_hash_table,
815           rtx->rtx_pt_map);
816       GST_OBJECT_UNLOCK (rtx);
817       break;
818     case PROP_MAX_SIZE_TIME:
819       GST_OBJECT_LOCK (rtx);
820       rtx->max_size_time = g_value_get_uint (value);
821       GST_OBJECT_UNLOCK (rtx);
822       break;
823     case PROP_MAX_SIZE_PACKETS:
824       GST_OBJECT_LOCK (rtx);
825       rtx->max_size_packets = g_value_get_uint (value);
826       GST_OBJECT_UNLOCK (rtx);
827       break;
828     default:
829       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
830       break;
831   }
832 }
833
834 static GstStateChangeReturn
835 gst_rtp_rtx_send_change_state (GstElement * element, GstStateChange transition)
836 {
837   GstStateChangeReturn ret;
838   GstRtpRtxSend *rtx;
839
840   rtx = GST_RTP_RTX_SEND (element);
841
842   switch (transition) {
843     default:
844       break;
845   }
846
847   ret =
848       GST_ELEMENT_CLASS (gst_rtp_rtx_send_parent_class)->change_state (element,
849       transition);
850
851   switch (transition) {
852     case GST_STATE_CHANGE_PAUSED_TO_READY:
853       gst_rtp_rtx_send_reset (rtx);
854       break;
855     default:
856       break;
857   }
858
859   return ret;
860 }
861
862 gboolean
863 gst_rtp_rtx_send_plugin_init (GstPlugin * plugin)
864 {
865   GST_DEBUG_CATEGORY_INIT (gst_rtp_rtx_send_debug, "rtprtxsend", 0,
866       "rtp retransmission sender");
867
868   return gst_element_register (plugin, "rtprtxsend", GST_RANK_NONE,
869       GST_TYPE_RTP_RTX_SEND);
870 }