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>
44 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
45 #define GST_CAT_DEFAULT gst_rtp_mux_debug
47 #define GST_TYPE_RTP_MUX (gst_rtp_mux_get_type())
48 #define GST_RTP_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_MUX, GstRTPMux))
49 #define GST_RTP_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_MUX, GstRTPMux))
50 #define GST_IS_RTP_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_MUX))
51 #define GST_IS_RTP_MUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_MUX))
53 typedef struct _GstRTPMux GstRTPMux;
54 typedef struct _GstRTPMuxClass GstRTPMuxClass;
59 * The opaque #GstRTPMux structure.
77 struct _GstRTPMuxClass
79 GstElementClass parent_class;
82 /* elementfactory information */
83 static const GstElementDetails gst_rtp_mux_details =
84 GST_ELEMENT_DETAILS ("RTP muxer",
87 "Zeeshan Ali <first.last@nokia.com>");
97 #define DEFAULT_SEQNUM_OFFSET -1
99 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
102 GST_STATIC_CAPS ("application/x-rtp")
105 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
108 GST_STATIC_CAPS ("application/x-rtp")
111 static void gst_rtp_mux_base_init (gpointer g_class);
112 static void gst_rtp_mux_class_init (GstRTPMuxClass * klass);
113 static void gst_rtp_mux_init (GstRTPMux * rtp_mux);
115 static void gst_rtp_mux_finalize (GObject * object);
117 static gboolean gst_rtp_mux_handle_sink_event (GstPad * pad,
119 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
120 GstPadTemplate * templ, const gchar * name);
121 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad,
123 static gboolean gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps);
125 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
126 element, GstStateChange transition);
128 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
129 const GValue * value, GParamSpec * pspec);
130 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
131 GValue * value, GParamSpec * pspec);
133 static GstElementClass *parent_class = NULL;
136 gst_rtp_mux_get_type (void)
138 static GType rtp_mux_type = 0;
141 static const GTypeInfo rtp_mux_info = {
142 sizeof (GstRTPMuxClass),
143 gst_rtp_mux_base_init,
145 (GClassInitFunc) gst_rtp_mux_class_init,
150 (GInstanceInitFunc) gst_rtp_mux_init,
154 g_type_register_static (GST_TYPE_ELEMENT, "GstRTPMux",
161 gst_rtp_mux_base_init (gpointer g_class)
163 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
165 gst_element_class_add_pad_template (element_class,
166 gst_static_pad_template_get (&src_factory));
167 gst_element_class_add_pad_template (element_class,
168 gst_static_pad_template_get (&sink_factory));
170 gst_element_class_set_details (element_class, &gst_rtp_mux_details);
174 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
176 GObjectClass *gobject_class;
177 GstElementClass *gstelement_class;
179 gobject_class = (GObjectClass *) klass;
180 gstelement_class = (GstElementClass *) klass;
182 parent_class = g_type_class_peek_parent (klass);
184 gobject_class->finalize = gst_rtp_mux_finalize;
185 gobject_class->get_property = gst_rtp_mux_get_property;
186 gobject_class->set_property = gst_rtp_mux_set_property;
188 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
189 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
190 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
191 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
193 g_param_spec_uint ("seqnum", "Sequence number",
194 "The RTP sequence number of the last processed packet",
195 0, G_MAXUINT, 0, G_PARAM_READABLE));
197 gstelement_class->request_new_pad = gst_rtp_mux_request_new_pad;
198 gstelement_class->change_state = gst_rtp_mux_change_state;
202 gst_rtp_mux_init (GstRTPMux * rtp_mux)
204 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
207 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
209 gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
211 rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
215 gst_rtp_mux_finalize (GObject * object)
219 rtp_mux = GST_RTP_MUX (object);
221 G_OBJECT_CLASS (parent_class)->finalize (object);
225 gst_rtp_mux_request_new_pad (GstElement * element,
226 GstPadTemplate * templ, const gchar * req_name)
230 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
232 g_return_val_if_fail (templ != NULL, NULL);
234 rtp_mux = GST_RTP_MUX (element);
236 if (templ->direction != GST_PAD_SINK) {
237 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
241 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
243 if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
246 /* create new pad with the name */
247 name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
248 newpad = gst_pad_new_from_template (templ, name);
253 GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
257 /* setup some pad functions */
258 gst_pad_set_chain_function (newpad, gst_rtp_mux_chain);
259 gst_pad_set_setcaps_function (newpad, gst_rtp_mux_setcaps);
260 gst_pad_set_event_function (newpad, gst_rtp_mux_handle_sink_event);
262 /* dd the pad to the element */
263 gst_element_add_pad (element, newpad);
269 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
273 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
276 GST_LOG_OBJECT (rtp_mux, "setting RTP seqnum %d", rtp_mux->seqnum);
277 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
279 gst_object_unref (rtp_mux);
281 GST_DEBUG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
282 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum - 1);
284 return gst_pad_push (rtp_mux->srcpad, buffer);
288 gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps)
290 /*GstRTPMux *rtp_mux;
296 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
298 new_caps = gst_caps_copy (caps);
300 / * We want our own seq base on the caps * /
301 for (i=0; i< gst_caps_get_size (new_caps); i++) {
302 GstStructure *structure = gst_caps_get_structure (new_caps, i);
303 gst_structure_set (structure,
304 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
307 old_caps = GST_PAD_CAPS (rtp_mux->srcpad);
308 if (old_caps != NULL) {
309 new_caps = gst_caps_union (old_caps, new_caps);
312 GST_DEBUG_OBJECT (rtp_mux,
313 "seting caps %" GST_PTR_FORMAT " on src pad..", caps);
314 ret = gst_pad_set_caps (rtp_mux->srcpad, new_caps);
315 gst_caps_unref (new_caps);
323 gst_rtp_mux_set_sinkpads_blocked (GstRTPMux *rtp_mux, gboolean blocked, GstPad *exception)
328 GST_DEBUG_OBJECT (rtp_mux, "blocking all sink pads except: %s",
329 GST_ELEMENT_NAME (exception));
331 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
332 while (gst_iterator_next (iter, (gpointer) &pad) == GST_ITERATOR_OK) {
333 if (pad != exception) {
336 peer = gst_pad_get_peer (pad);
338 GST_DEBUG_OBJECT (rtp_mux, "blocking pad %s..", GST_ELEMENT_NAME (pad));
339 gst_pad_set_blocked (peer, blocked);
340 GST_DEBUG_OBJECT (rtp_mux, "pad %s blocked", GST_ELEMENT_NAME (pad));
342 gst_object_unref (GST_OBJECT (peer));
344 gst_object_unref (GST_OBJECT (pad));
349 gst_rtp_mux_handle_sink_event (GstPad * pad, GstEvent * event)
354 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
356 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
359 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
361 const GstStructure *structure;
363 structure = gst_event_get_structure (event);
364 /* FIXME: is this event generic enough to be given a generic name? */
365 if (structure && gst_structure_has_name (structure, "stream-lock")) {
368 if (!gst_structure_get_boolean (structure, "lock", &lock))
372 if (rtp_mux->special_pad != NULL) {
373 GST_WARNING_OBJECT (rtp_mux,
374 "Stream lock already acquired by pad %s",
375 GST_ELEMENT_NAME (rtp_mux->special_pad));
379 rtp_mux->special_pad = gst_object_ref (pad);
383 if (rtp_mux->special_pad == NULL) {
384 GST_WARNING_OBJECT (rtp_mux,
385 "Stream lock not acquired, can't release it");
389 else if (pad != rtp_mux->special_pad) {
390 GST_WARNING_OBJECT (rtp_mux,
391 "pad %s attempted to release Stream lock"
392 " which was acquired by pad %s", GST_ELEMENT_NAME (pad),
393 GST_ELEMENT_NAME (rtp_mux->special_pad));
397 gst_object_unref (rtp_mux->special_pad);
398 rtp_mux->special_pad = NULL;
401 gst_rtp_mux_set_sinkpads_blocked (rtp_mux, lock, pad);
409 gst_object_unref (rtp_mux);
411 return gst_pad_event_default (pad, event);
415 gst_rtp_mux_get_property (GObject * object,
416 guint prop_id, GValue * value, GParamSpec * pspec)
420 rtp_mux = GST_RTP_MUX (object);
423 case PROP_SEQNUM_OFFSET:
424 g_value_set_int (value, rtp_mux->seqnum_offset);
427 g_value_set_uint (value, rtp_mux->seqnum);
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
436 gst_rtp_mux_set_property (GObject * object,
437 guint prop_id, const GValue * value, GParamSpec * pspec)
441 rtp_mux = GST_RTP_MUX (object);
444 case PROP_SEQNUM_OFFSET:
445 rtp_mux->seqnum_offset = g_value_get_int (value);
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 static GstStateChangeReturn
454 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
457 GstStateChangeReturn ret;
459 rtp_mux = GST_RTP_MUX (element);
461 switch (transition) {
462 case GST_STATE_CHANGE_NULL_TO_READY:
464 case GST_STATE_CHANGE_READY_TO_PAUSED:
465 if (rtp_mux->seqnum_offset == -1)
466 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
468 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
469 rtp_mux->seqnum = rtp_mux->seqnum_base;
471 case GST_STATE_CHANGE_PAUSED_TO_READY:
477 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
478 if (ret == GST_STATE_CHANGE_FAILURE)
481 switch (transition) {
490 gst_rtp_mux_plugin_init (GstPlugin * plugin)
492 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0,
495 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
499 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
503 gst_rtp_mux_plugin_init,
507 "http://farsight.sf.net")