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