rtpmux: Don't ignore requested pad name
[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
71   GstCaps *out_caps;
72 } GstRTPMuxPadPrivate;
73
74 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("application/x-rtp")
78     );
79
80 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
81     GST_PAD_SINK,
82     GST_PAD_REQUEST,
83     GST_STATIC_CAPS ("application/x-rtp")
84     );
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->get_property = gst_rtp_mux_get_property;
127   gobject_class->set_property = gst_rtp_mux_set_property;
128
129   g_object_class_install_property (G_OBJECT_CLASS (klass),
130       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
131           "Timestamp Offset",
132           "Offset to add to all outgoing timestamps (-1 = random)", -1,
133           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
134   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
135       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
136           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
137           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
138   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
139       g_param_spec_uint ("seqnum", "Sequence number",
140           "The RTP sequence number of the last processed packet",
141           0, G_MAXUINT, 0, G_PARAM_READABLE));
142   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
143       g_param_spec_uint ("ssrc", "SSRC",
144           "The SSRC of the packets (-1 == random)",
145           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
146
147   gstelement_class->request_new_pad =
148       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
149   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
150   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
151
152   klass->chain_func = gst_rtp_mux_chain;
153 }
154
155 static gboolean
156 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
157 {
158   GstElement *rtp_mux;
159   GstIterator *iter;
160   GstPad *sinkpad;
161   gboolean result = FALSE;
162   gboolean done = FALSE;
163
164   rtp_mux = gst_pad_get_parent_element (pad);
165   g_return_val_if_fail (rtp_mux != NULL, FALSE);
166
167   iter = gst_element_iterate_sink_pads (rtp_mux);
168
169   while (!done) {
170     switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
171       case GST_ITERATOR_OK:
172         gst_event_ref (event);
173         result |= gst_pad_push_event (sinkpad, event);
174         gst_object_unref (sinkpad);
175         break;
176       case GST_ITERATOR_RESYNC:
177         gst_iterator_resync (iter);
178         result = FALSE;
179         break;
180       case GST_ITERATOR_ERROR:
181         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
182       case GST_ITERATOR_DONE:
183         done = TRUE;
184         break;
185     }
186   }
187   gst_iterator_free (iter);
188   gst_object_unref (rtp_mux);
189   gst_event_unref (event);
190
191   return result;
192 }
193
194 static void
195 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
196 {
197   GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
198
199   object->srcpad =
200       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
201           "src"), "src");
202   gst_pad_set_event_function (object->srcpad,
203       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
204   gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
205
206   object->ssrc = DEFAULT_SSRC;
207   object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
208   object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
209 }
210
211 static GstPad *
212 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ,
213     const gchar * req_name)
214 {
215   GstPad *newpad = NULL;
216   GstPadTemplate *class_templ;
217
218   class_templ =
219       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (rtp_mux),
220       "sink_%d");
221
222   if (templ == class_templ) {
223     gchar *tmpname = NULL;
224     const gchar *name = NULL;
225
226     /* create new pad with the name */
227     if (req_name)
228       name = req_name;
229     else
230       name = tmpname = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
231     newpad = gst_pad_new_from_template (templ, name);
232     g_free (tmpname);
233
234     rtp_mux->numpads++;
235   } else {
236     GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
237   }
238
239   return newpad;
240 }
241
242 static void
243 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
244 {
245   GstRTPMuxClass *klass;
246   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
247
248   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
249
250   /* setup some pad functions */
251   gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
252   gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
253   if (klass->chain_func)
254     gst_pad_set_chain_function (sinkpad, klass->chain_func);
255   if (klass->sink_event_func)
256     gst_pad_set_event_function (sinkpad, klass->sink_event_func);
257
258   /* This could break with gstreamer 0.10.9 */
259   gst_pad_set_active (sinkpad, TRUE);
260
261   gst_pad_set_element_private (sinkpad, padpriv);
262
263   /* dd the pad to the element */
264   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
265 }
266
267 static GstPad *
268 gst_rtp_mux_request_new_pad (GstElement * element,
269     GstPadTemplate * templ, const gchar * req_name)
270 {
271   GstRTPMux *rtp_mux;
272   GstPad *newpad;
273
274   g_return_val_if_fail (templ != NULL, NULL);
275   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
276
277   rtp_mux = GST_RTP_MUX (element);
278
279   if (templ->direction != GST_PAD_SINK) {
280     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
281     return NULL;
282   }
283
284   newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ, req_name);
285   if (newpad)
286     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
287   else
288     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
289
290   return newpad;
291 }
292
293 static void
294 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
295 {
296   GstRTPMuxPadPrivate *padpriv;
297
298   GST_OBJECT_LOCK (element);
299   padpriv = gst_pad_get_element_private (pad);
300   gst_pad_set_element_private (pad, NULL);
301   GST_OBJECT_UNLOCK (element);
302
303   gst_element_remove_pad (element, pad);
304
305   if (padpriv) {
306     gst_caps_replace (&padpriv->out_caps, NULL);
307     g_slice_free (GstRTPMuxPadPrivate, padpriv);
308   }
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;
319
320
321   GST_OBJECT_LOCK (rtp_mux);
322   padpriv = gst_pad_get_element_private (pad);
323   if (padpriv && padpriv->have_clock_base)
324     sink_ts_base = padpriv->clock_base;
325   GST_OBJECT_UNLOCK (rtp_mux);
326
327   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
328   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
329       gst_rtp_buffer_get_timestamp (buffer), ts);
330   gst_rtp_buffer_set_timestamp (buffer, ts);
331 }
332
333 static GstFlowReturn
334 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
335 {
336   GstRTPMux *rtp_mux;
337   GstFlowReturn ret;
338   GstRTPMuxPadPrivate *padpriv;
339
340   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
341
342   if (!gst_rtp_buffer_validate (buffer)) {
343     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
344     gst_object_unref (rtp_mux);
345     return GST_FLOW_ERROR;
346   }
347
348   buffer = gst_buffer_make_writable (buffer);
349
350   GST_OBJECT_LOCK (rtp_mux);
351   rtp_mux->seqnum++;
352   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
353   padpriv = gst_pad_get_element_private (pad);
354   if (padpriv)
355     gst_buffer_set_caps (buffer, padpriv->out_caps);
356   GST_OBJECT_UNLOCK (rtp_mux);
357   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
358   gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
359   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
360       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
361       gst_rtp_buffer_get_timestamp (buffer));
362
363   if (!padpriv) {
364     ret = GST_FLOW_NOT_LINKED;
365     gst_buffer_unref (buffer);
366     goto out;
367   }
368
369   ret = gst_pad_push (rtp_mux->srcpad, buffer);
370
371 out:
372
373   gst_object_unref (rtp_mux);
374   return ret;
375 }
376
377 static gboolean
378 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
379 {
380   GstRTPMux *rtp_mux;
381   GstStructure *structure;
382   gboolean ret = FALSE;
383   GstRTPMuxPadPrivate *padpriv;
384
385   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
386
387   structure = gst_caps_get_structure (caps, 0);
388
389   if (!structure)
390     goto out;
391
392   GST_OBJECT_LOCK (rtp_mux);
393   padpriv = gst_pad_get_element_private (pad);
394   if (padpriv &&
395       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
396     padpriv->have_clock_base = TRUE;
397   }
398   GST_OBJECT_UNLOCK (rtp_mux);
399
400   caps = gst_caps_copy (caps);
401
402   gst_caps_set_simple (caps,
403       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
404       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
405
406   GST_DEBUG_OBJECT (rtp_mux,
407       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
408   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
409
410   if (ret) {
411     GST_OBJECT_LOCK (rtp_mux);
412     padpriv = gst_pad_get_element_private (pad);
413     if (padpriv)
414       gst_caps_replace (&padpriv->out_caps, caps);
415     GST_OBJECT_UNLOCK (rtp_mux);
416   }
417   gst_caps_unref (caps);
418
419 out:
420   gst_object_unref (rtp_mux);
421
422   return ret;
423 }
424
425 static void
426 clear_caps (GstCaps * caps, gboolean only_clock_rate)
427 {
428   gint i, j;
429
430   /* Lets only match on the clock-rate */
431   for (i = 0; i < gst_caps_get_size (caps); i++) {
432     GstStructure *s = gst_caps_get_structure (caps, i);
433
434     for (j = 0; j < gst_structure_n_fields (s); j++) {
435       const gchar *name = gst_structure_nth_field_name (s, j);
436
437       if (strcmp (name, "clock-rate") && (only_clock_rate ||
438               (strcmp (name, "ssrc")))) {
439         gst_structure_remove_field (s, name);
440         j--;
441       }
442     }
443   }
444 }
445
446 static gboolean
447 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
448 {
449   GstPad *mypad = user_data;
450   GstPad *pad = item;
451   GstCaps *peercaps;
452   GstCaps *othercaps;
453   const GstCaps *accumcaps;
454   GstCaps *intersect;
455
456   if (pad == mypad) {
457     gst_object_unref (pad);
458     return TRUE;
459   }
460
461   peercaps = gst_pad_peer_get_caps (pad);
462   if (!peercaps) {
463     gst_object_unref (pad);
464     return TRUE;
465   }
466
467   othercaps = gst_caps_intersect (peercaps,
468       gst_pad_get_pad_template_caps (pad));
469   gst_caps_unref (peercaps);
470
471   accumcaps = gst_value_get_caps (ret);
472
473   clear_caps (othercaps, TRUE);
474
475   intersect = gst_caps_intersect (accumcaps, othercaps);
476
477   g_value_take_boxed (ret, intersect);
478
479   gst_caps_unref (othercaps);
480   gst_object_unref (pad);
481
482   return !gst_caps_is_empty (intersect);
483 }
484
485 static GstCaps *
486 gst_rtp_mux_getcaps (GstPad * pad)
487 {
488   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
489   GstCaps *caps = NULL;
490   GstIterator *iter = NULL;
491   GValue v = { 0 };
492   GstIteratorResult res;
493   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
494   GstCaps *othercaps = NULL;
495
496   if (peercaps) {
497     othercaps = gst_caps_intersect (peercaps,
498         gst_pad_get_pad_template_caps (pad));
499     gst_caps_unref (peercaps);
500   } else {
501     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
502   }
503
504   clear_caps (othercaps, FALSE);
505
506   g_value_init (&v, GST_TYPE_CAPS);
507
508   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
509   do {
510     gst_value_set_caps (&v, othercaps);
511     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
512   } while (res == GST_ITERATOR_RESYNC);
513   gst_iterator_free (iter);
514
515   caps = (GstCaps *) gst_value_get_caps (&v);
516
517   if (res == GST_ITERATOR_ERROR) {
518     gst_caps_unref (caps);
519     caps = gst_caps_new_empty ();
520   }
521
522   if (othercaps)
523     gst_caps_unref (othercaps);
524   gst_object_unref (mux);
525
526   return caps;
527 }
528
529
530 static void
531 gst_rtp_mux_get_property (GObject * object,
532     guint prop_id, GValue * value, GParamSpec * pspec)
533 {
534   GstRTPMux *rtp_mux;
535
536   rtp_mux = GST_RTP_MUX (object);
537
538   switch (prop_id) {
539     case PROP_TIMESTAMP_OFFSET:
540       g_value_set_int (value, rtp_mux->ts_offset);
541       break;
542     case PROP_SEQNUM_OFFSET:
543       g_value_set_int (value, rtp_mux->seqnum_offset);
544       break;
545     case PROP_SEQNUM:
546       GST_OBJECT_LOCK (rtp_mux);
547       g_value_set_uint (value, rtp_mux->seqnum);
548       GST_OBJECT_UNLOCK (rtp_mux);
549       break;
550     case PROP_SSRC:
551       g_value_set_uint (value, rtp_mux->ssrc);
552       break;
553     default:
554       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
555       break;
556   }
557 }
558
559 static void
560 gst_rtp_mux_set_property (GObject * object,
561     guint prop_id, const GValue * value, GParamSpec * pspec)
562 {
563   GstRTPMux *rtp_mux;
564
565   rtp_mux = GST_RTP_MUX (object);
566
567   switch (prop_id) {
568     case PROP_TIMESTAMP_OFFSET:
569       rtp_mux->ts_offset = g_value_get_int (value);
570       break;
571     case PROP_SEQNUM_OFFSET:
572       rtp_mux->seqnum_offset = g_value_get_int (value);
573       break;
574     case PROP_SSRC:
575       rtp_mux->ssrc = g_value_get_uint (value);
576       break;
577     default:
578       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579       break;
580   }
581 }
582
583 static void
584 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
585 {
586   GST_OBJECT_LOCK (rtp_mux);
587
588   if (rtp_mux->ssrc == -1)
589     rtp_mux->current_ssrc = g_random_int ();
590   else
591     rtp_mux->current_ssrc = rtp_mux->ssrc;
592
593   if (rtp_mux->seqnum_offset == -1)
594     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
595   else
596     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
597   rtp_mux->seqnum = rtp_mux->seqnum_base;
598
599   if (rtp_mux->ts_offset == -1)
600     rtp_mux->ts_base = g_random_int ();
601   else
602     rtp_mux->ts_base = rtp_mux->ts_offset;
603   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
604
605   GST_OBJECT_UNLOCK (rtp_mux);
606 }
607
608 static GstStateChangeReturn
609 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
610 {
611   GstRTPMux *rtp_mux;
612
613   rtp_mux = GST_RTP_MUX (element);
614
615   switch (transition) {
616     case GST_STATE_CHANGE_NULL_TO_READY:
617       break;
618     case GST_STATE_CHANGE_READY_TO_PAUSED:
619       gst_rtp_mux_ready_to_paused (rtp_mux);
620       break;
621     case GST_STATE_CHANGE_PAUSED_TO_READY:
622       break;
623     default:
624       break;
625   }
626
627   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
628 }
629
630 gboolean
631 gst_rtp_mux_plugin_init (GstPlugin * plugin)
632 {
633   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
634
635   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
636       GST_TYPE_RTP_MUX);
637 }