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