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