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., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-rtpptdemux
29 * rtpptdemux 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
39 * on the output pads based on the result of the #GstRtpPtDemux::request-pt-map
43 * <title>Example pipelines</title>
45 * gst-launch-1.0 udpsrc caps="application/x-rtp" ! rtpptdemux ! 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.
53 * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
57 * - works with the test_rtpdemux.c tool
60 * - is emitting a signal enough, or should we
61 * use GstEvent to notify downstream elements
62 * of the new packet... no?
65 * - emits event both for new PTs, and whenever
75 #include <gst/rtp/gstrtpbuffer.h>
77 #include "gstrtpptdemux.h"
79 /* generic templates */
80 static GstStaticPadTemplate rtp_pt_demux_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
84 GST_STATIC_CAPS ("application/x-rtp")
87 static GstStaticPadTemplate rtp_pt_demux_src_template =
88 GST_STATIC_PAD_TEMPLATE ("src_%u",
91 GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
94 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
95 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
98 * Item for storing GstPad<->pt pairs.
100 struct _GstRtpPtDemuxPad
102 GstPad *pad; /**< pointer to the actual pad */
103 gint pt; /**< RTP payload-type attached to pad */
110 SIGNAL_REQUEST_PT_MAP,
111 SIGNAL_NEW_PAYLOAD_TYPE,
112 SIGNAL_PAYLOAD_TYPE_CHANGE,
117 #define gst_rtp_pt_demux_parent_class parent_class
118 G_DEFINE_TYPE (GstRtpPtDemux, gst_rtp_pt_demux, GST_TYPE_ELEMENT);
120 static void gst_rtp_pt_demux_finalize (GObject * object);
122 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
123 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
125 static gboolean gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent,
127 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent,
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 GstPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
135 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent,
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, g_cclosure_marshal_generic, GST_TYPE_CAPS, 1, G_TYPE_UINT);
163 * GstRtpPtDemux::new-payload-type:
164 * @demux: the object which received the signal
165 * @pt: the payload type
166 * @pad: the pad with the new payload
168 * Emited when a new payload type pad has been created in @demux.
170 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
171 g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
172 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
173 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, G_TYPE_UINT,
177 * GstRtpPtDemux::payload-type-change:
178 * @demux: the object which received the signal
179 * @pt: the new payload type
181 * Emited when the payload type changed.
183 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
184 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
185 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
186 payload_type_change), NULL, NULL, g_cclosure_marshal_VOID__UINT,
187 G_TYPE_NONE, 1, G_TYPE_UINT);
190 * GstRtpPtDemux::clear-pt-map:
191 * @demux: the object which received the signal
193 * The application can call this signal to instruct the element to discard the
194 * currently cached payload type map.
196 gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
197 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
199 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
200 G_TYPE_NONE, 0, G_TYPE_NONE);
202 gobject_klass->finalize = gst_rtp_pt_demux_finalize;
204 gstelement_klass->change_state =
205 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
207 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
209 gst_element_class_add_pad_template (gstelement_klass,
210 gst_static_pad_template_get (&rtp_pt_demux_sink_template));
211 gst_element_class_add_pad_template (gstelement_klass,
212 gst_static_pad_template_get (&rtp_pt_demux_src_template));
214 gst_element_class_set_static_metadata (gstelement_klass, "RTP Demux",
216 "Parses codec streams transmitted in the same RTP session",
217 "Kai Vehmanen <kai.vehmanen@nokia.com>");
219 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
220 "rtpptdemux", 0, "RTP codec demuxer");
224 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux)
226 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
229 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
231 g_assert (ptdemux->sink != NULL);
233 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
234 gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
236 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
240 gst_rtp_pt_demux_finalize (GObject * object)
242 gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
244 G_OBJECT_CLASS (parent_class)->finalize (object);
248 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
252 GValue args[2] = { {0}, {0} };
254 /* figure out the caps */
255 g_value_init (&args[0], GST_TYPE_ELEMENT);
256 g_value_set_object (&args[0], rtpdemux);
257 g_value_init (&args[1], G_TYPE_UINT);
258 g_value_set_uint (&args[1], pt);
260 g_value_init (&ret, GST_TYPE_CAPS);
261 g_value_set_boxed (&ret, NULL);
263 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
266 g_value_unset (&args[0]);
267 g_value_unset (&args[1]);
268 caps = g_value_dup_boxed (&ret);
269 g_value_unset (&ret);
271 caps = gst_pad_get_current_caps (rtpdemux->sink);
274 GST_DEBUG ("pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
280 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
284 GST_OBJECT_LOCK (rtpdemux);
285 GST_DEBUG ("clearing pt map");
286 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
287 GstRtpPtDemuxPad *pad = walk->data;
291 GST_OBJECT_UNLOCK (rtpdemux);
295 need_caps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
298 gboolean ret = FALSE;
300 GST_OBJECT_LOCK (rtpdemux);
301 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
302 GstRtpPtDemuxPad *pad = walk->data;
308 GST_OBJECT_UNLOCK (rtpdemux);
315 clear_newcaps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
319 GST_OBJECT_LOCK (rtpdemux);
320 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
321 GstRtpPtDemuxPad *pad = walk->data;
324 pad->newcaps = FALSE;
328 GST_OBJECT_UNLOCK (rtpdemux);
332 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
334 GstPad *srcpad = GST_PAD_CAST (user_data);
336 /* Stream start and caps have already been pushed */
337 if (GST_EVENT_TYPE (*event) >= GST_EVENT_SEGMENT)
338 gst_pad_push_event (srcpad, gst_event_ref (*event));
344 gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
346 GstFlowReturn ret = GST_FLOW_OK;
347 GstRtpPtDemux *rtpdemux;
351 GstRTPBuffer rtp = { NULL };
353 rtpdemux = GST_RTP_PT_DEMUX (parent);
355 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
358 pt = gst_rtp_buffer_get_payload_type (&rtp);
359 gst_rtp_buffer_unmap (&rtp);
361 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
363 srcpad = find_pad_for_pt (rtpdemux, pt);
364 if (srcpad == NULL) {
365 /* new PT, create a src pad */
366 GstRtpPtDemuxPad *rtpdemuxpad;
367 GstElementClass *klass;
368 GstPadTemplate *templ;
371 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
375 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
376 templ = gst_element_class_get_pad_template (klass, "src_%u");
377 padname = g_strdup_printf ("src_%u", pt);
378 srcpad = gst_pad_new_from_template (templ, padname);
379 gst_pad_use_fixed_caps (srcpad);
381 gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
383 GST_DEBUG ("Adding pt=%d to the list.", pt);
384 rtpdemuxpad = g_slice_new0 (GstRtpPtDemuxPad);
385 rtpdemuxpad->pt = pt;
386 rtpdemuxpad->newcaps = FALSE;
387 rtpdemuxpad->pad = srcpad;
388 gst_object_ref (srcpad);
389 GST_OBJECT_LOCK (rtpdemux);
390 rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
391 GST_OBJECT_UNLOCK (rtpdemux);
393 gst_pad_set_active (srcpad, TRUE);
396 /* First push the stream-start event, it must always come first */
397 gst_pad_push_event (srcpad,
398 gst_pad_get_sticky_event (rtpdemux->sink, GST_EVENT_STREAM_START, 0));
400 /* Then caps event is sent */
401 caps = gst_caps_make_writable (caps);
402 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
403 gst_pad_set_caps (srcpad, caps);
404 gst_caps_unref (caps);
406 /* First sticky events on sink pad are forwarded to the new src pad */
407 gst_pad_sticky_events_foreach (rtpdemux->sink, forward_sticky_events,
410 gst_element_add_pad (GST_ELEMENT_CAST (rtpdemux), srcpad);
412 GST_DEBUG ("emitting new-payload-type for pt %d", pt);
413 g_signal_emit (G_OBJECT (rtpdemux),
414 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
417 if (pt != rtpdemux->last_pt) {
420 /* our own signal with an extra flag that this is the only pad */
421 rtpdemux->last_pt = pt;
422 GST_DEBUG ("emitting payload-type-changed for pt %d", emit_pt);
423 g_signal_emit (G_OBJECT (rtpdemux),
424 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
427 while (need_caps_for_pt (rtpdemux, pt)) {
428 GST_DEBUG ("need new caps for %d", pt);
429 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
433 clear_newcaps_for_pt (rtpdemux, pt);
435 caps = gst_caps_make_writable (caps);
436 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
437 gst_pad_set_caps (srcpad, caps);
438 gst_caps_unref (caps);
442 ret = gst_pad_push (srcpad, buf);
444 gst_object_unref (srcpad);
451 /* this should not be fatal */
452 GST_ELEMENT_WARNING (rtpdemux, STREAM, DEMUX, (NULL),
453 ("Dropping invalid RTP payload"));
454 gst_buffer_unref (buf);
455 return GST_FLOW_ERROR;
459 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
460 ("Could not get caps for payload"));
461 gst_buffer_unref (buf);
463 gst_object_unref (srcpad);
464 return GST_FLOW_ERROR;
469 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
471 GstPad *respad = NULL;
474 GST_OBJECT_LOCK (rtpdemux);
475 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
476 GstRtpPtDemuxPad *pad = walk->data;
479 respad = gst_object_ref (pad->pad);
483 GST_OBJECT_UNLOCK (rtpdemux);
489 gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
491 GstRtpPtDemux *rtpdemux;
492 gboolean res = FALSE;
494 rtpdemux = GST_RTP_PT_DEMUX (parent);
496 switch (GST_EVENT_TYPE (event)) {
499 gst_rtp_pt_demux_clear_pt_map (rtpdemux);
500 /* don't forward the event, we cleared the ptmap and on the next buffer we
501 * will add the pt to the caps and push a new caps event */
502 gst_event_unref (event);
506 case GST_EVENT_CUSTOM_DOWNSTREAM:
508 const GstStructure *s;
510 s = gst_event_get_structure (event);
512 if (gst_structure_has_name (s, "GstRTPPacketLost")) {
513 GstPad *srcpad = find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
516 res = gst_pad_push_event (srcpad, event);
517 gst_object_unref (srcpad);
519 gst_event_unref (event);
523 res = gst_pad_event_default (pad, parent, event);
528 res = gst_pad_event_default (pad, parent, event);
537 gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
539 GstRtpPtDemux *demux;
540 const GstStructure *s;
542 demux = GST_RTP_PT_DEMUX (parent);
544 switch (GST_EVENT_TYPE (event)) {
545 case GST_EVENT_CUSTOM_UPSTREAM:
546 case GST_EVENT_CUSTOM_BOTH:
547 case GST_EVENT_CUSTOM_BOTH_OOB:
548 s = gst_event_get_structure (event);
549 if (s && !gst_structure_has_field (s, "payload")) {
552 GST_OBJECT_LOCK (demux);
553 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
554 GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
556 if (dpad->pad == pad) {
560 GST_EVENT_CAST (gst_mini_object_make_writable
561 (GST_MINI_OBJECT_CAST (event)));
562 ws = gst_event_writable_structure (event);
563 gst_structure_set (ws, "payload", G_TYPE_UINT, dpad->pt, NULL);
567 GST_OBJECT_UNLOCK (demux);
574 return gst_pad_event_default (pad, parent, event);
578 * Reserves resources for the object.
581 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
583 ptdemux->srcpads = NULL;
584 ptdemux->last_pt = 0xFFFF;
590 * Free resources for the object.
593 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
598 GST_OBJECT_LOCK (ptdemux);
599 tmppads = ptdemux->srcpads;
600 ptdemux->srcpads = NULL;
601 GST_OBJECT_UNLOCK (ptdemux);
603 for (walk = tmppads; walk; walk = g_slist_next (walk)) {
604 GstRtpPtDemuxPad *pad = walk->data;
606 gst_pad_set_active (pad->pad, FALSE);
607 gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
608 g_slice_free (GstRtpPtDemuxPad, pad);
610 g_slist_free (tmppads);
613 static GstStateChangeReturn
614 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
616 GstStateChangeReturn ret;
617 GstRtpPtDemux *ptdemux;
619 ptdemux = GST_RTP_PT_DEMUX (element);
621 switch (transition) {
622 case GST_STATE_CHANGE_NULL_TO_READY:
623 if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
624 ret = GST_STATE_CHANGE_FAILURE;
626 case GST_STATE_CHANGE_READY_TO_PAUSED:
627 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
632 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
634 switch (transition) {
635 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
636 case GST_STATE_CHANGE_PAUSED_TO_READY:
638 case GST_STATE_CHANGE_READY_TO_NULL:
639 gst_rtp_pt_demux_release (ptdemux);