Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / gconf / gstswitchsrc.c
1 /* GStreamer
2  * Copyright (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * Copyright (c) 2006 Jürg Billeter <j@bitron.ch>
4  * Copyright (c) 2007 Jan Schmidt <thaytan@noraisin.net>
5  * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include "gstswitchsrc.h"
30
31 GST_DEBUG_CATEGORY_STATIC (switch_debug);
32 #define GST_CAT_DEFAULT switch_debug
33
34 static void gst_switch_src_dispose (GObject * object);
35 static GstStateChangeReturn
36 gst_switch_src_change_state (GstElement * element, GstStateChange transition);
37
38 GST_BOILERPLATE (GstSwitchSrc, gst_switch_src, GstBin, GST_TYPE_BIN);
39
40 static void
41 gst_switch_src_base_init (gpointer klass)
42 {
43   GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsrc", 0, "switchsrc element");
44 }
45
46 static void
47 gst_switch_src_class_init (GstSwitchSrcClass * klass)
48 {
49   GObjectClass *oklass = G_OBJECT_CLASS (klass);
50   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
51   static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
52       GST_PAD_SRC,
53       GST_PAD_ALWAYS,
54       GST_STATIC_CAPS_ANY);
55   GstPadTemplate *child_pad_templ;
56
57   oklass->dispose = gst_switch_src_dispose;
58   eklass->change_state = gst_switch_src_change_state;
59
60   /* Provide a default pad template if the child didn't */
61   child_pad_templ = gst_element_class_get_pad_template (eklass, "src");
62   if (child_pad_templ == NULL) {
63     gst_element_class_add_static_pad_template (eklass, &src_template);
64   }
65 }
66
67 static gboolean
68 gst_switch_src_reset (GstSwitchSrc * src)
69 {
70   /* this will install fakesrc if no other child has been set,
71    * otherwise we rely on the subclass to know when to unset its
72    * custom kid */
73   if (src->kid == NULL) {
74     return gst_switch_src_set_child (src, NULL);
75   }
76
77   return TRUE;
78 }
79
80 static void
81 gst_switch_src_init (GstSwitchSrc * src, GstSwitchSrcClass * g_class)
82 {
83   GstElementClass *eklass = GST_ELEMENT_GET_CLASS (src);
84   GstPadTemplate *templ;
85
86   templ = gst_element_class_get_pad_template (eklass, "src");
87   src->pad = gst_ghost_pad_new_no_target_from_template ("src", templ);
88   gst_element_add_pad (GST_ELEMENT (src), src->pad);
89
90   gst_switch_src_reset (src);
91
92   GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
93 }
94
95 static void
96 gst_switch_src_dispose (GObject * object)
97 {
98   GstSwitchSrc *src = GST_SWITCH_SRC (object);
99   GstObject *new_kid, *kid;
100
101   GST_OBJECT_LOCK (src);
102   new_kid = GST_OBJECT_CAST (src->new_kid);
103   src->new_kid = NULL;
104
105   kid = GST_OBJECT_CAST (src->kid);
106   src->kid = NULL;
107   GST_OBJECT_UNLOCK (src);
108
109   gst_object_replace (&new_kid, NULL);
110   gst_object_replace (&kid, NULL);
111
112   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
113 }
114
115 static gboolean
116 gst_switch_src_commit_new_kid (GstSwitchSrc * src)
117 {
118   GstPad *targetpad;
119   GstState kid_state;
120   GstElement *new_kid, *old_kid;
121   gboolean is_fakesrc = FALSE;
122   GstBus *bus;
123
124   /* need locking around member accesses */
125   GST_OBJECT_LOCK (src);
126   /* If we're currently changing state, set the child to the next state
127    * we're transitioning too, rather than our current state which is 
128    * about to change */
129   if (GST_STATE_NEXT (src) != GST_STATE_VOID_PENDING)
130     kid_state = GST_STATE_NEXT (src);
131   else
132     kid_state = GST_STATE (src);
133
134   new_kid = src->new_kid ? gst_object_ref (src->new_kid) : NULL;
135   src->new_kid = NULL;
136   GST_OBJECT_UNLOCK (src);
137
138   /* Fakesrc by default if NULL is passed as the new child */
139   if (new_kid == NULL) {
140     GST_DEBUG_OBJECT (src, "Replacing kid with fakesrc");
141     new_kid = gst_element_factory_make ("fakesrc", "testsrc");
142     if (new_kid == NULL) {
143       GST_ERROR_OBJECT (src, "Failed to create fakesrc");
144       return FALSE;
145     }
146     /* Add a reference, as it would if the element came from src->new_kid */
147     gst_object_ref (new_kid);
148     is_fakesrc = TRUE;
149   } else {
150     GST_DEBUG_OBJECT (src, "Setting new kid");
151   }
152
153   /* set temporary bus of our own to catch error messages from the child
154    * (could we just set our own bus on it, or would the state change messages
155    * from the not-yet-added element confuse the state change algorithm? Let's
156    * play it safe for now) */
157   bus = gst_bus_new ();
158   gst_element_set_bus (new_kid, bus);
159   gst_object_unref (bus);
160
161   if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
162     GstMessage *msg;
163
164     /* check if child posted an error message and if so re-post it on our bus
165      * so that the application gets to see a decent error and not our generic
166      * fallback error message which is completely indecipherable to the user */
167     msg = gst_bus_pop_filtered (GST_ELEMENT_BUS (new_kid), GST_MESSAGE_ERROR);
168     if (msg) {
169       GST_INFO_OBJECT (src, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
170       gst_element_post_message (GST_ELEMENT (src), msg);
171     }
172     GST_ELEMENT_ERROR (src, CORE, STATE_CHANGE, (NULL),
173         ("Failed to set state on new child."));
174     gst_element_set_bus (new_kid, NULL);
175     gst_object_unref (new_kid);
176     return FALSE;
177   }
178   gst_element_set_bus (new_kid, NULL);
179   gst_bin_add (GST_BIN (src), new_kid);
180
181   /* Now, replace the existing child */
182   GST_OBJECT_LOCK (src);
183   old_kid = src->kid;
184   src->kid = new_kid;
185   /* Mark whether a custom kid or fakesrc has been installed */
186   src->have_kid = !is_fakesrc;
187   GST_OBJECT_UNLOCK (src);
188
189   /* kill old element */
190   if (old_kid) {
191     GST_DEBUG_OBJECT (src, "Removing old kid %" GST_PTR_FORMAT, old_kid);
192     gst_element_set_state (old_kid, GST_STATE_NULL);
193     gst_bin_remove (GST_BIN (src), old_kid);
194     gst_object_unref (old_kid);
195     /* Don't lose the SOURCE flag */
196     GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
197   }
198
199   /* re-attach ghostpad */
200   GST_DEBUG_OBJECT (src, "Creating new ghostpad");
201   targetpad = gst_element_get_static_pad (src->kid, "src");
202   gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad);
203   gst_object_unref (targetpad);
204   GST_DEBUG_OBJECT (src, "done changing child of switchsrc");
205
206   return TRUE;
207 }
208
209 gboolean
210 gst_switch_src_set_child (GstSwitchSrc * src, GstElement * new_kid)
211 {
212   GstState cur, next;
213   GstElement **p_kid;
214
215   /* Nothing to do if clearing the child and we've already installed fakesrc */
216   if (new_kid == NULL && src->kid != NULL && src->have_kid == FALSE)
217     return TRUE;
218
219   /* Store the new kid to be committed later */
220   GST_OBJECT_LOCK (src);
221   cur = GST_STATE (src);
222   next = GST_STATE_NEXT (src);
223   p_kid = &src->new_kid;
224   gst_object_replace ((GstObject **) p_kid, (GstObject *) new_kid);
225   GST_OBJECT_UNLOCK (src);
226   if (new_kid)
227     gst_object_unref (new_kid);
228
229   /* Sometime, it would be lovely to allow src changes even when
230    * already running */
231   /* FIXME: Block the pad and replace the kid when it completes */
232   if (cur > GST_STATE_READY || next == GST_STATE_PAUSED) {
233     GST_DEBUG_OBJECT (src,
234         "Switch-src is already running. Ignoring change of child.");
235     gst_object_unref (new_kid);
236     return TRUE;
237   }
238
239   return gst_switch_src_commit_new_kid (src);
240 }
241
242 static GstStateChangeReturn
243 gst_switch_src_change_state (GstElement * element, GstStateChange transition)
244 {
245   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
246   GstSwitchSrc *src = GST_SWITCH_SRC (element);
247
248   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
249       (element, transition), GST_STATE_CHANGE_SUCCESS);
250
251   switch (transition) {
252     case GST_STATE_CHANGE_READY_TO_NULL:
253       if (!gst_switch_src_reset (src))
254         ret = GST_STATE_CHANGE_FAILURE;
255       break;
256     default:
257       break;
258   }
259
260   return ret;
261 }