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-streamiddemux
28 * The basic concept was started from de-funneling element which restores one
29 * serialized stream via #GstFunnel to its original state. #GstStreamidDemux
30 * classifies each stream base on stream ids.
32 * The stream id demux always takes one input and checks how many streams
33 * are contained in a stream by STREAM_START event. Likewise #GstFunnel,
34 * #GstStreamidDemux does not synchronize the different output streams.
36 * #GstStreamidDemux:active-pad provides information about which output pad
37 * is activated at the moment.
39 * @see_also: #GstFunnel, #gst_event_new_stream_start
48 #include "gststreamiddemux.h"
50 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
51 #define GST_CAT_DEFAULT streamid_demux_debug
60 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
61 GST_STATIC_PAD_TEMPLATE ("sink",
66 static GstStaticPadTemplate gst_streamid_demux_src_factory =
67 GST_STATIC_PAD_TEMPLATE ("src_%u",
73 GST_DEBUG_CATEGORY_INIT (streamid_demux_debug, \
74 "streamiddemux", 0, "Streamid demuxer");
75 #define gst_streamid_demux_parent_class parent_class
76 G_DEFINE_TYPE_WITH_CODE (GstStreamidDemux, gst_streamid_demux,
77 GST_TYPE_ELEMENT, _do_init);
79 static void gst_streamid_demux_dispose (GObject * object);
80 static void gst_streamid_demux_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
82 static GstFlowReturn gst_streamid_demux_chain (GstPad * pad,
83 GstObject * parent, GstBuffer * buf);
84 static gboolean gst_streamid_demux_event (GstPad * pad, GstObject * parent,
86 static GstStateChangeReturn gst_streamid_demux_change_state (GstElement *
87 element, GstStateChange transition);
88 static GstPad *gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux *
89 demux, const gchar * stream_id);
90 static gboolean gst_streamid_demux_srcpad_create (GstStreamidDemux * demux,
91 GstPad * pad, const gchar * stream_id);
92 static void gst_streamid_demux_reset (GstStreamidDemux * demux);
93 static void gst_streamid_demux_release_srcpad (const GValue * item,
94 GstStreamidDemux * demux);
97 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
102 gobject_class->get_property = gst_streamid_demux_get_property;
103 gobject_class->dispose = gst_streamid_demux_dispose;
105 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
106 g_param_spec_object ("active-pad", "Active pad",
107 "The currently active src pad", GST_TYPE_PAD,
108 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
110 gst_element_class_set_static_metadata (gstelement_class, "Streamid Demux",
111 "Generic", "1-to-N output stream by stream-id",
112 "HoonHee Lee <hoonhee.lee@lge.com>");
113 gst_element_class_add_pad_template (gstelement_class,
114 gst_static_pad_template_get (&gst_streamid_demux_sink_factory));
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&gst_streamid_demux_src_factory));
119 gstelement_class->change_state = gst_streamid_demux_change_state;
123 gst_streamid_demux_init (GstStreamidDemux * demux)
126 gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
128 gst_pad_set_chain_function (demux->sinkpad,
129 GST_DEBUG_FUNCPTR (gst_streamid_demux_chain));
130 gst_pad_set_event_function (demux->sinkpad,
131 GST_DEBUG_FUNCPTR (gst_streamid_demux_event));
133 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
135 /* srcpad management */
136 demux->active_srcpad = NULL;
137 demux->nb_srcpads = 0;
139 /* initialize hash table for srcpad */
140 demux->stream_id_pairs =
141 g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free,
142 (GDestroyNotify) gst_object_unref);
146 gst_streamid_demux_dispose (GObject * object)
148 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
150 gst_streamid_demux_reset (demux);
152 G_OBJECT_CLASS (parent_class)->dispose (object);
156 gst_streamid_demux_get_property (GObject * object, guint prop_id,
157 GValue * value, GParamSpec * pspec)
159 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
162 case PROP_ACTIVE_PAD:
163 GST_OBJECT_LOCK (demux);
164 g_value_set_object (value, demux->active_srcpad);
165 GST_OBJECT_UNLOCK (demux);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
176 GstPad *srcpad = GST_PAD_CAST (user_data);
178 gst_pad_push_event (srcpad, gst_event_ref (*event));
184 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
185 const gchar * stream_id)
187 gchar *padname = NULL;
188 GstPad *srcpad = NULL;
189 GstPadTemplate *pad_tmpl = NULL;
191 padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
192 pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
194 GST_LOG_OBJECT (demux, "generating a srcpad:%s", padname);
195 srcpad = gst_pad_new_from_template (pad_tmpl, padname);
196 gst_object_unref (pad_tmpl);
198 g_return_val_if_fail (srcpad != NULL, FALSE);
200 demux->active_srcpad = srcpad;
201 g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
202 gst_object_ref (srcpad));
208 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
210 GstFlowReturn res = GST_FLOW_OK;
211 GstStreamidDemux *demux = NULL;
212 GstPad *srcpad = NULL;
214 demux = GST_STREAMID_DEMUX (parent);
216 GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
217 demux->active_srcpad);
219 GST_OBJECT_LOCK (demux);
220 if (demux->active_srcpad) {
221 srcpad = gst_object_ref (demux->active_srcpad);
222 GST_OBJECT_UNLOCK (demux);
223 res = gst_pad_push (srcpad, buf);
224 gst_object_unref (srcpad);
226 GST_OBJECT_UNLOCK (demux);
227 goto no_active_srcpad;
230 GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
236 GST_WARNING_OBJECT (demux, "srcpad is not initialized");
237 return GST_FLOW_NOT_NEGOTIATED;
242 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
243 const gchar * stream_id)
245 GstPad *srcpad = NULL;
247 GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
248 if (demux->stream_id_pairs == NULL || stream_id == NULL) {
252 srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
255 GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
256 GST_DEBUG_PAD_NAME (srcpad));
264 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
267 GstStreamidDemux *demux;
268 const gchar *stream_id = NULL;
269 GstPad *active_srcpad = NULL;
271 demux = GST_STREAMID_DEMUX (parent);
273 GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
274 GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
276 if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
277 gst_event_parse_stream_start (event, &stream_id);
281 GST_OBJECT_LOCK (demux);
283 gst_streamid_demux_get_srcpad_by_stream_id (demux, stream_id);
284 if (!active_srcpad) {
285 /* try to generate a srcpad */
286 if (gst_streamid_demux_srcpad_create (demux, pad, stream_id)) {
287 GST_OBJECT_UNLOCK (demux);
289 gst_pad_set_active (demux->active_srcpad, TRUE);
290 /* Forward sticky events to the new srcpad */
291 gst_pad_sticky_events_foreach (demux->sinkpad, forward_sticky_events,
292 demux->active_srcpad);
293 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->active_srcpad);
295 GST_OBJECT_UNLOCK (demux);
296 goto fail_create_srcpad;
298 } else if (demux->active_srcpad != active_srcpad) {
299 demux->active_srcpad = active_srcpad;
300 GST_OBJECT_UNLOCK (demux);
302 g_object_notify (G_OBJECT (demux), "active-pad");
304 GST_OBJECT_UNLOCK (demux);
307 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START
308 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP
309 || GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
310 res = gst_pad_event_default (pad, parent, event);
311 } else if (demux->active_srcpad) {
312 GstPad *srcpad = NULL;
313 GST_OBJECT_LOCK (demux);
314 srcpad = gst_object_ref (demux->active_srcpad);
315 GST_OBJECT_UNLOCK (demux);
316 res = gst_pad_push_event (srcpad, event);
317 gst_object_unref (srcpad);
319 gst_event_unref (event);
326 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
327 ("Error occurred trying to get stream-id to create a srcpad"),
328 ("no stream-id found at %s", GST_EVENT_TYPE_NAME (event)));
330 gst_event_unref (event);
336 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
337 ("Error occurred trying to create a srcpad"),
338 ("Failed to create a srcpad via stream-id:%s", stream_id));
339 gst_event_unref (event);
345 gst_streamid_demux_release_srcpad (const GValue * item,
346 GstStreamidDemux * demux)
348 GstPad *pad = g_value_get_object (item);
351 gst_pad_set_active (pad, FALSE);
352 gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
357 gst_streamid_demux_reset (GstStreamidDemux * demux)
359 GstIterator *it = NULL;
360 GstIteratorResult itret = GST_ITERATOR_OK;
362 GST_OBJECT_LOCK (demux);
363 if (demux->active_srcpad != NULL)
364 demux->active_srcpad = NULL;
366 demux->nb_srcpads = 0;
367 GST_OBJECT_UNLOCK (demux);
369 if (demux->stream_id_pairs != NULL) {
370 g_hash_table_unref (demux->stream_id_pairs);
371 demux->stream_id_pairs = NULL;
374 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
375 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
377 gst_iterator_foreach (it,
378 (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
379 if (itret == GST_ITERATOR_RESYNC)
380 gst_iterator_resync (it);
382 gst_iterator_free (it);
385 static GstStateChangeReturn
386 gst_streamid_demux_change_state (GstElement * element,
387 GstStateChange transition)
389 GstStreamidDemux *demux;
390 GstStateChangeReturn result;
392 demux = GST_STREAMID_DEMUX (element);
394 switch (transition) {
395 case GST_STATE_CHANGE_READY_TO_PAUSED:
401 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
403 switch (transition) {
404 case GST_STATE_CHANGE_PAUSED_TO_READY:
405 gst_streamid_demux_reset (demux);