Rename elements to avoid conflict with farsight elements with the same name. Fixes...
[platform/upstream/gstreamer.git] / gst / rtpmanager / gstrtpptdemux.c
1 /* 
2  * RTP Demux element
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  * @author Kai Vehmanen <kai.vehmanen@nokia.com>
6  *
7  * Loosely based on GStreamer gstdecodebin
8  * Copyright (C) <2004> 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-gstrtpptdemux
28  * @short_description: separate RTP payloads based on the payload type
29  *
30  * <refsect2>
31  * <para>
32  * gstrtpptdemux acts as a demuxer for RTP packets based on the payload type of the
33  * packets. Its main purpose is to allow an application to easily receive and
34  * decode an RTP stream with multiple payload types.
35  * </para>
36  * <para>
37  * For each payload type that is detected, a new pad will be created and the
38  * ::new-payload-type signal will be emitted. When the payload for the RTP
39  * stream changes, the ::payload-type-change signal will be emitted.
40  * </para>
41  * <para>
42  * The element will try to set complete and unique application/x-rtp caps on the
43  * outgoing buffers and pads based on the result of the ::request-pt-map signal.
44  * </para>
45  * <title>Example pipelines</title>
46  * <para>
47  * <programlisting>
48  * gst-launch udpsrc caps="application/x-rtp" ! gstrtpptdemux ! fakesink
49  * </programlisting>
50  * Takes an RTP stream and send the RTP packets with the first detected payload
51  * type to fakesink, discarding the other payload types.
52  * </para>
53  * </refsect2>
54  *
55  * Last reviewed on 2007-05-28 (0.10.5)
56  */
57
58 /*
59  * Contributors:
60  * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
61  */
62 /*
63  * Status:
64  *  - works with the test_rtpdemux.c tool
65  *
66  * Check:
67  *  - is emitting a signal enough, or should we
68  *    use GstEvent to notify downstream elements
69  *    of the new packet... no?
70  *
71  * Notes:
72  *  - emits event both for new PTs, and whenever
73  *    a PT is changed
74  */
75
76 #ifdef HAVE_CONFIG_H
77 #include "config.h"
78 #endif
79
80 #include <string.h>
81 #include <gst/gst.h>
82 #include <gst/rtp/gstrtpbuffer.h>
83
84 #include "gstrtpbin-marshal.h"
85 #include "gstrtpptdemux.h"
86
87 /* generic templates */
88 static GstStaticPadTemplate rtp_pt_demux_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("sink",
90     GST_PAD_SINK,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS ("application/x-rtp")
93     );
94
95 static GstStaticPadTemplate rtp_pt_demux_src_template =
96 GST_STATIC_PAD_TEMPLATE ("src_%d",
97     GST_PAD_SRC,
98     GST_PAD_SOMETIMES,
99     GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
100     );
101
102 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
103 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
104
105 /**
106  * Item for storing GstPad<->pt pairs.
107  */
108 struct _GstRTPPtDemuxPad
109 {
110   GstPad *pad;        /**< pointer to the actual pad */
111   gint pt;             /**< RTP payload-type attached to pad */
112 };
113
114 /* signals */
115 enum
116 {
117   SIGNAL_REQUEST_PT_MAP,
118   SIGNAL_NEW_PAYLOAD_TYPE,
119   SIGNAL_PAYLOAD_TYPE_CHANGE,
120   SIGNAL_CLEAR_PT_MAP,
121   LAST_SIGNAL
122 };
123
124 GST_BOILERPLATE (GstRTPPtDemux, gst_rtp_pt_demux, GstElement, GST_TYPE_ELEMENT);
125
126 static void gst_rtp_pt_demux_finalize (GObject * object);
127
128 static void gst_rtp_pt_demux_release (GstElement * element);
129 static gboolean gst_rtp_pt_demux_setup (GstElement * element);
130
131 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
132 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
133     GstStateChange transition);
134 static void gst_rtp_pt_demux_clear_pt_map (GstRTPPtDemux * rtpdemux);
135
136 static GstPad *find_pad_for_pt (GstRTPPtDemux * rtpdemux, guint8 pt);
137
138 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
139
140 static GstElementDetails gst_rtp_pt_demux_details = {
141   "RTP Demux",
142   "Demux/Network/RTP",
143   "Parses codec streams transmitted in the same RTP session",
144   "Kai Vehmanen <kai.vehmanen@nokia.com>"
145 };
146
147 static void
148 gst_rtp_pt_demux_base_init (gpointer g_class)
149 {
150   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
151
152   gst_element_class_add_pad_template (gstelement_klass,
153       gst_static_pad_template_get (&rtp_pt_demux_sink_template));
154   gst_element_class_add_pad_template (gstelement_klass,
155       gst_static_pad_template_get (&rtp_pt_demux_src_template));
156
157   gst_element_class_set_details (gstelement_klass, &gst_rtp_pt_demux_details);
158 }
159
160 static void
161 gst_rtp_pt_demux_class_init (GstRTPPtDemuxClass * klass)
162 {
163   GObjectClass *gobject_klass;
164   GstElementClass *gstelement_klass;
165
166   gobject_klass = (GObjectClass *) klass;
167   gstelement_klass = (GstElementClass *) klass;
168
169   /**
170    * GstRTPPtDemux::request-pt-map:
171    * @demux: the object which received the signal
172    * @pt: the payload type
173    *
174    * Request the payload type as #GstCaps for @pt.
175    */
176   gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
177       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
178       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass, request_pt_map),
179       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
180       G_TYPE_UINT);
181
182   /**
183    * GstRTPPtDemux::new-payload-type:
184    * @demux: the object which received the signal
185    * @pt: the payload type
186    * @pad: the pad with the new payload
187    *
188    * Emited when a new payload type pad has been created in @demux.
189    */
190   gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
191       g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
192       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass, new_payload_type),
193       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
194       G_TYPE_UINT, GST_TYPE_PAD);
195
196   /**
197    * GstRTPPtDemux::payload-type-change:
198    * @demux: the object which received the signal
199    * @pt: the new payload type
200    *
201    * Emited when the payload type changed.
202    */
203   gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
204       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
205       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass,
206           payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
207       G_TYPE_NONE, 1, G_TYPE_UINT);
208
209   /**
210    * GstRTPPtDemux::clear-pt-map:
211    * @demux: the object which received the signal
212    *
213    * The application can call this signal to instruct the element to discard the
214    * currently cached payload type map.
215    */
216   gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
217       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
218       G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass,
219           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
220       G_TYPE_NONE, 0, G_TYPE_NONE);
221
222   gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_finalize);
223
224   gstelement_klass->change_state =
225       GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
226
227   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
228
229   GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
230       "rtpptdemux", 0, "RTP codec demuxer");
231 }
232
233 static void
234 gst_rtp_pt_demux_init (GstRTPPtDemux * ptdemux, GstRTPPtDemuxClass * g_class)
235 {
236   GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
237
238   ptdemux->sink =
239       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
240           "sink"), "sink");
241   g_assert (ptdemux->sink != NULL);
242
243   gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
244
245   gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
246 }
247
248 static void
249 gst_rtp_pt_demux_finalize (GObject * object)
250 {
251   gst_rtp_pt_demux_release (GST_ELEMENT (object));
252
253   G_OBJECT_CLASS (parent_class)->finalize (object);
254 }
255
256 static void
257 gst_rtp_pt_demux_clear_pt_map (GstRTPPtDemux * rtpdemux)
258 {
259   /* FIXME, do something */
260 }
261
262 static GstFlowReturn
263 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
264 {
265   GstFlowReturn ret = GST_FLOW_OK;
266   GstRTPPtDemux *rtpdemux;
267   GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
268   guint8 pt;
269   GstPad *srcpad;
270
271   rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
272
273   if (!gst_rtp_buffer_validate (buf))
274     goto invalid_buffer;
275
276   pt = gst_rtp_buffer_get_payload_type (buf);
277
278   GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
279
280   srcpad = find_pad_for_pt (rtpdemux, pt);
281   if (srcpad == NULL) {
282     /* new PT, create a src pad */
283     GstElementClass *klass;
284     GstPadTemplate *templ;
285     gchar *padname;
286     GstCaps *caps;
287     GstRTPPtDemuxPad *rtpdemuxpad;
288     GValue ret = { 0 };
289     GValue args[2] = { {0}
290     , {0}
291     };
292
293
294     klass = GST_ELEMENT_GET_CLASS (rtpdemux);
295     templ = gst_element_class_get_pad_template (klass, "src_%d");
296     padname = g_strdup_printf ("src_%d", pt);
297     srcpad = gst_pad_new_from_template (templ, padname);
298     gst_pad_use_fixed_caps (srcpad);
299     g_free (padname);
300
301     /* figure out the caps */
302     g_value_init (&args[0], GST_TYPE_ELEMENT);
303     g_value_set_object (&args[0], rtpdemux);
304     g_value_init (&args[1], G_TYPE_UINT);
305     g_value_set_uint (&args[1], pt);
306
307     g_value_init (&ret, GST_TYPE_CAPS);
308     g_value_set_boxed (&ret, NULL);
309
310     g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
311         &ret);
312
313     caps = g_value_get_boxed (&ret);
314     if (caps == NULL)
315       caps = GST_PAD_CAPS (rtpdemux->sink);
316     if (!caps)
317       goto no_caps;
318
319     caps = gst_caps_make_writable (caps);
320     gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
321     gst_pad_set_caps (srcpad, caps);
322
323     GST_DEBUG ("Adding pt=%d to the list.", pt);
324     rtpdemuxpad = g_new0 (GstRTPPtDemuxPad, 1);
325     rtpdemuxpad->pt = pt;
326     rtpdemuxpad->pad = srcpad;
327     rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
328
329     gst_pad_set_active (srcpad, TRUE);
330     gst_element_add_pad (element, srcpad);
331
332     GST_DEBUG ("emitting new-payload_type for pt %d", pt);
333     g_signal_emit (G_OBJECT (rtpdemux),
334         gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
335   }
336
337   if (pt != rtpdemux->last_pt) {
338     gint emit_pt = pt;
339
340     /* our own signal with an extra flag that this is the only pad */
341     rtpdemux->last_pt = pt;
342     GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
343     g_signal_emit (G_OBJECT (rtpdemux),
344         gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
345   }
346
347   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
348
349   /* push to srcpad */
350   if (srcpad)
351     ret = gst_pad_push (srcpad, GST_BUFFER (buf));
352
353   return ret;
354
355   /* ERRORS */
356 invalid_buffer:
357   {
358     /* this is fatal and should be filtered earlier */
359     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
360         ("Dropping invalid RTP payload"));
361     gst_buffer_unref (buf);
362     return GST_FLOW_ERROR;
363   }
364 no_caps:
365   {
366     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
367         ("Could not get caps for payload"));
368     gst_buffer_unref (buf);
369     return GST_FLOW_ERROR;
370   }
371 }
372
373 static GstPad *
374 find_pad_for_pt (GstRTPPtDemux * rtpdemux, guint8 pt)
375 {
376   GstPad *respad = NULL;
377   GSList *item = rtpdemux->srcpads;
378
379   for (; item; item = g_slist_next (item)) {
380     GstRTPPtDemuxPad *pad = item->data;
381
382     if (pad->pt == pt) {
383       respad = pad->pad;
384       break;
385     }
386   }
387
388   return respad;
389 }
390
391 /**
392  * Reserves resources for the object.
393  */
394 static gboolean
395 gst_rtp_pt_demux_setup (GstElement * element)
396 {
397   GstRTPPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
398   gboolean res = TRUE;
399
400   if (ptdemux) {
401     ptdemux->srcpads = NULL;
402     ptdemux->last_pt = 0xFFFF;
403   }
404
405   return res;
406 }
407
408 /**
409  * Free resources for the object.
410  */
411 static void
412 gst_rtp_pt_demux_release (GstElement * element)
413 {
414   GstRTPPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
415
416   if (ptdemux) {
417     /* note: GstElement's dispose() will handle the pads */
418     g_slist_free (ptdemux->srcpads);
419     ptdemux->srcpads = NULL;
420   }
421 }
422
423 static GstStateChangeReturn
424 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
425 {
426   GstStateChangeReturn ret;
427   GstRTPPtDemux *ptdemux;
428
429   ptdemux = GST_RTP_PT_DEMUX (element);
430
431   switch (transition) {
432     case GST_STATE_CHANGE_NULL_TO_READY:
433       if (gst_rtp_pt_demux_setup (element) != TRUE)
434         ret = GST_STATE_CHANGE_FAILURE;
435       break;
436     case GST_STATE_CHANGE_READY_TO_PAUSED:
437     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
438     default:
439       break;
440   }
441
442   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
443
444   switch (transition) {
445     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
446     case GST_STATE_CHANGE_PAUSED_TO_READY:
447       break;
448     case GST_STATE_CHANGE_READY_TO_NULL:
449       gst_rtp_pt_demux_release (element);
450       break;
451     default:
452       break;
453   }
454
455   return ret;
456 }