gst/rtpmanager/async_jitter_queue.c: Fix the case where the buffer underruns and...
[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@fluendo.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  * Contributors:
28  * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
29  */
30
31 /*
32  * Status:
33  *  - works with the test_rtpdemux.c tool
34  *
35  * Check:
36  *  - is emitting a signal enough, or should we
37  *    use GstEvent to notify downstream elements
38  *    of the new packet... no?
39  *
40  * Notes:
41  *  - emits event both for new PTs, and whenever
42  *    a PT is changed
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <string.h>
50 #include <gst/gst.h>
51 #include <gst/rtp/gstrtpbuffer.h>
52
53 #include "gstrtpbin-marshal.h"
54 #include "gstrtpptdemux.h"
55
56 /* generic templates */
57 static GstStaticPadTemplate rtp_pt_demux_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp")
62     );
63
64 static GstStaticPadTemplate rtp_pt_demux_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src_%d",
66     GST_PAD_SRC,
67     GST_PAD_SOMETIMES,
68     GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
69     );
70
71 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
72 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
73
74 /**
75  * Item for storing GstPad<->pt pairs.
76  */
77 struct _GstRTPPtDemuxPad
78 {
79   GstPad *pad;        /**< pointer to the actual pad */
80   gint pt;             /**< RTP payload-type attached to pad */
81 };
82
83 /* signals */
84 enum
85 {
86   SIGNAL_REQUEST_PT_MAP,
87   SIGNAL_NEW_PAYLOAD_TYPE,
88   SIGNAL_PAYLOAD_TYPE_CHANGE,
89   LAST_SIGNAL
90 };
91
92 GST_BOILERPLATE (GstRTPPtDemux, gst_rtp_pt_demux, GstElement, GST_TYPE_ELEMENT);
93
94 static void gst_rtp_pt_demux_finalize (GObject * object);
95
96 static void gst_rtp_pt_demux_release (GstElement * element);
97 static gboolean gst_rtp_pt_demux_setup (GstElement * element);
98
99 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
100 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
101     GstStateChange transition);
102
103 static GstPad *find_pad_for_pt (GstRTPPtDemux * rtpdemux, guint8 pt);
104
105 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
106
107 static GstElementDetails gst_rtp_pt_demux_details = {
108   "RTP Demux",
109   /* XXX: what's the correct hierarchy? */
110   "Codec/Demux/Network",
111   "Parses codec streams transmitted in the same RTP session",
112   "Kai Vehmanen <kai.vehmanen@nokia.com>"
113 };
114
115 static void
116 gst_rtp_pt_demux_base_init (gpointer g_class)
117 {
118   GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
119
120   gst_element_class_add_pad_template (gstelement_klass,
121       gst_static_pad_template_get (&rtp_pt_demux_sink_template));
122   gst_element_class_add_pad_template (gstelement_klass,
123       gst_static_pad_template_get (&rtp_pt_demux_src_template));
124
125   gst_element_class_set_details (gstelement_klass, &gst_rtp_pt_demux_details);
126 }
127
128 static void
129 gst_rtp_pt_demux_class_init (GstRTPPtDemuxClass * klass)
130 {
131   GObjectClass *gobject_klass;
132   GstElementClass *gstelement_klass;
133
134   gobject_klass = (GObjectClass *) klass;
135   gstelement_klass = (GstElementClass *) klass;
136
137   /**
138    * GstRTPPtDemux::request-pt-map:
139    * @demux: the object which received the signal
140    * @pt: the payload type
141    *
142    * Request the payload type as #GstCaps for @pt.
143    */
144   gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
145       g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
146       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass, request_pt_map),
147       NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
148       G_TYPE_UINT);
149
150   /**
151    * GstRTPPtDemux::new-payload-type
152    * @demux: the object which received the signal
153    * @pt: the payload type
154    * @pad: the pad with the new payload
155    *
156    * Emited when a new payload type pad has been created in @demux.
157    */
158   gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
159       g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
160       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass, new_payload_type),
161       NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
162       G_TYPE_UINT, GST_TYPE_PAD);
163
164   /**
165    * GstRTPPtDemux::payload-type-change
166    * @demux: the object which received the signal
167    * @pt: the new payload type
168    *
169    * Emited when the payload type changed.
170    */
171   gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
172       g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
173       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTPPtDemuxClass,
174           payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
175       G_TYPE_NONE, 1, G_TYPE_UINT);
176
177   gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_finalize);
178
179   gstelement_klass->change_state =
180       GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
181
182   GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
183       "rtpptdemux", 0, "RTP codec demuxer");
184
185 }
186
187 static void
188 gst_rtp_pt_demux_init (GstRTPPtDemux * ptdemux, GstRTPPtDemuxClass * g_class)
189 {
190   GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
191
192   ptdemux->sink =
193       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
194           "sink"), "sink");
195   g_assert (ptdemux->sink != NULL);
196
197   gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
198
199   gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
200 }
201
202 static void
203 gst_rtp_pt_demux_finalize (GObject * object)
204 {
205   gst_rtp_pt_demux_release (GST_ELEMENT (object));
206
207   G_OBJECT_CLASS (parent_class)->finalize (object);
208 }
209
210 static GstFlowReturn
211 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
212 {
213   GstFlowReturn ret = GST_FLOW_OK;
214   GstRTPPtDemux *rtpdemux;
215   GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
216   guint8 pt;
217   GstPad *srcpad;
218
219   rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
220
221   if (!gst_rtp_buffer_validate (buf))
222     goto invalid_buffer;
223
224   pt = gst_rtp_buffer_get_payload_type (buf);
225
226   GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
227
228   srcpad = find_pad_for_pt (rtpdemux, pt);
229   if (srcpad == NULL) {
230     /* new PT, create a src pad */
231     GstElementClass *klass;
232     GstPadTemplate *templ;
233     gchar *padname;
234     GstCaps *caps;
235     GstRTPPtDemuxPad *rtpdemuxpad;
236     GValue ret = { 0 };
237     GValue args[2] = { {0}
238     , {0}
239     };
240
241
242     klass = GST_ELEMENT_GET_CLASS (rtpdemux);
243     templ = gst_element_class_get_pad_template (klass, "src_%d");
244     padname = g_strdup_printf ("src_%d", pt);
245     srcpad = gst_pad_new_from_template (templ, padname);
246     g_free (padname);
247
248     /* figure out the caps */
249     g_value_init (&args[0], GST_TYPE_ELEMENT);
250     g_value_set_object (&args[0], rtpdemux);
251     g_value_init (&args[1], G_TYPE_UINT);
252     g_value_set_uint (&args[1], pt);
253
254     g_value_init (&ret, GST_TYPE_CAPS);
255     g_value_set_boxed (&ret, NULL);
256
257     g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
258         &ret);
259
260     caps = g_value_get_boxed (&ret);
261     if (caps == NULL)
262       caps = GST_PAD_CAPS (rtpdemux->sink);
263     if (!caps)
264       goto no_caps;
265
266     caps = gst_caps_make_writable (caps);
267     gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
268     gst_pad_set_caps (srcpad, caps);
269
270     GST_DEBUG ("Adding pt=%d to the list.", pt);
271     rtpdemuxpad = g_new0 (GstRTPPtDemuxPad, 1);
272     rtpdemuxpad->pt = pt;
273     rtpdemuxpad->pad = srcpad;
274     rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
275
276     gst_pad_set_active (srcpad, TRUE);
277     gst_element_add_pad (element, srcpad);
278
279     GST_DEBUG ("emitting new-payload_type for pt %d", pt);
280     g_signal_emit (G_OBJECT (rtpdemux),
281         gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
282   }
283
284   if (pt != rtpdemux->last_pt) {
285     gint emit_pt = pt;
286
287     /* our own signal with an extra flag that this is the only pad */
288     rtpdemux->last_pt = pt;
289     GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
290     g_signal_emit (G_OBJECT (rtpdemux),
291         gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
292   }
293
294   gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
295
296   /* push to srcpad */
297   if (srcpad)
298     ret = gst_pad_push (srcpad, GST_BUFFER (buf));
299
300   return ret;
301
302   /* ERRORS */
303 invalid_buffer:
304   {
305     /* this is fatal and should be filtered earlier */
306     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
307         ("Dropping invalid RTP payload"));
308     gst_buffer_unref (buf);
309     return GST_FLOW_ERROR;
310   }
311 no_caps:
312   {
313     GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
314         ("Could not get caps for payload"));
315     gst_buffer_unref (buf);
316     return GST_FLOW_ERROR;
317   }
318 }
319
320 static GstPad *
321 find_pad_for_pt (GstRTPPtDemux * rtpdemux, guint8 pt)
322 {
323   GstPad *respad = NULL;
324   GSList *item = rtpdemux->srcpads;
325
326   for (; item; item = g_slist_next (item)) {
327     GstRTPPtDemuxPad *pad = item->data;
328
329     if (pad->pt == pt) {
330       respad = pad->pad;
331       break;
332     }
333   }
334
335   return respad;
336 }
337
338 /**
339  * Reserves resources for the object.
340  */
341 static gboolean
342 gst_rtp_pt_demux_setup (GstElement * element)
343 {
344   GstRTPPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
345   gboolean res = TRUE;
346
347   if (ptdemux) {
348     ptdemux->srcpads = NULL;
349     ptdemux->last_pt = 0xFFFF;
350   }
351
352   return res;
353 }
354
355 /**
356  * Free resources for the object.
357  */
358 static void
359 gst_rtp_pt_demux_release (GstElement * element)
360 {
361   GstRTPPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
362
363   if (ptdemux) {
364     /* note: GstElement's dispose() will handle the pads */
365     g_slist_free (ptdemux->srcpads);
366     ptdemux->srcpads = NULL;
367   }
368 }
369
370 static GstStateChangeReturn
371 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
372 {
373   GstStateChangeReturn ret;
374   GstRTPPtDemux *ptdemux;
375
376   ptdemux = GST_RTP_PT_DEMUX (element);
377
378   switch (transition) {
379     case GST_STATE_CHANGE_NULL_TO_READY:
380       if (gst_rtp_pt_demux_setup (element) != TRUE)
381         ret = GST_STATE_CHANGE_FAILURE;
382       break;
383     case GST_STATE_CHANGE_READY_TO_PAUSED:
384     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
385     default:
386       break;
387   }
388
389   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
390
391   switch (transition) {
392     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
393     case GST_STATE_CHANGE_PAUSED_TO_READY:
394       break;
395     case GST_STATE_CHANGE_READY_TO_NULL:
396       gst_rtp_pt_demux_release (element);
397       break;
398     default:
399       break;
400   }
401
402   return ret;
403 }