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
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 (GstRtpPtDemux * ptdemux);
130 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
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_RTP_PT_DEMUX (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 g_value_unset (&args[0]);
277 g_value_unset (&args[1]);
278 caps = g_value_dup_boxed (&ret);
279 g_value_unset (&ret);
281 caps = GST_PAD_CAPS (rtpdemux->sink);
286 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
292 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
296 GST_OBJECT_LOCK (rtpdemux);
297 GST_DEBUG ("clearing pt map");
298 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
299 GstRtpPtDemuxPad *pad = walk->data;
303 GST_OBJECT_UNLOCK (rtpdemux);
307 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
309 GstFlowReturn ret = GST_FLOW_OK;
310 GstRtpPtDemux *rtpdemux;
311 GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
314 GstRtpPtDemuxPad *rtpdemuxpad;
317 rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
319 if (!gst_rtp_buffer_validate (buf))
322 pt = gst_rtp_buffer_get_payload_type (buf);
324 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
326 rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
327 if (rtpdemuxpad == NULL) {
328 /* new PT, create a src pad */
329 GstElementClass *klass;
330 GstPadTemplate *templ;
333 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
334 templ = gst_element_class_get_pad_template (klass, "src_%d");
335 padname = g_strdup_printf ("src_%d", pt);
336 srcpad = gst_pad_new_from_template (templ, padname);
337 gst_pad_use_fixed_caps (srcpad);
340 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
344 caps = gst_caps_make_writable (caps);
345 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
346 gst_pad_set_caps (srcpad, caps);
347 gst_caps_unref (caps);
349 GST_DEBUG ("Adding pt=%d to the list.", pt);
350 rtpdemuxpad = g_new0 (GstRtpPtDemuxPad, 1);
351 rtpdemuxpad->pt = pt;
352 rtpdemuxpad->newcaps = FALSE;
353 rtpdemuxpad->pad = srcpad;
354 GST_OBJECT_LOCK (rtpdemux);
355 rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
356 GST_OBJECT_UNLOCK (rtpdemux);
358 gst_pad_set_active (srcpad, TRUE);
359 gst_element_add_pad (element, srcpad);
361 GST_DEBUG ("emitting new-payload_type for pt %d", pt);
362 g_signal_emit (G_OBJECT (rtpdemux),
363 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
366 srcpad = rtpdemuxpad->pad;
368 if (pt != rtpdemux->last_pt) {
371 /* our own signal with an extra flag that this is the only pad */
372 rtpdemux->last_pt = pt;
373 GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
374 g_signal_emit (G_OBJECT (rtpdemux),
375 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
378 if (rtpdemuxpad->newcaps) {
379 GST_DEBUG ("need new caps");
380 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
384 caps = gst_caps_make_writable (caps);
385 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
386 gst_pad_set_caps (srcpad, caps);
387 gst_caps_unref (caps);
388 rtpdemuxpad->newcaps = FALSE;
391 gst_buffer_set_caps (buf, GST_PAD_CAPS (srcpad));
394 ret = gst_pad_push (srcpad, buf);
401 /* this is fatal and should be filtered earlier */
402 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
403 ("Dropping invalid RTP payload"));
404 gst_buffer_unref (buf);
405 return GST_FLOW_ERROR;
409 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
410 ("Could not get caps for payload"));
411 gst_buffer_unref (buf);
412 return GST_FLOW_ERROR;
416 static GstRtpPtDemuxPad *
417 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
419 GstRtpPtDemuxPad *respad = NULL;
422 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
423 GstRtpPtDemuxPad *pad = walk->data;
434 * Reserves resources for the object.
437 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
439 ptdemux->srcpads = NULL;
440 ptdemux->last_pt = 0xFFFF;
446 * Free resources for the object.
449 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
453 for (walk = ptdemux->srcpads; walk; walk = g_slist_next (walk)) {
454 GstRtpPtDemuxPad *pad = walk->data;
456 gst_pad_set_active (pad->pad, FALSE);
457 gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
460 g_slist_free (ptdemux->srcpads);
461 ptdemux->srcpads = NULL;
464 static GstStateChangeReturn
465 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
467 GstStateChangeReturn ret;
468 GstRtpPtDemux *ptdemux;
470 ptdemux = GST_RTP_PT_DEMUX (element);
472 switch (transition) {
473 case GST_STATE_CHANGE_NULL_TO_READY:
474 if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
475 ret = GST_STATE_CHANGE_FAILURE;
477 case GST_STATE_CHANGE_READY_TO_PAUSED:
478 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
483 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
485 switch (transition) {
486 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
487 case GST_STATE_CHANGE_PAUSED_TO_READY:
489 case GST_STATE_CHANGE_READY_TO_NULL:
490 gst_rtp_pt_demux_release (ptdemux);