tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / gst / encoding / gststreamcombiner.c
1 /* GStreamer Stream Combiner
2  * Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
3  *           (C) 2009 Nokia Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gststreamcombiner.h"
26 #include "gst/glib-compat-private.h"
27
28 static GstStaticPadTemplate src_template =
29 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
30     GST_STATIC_CAPS_ANY);
31
32 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%d",
33     GST_PAD_SINK,
34     GST_PAD_REQUEST,
35     GST_STATIC_CAPS_ANY);
36
37 GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
38 #define GST_CAT_DEFAULT gst_stream_combiner_debug
39
40 G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
41
42 #define STREAMS_LOCK(obj) (g_mutex_lock(obj->lock))
43 #define STREAMS_UNLOCK(obj) (g_mutex_unlock(obj->lock))
44
45 static void gst_stream_combiner_dispose (GObject * object);
46
47 static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
48     GstPadTemplate * templ, const gchar * name);
49 static void gst_stream_combiner_release_pad (GstElement * element,
50     GstPad * pad);
51
52 static void
53 gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
54 {
55   GObjectClass *gobject_klass;
56   GstElementClass *gstelement_klass;
57
58   gobject_klass = (GObjectClass *) klass;
59   gstelement_klass = (GstElementClass *) klass;
60
61   gobject_klass->dispose = gst_stream_combiner_dispose;
62
63   GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
64       "Stream Combiner");
65
66   gst_element_class_add_static_pad_template (gstelement_klass, &src_template);
67   gst_element_class_add_static_pad_template (gstelement_klass, &sink_template);
68
69   gstelement_klass->request_new_pad =
70       GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
71   gstelement_klass->release_pad =
72       GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
73
74   gst_element_class_set_details_simple (gstelement_klass,
75       "streamcombiner", "Generic",
76       "Recombines streams splitted by the streamsplitter element",
77       "Edward Hervey <edward.hervey@collabora.co.uk>");
78 }
79
80 static void
81 gst_stream_combiner_dispose (GObject * object)
82 {
83   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
84
85   if (stream_combiner->lock) {
86     g_mutex_free (stream_combiner->lock);
87     stream_combiner->lock = NULL;
88   }
89
90   G_OBJECT_CLASS (gst_stream_combiner_parent_class)->dispose (object);
91 }
92
93 static GstFlowReturn
94 gst_stream_combiner_chain (GstPad * pad, GstBuffer * buf)
95 {
96   GstStreamCombiner *stream_combiner =
97       (GstStreamCombiner *) GST_PAD_PARENT (pad);
98   /* FIXME : IMPLEMENT */
99
100   /* with lock taken, check if we're the active stream, if not drop */
101   return gst_pad_push (stream_combiner->srcpad, buf);
102 }
103
104 static gboolean
105 gst_stream_combiner_sink_event (GstPad * pad, GstEvent * event)
106 {
107   GstStreamCombiner *stream_combiner =
108       (GstStreamCombiner *) GST_PAD_PARENT (pad);
109   /* FIXME : IMPLEMENT */
110
111   GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
112
113   switch (GST_EVENT_TYPE (event)) {
114     case GST_EVENT_CUSTOM_DOWNSTREAM:
115       if (gst_event_has_name (event, "stream-switching-eos")) {
116         gst_event_unref (event);
117         event = gst_event_new_eos ();
118       }
119       break;
120     default:
121       break;
122   }
123
124   /* NEW_SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
125   /* EOS : lock, mark pad as unused, unlock , drop event */
126   /* CUSTOM_REAL_EOS : push EOS downstream */
127   /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
128   /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
129   /* OTHER : if selected pad forward */
130   return gst_pad_push_event (stream_combiner->srcpad, event);
131 }
132
133 static GstCaps *
134 gst_stream_combiner_sink_getcaps (GstPad * pad)
135 {
136   GstStreamCombiner *stream_combiner =
137       (GstStreamCombiner *) GST_PAD_PARENT (pad);
138
139   return gst_pad_peer_get_caps_reffed (stream_combiner->srcpad);
140 }
141
142 static gboolean
143 gst_stream_combiner_sink_setcaps (GstPad * pad, GstCaps * caps)
144 {
145   GstStreamCombiner *stream_combiner =
146       (GstStreamCombiner *) GST_PAD_PARENT (pad);
147   GstPad *peer;
148   gboolean res = FALSE;
149
150   GST_DEBUG_OBJECT (pad, "caps:%" GST_PTR_FORMAT, caps);
151
152   peer = gst_pad_get_peer (stream_combiner->srcpad);
153   if (peer) {
154     GST_DEBUG_OBJECT (peer, "Setting caps");
155     res = gst_pad_set_caps (peer, caps);
156     gst_object_unref (peer);
157   } else
158     GST_WARNING_OBJECT (stream_combiner, "sourcepad has no peer !");
159   return res;
160 }
161
162 static gboolean
163 gst_stream_combiner_src_event (GstPad * pad, GstEvent * event)
164 {
165   GstStreamCombiner *stream_combiner =
166       (GstStreamCombiner *) GST_PAD_PARENT (pad);
167   GstPad *sinkpad = NULL;
168
169   STREAMS_LOCK (stream_combiner);
170   if (stream_combiner->current)
171     sinkpad = stream_combiner->current;
172   else if (stream_combiner->sinkpads)
173     sinkpad = (GstPad *) stream_combiner->sinkpads->data;
174   STREAMS_UNLOCK (stream_combiner);
175
176   if (sinkpad)
177     /* Forward upstream as is */
178     return gst_pad_push_event (sinkpad, event);
179   return FALSE;
180 }
181
182 static gboolean
183 gst_stream_combiner_src_query (GstPad * pad, GstQuery * query)
184 {
185   GstStreamCombiner *stream_combiner =
186       (GstStreamCombiner *) GST_PAD_PARENT (pad);
187
188   GstPad *sinkpad = NULL;
189
190   STREAMS_LOCK (stream_combiner);
191   if (stream_combiner->current)
192     sinkpad = stream_combiner->current;
193   else if (stream_combiner->sinkpads)
194     sinkpad = (GstPad *) stream_combiner->sinkpads->data;
195   STREAMS_UNLOCK (stream_combiner);
196
197   if (sinkpad)
198     /* Forward upstream as is */
199     return gst_pad_peer_query (sinkpad, query);
200   return FALSE;
201 }
202
203 static void
204 gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
205 {
206   stream_combiner->srcpad =
207       gst_pad_new_from_static_template (&src_template, "src");
208   gst_pad_set_event_function (stream_combiner->srcpad,
209       gst_stream_combiner_src_event);
210   gst_pad_set_query_function (stream_combiner->srcpad,
211       gst_stream_combiner_src_query);
212   gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
213
214   stream_combiner->lock = g_mutex_new ();
215 }
216
217 static GstPad *
218 gst_stream_combiner_request_new_pad (GstElement * element,
219     GstPadTemplate * templ, const gchar * name)
220 {
221   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
222   GstPad *sinkpad;
223
224   GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
225
226   sinkpad = gst_pad_new_from_static_template (&sink_template, name);
227   /* FIXME : No buffer alloc for the time being, it will resort to the fallback */
228   /* gst_pad_set_bufferalloc_function (sinkpad, gst_stream_combiner_buffer_alloc); */
229   gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
230   gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
231   gst_pad_set_getcaps_function (sinkpad, gst_stream_combiner_sink_getcaps);
232   gst_pad_set_setcaps_function (sinkpad, gst_stream_combiner_sink_setcaps);
233
234   STREAMS_LOCK (stream_combiner);
235   stream_combiner->sinkpads =
236       g_list_append (stream_combiner->sinkpads, sinkpad);
237   gst_pad_set_active (sinkpad, TRUE);
238   gst_element_add_pad (element, sinkpad);
239   stream_combiner->cookie++;
240   STREAMS_UNLOCK (stream_combiner);
241
242   GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
243
244   return sinkpad;
245 }
246
247 static void
248 gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
249 {
250   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
251   GList *tmp;
252
253   GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
254
255   STREAMS_LOCK (stream_combiner);
256   tmp = g_list_find (stream_combiner->sinkpads, pad);
257   if (tmp) {
258     GstPad *pad = (GstPad *) tmp->data;
259
260     stream_combiner->sinkpads =
261         g_list_delete_link (stream_combiner->sinkpads, tmp);
262     stream_combiner->cookie++;
263
264     if (pad == stream_combiner->current) {
265       /* Deactivate current flow */
266       GST_DEBUG_OBJECT (element, "Removed pad was the current one");
267       stream_combiner->current = NULL;
268     }
269     GST_DEBUG_OBJECT (element, "Removing pad from ourself");
270     gst_element_remove_pad (element, pad);
271   }
272   STREAMS_UNLOCK (stream_combiner);
273
274   return;
275 }