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