rtpmux: Improve documentation
[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_%d",
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 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
111
112 static void
113 gst_rtp_mux_base_init (gpointer g_class)
114 {
115   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
116
117   gst_element_class_add_pad_template (element_class,
118       gst_static_pad_template_get (&src_factory));
119   gst_element_class_add_pad_template (element_class,
120       gst_static_pad_template_get (&sink_factory));
121
122   gst_element_class_set_details_simple (element_class, "RTP muxer",
123       "Codec/Muxer",
124       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
125 }
126
127 static void
128 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
129 {
130   GObjectClass *gobject_class;
131   GstElementClass *gstelement_class;
132
133   gobject_class = (GObjectClass *) klass;
134   gstelement_class = (GstElementClass *) klass;
135
136   gobject_class->get_property = gst_rtp_mux_get_property;
137   gobject_class->set_property = gst_rtp_mux_set_property;
138   gobject_class->dispose = gst_rtp_mux_dispose;
139
140   g_object_class_install_property (G_OBJECT_CLASS (klass),
141       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
142           "Timestamp Offset",
143           "Offset to add to all outgoing timestamps (-1 = random)", -1,
144           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
145   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
146       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
147           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
148           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
149   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
150       g_param_spec_uint ("seqnum", "Sequence number",
151           "The RTP sequence number of the last processed packet",
152           0, G_MAXUINT, 0, G_PARAM_READABLE));
153   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
154       g_param_spec_uint ("ssrc", "SSRC",
155           "The SSRC of the packets (-1 == random)",
156           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
157
158   gstelement_class->request_new_pad =
159       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
160   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
161   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
162 }
163
164 static void
165 gst_rtp_mux_dispose (GObject * object)
166 {
167   GList *item;
168
169 restart:
170   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
171     GstPad *pad = GST_PAD (item->data);
172     if (GST_PAD_IS_SINK (pad)) {
173       gst_element_release_request_pad (GST_ELEMENT (object), pad);
174       goto restart;
175     }
176   }
177
178   G_OBJECT_CLASS (parent_class)->dispose (object);
179 }
180
181 static gboolean
182 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
183 {
184   GstElement *rtp_mux;
185   GstIterator *iter;
186   GstPad *sinkpad;
187   gboolean result = FALSE;
188   gboolean done = FALSE;
189
190   rtp_mux = gst_pad_get_parent_element (pad);
191   g_return_val_if_fail (rtp_mux != NULL, FALSE);
192
193   iter = gst_element_iterate_sink_pads (rtp_mux);
194
195   while (!done) {
196     switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
197       case GST_ITERATOR_OK:
198         gst_event_ref (event);
199         result |= gst_pad_push_event (sinkpad, event);
200         gst_object_unref (sinkpad);
201         break;
202       case GST_ITERATOR_RESYNC:
203         gst_iterator_resync (iter);
204         result = FALSE;
205         break;
206       case GST_ITERATOR_ERROR:
207         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
208       case GST_ITERATOR_DONE:
209         done = TRUE;
210         break;
211     }
212   }
213   gst_iterator_free (iter);
214   gst_object_unref (rtp_mux);
215   gst_event_unref (event);
216
217   return result;
218 }
219
220 static void
221 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
222 {
223   GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
224
225   object->srcpad =
226       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
227           "src"), "src");
228   gst_pad_set_event_function (object->srcpad,
229       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
230   gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
231
232   object->ssrc = DEFAULT_SSRC;
233   object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
234   object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
235
236   object->segment_pending = TRUE;
237 }
238
239 static void
240 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
241 {
242   GstRTPMuxClass *klass;
243   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
244
245   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
246
247   /* setup some pad functions */
248   gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
249   gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
250   gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
251   gst_pad_set_chain_list_function (sinkpad,
252       GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
253   gst_pad_set_event_function (sinkpad,
254       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
255
256
257   gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
258
259   gst_pad_set_element_private (sinkpad, padpriv);
260
261   gst_pad_set_active (sinkpad, TRUE);
262   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
263 }
264
265 static GstPad *
266 gst_rtp_mux_request_new_pad (GstElement * element,
267     GstPadTemplate * templ, const gchar * req_name)
268 {
269   GstRTPMux *rtp_mux;
270   GstPad *newpad;
271
272   g_return_val_if_fail (templ != NULL, NULL);
273   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
274
275   rtp_mux = GST_RTP_MUX (element);
276
277   if (templ->direction != GST_PAD_SINK) {
278     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
279     return NULL;
280   }
281
282   newpad = gst_pad_new_from_template (templ, req_name);
283   if (newpad)
284     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
285   else
286     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
287
288   return newpad;
289 }
290
291 static void
292 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
293 {
294   GstRTPMuxPadPrivate *padpriv;
295
296   GST_OBJECT_LOCK (element);
297   padpriv = gst_pad_get_element_private (pad);
298   gst_pad_set_element_private (pad, NULL);
299   GST_OBJECT_UNLOCK (element);
300
301   gst_element_remove_pad (element, pad);
302
303   if (padpriv) {
304     gst_caps_replace (&padpriv->out_caps, NULL);
305     g_slice_free (GstRTPMuxPadPrivate, padpriv);
306   }
307 }
308
309 /* Put our own clock-base on the buffer */
310 static void
311 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
312     GstRTPMuxPadPrivate * padpriv, GstBuffer * buffer)
313 {
314   guint32 ts;
315   guint32 sink_ts_base = 0;
316
317   if (padpriv && padpriv->have_clock_base)
318     sink_ts_base = padpriv->clock_base;
319
320   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
321   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
322       gst_rtp_buffer_get_timestamp (buffer), ts);
323   gst_rtp_buffer_set_timestamp (buffer, ts);
324 }
325
326 static gboolean
327 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
328     GstBuffer * buffer)
329 {
330   GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
331
332   if (klass->accept_buffer_locked)
333     if (!klass->accept_buffer_locked (rtp_mux, padpriv, buffer))
334       return FALSE;
335
336   rtp_mux->seqnum++;
337   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
338
339   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
340   gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, buffer);
341   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
342       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
343       gst_rtp_buffer_get_timestamp (buffer));
344
345   if (padpriv) {
346     gst_buffer_set_caps (buffer, padpriv->out_caps);
347     if (padpriv->segment.format == GST_FORMAT_TIME)
348       GST_BUFFER_TIMESTAMP (buffer) =
349           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
350           GST_BUFFER_TIMESTAMP (buffer));
351   }
352
353   return TRUE;
354 }
355
356 static GstFlowReturn
357 gst_rtp_mux_chain_list (GstPad * pad, GstBufferList * bufferlist)
358 {
359   GstRTPMux *rtp_mux;
360   GstFlowReturn ret;
361   GstBufferListIterator *it;
362   GstRTPMuxPadPrivate *padpriv;
363   GstEvent *newseg_event = NULL;
364   gboolean drop = TRUE;
365
366   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
367
368   if (!gst_rtp_buffer_list_validate (bufferlist)) {
369     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
370     gst_object_unref (rtp_mux);
371     return GST_FLOW_ERROR;
372   }
373
374   GST_OBJECT_LOCK (rtp_mux);
375
376   padpriv = gst_pad_get_element_private (pad);
377   if (!padpriv) {
378     GST_OBJECT_UNLOCK (rtp_mux);
379     ret = GST_FLOW_NOT_LINKED;
380     gst_buffer_list_unref (bufferlist);
381     goto out;
382   }
383
384   bufferlist = gst_buffer_list_make_writable (bufferlist);
385   it = gst_buffer_list_iterate (bufferlist);
386   while (gst_buffer_list_iterator_next_group (it)) {
387     GstBuffer *rtpbuf;
388
389     rtpbuf = gst_buffer_list_iterator_next (it);
390     rtpbuf = gst_buffer_make_writable (rtpbuf);
391
392     drop = !process_buffer_locked (rtp_mux, padpriv, rtpbuf);
393
394     if (drop)
395       break;
396
397     gst_buffer_list_iterator_take (it, rtpbuf);
398   }
399   gst_buffer_list_iterator_free (it);
400
401   if (!drop && rtp_mux->segment_pending) {
402     /*
403      * We set the start at 0, because we re-timestamps to the running time
404      */
405     newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
406         GST_FORMAT_TIME, 0, -1, 0);
407
408     rtp_mux->segment_pending = FALSE;
409   }
410
411   GST_OBJECT_UNLOCK (rtp_mux);
412
413   if (newseg_event)
414     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
415
416   if (drop) {
417     gst_buffer_list_unref (bufferlist);
418     ret = GST_FLOW_OK;
419   } else {
420     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
421   }
422
423 out:
424
425   gst_object_unref (rtp_mux);
426
427   return ret;
428 }
429
430 static GstFlowReturn
431 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
432 {
433   GstRTPMux *rtp_mux;
434   GstFlowReturn ret;
435   GstRTPMuxPadPrivate *padpriv;
436   GstEvent *newseg_event = NULL;
437   gboolean drop;
438
439   rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
440
441   if (!gst_rtp_buffer_validate (buffer)) {
442     gst_buffer_unref (buffer);
443     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
444     return GST_FLOW_ERROR;
445   }
446
447   GST_OBJECT_LOCK (rtp_mux);
448   padpriv = gst_pad_get_element_private (pad);
449
450   if (!padpriv) {
451     GST_OBJECT_UNLOCK (rtp_mux);
452     gst_buffer_unref (buffer);
453     return GST_FLOW_NOT_LINKED;
454   }
455
456   buffer = gst_buffer_make_writable (buffer);
457
458   drop = !process_buffer_locked (rtp_mux, padpriv, buffer);
459
460   if (!drop && rtp_mux->segment_pending) {
461     /*
462      * We set the start at 0, because we re-timestamps to the running time
463      */
464     newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
465         GST_FORMAT_TIME, 0, -1, 0);
466
467     rtp_mux->segment_pending = FALSE;
468   }
469   GST_OBJECT_UNLOCK (rtp_mux);
470
471   if (newseg_event)
472     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
473
474   if (drop) {
475     gst_buffer_unref (buffer);
476     ret = GST_FLOW_OK;
477   } else {
478     ret = gst_pad_push (rtp_mux->srcpad, buffer);
479   }
480
481   return ret;
482 }
483
484 static gboolean
485 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
486 {
487   GstRTPMux *rtp_mux;
488   GstStructure *structure;
489   gboolean ret = FALSE;
490   GstRTPMuxPadPrivate *padpriv;
491
492   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
493
494   structure = gst_caps_get_structure (caps, 0);
495
496   if (!structure)
497     goto out;
498
499   GST_OBJECT_LOCK (rtp_mux);
500   padpriv = gst_pad_get_element_private (pad);
501   if (padpriv &&
502       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
503     padpriv->have_clock_base = TRUE;
504   }
505   GST_OBJECT_UNLOCK (rtp_mux);
506
507   caps = gst_caps_copy (caps);
508
509   gst_caps_set_simple (caps,
510       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
511       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
512
513   GST_DEBUG_OBJECT (rtp_mux,
514       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
515   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
516
517   if (rtp_mux->ssrc == -1) {
518     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
519       rtp_mux->current_ssrc = g_value_get_uint
520           (gst_structure_get_value (structure, "ssrc"));
521     }
522   }
523
524   if (ret) {
525     GST_OBJECT_LOCK (rtp_mux);
526     padpriv = gst_pad_get_element_private (pad);
527     if (padpriv)
528       gst_caps_replace (&padpriv->out_caps, caps);
529     GST_OBJECT_UNLOCK (rtp_mux);
530   }
531   gst_caps_unref (caps);
532
533 out:
534   gst_object_unref (rtp_mux);
535
536   return ret;
537 }
538
539 static void
540 clear_caps (GstCaps * caps, gboolean only_clock_rate)
541 {
542   gint i, j;
543
544   /* Lets only match on the clock-rate */
545   for (i = 0; i < gst_caps_get_size (caps); i++) {
546     GstStructure *s = gst_caps_get_structure (caps, i);
547
548     for (j = 0; j < gst_structure_n_fields (s); j++) {
549       const gchar *name = gst_structure_nth_field_name (s, j);
550
551       if (strcmp (name, "clock-rate") && (only_clock_rate ||
552               (strcmp (name, "ssrc")))) {
553         gst_structure_remove_field (s, name);
554         j--;
555       }
556     }
557   }
558 }
559
560 static gboolean
561 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
562 {
563   GstPad *mypad = user_data;
564   GstPad *pad = item;
565   GstCaps *peercaps;
566   GstCaps *othercaps;
567   const GstCaps *accumcaps;
568   GstCaps *intersect;
569
570   if (pad == mypad) {
571     gst_object_unref (pad);
572     return TRUE;
573   }
574
575   peercaps = gst_pad_peer_get_caps (pad);
576   if (!peercaps) {
577     gst_object_unref (pad);
578     return TRUE;
579   }
580
581   othercaps = gst_caps_intersect (peercaps,
582       gst_pad_get_pad_template_caps (pad));
583   gst_caps_unref (peercaps);
584
585   accumcaps = gst_value_get_caps (ret);
586
587   clear_caps (othercaps, TRUE);
588
589   intersect = gst_caps_intersect (accumcaps, othercaps);
590
591   g_value_take_boxed (ret, intersect);
592
593   gst_caps_unref (othercaps);
594   gst_object_unref (pad);
595
596   return !gst_caps_is_empty (intersect);
597 }
598
599 static GstCaps *
600 gst_rtp_mux_getcaps (GstPad * pad)
601 {
602   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
603   GstCaps *caps = NULL;
604   GstIterator *iter = NULL;
605   GValue v = { 0 };
606   GstIteratorResult res;
607   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
608   GstCaps *othercaps = NULL;
609
610   if (peercaps) {
611     othercaps = gst_caps_intersect (peercaps,
612         gst_pad_get_pad_template_caps (pad));
613     gst_caps_unref (peercaps);
614   } else {
615     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
616   }
617
618   clear_caps (othercaps, FALSE);
619
620   g_value_init (&v, GST_TYPE_CAPS);
621
622   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
623   do {
624     gst_value_set_caps (&v, othercaps);
625     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
626   } while (res == GST_ITERATOR_RESYNC);
627   gst_iterator_free (iter);
628
629   caps = (GstCaps *) gst_value_get_caps (&v);
630
631   if (res == GST_ITERATOR_ERROR) {
632     gst_caps_unref (caps);
633     caps = gst_caps_new_empty ();
634   }
635
636   if (othercaps)
637     gst_caps_unref (othercaps);
638   gst_object_unref (mux);
639
640   return caps;
641 }
642
643
644 static void
645 gst_rtp_mux_get_property (GObject * object,
646     guint prop_id, GValue * value, GParamSpec * pspec)
647 {
648   GstRTPMux *rtp_mux;
649
650   rtp_mux = GST_RTP_MUX (object);
651
652   switch (prop_id) {
653     case PROP_TIMESTAMP_OFFSET:
654       g_value_set_int (value, rtp_mux->ts_offset);
655       break;
656     case PROP_SEQNUM_OFFSET:
657       g_value_set_int (value, rtp_mux->seqnum_offset);
658       break;
659     case PROP_SEQNUM:
660       GST_OBJECT_LOCK (rtp_mux);
661       g_value_set_uint (value, rtp_mux->seqnum);
662       GST_OBJECT_UNLOCK (rtp_mux);
663       break;
664     case PROP_SSRC:
665       g_value_set_uint (value, rtp_mux->ssrc);
666       break;
667     default:
668       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
669       break;
670   }
671 }
672
673 static void
674 gst_rtp_mux_set_property (GObject * object,
675     guint prop_id, const GValue * value, GParamSpec * pspec)
676 {
677   GstRTPMux *rtp_mux;
678
679   rtp_mux = GST_RTP_MUX (object);
680
681   switch (prop_id) {
682     case PROP_TIMESTAMP_OFFSET:
683       rtp_mux->ts_offset = g_value_get_int (value);
684       break;
685     case PROP_SEQNUM_OFFSET:
686       rtp_mux->seqnum_offset = g_value_get_int (value);
687       break;
688     case PROP_SSRC:
689       rtp_mux->ssrc = g_value_get_uint (value);
690       break;
691     default:
692       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693       break;
694   }
695 }
696
697 static gboolean
698 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
699 {
700
701   GstRTPMux *mux;
702   gboolean ret = FALSE;
703   gboolean forward = TRUE;
704
705   mux = GST_RTP_MUX (gst_pad_get_parent (pad));
706
707   switch (GST_EVENT_TYPE (event)) {
708     case GST_EVENT_FLUSH_STOP:
709     {
710       GstRTPMuxPadPrivate *padpriv;
711
712       GST_OBJECT_LOCK (mux);
713       mux->segment_pending = TRUE;
714       padpriv = gst_pad_get_element_private (pad);
715       if (padpriv)
716         gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
717       GST_OBJECT_UNLOCK (mux);
718     }
719       break;
720     case GST_EVENT_NEWSEGMENT:
721     {
722       gboolean update;
723       gdouble rate, applied_rate;
724       GstFormat format;
725       gint64 start, stop, position;
726       GstRTPMuxPadPrivate *padpriv;
727
728       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
729           &format, &start, &stop, &position);
730
731       GST_OBJECT_LOCK (mux);
732       padpriv = gst_pad_get_element_private (pad);
733
734       if (padpriv) {
735         if (format == GST_FORMAT_TIME)
736           gst_segment_set_newsegment_full (&padpriv->segment, update,
737               rate, applied_rate, format, start, stop, position);
738         else
739           gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
740       }
741       GST_OBJECT_UNLOCK (mux);
742       gst_event_unref (event);
743       forward = FALSE;
744       ret = TRUE;
745       break;
746     }
747     default:
748       break;
749   }
750
751   if (forward)
752     ret = gst_pad_push_event (mux->srcpad, event);
753
754   gst_object_unref (mux);
755   return ret;
756 }
757
758
759 static void
760 clear_segment (gpointer data, gpointer user_data)
761 {
762   GstPad *pad = data;
763   GstRTPMux *mux = user_data;
764   GstRTPMuxPadPrivate *padpriv;
765
766   GST_OBJECT_LOCK (mux);
767   padpriv = gst_pad_get_element_private (pad);
768   if (padpriv)
769     gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
770   GST_OBJECT_UNLOCK (mux);
771
772   gst_object_unref (pad);
773 }
774
775
776 static void
777 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
778 {
779   GstIterator *iter;
780
781   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
782   while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
783       GST_ITERATOR_RESYNC);
784   gst_iterator_free (iter);
785
786   GST_OBJECT_LOCK (rtp_mux);
787   rtp_mux->segment_pending = TRUE;
788
789   if (rtp_mux->ssrc == -1)
790     rtp_mux->current_ssrc = g_random_int ();
791   else
792     rtp_mux->current_ssrc = rtp_mux->ssrc;
793
794   if (rtp_mux->seqnum_offset == -1)
795     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
796   else
797     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
798   rtp_mux->seqnum = rtp_mux->seqnum_base;
799
800   if (rtp_mux->ts_offset == -1)
801     rtp_mux->ts_base = g_random_int ();
802   else
803     rtp_mux->ts_base = rtp_mux->ts_offset;
804
805   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
806
807   GST_OBJECT_UNLOCK (rtp_mux);
808 }
809
810 static GstStateChangeReturn
811 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
812 {
813   GstRTPMux *rtp_mux;
814
815   rtp_mux = GST_RTP_MUX (element);
816
817   switch (transition) {
818     case GST_STATE_CHANGE_NULL_TO_READY:
819       break;
820     case GST_STATE_CHANGE_READY_TO_PAUSED:
821       gst_rtp_mux_ready_to_paused (rtp_mux);
822       break;
823     case GST_STATE_CHANGE_PAUSED_TO_READY:
824       break;
825     default:
826       break;
827   }
828
829   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
830 }
831
832 gboolean
833 gst_rtp_mux_plugin_init (GstPlugin * plugin)
834 {
835   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
836
837   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
838       GST_TYPE_RTP_MUX);
839 }