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