Merge branch 'master' into 0.11
[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  *
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 #define gst_rtp_pt_demux_parent_class parent_class
121 G_DEFINE_TYPE (GstRtpPtDemux, gst_rtp_pt_demux, GST_TYPE_ELEMENT);
122
123 static void gst_rtp_pt_demux_finalize (GObject * object);
124
125 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
126 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
127
128 static gboolean gst_rtp_pt_demux_sink_event (GstPad * pad, GstEvent * event);
129 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
130 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
131     GstStateChange transition);
132 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
133
134 static GstRtpPtDemuxPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
135
136 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event);
137
138
139 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
140
141 static void
142 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
143 {
144   GObjectClass *gobject_klass;
145   GstElementClass *gstelement_klass;
146
147   gobject_klass = (GObjectClass *) klass;
148   gstelement_klass = (GstElementClass *) klass;
149
150   /**
151    * GstRtpPtDemux::request-pt-map:
152    * @demux: the object which received the signal
153    * @pt: the payload type
154    *
155    * Request the payload type as #GstCaps for @pt.
156    */
157   gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
158       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
159       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
160       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
161       G_TYPE_UINT);
162
163   /**
164    * GstRtpPtDemux::new-payload-type:
165    * @demux: the object which received the signal
166    * @pt: the payload type
167    * @pad: the pad with the new payload
168    *
169    * Emited when a new payload type pad has been created in @demux.
170    */
171   gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
172       g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
173       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
174       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
175       G_TYPE_UINT, GST_TYPE_PAD);
176
177   /**
178    * GstRtpPtDemux::payload-type-change:
179    * @demux: the object which received the signal
180    * @pt: the new payload type
181    *
182    * Emited when the payload type changed.
183    */
184   gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
185       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
186       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
187           payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
188       G_TYPE_NONE, 1, G_TYPE_UINT);
189
190   /**
191    * GstRtpPtDemux::clear-pt-map:
192    * @demux: the object which received the signal
193    *
194    * The application can call this signal to instruct the element to discard the
195    * currently cached payload type map.
196    */
197   gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
198       g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
199       G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
200           clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
201       G_TYPE_NONE, 0, G_TYPE_NONE);
202
203   gobject_klass->finalize = gst_rtp_pt_demux_finalize;
204
205   gstelement_klass->change_state =
206       GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
207
208   klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
209
210   gst_element_class_add_pad_template (gstelement_klass,
211       gst_static_pad_template_get (&rtp_pt_demux_sink_template));
212   gst_element_class_add_pad_template (gstelement_klass,
213       gst_static_pad_template_get (&rtp_pt_demux_src_template));
214
215   gst_element_class_set_details_simple (gstelement_klass, "RTP Demux",
216       "Demux/Network/RTP",
217       "Parses codec streams transmitted in the same RTP session",
218       "Kai Vehmanen <kai.vehmanen@nokia.com>");
219
220   GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
221       "rtpptdemux", 0, "RTP codec demuxer");
222 }
223
224 static void
225 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux)
226 {
227   GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
228
229   ptdemux->sink =
230       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
231           "sink"), "sink");
232   g_assert (ptdemux->sink != NULL);
233
234   gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
235   gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
236
237   gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
238 }
239
240 static void
241 gst_rtp_pt_demux_finalize (GObject * object)
242 {
243   gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
244
245   G_OBJECT_CLASS (parent_class)->finalize (object);
246 }
247
248 static GstCaps *
249 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
250 {
251   GstCaps *caps;
252   GValue ret = { 0 };
253   GValue args[2] = { {0}, {0} };
254
255   /* figure out the caps */
256   g_value_init (&args[0], GST_TYPE_ELEMENT);
257   g_value_set_object (&args[0], rtpdemux);
258   g_value_init (&args[1], G_TYPE_UINT);
259   g_value_set_uint (&args[1], pt);
260
261   g_value_init (&ret, GST_TYPE_CAPS);
262   g_value_set_boxed (&ret, NULL);
263
264   g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
265       &ret);
266
267   g_value_unset (&args[0]);
268   g_value_unset (&args[1]);
269   caps = g_value_dup_boxed (&ret);
270   g_value_unset (&ret);
271   if (caps == NULL) {
272     caps = gst_pad_get_current_caps (rtpdemux->sink);
273   }
274
275   GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
276
277   return caps;
278 }
279
280 static void
281 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
282 {
283   GSList *walk;
284
285   GST_OBJECT_LOCK (rtpdemux);
286   GST_DEBUG ("clearing pt map");
287   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
288     GstRtpPtDemuxPad *pad = walk->data;
289
290     pad->newcaps = TRUE;
291   }
292   GST_OBJECT_UNLOCK (rtpdemux);
293 }
294
295 static GstFlowReturn
296 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
297 {
298   GstFlowReturn ret = GST_FLOW_OK;
299   GstRtpPtDemux *rtpdemux;
300   GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
301   guint8 pt;
302   GstPad *srcpad;
303   GstRtpPtDemuxPad *rtpdemuxpad;
304   GstCaps *caps;
305   GstRTPBuffer rtp;
306
307   rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
308
309   if (!gst_rtp_buffer_validate (buf))
310     goto invalid_buffer;
311
312   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
313   pt = gst_rtp_buffer_get_payload_type (&rtp);
314   gst_rtp_buffer_unmap (&rtp);
315
316   GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
317
318   rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
319   if (rtpdemuxpad == NULL) {
320     /* new PT, create a src pad */
321     GstElementClass *klass;
322     GstPadTemplate *templ;
323     gchar *padname;
324
325     klass = GST_ELEMENT_GET_CLASS (rtpdemux);
326     templ = gst_element_class_get_pad_template (klass, "src_%d");
327     padname = g_strdup_printf ("src_%d", pt);
328     srcpad = gst_pad_new_from_template (templ, padname);
329     gst_pad_use_fixed_caps (srcpad);
330     g_free (padname);
331     gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
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   /* push to srcpad */
385   ret = gst_pad_push (srcpad, buf);
386
387   return ret;
388
389   /* ERRORS */
390 invalid_buffer:
391   {
392     /* this is fatal and should be filtered earlier */
393     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
394         ("Dropping invalid RTP payload"));
395     gst_buffer_unref (buf);
396     return GST_FLOW_ERROR;
397   }
398 no_caps:
399   {
400     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
401         ("Could not get caps for payload"));
402     gst_buffer_unref (buf);
403     return GST_FLOW_ERROR;
404   }
405 }
406
407 static GstRtpPtDemuxPad *
408 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
409 {
410   GstRtpPtDemuxPad *respad = NULL;
411   GSList *walk;
412
413   for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
414     GstRtpPtDemuxPad *pad = walk->data;
415
416     if (pad->pt == pt) {
417       respad = pad;
418       break;
419     }
420   }
421   return respad;
422 }
423
424 static gboolean
425 gst_rtp_pt_demux_sink_event (GstPad * pad, GstEvent * event)
426 {
427   GstRtpPtDemux *rtpdemux;
428   gboolean res = FALSE;
429
430   rtpdemux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
431   if (G_UNLIKELY (rtpdemux == NULL)) {
432     gst_event_unref (event);
433     return FALSE;
434   }
435
436   switch (GST_EVENT_TYPE (event)) {
437     case GST_EVENT_CUSTOM_DOWNSTREAM:
438     {
439       const GstStructure *s;
440
441       s = gst_event_get_structure (event);
442
443       if (gst_structure_has_name (s, "GstRTPPacketLost")) {
444         GstRtpPtDemuxPad *rtpdemuxpad =
445             find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
446
447         if (rtpdemuxpad)
448           res = gst_pad_push_event (rtpdemuxpad->pad, event);
449         else
450           gst_event_unref (event);
451
452       } else {
453         res = gst_pad_event_default (pad, event);
454       }
455       break;
456     }
457     default:
458       res = gst_pad_event_default (pad, event);
459       break;
460   }
461
462   gst_object_unref (rtpdemux);
463   return res;
464 }
465
466
467 static gboolean
468 gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event)
469 {
470   GstRtpPtDemux *demux;
471   const GstStructure *s;
472
473   demux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
474
475   switch (GST_EVENT_TYPE (event)) {
476     case GST_EVENT_CUSTOM_UPSTREAM:
477     case GST_EVENT_CUSTOM_BOTH:
478     case GST_EVENT_CUSTOM_BOTH_OOB:
479       s = gst_event_get_structure (event);
480       if (s && !gst_structure_has_field (s, "payload")) {
481         GSList *walk;
482
483         for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
484           GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
485
486           if (dpad->pad == pad) {
487             GstStructure *ws;
488
489             event =
490                 GST_EVENT_CAST (gst_mini_object_make_writable
491                 (GST_MINI_OBJECT_CAST (event)));
492             ws = gst_event_writable_structure (event);
493             gst_structure_set (ws, "payload", G_TYPE_UINT, dpad->pt, NULL);
494             break;
495           }
496         }
497       }
498       break;
499     default:
500       break;
501   }
502
503   gst_object_unref (demux);
504
505   return gst_pad_event_default (pad, event);
506 }
507
508
509
510 /*
511  * Reserves resources for the object.
512  */
513 static gboolean
514 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
515 {
516   ptdemux->srcpads = NULL;
517   ptdemux->last_pt = 0xFFFF;
518
519   return TRUE;
520 }
521
522 /*
523  * Free resources for the object.
524  */
525 static void
526 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
527 {
528   GSList *walk;
529
530   for (walk = ptdemux->srcpads; walk; walk = g_slist_next (walk)) {
531     GstRtpPtDemuxPad *pad = walk->data;
532
533     gst_pad_set_active (pad->pad, FALSE);
534     gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
535     g_free (pad);
536   }
537   g_slist_free (ptdemux->srcpads);
538   ptdemux->srcpads = NULL;
539 }
540
541 static GstStateChangeReturn
542 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
543 {
544   GstStateChangeReturn ret;
545   GstRtpPtDemux *ptdemux;
546
547   ptdemux = GST_RTP_PT_DEMUX (element);
548
549   switch (transition) {
550     case GST_STATE_CHANGE_NULL_TO_READY:
551       if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
552         ret = GST_STATE_CHANGE_FAILURE;
553       break;
554     case GST_STATE_CHANGE_READY_TO_PAUSED:
555     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
556     default:
557       break;
558   }
559
560   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
561
562   switch (transition) {
563     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
564     case GST_STATE_CHANGE_PAUSED_TO_READY:
565       break;
566     case GST_STATE_CHANGE_READY_TO_NULL:
567       gst_rtp_pt_demux_release (ptdemux);
568       break;
569     default:
570       break;
571   }
572
573   return ret;
574 }