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>
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.
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.
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.
29 #include "gstswitchsrc.h"
31 GST_DEBUG_CATEGORY_STATIC (switch_debug);
32 #define GST_CAT_DEFAULT switch_debug
34 static void gst_switch_src_dispose (GObject * object);
35 static GstStateChangeReturn
36 gst_switch_src_change_state (GstElement * element, GstStateChange transition);
38 GST_BOILERPLATE (GstSwitchSrc, gst_switch_src, GstBin, GST_TYPE_BIN);
41 gst_switch_src_base_init (gpointer klass)
43 GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsrc", 0, "switchsrc element");
47 gst_switch_src_class_init (GstSwitchSrcClass * klass)
49 GObjectClass *oklass = G_OBJECT_CLASS (klass);
50 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
55 GstPadTemplate *child_pad_templ;
57 oklass->dispose = gst_switch_src_dispose;
58 eklass->change_state = gst_switch_src_change_state;
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);
68 gst_switch_src_reset (GstSwitchSrc * src)
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
73 if (src->kid == NULL) {
74 return gst_switch_src_set_child (src, NULL);
81 gst_switch_src_init (GstSwitchSrc * src, GstSwitchSrcClass * g_class)
83 GstElementClass *eklass = GST_ELEMENT_GET_CLASS (src);
84 GstPadTemplate *templ;
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);
90 gst_switch_src_reset (src);
92 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
96 gst_switch_src_dispose (GObject * object)
98 GstSwitchSrc *src = GST_SWITCH_SRC (object);
99 GstObject *new_kid, *kid;
101 GST_OBJECT_LOCK (src);
102 new_kid = GST_OBJECT_CAST (src->new_kid);
105 kid = GST_OBJECT_CAST (src->kid);
107 GST_OBJECT_UNLOCK (src);
109 gst_object_replace (&new_kid, NULL);
110 gst_object_replace (&kid, NULL);
112 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
116 gst_switch_src_commit_new_kid (GstSwitchSrc * src)
120 GstElement *new_kid, *old_kid;
121 gboolean is_fakesrc = FALSE;
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
129 if (GST_STATE_NEXT (src) != GST_STATE_VOID_PENDING)
130 kid_state = GST_STATE_NEXT (src);
132 kid_state = GST_STATE (src);
134 new_kid = src->new_kid ? gst_object_ref (src->new_kid) : NULL;
136 GST_OBJECT_UNLOCK (src);
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");
146 /* Add a reference, as it would if the element came from src->new_kid */
147 gst_object_ref (new_kid);
150 GST_DEBUG_OBJECT (src, "Setting new kid");
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);
161 if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
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);
169 GST_INFO_OBJECT (src, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
170 gst_element_post_message (GST_ELEMENT (src), msg);
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);
178 gst_element_set_bus (new_kid, NULL);
179 gst_bin_add (GST_BIN (src), new_kid);
181 /* Now, replace the existing child */
182 GST_OBJECT_LOCK (src);
185 /* Mark whether a custom kid or fakesrc has been installed */
186 src->have_kid = !is_fakesrc;
187 GST_OBJECT_UNLOCK (src);
189 /* kill old element */
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);
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");
210 gst_switch_src_set_child (GstSwitchSrc * src, GstElement * new_kid)
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)
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);
227 gst_object_unref (new_kid);
229 /* Sometime, it would be lovely to allow src changes even when
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);
239 return gst_switch_src_commit_new_kid (src);
242 static GstStateChangeReturn
243 gst_switch_src_change_state (GstElement * element, GstStateChange transition)
245 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
246 GstSwitchSrc *src = GST_SWITCH_SRC (element);
248 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
249 (element, transition), GST_STATE_CHANGE_SUCCESS);
251 switch (transition) {
252 case GST_STATE_CHANGE_READY_TO_NULL:
253 if (!gst_switch_src_reset (src))
254 ret = GST_STATE_CHANGE_FAILURE;