rtpmux: make request pads take _%u
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpmux.c
1 /* RTP muxer element for GStreamer
2  *
3  * gstrtpmux.c:
4  *
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>
11  *
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.
16  *
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.
21  *
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.
26  */
27
28 /**
29  * SECTION:element-rtpmux
30  * @see_also: rtpdtmfmux
31  *
32  * The rtp muxer takes multiple RTP streams having the same clock-rate and
33  * muxes into a single stream with a single SSRC.
34  *
35  * <refsect2>
36  * <title>Example pipelines</title>
37  * |[
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
44  * ]|
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.
48  * </refsect2>
49  *
50  * Last reviewed on 2010-09-30 (0.10.21)
51  */
52
53 #ifdef HAVE_CONFIG_H
54 #include "config.h"
55 #endif
56
57 #include <gst/gst.h>
58 #include <gst/rtp/gstrtpbuffer.h>
59 #include <string.h>
60
61 #include "gstrtpmux.h"
62
63 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
64 #define GST_CAT_DEFAULT gst_rtp_mux_debug
65
66 enum
67 {
68   ARG_0,
69   PROP_TIMESTAMP_OFFSET,
70   PROP_SEQNUM_OFFSET,
71   PROP_SEQNUM,
72   PROP_SSRC
73 };
74
75 #define DEFAULT_TIMESTAMP_OFFSET -1
76 #define DEFAULT_SEQNUM_OFFSET    -1
77 #define DEFAULT_SSRC             -1
78
79 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
80     GST_PAD_SRC,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS ("application/x-rtp")
83     );
84
85 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
86     GST_PAD_SINK,
87     GST_PAD_REQUEST,
88     GST_STATIC_CAPS ("application/x-rtp")
89     );
90
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);
100
101 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
102     element, GstStateChange transition);
103
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);
109
110 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
111     GstEvent * event);
112
113 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
114
115 static void
116 gst_rtp_mux_base_init (gpointer g_class)
117 {
118   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
119
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));
124
125   gst_element_class_set_details_simple (element_class, "RTP muxer",
126       "Codec/Muxer",
127       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
128 }
129
130 static void
131 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
132 {
133   GObjectClass *gobject_class;
134   GstElementClass *gstelement_class;
135
136   gobject_class = (GObjectClass *) klass;
137   gstelement_class = (GstElementClass *) klass;
138
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;
142
143   klass->src_event = gst_rtp_mux_src_event_real;
144
145   g_object_class_install_property (G_OBJECT_CLASS (klass),
146       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
147           "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));
164
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);
169 }
170
171 static void
172 gst_rtp_mux_dispose (GObject * object)
173 {
174   GList *item;
175
176 restart:
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);
181       goto restart;
182     }
183   }
184
185   G_OBJECT_CLASS (parent_class)->dispose (object);
186 }
187
188 static gboolean
189 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
190 {
191   GstRTPMux *rtp_mux;
192   GstRTPMuxClass *klass;
193   gboolean ret = FALSE;
194
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);
198
199   ret = klass->src_event (rtp_mux, event);
200
201   gst_object_unref (rtp_mux);
202
203   return ret;
204 }
205
206 static gboolean
207 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
208 {
209   GstIterator *iter;
210   GstPad *sinkpad;
211   gboolean result = FALSE;
212   gboolean done = FALSE;
213
214   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
215
216   while (!done) {
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);
222         break;
223       case GST_ITERATOR_RESYNC:
224         gst_iterator_resync (iter);
225         result = FALSE;
226         break;
227       case GST_ITERATOR_ERROR:
228         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
229       case GST_ITERATOR_DONE:
230         done = TRUE;
231         break;
232     }
233   }
234   gst_iterator_free (iter);
235   gst_event_unref (event);
236
237   return result;
238 }
239
240 static void
241 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
242 {
243   GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
244
245   object->srcpad =
246       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
247           "src"), "src");
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);
251
252   object->ssrc = DEFAULT_SSRC;
253   object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
254   object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
255
256   object->segment_pending = TRUE;
257   object->last_stop = GST_CLOCK_TIME_NONE;
258 }
259
260 static void
261 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
262 {
263   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
264
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));
273
274
275   gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
276
277   gst_pad_set_element_private (sinkpad, padpriv);
278
279   gst_pad_set_active (sinkpad, TRUE);
280   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
281 }
282
283 static GstPad *
284 gst_rtp_mux_request_new_pad (GstElement * element,
285     GstPadTemplate * templ, const gchar * req_name)
286 {
287   GstRTPMux *rtp_mux;
288   GstPad *newpad;
289
290   g_return_val_if_fail (templ != NULL, NULL);
291   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
292
293   rtp_mux = GST_RTP_MUX (element);
294
295   if (templ->direction != GST_PAD_SINK) {
296     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
297     return NULL;
298   }
299
300   newpad = gst_pad_new_from_template (templ, req_name);
301   if (newpad)
302     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
303   else
304     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
305
306   return newpad;
307 }
308
309 static void
310 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
311 {
312   GstRTPMuxPadPrivate *padpriv;
313
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);
318
319   gst_element_remove_pad (element, pad);
320
321   if (padpriv) {
322     gst_caps_replace (&padpriv->out_caps, NULL);
323     g_slice_free (GstRTPMuxPadPrivate, padpriv);
324   }
325 }
326
327 /* Put our own clock-base on the buffer */
328 static void
329 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
330     GstRTPMuxPadPrivate * padpriv, GstBuffer * buffer)
331 {
332   guint32 ts;
333   guint32 sink_ts_base = 0;
334
335   if (padpriv && padpriv->have_clock_base)
336     sink_ts_base = padpriv->clock_base;
337
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);
342 }
343
344 static gboolean
345 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
346     GstBuffer * buffer)
347 {
348   GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
349
350   if (klass->accept_buffer_locked)
351     if (!klass->accept_buffer_locked (rtp_mux, padpriv, buffer))
352       return FALSE;
353
354   rtp_mux->seqnum++;
355   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
356
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));
362
363   if (padpriv) {
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));
369   }
370
371   return TRUE;
372 }
373
374 static GstFlowReturn
375 gst_rtp_mux_chain_list (GstPad * pad, GstBufferList * bufferlist)
376 {
377   GstRTPMux *rtp_mux;
378   GstFlowReturn ret;
379   GstBufferListIterator *it;
380   GstRTPMuxPadPrivate *padpriv;
381   GstEvent *newseg_event = NULL;
382   gboolean drop = TRUE;
383
384   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
385
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;
390   }
391
392   GST_OBJECT_LOCK (rtp_mux);
393
394   padpriv = gst_pad_get_element_private (pad);
395   if (!padpriv) {
396     GST_OBJECT_UNLOCK (rtp_mux);
397     ret = GST_FLOW_NOT_LINKED;
398     gst_buffer_list_unref (bufferlist);
399     goto out;
400   }
401
402   bufferlist = gst_buffer_list_make_writable (bufferlist);
403   it = gst_buffer_list_iterate (bufferlist);
404   while (gst_buffer_list_iterator_next_group (it)) {
405     GstBuffer *rtpbuf;
406
407     rtpbuf = gst_buffer_list_iterator_next (it);
408     rtpbuf = gst_buffer_make_writable (rtpbuf);
409
410     drop = !process_buffer_locked (rtp_mux, padpriv, rtpbuf);
411
412     if (drop)
413       break;
414
415     gst_buffer_list_iterator_take (it, rtpbuf);
416
417     do {
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);
422       else
423         rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
424
425       gst_buffer_list_iterator_take (it, rtpbuf);
426
427     } while ((rtpbuf = gst_buffer_list_iterator_next (it)) != NULL);
428
429
430   }
431   gst_buffer_list_iterator_free (it);
432
433   if (!drop && rtp_mux->segment_pending) {
434     /*
435      * We set the start at 0, because we re-timestamps to the running time
436      */
437     newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
438         GST_FORMAT_TIME, 0, -1, 0);
439
440     rtp_mux->segment_pending = FALSE;
441   }
442
443   GST_OBJECT_UNLOCK (rtp_mux);
444
445   if (newseg_event)
446     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
447
448   if (drop) {
449     gst_buffer_list_unref (bufferlist);
450     ret = GST_FLOW_OK;
451   } else {
452     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
453   }
454
455 out:
456
457   gst_object_unref (rtp_mux);
458
459   return ret;
460 }
461
462 static GstFlowReturn
463 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
464 {
465   GstRTPMux *rtp_mux;
466   GstFlowReturn ret;
467   GstRTPMuxPadPrivate *padpriv;
468   GstEvent *newseg_event = NULL;
469   gboolean drop;
470
471   rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
472
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;
477   }
478
479   GST_OBJECT_LOCK (rtp_mux);
480   padpriv = gst_pad_get_element_private (pad);
481
482   if (!padpriv) {
483     GST_OBJECT_UNLOCK (rtp_mux);
484     gst_buffer_unref (buffer);
485     return GST_FLOW_NOT_LINKED;
486   }
487
488   buffer = gst_buffer_make_writable (buffer);
489
490   drop = !process_buffer_locked (rtp_mux, padpriv, buffer);
491
492   if (!drop) {
493     if (rtp_mux->segment_pending) {
494       /*
495        * We set the start at 0, because we re-timestamps to the running time
496        */
497       newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
498           GST_FORMAT_TIME, 0, -1, 0);
499
500       rtp_mux->segment_pending = FALSE;
501     }
502
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);
507     else
508       rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
509   }
510
511   GST_OBJECT_UNLOCK (rtp_mux);
512
513   if (newseg_event)
514     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
515
516   if (drop) {
517     gst_buffer_unref (buffer);
518     ret = GST_FLOW_OK;
519   } else {
520     ret = gst_pad_push (rtp_mux->srcpad, buffer);
521   }
522
523   return ret;
524 }
525
526 static gboolean
527 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
528 {
529   GstRTPMux *rtp_mux;
530   GstStructure *structure;
531   gboolean ret = FALSE;
532   GstRTPMuxPadPrivate *padpriv;
533
534   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
535
536   structure = gst_caps_get_structure (caps, 0);
537
538   if (!structure)
539     goto out;
540
541   GST_OBJECT_LOCK (rtp_mux);
542   padpriv = gst_pad_get_element_private (pad);
543   if (padpriv &&
544       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
545     padpriv->have_clock_base = TRUE;
546   }
547   GST_OBJECT_UNLOCK (rtp_mux);
548
549   caps = gst_caps_copy (caps);
550
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);
554
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);
558
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"));
563     }
564   }
565
566   if (ret) {
567     GST_OBJECT_LOCK (rtp_mux);
568     padpriv = gst_pad_get_element_private (pad);
569     if (padpriv)
570       gst_caps_replace (&padpriv->out_caps, caps);
571     GST_OBJECT_UNLOCK (rtp_mux);
572   }
573   gst_caps_unref (caps);
574
575 out:
576   gst_object_unref (rtp_mux);
577
578   return ret;
579 }
580
581 static void
582 clear_caps (GstCaps * caps, gboolean only_clock_rate)
583 {
584   gint i, j;
585
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);
589
590     for (j = 0; j < gst_structure_n_fields (s); j++) {
591       const gchar *name = gst_structure_nth_field_name (s, j);
592
593       if (strcmp (name, "clock-rate") && (only_clock_rate ||
594               (strcmp (name, "ssrc")))) {
595         gst_structure_remove_field (s, name);
596         j--;
597       }
598     }
599   }
600 }
601
602 static gboolean
603 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
604 {
605   GstPad *mypad = user_data;
606   GstPad *pad = item;
607   GstCaps *peercaps;
608   GstCaps *othercaps;
609   const GstCaps *accumcaps;
610   GstCaps *intersect;
611
612   if (pad == mypad) {
613     gst_object_unref (pad);
614     return TRUE;
615   }
616
617   peercaps = gst_pad_peer_get_caps (pad);
618   if (!peercaps) {
619     gst_object_unref (pad);
620     return TRUE;
621   }
622
623   othercaps = gst_caps_intersect (peercaps,
624       gst_pad_get_pad_template_caps (pad));
625   gst_caps_unref (peercaps);
626
627   accumcaps = gst_value_get_caps (ret);
628
629   clear_caps (othercaps, TRUE);
630
631   intersect = gst_caps_intersect (accumcaps, othercaps);
632
633   g_value_take_boxed (ret, intersect);
634
635   gst_caps_unref (othercaps);
636   gst_object_unref (pad);
637
638   return !gst_caps_is_empty (intersect);
639 }
640
641 static GstCaps *
642 gst_rtp_mux_getcaps (GstPad * pad)
643 {
644   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
645   GstCaps *caps = NULL;
646   GstIterator *iter = NULL;
647   GValue v = { 0 };
648   GstIteratorResult res;
649   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
650   GstCaps *othercaps = NULL;
651
652   if (peercaps) {
653     othercaps = gst_caps_intersect (peercaps,
654         gst_pad_get_pad_template_caps (pad));
655     gst_caps_unref (peercaps);
656   } else {
657     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
658   }
659
660   clear_caps (othercaps, FALSE);
661
662   g_value_init (&v, GST_TYPE_CAPS);
663
664   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
665   do {
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);
670
671   caps = (GstCaps *) gst_value_get_caps (&v);
672
673   if (res == GST_ITERATOR_ERROR) {
674     gst_caps_unref (caps);
675     caps = gst_caps_new_empty ();
676   }
677
678   if (othercaps)
679     gst_caps_unref (othercaps);
680   gst_object_unref (mux);
681
682   return caps;
683 }
684
685
686 static void
687 gst_rtp_mux_get_property (GObject * object,
688     guint prop_id, GValue * value, GParamSpec * pspec)
689 {
690   GstRTPMux *rtp_mux;
691
692   rtp_mux = GST_RTP_MUX (object);
693
694   switch (prop_id) {
695     case PROP_TIMESTAMP_OFFSET:
696       g_value_set_int (value, rtp_mux->ts_offset);
697       break;
698     case PROP_SEQNUM_OFFSET:
699       g_value_set_int (value, rtp_mux->seqnum_offset);
700       break;
701     case PROP_SEQNUM:
702       GST_OBJECT_LOCK (rtp_mux);
703       g_value_set_uint (value, rtp_mux->seqnum);
704       GST_OBJECT_UNLOCK (rtp_mux);
705       break;
706     case PROP_SSRC:
707       g_value_set_uint (value, rtp_mux->ssrc);
708       break;
709     default:
710       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
711       break;
712   }
713 }
714
715 static void
716 gst_rtp_mux_set_property (GObject * object,
717     guint prop_id, const GValue * value, GParamSpec * pspec)
718 {
719   GstRTPMux *rtp_mux;
720
721   rtp_mux = GST_RTP_MUX (object);
722
723   switch (prop_id) {
724     case PROP_TIMESTAMP_OFFSET:
725       rtp_mux->ts_offset = g_value_get_int (value);
726       break;
727     case PROP_SEQNUM_OFFSET:
728       rtp_mux->seqnum_offset = g_value_get_int (value);
729       break;
730     case PROP_SSRC:
731       rtp_mux->ssrc = g_value_get_uint (value);
732       break;
733     default:
734       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
735       break;
736   }
737 }
738
739 static gboolean
740 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
741 {
742
743   GstRTPMux *mux;
744   gboolean ret = FALSE;
745   gboolean forward = TRUE;
746
747   mux = GST_RTP_MUX (gst_pad_get_parent (pad));
748
749   switch (GST_EVENT_TYPE (event)) {
750     case GST_EVENT_FLUSH_STOP:
751     {
752       GstRTPMuxPadPrivate *padpriv;
753
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);
758       if (padpriv)
759         gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
760       GST_OBJECT_UNLOCK (mux);
761     }
762       break;
763     case GST_EVENT_NEWSEGMENT:
764     {
765       gboolean update;
766       gdouble rate, applied_rate;
767       GstFormat format;
768       gint64 start, stop, position;
769       GstRTPMuxPadPrivate *padpriv;
770
771       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
772           &format, &start, &stop, &position);
773
774       GST_OBJECT_LOCK (mux);
775       padpriv = gst_pad_get_element_private (pad);
776
777       if (padpriv) {
778         if (format == GST_FORMAT_TIME)
779           gst_segment_set_newsegment_full (&padpriv->segment, update,
780               rate, applied_rate, format, start, stop, position);
781         else
782           gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
783       }
784       GST_OBJECT_UNLOCK (mux);
785       gst_event_unref (event);
786       forward = FALSE;
787       ret = TRUE;
788       break;
789     }
790     default:
791       break;
792   }
793
794   if (forward)
795     ret = gst_pad_push_event (mux->srcpad, event);
796
797   gst_object_unref (mux);
798   return ret;
799 }
800
801
802 static void
803 clear_segment (gpointer data, gpointer user_data)
804 {
805   GstPad *pad = data;
806   GstRTPMux *mux = user_data;
807   GstRTPMuxPadPrivate *padpriv;
808
809   GST_OBJECT_LOCK (mux);
810   padpriv = gst_pad_get_element_private (pad);
811   if (padpriv)
812     gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
813   GST_OBJECT_UNLOCK (mux);
814
815   gst_object_unref (pad);
816 }
817
818
819 static void
820 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
821 {
822   GstIterator *iter;
823
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);
828
829   GST_OBJECT_LOCK (rtp_mux);
830   rtp_mux->segment_pending = TRUE;
831
832   if (rtp_mux->ssrc == -1)
833     rtp_mux->current_ssrc = g_random_int ();
834   else
835     rtp_mux->current_ssrc = rtp_mux->ssrc;
836
837   if (rtp_mux->seqnum_offset == -1)
838     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
839   else
840     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
841   rtp_mux->seqnum = rtp_mux->seqnum_base;
842
843   if (rtp_mux->ts_offset == -1)
844     rtp_mux->ts_base = g_random_int ();
845   else
846     rtp_mux->ts_base = rtp_mux->ts_offset;
847
848   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
849
850   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
851
852   GST_OBJECT_UNLOCK (rtp_mux);
853 }
854
855 static GstStateChangeReturn
856 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
857 {
858   GstRTPMux *rtp_mux;
859
860   rtp_mux = GST_RTP_MUX (element);
861
862   switch (transition) {
863     case GST_STATE_CHANGE_NULL_TO_READY:
864       break;
865     case GST_STATE_CHANGE_READY_TO_PAUSED:
866       gst_rtp_mux_ready_to_paused (rtp_mux);
867       break;
868     case GST_STATE_CHANGE_PAUSED_TO_READY:
869       break;
870     default:
871       break;
872   }
873
874   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
875 }
876
877 gboolean
878 gst_rtp_mux_plugin_init (GstPlugin * plugin)
879 {
880   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
881
882   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
883       GST_TYPE_RTP_MUX);
884 }