rtpmux: Enable proxy caps on the src pads
[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., 51 Franklin St, Fifth Floor,
25  * Boston, MA 02110-1301, 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, const GstCaps * caps);
93 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
94 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstObject * parent,
95     GstBuffer * buffer);
96 static GstFlowReturn gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
97     GstBufferList * bufferlist);
98 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux,
99     GstCaps * caps);
100 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent,
101     GstEvent * event);
102 static gboolean gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent,
103     GstQuery * query);
104
105 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
106     element, GstStateChange transition);
107
108 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
109     const GValue * value, GParamSpec * pspec);
110 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
111     GValue * value, GParamSpec * pspec);
112 static void gst_rtp_mux_dispose (GObject * object);
113
114 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
115     GstEvent * event);
116
117 G_DEFINE_TYPE (GstRTPMux, gst_rtp_mux, GST_TYPE_ELEMENT);
118
119
120 static void
121 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125
126   gobject_class = (GObjectClass *) klass;
127   gstelement_class = (GstElementClass *) klass;
128
129
130   gst_element_class_add_pad_template (gstelement_class,
131       gst_static_pad_template_get (&src_factory));
132   gst_element_class_add_pad_template (gstelement_class,
133       gst_static_pad_template_get (&sink_factory));
134
135   gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
136       "Codec/Muxer",
137       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
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   GstRTPMux *rtp_mux = GST_RTP_MUX (object);
175   GList *item;
176
177   g_clear_object (&rtp_mux->last_pad);
178
179 restart:
180   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
181     GstPad *pad = GST_PAD (item->data);
182     if (GST_PAD_IS_SINK (pad)) {
183       gst_element_release_request_pad (GST_ELEMENT (object), pad);
184       goto restart;
185     }
186   }
187
188   G_OBJECT_CLASS (gst_rtp_mux_parent_class)->dispose (object);
189 }
190
191 static gboolean
192 gst_rtp_mux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
193 {
194   GstRTPMux *rtp_mux = GST_RTP_MUX (parent);
195   GstRTPMuxClass *klass;
196   gboolean ret = FALSE;
197
198   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
199
200   ret = klass->src_event (rtp_mux, event);
201
202   return ret;
203 }
204
205 static gboolean
206 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
207 {
208   GstIterator *iter;
209   gboolean result = FALSE;
210   gboolean done = FALSE;
211
212   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
213
214   while (!done) {
215     GValue item = { 0, };
216
217     switch (gst_iterator_next (iter, &item)) {
218       case GST_ITERATOR_OK:
219         gst_event_ref (event);
220         result |= gst_pad_push_event (g_value_get_object (&item), event);
221         g_value_reset (&item);
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 * rtp_mux)
242 {
243   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
244
245   rtp_mux->srcpad =
246       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
247           "src"), "src");
248   gst_pad_set_event_function (rtp_mux->srcpad,
249       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
250   GST_PAD_SET_PROXY_CAPS (rtp_mux->srcpad);
251   gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
252
253   rtp_mux->ssrc = DEFAULT_SSRC;
254   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
255   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
256
257   rtp_mux->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_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
267   gst_pad_set_chain_list_function (sinkpad,
268       GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
269   gst_pad_set_event_function (sinkpad,
270       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
271   gst_pad_set_query_function (sinkpad,
272       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
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, const GstCaps * caps)
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     g_slice_free (GstRTPMuxPadPrivate, padpriv);
323   }
324 }
325
326 /* Put our own clock-base on the buffer */
327 static void
328 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
329     GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
330 {
331   guint32 ts;
332   guint32 sink_ts_base = 0;
333
334   if (padpriv && padpriv->have_clock_base)
335     sink_ts_base = padpriv->clock_base;
336
337   ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
338       rtp_mux->ts_base;
339   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
340       gst_rtp_buffer_get_timestamp (rtpbuffer), ts);
341   gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
342 }
343
344 static gboolean
345 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
346     GstRTPBuffer * rtpbuffer)
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, rtpbuffer))
352       return FALSE;
353
354   rtp_mux->seqnum++;
355   gst_rtp_buffer_set_seq (rtpbuffer, rtp_mux->seqnum);
356
357   gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
358   gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
359   GST_LOG_OBJECT (rtp_mux,
360       "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u",
361       rtpbuffer->map[0].size, rtp_mux->seqnum,
362       gst_rtp_buffer_get_timestamp (rtpbuffer));
363
364   if (padpriv) {
365     if (padpriv->segment.format == GST_FORMAT_TIME)
366       GST_BUFFER_PTS (rtpbuffer->buffer) =
367           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
368           GST_BUFFER_PTS (rtpbuffer->buffer));
369   }
370
371   return TRUE;
372 }
373
374 struct BufferListData
375 {
376   GstRTPMux *rtp_mux;
377   GstRTPMuxPadPrivate *padpriv;
378   gboolean drop;
379 };
380
381 static gboolean
382 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
383 {
384   struct BufferListData *bd = user_data;
385   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
386
387   *buffer = gst_buffer_make_writable (*buffer);
388
389   gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
390
391   bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
392
393   gst_rtp_buffer_unmap (&rtpbuffer);
394
395   if (bd->drop)
396     return FALSE;
397
398   if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
399       GST_BUFFER_TIMESTAMP_IS_VALID (*buffer))
400     bd->rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (*buffer) +
401         GST_BUFFER_DURATION (*buffer);
402   else
403     bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
404
405   return TRUE;
406 }
407
408 static GstFlowReturn
409 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
410     GstBufferList * bufferlist)
411 {
412   GstRTPMux *rtp_mux;
413   GstFlowReturn ret;
414   GstRTPMuxPadPrivate *padpriv;
415   gboolean drop = TRUE;
416   struct BufferListData bd;
417
418   rtp_mux = GST_RTP_MUX (parent);
419
420   GST_OBJECT_LOCK (rtp_mux);
421
422   padpriv = gst_pad_get_element_private (pad);
423   if (!padpriv) {
424     GST_OBJECT_UNLOCK (rtp_mux);
425     ret = GST_FLOW_NOT_LINKED;
426     gst_buffer_list_unref (bufferlist);
427     goto out;
428   }
429
430   bd.rtp_mux = rtp_mux;
431   bd.padpriv = padpriv;
432   bd.drop = FALSE;
433
434   bufferlist = gst_buffer_list_make_writable (bufferlist);
435   gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
436
437   GST_OBJECT_UNLOCK (rtp_mux);
438
439   if (drop) {
440     gst_buffer_list_unref (bufferlist);
441     ret = GST_FLOW_OK;
442   } else {
443     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
444   }
445
446 out:
447
448   return ret;
449 }
450
451 static gboolean
452 resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
453 {
454   GstRTPMux *rtp_mux = user_data;
455
456   if (GST_EVENT_TYPE (*event) == GST_EVENT_CAPS) {
457     GstCaps *caps;
458
459     gst_event_parse_caps (*event, &caps);
460     gst_rtp_mux_setcaps (pad, rtp_mux, caps);
461   } else {
462     gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
463   }
464
465   return TRUE;
466 }
467
468 static GstFlowReturn
469 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
470 {
471   GstRTPMux *rtp_mux;
472   GstFlowReturn ret;
473   GstRTPMuxPadPrivate *padpriv;
474   gboolean drop;
475   gboolean changed = FALSE;
476   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
477
478   rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
479
480   GST_OBJECT_LOCK (rtp_mux);
481   padpriv = gst_pad_get_element_private (pad);
482
483   if (!padpriv) {
484     GST_OBJECT_UNLOCK (rtp_mux);
485     gst_buffer_unref (buffer);
486     return GST_FLOW_NOT_LINKED;
487   }
488
489   buffer = gst_buffer_make_writable (buffer);
490
491   if (!gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer)) {
492     GST_OBJECT_UNLOCK (rtp_mux);
493     gst_buffer_unref (buffer);
494     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
495     return GST_FLOW_ERROR;
496   }
497
498   drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
499
500   gst_rtp_buffer_unmap (&rtpbuffer);
501
502   if (!drop) {
503     if (pad != rtp_mux->last_pad) {
504       changed = TRUE;
505       g_clear_object (&rtp_mux->last_pad);
506       rtp_mux->last_pad = g_object_ref (pad);
507     }
508
509     if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
510         GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
511       rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (buffer) +
512           GST_BUFFER_DURATION (buffer);
513     else
514       rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
515   }
516
517   GST_OBJECT_UNLOCK (rtp_mux);
518
519   if (changed)
520     gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
521
522   if (drop) {
523     gst_buffer_unref (buffer);
524     ret = GST_FLOW_OK;
525   } else {
526     ret = gst_pad_push (rtp_mux->srcpad, buffer);
527   }
528
529   return ret;
530 }
531
532 static gboolean
533 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
534 {
535   GstStructure *structure;
536   gboolean ret = FALSE;
537   GstRTPMuxPadPrivate *padpriv;
538
539   structure = gst_caps_get_structure (caps, 0);
540
541   if (!structure)
542     return FALSE;
543
544   GST_OBJECT_LOCK (rtp_mux);
545   padpriv = gst_pad_get_element_private (pad);
546   if (padpriv &&
547       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
548     padpriv->have_clock_base = TRUE;
549   }
550   GST_OBJECT_UNLOCK (rtp_mux);
551
552   caps = gst_caps_copy (caps);
553
554   gst_caps_set_simple (caps,
555       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
556       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
557
558   if (rtp_mux->send_stream_start) {
559     gchar s_id[32];
560
561     /* stream-start (FIXME: create id based on input ids) */
562     g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
563     gst_pad_push_event (rtp_mux->srcpad, gst_event_new_stream_start (s_id));
564
565     rtp_mux->send_stream_start = FALSE;
566   }
567
568   GST_DEBUG_OBJECT (rtp_mux,
569       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
570   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
571
572   if (rtp_mux->ssrc == -1) {
573     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
574       rtp_mux->current_ssrc = g_value_get_uint
575           (gst_structure_get_value (structure, "ssrc"));
576     }
577   }
578
579   gst_caps_unref (caps);
580
581   return ret;
582 }
583
584 static void
585 clear_caps (GstCaps * caps, gboolean only_clock_rate)
586 {
587   gint i, j;
588
589   /* Lets only match on the clock-rate */
590   for (i = 0; i < gst_caps_get_size (caps); i++) {
591     GstStructure *s = gst_caps_get_structure (caps, i);
592
593     for (j = 0; j < gst_structure_n_fields (s); j++) {
594       const gchar *name = gst_structure_nth_field_name (s, j);
595
596       if (strcmp (name, "clock-rate") && (only_clock_rate ||
597               (strcmp (name, "ssrc")))) {
598         gst_structure_remove_field (s, name);
599         j--;
600       }
601     }
602   }
603 }
604
605 static gboolean
606 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
607 {
608   GstPad *mypad = user_data;
609   GstPad *pad = g_value_get_object (item);
610   GstCaps *peercaps;
611   GstCaps *accumcaps;
612   GstCaps *intersect;
613
614   if (pad == mypad)
615     return TRUE;
616
617   accumcaps = g_value_get_boxed (ret);
618   peercaps = gst_pad_peer_query_caps (pad, accumcaps);
619   if (!peercaps) {
620     g_warning ("no peercaps");
621     return TRUE;
622   }
623   peercaps = gst_caps_make_writable (peercaps);
624   clear_caps (peercaps, TRUE);
625
626   intersect = gst_caps_intersect (accumcaps, peercaps);
627
628   g_value_take_boxed (ret, intersect);
629   gst_caps_unref (peercaps);
630
631   return !gst_caps_is_empty (intersect);
632 }
633
634 static GstCaps *
635 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
636 {
637   GstCaps *caps = NULL;
638   GstIterator *iter = NULL;
639   GValue v = { 0 };
640   GstIteratorResult res;
641   GstCaps *peercaps;
642   GstCaps *othercaps;
643
644   peercaps = gst_pad_peer_query_caps (mux->srcpad, filter);
645
646   if (peercaps) {
647     othercaps = gst_caps_intersect_full (peercaps,
648         gst_pad_get_pad_template_caps (pad), GST_CAPS_INTERSECT_FIRST);
649     gst_caps_unref (peercaps);
650   } else {
651     if (filter)
652       othercaps = gst_caps_intersect_full (filter,
653           gst_pad_get_pad_template_caps (mux->srcpad),
654           GST_CAPS_INTERSECT_FIRST);
655     else
656       othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
657   }
658
659   clear_caps (othercaps, FALSE);
660
661   g_value_init (&v, GST_TYPE_CAPS);
662
663   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
664   do {
665     gst_value_set_caps (&v, othercaps);
666     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
667     gst_iterator_resync (iter);
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   gst_caps_unref (othercaps);
679
680   return caps;
681 }
682
683 static gboolean
684 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
685 {
686   GstRTPMux *mux = GST_RTP_MUX (parent);
687   gboolean res = FALSE;
688
689   switch (GST_QUERY_TYPE (query)) {
690     case GST_QUERY_CAPS:
691     {
692       GstCaps *filter, *caps;
693
694       gst_query_parse_caps (query, &filter);
695       caps = gst_rtp_mux_getcaps (pad, mux, filter);
696       gst_query_set_caps_result (query, caps);
697       gst_caps_unref (caps);
698       res = TRUE;
699       break;
700     }
701     default:
702       res = gst_pad_query_default (pad, parent, query);
703       break;
704   }
705
706   return res;
707
708
709 }
710
711
712 static void
713 gst_rtp_mux_get_property (GObject * object,
714     guint prop_id, GValue * value, GParamSpec * pspec)
715 {
716   GstRTPMux *rtp_mux;
717
718   rtp_mux = GST_RTP_MUX (object);
719
720   switch (prop_id) {
721     case PROP_TIMESTAMP_OFFSET:
722       g_value_set_int (value, rtp_mux->ts_offset);
723       break;
724     case PROP_SEQNUM_OFFSET:
725       g_value_set_int (value, rtp_mux->seqnum_offset);
726       break;
727     case PROP_SEQNUM:
728       GST_OBJECT_LOCK (rtp_mux);
729       g_value_set_uint (value, rtp_mux->seqnum);
730       GST_OBJECT_UNLOCK (rtp_mux);
731       break;
732     case PROP_SSRC:
733       g_value_set_uint (value, rtp_mux->ssrc);
734       break;
735     default:
736       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
737       break;
738   }
739 }
740
741 static void
742 gst_rtp_mux_set_property (GObject * object,
743     guint prop_id, const GValue * value, GParamSpec * pspec)
744 {
745   GstRTPMux *rtp_mux;
746
747   rtp_mux = GST_RTP_MUX (object);
748
749   switch (prop_id) {
750     case PROP_TIMESTAMP_OFFSET:
751       rtp_mux->ts_offset = g_value_get_int (value);
752       break;
753     case PROP_SEQNUM_OFFSET:
754       rtp_mux->seqnum_offset = g_value_get_int (value);
755       break;
756     case PROP_SSRC:
757       rtp_mux->ssrc = g_value_get_uint (value);
758       break;
759     default:
760       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
761       break;
762   }
763 }
764
765 static gboolean
766 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
767 {
768   GstRTPMux *mux = GST_RTP_MUX (parent);
769   gboolean is_pad;
770   gboolean ret;
771
772   switch (GST_EVENT_TYPE (event)) {
773     case GST_EVENT_CAPS:
774     {
775       GstCaps *caps;
776
777       gst_event_parse_caps (event, &caps);
778       ret = gst_rtp_mux_setcaps (pad, mux, caps);
779       gst_event_unref (event);
780       return ret;
781     }
782     case GST_EVENT_FLUSH_STOP:
783     {
784       GST_OBJECT_LOCK (mux);
785       mux->last_stop = GST_CLOCK_TIME_NONE;
786       GST_OBJECT_UNLOCK (mux);
787       break;
788     }
789     case GST_EVENT_SEGMENT:
790     {
791       GstRTPMuxPadPrivate *padpriv;
792
793       GST_OBJECT_LOCK (mux);
794       padpriv = gst_pad_get_element_private (pad);
795
796       if (padpriv) {
797         gst_event_copy_segment (event, &padpriv->segment);
798       }
799       GST_OBJECT_UNLOCK (mux);
800       break;
801     }
802     default:
803       break;
804   }
805
806   GST_OBJECT_LOCK (mux);
807   is_pad = (pad == mux->last_pad);
808   GST_OBJECT_UNLOCK (mux);
809
810   if (is_pad) {
811     return gst_pad_push_event (mux->srcpad, event);
812   } else {
813     gst_event_unref (event);
814     return TRUE;
815   }
816 }
817
818 static void
819 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
820 {
821
822   GST_OBJECT_LOCK (rtp_mux);
823
824   g_clear_object (&rtp_mux->last_pad);
825   rtp_mux->send_stream_start = TRUE;
826
827   if (rtp_mux->ssrc == -1)
828     rtp_mux->current_ssrc = g_random_int ();
829   else
830     rtp_mux->current_ssrc = rtp_mux->ssrc;
831
832   if (rtp_mux->seqnum_offset == -1)
833     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
834   else
835     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
836   rtp_mux->seqnum = rtp_mux->seqnum_base;
837
838   if (rtp_mux->ts_offset == -1)
839     rtp_mux->ts_base = g_random_int ();
840   else
841     rtp_mux->ts_base = rtp_mux->ts_offset;
842
843   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
844
845   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
846
847   GST_OBJECT_UNLOCK (rtp_mux);
848 }
849
850 static GstStateChangeReturn
851 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
852 {
853   GstRTPMux *rtp_mux;
854   GstStateChangeReturn ret;
855
856   rtp_mux = GST_RTP_MUX (element);
857
858   switch (transition) {
859     case GST_STATE_CHANGE_READY_TO_PAUSED:
860       gst_rtp_mux_ready_to_paused (rtp_mux);
861       break;
862     default:
863       break;
864   }
865
866   ret = GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
867       transition);
868
869   switch (transition) {
870     case GST_STATE_CHANGE_PAUSED_TO_READY:
871       g_clear_object (&rtp_mux->last_pad);
872       break;
873     default:
874       break;
875   }
876
877   return ret;
878 }
879
880 gboolean
881 gst_rtp_mux_plugin_init (GstPlugin * plugin)
882 {
883   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
884
885   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
886       GST_TYPE_RTP_MUX);
887 }