rtpmux: Fix warning
[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 /* elementfactory information */
47 static const GstElementDetails gst_rtp_mux_details =
48 GST_ELEMENT_DETAILS ("RTP muxer",
49     "Codec/Muxer",
50     "multiplex N rtp streams into one",
51     "Zeeshan Ali <first.last@nokia.com>");
52
53 enum
54 {
55   ARG_0,
56   PROP_TIMESTAMP_OFFSET,
57   PROP_SEQNUM_OFFSET,
58   PROP_SEQNUM,
59   PROP_SSRC
60 };
61
62 #define DEFAULT_TIMESTAMP_OFFSET -1
63 #define DEFAULT_SEQNUM_OFFSET    -1
64 #define DEFAULT_SSRC             -1
65
66 typedef struct
67 {
68   gboolean have_clock_base;
69   guint clock_base;
70
71   GstCaps *out_caps;
72 } GstRTPMuxPadPrivate;
73
74 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("application/x-rtp")
78     );
79
80 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
81     GST_PAD_SINK,
82     GST_PAD_REQUEST,
83     GST_STATIC_CAPS ("application/x-rtp")
84     );
85
86 static void gst_rtp_mux_finalize (GObject * object);
87
88 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
89     GstPadTemplate * templ, const gchar * name);
90 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer);
91 static gboolean gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps);
92 static GstCaps *gst_rtp_mux_getcaps (GstPad * pad);
93
94 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
95     element, GstStateChange transition);
96
97 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
98     const GValue * value, GParamSpec * pspec);
99 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
100     GValue * value, GParamSpec * pspec);
101
102
103 GST_BOILERPLATE (GstRTPMux, gst_rtp_mux, GstElement, GST_TYPE_ELEMENT);
104
105 static void
106 gst_rtp_mux_base_init (gpointer g_class)
107 {
108   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
109
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&src_factory));
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&sink_factory));
114
115   gst_element_class_set_details (element_class, &gst_rtp_mux_details);
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   gobject_class->finalize = gst_rtp_mux_finalize;
128   gobject_class->get_property = gst_rtp_mux_get_property;
129   gobject_class->set_property = gst_rtp_mux_set_property;
130
131   g_object_class_install_property (G_OBJECT_CLASS (klass),
132       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
133           "Timestamp Offset",
134           "Offset to add to all outgoing timestamps (-1 = random)", -1,
135           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
136   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
137       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
138           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
139           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
140   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
141       g_param_spec_uint ("seqnum", "Sequence number",
142           "The RTP sequence number of the last processed packet",
143           0, G_MAXUINT, 0, G_PARAM_READABLE));
144   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
145       g_param_spec_uint ("ssrc", "SSRC",
146           "The SSRC of the packets (-1 == random)",
147           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
148
149   gstelement_class->request_new_pad =
150       GST_DEBUG_FUNCPTR (gst_rtp_mux_request_new_pad);
151   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_mux_change_state);
152
153   klass->chain_func = gst_rtp_mux_chain;
154 }
155
156 static gboolean
157 gst_rtp_mux_src_event (GstPad * pad, GstEvent * event)
158 {
159   GstElement *rtp_mux;
160   GstIterator *iter;
161   GstPad *sinkpad;
162   gboolean result = FALSE;
163   gboolean done = FALSE;
164
165   rtp_mux = gst_pad_get_parent_element (pad);
166   g_return_val_if_fail (rtp_mux != NULL, FALSE);
167
168   iter = gst_element_iterate_sink_pads (rtp_mux);
169
170   while (!done) {
171     switch (gst_iterator_next (iter, (gpointer) & sinkpad)) {
172       case GST_ITERATOR_OK:
173         gst_event_ref (event);
174         result |= gst_pad_push_event (sinkpad, event);
175         gst_object_unref (sinkpad);
176         break;
177       case GST_ITERATOR_RESYNC:
178         gst_iterator_resync (iter);
179         result = FALSE;
180         break;
181       case GST_ITERATOR_ERROR:
182         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
183       case GST_ITERATOR_DONE:
184         done = TRUE;
185         break;
186     }
187   }
188   gst_iterator_free (iter);
189   gst_object_unref (rtp_mux);
190   gst_event_unref (event);
191
192   return result;
193 }
194
195 static void
196 gst_rtp_mux_init (GstRTPMux * object, GstRTPMuxClass * g_class)
197 {
198   GstElementClass *klass = GST_ELEMENT_GET_CLASS (object);
199
200   object->srcpad =
201       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
202           "src"), "src");
203   gst_pad_set_event_function (object->srcpad,
204       GST_DEBUG_FUNCPTR (gst_rtp_mux_src_event));
205   gst_element_add_pad (GST_ELEMENT (object), object->srcpad);
206
207   object->ssrc = DEFAULT_SSRC;
208   object->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
209   object->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
210 }
211
212 static void
213 gst_rtp_mux_finalize (GObject * object)
214 {
215   GstRTPMux *rtp_mux;
216
217   rtp_mux = GST_RTP_MUX (object);
218
219   G_OBJECT_CLASS (parent_class)->finalize (object);
220 }
221
222 static GstPad *
223 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ)
224 {
225   GstPad *newpad = NULL;
226   GstPadTemplate *class_templ;
227
228   class_templ =
229       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (rtp_mux),
230       "sink_%d");
231
232   if (templ == class_templ) {
233     gchar *name;
234
235     /* create new pad with the name */
236     name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
237     newpad = gst_pad_new_from_template (templ, name);
238     g_free (name);
239
240     rtp_mux->numpads++;
241   } else {
242     GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
243   }
244
245   return newpad;
246 }
247
248 static void
249 free_pad_private (gpointer data, GObject * where_the_object_was)
250 {
251   GstRTPMuxPadPrivate *padpriv = data;
252
253   gst_caps_replace (&padpriv->out_caps, NULL);
254   g_slice_free (GstRTPMuxPadPrivate, padpriv);
255 }
256
257 static void
258 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
259 {
260   GstRTPMuxClass *klass;
261   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
262
263   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
264
265   /* setup some pad functions */
266   gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
267   gst_pad_set_getcaps_function (sinkpad, gst_rtp_mux_getcaps);
268   if (klass->chain_func)
269     gst_pad_set_chain_function (sinkpad, klass->chain_func);
270   if (klass->sink_event_func)
271     gst_pad_set_event_function (sinkpad, klass->sink_event_func);
272
273   /* This could break with gstreamer 0.10.9 */
274   gst_pad_set_active (sinkpad, TRUE);
275
276   gst_pad_set_element_private (sinkpad, padpriv);
277   g_object_weak_ref (G_OBJECT (sinkpad), free_pad_private, padpriv);
278
279   /* dd the pad to the element */
280   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
281 }
282
283 static GstPad *
284 gst_rtp_mux_request_new_pad (GstElement * element,
285     GstPadTemplate * templ, const gchar * req_name)
286 {
287   GstRTPMux *rtp_mux;
288   GstPad *newpad;
289
290   g_return_val_if_fail (templ != NULL, NULL);
291   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
292
293   rtp_mux = GST_RTP_MUX (element);
294
295   if (templ->direction != GST_PAD_SINK) {
296     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
297     return NULL;
298   }
299
300   newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ);
301   if (newpad)
302     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
303   else
304     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
305
306   return newpad;
307 }
308
309 /* Put our own clock-base on the buffer */
310 static void
311 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
312     GstBuffer * buffer)
313 {
314   guint32 ts;
315   guint32 sink_ts_base = 0;
316   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
317
318   if (padpriv->have_clock_base)
319     sink_ts_base = padpriv->clock_base;
320
321   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
322   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
323       gst_rtp_buffer_get_timestamp (buffer), ts);
324   gst_rtp_buffer_set_timestamp (buffer, ts);
325 }
326
327 static GstFlowReturn
328 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
329 {
330   GstRTPMux *rtp_mux;
331   GstFlowReturn ret;
332   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
333
334   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
335
336   if (!gst_rtp_buffer_validate (buffer)) {
337     GST_ERROR_OBJECT (rtp_mux, "Invalid RTP buffer");
338     gst_object_unref (rtp_mux);
339     return GST_FLOW_ERROR;
340   }
341
342   buffer = gst_buffer_make_writable (buffer);
343
344   GST_OBJECT_LOCK (rtp_mux);
345   rtp_mux->seqnum++;
346   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
347   GST_OBJECT_UNLOCK (rtp_mux);
348   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
349   gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
350   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
351       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
352       gst_rtp_buffer_get_timestamp (buffer));
353
354   gst_buffer_set_caps (buffer, padpriv->out_caps);
355
356   ret = gst_pad_push (rtp_mux->srcpad, buffer);
357
358   gst_object_unref (rtp_mux);
359   return ret;
360 }
361
362 static gboolean
363 gst_rtp_mux_setcaps (GstPad * pad, GstCaps * caps)
364 {
365   GstRTPMux *rtp_mux;
366   GstStructure *structure;
367   gboolean ret = TRUE;
368   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
369
370   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
371
372   structure = gst_caps_get_structure (caps, 0);
373
374   if (!ret)
375     goto out;
376
377   if (gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
378     padpriv->have_clock_base = TRUE;
379   }
380
381   caps = gst_caps_copy (caps);
382
383   gst_caps_set_simple (caps,
384       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
385       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
386
387   GST_DEBUG_OBJECT (rtp_mux,
388       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
389   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
390
391   if (ret)
392     gst_caps_replace (&padpriv->out_caps, caps);
393   else
394     gst_caps_unref (caps);
395
396 out:
397   gst_object_unref (rtp_mux);
398
399   return ret;
400 }
401
402 static void
403 clear_caps (GstCaps * caps, gboolean only_clock_rate)
404 {
405   gint i, j;
406
407   /* Lets only match on the clock-rate */
408   for (i = 0; i < gst_caps_get_size (caps); i++) {
409     GstStructure *s = gst_caps_get_structure (caps, i);
410
411     for (j = 0; j < gst_structure_n_fields (s); j++) {
412       const gchar *name = gst_structure_nth_field_name (s, j);
413
414       if (strcmp (name, "clock-rate") && (only_clock_rate ||
415               (strcmp (name, "ssrc")))) {
416         gst_structure_remove_field (s, name);
417         j--;
418       }
419     }
420   }
421 }
422
423 static gboolean
424 same_clock_rate_fold (gpointer item, GValue * ret, gpointer user_data)
425 {
426   GstPad *mypad = user_data;
427   GstPad *pad = item;
428   GstCaps *peercaps;
429   GstCaps *othercaps;
430   const GstCaps *accumcaps;
431   GstCaps *intersect;
432
433   if (pad == mypad)
434     return TRUE;
435
436   peercaps = gst_pad_peer_get_caps (pad);
437   if (!peercaps)
438     return TRUE;
439
440   othercaps = gst_caps_intersect (peercaps,
441       gst_pad_get_pad_template_caps (pad));
442   gst_caps_unref (peercaps);
443
444   accumcaps = gst_value_get_caps (ret);
445
446   clear_caps (othercaps, TRUE);
447
448   intersect = gst_caps_intersect (accumcaps, othercaps);
449
450   g_value_take_boxed (ret, intersect);
451
452   gst_caps_unref (othercaps);
453
454   return !gst_caps_is_empty (intersect);
455 }
456
457 static GstCaps *
458 gst_rtp_mux_getcaps (GstPad * pad)
459 {
460   GstRTPMux *mux = GST_RTP_MUX (gst_pad_get_parent (pad));
461   GstCaps *caps = NULL;
462   GstIterator *iter = NULL;
463   GValue v = { 0 };
464   GstIteratorResult res;
465   GstCaps *peercaps = gst_pad_peer_get_caps (mux->srcpad);
466   GstCaps *othercaps = NULL;
467
468   if (peercaps) {
469     othercaps = gst_caps_intersect (peercaps,
470         gst_pad_get_pad_template_caps (pad));
471     gst_caps_unref (peercaps);
472   } else {
473     othercaps = gst_caps_copy (gst_pad_get_pad_template_caps (mux->srcpad));
474   }
475
476   clear_caps (othercaps, FALSE);
477
478   g_value_init (&v, GST_TYPE_CAPS);
479
480   iter = gst_element_iterate_sink_pads (GST_ELEMENT (mux));
481   do {
482     gst_value_set_caps (&v, othercaps);
483     res = gst_iterator_fold (iter, same_clock_rate_fold, &v, pad);
484   } while (res == GST_ITERATOR_RESYNC);
485   gst_iterator_free (iter);
486
487   caps = (GstCaps *) gst_value_get_caps (&v);
488
489   if (res == GST_ITERATOR_ERROR) {
490     gst_caps_unref (caps);
491     caps = gst_caps_new_empty ();
492   }
493
494   if (othercaps)
495     gst_caps_unref (othercaps);
496   gst_object_unref (mux);
497
498   return caps;
499 }
500
501
502 static void
503 gst_rtp_mux_get_property (GObject * object,
504     guint prop_id, GValue * value, GParamSpec * pspec)
505 {
506   GstRTPMux *rtp_mux;
507
508   rtp_mux = GST_RTP_MUX (object);
509
510   switch (prop_id) {
511     case PROP_TIMESTAMP_OFFSET:
512       g_value_set_int (value, rtp_mux->ts_offset);
513       break;
514     case PROP_SEQNUM_OFFSET:
515       g_value_set_int (value, rtp_mux->seqnum_offset);
516       break;
517     case PROP_SEQNUM:
518       GST_OBJECT_LOCK (rtp_mux);
519       g_value_set_uint (value, rtp_mux->seqnum);
520       GST_OBJECT_UNLOCK (rtp_mux);
521       break;
522     case PROP_SSRC:
523       g_value_set_uint (value, rtp_mux->ssrc);
524       break;
525     default:
526       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
527       break;
528   }
529 }
530
531 static void
532 gst_rtp_mux_set_property (GObject * object,
533     guint prop_id, const GValue * value, GParamSpec * pspec)
534 {
535   GstRTPMux *rtp_mux;
536
537   rtp_mux = GST_RTP_MUX (object);
538
539   switch (prop_id) {
540     case PROP_TIMESTAMP_OFFSET:
541       rtp_mux->ts_offset = g_value_get_int (value);
542       break;
543     case PROP_SEQNUM_OFFSET:
544       rtp_mux->seqnum_offset = g_value_get_int (value);
545       break;
546     case PROP_SSRC:
547       rtp_mux->ssrc = g_value_get_uint (value);
548       break;
549     default:
550       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551       break;
552   }
553 }
554
555 static void
556 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
557 {
558   GST_OBJECT_LOCK (rtp_mux);
559
560   if (rtp_mux->ssrc == -1)
561     rtp_mux->current_ssrc = g_random_int ();
562   else
563     rtp_mux->current_ssrc = rtp_mux->ssrc;
564
565   if (rtp_mux->seqnum_offset == -1)
566     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
567   else
568     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
569   rtp_mux->seqnum = rtp_mux->seqnum_base;
570
571   if (rtp_mux->ts_offset == -1)
572     rtp_mux->ts_base = g_random_int ();
573   else
574     rtp_mux->ts_base = rtp_mux->ts_offset;
575   GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
576
577   GST_OBJECT_UNLOCK (rtp_mux);
578 }
579
580 static GstStateChangeReturn
581 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
582 {
583   GstRTPMux *rtp_mux;
584
585   rtp_mux = GST_RTP_MUX (element);
586
587   switch (transition) {
588     case GST_STATE_CHANGE_NULL_TO_READY:
589       break;
590     case GST_STATE_CHANGE_READY_TO_PAUSED:
591       gst_rtp_mux_ready_to_paused (rtp_mux);
592       break;
593     case GST_STATE_CHANGE_PAUSED_TO_READY:
594       break;
595     default:
596       break;
597   }
598
599   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
600 }
601
602 gboolean
603 gst_rtp_mux_plugin_init (GstPlugin * plugin)
604 {
605   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0, "rtp muxer");
606
607   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
608       GST_TYPE_RTP_MUX);
609 }