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@fluendo.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
28 * @short_description: separate RTP payloads based on the payload type
32 * gstrtpptdemux acts as a demuxer for RTP packets based on the payload type of the
33 * packets. Its main purpose is to allow an application to easily receive and
34 * decode an RTP stream with multiple payload types.
37 * For each payload type that is detected, a new pad will be created and the
38 * ::new-payload-type signal will be emitted. When the payload for the RTP
39 * stream changes, the ::payload-type-change signal will be emitted.
42 * The element will try to set complete and unique application/x-rtp caps on the
43 * outgoing buffers and pads based on the result of the ::request-pt-map signal.
45 * <title>Example pipelines</title>
48 * gst-launch udpsrc caps="application/x-rtp" ! gstrtpptdemux ! fakesink
50 * Takes an RTP stream and send the RTP packets with the first detected payload
51 * type to fakesink, discarding the other payload types.
55 * Last reviewed on 2007-05-28 (0.10.5)
60 * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
64 * - works with the test_rtpdemux.c tool
67 * - is emitting a signal enough, or should we
68 * use GstEvent to notify downstream elements
69 * of the new packet... no?
72 * - emits event both for new PTs, and whenever
82 #include <gst/rtp/gstrtpbuffer.h>
84 #include "gstrtpbin-marshal.h"
85 #include "gstrtpptdemux.h"
87 /* generic templates */
88 static GstStaticPadTemplate rtp_pt_demux_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("sink",
92 GST_STATIC_CAPS ("application/x-rtp")
95 static GstStaticPadTemplate rtp_pt_demux_src_template =
96 GST_STATIC_PAD_TEMPLATE ("src_%d",
99 GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
102 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
103 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
106 * Item for storing GstPad<->pt pairs.
108 struct _GstRtpPtDemuxPad
110 GstPad *pad; /**< pointer to the actual pad */
111 gint pt; /**< RTP payload-type attached to pad */
118 SIGNAL_REQUEST_PT_MAP,
119 SIGNAL_NEW_PAYLOAD_TYPE,
120 SIGNAL_PAYLOAD_TYPE_CHANGE,
125 GST_BOILERPLATE (GstRtpPtDemux, gst_rtp_pt_demux, GstElement, GST_TYPE_ELEMENT);
127 static void gst_rtp_pt_demux_finalize (GObject * object);
129 static void gst_rtp_pt_demux_release (GstElement * element);
130 static gboolean gst_rtp_pt_demux_setup (GstElement * element);
132 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
133 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
134 GstStateChange transition);
135 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
137 static GstRtpPtDemuxPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
139 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
141 static GstElementDetails gst_rtp_pt_demux_details = {
144 "Parses codec streams transmitted in the same RTP session",
145 "Kai Vehmanen <kai.vehmanen@nokia.com>"
149 gst_rtp_pt_demux_base_init (gpointer g_class)
151 GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (g_class);
153 gst_element_class_add_pad_template (gstelement_klass,
154 gst_static_pad_template_get (&rtp_pt_demux_sink_template));
155 gst_element_class_add_pad_template (gstelement_klass,
156 gst_static_pad_template_get (&rtp_pt_demux_src_template));
158 gst_element_class_set_details (gstelement_klass, &gst_rtp_pt_demux_details);
162 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
164 GObjectClass *gobject_klass;
165 GstElementClass *gstelement_klass;
167 gobject_klass = (GObjectClass *) klass;
168 gstelement_klass = (GstElementClass *) klass;
171 * GstRtpPtDemux::request-pt-map:
172 * @demux: the object which received the signal
173 * @pt: the payload type
175 * Request the payload type as #GstCaps for @pt.
177 gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
178 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
179 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
180 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
184 * GstRtpPtDemux::new-payload-type:
185 * @demux: the object which received the signal
186 * @pt: the payload type
187 * @pad: the pad with the new payload
189 * Emited when a new payload type pad has been created in @demux.
191 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
192 g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
194 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
195 G_TYPE_UINT, GST_TYPE_PAD);
198 * GstRtpPtDemux::payload-type-change:
199 * @demux: the object which received the signal
200 * @pt: the new payload type
202 * Emited when the payload type changed.
204 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
205 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
207 payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
208 G_TYPE_NONE, 1, G_TYPE_UINT);
211 * GstRtpPtDemux::clear-pt-map:
212 * @demux: the object which received the signal
214 * The application can call this signal to instruct the element to discard the
215 * currently cached payload type map.
217 gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
218 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
220 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
221 G_TYPE_NONE, 0, G_TYPE_NONE);
223 gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_finalize);
225 gstelement_klass->change_state =
226 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
228 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
230 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
231 "rtpptdemux", 0, "RTP codec demuxer");
235 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux, GstRtpPtDemuxClass * g_class)
237 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
240 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
242 g_assert (ptdemux->sink != NULL);
244 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
246 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
250 gst_rtp_pt_demux_finalize (GObject * object)
252 gst_rtp_pt_demux_release (GST_ELEMENT (object));
254 G_OBJECT_CLASS (parent_class)->finalize (object);
258 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
262 GValue args[2] = { {0}, {0} };
264 /* figure out the caps */
265 g_value_init (&args[0], GST_TYPE_ELEMENT);
266 g_value_set_object (&args[0], rtpdemux);
267 g_value_init (&args[1], G_TYPE_UINT);
268 g_value_set_uint (&args[1], pt);
270 g_value_init (&ret, GST_TYPE_CAPS);
271 g_value_set_boxed (&ret, NULL);
273 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
276 caps = g_value_get_boxed (&ret);
278 caps = GST_PAD_CAPS (rtpdemux->sink);
280 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
286 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
290 GST_OBJECT_LOCK (rtpdemux);
291 GST_DEBUG ("clearing pt map");
292 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
293 GstRtpPtDemuxPad *pad = walk->data;
297 GST_OBJECT_UNLOCK (rtpdemux);
301 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
303 GstFlowReturn ret = GST_FLOW_OK;
304 GstRtpPtDemux *rtpdemux;
305 GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
308 GstRtpPtDemuxPad *rtpdemuxpad;
311 rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
313 if (!gst_rtp_buffer_validate (buf))
316 pt = gst_rtp_buffer_get_payload_type (buf);
318 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
320 rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
321 if (rtpdemuxpad == NULL) {
322 /* new PT, create a src pad */
323 GstElementClass *klass;
324 GstPadTemplate *templ;
327 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
328 templ = gst_element_class_get_pad_template (klass, "src_%d");
329 padname = g_strdup_printf ("src_%d", pt);
330 srcpad = gst_pad_new_from_template (templ, padname);
331 gst_pad_use_fixed_caps (srcpad);
334 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
338 caps = gst_caps_make_writable (caps);
339 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
340 gst_pad_set_caps (srcpad, caps);
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);
351 gst_pad_set_active (srcpad, TRUE);
352 gst_element_add_pad (element, srcpad);
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);
359 srcpad = rtpdemuxpad->pad;
361 if (pt != rtpdemux->last_pt) {
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);
371 if (rtpdemuxpad->newcaps) {
372 GST_DEBUG ("need new caps");
373 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
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 rtpdemuxpad->newcaps = FALSE;
383 gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
386 ret = gst_pad_push (srcpad, buf);
393 /* this is fatal and should be filtered earlier */
394 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
395 ("Dropping invalid RTP payload"));
396 gst_buffer_unref (buf);
397 return GST_FLOW_ERROR;
401 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
402 ("Could not get caps for payload"));
403 gst_buffer_unref (buf);
404 return GST_FLOW_ERROR;
408 static GstRtpPtDemuxPad *
409 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
411 GstRtpPtDemuxPad *respad = NULL;
414 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
415 GstRtpPtDemuxPad *pad = walk->data;
426 * Reserves resources for the object.
429 gst_rtp_pt_demux_setup (GstElement * element)
431 GstRtpPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
435 ptdemux->srcpads = NULL;
436 ptdemux->last_pt = 0xFFFF;
443 * Free resources for the object.
446 gst_rtp_pt_demux_release (GstElement * element)
448 GstRtpPtDemux *ptdemux = GST_RTP_PT_DEMUX (element);
451 /* note: GstElement's dispose() will handle the pads */
452 g_slist_foreach (ptdemux->srcpads, (GFunc) g_free, NULL);
453 g_slist_free (ptdemux->srcpads);
454 ptdemux->srcpads = NULL;
458 static GstStateChangeReturn
459 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
461 GstStateChangeReturn ret;
462 GstRtpPtDemux *ptdemux;
464 ptdemux = GST_RTP_PT_DEMUX (element);
466 switch (transition) {
467 case GST_STATE_CHANGE_NULL_TO_READY:
468 if (gst_rtp_pt_demux_setup (element) != TRUE)
469 ret = GST_STATE_CHANGE_FAILURE;
471 case GST_STATE_CHANGE_READY_TO_PAUSED:
472 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
477 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
479 switch (transition) {
480 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
481 case GST_STATE_CHANGE_PAUSED_TO_READY:
483 case GST_STATE_CHANGE_READY_TO_NULL:
484 gst_rtp_pt_demux_release (element);