Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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
27 static GstStaticPadTemplate src_template =
28 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
29     GST_STATIC_CAPS_ANY);
30
31 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%d",
32     GST_PAD_SINK,
33     GST_PAD_REQUEST,
34     GST_STATIC_CAPS_ANY);
35
36 GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
37 #define GST_CAT_DEFAULT gst_stream_combiner_debug
38
39 G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
40
41 #define STREAMS_LOCK(obj) (g_mutex_lock(obj->lock))
42 #define STREAMS_UNLOCK(obj) (g_mutex_unlock(obj->lock))
43
44 static void gst_stream_combiner_dispose (GObject * object);
45
46 static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
47     GstPadTemplate * templ, const gchar * name);
48 static void gst_stream_combiner_release_pad (GstElement * element,
49     GstPad * pad);
50
51 static void
52 gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
53 {
54   GObjectClass *gobject_klass;
55   GstElementClass *gstelement_klass;
56
57   gobject_klass = (GObjectClass *) klass;
58   gstelement_klass = (GstElementClass *) klass;
59
60   gobject_klass->dispose = gst_stream_combiner_dispose;
61
62   GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
63       "Stream Combiner");
64
65   gst_element_class_add_pad_template (gstelement_klass,
66       gst_static_pad_template_get (&src_template));
67   gst_element_class_add_pad_template (gstelement_klass,
68       gst_static_pad_template_get (&sink_template));
69
70   gstelement_klass->request_new_pad =
71       GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
72   gstelement_klass->release_pad =
73       GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
74
75   gst_element_class_set_details_simple (gstelement_klass,
76       "streamcombiner", "Generic",
77       "Recombines streams splitted by the streamsplitter element",
78       "Edward Hervey <edward.hervey@collabora.co.uk>");
79 }
80
81 static void
82 gst_stream_combiner_dispose (GObject * object)
83 {
84   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
85
86   if (stream_combiner->lock) {
87     g_mutex_free (stream_combiner->lock);
88     stream_combiner->lock = NULL;
89   }
90
91   G_OBJECT_CLASS (gst_stream_combiner_parent_class)->dispose (object);
92 }
93
94 static GstFlowReturn
95 gst_stream_combiner_chain (GstPad * pad, GstBuffer * buf)
96 {
97   GstStreamCombiner *stream_combiner =
98       (GstStreamCombiner *) GST_PAD_PARENT (pad);
99   /* FIXME : IMPLEMENT */
100
101   /* with lock taken, check if we're the active stream, if not drop */
102   return gst_pad_push (stream_combiner->srcpad, buf);
103 }
104
105 static gboolean
106 gst_stream_combiner_sink_event (GstPad * pad, GstEvent * event)
107 {
108   GstStreamCombiner *stream_combiner =
109       (GstStreamCombiner *) GST_PAD_PARENT (pad);
110   /* FIXME : IMPLEMENT */
111
112   GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
113
114   switch (GST_EVENT_TYPE (event)) {
115     case GST_EVENT_CUSTOM_DOWNSTREAM:
116       if (gst_event_has_name (event, "stream-switching-eos")) {
117         gst_event_unref (event);
118         event = gst_event_new_eos ();
119       }
120       break;
121     default:
122       break;
123   }
124
125   /* NEW_SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
126   /* EOS : lock, mark pad as unused, unlock , drop event */
127   /* CUSTOM_REAL_EOS : push EOS downstream */
128   /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
129   /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
130   /* OTHER : if selected pad forward */
131   return gst_pad_push_event (stream_combiner->srcpad, event);
132 }
133
134 static GstCaps *
135 gst_stream_combiner_sink_getcaps (GstPad * pad)
136 {
137   GstStreamCombiner *stream_combiner =
138       (GstStreamCombiner *) GST_PAD_PARENT (pad);
139
140   return gst_pad_peer_get_caps_reffed (stream_combiner->srcpad);
141 }
142
143 static gboolean
144 gst_stream_combiner_sink_setcaps (GstPad * pad, GstCaps * caps)
145 {
146   GstStreamCombiner *stream_combiner =
147       (GstStreamCombiner *) GST_PAD_PARENT (pad);
148   GstPad *peer;
149   gboolean res = FALSE;
150
151   GST_DEBUG_OBJECT (pad, "caps:%" GST_PTR_FORMAT, caps);
152
153   peer = gst_pad_get_peer (stream_combiner->srcpad);
154   if (peer) {
155     GST_DEBUG_OBJECT (peer, "Setting caps");
156     res = gst_pad_set_caps (peer, caps);
157     gst_object_unref (peer);
158   } else
159     GST_WARNING_OBJECT (stream_combiner, "sourcepad has no peer !");
160   return res;
161 }
162
163 static gboolean
164 gst_stream_combiner_src_event (GstPad * pad, GstEvent * event)
165 {
166   GstStreamCombiner *stream_combiner =
167       (GstStreamCombiner *) GST_PAD_PARENT (pad);
168   GstPad *sinkpad = NULL;
169
170   STREAMS_LOCK (stream_combiner);
171   if (stream_combiner->current)
172     sinkpad = stream_combiner->current;
173   else if (stream_combiner->sinkpads)
174     sinkpad = (GstPad *) stream_combiner->sinkpads->data;
175   STREAMS_UNLOCK (stream_combiner);
176
177   if (sinkpad)
178     /* Forward upstream as is */
179     return gst_pad_push_event (sinkpad, event);
180   return FALSE;
181 }
182
183 static gboolean
184 gst_stream_combiner_src_query (GstPad * pad, GstQuery * query)
185 {
186   GstStreamCombiner *stream_combiner =
187       (GstStreamCombiner *) GST_PAD_PARENT (pad);
188
189   GstPad *sinkpad = NULL;
190
191   STREAMS_LOCK (stream_combiner);
192   if (stream_combiner->current)
193     sinkpad = stream_combiner->current;
194   else if (stream_combiner->sinkpads)
195     sinkpad = (GstPad *) stream_combiner->sinkpads->data;
196   STREAMS_UNLOCK (stream_combiner);
197
198   if (sinkpad)
199     /* Forward upstream as is */
200     return gst_pad_peer_query (sinkpad, query);
201   return FALSE;
202 }
203
204 static void
205 gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
206 {
207   stream_combiner->srcpad =
208       gst_pad_new_from_static_template (&src_template, "src");
209   gst_pad_set_event_function (stream_combiner->srcpad,
210       gst_stream_combiner_src_event);
211   gst_pad_set_query_function (stream_combiner->srcpad,
212       gst_stream_combiner_src_query);
213   gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
214
215   stream_combiner->lock = g_mutex_new ();
216 }
217
218 static GstPad *
219 gst_stream_combiner_request_new_pad (GstElement * element,
220     GstPadTemplate * templ, const gchar * name)
221 {
222   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
223   GstPad *sinkpad;
224
225   GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
226
227   sinkpad = gst_pad_new_from_static_template (&sink_template, name);
228   /* FIXME : No buffer alloc for the time being, it will resort to the fallback */
229   /* gst_pad_set_bufferalloc_function (sinkpad, gst_stream_combiner_buffer_alloc); */
230   gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
231   gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
232   gst_pad_set_getcaps_function (sinkpad, gst_stream_combiner_sink_getcaps);
233   gst_pad_set_setcaps_function (sinkpad, gst_stream_combiner_sink_setcaps);
234
235   STREAMS_LOCK (stream_combiner);
236   stream_combiner->sinkpads =
237       g_list_append (stream_combiner->sinkpads, sinkpad);
238   gst_pad_set_active (sinkpad, TRUE);
239   gst_element_add_pad (element, sinkpad);
240   stream_combiner->cookie++;
241   STREAMS_UNLOCK (stream_combiner);
242
243   GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
244
245   return sinkpad;
246 }
247
248 static void
249 gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
250 {
251   GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
252   GList *tmp;
253
254   GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
255
256   STREAMS_LOCK (stream_combiner);
257   tmp = g_list_find (stream_combiner->sinkpads, pad);
258   if (tmp) {
259     GstPad *pad = (GstPad *) tmp->data;
260
261     stream_combiner->sinkpads =
262         g_list_delete_link (stream_combiner->sinkpads, tmp);
263     stream_combiner->cookie++;
264
265     if (pad == stream_combiner->current) {
266       /* Deactivate current flow */
267       GST_DEBUG_OBJECT (element, "Removed pad was the current one");
268       stream_combiner->current = NULL;
269     }
270     GST_DEBUG_OBJECT (element, "Removing pad from ourself");
271     gst_element_remove_pad (element, pad);
272   }
273   STREAMS_UNLOCK (stream_combiner);
274
275   return;
276 }