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
59 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-rtp")
65 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
68 GST_STATIC_CAPS ("application/x-rtp")
71 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
72 GstPadTemplate * templ, const gchar * name);
73 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
74 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
75 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
76 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
77 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event);
79 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
80 element, GstStateChange transition);
82 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static void gst_rtp_mux_dispose (GObject * object);
88 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
91 gst_rtp_mux_base_init (gpointer g_class)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95 gst_element_class_add_pad_template (element_class,
96 gst_static_pad_template_get (&src_factory));
97 gst_element_class_add_pad_template (element_class,
98 gst_static_pad_template_get (&sink_factory));
100 gst_element_class_set_details_simple (element_class, "RTP muxer",
102 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
106 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
108 GObjectClass *gobject_class;
109 GstElementClass *gstelement_class;
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
114 gobject_class->get_property = gst_rtp_mux_get_property;
115 gobject_class->set_property = gst_rtp_mux_set_property;
116 gobject_class->dispose = gst_rtp_mux_dispose;
118 g_object_class_install_property (G_OBJECT_CLASS (klass),
119 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
121 "Offset to add to all outgoing timestamps (-1 = random)", -1,
122 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
123 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
124 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
125 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
126 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
127 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
128 g_param_spec_uint ("seqnum", "Sequence number",
129 "The RTP sequence number of the last processed packet",
130 0, G_MAXUINT, 0, G_PARAM_READABLE));
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
132 g_param_spec_uint ("ssrc", "SSRC",
133 "The SSRC of the packets (-1 == random)",
134 0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
136 gstelement_class->request_new_pad =
137 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
138 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
139 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
141 klass->chain_func = gst_rtp_mux_chain;
145 gst_rtp_mux_dispose (GObject * object)
150 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
151 GstPad *pad = GST_PAD (item->data);
152 if (GST_PAD_IS_SINK (pad)) {
153 gst_element_release_request_pad (GST_ELEMENT (object), pad);
158 G_OBJECT_CLASS (parent_class)->dispose (object);
162 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
167 gboolean result = FALSE;
168 gboolean done = FALSE;
170 rtp_mux = gst_pad_get_parent_element (pad);
171 g_return_val_if_fail (rtp_mux != NULL, FALSE);
173 iter = gst_element_iterate_sink_pads (rtp_mux);
176 switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
177 case GST_ITERATOR_OK:
178 gst_event_ref (event);
179 result |= gst_pad_push_event (sinkpad, event);
180 gst_object_unref (sinkpad);
182 case GST_ITERATOR_RESYNC:
183 gst_iterator_resync (iter);
186 case GST_ITERATOR_ERROR:
187 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
188 case GST_ITERATOR_DONE:
193 gst_iterator_free (iter);
194 gst_object_unref (rtp_mux);
195 gst_event_unref (event);
201 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
203 GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
206 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
208 gst_pad_set_event_function (object->srcpad,
209 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
210 gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
212 object->ssrc = DEFAULT_SSRC;
213 object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
214 object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
216 object->segment_pending = TRUE;
220 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
222 GstRTPMuxClass *klass;
223 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
225 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
227 /* setup some pad functions */
228 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
229 gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
230 if (klass->chain_func)
231 gst_pad_set_chain_function (sinkpad, klass->chain_func);
232 gst_pad_set_event_function (sinkpad,
233 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
236 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
238 gst_pad_set_element_private (sinkpad, padpriv);
240 gst_pad_set_active (sinkpad, TRUE);
241 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
245 gst_rtp_mux_request_new_pad (GstElement * element,
246 GstPadTemplate * templ, const gchar * req_name)
251 g_return_val_if_fail (templ != NULL, NULL);
252 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
254 rtp_mux = GST_RTP_MUX (element);
256 if (templ->direction != GST_PAD_SINK) {
257 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
261 newpad = gst_pad_new_from_template (templ, req_name);
263 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
265 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
271 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
273 GstRTPMuxPadPrivate *padpriv;
275 GST_OBJECT_LOCK (element);
276 padpriv = gst_pad_get_element_private (pad);
277 gst_pad_set_element_private (pad, NULL);
278 GST_OBJECT_UNLOCK (element);
280 gst_element_remove_pad (element, pad);
283 gst_caps_replace (&padpriv->out_caps, NULL);
284 g_slice_free (GstRTPMuxPadPrivate, padpriv);
288 /* Put our own clock-base on the buffer */
290 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
294 guint32 sink_ts_base = 0;
295 GstRTPMuxPadPrivate *padpriv;
298 GST_OBJECT_LOCK (rtp_mux);
299 padpriv = gst_pad_get_element_private (pad);
300 if (padpriv && padpriv->have_clock_base)
301 sink_ts_base = padpriv->clock_base;
302 GST_OBJECT_UNLOCK (rtp_mux);
304 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
305 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
306 gst_rtp_buffer_get_timestamp (buffer), ts);
307 gst_rtp_buffer_set_timestamp (buffer, ts);
311 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
315 GstRTPMuxPadPrivate *padpriv;
316 GstEvent *newseg_event = NULL;
318 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
320 if (!gst_rtp_buffer_validate (buffer)) {
321 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
322 gst_object_unref (rtp_mux);
323 return GST_FLOW_ERROR;
326 buffer = gst_buffer_make_writable (buffer);
328 GST_OBJECT_LOCK (rtp_mux);
330 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
331 padpriv = gst_pad_get_element_private (pad);
333 gst_buffer_set_caps (buffer, padpriv->out_caps);
334 if (padpriv->segment.format == GST_FORMAT_TIME)
335 GST_BUFFER_TIMESTAMP (buffer) =
336 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
337 GST_BUFFER_TIMESTAMP (buffer));
340 if (rtp_mux->segment_pending) {
342 * We set the start at 0, because we re-timestamps to the running time
344 newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
345 GST_FORMAT_TIME, 0, -1, 0);
347 rtp_mux->segment_pending = FALSE;
349 GST_OBJECT_UNLOCK (rtp_mux);
351 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
352 gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
353 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
354 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
355 gst_rtp_buffer_get_timestamp (buffer));
358 gst_pad_push_event (rtp_mux->srcpad, newseg_event);
361 ret = GST_FLOW_NOT_LINKED;
362 gst_buffer_unref (buffer);
366 ret = gst_pad_push (rtp_mux->srcpad, buffer);
370 gst_object_unref (rtp_mux);
375 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
378 GstStructure *structure;
379 gboolean ret = FALSE;
380 GstRTPMuxPadPrivate *padpriv;
382 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
384 structure = gst_caps_get_structure (caps, 0);
389 GST_OBJECT_LOCK (rtp_mux);
390 padpriv = gst_pad_get_element_private (pad);
392 gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
393 padpriv->have_clock_base = TRUE;
395 GST_OBJECT_UNLOCK (rtp_mux);
397 caps = gst_caps_copy (caps);
399 gst_caps_set_simple (caps,
400 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
401 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
403 GST_DEBUG_OBJECT (rtp_mux,
404 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
405 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
407 if (rtp_mux->ssrc == -1) {
408 if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
409 rtp_mux->current_ssrc = g_value_get_uint
410 (gst_structure_get_value (structure, "ssrc"));
415 GST_OBJECT_LOCK (rtp_mux);
416 padpriv = gst_pad_get_element_private (pad);
418 gst_caps_replace (&padpriv->out_caps, caps);
419 GST_OBJECT_UNLOCK (rtp_mux);
421 gst_caps_unref (caps);
424 gst_object_unref (rtp_mux);
430 clear_caps (GstCaps * caps, gboolean only_clock_rate)
434 /* Lets only match on the clock-rate */
435 for (i = 0; i < gst_caps_get_size (caps); i++) {
436 GstStructure *s = gst_caps_get_structure (caps, i);
438 for (j = 0; j < gst_structure_n_fields (s); j++) {
439 const gchar *name = gst_structure_nth_field_name (s, j);
441 if (strcmp (name, "clock-rate") && (only_clock_rate ||
442 (strcmp (name, "ssrc")))) {
443 gst_structure_remove_field (s, name);
451 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
453 GstPad *mypad = user_data;
457 const GstCaps *accumcaps;
461 gst_object_unref (pad);
465 peercaps = gst_pad_peer_get_caps (pad);
467 gst_object_unref (pad);
471 othercaps = gst_caps_intersect (peercaps,
472 gst_pad_get_pad_template_caps (pad));
473 gst_caps_unref (peercaps);
475 accumcaps = gst_value_get_caps (ret);
477 clear_caps (othercaps, TRUE);
479 intersect = gst_caps_intersect (accumcaps, othercaps);
481 g_value_take_boxed (ret, intersect);
483 gst_caps_unref (othercaps);
484 gst_object_unref (pad);
486 return !gst_caps_is_empty (intersect);
490 gst_rtp_mux_getcaps (GstPad * pad)
492 GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
493 GstCaps *caps = NULL;
494 GstIterator *iter = NULL;
496 GstIteratorResult res;
497 GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
498 GstCaps *othercaps = NULL;
501 othercaps = gst_caps_intersect (peercaps,
502 gst_pad_get_pad_template_caps (pad));
503 gst_caps_unref (peercaps);
505 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
508 clear_caps (othercaps, FALSE);
510 g_value_init (&v, GST_TYPE_CAPS);
512 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
514 gst_value_set_caps (&v, othercaps);
515 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
516 } while (res == GST_ITERATOR_RESYNC);
517 gst_iterator_free (iter);
519 caps = (GstCaps *) gst_value_get_caps (&v);
521 if (res == GST_ITERATOR_ERROR) {
522 gst_caps_unref (caps);
523 caps = gst_caps_new_empty ();
527 gst_caps_unref (othercaps);
528 gst_object_unref (mux);
535 gst_rtp_mux_get_property (GObject * object,
536 guint prop_id, GValue * value, GParamSpec * pspec)
540 rtp_mux = GST_RTP_MUX (object);
543 case PROP_TIMESTAMP_OFFSET:
544 g_value_set_int (value, rtp_mux->ts_offset);
546 case PROP_SEQNUM_OFFSET:
547 g_value_set_int (value, rtp_mux->seqnum_offset);
550 GST_OBJECT_LOCK (rtp_mux);
551 g_value_set_uint (value, rtp_mux->seqnum);
552 GST_OBJECT_UNLOCK (rtp_mux);
555 g_value_set_uint (value, rtp_mux->ssrc);
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
564 gst_rtp_mux_set_property (GObject * object,
565 guint prop_id, const GValue * value, GParamSpec * pspec)
569 rtp_mux = GST_RTP_MUX (object);
572 case PROP_TIMESTAMP_OFFSET:
573 rtp_mux->ts_offset = g_value_get_int (value);
575 case PROP_SEQNUM_OFFSET:
576 rtp_mux->seqnum_offset = g_value_get_int (value);
579 rtp_mux->ssrc = g_value_get_uint (value);
582 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
588 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
592 gboolean ret = FALSE;
593 gboolean forward = TRUE;
595 mux = GST_RTP_MUX (gst_pad_get_parent (pad));
597 switch (GST_EVENT_TYPE (event)) {
598 case GST_EVENT_FLUSH_STOP:
600 GstRTPMuxPadPrivate *padpriv;
602 GST_OBJECT_LOCK (mux);
603 mux->segment_pending = TRUE;
604 padpriv = gst_pad_get_element_private (pad);
606 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
607 GST_OBJECT_UNLOCK (pad);
610 case GST_EVENT_NEWSEGMENT:
613 gdouble rate, applied_rate;
615 gint64 start, stop, position;
616 GstRTPMuxPadPrivate *padpriv;
618 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
619 &format, &start, &stop, &position);
621 GST_OBJECT_LOCK (mux);
622 padpriv = gst_pad_get_element_private (pad);
625 if (format == GST_FORMAT_TIME)
626 gst_segment_set_newsegment_full (&padpriv->segment, update,
627 rate, applied_rate, format, start, stop, position);
629 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
631 GST_OBJECT_UNLOCK (mux);
632 gst_event_unref (event);
642 ret = gst_pad_push_event (mux->srcpad, event);
644 gst_object_unref (mux);
650 clear_segment (gpointer data, gpointer user_data)
653 GstRTPMux *mux = user_data;
654 GstRTPMuxPadPrivate *padpriv;
656 GST_OBJECT_LOCK (mux);
657 padpriv = gst_pad_get_element_private (pad);
659 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
660 GST_OBJECT_UNLOCK (mux);
662 gst_object_unref (pad);
667 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
671 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
672 while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
673 GST_ITERATOR_RESYNC);
674 gst_iterator_free (iter);
676 GST_OBJECT_LOCK (rtp_mux);
677 rtp_mux->segment_pending = TRUE;
679 if (rtp_mux->ssrc == -1)
680 rtp_mux->current_ssrc = g_random_int ();
682 rtp_mux->current_ssrc = rtp_mux->ssrc;
684 if (rtp_mux->seqnum_offset == -1)
685 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
687 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
688 rtp_mux->seqnum = rtp_mux->seqnum_base;
690 if (rtp_mux->ts_offset == -1)
691 rtp_mux->ts_base = g_random_int ();
693 rtp_mux->ts_base = rtp_mux->ts_offset;
695 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
697 GST_OBJECT_UNLOCK (rtp_mux);
700 static GstStateChangeReturn
701 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
705 rtp_mux = GST_RTP_MUX (element);
707 switch (transition) {
708 case GST_STATE_CHANGE_NULL_TO_READY:
710 case GST_STATE_CHANGE_READY_TO_PAUSED:
711 gst_rtp_mux_ready_to_paused (rtp_mux);
713 case GST_STATE_CHANGE_PAUSED_TO_READY:
719 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
723 gst_rtp_mux_plugin_init (GstPlugin * plugin)
725 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
727 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,