rtpdtmfmux: Remove stream-lock event handling
[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 gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
76 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
77 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event);
78
79 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
80     element, GstStateChange transition);
81
82 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
83     const GValue * value, GParamSpec * pspec);
84 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86 static void gst_rtp_mux_dispose (GObject * object);
87
88 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
89
90 static void
91 gst_rtp_mux_base_init (gpointer g_class)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94
95   gst_element_class_add_pad_template (element_class,
96       gst_static_pad_template_get (&src_factory));
97   gst_element_class_add_pad_template (element_class,
98       gst_static_pad_template_get (&sink_factory));
99
100   gst_element_class_set_details_simple (element_class, "RTP muxer",
101       "Codec/Muxer",
102       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
103 }
104
105 static void
106 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
107 {
108   GObjectClass *gobject_class;
109   GstElementClass *gstelement_class;
110
111   gobject_class = (GObjectClass *) klass;
112   gstelement_class = (GstElementClass *) klass;
113
114   gobject_class->get_property = gst_rtp_mux_get_property;
115   gobject_class->set_property = gst_rtp_mux_set_property;
116   gobject_class->dispose = gst_rtp_mux_dispose;
117
118   g_object_class_install_property (G_OBJECT_CLASS (klass),
119       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
120           "Timestamp Offset",
121           "Offset to add to all outgoing timestamps (-1 = random)", -1,
122           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
123   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
124       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
125           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
126           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
127   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
128       g_param_spec_uint ("seqnum", "Sequence number",
129           "The RTP sequence number of the last processed packet",
130           0, G_MAXUINT, 0, G_PARAM_READABLE));
131   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
132       g_param_spec_uint ("ssrc", "SSRC",
133           "The SSRC of the packets (-1 == random)",
134           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
135
136   gstelement_class->request_new_pad =
137       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
138   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
139   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
140
141   klass->chain_func = gst_rtp_mux_chain;
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   if (klass->chain_func)
231     gst_pad_set_chain_function (sinkpad, klass->chain_func);
232   gst_pad_set_event_function (sinkpad,
233       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
234
235
236   gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
237
238   gst_pad_set_element_private (sinkpad, padpriv);
239
240   gst_pad_set_active (sinkpad, TRUE);
241   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
242 }
243
244 static GstPad *
245 gst_rtp_mux_request_new_pad (GstElement * element,
246     GstPadTemplate * templ, const gchar * req_name)
247 {
248   GstRTPMux *rtp_mux;
249   GstPad *newpad;
250
251   g_return_val_if_fail (templ != NULL, NULL);
252   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
253
254   rtp_mux = GST_RTP_MUX (element);
255
256   if (templ->direction != GST_PAD_SINK) {
257     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
258     return NULL;
259   }
260
261   newpad = gst_pad_new_from_template (templ, req_name);
262   if (newpad)
263     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
264   else
265     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
266
267   return newpad;
268 }
269
270 static void
271 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
272 {
273   GstRTPMuxPadPrivate *padpriv;
274
275   GST_OBJECT_LOCK (element);
276   padpriv = gst_pad_get_element_private (pad);
277   gst_pad_set_element_private (pad, NULL);
278   GST_OBJECT_UNLOCK (element);
279
280   gst_element_remove_pad (element, pad);
281
282   if (padpriv) {
283     gst_caps_replace (&padpriv->out_caps, NULL);
284     g_slice_free (GstRTPMuxPadPrivate, padpriv);
285   }
286 }
287
288 /* Put our own clock-base on the buffer */
289 static void
290 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
291     GstBuffer * buffer)
292 {
293   guint32 ts;
294   guint32 sink_ts_base = 0;
295   GstRTPMuxPadPrivate *padpriv;
296
297
298   GST_OBJECT_LOCK (rtp_mux);
299   padpriv = gst_pad_get_element_private (pad);
300   if (padpriv && padpriv->have_clock_base)
301     sink_ts_base = padpriv->clock_base;
302   GST_OBJECT_UNLOCK (rtp_mux);
303
304   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
305   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
306       gst_rtp_buffer_get_timestamp (buffer), ts);
307   gst_rtp_buffer_set_timestamp (buffer, ts);
308 }
309
310 static GstFlowReturn
311 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
312 {
313   GstRTPMux *rtp_mux;
314   GstFlowReturn ret;
315   GstRTPMuxPadPrivate *padpriv;
316   GstEvent *newseg_event = NULL;
317
318   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
319
320   if (!gst_rtp_buffer_validate (buffer)) {
321     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
322     gst_object_unref (rtp_mux);
323     return GST_FLOW_ERROR;
324   }
325
326   buffer = gst_buffer_make_writable (buffer);
327
328   GST_OBJECT_LOCK (rtp_mux);
329   rtp_mux->seqnum++;
330   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
331   padpriv = gst_pad_get_element_private (pad);
332   if (padpriv) {
333     gst_buffer_set_caps (buffer, padpriv->out_caps);
334     if (padpriv->segment.format == GST_FORMAT_TIME)
335       GST_BUFFER_TIMESTAMP (buffer) =
336           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
337           GST_BUFFER_TIMESTAMP (buffer));
338   }
339
340   if (rtp_mux->segment_pending) {
341     /*
342      * We set the start at 0, because we re-timestamps to the running time
343      */
344     newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
345         GST_FORMAT_TIME, 0, -1, 0);
346
347     rtp_mux->segment_pending = FALSE;
348   }
349   GST_OBJECT_UNLOCK (rtp_mux);
350
351   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
352   gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
353   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
354       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
355       gst_rtp_buffer_get_timestamp (buffer));
356
357   if (newseg_event)
358     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
359
360   if (!padpriv) {
361     ret = GST_FLOW_NOT_LINKED;
362     gst_buffer_unref (buffer);
363     goto out;
364   }
365
366   ret = gst_pad_push (rtp_mux->srcpad, buffer);
367
368 out:
369
370   gst_object_unref (rtp_mux);
371   return ret;
372 }
373
374 static gboolean
375 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
376 {
377   GstRTPMux *rtp_mux;
378   GstStructure *structure;
379   gboolean ret = FALSE;
380   GstRTPMuxPadPrivate *padpriv;
381
382   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
383
384   structure = gst_caps_get_structure (caps, 0);
385
386   if (!structure)
387     goto out;
388
389   GST_OBJECT_LOCK (rtp_mux);
390   padpriv = gst_pad_get_element_private (pad);
391   if (padpriv &&
392       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
393     padpriv->have_clock_base = TRUE;
394   }
395   GST_OBJECT_UNLOCK (rtp_mux);
396
397   caps = gst_caps_copy (caps);
398
399   gst_caps_set_simple (caps,
400       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
401       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
402
403   GST_DEBUG_OBJECT (rtp_mux,
404       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
405   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
406
407   if (rtp_mux->ssrc == -1) {
408     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
409       rtp_mux->current_ssrc = g_value_get_uint
410           (gst_structure_get_value (structure, "ssrc"));
411     }
412   }
413
414   if (ret) {
415     GST_OBJECT_LOCK (rtp_mux);
416     padpriv = gst_pad_get_element_private (pad);
417     if (padpriv)
418       gst_caps_replace (&padpriv->out_caps, caps);
419     GST_OBJECT_UNLOCK (rtp_mux);
420   }
421   gst_caps_unref (caps);
422
423 out:
424   gst_object_unref (rtp_mux);
425
426   return ret;
427 }
428
429 static void
430 clear_caps (GstCaps * caps, gboolean only_clock_rate)
431 {
432   gint i, j;
433
434   /* Lets only match on the clock-rate */
435   for (i = 0; i < gst_caps_get_size (caps); i++) {
436     GstStructure *s = gst_caps_get_structure (caps, i);
437
438     for (j = 0; j < gst_structure_n_fields (s); j++) {
439       const gchar *name = gst_structure_nth_field_name (s, j);
440
441       if (strcmp (name, "clock-rate") && (only_clock_rate ||
442               (strcmp (name, "ssrc")))) {
443         gst_structure_remove_field (s, name);
444         j--;
445       }
446     }
447   }
448 }
449
450 static gboolean
451 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
452 {
453   GstPad *mypad = user_data;
454   GstPad *pad = item;
455   GstCaps *peercaps;
456   GstCaps *othercaps;
457   const GstCaps *accumcaps;
458   GstCaps *intersect;
459
460   if (pad == mypad) {
461     gst_object_unref (pad);
462     return TRUE;
463   }
464
465   peercaps = gst_pad_peer_get_caps (pad);
466   if (!peercaps) {
467     gst_object_unref (pad);
468     return TRUE;
469   }
470
471   othercaps = gst_caps_intersect (peercaps,
472       gst_pad_get_pad_template_caps (pad));
473   gst_caps_unref (peercaps);
474
475   accumcaps = gst_value_get_caps (ret);
476
477   clear_caps (othercaps, TRUE);
478
479   intersect = gst_caps_intersect (accumcaps, othercaps);
480
481   g_value_take_boxed (ret, intersect);
482
483   gst_caps_unref (othercaps);
484   gst_object_unref (pad);
485
486   return !gst_caps_is_empty (intersect);
487 }
488
489 static GstCaps *
490 gst_rtp_mux_getcaps (GstPad * pad)
491 {
492   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
493   GstCaps *caps = NULL;
494   GstIterator *iter = NULL;
495   GValue v = { 0 };
496   GstIteratorResult res;
497   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
498   GstCaps *othercaps = NULL;
499
500   if (peercaps) {
501     othercaps = gst_caps_intersect (peercaps,
502         gst_pad_get_pad_template_caps (pad));
503     gst_caps_unref (peercaps);
504   } else {
505     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
506   }
507
508   clear_caps (othercaps, FALSE);
509
510   g_value_init (&v, GST_TYPE_CAPS);
511
512   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
513   do {
514     gst_value_set_caps (&v, othercaps);
515     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
516   } while (res == GST_ITERATOR_RESYNC);
517   gst_iterator_free (iter);
518
519   caps = (GstCaps *) gst_value_get_caps (&v);
520
521   if (res == GST_ITERATOR_ERROR) {
522     gst_caps_unref (caps);
523     caps = gst_caps_new_empty ();
524   }
525
526   if (othercaps)
527     gst_caps_unref (othercaps);
528   gst_object_unref (mux);
529
530   return caps;
531 }
532
533
534 static void
535 gst_rtp_mux_get_property (GObject * object,
536     guint prop_id, GValue * value, GParamSpec * pspec)
537 {
538   GstRTPMux *rtp_mux;
539
540   rtp_mux = GST_RTP_MUX (object);
541
542   switch (prop_id) {
543     case PROP_TIMESTAMP_OFFSET:
544       g_value_set_int (value, rtp_mux->ts_offset);
545       break;
546     case PROP_SEQNUM_OFFSET:
547       g_value_set_int (value, rtp_mux->seqnum_offset);
548       break;
549     case PROP_SEQNUM:
550       GST_OBJECT_LOCK (rtp_mux);
551       g_value_set_uint (value, rtp_mux->seqnum);
552       GST_OBJECT_UNLOCK (rtp_mux);
553       break;
554     case PROP_SSRC:
555       g_value_set_uint (value, rtp_mux->ssrc);
556       break;
557     default:
558       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559       break;
560   }
561 }
562
563 static void
564 gst_rtp_mux_set_property (GObject * object,
565     guint prop_id, const GValue * value, GParamSpec * pspec)
566 {
567   GstRTPMux *rtp_mux;
568
569   rtp_mux = GST_RTP_MUX (object);
570
571   switch (prop_id) {
572     case PROP_TIMESTAMP_OFFSET:
573       rtp_mux->ts_offset = g_value_get_int (value);
574       break;
575     case PROP_SEQNUM_OFFSET:
576       rtp_mux->seqnum_offset = g_value_get_int (value);
577       break;
578     case PROP_SSRC:
579       rtp_mux->ssrc = g_value_get_uint (value);
580       break;
581     default:
582       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583       break;
584   }
585 }
586
587 static gboolean
588 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
589 {
590
591   GstRTPMux *mux;
592   gboolean ret = FALSE;
593   gboolean forward = TRUE;
594
595   mux = GST_RTP_MUX (gst_pad_get_parent (pad));
596
597   switch (GST_EVENT_TYPE (event)) {
598     case GST_EVENT_FLUSH_STOP:
599     {
600       GstRTPMuxPadPrivate *padpriv;
601
602       GST_OBJECT_LOCK (mux);
603       mux->segment_pending = TRUE;
604       padpriv = gst_pad_get_element_private (pad);
605       if (padpriv)
606         gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
607       GST_OBJECT_UNLOCK (pad);
608     }
609       break;
610     case GST_EVENT_NEWSEGMENT:
611     {
612       gboolean update;
613       gdouble rate, applied_rate;
614       GstFormat format;
615       gint64 start, stop, position;
616       GstRTPMuxPadPrivate *padpriv;
617
618       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
619           &format, &start, &stop, &position);
620
621       GST_OBJECT_LOCK (mux);
622       padpriv = gst_pad_get_element_private (pad);
623
624       if (padpriv) {
625         if (format == GST_FORMAT_TIME)
626           gst_segment_set_newsegment_full (&padpriv->segment, update,
627               rate, applied_rate, format, start, stop, position);
628         else
629           gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
630       }
631       GST_OBJECT_UNLOCK (mux);
632       gst_event_unref (event);
633       forward = FALSE;
634       ret = TRUE;
635       break;
636     }
637     default:
638       break;
639   }
640
641   if (forward)
642     ret = gst_pad_push_event (mux->srcpad, event);
643
644   gst_object_unref (mux);
645   return ret;
646 }
647
648
649 static void
650 clear_segment (gpointer data, gpointer user_data)
651 {
652   GstPad *pad = data;
653   GstRTPMux *mux = user_data;
654   GstRTPMuxPadPrivate *padpriv;
655
656   GST_OBJECT_LOCK (mux);
657   padpriv = gst_pad_get_element_private (pad);
658   if (padpriv)
659     gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
660   GST_OBJECT_UNLOCK (mux);
661
662   gst_object_unref (pad);
663 }
664
665
666 static void
667 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
668 {
669   GstIterator *iter;
670
671   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
672   while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
673       GST_ITERATOR_RESYNC);
674   gst_iterator_free (iter);
675
676   GST_OBJECT_LOCK (rtp_mux);
677   rtp_mux->segment_pending = TRUE;
678
679   if (rtp_mux->ssrc == -1)
680     rtp_mux->current_ssrc = g_random_int ();
681   else
682     rtp_mux->current_ssrc = rtp_mux->ssrc;
683
684   if (rtp_mux->seqnum_offset == -1)
685     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
686   else
687     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
688   rtp_mux->seqnum = rtp_mux->seqnum_base;
689
690   if (rtp_mux->ts_offset == -1)
691     rtp_mux->ts_base = g_random_int ();
692   else
693     rtp_mux->ts_base = rtp_mux->ts_offset;
694
695   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
696
697   GST_OBJECT_UNLOCK (rtp_mux);
698 }
699
700 static GstStateChangeReturn
701 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
702 {
703   GstRTPMux *rtp_mux;
704
705   rtp_mux = GST_RTP_MUX (element);
706
707   switch (transition) {
708     case GST_STATE_CHANGE_NULL_TO_READY:
709       break;
710     case GST_STATE_CHANGE_READY_TO_PAUSED:
711       gst_rtp_mux_ready_to_paused (rtp_mux);
712       break;
713     case GST_STATE_CHANGE_PAUSED_TO_READY:
714       break;
715     default:
716       break;
717   }
718
719   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
720 }
721
722 gboolean
723 gst_rtp_mux_plugin_init (GstPlugin * plugin)
724 {
725   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
726
727   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
728       GST_TYPE_RTP_MUX);
729 }