rtpmux: more porting
[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, 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_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   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 (gst_rtp_mux_parent_class)->dispose (object);
186 }
187
188 static gboolean
189 gst_rtp_mux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
190 {
191   GstRTPMux *rtp_mux = GST_RTP_MUX (parent);
192   GstRTPMuxClass *klass;
193   gboolean ret = FALSE;
194
195   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
196
197   ret = klass->src_event (rtp_mux, event);
198
199   return ret;
200 }
201
202 static gboolean
203 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
204 {
205   GstIterator *iter;
206   gboolean result = FALSE;
207   gboolean done = FALSE;
208
209   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
210
211   while (!done) {
212     GValue item = { 0, };
213
214     switch (gst_iterator_next (iter, &item)) {
215       case GST_ITERATOR_OK:
216         gst_event_ref (event);
217         result |= gst_pad_push_event (g_value_get_object (&item), event);
218         break;
219       case GST_ITERATOR_RESYNC:
220         gst_iterator_resync (iter);
221         result = FALSE;
222         break;
223       case GST_ITERATOR_ERROR:
224         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
225       case GST_ITERATOR_DONE:
226         done = TRUE;
227         break;
228     }
229   }
230   gst_iterator_free (iter);
231   gst_event_unref (event);
232
233   return result;
234 }
235
236 static void
237 gst_rtp_mux_init (GstRTPMux * rtp_mux)
238 {
239   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
240   GstSegment segment;
241
242   rtp_mux->srcpad =
243       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
244           "src"), "src");
245   gst_pad_set_event_function (rtp_mux->srcpad,
246       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
247   gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
248
249   gst_segment_init (&segment, GST_FORMAT_TIME);
250   gst_pad_push_event (rtp_mux->srcpad, gst_event_new_segment (&segment));
251
252   rtp_mux->ssrc = DEFAULT_SSRC;
253   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
254   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
255
256   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
257 }
258
259 static void
260 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
261 {
262   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
263
264   /* setup some pad functions */
265   gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
266   gst_pad_set_chain_list_function (sinkpad,
267       GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
268   gst_pad_set_event_function (sinkpad,
269       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
270   gst_pad_set_query_function (sinkpad,
271       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
272
273
274   gst_segment_init (&padpriv->segment, GST_FORMAT_TIME);
275
276   gst_pad_set_element_private (sinkpad, padpriv);
277
278   gst_pad_set_active (sinkpad, TRUE);
279   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
280 }
281
282 static GstPad *
283 gst_rtp_mux_request_new_pad (GstElement * element,
284     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
285 {
286   GstRTPMux *rtp_mux;
287   GstPad *newpad;
288
289   g_return_val_if_fail (templ != NULL, NULL);
290   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
291
292   rtp_mux = GST_RTP_MUX (element);
293
294   if (templ->direction != GST_PAD_SINK) {
295     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
296     return NULL;
297   }
298
299   newpad = gst_pad_new_from_template (templ, req_name);
300   if (newpad)
301     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
302   else
303     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
304
305   return newpad;
306 }
307
308 static void
309 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
310 {
311   GstRTPMuxPadPrivate *padpriv;
312
313   GST_OBJECT_LOCK (element);
314   padpriv = gst_pad_get_element_private (pad);
315   gst_pad_set_element_private (pad, NULL);
316   GST_OBJECT_UNLOCK (element);
317
318   gst_element_remove_pad (element, pad);
319
320   if (padpriv) {
321     gst_caps_replace (&padpriv->out_caps, NULL);
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, "Pushing packet size %d, seq=%d, ts=%u",
360       rtpbuffer->map.size, rtp_mux->seqnum,
361       gst_rtp_buffer_get_timestamp (rtpbuffer));
362
363   if (padpriv) {
364     if (padpriv->segment.format == GST_FORMAT_TIME)
365       GST_BUFFER_PTS (rtpbuffer->buffer) =
366           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
367           GST_BUFFER_PTS (rtpbuffer->buffer));
368   }
369
370   return TRUE;
371 }
372
373 struct BufferListData
374 {
375   GstRTPMux *rtp_mux;
376   GstRTPMuxPadPrivate *padpriv;
377   gboolean drop;
378 };
379
380 static gboolean
381 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
382 {
383   struct BufferListData *bd = user_data;
384   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
385
386   *buffer = gst_buffer_make_writable (*buffer);
387
388   gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
389
390   bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
391
392   gst_rtp_buffer_unmap (&rtpbuffer);
393
394   if (bd->drop)
395     return FALSE;
396
397   if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
398       GST_BUFFER_TIMESTAMP_IS_VALID (*buffer))
399     bd->rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (*buffer) +
400         GST_BUFFER_DURATION (*buffer);
401   else
402     bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
403
404   return TRUE;
405 }
406
407 static GstFlowReturn
408 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
409     GstBufferList * bufferlist)
410 {
411   GstRTPMux *rtp_mux;
412   GstFlowReturn ret;
413   GstRTPMuxPadPrivate *padpriv;
414   gboolean drop = TRUE;
415   struct BufferListData bd;
416
417   rtp_mux = GST_RTP_MUX (parent);
418
419   GST_OBJECT_LOCK (rtp_mux);
420
421   padpriv = gst_pad_get_element_private (pad);
422   if (!padpriv) {
423     GST_OBJECT_UNLOCK (rtp_mux);
424     ret = GST_FLOW_NOT_LINKED;
425     gst_buffer_list_unref (bufferlist);
426     goto out;
427   }
428
429   bd.rtp_mux = rtp_mux;
430   bd.padpriv = padpriv;
431   bd.drop = FALSE;
432
433   bufferlist = gst_buffer_list_make_writable (bufferlist);
434   gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
435
436   GST_OBJECT_UNLOCK (rtp_mux);
437
438   if (drop) {
439     gst_buffer_list_unref (bufferlist);
440     ret = GST_FLOW_OK;
441   } else {
442     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
443   }
444
445 out:
446
447   return ret;
448 }
449
450 static GstFlowReturn
451 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
452 {
453   GstRTPMux *rtp_mux;
454   GstFlowReturn ret;
455   GstRTPMuxPadPrivate *padpriv;
456   GstEvent *newseg_event = NULL;
457   gboolean drop;
458   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
459
460   rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
461
462   if (!gst_rtp_buffer_validate (buffer)) {
463     gst_buffer_unref (buffer);
464     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
465     return GST_FLOW_ERROR;
466   }
467
468   GST_OBJECT_LOCK (rtp_mux);
469   padpriv = gst_pad_get_element_private (pad);
470
471   if (!padpriv) {
472     GST_OBJECT_UNLOCK (rtp_mux);
473     gst_buffer_unref (buffer);
474     return GST_FLOW_NOT_LINKED;
475   }
476
477   buffer = gst_buffer_make_writable (buffer);
478
479   gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer);
480
481   drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
482
483   gst_rtp_buffer_unmap (&rtpbuffer);
484
485   if (!drop) {
486     if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
487         GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
488       rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (buffer) +
489           GST_BUFFER_DURATION (buffer);
490     else
491       rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
492   }
493
494   GST_OBJECT_UNLOCK (rtp_mux);
495
496   if (newseg_event)
497     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
498
499   if (drop) {
500     gst_buffer_unref (buffer);
501     ret = GST_FLOW_OK;
502   } else {
503     ret = gst_pad_push (rtp_mux->srcpad, buffer);
504   }
505
506   return ret;
507 }
508
509 static gboolean
510 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
511 {
512   GstStructure *structure;
513   gboolean ret = FALSE;
514   GstRTPMuxPadPrivate *padpriv;
515
516   structure = gst_caps_get_structure (caps, 0);
517
518   if (!structure)
519     return FALSE;
520
521   GST_OBJECT_LOCK (rtp_mux);
522   padpriv = gst_pad_get_element_private (pad);
523   if (padpriv &&
524       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
525     padpriv->have_clock_base = TRUE;
526   }
527   GST_OBJECT_UNLOCK (rtp_mux);
528
529   caps = gst_caps_copy (caps);
530
531   gst_caps_set_simple (caps,
532       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
533       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
534
535   GST_DEBUG_OBJECT (rtp_mux,
536       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
537   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
538
539   if (rtp_mux->ssrc == -1) {
540     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
541       rtp_mux->current_ssrc = g_value_get_uint
542           (gst_structure_get_value (structure, "ssrc"));
543     }
544   }
545
546   if (ret) {
547     GST_OBJECT_LOCK (rtp_mux);
548     padpriv = gst_pad_get_element_private (pad);
549     if (padpriv)
550       gst_caps_replace (&padpriv->out_caps, caps);
551     GST_OBJECT_UNLOCK (rtp_mux);
552   }
553   gst_caps_unref (caps);
554
555   return ret;
556 }
557
558 static void
559 clear_caps (GstCaps * caps, gboolean only_clock_rate)
560 {
561   gint i, j;
562
563   /* Lets only match on the clock-rate */
564   for (i = 0; i < gst_caps_get_size (caps); i++) {
565     GstStructure *s = gst_caps_get_structure (caps, i);
566
567     for (j = 0; j < gst_structure_n_fields (s); j++) {
568       const gchar *name = gst_structure_nth_field_name (s, j);
569
570       if (strcmp (name, "clock-rate") && (only_clock_rate ||
571               (strcmp (name, "ssrc")))) {
572         gst_structure_remove_field (s, name);
573         j--;
574       }
575     }
576   }
577 }
578
579 static gboolean
580 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
581 {
582   GstPad *mypad = user_data;
583   GstPad *pad = g_value_get_object (item);
584   GstCaps *peercaps;
585   const GstCaps *accumcaps;
586   GstCaps *intersect;
587
588   if (pad == mypad) {
589     return TRUE;
590   }
591
592   accumcaps = gst_value_get_caps (ret);
593   peercaps = gst_pad_peer_query_caps (pad, (GstCaps *) accumcaps);
594   if (!peercaps) {
595     g_warning ("no peercaps");
596     return TRUE;
597   }
598   peercaps = gst_caps_make_writable (peercaps);
599   clear_caps (peercaps, TRUE);
600
601   intersect = gst_caps_intersect (accumcaps, peercaps);
602
603   g_value_take_boxed (ret, intersect);
604   gst_caps_unref (peercaps);
605
606   return !gst_caps_is_empty (intersect);
607 }
608
609 static GstCaps *
610 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
611 {
612   GstCaps *caps = NULL;
613   GstIterator *iter = NULL;
614   GValue v = { 0 };
615   GstIteratorResult res;
616   GstCaps *peercaps;
617   GstCaps *othercaps = NULL;
618   GstCaps *filtered_caps;
619
620   peercaps = gst_pad_peer_query_caps (mux->srcpad, filter);
621
622   if (peercaps) {
623     othercaps = gst_caps_intersect (peercaps,
624         gst_pad_get_pad_template_caps (pad));
625     gst_caps_unref (peercaps);
626   } else {
627     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
628   }
629
630   if (filter) {
631     filtered_caps = gst_caps_intersect (othercaps, filter);
632     gst_caps_unref (othercaps);
633   } else {
634     filtered_caps = othercaps;
635   }
636
637   filtered_caps = gst_caps_make_writable (filtered_caps);
638   clear_caps (filtered_caps, FALSE);
639
640   g_value_init (&v, GST_TYPE_CAPS);
641
642   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
643   do {
644     gst_value_set_caps (&v, filtered_caps);
645     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
646   } while (res == GST_ITERATOR_RESYNC);
647   gst_iterator_free (iter);
648
649   caps = (GstCaps *) gst_value_get_caps (&v);
650
651   if (res == GST_ITERATOR_ERROR) {
652     gst_caps_unref (caps);
653     caps = gst_caps_new_empty ();
654   }
655
656   gst_caps_unref (filtered_caps);
657
658   return caps;
659 }
660
661 static gboolean
662 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
663 {
664   GstRTPMux *mux = GST_RTP_MUX (parent);
665   gboolean res = FALSE;
666
667   switch (GST_QUERY_TYPE (query)) {
668     case GST_QUERY_CAPS:
669     {
670       GstCaps *filter, *caps;
671
672       gst_query_parse_caps (query, &filter);
673       caps = gst_rtp_mux_getcaps (pad, mux, filter);
674       gst_query_set_caps_result (query, caps);
675       gst_caps_unref (caps);
676       res = TRUE;
677       break;
678     }
679     default:
680       res = gst_pad_query_default (pad, parent, query);
681       break;
682   }
683
684   return res;
685
686
687 }
688
689
690 static void
691 gst_rtp_mux_get_property (GObject * object,
692     guint prop_id, GValue * value, GParamSpec * pspec)
693 {
694   GstRTPMux *rtp_mux;
695
696   rtp_mux = GST_RTP_MUX (object);
697
698   switch (prop_id) {
699     case PROP_TIMESTAMP_OFFSET:
700       g_value_set_int (value, rtp_mux->ts_offset);
701       break;
702     case PROP_SEQNUM_OFFSET:
703       g_value_set_int (value, rtp_mux->seqnum_offset);
704       break;
705     case PROP_SEQNUM:
706       GST_OBJECT_LOCK (rtp_mux);
707       g_value_set_uint (value, rtp_mux->seqnum);
708       GST_OBJECT_UNLOCK (rtp_mux);
709       break;
710     case PROP_SSRC:
711       g_value_set_uint (value, rtp_mux->ssrc);
712       break;
713     default:
714       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
715       break;
716   }
717 }
718
719 static void
720 gst_rtp_mux_set_property (GObject * object,
721     guint prop_id, const GValue * value, GParamSpec * pspec)
722 {
723   GstRTPMux *rtp_mux;
724
725   rtp_mux = GST_RTP_MUX (object);
726
727   switch (prop_id) {
728     case PROP_TIMESTAMP_OFFSET:
729       rtp_mux->ts_offset = g_value_get_int (value);
730       break;
731     case PROP_SEQNUM_OFFSET:
732       rtp_mux->seqnum_offset = g_value_get_int (value);
733       break;
734     case PROP_SSRC:
735       rtp_mux->ssrc = g_value_get_uint (value);
736       break;
737     default:
738       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739       break;
740   }
741 }
742
743 static gboolean
744 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
745 {
746
747   GstRTPMux *mux;
748   gboolean ret = FALSE;
749   gboolean forward = TRUE;
750
751   mux = GST_RTP_MUX (parent);
752
753   switch (GST_EVENT_TYPE (event)) {
754     case GST_EVENT_CAPS:
755     {
756       GstCaps *caps;
757
758       gst_event_parse_caps (event, &caps);
759       ret = gst_rtp_mux_setcaps (pad, mux, caps);
760       forward = FALSE;
761       break;
762     }
763     case GST_EVENT_FLUSH_STOP:
764     {
765       GST_OBJECT_LOCK (mux);
766       mux->last_stop = GST_CLOCK_TIME_NONE;
767       GST_OBJECT_UNLOCK (mux);
768       break;
769     }
770     case GST_EVENT_SEGMENT:
771     {
772       GstRTPMuxPadPrivate *padpriv;
773
774       GST_OBJECT_LOCK (mux);
775       padpriv = gst_pad_get_element_private (pad);
776
777       if (padpriv) {
778         gst_event_copy_segment (event, &padpriv->segment);
779       }
780       GST_OBJECT_UNLOCK (mux);
781       gst_event_unref (event);
782       forward = FALSE;
783       ret = TRUE;
784       break;
785     }
786     default:
787       break;
788   }
789
790   if (forward)
791     ret = gst_pad_push_event (mux->srcpad, event);
792
793   return ret;
794 }
795
796 static void
797 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
798 {
799
800   GST_OBJECT_LOCK (rtp_mux);
801
802   if (rtp_mux->ssrc == -1)
803     rtp_mux->current_ssrc = g_random_int ();
804   else
805     rtp_mux->current_ssrc = rtp_mux->ssrc;
806
807   if (rtp_mux->seqnum_offset == -1)
808     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
809   else
810     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
811   rtp_mux->seqnum = rtp_mux->seqnum_base;
812
813   if (rtp_mux->ts_offset == -1)
814     rtp_mux->ts_base = g_random_int ();
815   else
816     rtp_mux->ts_base = rtp_mux->ts_offset;
817
818   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
819
820   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
821
822   GST_OBJECT_UNLOCK (rtp_mux);
823 }
824
825 static GstStateChangeReturn
826 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
827 {
828   GstRTPMux *rtp_mux;
829
830   rtp_mux = GST_RTP_MUX (element);
831
832   switch (transition) {
833     case GST_STATE_CHANGE_NULL_TO_READY:
834       break;
835     case GST_STATE_CHANGE_READY_TO_PAUSED:
836       gst_rtp_mux_ready_to_paused (rtp_mux);
837       break;
838     case GST_STATE_CHANGE_PAUSED_TO_READY:
839       break;
840     default:
841       break;
842   }
843
844   return GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
845       transition);
846 }
847
848 gboolean
849 gst_rtp_mux_plugin_init (GstPlugin * plugin)
850 {
851   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
852
853   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
854       GST_TYPE_RTP_MUX);
855 }