1 /* GStreamer streamiddemux element
2 *
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>
7 *
8 * gststreamiddemux.c: Simple stream-id-demultiplexer element
9 *
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.
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 * Lesser General Public License for more details.
19 *
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
23 */
24
25 /**
26 * SECTION:element-streamiddemux
27 * @title: streamiddemux
28 *
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.
32 *
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.
36 *
37 * #GstStreamidDemux:active-pad provides information about which output pad
38 * is activated at the moment.
39 *
40 * @see_also: #GstFunnel, #gst_event_new_stream_start
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <string.h>
48
49 #include "gststreamiddemux.h"
50 #include "gstcoreelementselements.h"
51
52 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
53 #define GST_CAT_DEFAULT streamid_demux_debug
54
55 enum
56 {
57 PROP_0,
58 PROP_ACTIVE_PAD,
59 PROP_LAST
60 };
61
62 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
63 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_PAD_SINK,
65 GST_PAD_ALWAYS,
66 GST_STATIC_CAPS_ANY);
67
68 static GstStaticPadTemplate gst_streamid_demux_src_factory =
69 GST_STATIC_PAD_TEMPLATE ("src_%u",
70 GST_PAD_SRC,
71 GST_PAD_SOMETIMES,
72 GST_STATIC_CAPS_ANY);
73
74 #define _do_init \
75 GST_DEBUG_CATEGORY_INIT (streamid_demux_debug, \
76 "streamiddemux", 0, "Streamid demuxer");
77 #define gst_streamid_demux_parent_class parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstStreamidDemux, gst_streamid_demux,
79 GST_TYPE_ELEMENT, _do_init);
80 GST_ELEMENT_REGISTER_DEFINE (streamiddemux, "streamiddemux", GST_RANK_PRIMARY,
81 GST_TYPE_STREAMID_DEMUX);
82
83 static void gst_streamid_demux_dispose (GObject * object);
84 static void gst_streamid_demux_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static GstFlowReturn gst_streamid_demux_chain (GstPad * pad,
87 GstObject * parent, GstBuffer * buf);
88 static gboolean gst_streamid_demux_event (GstPad * pad, GstObject * parent,
89 GstEvent * event);
90 static GstStateChangeReturn gst_streamid_demux_change_state (GstElement *
91 element, GstStateChange transition);
92 static GstPad *gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux *
93 demux, const gchar * stream_id);
94 static gboolean gst_streamid_demux_srcpad_create (GstStreamidDemux * demux,
95 GstPad * pad, const gchar * stream_id);
96 static void gst_streamid_demux_reset (GstStreamidDemux * demux);
97 static void gst_streamid_demux_release_srcpad (const GValue * item,
98 GstStreamidDemux * demux);
99
100 static void
gst_streamid_demux_class_init(GstStreamidDemuxClass * klass)101 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
102 {
103 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
104 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
105
106 gobject_class->get_property = gst_streamid_demux_get_property;
107 gobject_class->dispose = gst_streamid_demux_dispose;
108
109 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
110 g_param_spec_object ("active-pad", "Active pad",
111 "The currently active src pad", GST_TYPE_PAD,
112 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
113
114 gst_element_class_set_static_metadata (gstelement_class, "Streamid Demux",
115 "Generic", "1-to-N output stream by stream-id",
116 "HoonHee Lee <hoonhee.lee@lge.com>");
117 gst_element_class_add_static_pad_template (gstelement_class,
118 &gst_streamid_demux_sink_factory);
119
120 gst_element_class_add_static_pad_template (gstelement_class,
121 &gst_streamid_demux_src_factory);
122
123 gstelement_class->change_state = gst_streamid_demux_change_state;
124 }
125
126 static void
gst_streamid_demux_init(GstStreamidDemux * demux)127 gst_streamid_demux_init (GstStreamidDemux * demux)
128 {
129 demux->sinkpad =
130 gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
131 "sink");
132 gst_pad_set_chain_function (demux->sinkpad,
133 GST_DEBUG_FUNCPTR (gst_streamid_demux_chain));
134 gst_pad_set_event_function (demux->sinkpad,
135 GST_DEBUG_FUNCPTR (gst_streamid_demux_event));
136
137 gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
138
139 /* srcpad management */
140 demux->active_srcpad = NULL;
141 demux->nb_srcpads = 0;
142
143 /* initialize hash table for srcpad */
144 demux->stream_id_pairs =
145 g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free,
146 (GDestroyNotify) gst_object_unref);
147 }
148
149 static void
gst_streamid_demux_dispose(GObject * object)150 gst_streamid_demux_dispose (GObject * object)
151 {
152 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
153
154 gst_streamid_demux_reset (demux);
155
156 G_OBJECT_CLASS (parent_class)->dispose (object);
157 }
158
159 static void
gst_streamid_demux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)160 gst_streamid_demux_get_property (GObject * object, guint prop_id,
161 GValue * value, GParamSpec * pspec)
162 {
163 GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
164
165 switch (prop_id) {
166 case PROP_ACTIVE_PAD:
167 GST_OBJECT_LOCK (demux);
168 g_value_set_object (value, demux->active_srcpad);
169 GST_OBJECT_UNLOCK (demux);
170 break;
171 default:
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 break;
174 }
175 }
176
177 static gboolean
forward_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)178 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
179 {
180 GstPad *srcpad = GST_PAD_CAST (user_data);
181
182 gst_pad_push_event (srcpad, gst_event_ref (*event));
183
184 return TRUE;
185 }
186
187 static gboolean
gst_streamid_demux_srcpad_create(GstStreamidDemux * demux,GstPad * pad,const gchar * stream_id)188 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
189 const gchar * stream_id)
190 {
191 gchar *padname = NULL;
192 GstPad *srcpad = NULL;
193 GstPadTemplate *pad_tmpl = NULL;
194
195 padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
196 pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
197
198 GST_LOG_OBJECT (demux, "generating a srcpad:%s", padname);
199 srcpad = gst_pad_new_from_template (pad_tmpl, padname);
200 gst_object_unref (pad_tmpl);
201 g_free (padname);
202 g_return_val_if_fail (srcpad != NULL, FALSE);
203
204 demux->active_srcpad = srcpad;
205 g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
206 gst_object_ref (srcpad));
207
208 return TRUE;
209 }
210
211 static GstFlowReturn
gst_streamid_demux_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)212 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
213 {
214 GstFlowReturn res = GST_FLOW_OK;
215 GstStreamidDemux *demux = NULL;
216 GstPad *srcpad = NULL;
217
218 demux = GST_STREAMID_DEMUX (parent);
219
220 GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
221 demux->active_srcpad);
222
223 GST_OBJECT_LOCK (demux);
224 if (demux->active_srcpad) {
225 srcpad = gst_object_ref (demux->active_srcpad);
226 GST_OBJECT_UNLOCK (demux);
227 res = gst_pad_push (srcpad, buf);
228 gst_object_unref (srcpad);
229 } else {
230 GST_OBJECT_UNLOCK (demux);
231 goto no_active_srcpad;
232 }
233
234 GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
235 return res;
236
237 /* ERROR */
238 no_active_srcpad:
239 {
240 GST_WARNING_OBJECT (demux, "srcpad is not initialized");
241 return GST_FLOW_NOT_NEGOTIATED;
242 }
243 }
244
245 static GstPad *
gst_streamid_demux_get_srcpad_by_stream_id(GstStreamidDemux * demux,const gchar * stream_id)246 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
247 const gchar * stream_id)
248 {
249 GstPad *srcpad = NULL;
250
251 GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
252 if (demux->stream_id_pairs == NULL || stream_id == NULL) {
253 goto done;
254 }
255
256 srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
257
258 if (srcpad) {
259 GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
260 GST_DEBUG_PAD_NAME (srcpad));
261 }
262
263 done:
264 return srcpad;
265 }
266
267 static gboolean
gst_streamid_demux_event(GstPad * pad,GstObject * parent,GstEvent * event)268 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
269 {
270 gboolean res = TRUE;
271 GstStreamidDemux *demux;
272 const gchar *stream_id = NULL;
273 GstPad *active_srcpad = NULL;
274
275 demux = GST_STREAMID_DEMUX (parent);
276
277 GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
278 GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
279
280 if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
281 gst_event_parse_stream_start (event, &stream_id);
282 if (!stream_id)
283 goto no_stream_id;
284
285 GST_OBJECT_LOCK (demux);
286 active_srcpad =
287 gst_streamid_demux_get_srcpad_by_stream_id (demux, stream_id);
288 if (!active_srcpad) {
289 /* try to generate a srcpad */
290 if (gst_streamid_demux_srcpad_create (demux, pad, stream_id)) {
291 GST_OBJECT_UNLOCK (demux);
292
293 gst_pad_set_active (demux->active_srcpad, TRUE);
294 /* Forward sticky events to the new srcpad */
295 gst_pad_sticky_events_foreach (demux->sinkpad, forward_sticky_events,
296 demux->active_srcpad);
297 gst_element_add_pad (GST_ELEMENT_CAST (demux), demux->active_srcpad);
298 } else {
299 GST_OBJECT_UNLOCK (demux);
300 goto fail_create_srcpad;
301 }
302 } else if (demux->active_srcpad != active_srcpad) {
303 demux->active_srcpad = active_srcpad;
304 GST_OBJECT_UNLOCK (demux);
305
306 g_object_notify (G_OBJECT (demux), "active-pad");
307 } else
308 GST_OBJECT_UNLOCK (demux);
309 }
310
311 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START
312 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP
313 || GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
314 res = gst_pad_event_default (pad, parent, event);
315 } else if (demux->active_srcpad) {
316 GstPad *srcpad = NULL;
317 GST_OBJECT_LOCK (demux);
318 srcpad = gst_object_ref (demux->active_srcpad);
319 GST_OBJECT_UNLOCK (demux);
320 res = gst_pad_push_event (srcpad, event);
321 gst_object_unref (srcpad);
322 } else {
323 gst_event_unref (event);
324 }
325 return res;
326
327 /* ERRORS */
328 no_stream_id:
329 {
330 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
331 ("Error occurred trying to get stream-id to create a srcpad"),
332 ("no stream-id found at %s", GST_EVENT_TYPE_NAME (event)));
333
334 gst_event_unref (event);
335 return FALSE;
336 }
337
338 fail_create_srcpad:
339 {
340 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
341 ("Error occurred trying to create a srcpad"),
342 ("Failed to create a srcpad via stream-id:%s", stream_id));
343 gst_event_unref (event);
344 return FALSE;
345 }
346 }
347
348 static void
gst_streamid_demux_release_srcpad(const GValue * item,GstStreamidDemux * demux)349 gst_streamid_demux_release_srcpad (const GValue * item,
350 GstStreamidDemux * demux)
351 {
352 GstPad *pad = g_value_get_object (item);
353
354 if (pad != NULL) {
355 gst_pad_set_active (pad, FALSE);
356 gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
357 }
358 }
359
360 static void
gst_streamid_demux_reset(GstStreamidDemux * demux)361 gst_streamid_demux_reset (GstStreamidDemux * demux)
362 {
363 GstIterator *it = NULL;
364 GstIteratorResult itret = GST_ITERATOR_OK;
365
366 GST_OBJECT_LOCK (demux);
367 if (demux->active_srcpad != NULL)
368 demux->active_srcpad = NULL;
369
370 demux->nb_srcpads = 0;
371 GST_OBJECT_UNLOCK (demux);
372
373 if (demux->stream_id_pairs != NULL) {
374 g_hash_table_unref (demux->stream_id_pairs);
375 demux->stream_id_pairs = NULL;
376 }
377
378 it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
379 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
380 itret =
381 gst_iterator_foreach (it,
382 (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
383 if (itret == GST_ITERATOR_RESYNC)
384 gst_iterator_resync (it);
385 }
386 gst_iterator_free (it);
387 }
388
389 static GstStateChangeReturn
gst_streamid_demux_change_state(GstElement * element,GstStateChange transition)390 gst_streamid_demux_change_state (GstElement * element,
391 GstStateChange transition)
392 {
393 GstStreamidDemux *demux;
394 GstStateChangeReturn result;
395
396 demux = GST_STREAMID_DEMUX (element);
397
398 switch (transition) {
399 case GST_STATE_CHANGE_READY_TO_PAUSED:
400 break;
401 default:
402 break;
403 }
404
405 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
406
407 switch (transition) {
408 case GST_STATE_CHANGE_PAUSED_TO_READY:
409 gst_streamid_demux_reset (demux);
410 break;
411 default:
412 break;
413 }
414
415 return result;
416 }
417