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>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
28 #include "gstswitchsink.h"
30 GST_DEBUG_CATEGORY_STATIC (switch_debug);
31 #define GST_CAT_DEFAULT switch_debug
33 static void gst_switch_sink_dispose (GObject * object);
34 static GstStateChangeReturn
35 gst_switch_sink_change_state (GstElement * element, GstStateChange transition);
42 GST_BOILERPLATE (GstSwitchSink, gst_switch_sink, GstBin, GST_TYPE_BIN);
45 gst_switch_sink_base_init (gpointer klass)
47 GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsink", 0, "switchsink element");
51 gst_switch_sink_class_init (GstSwitchSinkClass * klass)
53 GObjectClass *oklass = G_OBJECT_CLASS (klass);
54 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
55 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
59 GstPadTemplate *child_pad_templ;
61 oklass->dispose = gst_switch_sink_dispose;
62 eklass->change_state = gst_switch_sink_change_state;
64 /* Provide a default pad template if the child didn't */
65 child_pad_templ = gst_element_class_get_pad_template (eklass, "sink");
66 if (child_pad_templ == NULL) {
67 gst_element_class_add_pad_template (eklass,
68 gst_static_pad_template_get (&sink_template));
73 gst_switch_sink_reset (GstSwitchSink * sink)
75 /* this will install fakesink if no other child has been set,
76 * otherwise we rely on the subclass to know when to unset its
78 if (sink->kid == NULL) {
79 return gst_switch_sink_set_child (sink, NULL);
86 gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
88 GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
89 GstPadTemplate *templ;
91 templ = gst_element_class_get_pad_template (eklass, "sink");
92 sink->pad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
93 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
95 gst_switch_sink_reset (sink);
97 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
101 gst_switch_sink_dispose (GObject * object)
103 GstSwitchSink *sink = GST_SWITCH_SINK (object);
104 GstObject *new_kid, *kid;
106 GST_OBJECT_LOCK (sink);
107 new_kid = GST_OBJECT_CAST (sink->new_kid);
108 sink->new_kid = NULL;
110 kid = GST_OBJECT_CAST (sink->kid);
112 GST_OBJECT_UNLOCK (sink);
114 gst_object_replace (&new_kid, NULL);
115 gst_object_replace (&kid, NULL);
117 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
121 gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
125 GstElement *new_kid, *old_kid;
126 gboolean is_fakesink = FALSE;
129 /* need locking around member accesses */
130 GST_OBJECT_LOCK (sink);
131 /* If we're currently changing state, set the child to the next state
132 * we're transitioning too, rather than our current state which is
134 if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
135 kid_state = GST_STATE_NEXT (sink);
137 kid_state = GST_STATE (sink);
139 new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
140 sink->new_kid = NULL;
141 GST_OBJECT_UNLOCK (sink);
143 /* Fakesink by default if NULL is passed as the new child */
144 if (new_kid == NULL) {
145 GST_DEBUG_OBJECT (sink, "Replacing kid with fakesink");
146 new_kid = gst_element_factory_make ("fakesink", "testsink");
147 if (new_kid == NULL) {
148 GST_ERROR_OBJECT (sink, "Failed to create fakesink");
151 /* Add a reference, as it would if the element came from sink->new_kid */
152 gst_object_ref (new_kid);
153 g_object_set (new_kid, "sync", TRUE, NULL);
156 GST_DEBUG_OBJECT (sink, "Setting new kid");
159 /* set temporary bus of our own to catch error messages from the child
160 * (could we just set our own bus on it, or would the state change messages
161 * from the not-yet-added element confuse the state change algorithm? Let's
162 * play it safe for now) */
163 bus = gst_bus_new ();
164 gst_element_set_bus (new_kid, bus);
165 gst_object_unref (bus);
167 if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
170 /* check if child posted an error message and if so re-post it on our bus
171 * so that the application gets to see a decent error and not our generic
172 * fallback error message which is completely indecipherable to the user */
173 msg = gst_bus_pop_filtered (GST_ELEMENT_BUS (new_kid), GST_MESSAGE_ERROR);
175 GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
176 gst_element_post_message (GST_ELEMENT (sink), msg);
178 GST_ELEMENT_ERROR (sink, CORE, STATE_CHANGE, (NULL),
179 ("Failed to set state on new child."));
180 gst_element_set_bus (new_kid, NULL);
181 gst_object_unref (new_kid);
184 gst_element_set_bus (new_kid, NULL);
185 gst_bin_add (GST_BIN (sink), new_kid);
187 /* Now, replace the existing child */
188 GST_OBJECT_LOCK (sink);
191 /* Mark whether a custom kid or fakesink has been installed */
192 sink->have_kid = !is_fakesink;
193 GST_OBJECT_UNLOCK (sink);
195 /* kill old element */
197 GST_DEBUG_OBJECT (sink, "Removing old kid %" GST_PTR_FORMAT, old_kid);
198 gst_element_set_state (old_kid, GST_STATE_NULL);
199 gst_bin_remove (GST_BIN (sink), old_kid);
200 gst_object_unref (old_kid);
201 /* Don't lose the SINK flag */
202 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
205 /* re-attach ghostpad */
206 GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
207 targetpad = gst_element_get_static_pad (sink->kid, "sink");
208 gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
209 gst_object_unref (targetpad);
210 GST_DEBUG_OBJECT (sink, "done changing child of switchsink");
212 /* FIXME: Push new-segment info and pre-roll buffer(s) into the kid */
218 gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
223 /* Nothing to do if clearing the child and we've already installed fakesink */
224 if (new_kid == NULL && sink->kid != NULL && sink->have_kid == FALSE)
227 /* Store the new kid to be committed later */
228 GST_OBJECT_LOCK (sink);
229 cur = GST_STATE (sink);
230 next = GST_STATE_NEXT (sink);
231 p_kid = &sink->new_kid;
232 gst_object_replace ((GstObject **) p_kid, (GstObject *) new_kid);
233 GST_OBJECT_UNLOCK (sink);
235 gst_object_unref (new_kid);
237 /* Sometime, it would be lovely to allow sink changes even when
238 * already running, but this involves sending an appropriate new-segment
239 * and possibly prerolling etc */
240 /* FIXME: Block the pad and replace the kid when it completes */
241 if (cur > GST_STATE_READY || next == GST_STATE_PAUSED) {
242 GST_DEBUG_OBJECT (sink,
243 "Switch-sink is already running. Ignoring change of child.");
244 gst_object_unref (new_kid);
248 return gst_switch_sink_commit_new_kid (sink);
251 static GstStateChangeReturn
252 gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
254 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
255 GstSwitchSink *sink = GST_SWITCH_SINK (element);
257 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
258 (element, transition), GST_STATE_CHANGE_SUCCESS);
260 switch (transition) {
261 case GST_STATE_CHANGE_READY_TO_NULL:
262 if (!gst_switch_sink_reset (sink))
263 ret = GST_STATE_CHANGE_FAILURE;