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
49 PROP_TIMESTAMP_OFFSET,
55 #define DEFAULT_TIMESTAMP_OFFSET -1
56 #define DEFAULT_SEQNUM_OFFSET -1
57 #define DEFAULT_SSRC -1
61 gboolean have_clock_base;
65 } GstRTPMuxPadPrivate;
67 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("application/x-rtp")
73 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
76 GST_STATIC_CAPS ("application/x-rtp")
79 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
80 GstPadTemplate * templ, const gchar * name);
81 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
82 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
83 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
84 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
86 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
87 element, GstStateChange transition);
89 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
93 static void gst_rtp_mux_dispose (GObject * object);
95 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
98 gst_rtp_mux_base_init (gpointer g_class)
100 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&src_factory));
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&sink_factory));
107 gst_element_class_set_details_simple (element_class, "RTP muxer",
109 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
113 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
118 gobject_class = (GObjectClass *) klass;
119 gstelement_class = (GstElementClass *) klass;
121 gobject_class->get_property = gst_rtp_mux_get_property;
122 gobject_class->set_property = gst_rtp_mux_set_property;
123 gobject_class->dispose = gst_rtp_mux_dispose;
125 g_object_class_install_property (G_OBJECT_CLASS (klass),
126 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
128 "Offset to add to all outgoing timestamps (-1 = random)", -1,
129 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
130 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
131 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
132 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
133 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
134 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
135 g_param_spec_uint ("seqnum", "Sequence number",
136 "The RTP sequence number of the last processed packet",
137 0, G_MAXUINT, 0, G_PARAM_READABLE));
138 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
139 g_param_spec_uint ("ssrc", "SSRC",
140 "The SSRC of the packets (-1 == random)",
141 0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
143 gstelement_class->request_new_pad =
144 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
145 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
146 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
148 klass->chain_func = gst_rtp_mux_chain;
152 gst_rtp_mux_dispose (GObject * object)
157 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
158 GstPad *pad = GST_PAD (item->data);
159 if (GST_PAD_IS_SINK (pad)) {
160 gst_element_release_request_pad (GST_ELEMENT (object), pad);
165 G_OBJECT_CLASS (parent_class)->dispose (object);
169 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
174 gboolean result = FALSE;
175 gboolean done = FALSE;
177 rtp_mux = gst_pad_get_parent_element (pad);
178 g_return_val_if_fail (rtp_mux != NULL, FALSE);
180 iter = gst_element_iterate_sink_pads (rtp_mux);
183 switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
184 case GST_ITERATOR_OK:
185 gst_event_ref (event);
186 result |= gst_pad_push_event (sinkpad, event);
187 gst_object_unref (sinkpad);
189 case GST_ITERATOR_RESYNC:
190 gst_iterator_resync (iter);
193 case GST_ITERATOR_ERROR:
194 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
195 case GST_ITERATOR_DONE:
200 gst_iterator_free (iter);
201 gst_object_unref (rtp_mux);
202 gst_event_unref (event);
208 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
210 GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
213 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
215 gst_pad_set_event_function (object->srcpad,
216 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
217 gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
219 object->ssrc = DEFAULT_SSRC;
220 object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
221 object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
225 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
227 GstRTPMuxClass *klass;
228 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
230 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
232 /* setup some pad functions */
233 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
234 gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
235 if (klass->chain_func)
236 gst_pad_set_chain_function (sinkpad, klass->chain_func);
237 if (klass->sink_event_func)
238 gst_pad_set_event_function (sinkpad, klass->sink_event_func);
240 /* This could break with gstreamer 0.10.9 */
241 gst_pad_set_active (sinkpad, TRUE);
243 gst_pad_set_element_private (sinkpad, padpriv);
245 /* dd the pad to the element */
246 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
250 gst_rtp_mux_request_new_pad (GstElement * element,
251 GstPadTemplate * templ, const gchar * req_name)
256 g_return_val_if_fail (templ != NULL, NULL);
257 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
259 rtp_mux = GST_RTP_MUX (element);
261 if (templ->direction != GST_PAD_SINK) {
262 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
266 newpad = gst_pad_new_from_template (templ, req_name);
268 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
270 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
276 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
278 GstRTPMuxPadPrivate *padpriv;
280 GST_OBJECT_LOCK (element);
281 padpriv = gst_pad_get_element_private (pad);
282 gst_pad_set_element_private (pad, NULL);
283 GST_OBJECT_UNLOCK (element);
285 gst_element_remove_pad (element, pad);
288 gst_caps_replace (&padpriv->out_caps, NULL);
289 g_slice_free (GstRTPMuxPadPrivate, padpriv);
293 /* Put our own clock-base on the buffer */
295 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
299 guint32 sink_ts_base = 0;
300 GstRTPMuxPadPrivate *padpriv;
303 GST_OBJECT_LOCK (rtp_mux);
304 padpriv = gst_pad_get_element_private (pad);
305 if (padpriv && padpriv->have_clock_base)
306 sink_ts_base = padpriv->clock_base;
307 GST_OBJECT_UNLOCK (rtp_mux);
309 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
310 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
311 gst_rtp_buffer_get_timestamp (buffer), ts);
312 gst_rtp_buffer_set_timestamp (buffer, ts);
316 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
320 GstRTPMuxPadPrivate *padpriv;
322 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
324 if (!gst_rtp_buffer_validate (buffer)) {
325 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
326 gst_object_unref (rtp_mux);
327 return GST_FLOW_ERROR;
330 buffer = gst_buffer_make_writable (buffer);
332 GST_OBJECT_LOCK (rtp_mux);
334 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
335 padpriv = gst_pad_get_element_private (pad);
337 gst_buffer_set_caps (buffer, padpriv->out_caps);
338 GST_OBJECT_UNLOCK (rtp_mux);
339 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
340 gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
341 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
342 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
343 gst_rtp_buffer_get_timestamp (buffer));
346 ret = GST_FLOW_NOT_LINKED;
347 gst_buffer_unref (buffer);
351 ret = gst_pad_push (rtp_mux->srcpad, buffer);
355 gst_object_unref (rtp_mux);
360 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
363 GstStructure *structure;
364 gboolean ret = FALSE;
365 GstRTPMuxPadPrivate *padpriv;
367 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
369 structure = gst_caps_get_structure (caps, 0);
374 GST_OBJECT_LOCK (rtp_mux);
375 padpriv = gst_pad_get_element_private (pad);
377 gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
378 padpriv->have_clock_base = TRUE;
380 GST_OBJECT_UNLOCK (rtp_mux);
382 caps = gst_caps_copy (caps);
384 gst_caps_set_simple (caps,
385 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
386 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
388 GST_DEBUG_OBJECT (rtp_mux,
389 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
390 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
392 if (rtp_mux->ssrc == -1) {
393 if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
394 rtp_mux->current_ssrc = g_value_get_uint
395 (gst_structure_get_value (structure, "ssrc"));
400 GST_OBJECT_LOCK (rtp_mux);
401 padpriv = gst_pad_get_element_private (pad);
403 gst_caps_replace (&padpriv->out_caps, caps);
404 GST_OBJECT_UNLOCK (rtp_mux);
406 gst_caps_unref (caps);
409 gst_object_unref (rtp_mux);
415 clear_caps (GstCaps * caps, gboolean only_clock_rate)
419 /* Lets only match on the clock-rate */
420 for (i = 0; i < gst_caps_get_size (caps); i++) {
421 GstStructure *s = gst_caps_get_structure (caps, i);
423 for (j = 0; j < gst_structure_n_fields (s); j++) {
424 const gchar *name = gst_structure_nth_field_name (s, j);
426 if (strcmp (name, "clock-rate") && (only_clock_rate ||
427 (strcmp (name, "ssrc")))) {
428 gst_structure_remove_field (s, name);
436 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
438 GstPad *mypad = user_data;
442 const GstCaps *accumcaps;
446 gst_object_unref (pad);
450 peercaps = gst_pad_peer_get_caps (pad);
452 gst_object_unref (pad);
456 othercaps = gst_caps_intersect (peercaps,
457 gst_pad_get_pad_template_caps (pad));
458 gst_caps_unref (peercaps);
460 accumcaps = gst_value_get_caps (ret);
462 clear_caps (othercaps, TRUE);
464 intersect = gst_caps_intersect (accumcaps, othercaps);
466 g_value_take_boxed (ret, intersect);
468 gst_caps_unref (othercaps);
469 gst_object_unref (pad);
471 return !gst_caps_is_empty (intersect);
475 gst_rtp_mux_getcaps (GstPad * pad)
477 GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
478 GstCaps *caps = NULL;
479 GstIterator *iter = NULL;
481 GstIteratorResult res;
482 GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
483 GstCaps *othercaps = NULL;
486 othercaps = gst_caps_intersect (peercaps,
487 gst_pad_get_pad_template_caps (pad));
488 gst_caps_unref (peercaps);
490 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
493 clear_caps (othercaps, FALSE);
495 g_value_init (&v, GST_TYPE_CAPS);
497 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
499 gst_value_set_caps (&v, othercaps);
500 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
501 } while (res == GST_ITERATOR_RESYNC);
502 gst_iterator_free (iter);
504 caps = (GstCaps *) gst_value_get_caps (&v);
506 if (res == GST_ITERATOR_ERROR) {
507 gst_caps_unref (caps);
508 caps = gst_caps_new_empty ();
512 gst_caps_unref (othercaps);
513 gst_object_unref (mux);
520 gst_rtp_mux_get_property (GObject * object,
521 guint prop_id, GValue * value, GParamSpec * pspec)
525 rtp_mux = GST_RTP_MUX (object);
528 case PROP_TIMESTAMP_OFFSET:
529 g_value_set_int (value, rtp_mux->ts_offset);
531 case PROP_SEQNUM_OFFSET:
532 g_value_set_int (value, rtp_mux->seqnum_offset);
535 GST_OBJECT_LOCK (rtp_mux);
536 g_value_set_uint (value, rtp_mux->seqnum);
537 GST_OBJECT_UNLOCK (rtp_mux);
540 g_value_set_uint (value, rtp_mux->ssrc);
543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
549 gst_rtp_mux_set_property (GObject * object,
550 guint prop_id, const GValue * value, GParamSpec * pspec)
554 rtp_mux = GST_RTP_MUX (object);
557 case PROP_TIMESTAMP_OFFSET:
558 rtp_mux->ts_offset = g_value_get_int (value);
560 case PROP_SEQNUM_OFFSET:
561 rtp_mux->seqnum_offset = g_value_get_int (value);
564 rtp_mux->ssrc = g_value_get_uint (value);
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
575 GST_OBJECT_LOCK (rtp_mux);
577 if (rtp_mux->ssrc == -1)
578 rtp_mux->current_ssrc = g_random_int ();
580 rtp_mux->current_ssrc = rtp_mux->ssrc;
582 if (rtp_mux->seqnum_offset == -1)
583 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
585 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
586 rtp_mux->seqnum = rtp_mux->seqnum_base;
588 if (rtp_mux->ts_offset == -1)
589 rtp_mux->ts_base = g_random_int ();
591 rtp_mux->ts_base = rtp_mux->ts_offset;
592 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
594 GST_OBJECT_UNLOCK (rtp_mux);
597 static GstStateChangeReturn
598 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
602 rtp_mux = GST_RTP_MUX (element);
604 switch (transition) {
605 case GST_STATE_CHANGE_NULL_TO_READY:
607 case GST_STATE_CHANGE_READY_TO_PAUSED:
608 gst_rtp_mux_ready_to_paused (rtp_mux);
610 case GST_STATE_CHANGE_PAUSED_TO_READY:
616 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
620 gst_rtp_mux_plugin_init (GstPlugin * plugin)
622 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
624 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,