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, GstEvent * event);
129 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf);
130 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
131 GstStateChange transition);
132 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
134 static GstRtpPtDemuxPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
136 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event);
139 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
142 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
144 GObjectClass *gobject_klass;
145 GstElementClass *gstelement_klass;
147 gobject_klass = (GObjectClass *) klass;
148 gstelement_klass = (GstElementClass *) klass;
151 * GstRtpPtDemux::request-pt-map:
152 * @demux: the object which received the signal
153 * @pt: the payload type
155 * Request the payload type as #GstCaps for @pt.
157 gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
158 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
160 NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT, GST_TYPE_CAPS, 1,
164 * GstRtpPtDemux::new-payload-type:
165 * @demux: the object which received the signal
166 * @pt: the payload type
167 * @pad: the pad with the new payload
169 * Emited when a new payload type pad has been created in @demux.
171 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
172 g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
174 NULL, NULL, gst_rtp_bin_marshal_VOID__UINT_OBJECT, G_TYPE_NONE, 2,
175 G_TYPE_UINT, GST_TYPE_PAD);
178 * GstRtpPtDemux::payload-type-change:
179 * @demux: the object which received the signal
180 * @pt: the new payload type
182 * Emited when the payload type changed.
184 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
185 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
187 payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
188 G_TYPE_NONE, 1, G_TYPE_UINT);
191 * GstRtpPtDemux::clear-pt-map:
192 * @demux: the object which received the signal
194 * The application can call this signal to instruct the element to discard the
195 * currently cached payload type map.
197 gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
198 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
200 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
201 G_TYPE_NONE, 0, G_TYPE_NONE);
203 gobject_klass->finalize = gst_rtp_pt_demux_finalize;
205 gstelement_klass->change_state =
206 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
208 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
210 gst_element_class_add_pad_template (gstelement_klass,
211 gst_static_pad_template_get (&rtp_pt_demux_sink_template));
212 gst_element_class_add_pad_template (gstelement_klass,
213 gst_static_pad_template_get (&rtp_pt_demux_src_template));
215 gst_element_class_set_details_simple (gstelement_klass, "RTP Demux",
217 "Parses codec streams transmitted in the same RTP session",
218 "Kai Vehmanen <kai.vehmanen@nokia.com>");
220 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
221 "rtpptdemux", 0, "RTP codec demuxer");
225 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux)
227 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
230 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
232 g_assert (ptdemux->sink != NULL);
234 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
235 gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
237 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
241 gst_rtp_pt_demux_finalize (GObject * object)
243 gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
245 G_OBJECT_CLASS (parent_class)->finalize (object);
249 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
253 GValue args[2] = { {0}, {0} };
255 /* figure out the caps */
256 g_value_init (&args[0], GST_TYPE_ELEMENT);
257 g_value_set_object (&args[0], rtpdemux);
258 g_value_init (&args[1], G_TYPE_UINT);
259 g_value_set_uint (&args[1], pt);
261 g_value_init (&ret, GST_TYPE_CAPS);
262 g_value_set_boxed (&ret, NULL);
264 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
267 g_value_unset (&args[0]);
268 g_value_unset (&args[1]);
269 caps = g_value_dup_boxed (&ret);
270 g_value_unset (&ret);
272 caps = gst_pad_get_current_caps (rtpdemux->sink);
275 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
281 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
285 GST_OBJECT_LOCK (rtpdemux);
286 GST_DEBUG ("clearing pt map");
287 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
288 GstRtpPtDemuxPad *pad = walk->data;
292 GST_OBJECT_UNLOCK (rtpdemux);
296 gst_rtp_pt_demux_chain (GstPad * pad, GstBuffer * buf)
298 GstFlowReturn ret = GST_FLOW_OK;
299 GstRtpPtDemux *rtpdemux;
300 GstElement *element = GST_ELEMENT (GST_OBJECT_PARENT (pad));
303 GstRtpPtDemuxPad *rtpdemuxpad;
307 rtpdemux = GST_RTP_PT_DEMUX (GST_OBJECT_PARENT (pad));
309 if (!gst_rtp_buffer_validate (buf))
312 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
313 pt = gst_rtp_buffer_get_payload_type (&rtp);
314 gst_rtp_buffer_unmap (&rtp);
316 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
318 rtpdemuxpad = find_pad_for_pt (rtpdemux, pt);
319 if (rtpdemuxpad == NULL) {
320 /* new PT, create a src pad */
321 GstElementClass *klass;
322 GstPadTemplate *templ;
325 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
326 templ = gst_element_class_get_pad_template (klass, "src_%u");
327 padname = g_strdup_printf ("src_%u", pt);
328 srcpad = gst_pad_new_from_template (templ, padname);
329 gst_pad_use_fixed_caps (srcpad);
331 gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
333 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
337 caps = gst_caps_make_writable (caps);
338 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
339 gst_pad_set_caps (srcpad, caps);
340 gst_caps_unref (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 gst_caps_unref (caps);
381 rtpdemuxpad->newcaps = FALSE;
385 ret = gst_pad_push (srcpad, buf);
392 /* this is fatal and should be filtered earlier */
393 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
394 ("Dropping invalid RTP payload"));
395 gst_buffer_unref (buf);
396 return GST_FLOW_ERROR;
400 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
401 ("Could not get caps for payload"));
402 gst_buffer_unref (buf);
403 return GST_FLOW_ERROR;
407 static GstRtpPtDemuxPad *
408 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
410 GstRtpPtDemuxPad *respad = NULL;
413 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
414 GstRtpPtDemuxPad *pad = walk->data;
425 gst_rtp_pt_demux_sink_event (GstPad * pad, GstEvent * event)
427 GstRtpPtDemux *rtpdemux;
428 gboolean res = FALSE;
430 rtpdemux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
431 if (G_UNLIKELY (rtpdemux == NULL)) {
432 gst_event_unref (event);
436 switch (GST_EVENT_TYPE (event)) {
437 case GST_EVENT_CUSTOM_DOWNSTREAM:
439 const GstStructure *s;
441 s = gst_event_get_structure (event);
443 if (gst_structure_has_name (s, "GstRTPPacketLost")) {
444 GstRtpPtDemuxPad *rtpdemuxpad =
445 find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
448 res = gst_pad_push_event (rtpdemuxpad->pad, event);
450 gst_event_unref (event);
453 res = gst_pad_event_default (pad, event);
458 res = gst_pad_event_default (pad, event);
462 gst_object_unref (rtpdemux);
468 gst_rtp_pt_demux_src_event (GstPad * pad, GstEvent * event)
470 GstRtpPtDemux *demux;
471 const GstStructure *s;
473 demux = GST_RTP_PT_DEMUX (gst_pad_get_parent (pad));
475 switch (GST_EVENT_TYPE (event)) {
476 case GST_EVENT_CUSTOM_UPSTREAM:
477 case GST_EVENT_CUSTOM_BOTH:
478 case GST_EVENT_CUSTOM_BOTH_OOB:
479 s = gst_event_get_structure (event);
480 if (s && !gst_structure_has_field (s, "payload")) {
483 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
484 GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
486 if (dpad->pad == pad) {
490 GST_EVENT_CAST (gst_mini_object_make_writable
491 (GST_MINI_OBJECT_CAST (event)));
492 ws = gst_event_writable_structure (event);
493 gst_structure_set (ws, "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);