rtpmux: no need to ref pad in _chain()
[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_OBJECT_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     return GST_FLOW_ERROR;
425   }
426
427   GST_OBJECT_LOCK (rtp_mux);
428   padpriv = gst_pad_get_element_private (pad);
429
430   if (!padpriv) {
431     GST_OBJECT_UNLOCK (rtp_mux);
432     gst_buffer_unref (buffer);
433     return GST_FLOW_NOT_LINKED;
434   }
435
436   buffer = gst_buffer_make_writable (buffer);
437
438   drop = !process_buffer_locked (rtp_mux, padpriv, buffer);
439
440   if (!drop && rtp_mux->segment_pending) {
441     /*
442      * We set the start at 0, because we re-timestamps to the running time
443      */
444     newseg_event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
445         GST_FORMAT_TIME, 0, -1, 0);
446
447     rtp_mux->segment_pending = FALSE;
448   }
449   GST_OBJECT_UNLOCK (rtp_mux);
450
451   if (newseg_event)
452     gst_pad_push_event (rtp_mux->srcpad, newseg_event);
453
454   if (drop) {
455     gst_buffer_unref (buffer);
456     ret = GST_FLOW_OK;
457   } else {
458     ret = gst_pad_push (rtp_mux->srcpad, buffer);
459   }
460
461   return ret;
462 }
463
464 static gboolean
465 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
466 {
467   GstRTPMux *rtp_mux;
468   GstStructure *structure;
469   gboolean ret = FALSE;
470   GstRTPMuxPadPrivate *padpriv;
471
472   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
473
474   structure = gst_caps_get_structure (caps, 0);
475
476   if (!structure)
477     goto out;
478
479   GST_OBJECT_LOCK (rtp_mux);
480   padpriv = gst_pad_get_element_private (pad);
481   if (padpriv &&
482       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
483     padpriv->have_clock_base = TRUE;
484   }
485   GST_OBJECT_UNLOCK (rtp_mux);
486
487   caps = gst_caps_copy (caps);
488
489   gst_caps_set_simple (caps,
490       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
491       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
492
493   GST_DEBUG_OBJECT (rtp_mux,
494       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
495   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
496
497   if (rtp_mux->ssrc == -1) {
498     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
499       rtp_mux->current_ssrc = g_value_get_uint
500           (gst_structure_get_value (structure, "ssrc"));
501     }
502   }
503
504   if (ret) {
505     GST_OBJECT_LOCK (rtp_mux);
506     padpriv = gst_pad_get_element_private (pad);
507     if (padpriv)
508       gst_caps_replace (&padpriv->out_caps, caps);
509     GST_OBJECT_UNLOCK (rtp_mux);
510   }
511   gst_caps_unref (caps);
512
513 out:
514   gst_object_unref (rtp_mux);
515
516   return ret;
517 }
518
519 static void
520 clear_caps (GstCaps * caps, gboolean only_clock_rate)
521 {
522   gint i, j;
523
524   /* Lets only match on the clock-rate */
525   for (i = 0; i < gst_caps_get_size (caps); i++) {
526     GstStructure *s = gst_caps_get_structure (caps, i);
527
528     for (j = 0; j < gst_structure_n_fields (s); j++) {
529       const gchar *name = gst_structure_nth_field_name (s, j);
530
531       if (strcmp (name, "clock-rate") && (only_clock_rate ||
532               (strcmp (name, "ssrc")))) {
533         gst_structure_remove_field (s, name);
534         j--;
535       }
536     }
537   }
538 }
539
540 static gboolean
541 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
542 {
543   GstPad *mypad = user_data;
544   GstPad *pad = item;
545   GstCaps *peercaps;
546   GstCaps *othercaps;
547   const GstCaps *accumcaps;
548   GstCaps *intersect;
549
550   if (pad == mypad) {
551     gst_object_unref (pad);
552     return TRUE;
553   }
554
555   peercaps = gst_pad_peer_get_caps (pad);
556   if (!peercaps) {
557     gst_object_unref (pad);
558     return TRUE;
559   }
560
561   othercaps = gst_caps_intersect (peercaps,
562       gst_pad_get_pad_template_caps (pad));
563   gst_caps_unref (peercaps);
564
565   accumcaps = gst_value_get_caps (ret);
566
567   clear_caps (othercaps, TRUE);
568
569   intersect = gst_caps_intersect (accumcaps, othercaps);
570
571   g_value_take_boxed (ret, intersect);
572
573   gst_caps_unref (othercaps);
574   gst_object_unref (pad);
575
576   return !gst_caps_is_empty (intersect);
577 }
578
579 static GstCaps *
580 gst_rtp_mux_getcaps (GstPad * pad)
581 {
582   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
583   GstCaps *caps = NULL;
584   GstIterator *iter = NULL;
585   GValue v = { 0 };
586   GstIteratorResult res;
587   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
588   GstCaps *othercaps = NULL;
589
590   if (peercaps) {
591     othercaps = gst_caps_intersect (peercaps,
592         gst_pad_get_pad_template_caps (pad));
593     gst_caps_unref (peercaps);
594   } else {
595     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
596   }
597
598   clear_caps (othercaps, FALSE);
599
600   g_value_init (&v, GST_TYPE_CAPS);
601
602   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
603   do {
604     gst_value_set_caps (&v, othercaps);
605     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
606   } while (res == GST_ITERATOR_RESYNC);
607   gst_iterator_free (iter);
608
609   caps = (GstCaps *) gst_value_get_caps (&v);
610
611   if (res == GST_ITERATOR_ERROR) {
612     gst_caps_unref (caps);
613     caps = gst_caps_new_empty ();
614   }
615
616   if (othercaps)
617     gst_caps_unref (othercaps);
618   gst_object_unref (mux);
619
620   return caps;
621 }
622
623
624 static void
625 gst_rtp_mux_get_property (GObject * object,
626     guint prop_id, GValue * value, GParamSpec * pspec)
627 {
628   GstRTPMux *rtp_mux;
629
630   rtp_mux = GST_RTP_MUX (object);
631
632   switch (prop_id) {
633     case PROP_TIMESTAMP_OFFSET:
634       g_value_set_int (value, rtp_mux->ts_offset);
635       break;
636     case PROP_SEQNUM_OFFSET:
637       g_value_set_int (value, rtp_mux->seqnum_offset);
638       break;
639     case PROP_SEQNUM:
640       GST_OBJECT_LOCK (rtp_mux);
641       g_value_set_uint (value, rtp_mux->seqnum);
642       GST_OBJECT_UNLOCK (rtp_mux);
643       break;
644     case PROP_SSRC:
645       g_value_set_uint (value, rtp_mux->ssrc);
646       break;
647     default:
648       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649       break;
650   }
651 }
652
653 static void
654 gst_rtp_mux_set_property (GObject * object,
655     guint prop_id, const GValue * value, GParamSpec * pspec)
656 {
657   GstRTPMux *rtp_mux;
658
659   rtp_mux = GST_RTP_MUX (object);
660
661   switch (prop_id) {
662     case PROP_TIMESTAMP_OFFSET:
663       rtp_mux->ts_offset = g_value_get_int (value);
664       break;
665     case PROP_SEQNUM_OFFSET:
666       rtp_mux->seqnum_offset = g_value_get_int (value);
667       break;
668     case PROP_SSRC:
669       rtp_mux->ssrc = g_value_get_uint (value);
670       break;
671     default:
672       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
673       break;
674   }
675 }
676
677 static gboolean
678 gst_rtp_mux_sink_event (GstPad * pad, GstEvent * event)
679 {
680
681   GstRTPMux *mux;
682   gboolean ret = FALSE;
683   gboolean forward = TRUE;
684
685   mux = GST_RTP_MUX (gst_pad_get_parent (pad));
686
687   switch (GST_EVENT_TYPE (event)) {
688     case GST_EVENT_FLUSH_STOP:
689     {
690       GstRTPMuxPadPrivate *padpriv;
691
692       GST_OBJECT_LOCK (mux);
693       mux->segment_pending = TRUE;
694       padpriv = gst_pad_get_element_private (pad);
695       if (padpriv)
696         gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
697       GST_OBJECT_UNLOCK (mux);
698     }
699       break;
700     case GST_EVENT_NEWSEGMENT:
701     {
702       gboolean update;
703       gdouble rate, applied_rate;
704       GstFormat format;
705       gint64 start, stop, position;
706       GstRTPMuxPadPrivate *padpriv;
707
708       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
709           &format, &start, &stop, &position);
710
711       GST_OBJECT_LOCK (mux);
712       padpriv = gst_pad_get_element_private (pad);
713
714       if (padpriv) {
715         if (format == GST_FORMAT_TIME)
716           gst_segment_set_newsegment_full (&padpriv->segment, update,
717               rate, applied_rate, format, start, stop, position);
718         else
719           gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
720       }
721       GST_OBJECT_UNLOCK (mux);
722       gst_event_unref (event);
723       forward = FALSE;
724       ret = TRUE;
725       break;
726     }
727     default:
728       break;
729   }
730
731   if (forward)
732     ret = gst_pad_push_event (mux->srcpad, event);
733
734   gst_object_unref (mux);
735   return ret;
736 }
737
738
739 static void
740 clear_segment (gpointer data, gpointer user_data)
741 {
742   GstPad *pad = data;
743   GstRTPMux *mux = user_data;
744   GstRTPMuxPadPrivate *padpriv;
745
746   GST_OBJECT_LOCK (mux);
747   padpriv = gst_pad_get_element_private (pad);
748   if (padpriv)
749     gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
750   GST_OBJECT_UNLOCK (mux);
751
752   gst_object_unref (pad);
753 }
754
755
756 static void
757 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
758 {
759   GstIterator *iter;
760
761   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
762   while (gst_iterator_foreach (iter, clear_segment, rtp_mux) ==
763       GST_ITERATOR_RESYNC);
764   gst_iterator_free (iter);
765
766   GST_OBJECT_LOCK (rtp_mux);
767   rtp_mux->segment_pending = TRUE;
768
769   if (rtp_mux->ssrc == -1)
770     rtp_mux->current_ssrc = g_random_int ();
771   else
772     rtp_mux->current_ssrc = rtp_mux->ssrc;
773
774   if (rtp_mux->seqnum_offset == -1)
775     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
776   else
777     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
778   rtp_mux->seqnum = rtp_mux->seqnum_base;
779
780   if (rtp_mux->ts_offset == -1)
781     rtp_mux->ts_base = g_random_int ();
782   else
783     rtp_mux->ts_base = rtp_mux->ts_offset;
784
785   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
786
787   GST_OBJECT_UNLOCK (rtp_mux);
788 }
789
790 static GstStateChangeReturn
791 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
792 {
793   GstRTPMux *rtp_mux;
794
795   rtp_mux = GST_RTP_MUX (element);
796
797   switch (transition) {
798     case GST_STATE_CHANGE_NULL_TO_READY:
799       break;
800     case GST_STATE_CHANGE_READY_TO_PAUSED:
801       gst_rtp_mux_ready_to_paused (rtp_mux);
802       break;
803     case GST_STATE_CHANGE_PAUSED_TO_READY:
804       break;
805     default:
806       break;
807   }
808
809   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
810 }
811
812 gboolean
813 gst_rtp_mux_plugin_init (GstPlugin * plugin)
814 {
815   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
816
817   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
818       GST_TYPE_RTP_MUX);
819 }