1 /*
2 * RTP Demux element
3 *
4 * Copyright (C) 2005 Nokia Corporation.
5 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
6 *
7 * Loosely based on GStreamer gstdecodebin
8 * Copyright (C) <2004> Wim Taymans <wim.taymans@gmail.com>
9 *
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.
14 *
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.
19 *
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.
24 */
25
26 /**
27 * SECTION:element-rtpptdemux
28 * @title: rtpptdemux
29 *
30 * rtpptdemux acts as a demuxer for RTP packets based on the payload type of
31 * the packets. Its main purpose is to allow an application to easily receive
32 * and decode an RTP stream with multiple payload types.
33 *
34 * For each payload type that is detected, a new pad will be created and the
35 * #GstRtpPtDemux::new-payload-type signal will be emitted. When the payload for
36 * the RTP stream changes, the #GstRtpPtDemux::payload-type-change signal will be
37 * emitted.
38 *
39 * The element will try to set complete and unique application/x-rtp caps
40 * on the output pads based on the result of the #GstRtpPtDemux::request-pt-map
41 * signal.
42 *
43 * ## Example pipelines
44 * |[
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.
48 *
49 */
50
51 /*
52 * Contributors:
53 * Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
54 */
55 /*
56 * Status:
57 * - works with the test_rtpdemux.c tool
58 *
59 * Check:
60 * - is emitting a signal enough, or should we
61 * use GstEvent to notify downstream elements
62 * of the new packet... no?
63 *
64 * Notes:
65 * - emits event both for new PTs, and whenever
66 * a PT is changed
67 */
68
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #include <string.h>
74 #include <gst/gst.h>
75 #include <gst/rtp/gstrtpbuffer.h>
76
77 #include "gstrtpptdemux.h"
78
79 /* generic templates */
80 static GstStaticPadTemplate rtp_pt_demux_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
82 GST_PAD_SINK,
83 GST_PAD_ALWAYS,
84 GST_STATIC_CAPS ("application/x-rtp")
85 );
86
87 static GstStaticPadTemplate rtp_pt_demux_src_template =
88 GST_STATIC_PAD_TEMPLATE ("src_%u",
89 GST_PAD_SRC,
90 GST_PAD_SOMETIMES,
91 GST_STATIC_CAPS ("application/x-rtp, " "payload = (int) [ 0, 255 ]")
92 );
93
94 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pt_demux_debug);
95 #define GST_CAT_DEFAULT gst_rtp_pt_demux_debug
96
97 /*
98 * Item for storing GstPad<->pt pairs.
99 */
100 struct _GstRtpPtDemuxPad
101 {
102 GstPad *pad; /*< pointer to the actual pad */
103 gint pt; /*< RTP payload-type attached to pad */
104 gboolean newcaps;
105 };
106
107 /* signals */
108 enum
109 {
110 SIGNAL_REQUEST_PT_MAP,
111 SIGNAL_NEW_PAYLOAD_TYPE,
112 SIGNAL_PAYLOAD_TYPE_CHANGE,
113 SIGNAL_CLEAR_PT_MAP,
114 LAST_SIGNAL
115 };
116
117 enum
118 {
119 PROP_0,
120 PROP_IGNORED_PTS,
121 };
122
123 #define gst_rtp_pt_demux_parent_class parent_class
124 G_DEFINE_TYPE (GstRtpPtDemux, gst_rtp_pt_demux, GST_TYPE_ELEMENT);
125 GST_ELEMENT_REGISTER_DEFINE (rtpptdemux, "rtpptdemux", GST_RANK_NONE,
126 GST_TYPE_RTP_PT_DEMUX);
127
128 static void gst_rtp_pt_demux_finalize (GObject * object);
129
130 static void gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux);
131 static gboolean gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux);
132
133 static gboolean gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent,
134 GstEvent * event);
135 static GstFlowReturn gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent,
136 GstBuffer * buf);
137 static GstStateChangeReturn gst_rtp_pt_demux_change_state (GstElement * element,
138 GstStateChange transition);
139 static void gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux);
140
141 static GstPad *find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt);
142
143 static gboolean gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent,
144 GstEvent * event);
145
146
147 static guint gst_rtp_pt_demux_signals[LAST_SIGNAL] = { 0 };
148
149 static void
gst_rtp_pt_demux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)150 gst_rtp_pt_demux_set_property (GObject * object, guint prop_id,
151 const GValue * value, GParamSpec * pspec)
152 {
153 GstRtpPtDemux *rtpptdemux = GST_RTP_PT_DEMUX (object);
154
155 switch (prop_id) {
156 case PROP_IGNORED_PTS:
157 g_value_copy (value, &rtpptdemux->ignored_pts);
158 break;
159 default:
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 break;
162 }
163 }
164
165 static void
gst_rtp_pt_demux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)166 gst_rtp_pt_demux_get_property (GObject * object, guint prop_id,
167 GValue * value, GParamSpec * pspec)
168 {
169 GstRtpPtDemux *rtpptdemux = GST_RTP_PT_DEMUX (object);
170
171 switch (prop_id) {
172 case PROP_IGNORED_PTS:
173 g_value_copy (&rtpptdemux->ignored_pts, value);
174 break;
175 default:
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 break;
178 }
179 }
180
181 static void
gst_rtp_pt_demux_class_init(GstRtpPtDemuxClass * klass)182 gst_rtp_pt_demux_class_init (GstRtpPtDemuxClass * klass)
183 {
184 GObjectClass *gobject_klass;
185 GstElementClass *gstelement_klass;
186
187 gobject_klass = (GObjectClass *) klass;
188 gstelement_klass = (GstElementClass *) klass;
189
190 /**
191 * GstRtpPtDemux::request-pt-map:
192 * @demux: the object which received the signal
193 * @pt: the payload type
194 *
195 * Request the payload type as #GstCaps for @pt.
196 */
197 gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP] =
198 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, request_pt_map),
200 NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
201
202 /**
203 * GstRtpPtDemux::new-payload-type:
204 * @demux: the object which received the signal
205 * @pt: the payload type
206 * @pad: the pad with the new payload
207 *
208 * Emitted when a new payload type pad has been created in @demux.
209 */
210 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE] =
211 g_signal_new ("new-payload-type", G_TYPE_FROM_CLASS (klass),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass, new_payload_type),
213 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, GST_TYPE_PAD);
214
215 /**
216 * GstRtpPtDemux::payload-type-change:
217 * @demux: the object which received the signal
218 * @pt: the new payload type
219 *
220 * Emitted when the payload type changed.
221 */
222 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE] =
223 g_signal_new ("payload-type-change", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
225 payload_type_change), NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_UINT);
226
227 /**
228 * GstRtpPtDemux::clear-pt-map:
229 * @demux: the object which received the signal
230 *
231 * The application can call this signal to instruct the element to discard the
232 * currently cached payload type map.
233 */
234 gst_rtp_pt_demux_signals[SIGNAL_CLEAR_PT_MAP] =
235 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpPtDemuxClass,
237 clear_pt_map), NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
238
239 gobject_klass->set_property = gst_rtp_pt_demux_set_property;
240 gobject_klass->get_property = gst_rtp_pt_demux_get_property;
241
242 /**
243 * GstRtpPtDemux:ignored-payload-types:
244 *
245 * If specified, packets with an ignored payload type will be dropped,
246 * instead of causing a new pad to be exposed for these to be pushed on.
247 *
248 * This is for example useful to drop FEC protection packets, as they
249 * need to go through the #GstRtpJitterBuffer, but cease to be useful
250 * past that point, #GstRtpBin will make use of this property for that
251 * purpose.
252 *
253 * Since: 1.14
254 */
255 g_object_class_install_property (gobject_klass, PROP_IGNORED_PTS,
256 gst_param_spec_array ("ignored-payload-types",
257 "Ignored payload types",
258 "Packets with these payload types will be dropped",
259 g_param_spec_int ("payload-types", "payload-types", "Payload types",
260 0, G_MAXINT, 0,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263
264 gobject_klass->finalize = gst_rtp_pt_demux_finalize;
265
266 gstelement_klass->change_state =
267 GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_change_state);
268
269 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_pt_demux_clear_pt_map);
270
271 gst_element_class_add_static_pad_template (gstelement_klass,
272 &rtp_pt_demux_sink_template);
273 gst_element_class_add_static_pad_template (gstelement_klass,
274 &rtp_pt_demux_src_template);
275
276 gst_element_class_set_static_metadata (gstelement_klass, "RTP Demux",
277 "Demux/Network/RTP",
278 "Parses codec streams transmitted in the same RTP session",
279 "Kai Vehmanen <kai.vehmanen@nokia.com>");
280
281 GST_DEBUG_CATEGORY_INIT (gst_rtp_pt_demux_debug,
282 "rtpptdemux", 0, "RTP codec demuxer");
283
284 GST_DEBUG_REGISTER_FUNCPTR (gst_rtp_pt_demux_chain);
285 }
286
287 static void
gst_rtp_pt_demux_init(GstRtpPtDemux * ptdemux)288 gst_rtp_pt_demux_init (GstRtpPtDemux * ptdemux)
289 {
290 GstElementClass *klass = GST_ELEMENT_GET_CLASS (ptdemux);
291
292 ptdemux->sink =
293 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
294 "sink"), "sink");
295 g_assert (ptdemux->sink != NULL);
296
297 gst_pad_set_chain_function (ptdemux->sink, gst_rtp_pt_demux_chain);
298 gst_pad_set_event_function (ptdemux->sink, gst_rtp_pt_demux_sink_event);
299
300 gst_element_add_pad (GST_ELEMENT (ptdemux), ptdemux->sink);
301
302 g_value_init (&ptdemux->ignored_pts, GST_TYPE_ARRAY);
303 }
304
305 static void
gst_rtp_pt_demux_finalize(GObject * object)306 gst_rtp_pt_demux_finalize (GObject * object)
307 {
308 gst_rtp_pt_demux_release (GST_RTP_PT_DEMUX (object));
309
310 g_value_unset (&GST_RTP_PT_DEMUX (object)->ignored_pts);
311
312 G_OBJECT_CLASS (parent_class)->finalize (object);
313 }
314
315 static GstCaps *
gst_rtp_pt_demux_get_caps(GstRtpPtDemux * rtpdemux,guint pt)316 gst_rtp_pt_demux_get_caps (GstRtpPtDemux * rtpdemux, guint pt)
317 {
318 guint32 ssrc = 0;
319 gboolean have_ssrc = FALSE;
320 GstCaps *caps, *sink_caps;
321 GValue ret = { 0 };
322 GValue args[2] = { {0}, {0} };
323
324 /* figure out the caps */
325 g_value_init (&args[0], GST_TYPE_ELEMENT);
326 g_value_set_object (&args[0], rtpdemux);
327 g_value_init (&args[1], G_TYPE_UINT);
328 g_value_set_uint (&args[1], pt);
329
330 g_value_init (&ret, GST_TYPE_CAPS);
331 g_value_set_boxed (&ret, NULL);
332
333 g_signal_emitv (args, gst_rtp_pt_demux_signals[SIGNAL_REQUEST_PT_MAP], 0,
334 &ret);
335
336 g_value_unset (&args[0]);
337 g_value_unset (&args[1]);
338 caps = g_value_dup_boxed (&ret);
339 sink_caps = gst_pad_get_current_caps (rtpdemux->sink);
340 g_value_unset (&ret);
341
342 if (caps == NULL) {
343 caps = sink_caps;
344 } else if (sink_caps) {
345 have_ssrc =
346 gst_structure_get_uint (gst_caps_get_structure (sink_caps, 0), "ssrc",
347 &ssrc);
348 gst_caps_unref (sink_caps);
349 }
350
351 if (caps != NULL) {
352 caps = gst_caps_make_writable (caps);
353 gst_caps_set_simple (caps, "payload", G_TYPE_INT, pt, NULL);
354 if (have_ssrc)
355 gst_caps_set_simple (caps, "ssrc", G_TYPE_UINT, ssrc, NULL);
356 }
357
358 GST_DEBUG_OBJECT (rtpdemux, "pt %d, got caps %" GST_PTR_FORMAT, pt, caps);
359
360 return caps;
361 }
362
363 static void
gst_rtp_pt_demux_clear_pt_map(GstRtpPtDemux * rtpdemux)364 gst_rtp_pt_demux_clear_pt_map (GstRtpPtDemux * rtpdemux)
365 {
366 GSList *walk;
367
368 GST_OBJECT_LOCK (rtpdemux);
369 GST_DEBUG_OBJECT (rtpdemux, "clearing pt map");
370 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
371 GstRtpPtDemuxPad *pad = walk->data;
372
373 pad->newcaps = TRUE;
374 }
375 GST_OBJECT_UNLOCK (rtpdemux);
376 }
377
378 static gboolean
need_caps_for_pt(GstRtpPtDemux * rtpdemux,guint8 pt)379 need_caps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
380 {
381 GSList *walk;
382 gboolean ret = FALSE;
383
384 GST_OBJECT_LOCK (rtpdemux);
385 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
386 GstRtpPtDemuxPad *pad = walk->data;
387
388 if (pad->pt == pt) {
389 ret = pad->newcaps;
390 }
391 }
392 GST_OBJECT_UNLOCK (rtpdemux);
393
394 return ret;
395 }
396
397
398 static void
clear_newcaps_for_pt(GstRtpPtDemux * rtpdemux,guint8 pt)399 clear_newcaps_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
400 {
401 GSList *walk;
402
403 GST_OBJECT_LOCK (rtpdemux);
404 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
405 GstRtpPtDemuxPad *pad = walk->data;
406
407 if (pad->pt == pt) {
408 pad->newcaps = FALSE;
409 break;
410 }
411 }
412 GST_OBJECT_UNLOCK (rtpdemux);
413 }
414
415 static gboolean
forward_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)416 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
417 {
418 GstPad *srcpad = GST_PAD_CAST (user_data);
419
420 /* Stream start and caps have already been pushed */
421 if (GST_EVENT_TYPE (*event) >= GST_EVENT_SEGMENT)
422 gst_pad_push_event (srcpad, gst_event_ref (*event));
423
424 return TRUE;
425 }
426
427 static gboolean
gst_rtp_pt_demux_pt_is_ignored(GstRtpPtDemux * ptdemux,guint8 pt)428 gst_rtp_pt_demux_pt_is_ignored (GstRtpPtDemux * ptdemux, guint8 pt)
429 {
430 gboolean ret = FALSE;
431 guint i;
432
433 for (i = 0; i < gst_value_array_get_size (&ptdemux->ignored_pts); i++) {
434 const GValue *tmp = gst_value_array_get_value (&ptdemux->ignored_pts, i);
435
436 if (g_value_get_int (tmp) == pt) {
437 ret = TRUE;
438 break;
439 }
440 }
441
442 return ret;
443 }
444
445 static GstFlowReturn
gst_rtp_pt_demux_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)446 gst_rtp_pt_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
447 {
448 GstFlowReturn ret = GST_FLOW_OK;
449 GstRtpPtDemux *rtpdemux;
450 guint8 pt;
451 GstPad *srcpad;
452 GstCaps *caps;
453 GstRTPBuffer rtp = { NULL };
454
455 rtpdemux = GST_RTP_PT_DEMUX (parent);
456
457 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
458 goto invalid_buffer;
459
460 pt = gst_rtp_buffer_get_payload_type (&rtp);
461 gst_rtp_buffer_unmap (&rtp);
462
463 if (gst_rtp_pt_demux_pt_is_ignored (rtpdemux, pt))
464 goto ignored;
465
466 GST_DEBUG_OBJECT (rtpdemux, "received buffer for pt %d", pt);
467
468 srcpad = find_pad_for_pt (rtpdemux, pt);
469 if (srcpad == NULL) {
470 /* new PT, create a src pad */
471 GstRtpPtDemuxPad *rtpdemuxpad;
472 GstElementClass *klass;
473 GstPadTemplate *templ;
474 gchar *padname;
475
476 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
477 if (!caps)
478 goto no_caps;
479
480 /* must be after the get_caps() call as get_caps() may cause external code
481 * (e.g. rtpbin) to update the ignored-pt list */
482 if (gst_rtp_pt_demux_pt_is_ignored (rtpdemux, pt)) {
483 gst_clear_caps (&caps);
484 goto ignored;
485 }
486
487 klass = GST_ELEMENT_GET_CLASS (rtpdemux);
488 templ = gst_element_class_get_pad_template (klass, "src_%u");
489 padname = g_strdup_printf ("src_%u", pt);
490 srcpad = gst_pad_new_from_template (templ, padname);
491 gst_pad_use_fixed_caps (srcpad);
492 g_free (padname);
493 gst_pad_set_event_function (srcpad, gst_rtp_pt_demux_src_event);
494
495 GST_DEBUG_OBJECT (rtpdemux, "Adding pt=%d to the list.", pt);
496 rtpdemuxpad = g_slice_new0 (GstRtpPtDemuxPad);
497 rtpdemuxpad->pt = pt;
498 rtpdemuxpad->newcaps = FALSE;
499 rtpdemuxpad->pad = srcpad;
500 gst_object_ref (srcpad);
501 GST_OBJECT_LOCK (rtpdemux);
502 rtpdemux->srcpads = g_slist_append (rtpdemux->srcpads, rtpdemuxpad);
503 GST_OBJECT_UNLOCK (rtpdemux);
504
505 gst_pad_set_active (srcpad, TRUE);
506
507 /* First push the stream-start event, it must always come first */
508 gst_pad_push_event (srcpad,
509 gst_pad_get_sticky_event (rtpdemux->sink, GST_EVENT_STREAM_START, 0));
510
511 /* Then caps event is sent */
512 gst_pad_set_caps (srcpad, caps);
513 gst_caps_unref (caps);
514
515 /* First sticky events on sink pad are forwarded to the new src pad */
516 gst_pad_sticky_events_foreach (rtpdemux->sink, forward_sticky_events,
517 srcpad);
518
519 gst_element_add_pad (GST_ELEMENT_CAST (rtpdemux), srcpad);
520
521 GST_DEBUG_OBJECT (rtpdemux, "emitting new-payload-type for pt %d", pt);
522 g_signal_emit (G_OBJECT (rtpdemux),
523 gst_rtp_pt_demux_signals[SIGNAL_NEW_PAYLOAD_TYPE], 0, pt, srcpad);
524 }
525
526 if (pt != rtpdemux->last_pt) {
527 gint emit_pt = pt;
528
529 /* our own signal with an extra flag that this is the only pad */
530 rtpdemux->last_pt = pt;
531 GST_DEBUG_OBJECT (rtpdemux, "emitting payload-type-changed for pt %d",
532 emit_pt);
533 g_signal_emit (G_OBJECT (rtpdemux),
534 gst_rtp_pt_demux_signals[SIGNAL_PAYLOAD_TYPE_CHANGE], 0, emit_pt);
535 }
536
537 while (need_caps_for_pt (rtpdemux, pt)) {
538 GST_DEBUG_OBJECT (rtpdemux, "need new caps for %d", pt);
539 caps = gst_rtp_pt_demux_get_caps (rtpdemux, pt);
540 if (!caps)
541 goto no_caps;
542
543 clear_newcaps_for_pt (rtpdemux, pt);
544
545 gst_pad_set_caps (srcpad, caps);
546 gst_caps_unref (caps);
547 }
548
549 /* push to srcpad */
550 ret = gst_pad_push (srcpad, buf);
551
552 gst_object_unref (srcpad);
553
554 return ret;
555
556 ignored:
557 {
558 GST_DEBUG_OBJECT (rtpdemux, "Dropped buffer for pt %d", pt);
559 gst_buffer_unref (buf);
560 return GST_FLOW_OK;
561 }
562
563 /* ERRORS */
564 invalid_buffer:
565 {
566 /* this should not be fatal */
567 GST_ELEMENT_WARNING (rtpdemux, STREAM, DEMUX, (NULL),
568 ("Dropping invalid RTP payload"));
569 gst_buffer_unref (buf);
570 return GST_FLOW_OK;
571 }
572 no_caps:
573 {
574 GST_ELEMENT_ERROR (rtpdemux, STREAM, DECODE, (NULL),
575 ("Could not get caps for payload"));
576 gst_buffer_unref (buf);
577 if (srcpad)
578 gst_object_unref (srcpad);
579 return GST_FLOW_ERROR;
580 }
581 }
582
583 static GstPad *
find_pad_for_pt(GstRtpPtDemux * rtpdemux,guint8 pt)584 find_pad_for_pt (GstRtpPtDemux * rtpdemux, guint8 pt)
585 {
586 GstPad *respad = NULL;
587 GSList *walk;
588
589 GST_OBJECT_LOCK (rtpdemux);
590 for (walk = rtpdemux->srcpads; walk; walk = g_slist_next (walk)) {
591 GstRtpPtDemuxPad *pad = walk->data;
592
593 if (pad->pt == pt) {
594 respad = gst_object_ref (pad->pad);
595 break;
596 }
597 }
598 GST_OBJECT_UNLOCK (rtpdemux);
599
600 return respad;
601 }
602
603 static gboolean
gst_rtp_pt_demux_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)604 gst_rtp_pt_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
605 {
606 GstRtpPtDemux *rtpdemux;
607 gboolean res = FALSE;
608
609 rtpdemux = GST_RTP_PT_DEMUX (parent);
610
611 switch (GST_EVENT_TYPE (event)) {
612 case GST_EVENT_CAPS:
613 {
614 gst_rtp_pt_demux_clear_pt_map (rtpdemux);
615 /* don't forward the event, we cleared the ptmap and on the next buffer we
616 * will add the pt to the caps and push a new caps event */
617 gst_event_unref (event);
618 res = TRUE;
619 break;
620 }
621 case GST_EVENT_CUSTOM_DOWNSTREAM:
622 {
623 const GstStructure *s;
624
625 s = gst_event_get_structure (event);
626
627 if (gst_structure_has_name (s, "GstRTPPacketLost")) {
628 GstPad *srcpad = find_pad_for_pt (rtpdemux, rtpdemux->last_pt);
629
630 if (srcpad) {
631 res = gst_pad_push_event (srcpad, event);
632 gst_object_unref (srcpad);
633 } else {
634 gst_event_unref (event);
635 }
636
637 } else {
638 res = gst_pad_event_default (pad, parent, event);
639 }
640 break;
641 }
642 default:
643 res = gst_pad_event_default (pad, parent, event);
644 break;
645 }
646
647 return res;
648 }
649
650
651 static gboolean
gst_rtp_pt_demux_src_event(GstPad * pad,GstObject * parent,GstEvent * event)652 gst_rtp_pt_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
653 {
654 GstRtpPtDemux *demux;
655 const GstStructure *s;
656
657 demux = GST_RTP_PT_DEMUX (parent);
658
659 switch (GST_EVENT_TYPE (event)) {
660 case GST_EVENT_CUSTOM_UPSTREAM:
661 case GST_EVENT_CUSTOM_BOTH:
662 case GST_EVENT_CUSTOM_BOTH_OOB:
663 s = gst_event_get_structure (event);
664 if (s && !gst_structure_has_field (s, "payload")) {
665 GSList *walk;
666
667 GST_OBJECT_LOCK (demux);
668 for (walk = demux->srcpads; walk; walk = g_slist_next (walk)) {
669 GstRtpPtDemuxPad *dpad = (GstRtpPtDemuxPad *) walk->data;
670
671 if (dpad->pad == pad) {
672 GstStructure *ws;
673
674 event =
675 GST_EVENT_CAST (gst_mini_object_make_writable
676 (GST_MINI_OBJECT_CAST (event)));
677 ws = gst_event_writable_structure (event);
678 gst_structure_set (ws, "payload", G_TYPE_UINT, dpad->pt, NULL);
679 break;
680 }
681 }
682 GST_OBJECT_UNLOCK (demux);
683 }
684 break;
685 default:
686 break;
687 }
688
689 return gst_pad_event_default (pad, parent, event);
690 }
691
692 /*
693 * Reserves resources for the object.
694 */
695 static gboolean
gst_rtp_pt_demux_setup(GstRtpPtDemux * ptdemux)696 gst_rtp_pt_demux_setup (GstRtpPtDemux * ptdemux)
697 {
698 ptdemux->srcpads = NULL;
699 ptdemux->last_pt = 0xFFFF;
700
701 return TRUE;
702 }
703
704 /*
705 * Free resources for the object.
706 */
707 static void
gst_rtp_pt_demux_release(GstRtpPtDemux * ptdemux)708 gst_rtp_pt_demux_release (GstRtpPtDemux * ptdemux)
709 {
710 GSList *tmppads;
711 GSList *walk;
712
713 GST_OBJECT_LOCK (ptdemux);
714 tmppads = ptdemux->srcpads;
715 ptdemux->srcpads = NULL;
716 GST_OBJECT_UNLOCK (ptdemux);
717
718 for (walk = tmppads; walk; walk = g_slist_next (walk)) {
719 GstRtpPtDemuxPad *pad = walk->data;
720
721 gst_pad_set_active (pad->pad, FALSE);
722 gst_element_remove_pad (GST_ELEMENT_CAST (ptdemux), pad->pad);
723 g_slice_free (GstRtpPtDemuxPad, pad);
724 }
725 g_slist_free (tmppads);
726 }
727
728 static GstStateChangeReturn
gst_rtp_pt_demux_change_state(GstElement * element,GstStateChange transition)729 gst_rtp_pt_demux_change_state (GstElement * element, GstStateChange transition)
730 {
731 GstStateChangeReturn ret;
732 GstRtpPtDemux *ptdemux;
733
734 ptdemux = GST_RTP_PT_DEMUX (element);
735
736 switch (transition) {
737 case GST_STATE_CHANGE_NULL_TO_READY:
738 if (gst_rtp_pt_demux_setup (ptdemux) != TRUE)
739 ret = GST_STATE_CHANGE_FAILURE;
740 break;
741 case GST_STATE_CHANGE_READY_TO_PAUSED:
742 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
743 default:
744 break;
745 }
746
747 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
748
749 switch (transition) {
750 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
751 break;
752 case GST_STATE_CHANGE_PAUSED_TO_READY:
753 case GST_STATE_CHANGE_READY_TO_NULL:
754 gst_rtp_pt_demux_release (ptdemux);
755 break;
756 default:
757 break;
758 }
759
760 return ret;
761 }
762