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
27 * @title: streamiddemux
29 * The basic concept was started from de-funneling element which restores one
30 * serialized stream via #GstFunnel to its original state. #GstStreamidDemux
31 * classifies each stream base on stream ids.
33 * The stream id demux always takes one input and checks how many streams
34 * are contained in a stream by STREAM_START event. Likewise #GstFunnel,
35 * #GstStreamidDemux does not synchronize the different output streams.
37 * #GstStreamidDemux:active-pad provides information about which output pad
38 * is activated at the moment.
40 * @see_also: #GstFunnel, #gst_event_new_stream_start
49 #include "gststreamiddemux.h"
51 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
52 #define GST_CAT_DEFAULT streamid_demux_debug
61 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
62 GST_STATIC_PAD_TEMPLATE ("sink",
67 static GstStaticPadTemplate gst_streamid_demux_src_factory =
68 GST_STATIC_PAD_TEMPLATE ("src_%u",
74 GST_DEBUG_CATEGORY_INIT (streamid_demux_debug, \
75 "streamiddemux", 0, "Streamid demuxer");
76 #define gst_streamid_demux_parent_class parent_class
77 G_DEFINE_TYPE_WITH_CODE (GstStreamidDemux, gst_streamid_demux,
78 GST_TYPE_ELEMENT, _do_init);
80 static void gst_streamid_demux_dispose (GObject * object);
81 static void gst_streamid_demux_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static GstFlowReturn gst_streamid_demux_chain (GstPad * pad,
84 GstObject * parent, GstBuffer * buf);
85 static gboolean gst_streamid_demux_event (GstPad * pad, GstObject * parent,
87 static GstStateChangeReturn gst_streamid_demux_change_state (GstElement *
88 element, GstStateChange transition);
89 static GstPad *gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux *
90 demux, const gchar * stream_id);
91 static gboolean gst_streamid_demux_srcpad_create (GstStreamidDemux * demux,
92 GstPad * pad, const gchar * stream_id);
93 static void gst_streamid_demux_reset (GstStreamidDemux * demux);
94 static void gst_streamid_demux_release_srcpad (const GValue * item,
95 GstStreamidDemux * demux);
98 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
103 gobject_class->get_property = gst_streamid_demux_get_property;
104 gobject_class->dispose = gst_streamid_demux_dispose;
106 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
107 g_param_spec_object ("active-pad", "Active pad",
108 "The currently active src pad", GST_TYPE_PAD,
109 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
111 gst_element_class_set_static_metadata (gstelement_class, "Streamid Demux",
112 "Generic", "1-to-N output stream by stream-id",
113 "HoonHee Lee <hoonhee.lee@lge.com>");
114 gst_element_class_add_static_pad_template (gstelement_class,
115 &gst_streamid_demux_sink_factory);
117 gst_element_class_add_static_pad_template (gstelement_class,
118 &gst_streamid_demux_src_factory);
120 gstelement_class->change_state = gst_streamid_demux_change_state;
124 gst_streamid_demux_init (GstStreamidDemux * demux)
127 gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
129 gst_pad_set_chain_function (demux->sinkpad,
130 GST_DEBUG_FUNCPTR (gst_streamid_demux_chain));
131 gst_pad_set_event_function (demux->sinkpad,
132 GST_DEBUG_FUNCPTR (gst_streamid_demux_event));
134 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
136 /* srcpad management */
137 demux->active_srcpad = NULL;
138 demux->nb_srcpads = 0;
140 /* initialize hash table for srcpad */
141 demux->stream_id_pairs =
142 g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free,
143 (GDestroyNotify) gst_object_unref);
147 gst_streamid_demux_dispose (GObject * object)
149 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
151 gst_streamid_demux_reset (demux);
153 G_OBJECT_CLASS (parent_class)->dispose (object);
157 gst_streamid_demux_get_property (GObject * object, guint prop_id,
158 GValue * value, GParamSpec * pspec)
160 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
163 case PROP_ACTIVE_PAD:
164 GST_OBJECT_LOCK (demux);
165 g_value_set_object (value, demux->active_srcpad);
166 GST_OBJECT_UNLOCK (demux);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
177 GstPad *srcpad = GST_PAD_CAST (user_data);
179 gst_pad_push_event (srcpad, gst_event_ref (*event));
185 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
186 const gchar * stream_id)
188 gchar *padname = NULL;
189 GstPad *srcpad = NULL;
190 GstPadTemplate *pad_tmpl = NULL;
192 padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
193 pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
195 GST_LOG_OBJECT (demux, "generating a srcpad:%s", padname);
196 srcpad = gst_pad_new_from_template (pad_tmpl, padname);
197 gst_object_unref (pad_tmpl);
199 g_return_val_if_fail (srcpad != NULL, FALSE);
201 demux->active_srcpad = srcpad;
202 g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
203 gst_object_ref (srcpad));
209 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
211 GstFlowReturn res = GST_FLOW_OK;
212 GstStreamidDemux *demux = NULL;
213 GstPad *srcpad = NULL;
215 demux = GST_STREAMID_DEMUX (parent);
217 GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
218 demux->active_srcpad);
220 GST_OBJECT_LOCK (demux);
221 if (demux->active_srcpad) {
222 srcpad = gst_object_ref (demux->active_srcpad);
223 GST_OBJECT_UNLOCK (demux);
224 res = gst_pad_push (srcpad, buf);
225 gst_object_unref (srcpad);
227 GST_OBJECT_UNLOCK (demux);
228 goto no_active_srcpad;
231 GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
237 GST_WARNING_OBJECT (demux, "srcpad is not initialized");
238 return GST_FLOW_NOT_NEGOTIATED;
243 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
244 const gchar * stream_id)
246 GstPad *srcpad = NULL;
248 GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
249 if (demux->stream_id_pairs == NULL || stream_id == NULL) {
253 srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
256 GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
257 GST_DEBUG_PAD_NAME (srcpad));
265 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
268 GstStreamidDemux *demux;
269 const gchar *stream_id = NULL;
270 GstPad *active_srcpad = NULL;
272 demux = GST_STREAMID_DEMUX (parent);
274 GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
275 GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
277 if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
278 gst_event_parse_stream_start (event, &stream_id);
282 GST_OBJECT_LOCK (demux);
284 gst_streamid_demux_get_srcpad_by_stream_id (demux, stream_id);
285 if (!active_srcpad) {
286 /* try to generate a srcpad */
287 if (gst_streamid_demux_srcpad_create (demux, pad, stream_id)) {
288 GST_OBJECT_UNLOCK (demux);
290 gst_pad_set_active (demux->active_srcpad, TRUE);
291 /* Forward sticky events to the new srcpad */
292 gst_pad_sticky_events_foreach (demux->sinkpad, forward_sticky_events,
293 demux->active_srcpad);
294 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->active_srcpad);
296 GST_OBJECT_UNLOCK (demux);
297 goto fail_create_srcpad;
299 } else if (demux->active_srcpad != active_srcpad) {
300 demux->active_srcpad = active_srcpad;
301 GST_OBJECT_UNLOCK (demux);
303 g_object_notify (G_OBJECT (demux), "active-pad");
305 GST_OBJECT_UNLOCK (demux);
308 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START
309 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP
310 || GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
311 res = gst_pad_event_default (pad, parent, event);
312 } else if (demux->active_srcpad) {
313 GstPad *srcpad = NULL;
314 GST_OBJECT_LOCK (demux);
315 srcpad = gst_object_ref (demux->active_srcpad);
316 GST_OBJECT_UNLOCK (demux);
317 res = gst_pad_push_event (srcpad, event);
318 gst_object_unref (srcpad);
320 gst_event_unref (event);
327 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
328 ("Error occurred trying to get stream-id to create a srcpad"),
329 ("no stream-id found at %s", GST_EVENT_TYPE_NAME (event)));
331 gst_event_unref (event);
337 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
338 ("Error occurred trying to create a srcpad"),
339 ("Failed to create a srcpad via stream-id:%s", stream_id));
340 gst_event_unref (event);
346 gst_streamid_demux_release_srcpad (const GValue * item,
347 GstStreamidDemux * demux)
349 GstPad *pad = g_value_get_object (item);
352 gst_pad_set_active (pad, FALSE);
353 gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
358 gst_streamid_demux_reset (GstStreamidDemux * demux)
360 GstIterator *it = NULL;
361 GstIteratorResult itret = GST_ITERATOR_OK;
363 GST_OBJECT_LOCK (demux);
364 if (demux->active_srcpad != NULL)
365 demux->active_srcpad = NULL;
367 demux->nb_srcpads = 0;
368 GST_OBJECT_UNLOCK (demux);
370 if (demux->stream_id_pairs != NULL) {
371 g_hash_table_unref (demux->stream_id_pairs);
372 demux->stream_id_pairs = NULL;
375 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
376 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
378 gst_iterator_foreach (it,
379 (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
380 if (itret == GST_ITERATOR_RESYNC)
381 gst_iterator_resync (it);
383 gst_iterator_free (it);
386 static GstStateChangeReturn
387 gst_streamid_demux_change_state (GstElement * element,
388 GstStateChange transition)
390 GstStreamidDemux *demux;
391 GstStateChangeReturn result;
393 demux = GST_STREAMID_DEMUX (element);
395 switch (transition) {
396 case GST_STATE_CHANGE_READY_TO_PAUSED:
402 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
404 switch (transition) {
405 case GST_STATE_CHANGE_PAUSED_TO_READY:
406 gst_streamid_demux_reset (demux);