1 /* RTP muxer element for GStreamer
5 * Copyright (C) <2007-2010> Nokia Corporation.
6 * Contact: Zeeshan Ali <zeeshan.ali@nokia.com>
7 * Copyright (C) <2007-2010> Collabora Ltd
8 * Contact: Olivier Crete <olivier.crete@collabora.co.uk>
9 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
10 * 2000,2005 Wim Taymans <wim@fluendo.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
29 * SECTION:element-rtpmux
30 * @see_also: rtpdtmfmux
32 * The rtp muxer takes multiple RTP streams having the same clock-rate and
33 * muxes into a single stream with a single SSRC.
36 * <title>Example pipelines</title>
38 * gst-launch-1.0 rtpmux name=mux ! udpsink host=127.0.0.1 port=8888 \
39 * alsasrc ! alawenc ! rtppcmapay ! \
40 * application/x-rtp, payload=8, rate=8000 ! mux.sink_0 \
41 * audiotestsrc is-live=1 ! \
42 * mulawenc ! rtppcmupay ! \
43 * application/x-rtp, payload=0, rate=8000 ! mux.sink_1
45 * In this example, an audio stream is captured from ALSA and another is
46 * generated, both are encoded into different payload types and muxed together
47 * so they can be sent on the same port.
56 #include <gst/rtp/gstrtpbuffer.h>
59 #include "gstrtpmux.h"
61 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
62 #define GST_CAT_DEFAULT gst_rtp_mux_debug
67 PROP_TIMESTAMP_OFFSET,
73 #define DEFAULT_TIMESTAMP_OFFSET -1
74 #define DEFAULT_SEQNUM_OFFSET -1
75 #define DEFAULT_SSRC -1
77 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
80 GST_STATIC_CAPS ("application/x-rtp")
83 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
86 GST_STATIC_CAPS ("application/x-rtp")
89 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
90 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
91 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
92 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstObject * parent,
94 static GstFlowReturn gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
95 GstBufferList * bufferlist);
96 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux,
98 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent,
100 static gboolean gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent,
103 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
104 element, GstStateChange transition);
106 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
107 const GValue * value, GParamSpec * pspec);
108 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
110 static void gst_rtp_mux_dispose (GObject * object);
112 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
115 G_DEFINE_TYPE (GstRTPMux, gst_rtp_mux, GST_TYPE_ELEMENT);
119 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
121 GObjectClass *gobject_class;
122 GstElementClass *gstelement_class;
124 gobject_class = (GObjectClass *) klass;
125 gstelement_class = (GstElementClass *) klass;
128 gst_element_class_add_pad_template (gstelement_class,
129 gst_static_pad_template_get (&src_factory));
130 gst_element_class_add_pad_template (gstelement_class,
131 gst_static_pad_template_get (&sink_factory));
133 gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
135 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
137 gobject_class->get_property = gst_rtp_mux_get_property;
138 gobject_class->set_property = gst_rtp_mux_set_property;
139 gobject_class->dispose = gst_rtp_mux_dispose;
141 klass->src_event = gst_rtp_mux_src_event_real;
143 g_object_class_install_property (G_OBJECT_CLASS (klass),
144 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
146 "Offset to add to all outgoing timestamps (-1 = random)", -1,
147 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET,
148 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
150 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
151 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
152 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
154 g_param_spec_uint ("seqnum", "Sequence number",
155 "The RTP sequence number of the last processed packet",
156 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
158 g_param_spec_uint ("ssrc", "SSRC",
159 "The SSRC of the packets (-1 == random)",
160 0, G_MAXUINT, DEFAULT_SSRC,
161 GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
162 G_PARAM_STATIC_STRINGS));
164 gstelement_class->request_new_pad =
165 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
166 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
167 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
171 gst_rtp_mux_dispose (GObject * object)
173 GstRTPMux *rtp_mux = GST_RTP_MUX (object);
176 g_clear_object (&rtp_mux->last_pad);
179 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
180 GstPad *pad = GST_PAD (item->data);
181 if (GST_PAD_IS_SINK (pad)) {
182 gst_element_release_request_pad (GST_ELEMENT (object), pad);
187 G_OBJECT_CLASS (gst_rtp_mux_parent_class)->dispose (object);
191 gst_rtp_mux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
193 GstRTPMux *rtp_mux = GST_RTP_MUX (parent);
194 GstRTPMuxClass *klass;
197 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
199 ret = klass->src_event (rtp_mux, event);
205 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
208 gboolean result = FALSE;
209 gboolean done = FALSE;
211 switch (GST_EVENT_TYPE (event)) {
212 case GST_EVENT_CUSTOM_UPSTREAM:
214 const GstStructure *s = gst_event_get_structure (event);
216 if (gst_structure_has_name (s, "GstRTPCollision")) {
219 if (!gst_structure_get_uint (s, "ssrc", &ssrc))
222 GST_DEBUG_OBJECT (rtp_mux, "collided ssrc: %" G_GUINT32_FORMAT, ssrc);
224 /* choose another ssrc for our stream */
225 GST_OBJECT_LOCK (rtp_mux);
226 if (ssrc == rtp_mux->current_ssrc) {
228 guint suggested_ssrc = 0;
231 if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
232 rtp_mux->current_ssrc = suggested_ssrc;
234 while (ssrc == rtp_mux->current_ssrc)
235 rtp_mux->current_ssrc = g_random_int ();
237 new_ssrc = rtp_mux->current_ssrc;
238 GST_OBJECT_UNLOCK (rtp_mux);
240 caps = gst_pad_get_current_caps (rtp_mux->srcpad);
241 caps = gst_caps_make_writable (caps);
242 gst_caps_set_simple (caps, "ssrc", G_TYPE_UINT, new_ssrc, NULL);
243 gst_pad_set_caps (rtp_mux->srcpad, caps);
244 gst_caps_unref (caps);
246 GST_OBJECT_UNLOCK (rtp_mux);
256 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
259 GValue item = { 0, };
261 switch (gst_iterator_next (iter, &item)) {
262 case GST_ITERATOR_OK:
263 gst_event_ref (event);
264 result |= gst_pad_push_event (g_value_get_object (&item), event);
265 g_value_reset (&item);
267 case GST_ITERATOR_RESYNC:
268 gst_iterator_resync (iter);
271 case GST_ITERATOR_ERROR:
272 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
273 case GST_ITERATOR_DONE:
278 gst_iterator_free (iter);
279 gst_event_unref (event);
285 gst_rtp_mux_init (GstRTPMux * rtp_mux)
287 GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
290 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
292 gst_pad_set_event_function (rtp_mux->srcpad,
293 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
294 gst_pad_use_fixed_caps (rtp_mux->srcpad);
295 gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
297 rtp_mux->ssrc = DEFAULT_SSRC;
298 rtp_mux->current_ssrc = DEFAULT_SSRC;
299 rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
300 rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
302 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
306 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
308 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
310 /* setup some pad functions */
311 gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
312 gst_pad_set_chain_list_function (sinkpad,
313 GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
314 gst_pad_set_event_function (sinkpad,
315 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
316 gst_pad_set_query_function (sinkpad,
317 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
320 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
322 gst_pad_set_element_private (sinkpad, padpriv);
324 gst_pad_set_active (sinkpad, TRUE);
325 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
329 gst_rtp_mux_request_new_pad (GstElement * element,
330 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
335 g_return_val_if_fail (templ != NULL, NULL);
336 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
338 rtp_mux = GST_RTP_MUX (element);
340 if (templ->direction != GST_PAD_SINK) {
341 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
345 newpad = gst_pad_new_from_template (templ, req_name);
347 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
349 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
355 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
357 GstRTPMuxPadPrivate *padpriv;
359 GST_OBJECT_LOCK (element);
360 padpriv = gst_pad_get_element_private (pad);
361 gst_pad_set_element_private (pad, NULL);
362 GST_OBJECT_UNLOCK (element);
364 gst_element_remove_pad (element, pad);
367 g_slice_free (GstRTPMuxPadPrivate, padpriv);
371 /* Put our own timestamp-offset on the buffer */
373 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
374 GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
377 guint32 sink_ts_base = 0;
379 if (padpriv && padpriv->have_timestamp_offset)
380 sink_ts_base = padpriv->timestamp_offset;
382 ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
384 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
385 gst_rtp_buffer_get_timestamp (rtpbuffer), ts);
386 gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
390 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
391 GstRTPBuffer * rtpbuffer)
393 GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
395 if (klass->accept_buffer_locked)
396 if (!klass->accept_buffer_locked (rtp_mux, padpriv, rtpbuffer))
400 gst_rtp_buffer_set_seq (rtpbuffer, rtp_mux->seqnum);
402 gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
403 gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
404 GST_LOG_OBJECT (rtp_mux,
405 "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u",
406 rtpbuffer->map[0].size, rtp_mux->seqnum,
407 gst_rtp_buffer_get_timestamp (rtpbuffer));
410 if (padpriv->segment.format == GST_FORMAT_TIME)
411 GST_BUFFER_PTS (rtpbuffer->buffer) =
412 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
413 GST_BUFFER_PTS (rtpbuffer->buffer));
419 struct BufferListData
422 GstRTPMuxPadPrivate *padpriv;
427 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
429 struct BufferListData *bd = user_data;
430 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
432 *buffer = gst_buffer_make_writable (*buffer);
434 gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
436 bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
438 gst_rtp_buffer_unmap (&rtpbuffer);
443 if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
444 GST_BUFFER_PTS_IS_VALID (*buffer))
445 bd->rtp_mux->last_stop = GST_BUFFER_PTS (*buffer) +
446 GST_BUFFER_DURATION (*buffer);
448 bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
454 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
455 GstBufferList * bufferlist)
459 GstRTPMuxPadPrivate *padpriv;
460 struct BufferListData bd;
462 rtp_mux = GST_RTP_MUX (parent);
464 if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
465 GstCaps *current_caps = gst_pad_get_current_caps (pad);
467 if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
468 ret = GST_FLOW_NOT_NEGOTIATED;
469 gst_buffer_list_unref (bufferlist);
472 gst_caps_unref (current_caps);
475 GST_OBJECT_LOCK (rtp_mux);
477 padpriv = gst_pad_get_element_private (pad);
479 GST_OBJECT_UNLOCK (rtp_mux);
480 ret = GST_FLOW_NOT_LINKED;
481 gst_buffer_list_unref (bufferlist);
485 bd.rtp_mux = rtp_mux;
486 bd.padpriv = padpriv;
489 bufferlist = gst_buffer_list_make_writable (bufferlist);
490 gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
492 GST_OBJECT_UNLOCK (rtp_mux);
495 gst_buffer_list_unref (bufferlist);
498 ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
507 resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
509 GstRTPMux *rtp_mux = user_data;
511 if (GST_EVENT_TYPE (*event) == GST_EVENT_CAPS) {
514 gst_event_parse_caps (*event, &caps);
515 gst_rtp_mux_setcaps (pad, rtp_mux, caps);
517 gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
524 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
528 GstRTPMuxPadPrivate *padpriv;
530 gboolean changed = FALSE;
531 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
533 rtp_mux = GST_RTP_MUX (parent);
535 if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
536 GstCaps *current_caps = gst_pad_get_current_caps (pad);
538 if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
539 ret = GST_FLOW_NOT_NEGOTIATED;
540 gst_buffer_unref (buffer);
543 gst_caps_unref (current_caps);
546 GST_OBJECT_LOCK (rtp_mux);
547 padpriv = gst_pad_get_element_private (pad);
550 GST_OBJECT_UNLOCK (rtp_mux);
551 gst_buffer_unref (buffer);
552 return GST_FLOW_NOT_LINKED;
555 buffer = gst_buffer_make_writable (buffer);
557 if (!gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer)) {
558 GST_OBJECT_UNLOCK (rtp_mux);
559 gst_buffer_unref (buffer);
560 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
561 return GST_FLOW_ERROR;
564 drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
566 gst_rtp_buffer_unmap (&rtpbuffer);
569 if (pad != rtp_mux->last_pad) {
571 g_clear_object (&rtp_mux->last_pad);
572 rtp_mux->last_pad = g_object_ref (pad);
575 if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
576 GST_BUFFER_PTS_IS_VALID (buffer))
577 rtp_mux->last_stop = GST_BUFFER_PTS (buffer) +
578 GST_BUFFER_DURATION (buffer);
580 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
583 GST_OBJECT_UNLOCK (rtp_mux);
586 gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
589 gst_buffer_unref (buffer);
592 ret = gst_pad_push (rtp_mux->srcpad, buffer);
600 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
602 GstStructure *structure;
603 gboolean ret = FALSE;
604 GstRTPMuxPadPrivate *padpriv;
607 if (!gst_caps_is_fixed (caps))
610 peercaps = gst_pad_peer_query_caps (rtp_mux->srcpad, NULL);
612 GstCaps *tcaps, *othercaps;;
613 tcaps = gst_pad_get_pad_template_caps (pad);
614 othercaps = gst_caps_intersect_full (peercaps, tcaps,
615 GST_CAPS_INTERSECT_FIRST);
617 if (gst_caps_get_size (othercaps) > 0) {
618 structure = gst_caps_get_structure (othercaps, 0);
619 GST_OBJECT_LOCK (rtp_mux);
620 if (gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
621 GST_DEBUG_OBJECT (pad, "Use downstream ssrc: %x",
622 rtp_mux->current_ssrc);
623 rtp_mux->have_ssrc = TRUE;
625 GST_OBJECT_UNLOCK (rtp_mux);
628 gst_caps_unref (othercaps);
630 gst_caps_unref (peercaps);
631 gst_caps_unref (tcaps);
634 structure = gst_caps_get_structure (caps, 0);
639 GST_OBJECT_LOCK (rtp_mux);
640 padpriv = gst_pad_get_element_private (pad);
642 gst_structure_get_uint (structure, "timestamp-offset",
643 &padpriv->timestamp_offset)) {
644 padpriv->have_timestamp_offset = TRUE;
647 caps = gst_caps_copy (caps);
649 /* if we don't have a specified ssrc, first try to take one from the caps,
650 and if that fails, generate one */
651 if (!rtp_mux->have_ssrc) {
652 if (rtp_mux->ssrc == DEFAULT_SSRC) {
653 if (!gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc))
654 rtp_mux->current_ssrc = g_random_int ();
655 rtp_mux->have_ssrc = TRUE;
659 gst_caps_set_simple (caps,
660 "timestamp-offset", G_TYPE_UINT, rtp_mux->ts_base,
661 "seqnum-offset", G_TYPE_UINT, rtp_mux->seqnum_base,
662 "ssrc", G_TYPE_UINT, rtp_mux->current_ssrc, NULL);
664 GST_OBJECT_UNLOCK (rtp_mux);
666 if (rtp_mux->send_stream_start) {
669 /* stream-start (FIXME: create id based on input ids) */
670 g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
671 gst_pad_push_event (rtp_mux->srcpad, gst_event_new_stream_start (s_id));
673 rtp_mux->send_stream_start = FALSE;
676 GST_DEBUG_OBJECT (rtp_mux,
677 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
678 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
681 gst_caps_unref (caps);
687 clear_caps (GstCaps * caps, gboolean only_clock_rate)
691 /* Lets only match on the clock-rate */
692 for (i = 0; i < gst_caps_get_size (caps); i++) {
693 GstStructure *s = gst_caps_get_structure (caps, i);
695 for (j = 0; j < gst_structure_n_fields (s); j++) {
696 const gchar *name = gst_structure_nth_field_name (s, j);
698 if (strcmp (name, "clock-rate") && (only_clock_rate ||
699 (strcmp (name, "ssrc")))) {
700 gst_structure_remove_field (s, name);
708 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
710 GstPad *mypad = user_data;
711 GstPad *pad = g_value_get_object (item);
718 accumcaps = g_value_get_boxed (ret);
719 peercaps = gst_pad_peer_query_caps (pad, accumcaps);
721 g_warning ("no peercaps");
724 peercaps = gst_caps_make_writable (peercaps);
725 clear_caps (peercaps, TRUE);
727 g_value_take_boxed (ret, peercaps);
729 return !gst_caps_is_empty (peercaps);
733 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
735 GstCaps *caps = NULL;
736 GstIterator *iter = NULL;
738 GstIteratorResult res;
743 peercaps = gst_pad_peer_query_caps (mux->srcpad, NULL);
746 tcaps = gst_pad_get_pad_template_caps (pad);
747 othercaps = gst_caps_intersect_full (peercaps, tcaps,
748 GST_CAPS_INTERSECT_FIRST);
749 gst_caps_unref (peercaps);
751 tcaps = gst_pad_get_pad_template_caps (mux->srcpad);
753 othercaps = gst_caps_intersect_full (filter, tcaps,
754 GST_CAPS_INTERSECT_FIRST);
756 othercaps = gst_caps_copy (tcaps);
758 gst_caps_unref (tcaps);
760 GST_LOG_OBJECT (pad, "Intersected srcpad-peercaps and template caps: %"
761 GST_PTR_FORMAT, othercaps);
763 clear_caps (othercaps, TRUE);
765 g_value_init (&v, GST_TYPE_CAPS);
767 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
769 gst_value_set_caps (&v, othercaps);
770 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
771 gst_iterator_resync (iter);
772 } while (res == GST_ITERATOR_RESYNC);
773 gst_iterator_free (iter);
775 caps = gst_caps_intersect ((GstCaps *) gst_value_get_caps (&v), othercaps);
778 gst_caps_unref (othercaps);
780 if (res == GST_ITERATOR_ERROR) {
781 gst_caps_unref (caps);
782 caps = gst_caps_new_empty ();
790 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
792 GstRTPMux *mux = GST_RTP_MUX (parent);
793 gboolean res = FALSE;
795 switch (GST_QUERY_TYPE (query)) {
798 GstCaps *filter, *caps;
800 gst_query_parse_caps (query, &filter);
801 GST_LOG_OBJECT (pad, "Received caps-query with filter-caps: %"
802 GST_PTR_FORMAT, filter);
803 caps = gst_rtp_mux_getcaps (pad, mux, filter);
804 gst_query_set_caps_result (query, caps);
805 GST_LOG_OBJECT (mux, "Answering caps-query with caps: %"
806 GST_PTR_FORMAT, caps);
807 gst_caps_unref (caps);
812 res = gst_pad_query_default (pad, parent, query);
820 gst_rtp_mux_get_property (GObject * object,
821 guint prop_id, GValue * value, GParamSpec * pspec)
825 rtp_mux = GST_RTP_MUX (object);
827 GST_OBJECT_LOCK (rtp_mux);
829 case PROP_TIMESTAMP_OFFSET:
830 g_value_set_int (value, rtp_mux->ts_offset);
832 case PROP_SEQNUM_OFFSET:
833 g_value_set_int (value, rtp_mux->seqnum_offset);
836 g_value_set_uint (value, rtp_mux->seqnum);
839 g_value_set_uint (value, rtp_mux->ssrc);
842 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
845 GST_OBJECT_UNLOCK (rtp_mux);
849 gst_rtp_mux_set_property (GObject * object,
850 guint prop_id, const GValue * value, GParamSpec * pspec)
854 rtp_mux = GST_RTP_MUX (object);
857 case PROP_TIMESTAMP_OFFSET:
858 rtp_mux->ts_offset = g_value_get_int (value);
860 case PROP_SEQNUM_OFFSET:
861 rtp_mux->seqnum_offset = g_value_get_int (value);
864 GST_OBJECT_LOCK (rtp_mux);
865 rtp_mux->ssrc = g_value_get_uint (value);
866 rtp_mux->current_ssrc = rtp_mux->ssrc;
867 rtp_mux->have_ssrc = TRUE;
868 GST_OBJECT_UNLOCK (rtp_mux);
871 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
877 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
879 GstRTPMux *mux = GST_RTP_MUX (parent);
883 switch (GST_EVENT_TYPE (event)) {
888 gst_event_parse_caps (event, &caps);
889 GST_LOG_OBJECT (pad, "Received caps-event with caps: %"
890 GST_PTR_FORMAT, caps);
891 ret = gst_rtp_mux_setcaps (pad, mux, caps);
892 gst_event_unref (event);
895 case GST_EVENT_FLUSH_STOP:
897 GST_OBJECT_LOCK (mux);
898 mux->last_stop = GST_CLOCK_TIME_NONE;
899 GST_OBJECT_UNLOCK (mux);
902 case GST_EVENT_SEGMENT:
904 GstRTPMuxPadPrivate *padpriv;
906 GST_OBJECT_LOCK (mux);
907 padpriv = gst_pad_get_element_private (pad);
910 gst_event_copy_segment (event, &padpriv->segment);
912 GST_OBJECT_UNLOCK (mux);
919 GST_OBJECT_LOCK (mux);
920 is_pad = (pad == mux->last_pad);
921 GST_OBJECT_UNLOCK (mux);
924 return gst_pad_push_event (mux->srcpad, event);
926 gst_event_unref (event);
932 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
935 GST_OBJECT_LOCK (rtp_mux);
937 g_clear_object (&rtp_mux->last_pad);
938 rtp_mux->send_stream_start = TRUE;
940 if (rtp_mux->seqnum_offset == -1)
941 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
943 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
944 rtp_mux->seqnum = rtp_mux->seqnum_base;
946 if (rtp_mux->ts_offset == -1)
947 rtp_mux->ts_base = g_random_int ();
949 rtp_mux->ts_base = rtp_mux->ts_offset;
951 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
953 if (rtp_mux->ssrc == DEFAULT_SSRC) {
954 rtp_mux->have_ssrc = FALSE;
956 rtp_mux->current_ssrc = rtp_mux->ssrc;
957 rtp_mux->have_ssrc = TRUE;
960 GST_DEBUG_OBJECT (rtp_mux, "set timestamp-offset to %u", rtp_mux->ts_base);
962 GST_OBJECT_UNLOCK (rtp_mux);
965 static GstStateChangeReturn
966 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
969 GstStateChangeReturn ret;
971 rtp_mux = GST_RTP_MUX (element);
973 switch (transition) {
974 case GST_STATE_CHANGE_READY_TO_PAUSED:
975 gst_rtp_mux_ready_to_paused (rtp_mux);
981 ret = GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
984 switch (transition) {
985 case GST_STATE_CHANGE_PAUSED_TO_READY:
986 g_clear_object (&rtp_mux->last_pad);
996 gst_rtp_mux_plugin_init (GstPlugin * plugin)
998 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
1000 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,