4 * Copyright (C) 2005 Nokia Corporation.
5 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
7 * Loosely based on GStreamer gstdecodebin
8 * Copyright (C) <2004> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
27 * SECTION:element-gstrtpptdemux
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.
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
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.
43 * <title>Example pipelines</title>
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.
50 * Last reviewed on 2007-05-28 (0.10.5)
55 * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
59 * - works with the test_rtpdemux.c tool
62 * - is emitting a signal enough, or should we
63 * use GstEvent to notify downstream elements
64 * of the new packet... no?
67 * - emits event both for new PTs, and whenever
77 #include <gst/rtp/gstrtpbuffer.h>
79 #include "gstrtpbin-marshal.h"
80 #include "gstrtpptdemux.h"
82 /* generic templates */
83 static GstStaticPadTemplate rtp_pt_demux_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
87 GST_STATIC_CAPS ("application/x-rtp")
90 static GstStaticPadTemplate rtp_pt_demux_src_template =
91 GST_STATIC_PAD_TEMPLATE ("src_%d",
94 GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
97 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
98 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
101 * Item for storing GstPad<->pt pairs.
103 struct _GstRtpPtDemuxPad
105 GstPad *pad; /**< pointer to the actual pad */
106 gint pt; /**< RTP payload-type attached to pad */
113 SIGNAL_REQUEST_PT_MAP,
114 SIGNAL_NEW_PAYLOAD_TYPE,
115 SIGNAL_PAYLOAD_TYPE_CHANGE,
120 GST_BOILERPLATE (GstRtpPtDemux, gst_rtp_pt_demux, GstElement, GST_TYPE_ELEMENT);
122 static void gst_rtp_pt_demux_finalize (GObject * object);
124 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
125 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
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);
133 static GstRtpPtDemuxPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
135 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event);
138 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
141 gst_rtp_pt_demux_base_init (gpointer g_class)
143 GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
145 gst_element_class_add_pad_template (gstelement_klass,
146 gst_static_pad_template_get (&rtp_pt_demux_sink_template));
147 gst_element_class_add_pad_template (gstelement_klass,
148 gst_static_pad_template_get (&rtp_pt_demux_src_template));
150 gst_element_class_set_details_simple (gstelement_klass, "RTP Demux",
152 "Parses codec streams transmitted in the same RTP session",
153 "Kai Vehmanen <kai.vehmanen@nokia.com>");
157 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
159 GObjectClass *gobject_klass;
160 GstElementClass *gstelement_klass;
162 gobject_klass = (GObjectClass *) klass;
163 gstelement_klass = (GstElementClass *) klass;
166 * GstRtpPtDemux::request-pt-map:
167 * @demux: the object which received the signal
168 * @pt: the payload type
170 * Request the payload type as #GstCaps for @pt.
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,
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
184 * Emited when a new payload type pad has been created in @demux.
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);
193 * GstRtpPtDemux::payload-type-change:
194 * @demux: the object which received the signal
195 * @pt: the new payload type
197 * Emited when the payload type changed.
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);
206 * GstRtpPtDemux::clear-pt-map:
207 * @demux: the object which received the signal
209 * The application can call this signal to instruct the element to discard the
210 * currently cached payload type map.
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);
218 gobject_klass->finalize = gst_rtp_pt_demux_finalize;
220 gstelement_klass->change_state =
221 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
223 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
225 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
226 "rtpptdemux", 0, "RTP codec demuxer");
230 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux, GstRtpPtDemuxClass * g_class)
232 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
235 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
237 g_assert (ptdemux->sink != NULL);
239 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
240 gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
242 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
246 gst_rtp_pt_demux_finalize (GObject * object)
248 gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
250 G_OBJECT_CLASS (parent_class)->finalize (object);
254 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
258 GValue args[2] = { {0}, {0} };
260 /* figure out the caps */
261 g_value_init (&args[0], GST_TYPE_ELEMENT);
262 g_value_set_object (&args[0], rtpdemux);
263 g_value_init (&args[1], G_TYPE_UINT);
264 g_value_set_uint (&args[1], pt);
266 g_value_init (&ret, GST_TYPE_CAPS);
267 g_value_set_boxed (&ret, NULL);
269 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
272 g_value_unset (&args[0]);
273 g_value_unset (&args[1]);
274 caps = g_value_dup_boxed (&ret);
275 g_value_unset (&ret);
277 caps = GST_PAD_CAPS (rtpdemux->sink);
282 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
288 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
292 GST_OBJECT_LOCK (rtpdemux);
293 GST_DEBUG ("clearing pt map");
294 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
295 GstRtpPtDemuxPad *pad = walk->data;
299 GST_OBJECT_UNLOCK (rtpdemux);
303 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
305 GstFlowReturn ret = GST_FLOW_OK;
306 GstRtpPtDemux *rtpdemux;
307 GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
310 GstRtpPtDemuxPad *rtpdemuxpad;
313 rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
315 if (!gst_rtp_buffer_validate (buf))
318 pt = gst_rtp_buffer_get_payload_type (buf);
320 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
322 rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
323 if (rtpdemuxpad == NULL) {
324 /* new PT, create a src pad */
325 GstElementClass *klass;
326 GstPadTemplate *templ;
329 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
330 templ = gst_element_class_get_pad_template (klass, "src_%d");
331 padname = g_strdup_printf ("src_%d", pt);
332 srcpad = gst_pad_new_from_template (templ, padname);
333 gst_pad_use_fixed_caps (srcpad);
335 gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
337 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
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);
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);
355 gst_pad_set_active (srcpad, TRUE);
356 gst_element_add_pad (element, srcpad);
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);
363 srcpad = rtpdemuxpad->pad;
365 if (pt != rtpdemux->last_pt) {
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);
375 if (rtpdemuxpad->newcaps) {
376 GST_DEBUG ("need new caps");
377 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
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;
388 gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
391 ret = gst_pad_push (srcpad, buf);
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;
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;
413 static GstRtpPtDemuxPad *
414 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
416 GstRtpPtDemuxPad *respad = NULL;
419 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
420 GstRtpPtDemuxPad *pad = walk->data;
431 gst_rtp_pt_demux_sink_event (GstPad * pad, GstEvent * event)
433 GstRtpPtDemux *rtpdemux;
434 gboolean res = FALSE;
436 rtpdemux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
438 switch (GST_EVENT_TYPE (event)) {
439 case GST_EVENT_CUSTOM_DOWNSTREAM:
441 const GstStructure *s;
443 s = gst_event_get_structure (event);
445 if (gst_structure_has_name (s, "GstRTPPacketLost")) {
446 GstRtpPtDemuxPad *rtpdemuxpad =
447 find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
450 res = gst_pad_push_event (rtpdemuxpad->pad, event);
452 gst_event_unref (event);
455 res = gst_pad_event_default (pad, event);
460 res = gst_pad_event_default (pad, event);
464 gst_object_unref (rtpdemux);
470 gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event)
472 GstRtpPtDemux *demux;
473 const GstStructure *s;
475 demux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
477 switch (GST_EVENT_TYPE (event)) {
478 case GST_EVENT_CUSTOM_UPSTREAM:
479 case GST_EVENT_CUSTOM_BOTH:
480 case GST_EVENT_CUSTOM_BOTH_OOB:
481 s = gst_event_get_structure (event);
482 if (s && !gst_structure_has_field (s, "payload")) {
485 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
486 GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
488 if (dpad->pad == pad) {
490 GST_EVENT_CAST (gst_mini_object_make_writable
491 (GST_MINI_OBJECT_CAST (event)));
492 gst_structure_set (event->structure,
493 "payload", G_TYPE_UINT, dpad->pt, NULL);
503 gst_object_unref (demux);
505 return gst_pad_event_default (pad, event);
511 * Reserves resources for the object.
514 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
516 ptdemux->srcpads = NULL;
517 ptdemux->last_pt = 0xFFFF;
523 * Free resources for the object.
526 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
530 for (walk = ptdemux->srcpads; walk; walk = g_slist_next (walk)) {
531 GstRtpPtDemuxPad *pad = walk->data;
533 gst_pad_set_active (pad->pad, FALSE);
534 gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
537 g_slist_free (ptdemux->srcpads);
538 ptdemux->srcpads = NULL;
541 static GstStateChangeReturn
542 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
544 GstStateChangeReturn ret;
545 GstRtpPtDemux *ptdemux;
547 ptdemux = GST_RTP_PT_DEMUX (element);
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;
554 case GST_STATE_CHANGE_READY_TO_PAUSED:
555 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
560 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
562 switch (transition) {
563 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
564 case GST_STATE_CHANGE_PAUSED_TO_READY:
566 case GST_STATE_CHANGE_READY_TO_NULL:
567 gst_rtp_pt_demux_release (ptdemux);