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