rtpmux: Missing format parameter
[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  * @short_description: Muxer that takes one or several RTP streams
29  * and muxes them to a single rtp stream.
30  *
31  * <refsect2>
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gst/gst.h>
40 #include <gst/rtp/gstrtpbuffer.h>
41 #include <gstrtpmux.h>
42 #include <string.h>
43
44 GST_DEBUG_CATEGORY_STATIC (gst_rtp_mux_debug);
45 #define GST_CAT_DEFAULT gst_rtp_mux_debug
46
47 /* elementfactory information */
48 static const GstElementDetails gst_rtp_mux_details =
49 GST_ELEMENT_DETAILS ("RTP muxer",
50     "Codec/Muxer",
51     "multiplex N rtp streams into one",
52     "Zeeshan Ali <first.last@nokia.com>");
53
54 enum
55 {
56   ARG_0,
57   PROP_CLOCK_RATE,
58   PROP_TIMESTAMP_OFFSET,
59   PROP_SEQNUM_OFFSET,
60   PROP_SEQNUM,
61   PROP_SSRC
62 };
63
64 #define DEFAULT_TIMESTAMP_OFFSET -1
65 #define DEFAULT_SEQNUM_OFFSET    -1
66 #define DEFAULT_SSRC             -1
67 #define DEFAULT_CLOCK_RATE        0
68
69 typedef struct {
70   gboolean have_base;
71   guint clock_base;
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_base_init (gpointer g_class);
87 static void gst_rtp_mux_class_init (GstRTPMuxClass * klass);
88 static void gst_rtp_mux_init (GstRTPMux * rtp_mux);
89
90 static void gst_rtp_mux_finalize (GObject * object);
91
92 static GstPad *gst_rtp_mux_request_new_pad (GstElement * element,
93     GstPadTemplate * templ, const gchar * name);
94 static void gst_rtp_mux_release_pad (GstElement * element, GstPad *pad);
95 static GstFlowReturn gst_rtp_mux_chain (GstPad * pad,
96     GstBuffer * buffer);
97 static gboolean gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps);
98
99 static GstStateChangeReturn gst_rtp_mux_change_state (GstElement *
100     element, GstStateChange transition);
101
102 static void gst_rtp_mux_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_rtp_mux_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106
107 static gboolean gst_rtp_mux_src_event (GstPad * pad, GstEvent * event);
108
109 static GstElementClass *parent_class = NULL;
110
111 GType
112 gst_rtp_mux_get_type (void)
113 {
114   static GType rtp_mux_type = 0;
115
116   if (!rtp_mux_type) {
117     static const GTypeInfo rtp_mux_info = {
118       sizeof (GstRTPMuxClass),
119       gst_rtp_mux_base_init,
120       NULL,
121       (GClassInitFunc) gst_rtp_mux_class_init,
122       NULL,
123       NULL,
124       sizeof (GstRTPMux),
125       0,
126       (GInstanceInitFunc) gst_rtp_mux_init,
127     };
128
129     rtp_mux_type =
130         g_type_register_static (GST_TYPE_ELEMENT, "GstRTPMux",
131         &rtp_mux_info, 0);
132   }
133   return rtp_mux_type;
134 }
135
136 static void
137 gst_rtp_mux_base_init (gpointer g_class)
138 {
139   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
140
141   gst_element_class_add_pad_template (element_class,
142       gst_static_pad_template_get (&src_factory));
143   gst_element_class_add_pad_template (element_class,
144       gst_static_pad_template_get (&sink_factory));
145
146   gst_element_class_set_details (element_class, &gst_rtp_mux_details);
147 }
148
149 static void
150 gst_rtp_mux_class_init (GstRTPMuxClass * klass)
151 {
152   GObjectClass *gobject_class;
153   GstElementClass *gstelement_class;
154
155   gobject_class = (GObjectClass *) klass;
156   gstelement_class = (GstElementClass *) klass;
157
158   parent_class = g_type_class_peek_parent (klass);
159
160   gobject_class->finalize = gst_rtp_mux_finalize;
161   gobject_class->get_property = gst_rtp_mux_get_property;
162   gobject_class->set_property = gst_rtp_mux_set_property;
163
164   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLOCK_RATE,
165       g_param_spec_uint ("clock-rate", "clockrate",
166           "The clock-rate of the RTP streams",
167           0, G_MAXUINT, DEFAULT_CLOCK_RATE, G_PARAM_READWRITE));
168   g_object_class_install_property (G_OBJECT_CLASS (klass),
169       PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
170           "Timestamp Offset",
171           "Offset to add to all outgoing timestamps (-1 = random)", -1,
172           G_MAXINT, DEFAULT_TIMESTAMP_OFFSET, G_PARAM_READWRITE));
173   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
174       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
175           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
176           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE));
177   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
178       g_param_spec_uint ("seqnum", "Sequence number",
179           "The RTP sequence number of the last processed packet",
180           0, G_MAXUINT, 0, G_PARAM_READABLE));
181   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
182       g_param_spec_uint ("ssrc", "SSRC",
183           "The SSRC of the packets (-1 == random)",
184           0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
185
186   gstelement_class->request_new_pad = gst_rtp_mux_request_new_pad;
187   gstelement_class->release_pad = gst_rtp_mux_release_pad;
188   gstelement_class->change_state = gst_rtp_mux_change_state;
189
190   klass->chain_func = gst_rtp_mux_chain;
191 }
192
193 static gboolean gst_rtp_mux_src_event (GstPad * pad,
194     GstEvent * event)
195 {
196   GstElement *rtp_mux;
197   GstIterator *iter;
198   GstPad *sinkpad;
199   gboolean result = FALSE;
200   gboolean done = FALSE;
201
202   rtp_mux = gst_pad_get_parent_element (pad);
203   g_return_val_if_fail (rtp_mux != NULL, FALSE);
204
205   iter = gst_element_iterate_sink_pads (rtp_mux);
206
207   while (!done) {
208     switch (gst_iterator_next (iter, (gpointer) &sinkpad)) {
209       case GST_ITERATOR_OK:
210         gst_event_ref (event);
211         result |= gst_pad_push_event (sinkpad, event);
212         gst_object_unref (sinkpad);
213         break;
214       case GST_ITERATOR_RESYNC:
215         gst_iterator_resync (iter);
216         result = FALSE;
217         break;
218       case GST_ITERATOR_ERROR:
219         GST_WARNING_OBJECT (rtp_mux, "Error iterating sinkpads");
220       case GST_ITERATOR_DONE:
221         done = TRUE;
222         break;
223     }
224   }
225   gst_iterator_free (iter);
226   gst_object_unref (rtp_mux);
227   gst_event_unref (event);
228
229   return result;
230 }
231
232 static void
233 gst_rtp_mux_init (GstRTPMux * rtp_mux)
234 {
235   GstElementClass *klass = GST_ELEMENT_GET_CLASS (rtp_mux);
236
237   rtp_mux->srcpad =
238       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
239           "src"), "src");
240   gst_pad_set_event_function (rtp_mux->srcpad, gst_rtp_mux_src_event);
241   gst_element_add_pad (GST_ELEMENT (rtp_mux), rtp_mux->srcpad);
242
243   rtp_mux->ssrc = DEFAULT_SSRC;
244   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
245   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
246   rtp_mux->clock_rate = DEFAULT_CLOCK_RATE;
247 }
248
249 static void
250 gst_rtp_mux_finalize (GObject * object)
251 {
252   GstRTPMux *rtp_mux;
253
254   rtp_mux = GST_RTP_MUX (object);
255
256   G_OBJECT_CLASS (parent_class)->finalize (object);
257 }
258
259 static GstPad *
260 gst_rtp_mux_create_sinkpad (GstRTPMux * rtp_mux, GstPadTemplate * templ)
261 {
262   GstPad *newpad = NULL;
263   GstPadTemplate * class_templ;
264
265   class_templ = gst_element_class_get_pad_template (
266           GST_ELEMENT_GET_CLASS (rtp_mux), "sink_%d");
267
268   if (templ == class_templ) {
269     gchar *name;
270
271     /* create new pad with the name */
272     name = g_strdup_printf ("sink_%02d", rtp_mux->numpads);
273     newpad = gst_pad_new_from_template (templ, name);
274     g_free (name);
275
276     rtp_mux->numpads++;
277   } else {
278     GST_WARNING_OBJECT (rtp_mux, "this is not our template!\n");
279   }
280
281   return newpad;
282 }
283
284 static void
285 gst_rtp_mux_setup_sinkpad (GstRTPMux * rtp_mux, GstPad * sinkpad)
286 {
287   GstRTPMuxClass *klass;
288   GstRTPMuxPadPrivate *padpriv = g_slice_new0 (GstRTPMuxPadPrivate);
289
290   klass = GST_RTP_MUX_GET_CLASS (rtp_mux);
291
292   /* setup some pad functions */
293   gst_pad_set_setcaps_function (sinkpad, gst_rtp_mux_setcaps);
294   if (klass->chain_func)
295     gst_pad_set_chain_function (sinkpad, klass->chain_func);
296   if (klass->sink_event_func)
297     gst_pad_set_event_function (sinkpad, klass->sink_event_func);
298
299   /* This could break with gstreamer 0.10.9 */
300   gst_pad_set_active (sinkpad, TRUE);
301
302   gst_pad_set_element_private (sinkpad, padpriv);
303
304   /* dd the pad to the element */
305   gst_element_add_pad (GST_ELEMENT (rtp_mux), sinkpad);
306 }
307
308 static GstPad *
309 gst_rtp_mux_request_new_pad (GstElement * element,
310     GstPadTemplate * templ, const gchar * req_name)
311 {
312   GstRTPMux *rtp_mux;
313   GstPad *newpad;
314
315   g_return_val_if_fail (templ != NULL, NULL);
316   g_return_val_if_fail (GST_IS_RTP_MUX (element), NULL);
317
318   rtp_mux = GST_RTP_MUX (element);
319
320   if (templ->direction != GST_PAD_SINK) {
321     GST_WARNING_OBJECT (rtp_mux, "request pad that is not a SINK pad");
322     return NULL;
323   }
324
325   newpad = gst_rtp_mux_create_sinkpad (rtp_mux, templ);
326   if (newpad)
327     gst_rtp_mux_setup_sinkpad (rtp_mux, newpad);
328   else
329     GST_WARNING_OBJECT (rtp_mux, "failed to create request pad");
330
331   return newpad;
332 }
333
334 static void
335 gst_rtp_mux_release_pad (GstElement * element, GstPad *pad)
336 {
337   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
338
339   if (padpriv)
340     g_slice_free (GstRTPMuxPadPrivate, padpriv);
341   gst_pad_set_element_private (pad, NULL);
342
343   gst_element_remove_pad (element, pad);
344 }
345
346 /* Put our own clock-base on the buffer */
347 static void
348 gst_rtp_mux_readjust_rtp_timestamp (GstRTPMux * rtp_mux, GstPad * pad,
349     GstBuffer * buffer)
350 {
351   guint32 ts;
352   guint32 sink_ts_base = 0;
353   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
354
355   if (padpriv->have_base)
356     sink_ts_base = padpriv->clock_base;
357
358   ts = gst_rtp_buffer_get_timestamp (buffer) - sink_ts_base + rtp_mux->ts_base;
359   GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
360       gst_rtp_buffer_get_timestamp (buffer), ts);
361   gst_rtp_buffer_set_timestamp (buffer, ts);
362 }
363
364 static GstFlowReturn
365 gst_rtp_mux_chain (GstPad * pad, GstBuffer * buffer)
366 {
367   GstRTPMux *rtp_mux;
368   GstStructure * structure;
369   GstFlowReturn ret;
370
371   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
372
373   buffer = gst_buffer_make_writable(buffer);
374
375   rtp_mux->seqnum++;
376   gst_rtp_buffer_set_seq (buffer, rtp_mux->seqnum);
377   GST_BUFFER_CAPS (buffer) = gst_caps_make_writable(GST_BUFFER_CAPS (buffer));
378   structure = gst_caps_get_structure (GST_BUFFER_CAPS (buffer), 0U);
379   gst_structure_set (structure, "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base, NULL);
380   gst_rtp_buffer_set_ssrc (buffer, rtp_mux->current_ssrc);
381   gst_rtp_mux_readjust_rtp_timestamp (rtp_mux, pad, buffer);
382   GST_LOG_OBJECT (rtp_mux, "Pushing packet size %d, seq=%d, ts=%u",
383       GST_BUFFER_SIZE (buffer), rtp_mux->seqnum,
384       gst_rtp_buffer_get_timestamp (buffer));
385
386   gst_buffer_set_caps (buffer, GST_PAD_CAPS (rtp_mux->srcpad));
387
388   ret = gst_pad_push (rtp_mux->srcpad, buffer);
389
390   gst_object_unref (rtp_mux);
391   return ret;
392 }
393
394 static gboolean
395 gst_rtp_mux_set_clock_rate (GstRTPMux *rtp_mux, gint clock_rate)
396 {
397   gint ret = TRUE;
398
399   if (rtp_mux->clock_rate == 0) {
400     rtp_mux->clock_rate = clock_rate;
401     ret = TRUE;
402   }
403
404   else if (rtp_mux->clock_rate != clock_rate) {
405     GST_WARNING_OBJECT (rtp_mux, "Clock-rate already set to: %u",
406             rtp_mux->clock_rate);
407     ret = FALSE;
408   }
409
410   return ret;
411 }
412
413 static gboolean
414 gst_rtp_mux_setcaps (GstPad *pad, GstCaps *caps)
415 {
416   GstRTPMux *rtp_mux;
417   GstStructure *structure;
418   gboolean ret = TRUE;
419   gint clock_rate;
420   GstRTPMuxPadPrivate *padpriv = gst_pad_get_element_private (pad);
421
422   rtp_mux = GST_RTP_MUX (gst_pad_get_parent (pad));
423
424   structure = gst_caps_get_structure (caps, 0);
425   if (gst_structure_get_int (structure, "clock-rate", &clock_rate)) {
426     ret = gst_rtp_mux_set_clock_rate (rtp_mux, clock_rate);
427   }
428
429   if (!ret)
430     goto out;
431
432   if (gst_structure_get_uint (structure, "clock-base", &padpriv->clock_base)) {
433     padpriv->have_base = TRUE;
434   }
435
436   caps = gst_caps_copy (caps);
437
438   gst_caps_set_simple (caps,
439       "clock-base", G_TYPE_UINT, rtp_mux->ts_base,
440       "seqnum-base", G_TYPE_UINT, rtp_mux->seqnum_base,
441       NULL);
442
443   GST_DEBUG_OBJECT (rtp_mux,
444       "setting caps %" GST_PTR_FORMAT " on src pad..", caps);
445   ret = gst_pad_set_caps (rtp_mux->srcpad, caps);
446   gst_caps_unref (caps);
447
448  out:
449   gst_object_unref (rtp_mux);
450
451   return ret;
452 }
453
454 static void
455 gst_rtp_mux_get_property (GObject * object,
456     guint prop_id, GValue * value, GParamSpec * pspec)
457 {
458   GstRTPMux *rtp_mux;
459
460   rtp_mux = GST_RTP_MUX (object);
461
462   switch (prop_id) {
463     case PROP_CLOCK_RATE:
464       g_value_set_uint (value, rtp_mux->clock_rate);
465       break;
466     case PROP_TIMESTAMP_OFFSET:
467       g_value_set_int (value, rtp_mux->ts_offset);
468       break;
469     case PROP_SEQNUM_OFFSET:
470       g_value_set_int (value, rtp_mux->seqnum_offset);
471       break;
472     case PROP_SEQNUM:
473       g_value_set_uint (value, rtp_mux->seqnum);
474       break;
475     case PROP_SSRC:
476       g_value_set_uint (value, rtp_mux->ssrc);
477       break;
478     default:
479       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480       break;
481   }
482 }
483
484 static void
485 gst_rtp_mux_set_property (GObject * object,
486     guint prop_id, const GValue * value, GParamSpec * pspec)
487 {
488   GstRTPMux *rtp_mux;
489
490   rtp_mux = GST_RTP_MUX (object);
491
492   switch (prop_id) {
493     case PROP_CLOCK_RATE:
494       rtp_mux->clock_rate = g_value_get_uint (value);
495       break;
496     case PROP_TIMESTAMP_OFFSET:
497       rtp_mux->ts_offset = g_value_get_int (value);
498       break;
499     case PROP_SEQNUM_OFFSET:
500       rtp_mux->seqnum_offset = g_value_get_int (value);
501       break;
502     case PROP_SSRC:
503       rtp_mux->ssrc = g_value_get_uint (value);
504       break;
505     default:
506       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507       break;
508   }
509 }
510
511 static void
512 gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
513 {
514   if (rtp_mux->ssrc == -1)
515     rtp_mux->current_ssrc = g_random_int ();
516   else
517     rtp_mux->current_ssrc = rtp_mux->ssrc;
518
519   if (rtp_mux->seqnum_offset == -1)
520     rtp_mux->seqnum_base = g_random_int_range (0, G_MAXUINT16);
521   else
522     rtp_mux->seqnum_base = rtp_mux->seqnum_offset;
523   rtp_mux->seqnum = rtp_mux->seqnum_base;
524
525   if (rtp_mux->ts_offset == -1)
526     rtp_mux->ts_base = g_random_int ();
527   else
528     rtp_mux->ts_base = rtp_mux->ts_offset;
529     GST_DEBUG_OBJECT (rtp_mux, "set clock-base to %u", rtp_mux->ts_base);
530 }
531
532 static GstStateChangeReturn
533 gst_rtp_mux_change_state (GstElement * element, GstStateChange transition)
534 {
535   GstRTPMux *rtp_mux;
536
537   rtp_mux = GST_RTP_MUX (element);
538
539   switch (transition) {
540     case GST_STATE_CHANGE_NULL_TO_READY:
541       break;
542     case GST_STATE_CHANGE_READY_TO_PAUSED:
543       gst_rtp_mux_ready_to_paused (rtp_mux);
544       break;
545     case GST_STATE_CHANGE_PAUSED_TO_READY:
546       break;
547     default:
548       break;
549   }
550
551   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
552 }
553
554 gboolean
555 gst_rtp_mux_plugin_init (GstPlugin * plugin)
556 {
557   GST_DEBUG_CATEGORY_INIT (gst_rtp_mux_debug, "rtpmux", 0,
558       "rtp muxer");
559
560   return gst_element_register (plugin, "rtpmux", GST_RANK_NONE,
561       GST_TYPE_RTP_MUX);
562 }
563