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_%u",
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 #define gst_rtp_pt_demux_parent_class parent_class
121 G_DEFINE_TYPE (GstRtpPtDemux, gst_rtp_pt_demux, GST_TYPE_ELEMENT);
123 static void gst_rtp_pt_demux_finalize (GObject * object);
125 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
126 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
128 static gboolean gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent,
130 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent,
132 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
133 GstStateChange transition);
134 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
136 static GstPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
138 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent,
142 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
145 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
147 GObjectClass *gobject_klass;
148 GstElementClass *gstelement_klass;
150 gobject_klass = (GObjectClass *) klass;
151 gstelement_klass = (GstElementClass *) klass;
154 * GstRtpPtDemux::request-pt-map:
155 * @demux: the object which received the signal
156 * @pt: the payload type
158 * Request the payload type as #GstCaps for @pt.
160 gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
161 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
163 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
167 * GstRtpPtDemux::new-payload-type:
168 * @demux: the object which received the signal
169 * @pt: the payload type
170 * @pad: the pad with the new payload
172 * Emited when a new payload type pad has been created in @demux.
174 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
175 g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
176 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
177 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
178 G_TYPE_UINT, GST_TYPE_PAD);
181 * GstRtpPtDemux::payload-type-change:
182 * @demux: the object which received the signal
183 * @pt: the new payload type
185 * Emited when the payload type changed.
187 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
188 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
190 payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
191 G_TYPE_NONE, 1, G_TYPE_UINT);
194 * GstRtpPtDemux::clear-pt-map:
195 * @demux: the object which received the signal
197 * The application can call this signal to instruct the element to discard the
198 * currently cached payload type map.
200 gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
201 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
203 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
204 G_TYPE_NONE, 0, G_TYPE_NONE);
206 gobject_klass->finalize = gst_rtp_pt_demux_finalize;
208 gstelement_klass->change_state =
209 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
211 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
213 gst_element_class_add_pad_template (gstelement_klass,
214 gst_static_pad_template_get (&rtp_pt_demux_sink_template));
215 gst_element_class_add_pad_template (gstelement_klass,
216 gst_static_pad_template_get (&rtp_pt_demux_src_template));
218 gst_element_class_set_details_simple (gstelement_klass, "RTP Demux",
220 "Parses codec streams transmitted in the same RTP session",
221 "Kai Vehmanen <kai.vehmanen@nokia.com>");
223 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
224 "rtpptdemux", 0, "RTP codec demuxer");
228 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux)
230 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
233 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
235 g_assert (ptdemux->sink != NULL);
237 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
238 gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
240 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
244 gst_rtp_pt_demux_finalize (GObject * object)
246 gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
248 G_OBJECT_CLASS (parent_class)->finalize (object);
252 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
256 GValue args[2] = { {0}, {0} };
258 /* figure out the caps */
259 g_value_init (&args[0], GST_TYPE_ELEMENT);
260 g_value_set_object (&args[0], rtpdemux);
261 g_value_init (&args[1], G_TYPE_UINT);
262 g_value_set_uint (&args[1], pt);
264 g_value_init (&ret, GST_TYPE_CAPS);
265 g_value_set_boxed (&ret, NULL);
267 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
270 g_value_unset (&args[0]);
271 g_value_unset (&args[1]);
272 caps = g_value_dup_boxed (&ret);
273 g_value_unset (&ret);
275 caps = gst_pad_get_current_caps (rtpdemux->sink);
278 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
284 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
288 GST_OBJECT_LOCK (rtpdemux);
289 GST_DEBUG ("clearing pt map");
290 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
291 GstRtpPtDemuxPad *pad = walk->data;
295 GST_OBJECT_UNLOCK (rtpdemux);
299 need_caps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
302 gboolean ret = FALSE;
304 GST_OBJECT_LOCK (rtpdemux);
305 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
306 GstRtpPtDemuxPad *pad = walk->data;
312 GST_OBJECT_UNLOCK (rtpdemux);
319 clear_newcaps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
323 GST_OBJECT_LOCK (rtpdemux);
324 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
325 GstRtpPtDemuxPad *pad = walk->data;
328 pad->newcaps = FALSE;
332 GST_OBJECT_UNLOCK (rtpdemux);
336 gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
338 GstFlowReturn ret = GST_FLOW_OK;
339 GstRtpPtDemux *rtpdemux;
343 GstRTPBuffer rtp = { NULL };
345 rtpdemux = GST_RTP_PT_DEMUX (parent);
347 if (!gst_rtp_buffer_validate (buf))
350 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
351 pt = gst_rtp_buffer_get_payload_type (&rtp);
352 gst_rtp_buffer_unmap (&rtp);
354 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
356 srcpad = find_pad_for_pt (rtpdemux, pt);
357 if (srcpad == NULL) {
358 /* new PT, create a src pad */
359 GstRtpPtDemuxPad *rtpdemuxpad;
360 GstElementClass *klass;
361 GstPadTemplate *templ;
364 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
368 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
369 templ = gst_element_class_get_pad_template (klass, "src_%u");
370 padname = g_strdup_printf ("src_%u", pt);
371 srcpad = gst_pad_new_from_template (templ, padname);
372 gst_pad_use_fixed_caps (srcpad);
374 gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
376 caps = gst_caps_make_writable (caps);
377 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
378 gst_pad_set_caps (srcpad, caps);
379 gst_caps_unref (caps);
381 GST_DEBUG ("Adding pt=%d to the list.", pt);
382 rtpdemuxpad = g_slice_new0 (GstRtpPtDemuxPad);
383 rtpdemuxpad->pt = pt;
384 rtpdemuxpad->newcaps = FALSE;
385 rtpdemuxpad->pad = srcpad;
386 gst_object_ref (srcpad);
387 GST_OBJECT_LOCK (rtpdemux);
388 rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
389 GST_OBJECT_UNLOCK (rtpdemux);
391 gst_pad_set_active (srcpad, TRUE);
392 gst_element_add_pad (GST_ELEMENT_CAST (rtpdemux), srcpad);
394 GST_DEBUG ("emitting new-payload-type for pt %d", pt);
395 g_signal_emit (G_OBJECT (rtpdemux),
396 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
399 if (pt != rtpdemux->last_pt) {
402 /* our own signal with an extra flag that this is the only pad */
403 rtpdemux->last_pt = pt;
404 GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
405 g_signal_emit (G_OBJECT (rtpdemux),
406 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
409 while (need_caps_for_pt (rtpdemux, pt)) {
410 GST_DEBUG ("need new caps for %d", pt);
411 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
415 clear_newcaps_for_pt (rtpdemux, pt);
417 caps = gst_caps_make_writable (caps);
418 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
419 gst_pad_set_caps (srcpad, caps);
420 gst_caps_unref (caps);
424 ret = gst_pad_push (srcpad, buf);
426 gst_object_unref (srcpad);
433 /* this is fatal and should be filtered earlier */
434 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
435 ("Dropping invalid RTP payload"));
436 gst_buffer_unref (buf);
437 return GST_FLOW_ERROR;
441 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
442 ("Could not get caps for payload"));
443 gst_buffer_unref (buf);
445 gst_object_unref (srcpad);
446 return GST_FLOW_ERROR;
451 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
453 GstPad *respad = NULL;
456 GST_OBJECT_LOCK (rtpdemux);
457 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
458 GstRtpPtDemuxPad *pad = walk->data;
461 respad = gst_object_ref (pad->pad);
465 GST_OBJECT_UNLOCK (rtpdemux);
471 gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
473 GstRtpPtDemux *rtpdemux;
474 gboolean res = FALSE;
476 rtpdemux = GST_RTP_PT_DEMUX (parent);
478 switch (GST_EVENT_TYPE (event)) {
479 case GST_EVENT_CUSTOM_DOWNSTREAM:
481 const GstStructure *s;
483 s = gst_event_get_structure (event);
485 if (gst_structure_has_name (s, "GstRTPPacketLost")) {
486 GstPad *srcpad = find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
489 res = gst_pad_push_event (srcpad, event);
490 gst_object_unref (srcpad);
492 gst_event_unref (event);
496 res = gst_pad_event_default (pad, parent, event);
501 res = gst_pad_event_default (pad, parent, event);
510 gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
512 GstRtpPtDemux *demux;
513 const GstStructure *s;
515 demux = GST_RTP_PT_DEMUX (parent);
517 switch (GST_EVENT_TYPE (event)) {
518 case GST_EVENT_CUSTOM_UPSTREAM:
519 case GST_EVENT_CUSTOM_BOTH:
520 case GST_EVENT_CUSTOM_BOTH_OOB:
521 s = gst_event_get_structure (event);
522 if (s && !gst_structure_has_field (s, "payload")) {
525 GST_OBJECT_LOCK (demux);
526 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
527 GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
529 if (dpad->pad == pad) {
533 GST_EVENT_CAST (gst_mini_object_make_writable
534 (GST_MINI_OBJECT_CAST (event)));
535 ws = gst_event_writable_structure (event);
536 gst_structure_set (ws, "payload", G_TYPE_UINT, dpad->pt, NULL);
540 GST_OBJECT_UNLOCK (demux);
547 return gst_pad_event_default (pad, parent, event);
551 * Reserves resources for the object.
554 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
556 ptdemux->srcpads = NULL;
557 ptdemux->last_pt = 0xFFFF;
563 * Free resources for the object.
566 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
571 GST_OBJECT_LOCK (ptdemux);
572 tmppads = ptdemux->srcpads;
573 ptdemux->srcpads = NULL;
574 GST_OBJECT_UNLOCK (ptdemux);
576 for (walk = tmppads; walk; walk = g_slist_next (walk)) {
577 GstRtpPtDemuxPad *pad = walk->data;
579 gst_pad_set_active (pad->pad, FALSE);
580 gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
581 g_slice_free (GstRtpPtDemuxPad, pad);
583 g_slist_free (tmppads);
586 static GstStateChangeReturn
587 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
589 GstStateChangeReturn ret;
590 GstRtpPtDemux *ptdemux;
592 ptdemux = GST_RTP_PT_DEMUX (element);
594 switch (transition) {
595 case GST_STATE_CHANGE_NULL_TO_READY:
596 if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
597 ret = GST_STATE_CHANGE_FAILURE;
599 case GST_STATE_CHANGE_READY_TO_PAUSED:
600 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
605 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
607 switch (transition) {
608 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
609 case GST_STATE_CHANGE_PAUSED_TO_READY:
611 case GST_STATE_CHANGE_READY_TO_NULL:
612 gst_rtp_pt_demux_release (ptdemux);