rtpmux: fix PROP_TIMESTAMP_OFFSET range problems
[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-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
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
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54
55 #include <gst/gst.h>
56 #include <gst/rtp/gstrtpbuffer.h>
57 #include <string.h>
58
59 #include "gstrtpmux.h"
60
61 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
62 #define GST_CAT_DEFAULT gst_rtp_mux_debug
63
64 enum
65 {
66   PROP_0,
67   PROP_TIMESTAMP_OFFSET,
68   PROP_SEQNUM_OFFSET,
69   PROP_SEQNUM,
70   PROP_SSRC
71 };
72
73 #define DEFAULT_TIMESTAMP_OFFSET -1
74 #define DEFAULT_SEQNUM_OFFSET    -1
75 #define DEFAULT_SSRC             -1
76
77 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS ("application/x-rtp")
81     );
82
83 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
84     GST_PAD_SINK,
85     GST_PAD_REQUEST,
86     GST_STATIC_CAPS ("application/x-rtp")
87     );
88
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,
93     GstBuffer * buffer);
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,
97     GstCaps * caps);
98 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent,
99     GstEvent * event);
100 static gboolean gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent,
101     GstQuery * query);
102
103 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
104     element, GstStateChange transition);
105
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);
111
112 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
113     GstEvent * event);
114
115 G_DEFINE_TYPE (GstRTPMux, gst_rtp_mux, GST_TYPE_ELEMENT);
116
117
118 static void
119 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *gstelement_class;
123
124   gobject_class = (GObjectClass *) klass;
125   gstelement_class = (GstElementClass *) klass;
126
127   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
128   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
129
130   gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
131       "Codec/Muxer",
132       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
133
134   gobject_class->get_property = gst_rtp_mux_get_property;
135   gobject_class->set_property = gst_rtp_mux_set_property;
136   gobject_class->dispose = gst_rtp_mux_dispose;
137
138   klass->src_event = gst_rtp_mux_src_event_real;
139
140   g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
141       g_param_spec_int64 ("timestamp-offset", "Timestamp Offset",
142           "Offset to add to all outgoing timestamps (-1 = random)",
143           -1, G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
144           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145
146   g_object_class_install_property (gobject_class, PROP_SEQNUM_OFFSET,
147       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
148           "Offset to add to all outgoing seqnum (-1 = random)",
149           -1, G_MAXUINT16, DEFAULT_SEQNUM_OFFSET,
150           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151
152   g_object_class_install_property (gobject_class, PROP_SEQNUM,
153       g_param_spec_uint ("seqnum", "Sequence number",
154           "The RTP sequence number of the last processed packet",
155           0, G_MAXUINT, 0,
156           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157
158   g_object_class_install_property (gobject_class, PROP_SSRC,
159       g_param_spec_uint ("ssrc", "SSRC",
160           "The SSRC of the packets (default == random)",
161           0, G_MAXUINT, DEFAULT_SSRC,
162           GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
163           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;
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   switch (GST_EVENT_TYPE (event)) {
209     case GST_EVENT_CUSTOM_UPSTREAM:
210     {
211       const GstStructure *s = gst_event_get_structure (event);
212
213       if (gst_structure_has_name (s, "GstRTPCollision")) {
214         guint ssrc = 0;
215
216         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
217           ssrc = -1;
218
219         GST_DEBUG_OBJECT (rtp_mux, "collided ssrc: %" G_GUINT32_FORMAT, ssrc);
220
221         /* choose another ssrc for our stream */
222         GST_OBJECT_LOCK (rtp_mux);
223         if (ssrc == rtp_mux->current_ssrc) {
224           GstCaps *caps;
225           guint suggested_ssrc = 0;
226           guint32 new_ssrc;
227
228           if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
229             rtp_mux->current_ssrc = suggested_ssrc;
230
231           while (ssrc == rtp_mux->current_ssrc)
232             rtp_mux->current_ssrc = g_random_int ();
233
234           new_ssrc = rtp_mux->current_ssrc;
235           GST_OBJECT_UNLOCK (rtp_mux);
236
237           caps = gst_pad_get_current_caps (rtp_mux->srcpad);
238           caps = gst_caps_make_writable (caps);
239           gst_caps_set_simple (caps, "ssrc", G_TYPE_UINT, new_ssrc, NULL);
240           gst_pad_set_caps (rtp_mux->srcpad, caps);
241           gst_caps_unref (caps);
242         } else {
243           GST_OBJECT_UNLOCK (rtp_mux);
244         }
245       }
246       break;
247     }
248     default:
249       break;
250   }
251
252
253   return gst_pad_event_default (rtp_mux->srcpad, GST_OBJECT (rtp_mux), event);
254 }
255
256 static void
257 gst_rtp_mux_init (GstRTPMux * rtp_mux)
258 {
259   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
260
261   rtp_mux->srcpad =
262       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
263           "src"), "src");
264   gst_pad_set_event_function (rtp_mux->srcpad,
265       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
266   gst_pad_use_fixed_caps (rtp_mux->srcpad);
267   gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
268
269   rtp_mux->ssrc = DEFAULT_SSRC;
270   rtp_mux->current_ssrc = DEFAULT_SSRC;
271   rtp_mux->ssrc_random = TRUE;
272   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
273   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
274
275   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
276 }
277
278 static void
279 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
280 {
281   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
282
283   /* setup some pad functions */
284   gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
285   gst_pad_set_chain_list_function (sinkpad,
286       GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
287   gst_pad_set_event_function (sinkpad,
288       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
289   gst_pad_set_query_function (sinkpad,
290       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
291
292
293   gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
294
295   gst_pad_set_element_private (sinkpad, padpriv);
296
297   gst_pad_set_active (sinkpad, TRUE);
298   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
299 }
300
301 static GstPad *
302 gst_rtp_mux_request_new_pad (GstElement * element,
303     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
304 {
305   GstRTPMux *rtp_mux;
306   GstPad *newpad;
307
308   g_return_val_if_fail (templ != NULL, NULL);
309   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
310
311   rtp_mux = GST_RTP_MUX (element);
312
313   if (templ->direction != GST_PAD_SINK) {
314     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
315     return NULL;
316   }
317
318   newpad = gst_pad_new_from_template (templ, req_name);
319   if (newpad)
320     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
321   else
322     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
323
324   return newpad;
325 }
326
327 static void
328 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
329 {
330   GstRTPMuxPadPrivate *padpriv;
331
332   GST_OBJECT_LOCK (element);
333   padpriv = gst_pad_get_element_private (pad);
334   gst_pad_set_element_private (pad, NULL);
335   GST_OBJECT_UNLOCK (element);
336
337   gst_element_remove_pad (element, pad);
338
339   if (padpriv) {
340     g_slice_free (GstRTPMuxPadPrivate, padpriv);
341   }
342 }
343
344 /* Put our own timestamp-offset on the buffer */
345 static void
346 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
347     GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
348 {
349   guint32 ts;
350   guint32 sink_ts_base = 0;
351
352   if (padpriv && padpriv->have_timestamp_offset)
353     sink_ts_base = padpriv->timestamp_offset;
354
355   ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
356       rtp_mux->ts_base;
357   GST_LOG_OBJECT (rtp_mux,
358       "Re-adjusting RTP ts %u to %u (sink_ts_base = %u, rtp_mux->ts_base=%u)",
359       gst_rtp_buffer_get_timestamp (rtpbuffer),
360       ts, sink_ts_base, rtp_mux->ts_base);
361   gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
362 }
363
364 static gboolean
365 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
366     GstRTPBuffer * rtpbuffer)
367 {
368   GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
369
370   if (klass->accept_buffer_locked)
371     if (!klass->accept_buffer_locked (rtp_mux, padpriv, rtpbuffer))
372       return FALSE;
373
374   rtp_mux->seqnum++;
375   gst_rtp_buffer_set_seq (rtpbuffer, rtp_mux->seqnum);
376
377   gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
378   gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
379   GST_LOG_OBJECT (rtp_mux,
380       "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u",
381       rtpbuffer->map[0].size, rtp_mux->seqnum,
382       gst_rtp_buffer_get_timestamp (rtpbuffer));
383
384   if (padpriv) {
385     if (padpriv->segment.format == GST_FORMAT_TIME)
386       GST_BUFFER_PTS (rtpbuffer->buffer) =
387           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
388           GST_BUFFER_PTS (rtpbuffer->buffer));
389   }
390
391   return TRUE;
392 }
393
394 struct BufferListData
395 {
396   GstRTPMux *rtp_mux;
397   GstRTPMuxPadPrivate *padpriv;
398   gboolean drop;
399 };
400
401 static gboolean
402 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
403 {
404   struct BufferListData *bd = user_data;
405   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
406
407   *buffer = gst_buffer_make_writable (*buffer);
408
409   gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
410
411   bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
412
413   gst_rtp_buffer_unmap (&rtpbuffer);
414
415   if (bd->drop)
416     return FALSE;
417
418   if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
419       GST_BUFFER_PTS_IS_VALID (*buffer))
420     bd->rtp_mux->last_stop = GST_BUFFER_PTS (*buffer) +
421         GST_BUFFER_DURATION (*buffer);
422   else
423     bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
424
425   return TRUE;
426 }
427
428 static gboolean resend_events (GstPad * pad, GstEvent ** event,
429     gpointer user_data);
430
431 static GstFlowReturn
432 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
433     GstBufferList * bufferlist)
434 {
435   GstRTPMux *rtp_mux;
436   GstFlowReturn ret;
437   GstRTPMuxPadPrivate *padpriv;
438   gboolean changed = FALSE;
439   struct BufferListData bd;
440
441   rtp_mux = GST_RTP_MUX (parent);
442
443   if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
444     GstCaps *current_caps = gst_pad_get_current_caps (pad);
445
446     if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
447       ret = GST_FLOW_NOT_NEGOTIATED;
448       gst_buffer_list_unref (bufferlist);
449       goto out;
450     }
451     gst_caps_unref (current_caps);
452   }
453
454   GST_OBJECT_LOCK (rtp_mux);
455
456   padpriv = gst_pad_get_element_private (pad);
457   if (!padpriv) {
458     GST_OBJECT_UNLOCK (rtp_mux);
459     ret = GST_FLOW_NOT_LINKED;
460     gst_buffer_list_unref (bufferlist);
461     goto out;
462   }
463
464   bd.rtp_mux = rtp_mux;
465   bd.padpriv = padpriv;
466   bd.drop = FALSE;
467
468   bufferlist = gst_buffer_list_make_writable (bufferlist);
469   gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
470
471   if (!bd.drop && pad != rtp_mux->last_pad) {
472     changed = TRUE;
473     g_clear_object (&rtp_mux->last_pad);
474     rtp_mux->last_pad = g_object_ref (pad);
475   }
476
477   GST_OBJECT_UNLOCK (rtp_mux);
478
479   if (changed)
480     gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
481
482   if (bd.drop) {
483     gst_buffer_list_unref (bufferlist);
484     ret = GST_FLOW_OK;
485   } else {
486     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
487   }
488
489 out:
490
491   return ret;
492 }
493
494 static gboolean
495 resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
496 {
497   GstRTPMux *rtp_mux = user_data;
498
499   if (GST_EVENT_TYPE (*event) == GST_EVENT_CAPS) {
500     GstCaps *caps;
501
502     gst_event_parse_caps (*event, &caps);
503     gst_rtp_mux_setcaps (pad, rtp_mux, caps);
504   } else {
505     gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
506   }
507
508   return TRUE;
509 }
510
511 static GstFlowReturn
512 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
513 {
514   GstRTPMux *rtp_mux;
515   GstFlowReturn ret;
516   GstRTPMuxPadPrivate *padpriv;
517   gboolean drop;
518   gboolean changed = FALSE;
519   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
520
521   rtp_mux = GST_RTP_MUX (parent);
522
523   if (gst_pad_check_reconfigure (rtp_mux->srcpad)) {
524     GstCaps *current_caps = gst_pad_get_current_caps (pad);
525
526     if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
527       ret = GST_FLOW_NOT_NEGOTIATED;
528       gst_buffer_unref (buffer);
529       goto out;
530     }
531     gst_caps_unref (current_caps);
532   }
533
534   GST_OBJECT_LOCK (rtp_mux);
535   padpriv = gst_pad_get_element_private (pad);
536
537   if (!padpriv) {
538     GST_OBJECT_UNLOCK (rtp_mux);
539     gst_buffer_unref (buffer);
540     return GST_FLOW_NOT_LINKED;
541   }
542
543   buffer = gst_buffer_make_writable (buffer);
544
545   if (!gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer)) {
546     GST_OBJECT_UNLOCK (rtp_mux);
547     gst_buffer_unref (buffer);
548     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
549     return GST_FLOW_ERROR;
550   }
551
552   drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
553
554   gst_rtp_buffer_unmap (&rtpbuffer);
555
556   if (!drop) {
557     if (pad != rtp_mux->last_pad) {
558       changed = TRUE;
559       g_clear_object (&rtp_mux->last_pad);
560       rtp_mux->last_pad = g_object_ref (pad);
561     }
562
563     if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
564         GST_BUFFER_PTS_IS_VALID (buffer))
565       rtp_mux->last_stop = GST_BUFFER_PTS (buffer) +
566           GST_BUFFER_DURATION (buffer);
567     else
568       rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
569   }
570
571   GST_OBJECT_UNLOCK (rtp_mux);
572
573   if (changed)
574     gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
575
576   if (drop) {
577     gst_buffer_unref (buffer);
578     ret = GST_FLOW_OK;
579   } else {
580     ret = gst_pad_push (rtp_mux->srcpad, buffer);
581   }
582
583 out:
584   return ret;
585 }
586
587 static gboolean
588 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
589 {
590   GstStructure *structure;
591   gboolean ret = FALSE;
592   GstRTPMuxPadPrivate *padpriv;
593   GstCaps *peercaps;
594
595   if (!gst_caps_is_fixed (caps))
596     return FALSE;
597
598   peercaps = gst_pad_peer_query_caps (rtp_mux->srcpad, NULL);
599   if (peercaps) {
600     GstCaps *tcaps, *othercaps;;
601     tcaps = gst_pad_get_pad_template_caps (pad);
602     othercaps = gst_caps_intersect_full (peercaps, tcaps,
603         GST_CAPS_INTERSECT_FIRST);
604
605     if (gst_caps_get_size (othercaps) > 0) {
606       structure = gst_caps_get_structure (othercaps, 0);
607       GST_OBJECT_LOCK (rtp_mux);
608       if (gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
609         GST_DEBUG_OBJECT (pad, "Use downstream ssrc: %x",
610             rtp_mux->current_ssrc);
611         rtp_mux->have_ssrc = TRUE;
612       }
613       GST_OBJECT_UNLOCK (rtp_mux);
614     }
615
616     gst_caps_unref (othercaps);
617
618     gst_caps_unref (peercaps);
619     gst_caps_unref (tcaps);
620   }
621
622   structure = gst_caps_get_structure (caps, 0);
623
624   if (!structure)
625     return FALSE;
626
627   GST_OBJECT_LOCK (rtp_mux);
628   padpriv = gst_pad_get_element_private (pad);
629   if (padpriv &&
630       gst_structure_get_uint (structure, "timestamp-offset",
631           &padpriv->timestamp_offset)) {
632     padpriv->have_timestamp_offset = TRUE;
633   }
634
635   caps = gst_caps_copy (caps);
636
637   /* if we don't have a specified ssrc, first try to take one from the caps,
638      and if that fails, generate one */
639   if (!rtp_mux->have_ssrc) {
640     if (rtp_mux->ssrc_random) {
641       if (!gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc))
642         rtp_mux->current_ssrc = g_random_int ();
643       rtp_mux->have_ssrc = TRUE;
644     }
645   }
646
647   gst_caps_set_simple (caps,
648       "timestamp-offset", G_TYPE_UINT, rtp_mux->ts_base,
649       "seqnum-offset", G_TYPE_UINT, rtp_mux->seqnum_base,
650       "ssrc", G_TYPE_UINT, rtp_mux->current_ssrc, NULL);
651
652   GST_OBJECT_UNLOCK (rtp_mux);
653
654   if (rtp_mux->send_stream_start) {
655     gchar s_id[32];
656
657     /* stream-start (FIXME: create id based on input ids) */
658     g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
659     gst_pad_push_event (rtp_mux->srcpad, gst_event_new_stream_start (s_id));
660
661     rtp_mux->send_stream_start = FALSE;
662   }
663
664   GST_DEBUG_OBJECT (rtp_mux,
665       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
666   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
667
668
669   gst_caps_unref (caps);
670
671   return ret;
672 }
673
674 static void
675 clear_caps (GstCaps * caps, gboolean only_clock_rate)
676 {
677   gint i, j;
678
679   /* Lets only match on the clock-rate */
680   for (i = 0; i < gst_caps_get_size (caps); i++) {
681     GstStructure *s = gst_caps_get_structure (caps, i);
682
683     for (j = 0; j < gst_structure_n_fields (s); j++) {
684       const gchar *name = gst_structure_nth_field_name (s, j);
685
686       if (strcmp (name, "clock-rate") && (only_clock_rate ||
687               (strcmp (name, "ssrc")))) {
688         gst_structure_remove_field (s, name);
689         j--;
690       }
691     }
692   }
693 }
694
695 static gboolean
696 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
697 {
698   GstPad *mypad = user_data;
699   GstPad *pad = g_value_get_object (item);
700   GstCaps *peercaps;
701   GstCaps *accumcaps;
702
703   if (pad == mypad)
704     return TRUE;
705
706   accumcaps = g_value_get_boxed (ret);
707   peercaps = gst_pad_peer_query_caps (pad, accumcaps);
708   if (!peercaps) {
709     g_warning ("no peercaps");
710     return TRUE;
711   }
712   peercaps = gst_caps_make_writable (peercaps);
713   clear_caps (peercaps, TRUE);
714
715   g_value_take_boxed (ret, peercaps);
716
717   return !gst_caps_is_empty (peercaps);
718 }
719
720 static GstCaps *
721 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
722 {
723   GstCaps *caps = NULL;
724   GstIterator *iter = NULL;
725   GValue v = { 0 };
726   GstIteratorResult res;
727   GstCaps *peercaps;
728   GstCaps *othercaps;
729   GstCaps *tcaps;
730
731   peercaps = gst_pad_peer_query_caps (mux->srcpad, NULL);
732
733   if (peercaps) {
734     tcaps = gst_pad_get_pad_template_caps (pad);
735     othercaps = gst_caps_intersect_full (peercaps, tcaps,
736         GST_CAPS_INTERSECT_FIRST);
737     gst_caps_unref (peercaps);
738   } else {
739     tcaps = gst_pad_get_pad_template_caps (mux->srcpad);
740     if (filter)
741       othercaps = gst_caps_intersect_full (filter, tcaps,
742           GST_CAPS_INTERSECT_FIRST);
743     else
744       othercaps = gst_caps_copy (tcaps);
745   }
746   gst_caps_unref (tcaps);
747
748   GST_LOG_OBJECT (pad, "Intersected srcpad-peercaps and template caps: %"
749       GST_PTR_FORMAT, othercaps);
750
751   clear_caps (othercaps, TRUE);
752
753   g_value_init (&v, GST_TYPE_CAPS);
754
755   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
756   do {
757     gst_value_set_caps (&v, othercaps);
758     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
759     gst_iterator_resync (iter);
760   } while (res == GST_ITERATOR_RESYNC);
761   gst_iterator_free (iter);
762
763   caps = gst_caps_intersect ((GstCaps *) gst_value_get_caps (&v), othercaps);
764
765   g_value_unset (&v);
766   gst_caps_unref (othercaps);
767
768   if (res == GST_ITERATOR_ERROR) {
769     gst_caps_unref (caps);
770     caps = gst_caps_new_empty ();
771   }
772
773
774   return caps;
775 }
776
777 static gboolean
778 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
779 {
780   GstRTPMux *mux = GST_RTP_MUX (parent);
781   gboolean res = FALSE;
782
783   switch (GST_QUERY_TYPE (query)) {
784     case GST_QUERY_CAPS:
785     {
786       GstCaps *filter, *caps;
787
788       gst_query_parse_caps (query, &filter);
789       GST_LOG_OBJECT (pad, "Received caps-query with filter-caps: %"
790           GST_PTR_FORMAT, filter);
791       caps = gst_rtp_mux_getcaps (pad, mux, filter);
792       gst_query_set_caps_result (query, caps);
793       GST_LOG_OBJECT (mux, "Answering caps-query with caps: %"
794           GST_PTR_FORMAT, caps);
795       gst_caps_unref (caps);
796       res = TRUE;
797       break;
798     }
799     default:
800       res = gst_pad_query_default (pad, parent, query);
801       break;
802   }
803
804   return res;
805 }
806
807 static void
808 gst_rtp_mux_get_property (GObject * object,
809     guint prop_id, GValue * value, GParamSpec * pspec)
810 {
811   GstRTPMux *rtp_mux;
812
813   rtp_mux = GST_RTP_MUX (object);
814
815   GST_OBJECT_LOCK (rtp_mux);
816   switch (prop_id) {
817     case PROP_TIMESTAMP_OFFSET:
818       g_value_set_int64 (value, rtp_mux->ts_offset);
819       break;
820     case PROP_SEQNUM_OFFSET:
821       g_value_set_int (value, rtp_mux->seqnum_offset);
822       break;
823     case PROP_SEQNUM:
824       g_value_set_uint (value, rtp_mux->seqnum);
825       break;
826     case PROP_SSRC:
827       g_value_set_uint (value, rtp_mux->ssrc);
828       break;
829     default:
830       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
831       break;
832   }
833   GST_OBJECT_UNLOCK (rtp_mux);
834 }
835
836 static void
837 gst_rtp_mux_set_property (GObject * object,
838     guint prop_id, const GValue * value, GParamSpec * pspec)
839 {
840   GstRTPMux *rtp_mux;
841
842   rtp_mux = GST_RTP_MUX (object);
843
844   switch (prop_id) {
845     case PROP_TIMESTAMP_OFFSET:
846       rtp_mux->ts_offset = g_value_get_int64 (value);
847       break;
848     case PROP_SEQNUM_OFFSET:
849       rtp_mux->seqnum_offset = g_value_get_int (value);
850       break;
851     case PROP_SSRC:
852       GST_OBJECT_LOCK (rtp_mux);
853       rtp_mux->ssrc = g_value_get_uint (value);
854       rtp_mux->current_ssrc = rtp_mux->ssrc;
855       rtp_mux->have_ssrc = TRUE;
856       rtp_mux->ssrc_random = FALSE;
857       GST_OBJECT_UNLOCK (rtp_mux);
858       break;
859     default:
860       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
861       break;
862   }
863 }
864
865 static gboolean
866 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
867 {
868   GstRTPMux *mux = GST_RTP_MUX (parent);
869   gboolean is_pad;
870   gboolean ret;
871
872   switch (GST_EVENT_TYPE (event)) {
873     case GST_EVENT_CAPS:
874     {
875       GstCaps *caps;
876
877       gst_event_parse_caps (event, &caps);
878       GST_LOG_OBJECT (pad, "Received caps-event with caps: %"
879           GST_PTR_FORMAT, caps);
880       ret = gst_rtp_mux_setcaps (pad, mux, caps);
881       gst_event_unref (event);
882       return ret;
883     }
884     case GST_EVENT_FLUSH_STOP:
885     {
886       GST_OBJECT_LOCK (mux);
887       mux->last_stop = GST_CLOCK_TIME_NONE;
888       GST_OBJECT_UNLOCK (mux);
889       break;
890     }
891     case GST_EVENT_SEGMENT:
892     {
893       GstRTPMuxPadPrivate *padpriv;
894
895       GST_OBJECT_LOCK (mux);
896       padpriv = gst_pad_get_element_private (pad);
897
898       if (padpriv) {
899         gst_event_copy_segment (event, &padpriv->segment);
900       }
901       GST_OBJECT_UNLOCK (mux);
902       break;
903     }
904     default:
905       break;
906   }
907
908   GST_OBJECT_LOCK (mux);
909   is_pad = (pad == mux->last_pad);
910   GST_OBJECT_UNLOCK (mux);
911
912   if (is_pad) {
913     return gst_pad_push_event (mux->srcpad, event);
914   } else {
915     gst_event_unref (event);
916     return TRUE;
917   }
918 }
919
920 static void
921 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
922 {
923
924   GST_OBJECT_LOCK (rtp_mux);
925
926   g_clear_object (&rtp_mux->last_pad);
927   rtp_mux->send_stream_start = TRUE;
928
929   if (rtp_mux->seqnum_offset == -1)
930     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
931   else
932     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
933   rtp_mux->seqnum = rtp_mux->seqnum_base;
934
935   if (rtp_mux->ts_offset == -1)
936     rtp_mux->ts_base = g_random_int ();
937   else
938     rtp_mux->ts_base = rtp_mux->ts_offset;
939
940   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
941
942   if (rtp_mux->ssrc_random) {
943     rtp_mux->have_ssrc = FALSE;
944   } else {
945     rtp_mux->current_ssrc = rtp_mux->ssrc;
946     rtp_mux->have_ssrc = TRUE;
947   }
948
949   GST_DEBUG_OBJECT (rtp_mux, "set timestamp-offset to %u", rtp_mux->ts_base);
950
951   GST_OBJECT_UNLOCK (rtp_mux);
952 }
953
954 static GstStateChangeReturn
955 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
956 {
957   GstRTPMux *rtp_mux;
958   GstStateChangeReturn ret;
959
960   rtp_mux = GST_RTP_MUX (element);
961
962   switch (transition) {
963     case GST_STATE_CHANGE_READY_TO_PAUSED:
964       gst_rtp_mux_ready_to_paused (rtp_mux);
965       break;
966     default:
967       break;
968   }
969
970   ret = GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
971       transition);
972
973   switch (transition) {
974     case GST_STATE_CHANGE_PAUSED_TO_READY:
975       g_clear_object (&rtp_mux->last_pad);
976       break;
977     default:
978       break;
979   }
980
981   return ret;
982 }
983
984 gboolean
985 gst_rtp_mux_plugin_init (GstPlugin * plugin)
986 {
987   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
988
989   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
990       GST_TYPE_RTP_MUX);
991 }