1 /* RTP muxer element for GStreamer
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>
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.
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.
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.
27 * SECTION:element-rtpmux
29 * The rtp muxer takes multiple RTP streams having the same clock-rate and
30 * muxes into a single stream with a single SSRC.
38 #include <gst/rtp/gstrtpbuffer.h>
41 #include "gstrtpmux.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
44 #define GST_CAT_DEFAULT gst_rtp_mux_debug
46 /* elementfactory information */
47 static const GstElementDetails gst_rtp_mux_details =
48 GST_ELEMENT_DETAILS ("RTP muxer",
50 "multiplex N rtp streams into one",
51 "Zeeshan Ali <first.last@nokia.com>");
56 PROP_TIMESTAMP_OFFSET,
62 #define DEFAULT_TIMESTAMP_OFFSET -1
63 #define DEFAULT_SEQNUM_OFFSET -1
64 #define DEFAULT_SSRC -1
68 gboolean have_clock_base;
72 } GstRTPMuxPadPrivate;
74 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS ("application/x-rtp")
80 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
83 GST_STATIC_CAPS ("application/x-rtp")
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);
93 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
94 element, GstStateChange transition);
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);
102 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
105 gst_rtp_mux_base_init (gpointer g_class)
107 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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));
114 gst_element_class_set_details (element_class, &gst_rtp_mux_details);
118 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 gobject_class->get_property = gst_rtp_mux_get_property;
127 gobject_class->set_property = gst_rtp_mux_set_property;
129 g_object_class_install_property (G_OBJECT_CLASS (klass),
130 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("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));
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);
152 klass->chain_func = gst_rtp_mux_chain;
156 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
161 gboolean result = FALSE;
162 gboolean done = FALSE;
164 rtp_mux = gst_pad_get_parent_element (pad);
165 g_return_val_if_fail (rtp_mux != NULL, FALSE);
167 iter = gst_element_iterate_sink_pads (rtp_mux);
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);
176 case GST_ITERATOR_RESYNC:
177 gst_iterator_resync (iter);
180 case GST_ITERATOR_ERROR:
181 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
182 case GST_ITERATOR_DONE:
187 gst_iterator_free (iter);
188 gst_object_unref (rtp_mux);
189 gst_event_unref (event);
195 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
197 GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
200 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
206 object->ssrc = DEFAULT_SSRC;
207 object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
208 object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
212 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ)
214 GstPad *newpad = NULL;
215 GstPadTemplate *class_templ;
218 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (rtp_mux),
221 if (templ == class_templ) {
224 /* create new pad with the name */
225 name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
226 newpad = gst_pad_new_from_template (templ, name);
231 GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
238 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
240 GstRTPMuxClass *klass;
241 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
243 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
245 /* setup some pad functions */
246 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
247 gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
248 if (klass->chain_func)
249 gst_pad_set_chain_function (sinkpad, klass->chain_func);
250 if (klass->sink_event_func)
251 gst_pad_set_event_function (sinkpad, klass->sink_event_func);
253 /* This could break with gstreamer 0.10.9 */
254 gst_pad_set_active (sinkpad, TRUE);
256 gst_pad_set_element_private (sinkpad, padpriv);
258 /* dd the pad to the element */
259 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
263 gst_rtp_mux_request_new_pad (GstElement * element,
264 GstPadTemplate * templ, const gchar * req_name)
269 g_return_val_if_fail (templ != NULL, NULL);
270 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
272 rtp_mux = GST_RTP_MUX (element);
274 if (templ->direction != GST_PAD_SINK) {
275 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
279 newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ);
281 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
283 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
289 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
291 GstRTPMuxPadPrivate *padpriv;
293 GST_OBJECT_LOCK (element);
294 padpriv = gst_pad_get_element_private (pad);
295 gst_pad_set_element_private (pad, NULL);
296 GST_OBJECT_UNLOCK (element);
298 gst_element_remove_pad (element, pad);
301 gst_caps_replace (&padpriv->out_caps, NULL);
302 g_slice_free (GstRTPMuxPadPrivate, padpriv);
306 /* Put our own clock-base on the buffer */
308 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
312 guint32 sink_ts_base = 0;
313 GstRTPMuxPadPrivate *padpriv;
316 GST_OBJECT_LOCK (rtp_mux);
317 padpriv = gst_pad_get_element_private (pad);
318 if (padpriv && padpriv->have_clock_base)
319 sink_ts_base = padpriv->clock_base;
320 GST_OBJECT_UNLOCK (rtp_mux);
322 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
323 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
324 gst_rtp_buffer_get_timestamp (buffer), ts);
325 gst_rtp_buffer_set_timestamp (buffer, ts);
329 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
333 GstRTPMuxPadPrivate *padpriv;
335 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
337 if (!gst_rtp_buffer_validate (buffer)) {
338 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
339 gst_object_unref (rtp_mux);
340 return GST_FLOW_ERROR;
343 buffer = gst_buffer_make_writable (buffer);
345 GST_OBJECT_LOCK (rtp_mux);
347 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
348 padpriv = gst_pad_get_element_private (pad);
350 gst_buffer_set_caps (buffer, padpriv->out_caps);
351 GST_OBJECT_UNLOCK (rtp_mux);
352 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
353 gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
354 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
355 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
356 gst_rtp_buffer_get_timestamp (buffer));
359 ret = GST_FLOW_NOT_LINKED;
360 gst_buffer_unref (buffer);
364 ret = gst_pad_push (rtp_mux->srcpad, buffer);
368 gst_object_unref (rtp_mux);
373 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
376 GstStructure *structure;
377 gboolean ret = FALSE;
378 GstRTPMuxPadPrivate *padpriv;
380 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
382 structure = gst_caps_get_structure (caps, 0);
387 GST_OBJECT_LOCK (rtp_mux);
388 padpriv = gst_pad_get_element_private (pad);
390 gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
391 padpriv->have_clock_base = TRUE;
393 GST_OBJECT_UNLOCK (rtp_mux);
395 caps = gst_caps_copy (caps);
397 gst_caps_set_simple (caps,
398 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
399 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
401 GST_DEBUG_OBJECT (rtp_mux,
402 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
403 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
406 GST_OBJECT_LOCK (rtp_mux);
407 padpriv = gst_pad_get_element_private (pad);
409 gst_caps_replace (&padpriv->out_caps, caps);
410 GST_OBJECT_UNLOCK (rtp_mux);
412 gst_caps_unref (caps);
415 gst_object_unref (rtp_mux);
421 clear_caps (GstCaps * caps, gboolean only_clock_rate)
425 /* Lets only match on the clock-rate */
426 for (i = 0; i < gst_caps_get_size (caps); i++) {
427 GstStructure *s = gst_caps_get_structure (caps, i);
429 for (j = 0; j < gst_structure_n_fields (s); j++) {
430 const gchar *name = gst_structure_nth_field_name (s, j);
432 if (strcmp (name, "clock-rate") && (only_clock_rate ||
433 (strcmp (name, "ssrc")))) {
434 gst_structure_remove_field (s, name);
442 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
444 GstPad *mypad = user_data;
448 const GstCaps *accumcaps;
452 gst_object_unref (pad);
456 peercaps = gst_pad_peer_get_caps (pad);
458 gst_object_unref (pad);
462 othercaps = gst_caps_intersect (peercaps,
463 gst_pad_get_pad_template_caps (pad));
464 gst_caps_unref (peercaps);
466 accumcaps = gst_value_get_caps (ret);
468 clear_caps (othercaps, TRUE);
470 intersect = gst_caps_intersect (accumcaps, othercaps);
472 g_value_take_boxed (ret, intersect);
474 gst_caps_unref (othercaps);
475 gst_object_unref (pad);
477 return !gst_caps_is_empty (intersect);
481 gst_rtp_mux_getcaps (GstPad * pad)
483 GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
484 GstCaps *caps = NULL;
485 GstIterator *iter = NULL;
487 GstIteratorResult res;
488 GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
489 GstCaps *othercaps = NULL;
492 othercaps = gst_caps_intersect (peercaps,
493 gst_pad_get_pad_template_caps (pad));
494 gst_caps_unref (peercaps);
496 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
499 clear_caps (othercaps, FALSE);
501 g_value_init (&v, GST_TYPE_CAPS);
503 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
505 gst_value_set_caps (&v, othercaps);
506 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
507 } while (res == GST_ITERATOR_RESYNC);
508 gst_iterator_free (iter);
510 caps = (GstCaps *) gst_value_get_caps (&v);
512 if (res == GST_ITERATOR_ERROR) {
513 gst_caps_unref (caps);
514 caps = gst_caps_new_empty ();
518 gst_caps_unref (othercaps);
519 gst_object_unref (mux);
526 gst_rtp_mux_get_property (GObject * object,
527 guint prop_id, GValue * value, GParamSpec * pspec)
531 rtp_mux = GST_RTP_MUX (object);
534 case PROP_TIMESTAMP_OFFSET:
535 g_value_set_int (value, rtp_mux->ts_offset);
537 case PROP_SEQNUM_OFFSET:
538 g_value_set_int (value, rtp_mux->seqnum_offset);
541 GST_OBJECT_LOCK (rtp_mux);
542 g_value_set_uint (value, rtp_mux->seqnum);
543 GST_OBJECT_UNLOCK (rtp_mux);
546 g_value_set_uint (value, rtp_mux->ssrc);
549 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
555 gst_rtp_mux_set_property (GObject * object,
556 guint prop_id, const GValue * value, GParamSpec * pspec)
560 rtp_mux = GST_RTP_MUX (object);
563 case PROP_TIMESTAMP_OFFSET:
564 rtp_mux->ts_offset = g_value_get_int (value);
566 case PROP_SEQNUM_OFFSET:
567 rtp_mux->seqnum_offset = g_value_get_int (value);
570 rtp_mux->ssrc = g_value_get_uint (value);
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
581 GST_OBJECT_LOCK (rtp_mux);
583 if (rtp_mux->ssrc == -1)
584 rtp_mux->current_ssrc = g_random_int ();
586 rtp_mux->current_ssrc = rtp_mux->ssrc;
588 if (rtp_mux->seqnum_offset == -1)
589 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
591 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
592 rtp_mux->seqnum = rtp_mux->seqnum_base;
594 if (rtp_mux->ts_offset == -1)
595 rtp_mux->ts_base = g_random_int ();
597 rtp_mux->ts_base = rtp_mux->ts_offset;
598 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
600 GST_OBJECT_UNLOCK (rtp_mux);
603 static GstStateChangeReturn
604 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
608 rtp_mux = GST_RTP_MUX (element);
610 switch (transition) {
611 case GST_STATE_CHANGE_NULL_TO_READY:
613 case GST_STATE_CHANGE_READY_TO_PAUSED:
614 gst_rtp_mux_ready_to_paused (rtp_mux);
616 case GST_STATE_CHANGE_PAUSED_TO_READY:
622 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
626 gst_rtp_mux_plugin_init (GstPlugin * plugin)
628 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
630 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,