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