rtpmux: Rename variable for more clarity
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpmux.c
1 /* RTP muxer element for GStreamer
2  *
3  * gstrtpmux.c:
4  *
5  * Copyright (C) <2007> Nokia Corporation.
6  *   Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
7  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
8  *               2000,2005 Wim Taymans <wim@fluendo.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /**
27  * SECTION:element-rtpmux
28  *
29  * The rtp muxer takes multiple RTP streams having the same clock-rate and
30  * muxes into a single stream with a single SSRC.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <gst/gst.h>
38 #include <gst/rtp/gstrtpbuffer.h>
39 #include <string.h>
40
41 #include "gstrtpmux.h"
42
43 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
44 #define GST_CAT_DEFAULT gst_rtp_mux_debug
45
46 /* elementfactory information */
47 static const GstElementDetails gst_rtp_mux_details =
48 GST_ELEMENT_DETAILS ("RTP muxer",
49     "Codec/Muxer",
50     "multiplex N rtp streams into one",
51     "Zeeshan Ali <first.last@nokia.com>");
52
53 enum
54 {
55   ARG_0,
56   PROP_TIMESTAMP_OFFSET,
57   PROP_SEQNUM_OFFSET,
58   PROP_SEQNUM,
59   PROP_SSRC
60 };
61
62 #define DEFAULT_TIMESTAMP_OFFSET -1
63 #define DEFAULT_SEQNUM_OFFSET    -1
64 #define DEFAULT_SSRC             -1
65
66 typedef struct
67 {
68   gboolean have_clock_base;
69   guint clock_base;
70 } GstRTPMuxPadPrivate;
71
72 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS ("application/x-rtp")
76     );
77
78 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
79     GST_PAD_SINK,
80     GST_PAD_REQUEST,
81     GST_STATIC_CAPS ("application/x-rtp")
82     );
83
84 static void gst_rtp_mux_finalize (GObject * object);
85
86 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
87     GstPadTemplate * templ, const gchar * name);
88 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
89 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
90 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
91 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
92
93 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
94     element, GstStateChange transition);
95
96 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
97     const GValue * value, GParamSpec * pspec);
98 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
99     GValue * value, GParamSpec * pspec);
100
101
102 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
103
104 static void
105 gst_rtp_mux_base_init (gpointer g_class)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108
109   gst_element_class_add_pad_template (element_class,
110       gst_static_pad_template_get (&src_factory));
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&sink_factory));
113
114   gst_element_class_set_details (element_class, &gst_rtp_mux_details);
115 }
116
117 static void
118 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
119 {
120   GObjectClass *gobject_class;
121   GstElementClass *gstelement_class;
122
123   gobject_class = (GObjectClass *) klass;
124   gstelement_class = (GstElementClass *) klass;
125
126   gobject_class->finalize = gst_rtp_mux_finalize;
127   gobject_class->get_property = gst_rtp_mux_get_property;
128   gobject_class->set_property = gst_rtp_mux_set_property;
129
130   g_object_class_install_property (G_OBJECT_CLASS (klass),
131       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
132           "Timestamp Offset",
133           "Offset to add to all outgoing timestamps (-1 = random)", -1,
134           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
135   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
136       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
137           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
138           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
139   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
140       g_param_spec_uint ("seqnum", "Sequence number",
141           "The RTP sequence number of the last processed packet",
142           0, G_MAXUINT, 0, G_PARAM_READABLE));
143   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
144       g_param_spec_uint ("ssrc", "SSRC",
145           "The SSRC of the packets (-1 == random)",
146           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
147
148   gstelement_class->request_new_pad =
149       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
150   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
151   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
152
153   klass->chain_func = gst_rtp_mux_chain;
154 }
155
156 static gboolean
157 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
158 {
159   GstElement *rtp_mux;
160   GstIterator *iter;
161   GstPad *sinkpad;
162   gboolean result = FALSE;
163   gboolean done = FALSE;
164
165   rtp_mux = gst_pad_get_parent_element (pad);
166   g_return_val_if_fail (rtp_mux != NULL, FALSE);
167
168   iter = gst_element_iterate_sink_pads (rtp_mux);
169
170   while (!done) {
171     switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
172       case GST_ITERATOR_OK:
173         gst_event_ref (event);
174         result |= gst_pad_push_event (sinkpad, event);
175         gst_object_unref (sinkpad);
176         break;
177       case GST_ITERATOR_RESYNC:
178         gst_iterator_resync (iter);
179         result = FALSE;
180         break;
181       case GST_ITERATOR_ERROR:
182         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
183       case GST_ITERATOR_DONE:
184         done = TRUE;
185         break;
186     }
187   }
188   gst_iterator_free (iter);
189   gst_object_unref (rtp_mux);
190   gst_event_unref (event);
191
192   return result;
193 }
194
195 static void
196 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
197 {
198   GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
199
200   object->srcpad =
201       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
202           "src"), "src");
203   gst_pad_set_event_function (object->srcpad,
204       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
205   gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
206
207   object->ssrc = DEFAULT_SSRC;
208   object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
209   object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
210 }
211
212 static void
213 gst_rtp_mux_finalize (GObject * object)
214 {
215   GstRTPMux *rtp_mux;
216
217   rtp_mux = GST_RTP_MUX (object);
218
219   G_OBJECT_CLASS (parent_class)->finalize (object);
220 }
221
222 static GstPad *
223 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ)
224 {
225   GstPad *newpad = NULL;
226   GstPadTemplate *class_templ;
227
228   class_templ =
229       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (rtp_mux),
230       "sink_%d");
231
232   if (templ == class_templ) {
233     gchar *name;
234
235     /* create new pad with the name */
236     name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
237     newpad = gst_pad_new_from_template (templ, name);
238     g_free (name);
239
240     rtp_mux->numpads++;
241   } else {
242     GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
243   }
244
245   return newpad;
246 }
247
248 static void
249 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
250 {
251   GstRTPMuxClass *klass;
252   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
253
254   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
255
256   /* setup some pad functions */
257   gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
258   gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
259   if (klass->chain_func)
260     gst_pad_set_chain_function (sinkpad, klass->chain_func);
261   if (klass->sink_event_func)
262     gst_pad_set_event_function (sinkpad, klass->sink_event_func);
263
264   /* This could break with gstreamer 0.10.9 */
265   gst_pad_set_active (sinkpad, TRUE);
266
267   gst_pad_set_element_private (sinkpad, padpriv);
268
269   /* dd the pad to the element */
270   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
271 }
272
273 static GstPad *
274 gst_rtp_mux_request_new_pad (GstElement * element,
275     GstPadTemplate * templ, const gchar * req_name)
276 {
277   GstRTPMux *rtp_mux;
278   GstPad *newpad;
279
280   g_return_val_if_fail (templ != NULL, NULL);
281   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
282
283   rtp_mux = GST_RTP_MUX (element);
284
285   if (templ->direction != GST_PAD_SINK) {
286     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
287     return NULL;
288   }
289
290   newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ);
291   if (newpad)
292     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
293   else
294     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
295
296   return newpad;
297 }
298
299 static void
300 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
301 {
302   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
303
304   if (padpriv)
305     g_slice_free (GstRTPMuxPadPrivate, padpriv);
306   gst_pad_set_element_private (pad, NULL);
307
308   gst_element_remove_pad (element, pad);
309 }
310
311 /* Put our own clock-base on the buffer */
312 static void
313 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
314     GstBuffer * buffer)
315 {
316   guint32 ts;
317   guint32 sink_ts_base = 0;
318   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
319
320   if (padpriv->have_clock_base)
321     sink_ts_base = padpriv->clock_base;
322
323   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
324   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
325       gst_rtp_buffer_get_timestamp (buffer), ts);
326   gst_rtp_buffer_set_timestamp (buffer, ts);
327 }
328
329 static GstFlowReturn
330 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
331 {
332   GstRTPMux *rtp_mux;
333   GstStructure *structure;
334   GstFlowReturn ret;
335
336   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
337
338   if (!gst_rtp_buffer_validate (buffer)) {
339     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
340     gst_object_unref (rtp_mux);
341     return GST_FLOW_ERROR;
342   }
343
344   buffer = gst_buffer_make_writable (buffer);
345
346   GST_OBJECT_LOCK (rtp_mux);
347   rtp_mux->seqnum++;
348   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
349   GST_OBJECT_UNLOCK (rtp_mux);
350   GST_BUFFER_CAPS (buffer) = gst_caps_make_writable (GST_BUFFER_CAPS (buffer));
351   structure = gst_caps_get_structure (GST_BUFFER_CAPS (buffer), 0U);
352   gst_structure_set (structure, "seqnum-base", G_TYPE_UINT,
353       rtp_mux->seqnum_base, NULL);
354   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
355   gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
356   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
357       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
358       gst_rtp_buffer_get_timestamp (buffer));
359
360   gst_buffer_set_caps (buffer, GST_PAD_CAPS (rtp_mux->srcpad));
361
362   ret = gst_pad_push (rtp_mux->srcpad, buffer);
363
364   gst_object_unref (rtp_mux);
365   return ret;
366 }
367
368 static gboolean
369 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
370 {
371   GstRTPMux *rtp_mux;
372   GstStructure *structure;
373   gboolean ret = TRUE;
374   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
375
376   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
377
378   structure = gst_caps_get_structure (caps, 0);
379
380   if (!ret)
381     goto out;
382
383   if (gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
384     padpriv->have_clock_base = TRUE;
385   }
386
387   caps = gst_caps_copy (caps);
388
389   gst_caps_set_simple (caps,
390       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
391       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
392
393   GST_DEBUG_OBJECT (rtp_mux,
394       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
395   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
396   gst_caps_unref (caps);
397
398 out:
399   gst_object_unref (rtp_mux);
400
401   return ret;
402 }
403
404 static void
405 clear_caps (GstCaps * caps, gboolean only_clock_rate)
406 {
407   gint i, j;
408
409   /* Lets only match on the clock-rate */
410   for (i = 0; i < gst_caps_get_size (caps); i++) {
411     GstStructure *s = gst_caps_get_structure (caps, i);
412
413     for (j = 0; j < gst_structure_n_fields (s); j++) {
414       const gchar *name = gst_structure_nth_field_name (s, j);
415
416       if (strcmp (name, "clock-rate") && (only_clock_rate ||
417               (strcmp (name, "ssrc")))) {
418         gst_structure_remove_field (s, name);
419         j--;
420       }
421     }
422   }
423 }
424
425 static gboolean
426 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
427 {
428   GstPad *mypad = user_data;
429   GstPad *pad = item;
430   GstCaps *peercaps;
431   GstCaps *othercaps;
432   const GstCaps *accumcaps;
433   GstCaps *intersect;
434
435   if (pad == mypad)
436     return TRUE;
437
438   peercaps = gst_pad_peer_get_caps (pad);
439   if (!peercaps)
440     return TRUE;
441
442   othercaps = gst_caps_intersect (peercaps,
443       gst_pad_get_pad_template_caps (pad));
444   gst_caps_unref (peercaps);
445
446   accumcaps = gst_value_get_caps (ret);
447
448   clear_caps (othercaps, TRUE);
449
450   intersect = gst_caps_intersect (accumcaps, othercaps);
451
452   g_value_take_boxed (ret, intersect);
453
454   gst_caps_unref (othercaps);
455
456   return !gst_caps_is_empty (intersect);
457 }
458
459 static GstCaps *
460 gst_rtp_mux_getcaps (GstPad * pad)
461 {
462   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
463   GstCaps *caps = NULL;
464   GstIterator *iter = NULL;
465   GValue v = { 0 };
466   GstIteratorResult res;
467   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
468   GstCaps *othercaps = NULL;
469
470   if (peercaps) {
471     othercaps = gst_caps_intersect (peercaps,
472         gst_pad_get_pad_template_caps (pad));
473     gst_caps_unref (peercaps);
474   } else {
475     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
476   }
477
478   clear_caps (othercaps, FALSE);
479
480   g_value_init (&v, GST_TYPE_CAPS);
481
482   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
483   do {
484     gst_value_set_caps (&v, othercaps);
485     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
486   } while (res == GST_ITERATOR_RESYNC);
487   gst_iterator_free (iter);
488
489   caps = (GstCaps *) gst_value_get_caps (&v);
490
491   if (res == GST_ITERATOR_ERROR) {
492     gst_caps_unref (caps);
493     caps = gst_caps_new_empty ();
494   }
495
496   if (othercaps)
497     gst_caps_unref (othercaps);
498   gst_object_unref (mux);
499
500   return caps;
501 }
502
503
504 static void
505 gst_rtp_mux_get_property (GObject * object,
506     guint prop_id, GValue * value, GParamSpec * pspec)
507 {
508   GstRTPMux *rtp_mux;
509
510   rtp_mux = GST_RTP_MUX (object);
511
512   switch (prop_id) {
513     case PROP_TIMESTAMP_OFFSET:
514       g_value_set_int (value, rtp_mux->ts_offset);
515       break;
516     case PROP_SEQNUM_OFFSET:
517       g_value_set_int (value, rtp_mux->seqnum_offset);
518       break;
519     case PROP_SEQNUM:
520       GST_OBJECT_LOCK (rtp_mux);
521       g_value_set_uint (value, rtp_mux->seqnum);
522       GST_OBJECT_UNLOCK (rtp_mux);
523       break;
524     case PROP_SSRC:
525       g_value_set_uint (value, rtp_mux->ssrc);
526       break;
527     default:
528       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529       break;
530   }
531 }
532
533 static void
534 gst_rtp_mux_set_property (GObject * object,
535     guint prop_id, const GValue * value, GParamSpec * pspec)
536 {
537   GstRTPMux *rtp_mux;
538
539   rtp_mux = GST_RTP_MUX (object);
540
541   switch (prop_id) {
542     case PROP_TIMESTAMP_OFFSET:
543       rtp_mux->ts_offset = g_value_get_int (value);
544       break;
545     case PROP_SEQNUM_OFFSET:
546       rtp_mux->seqnum_offset = g_value_get_int (value);
547       break;
548     case PROP_SSRC:
549       rtp_mux->ssrc = g_value_get_uint (value);
550       break;
551     default:
552       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553       break;
554   }
555 }
556
557 static void
558 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
559 {
560   GST_OBJECT_LOCK (rtp_mux);
561
562   if (rtp_mux->ssrc == -1)
563     rtp_mux->current_ssrc = g_random_int ();
564   else
565     rtp_mux->current_ssrc = rtp_mux->ssrc;
566
567   if (rtp_mux->seqnum_offset == -1)
568     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
569   else
570     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
571   rtp_mux->seqnum = rtp_mux->seqnum_base;
572
573   if (rtp_mux->ts_offset == -1)
574     rtp_mux->ts_base = g_random_int ();
575   else
576     rtp_mux->ts_base = rtp_mux->ts_offset;
577   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
578
579   GST_OBJECT_UNLOCK (rtp_mux);
580 }
581
582 static GstStateChangeReturn
583 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
584 {
585   GstRTPMux *rtp_mux;
586
587   rtp_mux = GST_RTP_MUX (element);
588
589   switch (transition) {
590     case GST_STATE_CHANGE_NULL_TO_READY:
591       break;
592     case GST_STATE_CHANGE_READY_TO_PAUSED:
593       gst_rtp_mux_ready_to_paused (rtp_mux);
594       break;
595     case GST_STATE_CHANGE_PAUSED_TO_READY:
596       break;
597     default:
598       break;
599   }
600
601   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
602 }
603
604 gboolean
605 gst_rtp_mux_plugin_init (GstPlugin * plugin)
606 {
607   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
608
609   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
610       GST_TYPE_RTP_MUX);
611 }