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;
66 } GstRTPMuxPadPrivate;
68 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("application/x-rtp")
74 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
77 GST_STATIC_CAPS ("application/x-rtp")
80 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
81 GstPadTemplate * templ, const gchar * name);
82 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
83 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
84 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
85 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
86 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event);
88 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
89 element, GstStateChange transition);
91 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
95 static void gst_rtp_mux_dispose (GObject * object);
97 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
100 gst_rtp_mux_base_init (gpointer g_class)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&src_factory));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&sink_factory));
109 gst_element_class_set_details_simple (element_class, "RTP muxer",
111 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
115 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass *) klass;
121 gstelement_class = (GstElementClass *) klass;
123 gobject_class->get_property = gst_rtp_mux_get_property;
124 gobject_class->set_property = gst_rtp_mux_set_property;
125 gobject_class->dispose = gst_rtp_mux_dispose;
127 g_object_class_install_property (G_OBJECT_CLASS (klass),
128 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
130 "Offset to add to all outgoing timestamps (-1 = random)", -1,
131 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
132 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
133 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
134 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
135 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
136 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
137 g_param_spec_uint ("seqnum", "Sequence number",
138 "The RTP sequence number of the last processed packet",
139 0, G_MAXUINT, 0, G_PARAM_READABLE));
140 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
141 g_param_spec_uint ("ssrc", "SSRC",
142 "The SSRC of the packets (-1 == random)",
143 0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
145 gstelement_class->request_new_pad =
146 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
147 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
148 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
150 klass->chain_func = gst_rtp_mux_chain;
154 gst_rtp_mux_dispose (GObject * object)
159 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
160 GstPad *pad = GST_PAD (item->data);
161 if (GST_PAD_IS_SINK (pad)) {
162 gst_element_release_request_pad (GST_ELEMENT (object), pad);
167 G_OBJECT_CLASS (parent_class)->dispose (object);
171 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
176 gboolean result = FALSE;
177 gboolean done = FALSE;
179 rtp_mux = gst_pad_get_parent_element (pad);
180 g_return_val_if_fail (rtp_mux != NULL, FALSE);
182 iter = gst_element_iterate_sink_pads (rtp_mux);
185 switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
186 case GST_ITERATOR_OK:
187 gst_event_ref (event);
188 result |= gst_pad_push_event (sinkpad, event);
189 gst_object_unref (sinkpad);
191 case GST_ITERATOR_RESYNC:
192 gst_iterator_resync (iter);
195 case GST_ITERATOR_ERROR:
196 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
197 case GST_ITERATOR_DONE:
202 gst_iterator_free (iter);
203 gst_object_unref (rtp_mux);
204 gst_event_unref (event);
210 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
212 GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
215 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
217 gst_pad_set_event_function (object->srcpad,
218 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
219 gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
221 object->ssrc = DEFAULT_SSRC;
222 object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
223 object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
225 object->segment_pending = TRUE;
229 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
231 GstRTPMuxClass *klass;
232 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
234 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
236 /* setup some pad functions */
237 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
238 gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
239 if (klass->chain_func)
240 gst_pad_set_chain_function (sinkpad, klass->chain_func);
241 gst_pad_set_event_function (sinkpad,
242 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
245 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
247 gst_pad_set_element_private (sinkpad, padpriv);
249 gst_pad_set_active (sinkpad, TRUE);
250 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
254 gst_rtp_mux_request_new_pad (GstElement * element,
255 GstPadTemplate * templ, const gchar * req_name)
260 g_return_val_if_fail (templ != NULL, NULL);
261 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
263 rtp_mux = GST_RTP_MUX (element);
265 if (templ->direction != GST_PAD_SINK) {
266 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
270 newpad = gst_pad_new_from_template (templ, req_name);
272 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
274 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
280 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
282 GstRTPMuxPadPrivate *padpriv;
284 GST_OBJECT_LOCK (element);
285 padpriv = gst_pad_get_element_private (pad);
286 gst_pad_set_element_private (pad, NULL);
287 GST_OBJECT_UNLOCK (element);
289 gst_element_remove_pad (element, pad);
292 gst_caps_replace (&padpriv->out_caps, NULL);
293 g_slice_free (GstRTPMuxPadPrivate, padpriv);
297 /* Put our own clock-base on the buffer */
299 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
303 guint32 sink_ts_base = 0;
304 GstRTPMuxPadPrivate *padpriv;
307 GST_OBJECT_LOCK (rtp_mux);
308 padpriv = gst_pad_get_element_private (pad);
309 if (padpriv && padpriv->have_clock_base)
310 sink_ts_base = padpriv->clock_base;
311 GST_OBJECT_UNLOCK (rtp_mux);
313 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
314 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
315 gst_rtp_buffer_get_timestamp (buffer), ts);
316 gst_rtp_buffer_set_timestamp (buffer, ts);
320 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
324 GstRTPMuxPadPrivate *padpriv;
325 GstEvent *newseg_event = NULL;
327 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
329 if (!gst_rtp_buffer_validate (buffer)) {
330 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
331 gst_object_unref (rtp_mux);
332 return GST_FLOW_ERROR;
335 buffer = gst_buffer_make_writable (buffer);
337 GST_OBJECT_LOCK (rtp_mux);
339 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
340 padpriv = gst_pad_get_element_private (pad);
342 gst_buffer_set_caps (buffer, padpriv->out_caps);
343 if (padpriv->segment.format == GST_FORMAT_TIME)
344 GST_BUFFER_TIMESTAMP (buffer) =
345 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
346 GST_BUFFER_TIMESTAMP (buffer));
349 if (rtp_mux->segment_pending) {
351 * We set the start at 0, because we re-timestamps to the running time
353 newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
354 GST_FORMAT_TIME, 0, -1, 0);
356 rtp_mux->segment_pending = FALSE;
358 GST_OBJECT_UNLOCK (rtp_mux);
360 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
361 gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
362 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
363 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
364 gst_rtp_buffer_get_timestamp (buffer));
367 gst_pad_push_event (rtp_mux->srcpad, newseg_event);
370 ret = GST_FLOW_NOT_LINKED;
371 gst_buffer_unref (buffer);
375 ret = gst_pad_push (rtp_mux->srcpad, buffer);
379 gst_object_unref (rtp_mux);
384 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
387 GstStructure *structure;
388 gboolean ret = FALSE;
389 GstRTPMuxPadPrivate *padpriv;
391 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
393 structure = gst_caps_get_structure (caps, 0);
398 GST_OBJECT_LOCK (rtp_mux);
399 padpriv = gst_pad_get_element_private (pad);
401 gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
402 padpriv->have_clock_base = TRUE;
404 GST_OBJECT_UNLOCK (rtp_mux);
406 caps = gst_caps_copy (caps);
408 gst_caps_set_simple (caps,
409 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
410 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
412 GST_DEBUG_OBJECT (rtp_mux,
413 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
414 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
416 if (rtp_mux->ssrc == -1) {
417 if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
418 rtp_mux->current_ssrc = g_value_get_uint
419 (gst_structure_get_value (structure, "ssrc"));
424 GST_OBJECT_LOCK (rtp_mux);
425 padpriv = gst_pad_get_element_private (pad);
427 gst_caps_replace (&padpriv->out_caps, caps);
428 GST_OBJECT_UNLOCK (rtp_mux);
430 gst_caps_unref (caps);
433 gst_object_unref (rtp_mux);
439 clear_caps (GstCaps * caps, gboolean only_clock_rate)
443 /* Lets only match on the clock-rate */
444 for (i = 0; i < gst_caps_get_size (caps); i++) {
445 GstStructure *s = gst_caps_get_structure (caps, i);
447 for (j = 0; j < gst_structure_n_fields (s); j++) {
448 const gchar *name = gst_structure_nth_field_name (s, j);
450 if (strcmp (name, "clock-rate") && (only_clock_rate ||
451 (strcmp (name, "ssrc")))) {
452 gst_structure_remove_field (s, name);
460 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
462 GstPad *mypad = user_data;
466 const GstCaps *accumcaps;
470 gst_object_unref (pad);
474 peercaps = gst_pad_peer_get_caps (pad);
476 gst_object_unref (pad);
480 othercaps = gst_caps_intersect (peercaps,
481 gst_pad_get_pad_template_caps (pad));
482 gst_caps_unref (peercaps);
484 accumcaps = gst_value_get_caps (ret);
486 clear_caps (othercaps, TRUE);
488 intersect = gst_caps_intersect (accumcaps, othercaps);
490 g_value_take_boxed (ret, intersect);
492 gst_caps_unref (othercaps);
493 gst_object_unref (pad);
495 return !gst_caps_is_empty (intersect);
499 gst_rtp_mux_getcaps (GstPad * pad)
501 GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
502 GstCaps *caps = NULL;
503 GstIterator *iter = NULL;
505 GstIteratorResult res;
506 GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
507 GstCaps *othercaps = NULL;
510 othercaps = gst_caps_intersect (peercaps,
511 gst_pad_get_pad_template_caps (pad));
512 gst_caps_unref (peercaps);
514 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
517 clear_caps (othercaps, FALSE);
519 g_value_init (&v, GST_TYPE_CAPS);
521 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
523 gst_value_set_caps (&v, othercaps);
524 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
525 } while (res == GST_ITERATOR_RESYNC);
526 gst_iterator_free (iter);
528 caps = (GstCaps *) gst_value_get_caps (&v);
530 if (res == GST_ITERATOR_ERROR) {
531 gst_caps_unref (caps);
532 caps = gst_caps_new_empty ();
536 gst_caps_unref (othercaps);
537 gst_object_unref (mux);
544 gst_rtp_mux_get_property (GObject * object,
545 guint prop_id, GValue * value, GParamSpec * pspec)
549 rtp_mux = GST_RTP_MUX (object);
552 case PROP_TIMESTAMP_OFFSET:
553 g_value_set_int (value, rtp_mux->ts_offset);
555 case PROP_SEQNUM_OFFSET:
556 g_value_set_int (value, rtp_mux->seqnum_offset);
559 GST_OBJECT_LOCK (rtp_mux);
560 g_value_set_uint (value, rtp_mux->seqnum);
561 GST_OBJECT_UNLOCK (rtp_mux);
564 g_value_set_uint (value, rtp_mux->ssrc);
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 gst_rtp_mux_set_property (GObject * object,
574 guint prop_id, const GValue * value, GParamSpec * pspec)
578 rtp_mux = GST_RTP_MUX (object);
581 case PROP_TIMESTAMP_OFFSET:
582 rtp_mux->ts_offset = g_value_get_int (value);
584 case PROP_SEQNUM_OFFSET:
585 rtp_mux->seqnum_offset = g_value_get_int (value);
588 rtp_mux->ssrc = g_value_get_uint (value);
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
601 gboolean ret = FALSE;
602 gboolean forward = TRUE;
604 mux = GST_RTP_MUX (gst_pad_get_parent (pad));
606 switch (GST_EVENT_TYPE (event)) {
607 case GST_EVENT_FLUSH_STOP:
609 GstRTPMuxPadPrivate *padpriv;
611 GST_OBJECT_LOCK (mux);
612 mux->segment_pending = TRUE;
613 padpriv = gst_pad_get_element_private (pad);
615 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
616 GST_OBJECT_UNLOCK (pad);
619 case GST_EVENT_NEWSEGMENT:
622 gdouble rate, applied_rate;
624 gint64 start, stop, position;
625 GstRTPMuxPadPrivate *padpriv;
627 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
628 &format, &start, &stop, &position);
630 GST_OBJECT_LOCK (mux);
631 padpriv = gst_pad_get_element_private (pad);
634 if (format == GST_FORMAT_TIME)
635 gst_segment_set_newsegment_full (&padpriv->segment, update,
636 rate, applied_rate, format, start, stop, position);
638 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
640 GST_OBJECT_UNLOCK (mux);
641 gst_event_unref (event);
651 GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (mux);
653 if (klass->sink_event_func)
654 klass->sink_event_func (pad, event);
656 ret = gst_pad_push_event (mux->srcpad, event);
659 gst_object_unref (mux);
665 clear_segment (gpointer data, gpointer user_data)
668 GstRTPMux *mux = user_data;
669 GstRTPMuxPadPrivate *padpriv;
671 GST_OBJECT_LOCK (mux);
672 padpriv = gst_pad_get_element_private (pad);
674 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
675 GST_OBJECT_UNLOCK (mux);
677 gst_object_unref (pad);
682 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
686 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
687 while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
688 GST_ITERATOR_RESYNC);
689 gst_iterator_free (iter);
691 GST_OBJECT_LOCK (rtp_mux);
692 rtp_mux->segment_pending = TRUE;
694 if (rtp_mux->ssrc == -1)
695 rtp_mux->current_ssrc = g_random_int ();
697 rtp_mux->current_ssrc = rtp_mux->ssrc;
699 if (rtp_mux->seqnum_offset == -1)
700 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
702 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
703 rtp_mux->seqnum = rtp_mux->seqnum_base;
705 if (rtp_mux->ts_offset == -1)
706 rtp_mux->ts_base = g_random_int ();
708 rtp_mux->ts_base = rtp_mux->ts_offset;
710 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
712 GST_OBJECT_UNLOCK (rtp_mux);
715 static GstStateChangeReturn
716 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
720 rtp_mux = GST_RTP_MUX (element);
722 switch (transition) {
723 case GST_STATE_CHANGE_NULL_TO_READY:
725 case GST_STATE_CHANGE_READY_TO_PAUSED:
726 gst_rtp_mux_ready_to_paused (rtp_mux);
728 case GST_STATE_CHANGE_PAUSED_TO_READY:
734 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
738 gst_rtp_mux_plugin_init (GstPlugin * plugin)
740 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
742 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,