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
28 * @short_description: Muxer that takes one or several RTP streams
29 * and muxes them to a single rtp stream.
40 #include <gst/rtp/gstrtpbuffer.h>
41 #include <gstrtpmux.h>
44 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
45 #define GST_CAT_DEFAULT gst_rtp_mux_debug
47 /* elementfactory information */
48 static const GstElementDetails gst_rtp_mux_details =
49 GST_ELEMENT_DETAILS ("RTP muxer",
51 "multiplex N rtp streams into one",
52 "Zeeshan Ali <first.last@nokia.com>");
58 PROP_TIMESTAMP_OFFSET,
64 #define DEFAULT_TIMESTAMP_OFFSET -1
65 #define DEFAULT_SEQNUM_OFFSET -1
66 #define DEFAULT_SSRC -1
67 #define DEFAULT_CLOCK_RATE 0
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 void gst_rtp_mux_base_init (gpointer g_class);
87 static void gst_rtp_mux_class_init (GstRTPMuxClass * klass);
88 static void gst_rtp_mux_init (GstRTPMux * rtp_mux);
90 static void gst_rtp_mux_finalize (GObject * object);
92 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
93 GstPadTemplate * templ, const gchar * name);
94 static void gst_rtp_mux_release_pad (GstElement * element, GstPad *pad);
95 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad,
97 static gboolean gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps);
99 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
100 element, GstStateChange transition);
102 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static GstElementClass *parent_class = NULL;
110 gst_rtp_mux_get_type (void)
112 static GType rtp_mux_type = 0;
115 static const GTypeInfo rtp_mux_info = {
116 sizeof (GstRTPMuxClass),
117 gst_rtp_mux_base_init,
119 (GClassInitFunc) gst_rtp_mux_class_init,
124 (GInstanceInitFunc) gst_rtp_mux_init,
128 g_type_register_static (GST_TYPE_ELEMENT, "GstRTPMux",
135 gst_rtp_mux_base_init (gpointer g_class)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
139 gst_element_class_add_pad_template (element_class,
140 gst_static_pad_template_get (&src_factory));
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&sink_factory));
144 gst_element_class_set_details (element_class, &gst_rtp_mux_details);
148 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
156 parent_class = g_type_class_peek_parent (klass);
158 gobject_class->finalize = gst_rtp_mux_finalize;
159 gobject_class->get_property = gst_rtp_mux_get_property;
160 gobject_class->set_property = gst_rtp_mux_set_property;
162 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLOCK_RATE,
163 g_param_spec_uint ("clock-rate", "clockrate",
164 "The clock-rate of the RTP streams",
165 0, G_MAXUINT, DEFAULT_CLOCK_RATE, G_PARAM_READWRITE));
166 g_object_class_install_property (G_OBJECT_CLASS (klass),
167 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
169 "Offset to add to all outgoing timestamps (-1 = random)", -1,
170 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
171 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
172 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
173 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
174 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
175 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
176 g_param_spec_uint ("seqnum", "Sequence number",
177 "The RTP sequence number of the last processed packet",
178 0, G_MAXUINT, 0, G_PARAM_READABLE));
179 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
180 g_param_spec_uint ("ssrc", "SSRC",
181 "The SSRC of the packets (-1 == random)",
182 0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
184 gstelement_class->request_new_pad = gst_rtp_mux_request_new_pad;
185 gstelement_class->release_pad = gst_rtp_mux_release_pad;
186 gstelement_class->change_state = gst_rtp_mux_change_state;
188 klass->chain_func = gst_rtp_mux_chain;
191 static gboolean gst_rtp_mux_src_event (GstPad * pad,
197 gboolean result = FALSE;
198 gboolean done = FALSE;
200 rtp_mux = gst_pad_get_parent_element (pad);
201 g_return_val_if_fail (rtp_mux != NULL, FALSE);
203 iter = gst_element_iterate_sink_pads (rtp_mux);
206 switch (gst_iterator_next (iter, (gpointer) &sinkpad)) {
207 case GST_ITERATOR_OK:
208 gst_event_ref (event);
209 result |= gst_pad_push_event (sinkpad, event);
210 gst_object_unref (sinkpad);
212 case GST_ITERATOR_RESYNC:
213 gst_iterator_resync (iter);
215 case GST_ITERATOR_ERROR:
216 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
217 case GST_ITERATOR_DONE:
223 gst_event_unref (event);
229 gst_rtp_mux_init (GstRTPMux * rtp_mux)
231 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
234 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
236 gst_pad_set_event_function (rtp_mux->srcpad, gst_rtp_mux_src_event);
237 gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
239 rtp_mux->ssrc = DEFAULT_SSRC;
240 rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
241 rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
242 rtp_mux->clock_rate = DEFAULT_CLOCK_RATE;
246 gst_rtp_mux_finalize (GObject * object)
250 rtp_mux = GST_RTP_MUX (object);
252 G_OBJECT_CLASS (parent_class)->finalize (object);
256 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ)
258 GstPad *newpad = NULL;
259 GstPadTemplate * class_templ;
261 class_templ = gst_element_class_get_pad_template (
262 GST_ELEMENT_GET_CLASS (rtp_mux), "sink_%d");
264 if (templ == class_templ) {
267 /* create new pad with the name */
268 name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
269 newpad = gst_pad_new_from_template (templ, name);
274 GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
281 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
283 GstRTPMuxClass *klass;
284 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
286 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
288 /* setup some pad functions */
289 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
290 if (klass->chain_func)
291 gst_pad_set_chain_function (sinkpad, klass->chain_func);
292 if (klass->sink_event_func)
293 gst_pad_set_event_function (sinkpad, klass->sink_event_func);
295 /* This could break with gstreamer 0.10.9 */
296 gst_pad_set_active (sinkpad, TRUE);
298 gst_pad_set_element_private (sinkpad, padpriv);
300 /* dd the pad to the element */
301 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
305 gst_rtp_mux_request_new_pad (GstElement * element,
306 GstPadTemplate * templ, const gchar * req_name)
311 g_return_val_if_fail (templ != NULL, NULL);
312 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
314 rtp_mux = GST_RTP_MUX (element);
316 if (templ->direction != GST_PAD_SINK) {
317 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
321 newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ);
323 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
325 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
331 gst_rtp_mux_release_pad (GstElement * element, GstPad *pad)
333 GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
336 g_slice_free (GstRTPMuxPadPrivate, padpriv);
337 gst_pad_set_element_private (pad, NULL);
339 gst_element_remove_pad (element, pad);
342 /* Put our own clock-base on the buffer */
344 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
348 guint32 sink_ts_base = 0;
349 GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
351 if (padpriv->have_base)
352 sink_ts_base = padpriv->clock_base;
354 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
355 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
356 gst_rtp_buffer_get_timestamp (buffer), ts);
357 gst_rtp_buffer_set_timestamp (buffer, ts);
361 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
366 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
368 buffer = gst_buffer_make_writable(buffer);
371 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
372 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
373 gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
374 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
375 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum);
377 gst_buffer_set_caps (buffer, GST_PAD_CAPS (rtp_mux->srcpad));
379 ret = gst_pad_push (rtp_mux->srcpad, buffer);
381 gst_object_unref (rtp_mux);
386 gst_rtp_mux_set_clock_rate (GstRTPMux *rtp_mux, gint clock_rate)
390 if (rtp_mux->clock_rate == 0) {
391 rtp_mux->clock_rate = clock_rate;
395 else if (rtp_mux->clock_rate != clock_rate) {
396 GST_WARNING_OBJECT (rtp_mux, "Clock-rate already set to: %u",
397 rtp_mux->clock_rate);
405 gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps)
408 GstStructure *structure;
411 GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
413 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
415 structure = gst_caps_get_structure (caps, 0);
416 if (gst_structure_get_int (structure, "clock-rate", &clock_rate)) {
417 ret = gst_rtp_mux_set_clock_rate (rtp_mux, clock_rate);
423 if (gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
424 padpriv->have_base = TRUE;
427 caps = gst_caps_copy (caps);
429 gst_caps_set_simple (caps,
430 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
431 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base,
434 GST_DEBUG_OBJECT (rtp_mux,
435 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
436 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
437 gst_caps_unref (caps);
440 gst_object_unref (rtp_mux);
446 gst_rtp_mux_get_property (GObject * object,
447 guint prop_id, GValue * value, GParamSpec * pspec)
451 rtp_mux = GST_RTP_MUX (object);
454 case PROP_CLOCK_RATE:
455 g_value_set_uint (value, rtp_mux->clock_rate);
457 case PROP_TIMESTAMP_OFFSET:
458 g_value_set_int (value, rtp_mux->ts_offset);
460 case PROP_SEQNUM_OFFSET:
461 g_value_set_int (value, rtp_mux->seqnum_offset);
464 g_value_set_uint (value, rtp_mux->seqnum);
467 g_value_set_uint (value, rtp_mux->ssrc);
470 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
476 gst_rtp_mux_set_property (GObject * object,
477 guint prop_id, const GValue * value, GParamSpec * pspec)
481 rtp_mux = GST_RTP_MUX (object);
484 case PROP_CLOCK_RATE:
485 rtp_mux->clock_rate = g_value_get_uint (value);
487 case PROP_TIMESTAMP_OFFSET:
488 rtp_mux->ts_offset = g_value_get_int (value);
490 case PROP_SEQNUM_OFFSET:
491 rtp_mux->seqnum_offset = g_value_get_int (value);
494 rtp_mux->ssrc = g_value_get_uint (value);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
505 if (rtp_mux->ssrc == -1)
506 rtp_mux->current_ssrc = g_random_int ();
508 rtp_mux->current_ssrc = rtp_mux->ssrc;
510 if (rtp_mux->seqnum_offset == -1)
511 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
513 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
514 rtp_mux->seqnum = rtp_mux->seqnum_base;
516 if (rtp_mux->ts_offset == -1)
517 rtp_mux->ts_base = g_random_int ();
519 rtp_mux->ts_base = rtp_mux->ts_offset;
520 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
523 static GstStateChangeReturn
524 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
528 rtp_mux = GST_RTP_MUX (element);
530 switch (transition) {
531 case GST_STATE_CHANGE_NULL_TO_READY:
533 case GST_STATE_CHANGE_READY_TO_PAUSED:
534 gst_rtp_mux_ready_to_paused (rtp_mux);
536 case GST_STATE_CHANGE_PAUSED_TO_READY:
542 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
546 gst_rtp_mux_plugin_init (GstPlugin * plugin)
548 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0,
551 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,