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., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, 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 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.
50 * Last reviewed on 2010-09-30 (0.10.21)
58 #include <gst/rtp/gstrtpbuffer.h>
61 #include "gstrtpmux.h"
63 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
64 #define GST_CAT_DEFAULT gst_rtp_mux_debug
69 PROP_TIMESTAMP_OFFSET,
75 #define DEFAULT_TIMESTAMP_OFFSET -1
76 #define DEFAULT_SEQNUM_OFFSET -1
77 #define DEFAULT_SSRC -1
79 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS ("application/x-rtp")
85 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
88 GST_STATIC_CAPS ("application/x-rtp")
91 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
92 GstPadTemplate * templ, const gchar * name);
93 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
94 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
95 static GstFlowReturn gst_rtp_mux_chain_list (GstPad * pad,
96 GstBufferList * bufferlist);
97 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
98 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
99 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event);
101 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
102 element, GstStateChange transition);
104 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
108 static void gst_rtp_mux_dispose (GObject * object);
110 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
113 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
116 gst_rtp_mux_base_init (gpointer g_class)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&src_factory));
122 gst_element_class_add_pad_template (element_class,
123 gst_static_pad_template_get (&sink_factory));
125 gst_element_class_set_details_simple (element_class, "RTP muxer",
127 "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
131 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
133 GObjectClass *gobject_class;
134 GstElementClass *gstelement_class;
136 gobject_class = (GObjectClass *) klass;
137 gstelement_class = (GstElementClass *) klass;
139 gobject_class->get_property = gst_rtp_mux_get_property;
140 gobject_class->set_property = gst_rtp_mux_set_property;
141 gobject_class->dispose = gst_rtp_mux_dispose;
143 klass->src_event = gst_rtp_mux_src_event_real;
145 g_object_class_install_property (G_OBJECT_CLASS (klass),
146 PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
148 "Offset to add to all outgoing timestamps (-1 = random)", -1,
149 G_MAXINT, DEFAULT_TIMESTAMP_OFFSET,
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
152 g_param_spec_int ("seqnum-offset", "Sequence number Offset",
153 "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
154 DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
156 g_param_spec_uint ("seqnum", "Sequence number",
157 "The RTP sequence number of the last processed packet",
158 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
160 g_param_spec_uint ("ssrc", "SSRC",
161 "The SSRC of the packets (-1 == random)",
162 0, G_MAXUINT, DEFAULT_SSRC,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 gstelement_class->request_new_pad =
166 GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
167 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
168 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
172 gst_rtp_mux_dispose (GObject * object)
177 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
178 GstPad *pad = GST_PAD (item->data);
179 if (GST_PAD_IS_SINK (pad)) {
180 gst_element_release_request_pad (GST_ELEMENT (object), pad);
185 G_OBJECT_CLASS (parent_class)->dispose (object);
189 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
192 GstRTPMuxClass *klass;
193 gboolean ret = FALSE;
195 rtp_mux = (GstRTPMux *) gst_pad_get_parent_element (pad);
196 g_return_val_if_fail (rtp_mux != NULL, FALSE);
197 klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
199 ret = klass->src_event (rtp_mux, event);
201 gst_object_unref (rtp_mux);
207 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
211 gboolean result = FALSE;
212 gboolean done = FALSE;
214 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
217 switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
218 case GST_ITERATOR_OK:
219 gst_event_ref (event);
220 result |= gst_pad_push_event (sinkpad, event);
221 gst_object_unref (sinkpad);
223 case GST_ITERATOR_RESYNC:
224 gst_iterator_resync (iter);
227 case GST_ITERATOR_ERROR:
228 GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
229 case GST_ITERATOR_DONE:
234 gst_iterator_free (iter);
235 gst_event_unref (event);
241 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
243 GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
246 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
248 gst_pad_set_event_function (object->srcpad,
249 GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
250 gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
252 object->ssrc = DEFAULT_SSRC;
253 object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
254 object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
256 object->segment_pending = TRUE;
257 object->last_stop = GST_CLOCK_TIME_NONE;
261 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
263 GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
265 /* setup some pad functions */
266 gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
267 gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
268 gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
269 gst_pad_set_chain_list_function (sinkpad,
270 GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
271 gst_pad_set_event_function (sinkpad,
272 GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
275 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
277 gst_pad_set_element_private (sinkpad, padpriv);
279 gst_pad_set_active (sinkpad, TRUE);
280 gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
284 gst_rtp_mux_request_new_pad (GstElement * element,
285 GstPadTemplate * templ, const gchar * req_name)
290 g_return_val_if_fail (templ != NULL, NULL);
291 g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
293 rtp_mux = GST_RTP_MUX (element);
295 if (templ->direction != GST_PAD_SINK) {
296 GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
300 newpad = gst_pad_new_from_template (templ, req_name);
302 gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
304 GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
310 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
312 GstRTPMuxPadPrivate *padpriv;
314 GST_OBJECT_LOCK (element);
315 padpriv = gst_pad_get_element_private (pad);
316 gst_pad_set_element_private (pad, NULL);
317 GST_OBJECT_UNLOCK (element);
319 gst_element_remove_pad (element, pad);
322 gst_caps_replace (&padpriv->out_caps, NULL);
323 g_slice_free (GstRTPMuxPadPrivate, padpriv);
327 /* Put our own clock-base on the buffer */
329 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
330 GstRTPMuxPadPrivate * padpriv, GstBuffer * buffer)
333 guint32 sink_ts_base = 0;
335 if (padpriv && padpriv->have_clock_base)
336 sink_ts_base = padpriv->clock_base;
338 ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
339 GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
340 gst_rtp_buffer_get_timestamp (buffer), ts);
341 gst_rtp_buffer_set_timestamp (buffer, ts);
345 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
348 GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
350 if (klass->accept_buffer_locked)
351 if (!klass->accept_buffer_locked (rtp_mux, padpriv, buffer))
355 gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
357 gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
358 gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, buffer);
359 GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
360 GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
361 gst_rtp_buffer_get_timestamp (buffer));
364 gst_buffer_set_caps (buffer, padpriv->out_caps);
365 if (padpriv->segment.format == GST_FORMAT_TIME)
366 GST_BUFFER_TIMESTAMP (buffer) =
367 gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
368 GST_BUFFER_TIMESTAMP (buffer));
375 gst_rtp_mux_chain_list (GstPad * pad, GstBufferList * bufferlist)
379 GstBufferListIterator *it;
380 GstRTPMuxPadPrivate *padpriv;
381 GstEvent *newseg_event = NULL;
382 gboolean drop = TRUE;
384 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
386 if (!gst_rtp_buffer_list_validate (bufferlist)) {
387 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
388 gst_object_unref (rtp_mux);
389 return GST_FLOW_ERROR;
392 GST_OBJECT_LOCK (rtp_mux);
394 padpriv = gst_pad_get_element_private (pad);
396 GST_OBJECT_UNLOCK (rtp_mux);
397 ret = GST_FLOW_NOT_LINKED;
398 gst_buffer_list_unref (bufferlist);
402 bufferlist = gst_buffer_list_make_writable (bufferlist);
403 it = gst_buffer_list_iterate (bufferlist);
404 while (gst_buffer_list_iterator_next_group (it)) {
407 rtpbuf = gst_buffer_list_iterator_next (it);
408 rtpbuf = gst_buffer_make_writable (rtpbuf);
410 drop = !process_buffer_locked (rtp_mux, padpriv, rtpbuf);
415 gst_buffer_list_iterator_take (it, rtpbuf);
418 if (GST_BUFFER_DURATION_IS_VALID (rtpbuf) &&
419 GST_BUFFER_TIMESTAMP_IS_VALID (rtpbuf))
420 rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (rtpbuf) +
421 GST_BUFFER_DURATION (rtpbuf);
423 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
425 gst_buffer_list_iterator_take (it, rtpbuf);
427 } while ((rtpbuf = gst_buffer_list_iterator_next (it)) != NULL);
431 gst_buffer_list_iterator_free (it);
433 if (!drop && rtp_mux->segment_pending) {
435 * We set the start at 0, because we re-timestamps to the running time
437 newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
438 GST_FORMAT_TIME, 0, -1, 0);
440 rtp_mux->segment_pending = FALSE;
443 GST_OBJECT_UNLOCK (rtp_mux);
446 gst_pad_push_event (rtp_mux->srcpad, newseg_event);
449 gst_buffer_list_unref (bufferlist);
452 ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
457 gst_object_unref (rtp_mux);
463 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
467 GstRTPMuxPadPrivate *padpriv;
468 GstEvent *newseg_event = NULL;
471 rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
473 if (!gst_rtp_buffer_validate (buffer)) {
474 gst_buffer_unref (buffer);
475 GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
476 return GST_FLOW_ERROR;
479 GST_OBJECT_LOCK (rtp_mux);
480 padpriv = gst_pad_get_element_private (pad);
483 GST_OBJECT_UNLOCK (rtp_mux);
484 gst_buffer_unref (buffer);
485 return GST_FLOW_NOT_LINKED;
488 buffer = gst_buffer_make_writable (buffer);
490 drop = !process_buffer_locked (rtp_mux, padpriv, buffer);
493 if (rtp_mux->segment_pending) {
495 * We set the start at 0, because we re-timestamps to the running time
497 newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
498 GST_FORMAT_TIME, 0, -1, 0);
500 rtp_mux->segment_pending = FALSE;
503 if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
504 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
505 rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (buffer) +
506 GST_BUFFER_DURATION (buffer);
508 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
511 GST_OBJECT_UNLOCK (rtp_mux);
514 gst_pad_push_event (rtp_mux->srcpad, newseg_event);
517 gst_buffer_unref (buffer);
520 ret = gst_pad_push (rtp_mux->srcpad, buffer);
527 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
530 GstStructure *structure;
531 gboolean ret = FALSE;
532 GstRTPMuxPadPrivate *padpriv;
534 rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
536 structure = gst_caps_get_structure (caps, 0);
541 GST_OBJECT_LOCK (rtp_mux);
542 padpriv = gst_pad_get_element_private (pad);
544 gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
545 padpriv->have_clock_base = TRUE;
547 GST_OBJECT_UNLOCK (rtp_mux);
549 caps = gst_caps_copy (caps);
551 gst_caps_set_simple (caps,
552 "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
553 "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
555 GST_DEBUG_OBJECT (rtp_mux,
556 "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
557 ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
559 if (rtp_mux->ssrc == -1) {
560 if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
561 rtp_mux->current_ssrc = g_value_get_uint
562 (gst_structure_get_value (structure, "ssrc"));
567 GST_OBJECT_LOCK (rtp_mux);
568 padpriv = gst_pad_get_element_private (pad);
570 gst_caps_replace (&padpriv->out_caps, caps);
571 GST_OBJECT_UNLOCK (rtp_mux);
573 gst_caps_unref (caps);
576 gst_object_unref (rtp_mux);
582 clear_caps (GstCaps * caps, gboolean only_clock_rate)
586 /* Lets only match on the clock-rate */
587 for (i = 0; i < gst_caps_get_size (caps); i++) {
588 GstStructure *s = gst_caps_get_structure (caps, i);
590 for (j = 0; j < gst_structure_n_fields (s); j++) {
591 const gchar *name = gst_structure_nth_field_name (s, j);
593 if (strcmp (name, "clock-rate") && (only_clock_rate ||
594 (strcmp (name, "ssrc")))) {
595 gst_structure_remove_field (s, name);
603 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
605 GstPad *mypad = user_data;
609 const GstCaps *accumcaps;
613 gst_object_unref (pad);
617 peercaps = gst_pad_peer_get_caps (pad);
619 gst_object_unref (pad);
623 othercaps = gst_caps_intersect (peercaps,
624 gst_pad_get_pad_template_caps (pad));
625 gst_caps_unref (peercaps);
627 accumcaps = gst_value_get_caps (ret);
629 clear_caps (othercaps, TRUE);
631 intersect = gst_caps_intersect (accumcaps, othercaps);
633 g_value_take_boxed (ret, intersect);
635 gst_caps_unref (othercaps);
636 gst_object_unref (pad);
638 return !gst_caps_is_empty (intersect);
642 gst_rtp_mux_getcaps (GstPad * pad)
644 GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
645 GstCaps *caps = NULL;
646 GstIterator *iter = NULL;
648 GstIteratorResult res;
649 GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
650 GstCaps *othercaps = NULL;
653 othercaps = gst_caps_intersect (peercaps,
654 gst_pad_get_pad_template_caps (pad));
655 gst_caps_unref (peercaps);
657 othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
660 clear_caps (othercaps, FALSE);
662 g_value_init (&v, GST_TYPE_CAPS);
664 iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
666 gst_value_set_caps (&v, othercaps);
667 res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
668 } while (res == GST_ITERATOR_RESYNC);
669 gst_iterator_free (iter);
671 caps = (GstCaps *) gst_value_get_caps (&v);
673 if (res == GST_ITERATOR_ERROR) {
674 gst_caps_unref (caps);
675 caps = gst_caps_new_empty ();
679 gst_caps_unref (othercaps);
680 gst_object_unref (mux);
687 gst_rtp_mux_get_property (GObject * object,
688 guint prop_id, GValue * value, GParamSpec * pspec)
692 rtp_mux = GST_RTP_MUX (object);
695 case PROP_TIMESTAMP_OFFSET:
696 g_value_set_int (value, rtp_mux->ts_offset);
698 case PROP_SEQNUM_OFFSET:
699 g_value_set_int (value, rtp_mux->seqnum_offset);
702 GST_OBJECT_LOCK (rtp_mux);
703 g_value_set_uint (value, rtp_mux->seqnum);
704 GST_OBJECT_UNLOCK (rtp_mux);
707 g_value_set_uint (value, rtp_mux->ssrc);
710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
716 gst_rtp_mux_set_property (GObject * object,
717 guint prop_id, const GValue * value, GParamSpec * pspec)
721 rtp_mux = GST_RTP_MUX (object);
724 case PROP_TIMESTAMP_OFFSET:
725 rtp_mux->ts_offset = g_value_get_int (value);
727 case PROP_SEQNUM_OFFSET:
728 rtp_mux->seqnum_offset = g_value_get_int (value);
731 rtp_mux->ssrc = g_value_get_uint (value);
734 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
740 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
744 gboolean ret = FALSE;
745 gboolean forward = TRUE;
747 mux = GST_RTP_MUX (gst_pad_get_parent (pad));
749 switch (GST_EVENT_TYPE (event)) {
750 case GST_EVENT_FLUSH_STOP:
752 GstRTPMuxPadPrivate *padpriv;
754 GST_OBJECT_LOCK (mux);
755 mux->last_stop = GST_CLOCK_TIME_NONE;
756 mux->segment_pending = TRUE;
757 padpriv = gst_pad_get_element_private (pad);
759 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
760 GST_OBJECT_UNLOCK (mux);
763 case GST_EVENT_NEWSEGMENT:
766 gdouble rate, applied_rate;
768 gint64 start, stop, position;
769 GstRTPMuxPadPrivate *padpriv;
771 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
772 &format, &start, &stop, &position);
774 GST_OBJECT_LOCK (mux);
775 padpriv = gst_pad_get_element_private (pad);
778 if (format == GST_FORMAT_TIME)
779 gst_segment_set_newsegment_full (&padpriv->segment, update,
780 rate, applied_rate, format, start, stop, position);
782 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
784 GST_OBJECT_UNLOCK (mux);
785 gst_event_unref (event);
795 ret = gst_pad_push_event (mux->srcpad, event);
797 gst_object_unref (mux);
803 clear_segment (gpointer data, gpointer user_data)
806 GstRTPMux *mux = user_data;
807 GstRTPMuxPadPrivate *padpriv;
809 GST_OBJECT_LOCK (mux);
810 padpriv = gst_pad_get_element_private (pad);
812 gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
813 GST_OBJECT_UNLOCK (mux);
815 gst_object_unref (pad);
820 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
824 iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
825 while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
826 GST_ITERATOR_RESYNC);
827 gst_iterator_free (iter);
829 GST_OBJECT_LOCK (rtp_mux);
830 rtp_mux->segment_pending = TRUE;
832 if (rtp_mux->ssrc == -1)
833 rtp_mux->current_ssrc = g_random_int ();
835 rtp_mux->current_ssrc = rtp_mux->ssrc;
837 if (rtp_mux->seqnum_offset == -1)
838 rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
840 rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
841 rtp_mux->seqnum = rtp_mux->seqnum_base;
843 if (rtp_mux->ts_offset == -1)
844 rtp_mux->ts_base = g_random_int ();
846 rtp_mux->ts_base = rtp_mux->ts_offset;
848 rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
850 GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
852 GST_OBJECT_UNLOCK (rtp_mux);
855 static GstStateChangeReturn
856 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
860 rtp_mux = GST_RTP_MUX (element);
862 switch (transition) {
863 case GST_STATE_CHANGE_NULL_TO_READY:
865 case GST_STATE_CHANGE_READY_TO_PAUSED:
866 gst_rtp_mux_ready_to_paused (rtp_mux);
868 case GST_STATE_CHANGE_PAUSED_TO_READY:
874 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
878 gst_rtp_mux_plugin_init (GstPlugin * plugin)
880 GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
882 return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,