gst/rtpmanager/gstrtpbin.c: Ref caps when inserting into the cache.
[platform/upstream/gst-plugins-good.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  * @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   gboolean newcaps;
113 };
114
115 /* signals */
116 enum
117 {
118   SIGNAL_REQUEST_PT_MAP,
119   SIGNAL_NEW_PAYLOAD_TYPE,
120   SIGNAL_PAYLOAD_TYPE_CHANGE,
121   SIGNAL_CLEAR_PT_MAP,
122   LAST_SIGNAL
123 };
124
125 GST_BOILERPLATE (GstRtpPtDemux, gst_rtp_pt_demux, GstElement, GST_TYPE_ELEMENT);
126
127 static void gst_rtp_pt_demux_finalize (GObject * object);
128
129 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
130 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
131
132 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
133 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
134     GstStateChange transition);
135 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
136
137 static GstRtpPtDemuxPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
138
139 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
140
141 static GstElementDetails gst_rtp_pt_demux_details = {
142   "RTP Demux",
143   "Demux/Network/RTP",
144   "Parses codec streams transmitted in the same RTP session",
145   "Kai Vehmanen <kai.vehmanen@nokia.com>"
146 };
147
148 static void
149 gst_rtp_pt_demux_base_init (gpointer g_class)
150 {
151   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
152
153   gst_element_class_add_pad_template (gstelement_klass,
154       gst_static_pad_template_get (&rtp_pt_demux_sink_template));
155   gst_element_class_add_pad_template (gstelement_klass,
156       gst_static_pad_template_get (&rtp_pt_demux_src_template));
157
158   gst_element_class_set_details (gstelement_klass, &gst_rtp_pt_demux_details);
159 }
160
161 static void
162 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
163 {
164   GObjectClass *gobject_klass;
165   GstElementClass *gstelement_klass;
166
167   gobject_klass = (GObjectClass *) klass;
168   gstelement_klass = (GstElementClass *) klass;
169
170   /**
171    * GstRtpPtDemux::request-pt-map:
172    * @demux: the object which received the signal
173    * @pt: the payload type
174    *
175    * Request the payload type as #GstCaps for @pt.
176    */
177   gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
178       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
179       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
180       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
181       G_TYPE_UINT);
182
183   /**
184    * GstRtpPtDemux::new-payload-type:
185    * @demux: the object which received the signal
186    * @pt: the payload type
187    * @pad: the pad with the new payload
188    *
189    * Emited when a new payload type pad has been created in @demux.
190    */
191   gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
192       g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
193       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
194       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
195       G_TYPE_UINT, GST_TYPE_PAD);
196
197   /**
198    * GstRtpPtDemux::payload-type-change:
199    * @demux: the object which received the signal
200    * @pt: the new payload type
201    *
202    * Emited when the payload type changed.
203    */
204   gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
205       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
206       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
207           payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
208       G_TYPE_NONE, 1, G_TYPE_UINT);
209
210   /**
211    * GstRtpPtDemux::clear-pt-map:
212    * @demux: the object which received the signal
213    *
214    * The application can call this signal to instruct the element to discard the
215    * currently cached payload type map.
216    */
217   gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
218       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
219       G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
220           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
221       G_TYPE_NONE, 0, G_TYPE_NONE);
222
223   gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_finalize);
224
225   gstelement_klass->change_state =
226       GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
227
228   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
229
230   GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
231       "rtpptdemux", 0, "RTP codec demuxer");
232 }
233
234 static void
235 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux, GstRtpPtDemuxClass * g_class)
236 {
237   GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
238
239   ptdemux->sink =
240       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
241           "sink"), "sink");
242   g_assert (ptdemux->sink != NULL);
243
244   gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
245
246   gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
247 }
248
249 static void
250 gst_rtp_pt_demux_finalize (GObject * object)
251 {
252   gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
253
254   G_OBJECT_CLASS (parent_class)->finalize (object);
255 }
256
257 static GstCaps *
258 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
259 {
260   GstCaps *caps;
261   GValue ret = { 0 };
262   GValue args[2] = { {0}, {0} };
263
264   /* figure out the caps */
265   g_value_init (&args[0], GST_TYPE_ELEMENT);
266   g_value_set_object (&args[0], rtpdemux);
267   g_value_init (&args[1], G_TYPE_UINT);
268   g_value_set_uint (&args[1], pt);
269
270   g_value_init (&ret, GST_TYPE_CAPS);
271   g_value_set_boxed (&ret, NULL);
272
273   g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
274       &ret);
275
276   g_value_unset (&args[0]);
277   g_value_unset (&args[1]);
278   caps = g_value_dup_boxed (&ret);
279   g_value_unset (&ret);
280   if (caps == NULL) {
281     caps = GST_PAD_CAPS (rtpdemux->sink);
282     if (caps)
283       gst_caps_ref (caps);
284   }
285
286   GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
287
288   return caps;
289 }
290
291 static void
292 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
293 {
294   GSList *walk;
295
296   GST_OBJECT_LOCK (rtpdemux);
297   GST_DEBUG ("clearing pt map");
298   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
299     GstRtpPtDemuxPad *pad = walk->data;
300
301     pad->newcaps = TRUE;
302   }
303   GST_OBJECT_UNLOCK (rtpdemux);
304 }
305
306 static GstFlowReturn
307 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
308 {
309   GstFlowReturn ret = GST_FLOW_OK;
310   GstRtpPtDemux *rtpdemux;
311   GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
312   guint8 pt;
313   GstPad *srcpad;
314   GstRtpPtDemuxPad *rtpdemuxpad;
315   GstCaps *caps;
316
317   rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
318
319   if (!gst_rtp_buffer_validate (buf))
320     goto invalid_buffer;
321
322   pt = gst_rtp_buffer_get_payload_type (buf);
323
324   GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
325
326   rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
327   if (rtpdemuxpad == NULL) {
328     /* new PT, create a src pad */
329     GstElementClass *klass;
330     GstPadTemplate *templ;
331     gchar *padname;
332
333     klass = GST_ELEMENT_GET_CLASS (rtpdemux);
334     templ = gst_element_class_get_pad_template (klass, "src_%d");
335     padname = g_strdup_printf ("src_%d", pt);
336     srcpad = gst_pad_new_from_template (templ, padname);
337     gst_pad_use_fixed_caps (srcpad);
338     g_free (padname);
339
340     caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
341     if (!caps)
342       goto no_caps;
343
344     caps = gst_caps_make_writable (caps);
345     gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
346     gst_pad_set_caps (srcpad, caps);
347     gst_caps_unref (caps);
348
349     GST_DEBUG ("Adding pt=%d to the list.", pt);
350     rtpdemuxpad = g_new0 (GstRtpPtDemuxPad, 1);
351     rtpdemuxpad->pt = pt;
352     rtpdemuxpad->newcaps = FALSE;
353     rtpdemuxpad->pad = srcpad;
354     GST_OBJECT_LOCK (rtpdemux);
355     rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
356     GST_OBJECT_UNLOCK (rtpdemux);
357
358     gst_pad_set_active (srcpad, TRUE);
359     gst_element_add_pad (element, srcpad);
360
361     GST_DEBUG ("emitting new-payload_type for pt %d", pt);
362     g_signal_emit (G_OBJECT (rtpdemux),
363         gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
364   }
365
366   srcpad = rtpdemuxpad->pad;
367
368   if (pt != rtpdemux->last_pt) {
369     gint emit_pt = pt;
370
371     /* our own signal with an extra flag that this is the only pad */
372     rtpdemux->last_pt = pt;
373     GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
374     g_signal_emit (G_OBJECT (rtpdemux),
375         gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
376   }
377
378   if (rtpdemuxpad->newcaps) {
379     GST_DEBUG ("need new caps");
380     caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
381     if (!caps)
382       goto no_caps;
383
384     caps = gst_caps_make_writable (caps);
385     gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
386     gst_pad_set_caps (srcpad, caps);
387     gst_caps_unref (caps);
388     rtpdemuxpad->newcaps = FALSE;
389   }
390
391   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
392
393   /* push to srcpad */
394   ret = gst_pad_push (srcpad, buf);
395
396   return ret;
397
398   /* ERRORS */
399 invalid_buffer:
400   {
401     /* this is fatal and should be filtered earlier */
402     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
403         ("Dropping invalid RTP payload"));
404     gst_buffer_unref (buf);
405     return GST_FLOW_ERROR;
406   }
407 no_caps:
408   {
409     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
410         ("Could not get caps for payload"));
411     gst_buffer_unref (buf);
412     return GST_FLOW_ERROR;
413   }
414 }
415
416 static GstRtpPtDemuxPad *
417 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
418 {
419   GstRtpPtDemuxPad *respad = NULL;
420   GSList *walk;
421
422   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
423     GstRtpPtDemuxPad *pad = walk->data;
424
425     if (pad->pt == pt) {
426       respad = pad;
427       break;
428     }
429   }
430   return respad;
431 }
432
433 /**
434  * Reserves resources for the object.
435  */
436 static gboolean
437 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
438 {
439   ptdemux->srcpads = NULL;
440   ptdemux->last_pt = 0xFFFF;
441
442   return TRUE;
443 }
444
445 /**
446  * Free resources for the object.
447  */
448 static void
449 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
450 {
451   GSList *walk;
452
453   for (walk = ptdemux->srcpads; walk; walk = g_slist_next (walk)) {
454     GstRtpPtDemuxPad *pad = walk->data;
455
456     gst_pad_set_active (pad->pad, FALSE);
457     gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
458     g_free (pad);
459   }
460   g_slist_free (ptdemux->srcpads);
461   ptdemux->srcpads = NULL;
462 }
463
464 static GstStateChangeReturn
465 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
466 {
467   GstStateChangeReturn ret;
468   GstRtpPtDemux *ptdemux;
469
470   ptdemux = GST_RTP_PT_DEMUX (element);
471
472   switch (transition) {
473     case GST_STATE_CHANGE_NULL_TO_READY:
474       if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
475         ret = GST_STATE_CHANGE_FAILURE;
476       break;
477     case GST_STATE_CHANGE_READY_TO_PAUSED:
478     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
479     default:
480       break;
481   }
482
483   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
484
485   switch (transition) {
486     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
487     case GST_STATE_CHANGE_PAUSED_TO_READY:
488       break;
489     case GST_STATE_CHANGE_READY_TO_NULL:
490       gst_rtp_pt_demux_release (ptdemux);
491       break;
492     default:
493       break;
494   }
495
496   return ret;
497 }