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