docs: remove outdated and pointless 'Last reviewed' lines from docs
[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., 51 Franklin St, Fifth Floor,
25  * Boston, MA 02110-1301, 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
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54
55 #include <gst/gst.h>
56 #include <gst/rtp/gstrtpbuffer.h>
57 #include <string.h>
58
59 #include "gstrtpmux.h"
60
61 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
62 #define GST_CAT_DEFAULT gst_rtp_mux_debug
63
64 enum
65 {
66   ARG_0,
67   PROP_TIMESTAMP_OFFSET,
68   PROP_SEQNUM_OFFSET,
69   PROP_SEQNUM,
70   PROP_SSRC
71 };
72
73 #define DEFAULT_TIMESTAMP_OFFSET -1
74 #define DEFAULT_SEQNUM_OFFSET    -1
75 #define DEFAULT_SSRC             -1
76
77 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS ("application/x-rtp")
81     );
82
83 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
84     GST_PAD_SINK,
85     GST_PAD_REQUEST,
86     GST_STATIC_CAPS ("application/x-rtp")
87     );
88
89 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
90     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
91 static void gst_rtp_mux_release_pad (GstElement * element, GstPad * pad);
92 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstObject * parent,
93     GstBuffer * buffer);
94 static GstFlowReturn gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
95     GstBufferList * bufferlist);
96 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux,
97     GstCaps * caps);
98 static gboolean gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent,
99     GstEvent * event);
100 static gboolean gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent,
101     GstQuery * query);
102
103 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
104     element, GstStateChange transition);
105
106 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
107     const GValue * value, GParamSpec * pspec);
108 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
109     GValue * value, GParamSpec * pspec);
110 static void gst_rtp_mux_dispose (GObject * object);
111
112 static gboolean gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux,
113     GstEvent * event);
114
115 G_DEFINE_TYPE (GstRTPMux, gst_rtp_mux, GST_TYPE_ELEMENT);
116
117
118 static void
119 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *gstelement_class;
123
124   gobject_class = (GObjectClass *) klass;
125   gstelement_class = (GstElementClass *) klass;
126
127
128   gst_element_class_add_pad_template (gstelement_class,
129       gst_static_pad_template_get (&src_factory));
130   gst_element_class_add_pad_template (gstelement_class,
131       gst_static_pad_template_get (&sink_factory));
132
133   gst_element_class_set_static_metadata (gstelement_class, "RTP muxer",
134       "Codec/Muxer",
135       "multiplex N rtp streams into one", "Zeeshan Ali <first.last@nokia.com>");
136
137   gobject_class->get_property = gst_rtp_mux_get_property;
138   gobject_class->set_property = gst_rtp_mux_set_property;
139   gobject_class->dispose = gst_rtp_mux_dispose;
140
141   klass->src_event = gst_rtp_mux_src_event_real;
142
143   g_object_class_install_property (G_OBJECT_CLASS (klass),
144       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
145           "Timestamp Offset",
146           "Offset to add to all outgoing timestamps (-1 = random)", -1,
147           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET,
148           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
150       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
151           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
152           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
154       g_param_spec_uint ("seqnum", "Sequence number",
155           "The RTP sequence number of the last processed packet",
156           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
158       g_param_spec_uint ("ssrc", "SSRC",
159           "The SSRC of the packets (-1 == random)",
160           0, G_MAXUINT, DEFAULT_SSRC,
161           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162
163   gstelement_class->request_new_pad =
164       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
165   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_mux_release_pad);
166   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
167 }
168
169 static void
170 gst_rtp_mux_dispose (GObject * object)
171 {
172   GstRTPMux *rtp_mux = GST_RTP_MUX (object);
173   GList *item;
174
175   g_clear_object (&rtp_mux->last_pad);
176
177 restart:
178   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
179     GstPad *pad = GST_PAD (item->data);
180     if (GST_PAD_IS_SINK (pad)) {
181       gst_element_release_request_pad (GST_ELEMENT (object), pad);
182       goto restart;
183     }
184   }
185
186   G_OBJECT_CLASS (gst_rtp_mux_parent_class)->dispose (object);
187 }
188
189 static gboolean
190 gst_rtp_mux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
191 {
192   GstRTPMux *rtp_mux = GST_RTP_MUX (parent);
193   GstRTPMuxClass *klass;
194   gboolean ret = FALSE;
195
196   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
197
198   ret = klass->src_event (rtp_mux, event);
199
200   return ret;
201 }
202
203 static gboolean
204 gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
205 {
206   GstIterator *iter;
207   gboolean result = FALSE;
208   gboolean done = FALSE;
209
210   iter = gst_element_iterate_sink_pads (GST_ELEMENT (rtp_mux));
211
212   while (!done) {
213     GValue item = { 0, };
214
215     switch (gst_iterator_next (iter, &item)) {
216       case GST_ITERATOR_OK:
217         gst_event_ref (event);
218         result |= gst_pad_push_event (g_value_get_object (&item), event);
219         g_value_reset (&item);
220         break;
221       case GST_ITERATOR_RESYNC:
222         gst_iterator_resync (iter);
223         result = FALSE;
224         break;
225       case GST_ITERATOR_ERROR:
226         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
227       case GST_ITERATOR_DONE:
228         done = TRUE;
229         break;
230     }
231   }
232   gst_iterator_free (iter);
233   gst_event_unref (event);
234
235   return result;
236 }
237
238 static void
239 gst_rtp_mux_init (GstRTPMux * rtp_mux)
240 {
241   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
242
243   rtp_mux->srcpad =
244       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
245           "src"), "src");
246   gst_pad_set_event_function (rtp_mux->srcpad,
247       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
248   GST_PAD_SET_PROXY_CAPS (rtp_mux->srcpad);
249   gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
250
251   rtp_mux->ssrc = DEFAULT_SSRC;
252   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
253   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
254
255   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
256 }
257
258 static void
259 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
260 {
261   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
262
263   /* setup some pad functions */
264   gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_rtp_mux_chain));
265   gst_pad_set_chain_list_function (sinkpad,
266       GST_DEBUG_FUNCPTR (gst_rtp_mux_chain_list));
267   gst_pad_set_event_function (sinkpad,
268       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_event));
269   gst_pad_set_query_function (sinkpad,
270       GST_DEBUG_FUNCPTR (gst_rtp_mux_sink_query));
271
272
273   gst_segment_init (&padpriv->segment, GST_FORMAT_UNDEFINED);
274
275   gst_pad_set_element_private (sinkpad, padpriv);
276
277   gst_pad_set_active (sinkpad, TRUE);
278   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
279 }
280
281 static GstPad *
282 gst_rtp_mux_request_new_pad (GstElement * element,
283     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
284 {
285   GstRTPMux *rtp_mux;
286   GstPad *newpad;
287
288   g_return_val_if_fail (templ != NULL, NULL);
289   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
290
291   rtp_mux = GST_RTP_MUX (element);
292
293   if (templ->direction != GST_PAD_SINK) {
294     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
295     return NULL;
296   }
297
298   newpad = gst_pad_new_from_template (templ, req_name);
299   if (newpad)
300     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
301   else
302     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
303
304   return newpad;
305 }
306
307 static void
308 gst_rtp_mux_release_pad (GstElement * element, GstPad * pad)
309 {
310   GstRTPMuxPadPrivate *padpriv;
311
312   GST_OBJECT_LOCK (element);
313   padpriv = gst_pad_get_element_private (pad);
314   gst_pad_set_element_private (pad, NULL);
315   GST_OBJECT_UNLOCK (element);
316
317   gst_element_remove_pad (element, pad);
318
319   if (padpriv) {
320     g_slice_free (GstRTPMuxPadPrivate, padpriv);
321   }
322 }
323
324 /* Put our own clock-base on the buffer */
325 static void
326 gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
327     GstRTPMuxPadPrivate * padpriv, GstRTPBuffer * rtpbuffer)
328 {
329   guint32 ts;
330   guint32 sink_ts_base = 0;
331
332   if (padpriv && padpriv->have_clock_base)
333     sink_ts_base = padpriv->clock_base;
334
335   ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
336       rtp_mux->ts_base;
337   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
338       gst_rtp_buffer_get_timestamp (rtpbuffer), ts);
339   gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
340 }
341
342 static gboolean
343 process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
344     GstRTPBuffer * rtpbuffer)
345 {
346   GstRTPMuxClass *klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
347
348   if (klass->accept_buffer_locked)
349     if (!klass->accept_buffer_locked (rtp_mux, padpriv, rtpbuffer))
350       return FALSE;
351
352   rtp_mux->seqnum++;
353   gst_rtp_buffer_set_seq (rtpbuffer, rtp_mux->seqnum);
354
355   gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
356   gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
357   GST_LOG_OBJECT (rtp_mux,
358       "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u",
359       rtpbuffer->map[0].size, rtp_mux->seqnum,
360       gst_rtp_buffer_get_timestamp (rtpbuffer));
361
362   if (padpriv) {
363     if (padpriv->segment.format == GST_FORMAT_TIME)
364       GST_BUFFER_PTS (rtpbuffer->buffer) =
365           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
366           GST_BUFFER_PTS (rtpbuffer->buffer));
367   }
368
369   return TRUE;
370 }
371
372 struct BufferListData
373 {
374   GstRTPMux *rtp_mux;
375   GstRTPMuxPadPrivate *padpriv;
376   gboolean drop;
377 };
378
379 static gboolean
380 process_list_item (GstBuffer ** buffer, guint idx, gpointer user_data)
381 {
382   struct BufferListData *bd = user_data;
383   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
384
385   *buffer = gst_buffer_make_writable (*buffer);
386
387   gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtpbuffer);
388
389   bd->drop = !process_buffer_locked (bd->rtp_mux, bd->padpriv, &rtpbuffer);
390
391   gst_rtp_buffer_unmap (&rtpbuffer);
392
393   if (bd->drop)
394     return FALSE;
395
396   if (GST_BUFFER_DURATION_IS_VALID (*buffer) &&
397       GST_BUFFER_TIMESTAMP_IS_VALID (*buffer))
398     bd->rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (*buffer) +
399         GST_BUFFER_DURATION (*buffer);
400   else
401     bd->rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
402
403   return TRUE;
404 }
405
406 static GstFlowReturn
407 gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
408     GstBufferList * bufferlist)
409 {
410   GstRTPMux *rtp_mux;
411   GstFlowReturn ret;
412   GstRTPMuxPadPrivate *padpriv;
413   struct BufferListData bd;
414
415   rtp_mux = GST_RTP_MUX (parent);
416
417   GST_OBJECT_LOCK (rtp_mux);
418
419   padpriv = gst_pad_get_element_private (pad);
420   if (!padpriv) {
421     GST_OBJECT_UNLOCK (rtp_mux);
422     ret = GST_FLOW_NOT_LINKED;
423     gst_buffer_list_unref (bufferlist);
424     goto out;
425   }
426
427   bd.rtp_mux = rtp_mux;
428   bd.padpriv = padpriv;
429   bd.drop = FALSE;
430
431   bufferlist = gst_buffer_list_make_writable (bufferlist);
432   gst_buffer_list_foreach (bufferlist, process_list_item, &bd);
433
434   GST_OBJECT_UNLOCK (rtp_mux);
435
436   if (bd.drop) {
437     gst_buffer_list_unref (bufferlist);
438     ret = GST_FLOW_OK;
439   } else {
440     ret = gst_pad_push_list (rtp_mux->srcpad, bufferlist);
441   }
442
443 out:
444
445   return ret;
446 }
447
448 static gboolean
449 resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
450 {
451   GstRTPMux *rtp_mux = user_data;
452
453   if (GST_EVENT_TYPE (*event) == GST_EVENT_CAPS) {
454     GstCaps *caps;
455
456     gst_event_parse_caps (*event, &caps);
457     gst_rtp_mux_setcaps (pad, rtp_mux, caps);
458   } else {
459     gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
460   }
461
462   return TRUE;
463 }
464
465 static GstFlowReturn
466 gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
467 {
468   GstRTPMux *rtp_mux;
469   GstFlowReturn ret;
470   GstRTPMuxPadPrivate *padpriv;
471   gboolean drop;
472   gboolean changed = FALSE;
473   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
474
475   rtp_mux = GST_RTP_MUX (GST_OBJECT_PARENT (pad));
476
477   GST_OBJECT_LOCK (rtp_mux);
478   padpriv = gst_pad_get_element_private (pad);
479
480   if (!padpriv) {
481     GST_OBJECT_UNLOCK (rtp_mux);
482     gst_buffer_unref (buffer);
483     return GST_FLOW_NOT_LINKED;
484   }
485
486   buffer = gst_buffer_make_writable (buffer);
487
488   if (!gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer)) {
489     GST_OBJECT_UNLOCK (rtp_mux);
490     gst_buffer_unref (buffer);
491     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
492     return GST_FLOW_ERROR;
493   }
494
495   drop = !process_buffer_locked (rtp_mux, padpriv, &rtpbuffer);
496
497   gst_rtp_buffer_unmap (&rtpbuffer);
498
499   if (!drop) {
500     if (pad != rtp_mux->last_pad) {
501       changed = TRUE;
502       g_clear_object (&rtp_mux->last_pad);
503       rtp_mux->last_pad = g_object_ref (pad);
504     }
505
506     if (GST_BUFFER_DURATION_IS_VALID (buffer) &&
507         GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
508       rtp_mux->last_stop = GST_BUFFER_TIMESTAMP (buffer) +
509           GST_BUFFER_DURATION (buffer);
510     else
511       rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
512   }
513
514   GST_OBJECT_UNLOCK (rtp_mux);
515
516   if (changed)
517     gst_pad_sticky_events_foreach (pad, resend_events, rtp_mux);
518
519   if (drop) {
520     gst_buffer_unref (buffer);
521     ret = GST_FLOW_OK;
522   } else {
523     ret = gst_pad_push (rtp_mux->srcpad, buffer);
524   }
525
526   return ret;
527 }
528
529 static gboolean
530 gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
531 {
532   GstStructure *structure;
533   gboolean ret = FALSE;
534   GstRTPMuxPadPrivate *padpriv;
535
536   structure = gst_caps_get_structure (caps, 0);
537
538   if (!structure)
539     return FALSE;
540
541   GST_OBJECT_LOCK (rtp_mux);
542   padpriv = gst_pad_get_element_private (pad);
543   if (padpriv &&
544       gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
545     padpriv->have_clock_base = TRUE;
546   }
547   GST_OBJECT_UNLOCK (rtp_mux);
548
549   caps = gst_caps_copy (caps);
550
551   gst_caps_set_simple (caps,
552       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
553       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
554
555   if (rtp_mux->send_stream_start) {
556     gchar s_id[32];
557
558     /* stream-start (FIXME: create id based on input ids) */
559     g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
560     gst_pad_push_event (rtp_mux->srcpad, gst_event_new_stream_start (s_id));
561
562     rtp_mux->send_stream_start = FALSE;
563   }
564
565   GST_DEBUG_OBJECT (rtp_mux,
566       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
567   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
568
569   if (rtp_mux->ssrc == -1) {
570     if (gst_structure_has_field_typed (structure, "ssrc", G_TYPE_UINT)) {
571       rtp_mux->current_ssrc = g_value_get_uint
572           (gst_structure_get_value (structure, "ssrc"));
573     }
574   }
575
576   gst_caps_unref (caps);
577
578   return ret;
579 }
580
581 static void
582 clear_caps (GstCaps * caps, gboolean only_clock_rate)
583 {
584   gint i, j;
585
586   /* Lets only match on the clock-rate */
587   for (i = 0; i < gst_caps_get_size (caps); i++) {
588     GstStructure *s = gst_caps_get_structure (caps, i);
589
590     for (j = 0; j < gst_structure_n_fields (s); j++) {
591       const gchar *name = gst_structure_nth_field_name (s, j);
592
593       if (strcmp (name, "clock-rate") && (only_clock_rate ||
594               (strcmp (name, "ssrc")))) {
595         gst_structure_remove_field (s, name);
596         j--;
597       }
598     }
599   }
600 }
601
602 static gboolean
603 same_clock_rate_fold (const GValue * item, GValue * ret, gpointer user_data)
604 {
605   GstPad *mypad = user_data;
606   GstPad *pad = g_value_get_object (item);
607   GstCaps *peercaps;
608   GstCaps *accumcaps;
609   GstCaps *intersect;
610
611   if (pad == mypad)
612     return TRUE;
613
614   accumcaps = g_value_get_boxed (ret);
615   peercaps = gst_pad_peer_query_caps (pad, accumcaps);
616   if (!peercaps) {
617     g_warning ("no peercaps");
618     return TRUE;
619   }
620   peercaps = gst_caps_make_writable (peercaps);
621   clear_caps (peercaps, TRUE);
622
623   intersect = gst_caps_intersect (accumcaps, peercaps);
624
625   g_value_take_boxed (ret, intersect);
626   gst_caps_unref (peercaps);
627
628   return !gst_caps_is_empty (intersect);
629 }
630
631 static GstCaps *
632 gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
633 {
634   GstCaps *caps = NULL;
635   GstIterator *iter = NULL;
636   GValue v = { 0 };
637   GstIteratorResult res;
638   GstCaps *peercaps;
639   GstCaps *othercaps;
640
641   peercaps = gst_pad_peer_query_caps (mux->srcpad, filter);
642
643   if (peercaps) {
644     othercaps = gst_caps_intersect_full (peercaps,
645         gst_pad_get_pad_template_caps (pad), GST_CAPS_INTERSECT_FIRST);
646     gst_caps_unref (peercaps);
647   } else {
648     if (filter)
649       othercaps = gst_caps_intersect_full (filter,
650           gst_pad_get_pad_template_caps (mux->srcpad),
651           GST_CAPS_INTERSECT_FIRST);
652     else
653       othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
654   }
655
656   clear_caps (othercaps, FALSE);
657
658   g_value_init (&v, GST_TYPE_CAPS);
659
660   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
661   do {
662     gst_value_set_caps (&v, othercaps);
663     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
664     gst_iterator_resync (iter);
665   } while (res == GST_ITERATOR_RESYNC);
666   gst_iterator_free (iter);
667
668   caps = (GstCaps *) gst_value_get_caps (&v);
669
670   if (res == GST_ITERATOR_ERROR) {
671     gst_caps_unref (caps);
672     caps = gst_caps_new_empty ();
673   }
674
675   gst_caps_unref (othercaps);
676
677   return caps;
678 }
679
680 static gboolean
681 gst_rtp_mux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
682 {
683   GstRTPMux *mux = GST_RTP_MUX (parent);
684   gboolean res = FALSE;
685
686   switch (GST_QUERY_TYPE (query)) {
687     case GST_QUERY_CAPS:
688     {
689       GstCaps *filter, *caps;
690
691       gst_query_parse_caps (query, &filter);
692       caps = gst_rtp_mux_getcaps (pad, mux, filter);
693       gst_query_set_caps_result (query, caps);
694       gst_caps_unref (caps);
695       res = TRUE;
696       break;
697     }
698     default:
699       res = gst_pad_query_default (pad, parent, query);
700       break;
701   }
702
703   return res;
704
705
706 }
707
708
709 static void
710 gst_rtp_mux_get_property (GObject * object,
711     guint prop_id, GValue * value, GParamSpec * pspec)
712 {
713   GstRTPMux *rtp_mux;
714
715   rtp_mux = GST_RTP_MUX (object);
716
717   switch (prop_id) {
718     case PROP_TIMESTAMP_OFFSET:
719       g_value_set_int (value, rtp_mux->ts_offset);
720       break;
721     case PROP_SEQNUM_OFFSET:
722       g_value_set_int (value, rtp_mux->seqnum_offset);
723       break;
724     case PROP_SEQNUM:
725       GST_OBJECT_LOCK (rtp_mux);
726       g_value_set_uint (value, rtp_mux->seqnum);
727       GST_OBJECT_UNLOCK (rtp_mux);
728       break;
729     case PROP_SSRC:
730       g_value_set_uint (value, rtp_mux->ssrc);
731       break;
732     default:
733       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
734       break;
735   }
736 }
737
738 static void
739 gst_rtp_mux_set_property (GObject * object,
740     guint prop_id, const GValue * value, GParamSpec * pspec)
741 {
742   GstRTPMux *rtp_mux;
743
744   rtp_mux = GST_RTP_MUX (object);
745
746   switch (prop_id) {
747     case PROP_TIMESTAMP_OFFSET:
748       rtp_mux->ts_offset = g_value_get_int (value);
749       break;
750     case PROP_SEQNUM_OFFSET:
751       rtp_mux->seqnum_offset = g_value_get_int (value);
752       break;
753     case PROP_SSRC:
754       rtp_mux->ssrc = g_value_get_uint (value);
755       break;
756     default:
757       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
758       break;
759   }
760 }
761
762 static gboolean
763 gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
764 {
765   GstRTPMux *mux = GST_RTP_MUX (parent);
766   gboolean is_pad;
767   gboolean ret;
768
769   switch (GST_EVENT_TYPE (event)) {
770     case GST_EVENT_CAPS:
771     {
772       GstCaps *caps;
773
774       gst_event_parse_caps (event, &caps);
775       ret = gst_rtp_mux_setcaps (pad, mux, caps);
776       gst_event_unref (event);
777       return ret;
778     }
779     case GST_EVENT_FLUSH_STOP:
780     {
781       GST_OBJECT_LOCK (mux);
782       mux->last_stop = GST_CLOCK_TIME_NONE;
783       GST_OBJECT_UNLOCK (mux);
784       break;
785     }
786     case GST_EVENT_SEGMENT:
787     {
788       GstRTPMuxPadPrivate *padpriv;
789
790       GST_OBJECT_LOCK (mux);
791       padpriv = gst_pad_get_element_private (pad);
792
793       if (padpriv) {
794         gst_event_copy_segment (event, &padpriv->segment);
795       }
796       GST_OBJECT_UNLOCK (mux);
797       break;
798     }
799     default:
800       break;
801   }
802
803   GST_OBJECT_LOCK (mux);
804   is_pad = (pad == mux->last_pad);
805   GST_OBJECT_UNLOCK (mux);
806
807   if (is_pad) {
808     return gst_pad_push_event (mux->srcpad, event);
809   } else {
810     gst_event_unref (event);
811     return TRUE;
812   }
813 }
814
815 static void
816 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
817 {
818
819   GST_OBJECT_LOCK (rtp_mux);
820
821   g_clear_object (&rtp_mux->last_pad);
822   rtp_mux->send_stream_start = TRUE;
823
824   if (rtp_mux->ssrc == -1)
825     rtp_mux->current_ssrc = g_random_int ();
826   else
827     rtp_mux->current_ssrc = rtp_mux->ssrc;
828
829   if (rtp_mux->seqnum_offset == -1)
830     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
831   else
832     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
833   rtp_mux->seqnum = rtp_mux->seqnum_base;
834
835   if (rtp_mux->ts_offset == -1)
836     rtp_mux->ts_base = g_random_int ();
837   else
838     rtp_mux->ts_base = rtp_mux->ts_offset;
839
840   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
841
842   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
843
844   GST_OBJECT_UNLOCK (rtp_mux);
845 }
846
847 static GstStateChangeReturn
848 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
849 {
850   GstRTPMux *rtp_mux;
851   GstStateChangeReturn ret;
852
853   rtp_mux = GST_RTP_MUX (element);
854
855   switch (transition) {
856     case GST_STATE_CHANGE_READY_TO_PAUSED:
857       gst_rtp_mux_ready_to_paused (rtp_mux);
858       break;
859     default:
860       break;
861   }
862
863   ret = GST_ELEMENT_CLASS (gst_rtp_mux_parent_class)->change_state (element,
864       transition);
865
866   switch (transition) {
867     case GST_STATE_CHANGE_PAUSED_TO_READY:
868       g_clear_object (&rtp_mux->last_pad);
869       break;
870     default:
871       break;
872   }
873
874   return ret;
875 }
876
877 gboolean
878 gst_rtp_mux_plugin_init (GstPlugin * plugin)
879 {
880   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
881
882   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
883       GST_TYPE_RTP_MUX);
884 }