gst/rtpmanager/: Clean up the dynamic pads when going to READY.
[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  * @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   caps = g_value_get_boxed (&ret);
277   if (caps == NULL)
278     caps = GST_PAD_CAPS (rtpdemux->sink);
279
280   GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
281
282   return caps;
283 }
284
285 static void
286 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
287 {
288   GSList *walk;
289
290   GST_OBJECT_LOCK (rtpdemux);
291   GST_DEBUG ("clearing pt map");
292   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
293     GstRtpPtDemuxPad *pad = walk->data;
294
295     pad->newcaps = TRUE;
296   }
297   GST_OBJECT_UNLOCK (rtpdemux);
298 }
299
300 static GstFlowReturn
301 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
302 {
303   GstFlowReturn ret = GST_FLOW_OK;
304   GstRtpPtDemux *rtpdemux;
305   GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
306   guint8 pt;
307   GstPad *srcpad;
308   GstRtpPtDemuxPad *rtpdemuxpad;
309   GstCaps *caps;
310
311   rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
312
313   if (!gst_rtp_buffer_validate (buf))
314     goto invalid_buffer;
315
316   pt = gst_rtp_buffer_get_payload_type (buf);
317
318   GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
319
320   rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
321   if (rtpdemuxpad == NULL) {
322     /* new PT, create a src pad */
323     GstElementClass *klass;
324     GstPadTemplate *templ;
325     gchar *padname;
326
327     klass = GST_ELEMENT_GET_CLASS (rtpdemux);
328     templ = gst_element_class_get_pad_template (klass, "src_%d");
329     padname = g_strdup_printf ("src_%d", pt);
330     srcpad = gst_pad_new_from_template (templ, padname);
331     gst_pad_use_fixed_caps (srcpad);
332     g_free (padname);
333
334     caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
335     if (!caps)
336       goto no_caps;
337
338     caps = gst_caps_make_writable (caps);
339     gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
340     gst_pad_set_caps (srcpad, 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     rtpdemuxpad->newcaps = FALSE;
381   }
382
383   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
384
385   /* push to srcpad */
386   ret = gst_pad_push (srcpad, buf);
387
388   return ret;
389
390   /* ERRORS */
391 invalid_buffer:
392   {
393     /* this is fatal and should be filtered earlier */
394     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
395         ("Dropping invalid RTP payload"));
396     gst_buffer_unref (buf);
397     return GST_FLOW_ERROR;
398   }
399 no_caps:
400   {
401     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
402         ("Could not get caps for payload"));
403     gst_buffer_unref (buf);
404     return GST_FLOW_ERROR;
405   }
406 }
407
408 static GstRtpPtDemuxPad *
409 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
410 {
411   GstRtpPtDemuxPad *respad = NULL;
412   GSList *walk;
413
414   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
415     GstRtpPtDemuxPad *pad = walk->data;
416
417     if (pad->pt == pt) {
418       respad = pad;
419       break;
420     }
421   }
422   return respad;
423 }
424
425 /**
426  * Reserves resources for the object.
427  */
428 static gboolean
429 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
430 {
431   ptdemux->srcpads = NULL;
432   ptdemux->last_pt = 0xFFFF;
433
434   return TRUE;
435 }
436
437 /**
438  * Free resources for the object.
439  */
440 static void
441 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
442 {
443   GSList *walk;
444
445   for (walk = ptdemux->srcpads; walk; walk = g_slist_next (walk)) {
446     GstRtpPtDemuxPad *pad = walk->data;
447
448     gst_pad_set_active (pad->pad, FALSE);
449     gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
450     g_free (pad);
451   }
452   g_slist_free (ptdemux->srcpads);
453   ptdemux->srcpads = NULL;
454 }
455
456 static GstStateChangeReturn
457 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
458 {
459   GstStateChangeReturn ret;
460   GstRtpPtDemux *ptdemux;
461
462   ptdemux = GST_RTP_PT_DEMUX (element);
463
464   switch (transition) {
465     case GST_STATE_CHANGE_NULL_TO_READY:
466       if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
467         ret = GST_STATE_CHANGE_FAILURE;
468       break;
469     case GST_STATE_CHANGE_READY_TO_PAUSED:
470     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
471     default:
472       break;
473   }
474
475   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
476
477   switch (transition) {
478     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
479     case GST_STATE_CHANGE_PAUSED_TO_READY:
480       break;
481     case GST_STATE_CHANGE_READY_TO_NULL:
482       gst_rtp_pt_demux_release (ptdemux);
483       break;
484     default:
485       break;
486   }
487
488   return ret;
489 }