Port gtk-doc comments to their equivalent markdown syntax
[platform/upstream/gstreamer.git] / plugins / elements / gststreamiddemux.c
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
51 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
52 #define GST_CAT_DEFAULT streamid_demux_debug
53
54 enum
55 {
56   PROP_0,
57   PROP_ACTIVE_PAD,
58   PROP_LAST
59 };
60
61 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
62 GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS_ANY);
66
67 static GstStaticPadTemplate gst_streamid_demux_src_factory =
68 GST_STATIC_PAD_TEMPLATE ("src_%u",
69     GST_PAD_SRC,
70     GST_PAD_SOMETIMES,
71     GST_STATIC_CAPS_ANY);
72
73 #define _do_init \
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);
79
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,
86     GstEvent * event);
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);
96
97 static void
98 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
99 {
100   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
102
103   gobject_class->get_property = gst_streamid_demux_get_property;
104   gobject_class->dispose = gst_streamid_demux_dispose;
105
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));
110
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);
116
117   gst_element_class_add_static_pad_template (gstelement_class,
118       &gst_streamid_demux_src_factory);
119
120   gstelement_class->change_state = gst_streamid_demux_change_state;
121 }
122
123 static void
124 gst_streamid_demux_init (GstStreamidDemux * demux)
125 {
126   demux->sinkpad =
127       gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
128       "sink");
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));
133
134   gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
135
136   /* srcpad management */
137   demux->active_srcpad = NULL;
138   demux->nb_srcpads = 0;
139
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);
144 }
145
146 static void
147 gst_streamid_demux_dispose (GObject * object)
148 {
149   GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
150
151   gst_streamid_demux_reset (demux);
152
153   G_OBJECT_CLASS (parent_class)->dispose (object);
154 }
155
156 static void
157 gst_streamid_demux_get_property (GObject * object, guint prop_id,
158     GValue * value, GParamSpec * pspec)
159 {
160   GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
161
162   switch (prop_id) {
163     case PROP_ACTIVE_PAD:
164       GST_OBJECT_LOCK (demux);
165       g_value_set_object (value, demux->active_srcpad);
166       GST_OBJECT_UNLOCK (demux);
167       break;
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173
174 static gboolean
175 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
176 {
177   GstPad *srcpad = GST_PAD_CAST (user_data);
178
179   gst_pad_push_event (srcpad, gst_event_ref (*event));
180
181   return TRUE;
182 }
183
184 static gboolean
185 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
186     const gchar * stream_id)
187 {
188   gchar *padname = NULL;
189   GstPad *srcpad = NULL;
190   GstPadTemplate *pad_tmpl = NULL;
191
192   padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
193   pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
194
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);
198   g_free (padname);
199   g_return_val_if_fail (srcpad != NULL, FALSE);
200
201   demux->active_srcpad = srcpad;
202   g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
203       gst_object_ref (srcpad));
204
205   return TRUE;
206 }
207
208 static GstFlowReturn
209 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
210 {
211   GstFlowReturn res = GST_FLOW_OK;
212   GstStreamidDemux *demux = NULL;
213   GstPad *srcpad = NULL;
214
215   demux = GST_STREAMID_DEMUX (parent);
216
217   GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
218       demux->active_srcpad);
219
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);
226   } else {
227     GST_OBJECT_UNLOCK (demux);
228     goto no_active_srcpad;
229   }
230
231   GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
232   return res;
233
234 /* ERROR */
235 no_active_srcpad:
236   {
237     GST_WARNING_OBJECT (demux, "srcpad is not initialized");
238     return GST_FLOW_NOT_NEGOTIATED;
239   }
240 }
241
242 static GstPad *
243 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
244     const gchar * stream_id)
245 {
246   GstPad *srcpad = NULL;
247
248   GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
249   if (demux->stream_id_pairs == NULL || stream_id == NULL) {
250     goto done;
251   }
252
253   srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
254
255   if (srcpad) {
256     GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
257         GST_DEBUG_PAD_NAME (srcpad));
258   }
259
260 done:
261   return srcpad;
262 }
263
264 static gboolean
265 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
266 {
267   gboolean res = TRUE;
268   GstStreamidDemux *demux;
269   const gchar *stream_id = NULL;
270   GstPad *active_srcpad = NULL;
271
272   demux = GST_STREAMID_DEMUX (parent);
273
274   GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
275       GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
276
277   if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
278     gst_event_parse_stream_start (event, &stream_id);
279     if (!stream_id)
280       goto no_stream_id;
281
282     GST_OBJECT_LOCK (demux);
283     active_srcpad =
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);
289
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);
295       } else {
296         GST_OBJECT_UNLOCK (demux);
297         goto fail_create_srcpad;
298       }
299     } else if (demux->active_srcpad != active_srcpad) {
300       demux->active_srcpad = active_srcpad;
301       GST_OBJECT_UNLOCK (demux);
302
303       g_object_notify (G_OBJECT (demux), "active-pad");
304     } else
305       GST_OBJECT_UNLOCK (demux);
306   }
307
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);
319   } else {
320     gst_event_unref (event);
321   }
322   return res;
323
324   /* ERRORS */
325 no_stream_id:
326   {
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)));
330
331     gst_event_unref (event);
332     return FALSE;
333   }
334
335 fail_create_srcpad:
336   {
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);
341     return FALSE;
342   }
343 }
344
345 static void
346 gst_streamid_demux_release_srcpad (const GValue * item,
347     GstStreamidDemux * demux)
348 {
349   GstPad *pad = g_value_get_object (item);
350
351   if (pad != NULL) {
352     gst_pad_set_active (pad, FALSE);
353     gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
354   }
355 }
356
357 static void
358 gst_streamid_demux_reset (GstStreamidDemux * demux)
359 {
360   GstIterator *it = NULL;
361   GstIteratorResult itret = GST_ITERATOR_OK;
362
363   GST_OBJECT_LOCK (demux);
364   if (demux->active_srcpad != NULL)
365     demux->active_srcpad = NULL;
366
367   demux->nb_srcpads = 0;
368   GST_OBJECT_UNLOCK (demux);
369
370   if (demux->stream_id_pairs != NULL) {
371     g_hash_table_unref (demux->stream_id_pairs);
372     demux->stream_id_pairs = NULL;
373   }
374
375   it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
376   while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
377     itret =
378         gst_iterator_foreach (it,
379         (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
380     if (itret == GST_ITERATOR_RESYNC)
381       gst_iterator_resync (it);
382   }
383   gst_iterator_free (it);
384 }
385
386 static GstStateChangeReturn
387 gst_streamid_demux_change_state (GstElement * element,
388     GstStateChange transition)
389 {
390   GstStreamidDemux *demux;
391   GstStateChangeReturn result;
392
393   demux = GST_STREAMID_DEMUX (element);
394
395   switch (transition) {
396     case GST_STATE_CHANGE_READY_TO_PAUSED:
397       break;
398     default:
399       break;
400   }
401
402   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
403
404   switch (transition) {
405     case GST_STATE_CHANGE_PAUSED_TO_READY:
406       gst_streamid_demux_reset (demux);
407       break;
408     default:
409       break;
410   }
411
412   return result;
413 }