1 /* GStreamer streamiddemux element
3 * Copyright 2013 LGE Corporation.
4 * @author: Hoonhee Lee <hoonhee.lee@lge.com>
5 * @author: Jeongseok Kim <jeongseok.kim@lge.com>
6 * @author: Wonchul Lee <wonchul86.lee@lge.com>
8 * gststreamiddemux.c: Simple stream-id-demultiplexer element
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 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 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * SECTION:element-streamid-demux
27 * @see_also: #GstFunnel
29 * Direct input stream to one out of N output pads by stream-id.
38 #include "gststreamiddemux.h"
40 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
41 #define GST_CAT_DEFAULT streamid_demux_debug
50 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
51 GST_STATIC_PAD_TEMPLATE ("sink",
56 static GstStaticPadTemplate gst_streamid_demux_src_factory =
57 GST_STATIC_PAD_TEMPLATE ("src_%u",
63 GST_DEBUG_CATEGORY_INIT (streamid_demux_debug, \
64 "streamiddemux", 0, "Streamid demuxer");
65 #define gst_streamid_demux_parent_class parent_class
66 G_DEFINE_TYPE_WITH_CODE (GstStreamidDemux, gst_streamid_demux,
67 GST_TYPE_ELEMENT, _do_init);
69 static void gst_streamid_demux_dispose (GObject * object);
70 static void gst_streamid_demux_get_property (GObject * object, guint prop_id,
71 GValue * value, GParamSpec * pspec);
72 static GstFlowReturn gst_streamid_demux_chain (GstPad * pad,
73 GstObject * parent, GstBuffer * buf);
74 static gboolean gst_streamid_demux_event (GstPad * pad, GstObject * parent,
76 static GstStateChangeReturn gst_streamid_demux_change_state (GstElement *
77 element, GstStateChange transition);
78 static GstPad *gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux *
79 demux, const gchar * stream_id);
80 static gboolean gst_streamid_demux_srcpad_create (GstStreamidDemux * demux,
81 GstPad * pad, const gchar * stream_id);
82 static void gst_streamid_demux_reset (GstStreamidDemux * demux);
83 static void gst_streamid_demux_release_srcpad (const GValue * item,
84 GstStreamidDemux * demux);
87 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
92 gobject_class->get_property = gst_streamid_demux_get_property;
93 gobject_class->dispose = gst_streamid_demux_dispose;
95 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
96 g_param_spec_object ("active-pad", "Active pad",
97 "The currently active src pad", GST_TYPE_PAD,
98 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
100 gst_element_class_set_static_metadata (gstelement_class, "Streamid Demux",
101 "Generic", "1-to-N output stream by stream-id",
102 "HoonHee Lee <hoonhee.lee@lge.com>");
103 gst_element_class_add_pad_template (gstelement_class,
104 gst_static_pad_template_get (&gst_streamid_demux_sink_factory));
106 gst_element_class_add_pad_template (gstelement_class,
107 gst_static_pad_template_get (&gst_streamid_demux_src_factory));
109 gstelement_class->change_state = gst_streamid_demux_change_state;
113 gst_streamid_demux_init (GstStreamidDemux * demux)
116 gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
118 gst_pad_set_chain_function (demux->sinkpad,
119 GST_DEBUG_FUNCPTR (gst_streamid_demux_chain));
120 gst_pad_set_event_function (demux->sinkpad,
121 GST_DEBUG_FUNCPTR (gst_streamid_demux_event));
123 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
125 /* srcpad management */
126 demux->active_srcpad = NULL;
127 demux->nb_srcpads = 0;
129 /* initialize hash table for srcpad */
130 demux->stream_id_pairs =
131 g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free,
132 (GDestroyNotify) gst_object_unref);
136 gst_streamid_demux_dispose (GObject * object)
138 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
140 gst_streamid_demux_reset (demux);
142 G_OBJECT_CLASS (parent_class)->dispose (object);
146 gst_streamid_demux_get_property (GObject * object, guint prop_id,
147 GValue * value, GParamSpec * pspec)
149 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
152 case PROP_ACTIVE_PAD:
153 GST_OBJECT_LOCK (demux);
154 g_value_set_object (value, demux->active_srcpad);
155 GST_OBJECT_UNLOCK (demux);
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
166 GstPad *srcpad = GST_PAD_CAST (user_data);
168 gst_pad_push_event (srcpad, gst_event_ref (*event));
174 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
175 const gchar * stream_id)
177 gchar *padname = NULL;
178 GstPad *srcpad = NULL;
179 GstPadTemplate *pad_tmpl = NULL;
181 padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
182 pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
184 GST_LOG_OBJECT (demux, "generating a srcpad:%s", padname);
185 srcpad = gst_pad_new_from_template (pad_tmpl, padname);
186 gst_object_unref (pad_tmpl);
188 g_return_val_if_fail (srcpad != NULL, FALSE);
190 demux->active_srcpad = srcpad;
191 g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
192 gst_object_ref (srcpad));
198 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
200 GstFlowReturn res = GST_FLOW_OK;
201 GstStreamidDemux *demux = NULL;
202 GstPad *srcpad = NULL;
204 demux = GST_STREAMID_DEMUX (parent);
206 GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
207 demux->active_srcpad);
209 GST_OBJECT_LOCK (demux);
210 if (demux->active_srcpad) {
211 srcpad = gst_object_ref (demux->active_srcpad);
212 GST_OBJECT_UNLOCK (demux);
213 res = gst_pad_push (srcpad, buf);
214 gst_object_unref (srcpad);
216 GST_OBJECT_UNLOCK (demux);
217 goto no_active_srcpad;
220 GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
226 GST_WARNING_OBJECT (demux, "srcpad is not initialized");
227 return GST_FLOW_NOT_NEGOTIATED;
232 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
233 const gchar * stream_id)
235 GstPad *srcpad = NULL;
237 GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
238 if (demux->stream_id_pairs == NULL || stream_id == NULL) {
242 srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
245 GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
246 GST_DEBUG_PAD_NAME (srcpad));
254 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
257 GstStreamidDemux *demux;
258 const gchar *stream_id = NULL;
259 GstPad *active_srcpad = NULL;
261 demux = GST_STREAMID_DEMUX (parent);
263 GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
264 GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
266 if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
267 gst_event_parse_stream_start (event, &stream_id);
271 GST_OBJECT_LOCK (demux);
273 gst_streamid_demux_get_srcpad_by_stream_id (demux, stream_id);
274 if (!active_srcpad) {
275 /* try to generate a srcpad */
276 if (gst_streamid_demux_srcpad_create (demux, pad, stream_id)) {
277 GST_OBJECT_UNLOCK (demux);
279 gst_pad_set_active (demux->active_srcpad, TRUE);
280 /* Forward sticky events to the new srcpad */
281 gst_pad_sticky_events_foreach (demux->sinkpad, forward_sticky_events,
282 demux->active_srcpad);
283 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->active_srcpad);
285 GST_OBJECT_UNLOCK (demux);
286 goto fail_create_srcpad;
288 } else if (demux->active_srcpad != active_srcpad) {
289 demux->active_srcpad = active_srcpad;
290 GST_OBJECT_UNLOCK (demux);
292 g_object_notify (G_OBJECT (demux), "active-pad");
294 GST_OBJECT_UNLOCK (demux);
297 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START
298 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP
299 || GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
300 res = gst_pad_event_default (pad, parent, event);
301 } else if (demux->active_srcpad) {
302 GstPad *srcpad = NULL;
303 GST_OBJECT_LOCK (demux);
304 srcpad = gst_object_ref (demux->active_srcpad);
305 GST_OBJECT_UNLOCK (demux);
306 res = gst_pad_push_event (srcpad, event);
307 gst_object_unref (srcpad);
309 gst_event_unref (event);
316 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
317 ("Error occurred trying to get stream-id to create a srcpad"),
318 ("no stream-id found at %s", GST_EVENT_TYPE_NAME (event)));
320 gst_event_unref (event);
326 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
327 ("Error occurred trying to create a srcpad"),
328 ("Failed to create a srcpad via stream-id:%s", stream_id));
329 gst_event_unref (event);
335 gst_streamid_demux_release_srcpad (const GValue * item,
336 GstStreamidDemux * demux)
338 GstPad *pad = g_value_get_object (item);
341 gst_pad_set_active (pad, FALSE);
342 gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
347 gst_streamid_demux_reset (GstStreamidDemux * demux)
349 GstIterator *it = NULL;
350 GstIteratorResult itret = GST_ITERATOR_OK;
352 GST_OBJECT_LOCK (demux);
353 if (demux->active_srcpad != NULL)
354 demux->active_srcpad = NULL;
356 GST_OBJECT_UNLOCK (demux);
358 if (demux->stream_id_pairs != NULL) {
359 g_hash_table_unref (demux->stream_id_pairs);
360 demux->stream_id_pairs = NULL;
363 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
364 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
366 gst_iterator_foreach (it,
367 (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
368 if (itret == GST_ITERATOR_RESYNC)
369 gst_iterator_resync (it);
371 gst_iterator_free (it);
374 static GstStateChangeReturn
375 gst_streamid_demux_change_state (GstElement * element,
376 GstStateChange transition)
378 GstStreamidDemux *demux;
379 GstStateChangeReturn result;
381 demux = GST_STREAMID_DEMUX (element);
383 switch (transition) {
384 case GST_STATE_CHANGE_READY_TO_PAUSED:
390 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
392 switch (transition) {
393 case GST_STATE_CHANGE_PAUSED_TO_READY:
394 gst_streamid_demux_reset (demux);