streamiddemux: Reset pad counter after removing all pads
[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-streamid-demux
27  * @see_also: #GstFunnel
28  *
29  * Direct input stream to one out of N output pads by stream-id.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <string.h>
37
38 #include "gststreamiddemux.h"
39
40 GST_DEBUG_CATEGORY_STATIC (streamid_demux_debug);
41 #define GST_CAT_DEFAULT streamid_demux_debug
42
43 enum
44 {
45   PROP_0,
46   PROP_ACTIVE_PAD,
47   PROP_LAST
48 };
49
50 static GstStaticPadTemplate gst_streamid_demux_sink_factory =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 static GstStaticPadTemplate gst_streamid_demux_src_factory =
57 GST_STATIC_PAD_TEMPLATE ("src_%u",
58     GST_PAD_SRC,
59     GST_PAD_SOMETIMES,
60     GST_STATIC_CAPS_ANY);
61
62 #define _do_init \
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);
68
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,
75     GstEvent * event);
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);
85
86 static void
87 gst_streamid_demux_class_init (GstStreamidDemuxClass * klass)
88 {
89   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
91
92   gobject_class->get_property = gst_streamid_demux_get_property;
93   gobject_class->dispose = gst_streamid_demux_dispose;
94
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));
99
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));
105
106   gst_element_class_add_pad_template (gstelement_class,
107       gst_static_pad_template_get (&gst_streamid_demux_src_factory));
108
109   gstelement_class->change_state = gst_streamid_demux_change_state;
110 }
111
112 static void
113 gst_streamid_demux_init (GstStreamidDemux * demux)
114 {
115   demux->sinkpad =
116       gst_pad_new_from_static_template (&gst_streamid_demux_sink_factory,
117       "sink");
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));
122
123   gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
124
125   /* srcpad management */
126   demux->active_srcpad = NULL;
127   demux->nb_srcpads = 0;
128
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);
133 }
134
135 static void
136 gst_streamid_demux_dispose (GObject * object)
137 {
138   GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
139
140   gst_streamid_demux_reset (demux);
141
142   G_OBJECT_CLASS (parent_class)->dispose (object);
143 }
144
145 static void
146 gst_streamid_demux_get_property (GObject * object, guint prop_id,
147     GValue * value, GParamSpec * pspec)
148 {
149   GstStreamidDemux *demux = GST_STREAMID_DEMUX (object);
150
151   switch (prop_id) {
152     case PROP_ACTIVE_PAD:
153       GST_OBJECT_LOCK (demux);
154       g_value_set_object (value, demux->active_srcpad);
155       GST_OBJECT_UNLOCK (demux);
156       break;
157     default:
158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159       break;
160   }
161 }
162
163 static gboolean
164 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
165 {
166   GstPad *srcpad = GST_PAD_CAST (user_data);
167
168   gst_pad_push_event (srcpad, gst_event_ref (*event));
169
170   return TRUE;
171 }
172
173 static gboolean
174 gst_streamid_demux_srcpad_create (GstStreamidDemux * demux, GstPad * pad,
175     const gchar * stream_id)
176 {
177   gchar *padname = NULL;
178   GstPad *srcpad = NULL;
179   GstPadTemplate *pad_tmpl = NULL;
180
181   padname = g_strdup_printf ("src_%u", demux->nb_srcpads++);
182   pad_tmpl = gst_static_pad_template_get (&gst_streamid_demux_src_factory);
183
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);
187   g_free (padname);
188   g_return_val_if_fail (srcpad != NULL, FALSE);
189
190   demux->active_srcpad = srcpad;
191   g_hash_table_insert (demux->stream_id_pairs, g_strdup (stream_id),
192       gst_object_ref (srcpad));
193
194   return TRUE;
195 }
196
197 static GstFlowReturn
198 gst_streamid_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
199 {
200   GstFlowReturn res = GST_FLOW_OK;
201   GstStreamidDemux *demux = NULL;
202   GstPad *srcpad = NULL;
203
204   demux = GST_STREAMID_DEMUX (parent);
205
206   GST_LOG_OBJECT (demux, "pushing buffer to %" GST_PTR_FORMAT,
207       demux->active_srcpad);
208
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);
215   } else {
216     GST_OBJECT_UNLOCK (demux);
217     goto no_active_srcpad;
218   }
219
220   GST_LOG_OBJECT (demux, "handled buffer %s", gst_flow_get_name (res));
221   return res;
222
223 /* ERROR */
224 no_active_srcpad:
225   {
226     GST_WARNING_OBJECT (demux, "srcpad is not initialized");
227     return GST_FLOW_NOT_NEGOTIATED;
228   }
229 }
230
231 static GstPad *
232 gst_streamid_demux_get_srcpad_by_stream_id (GstStreamidDemux * demux,
233     const gchar * stream_id)
234 {
235   GstPad *srcpad = NULL;
236
237   GST_DEBUG_OBJECT (demux, "stream_id = %s", stream_id);
238   if (demux->stream_id_pairs == NULL || stream_id == NULL) {
239     goto done;
240   }
241
242   srcpad = g_hash_table_lookup (demux->stream_id_pairs, stream_id);
243
244   if (srcpad) {
245     GST_DEBUG_OBJECT (demux, "srcpad = %s:%s matched",
246         GST_DEBUG_PAD_NAME (srcpad));
247   }
248
249 done:
250   return srcpad;
251 }
252
253 static gboolean
254 gst_streamid_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
255 {
256   gboolean res = TRUE;
257   GstStreamidDemux *demux;
258   const gchar *stream_id = NULL;
259   GstPad *active_srcpad = NULL;
260
261   demux = GST_STREAMID_DEMUX (parent);
262
263   GST_DEBUG_OBJECT (demux, "event = %s, sticky = %d",
264       GST_EVENT_TYPE_NAME (event), GST_EVENT_IS_STICKY (event));
265
266   if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
267     gst_event_parse_stream_start (event, &stream_id);
268     if (!stream_id)
269       goto no_stream_id;
270
271     GST_OBJECT_LOCK (demux);
272     active_srcpad =
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);
278
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);
284       } else {
285         GST_OBJECT_UNLOCK (demux);
286         goto fail_create_srcpad;
287       }
288     } else if (demux->active_srcpad != active_srcpad) {
289       demux->active_srcpad = active_srcpad;
290       GST_OBJECT_UNLOCK (demux);
291
292       g_object_notify (G_OBJECT (demux), "active-pad");
293     } else
294       GST_OBJECT_UNLOCK (demux);
295   }
296
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);
308   } else {
309     gst_event_unref (event);
310   }
311   return res;
312
313   /* ERRORS */
314 no_stream_id:
315   {
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)));
319
320     gst_event_unref (event);
321     return FALSE;
322   }
323
324 fail_create_srcpad:
325   {
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);
330     return FALSE;
331   }
332 }
333
334 static void
335 gst_streamid_demux_release_srcpad (const GValue * item,
336     GstStreamidDemux * demux)
337 {
338   GstPad *pad = g_value_get_object (item);
339
340   if (pad != NULL) {
341     gst_pad_set_active (pad, FALSE);
342     gst_element_remove_pad (GST_ELEMENT_CAST (demux), pad);
343   }
344 }
345
346 static void
347 gst_streamid_demux_reset (GstStreamidDemux * demux)
348 {
349   GstIterator *it = NULL;
350   GstIteratorResult itret = GST_ITERATOR_OK;
351
352   GST_OBJECT_LOCK (demux);
353   if (demux->active_srcpad != NULL)
354     demux->active_srcpad = NULL;
355
356   demux->nb_srcpads = 0;
357   GST_OBJECT_UNLOCK (demux);
358
359   if (demux->stream_id_pairs != NULL) {
360     g_hash_table_unref (demux->stream_id_pairs);
361     demux->stream_id_pairs = NULL;
362   }
363
364   it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (demux));
365   while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
366     itret =
367         gst_iterator_foreach (it,
368         (GstIteratorForeachFunction) gst_streamid_demux_release_srcpad, demux);
369     if (itret == GST_ITERATOR_RESYNC)
370       gst_iterator_resync (it);
371   }
372   gst_iterator_free (it);
373 }
374
375 static GstStateChangeReturn
376 gst_streamid_demux_change_state (GstElement * element,
377     GstStateChange transition)
378 {
379   GstStreamidDemux *demux;
380   GstStateChangeReturn result;
381
382   demux = GST_STREAMID_DEMUX (element);
383
384   switch (transition) {
385     case GST_STATE_CHANGE_READY_TO_PAUSED:
386       break;
387     default:
388       break;
389   }
390
391   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
392
393   switch (transition) {
394     case GST_STATE_CHANGE_PAUSED_TO_READY:
395       gst_streamid_demux_reset (demux);
396       break;
397     default:
398       break;
399   }
400
401   return result;
402 }