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_static_pad_template (eklass, &sink_template);
72 gst_switch_sink_reset (GstSwitchSink * sink)
74 /* this will install fakesink if no other child has been set,
75 * otherwise we rely on the subclass to know when to unset its
77 if (sink->kid == NULL) {
78 return gst_switch_sink_set_child (sink, NULL);
85 gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
87 GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
88 GstPadTemplate *templ;
90 templ = gst_element_class_get_pad_template (eklass, "sink");
91 sink->pad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
92 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
94 gst_switch_sink_reset (sink);
96 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
100 gst_switch_sink_dispose (GObject * object)
102 GstSwitchSink *sink = GST_SWITCH_SINK (object);
103 GstObject *new_kid, *kid;
105 GST_OBJECT_LOCK (sink);
106 new_kid = GST_OBJECT_CAST (sink->new_kid);
107 sink->new_kid = NULL;
109 kid = GST_OBJECT_CAST (sink->kid);
111 GST_OBJECT_UNLOCK (sink);
113 gst_object_replace (&new_kid, NULL);
114 gst_object_replace (&kid, NULL);
116 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
120 gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
124 GstElement *new_kid, *old_kid;
125 gboolean is_fakesink = FALSE;
128 /* need locking around member accesses */
129 GST_OBJECT_LOCK (sink);
130 /* If we're currently changing state, set the child to the next state
131 * we're transitioning too, rather than our current state which is
133 if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
134 kid_state = GST_STATE_NEXT (sink);
136 kid_state = GST_STATE (sink);
138 new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
139 sink->new_kid = NULL;
140 GST_OBJECT_UNLOCK (sink);
142 /* Fakesink by default if NULL is passed as the new child */
143 if (new_kid == NULL) {
144 GST_DEBUG_OBJECT (sink, "Replacing kid with fakesink");
145 new_kid = gst_element_factory_make ("fakesink", "testsink");
146 if (new_kid == NULL) {
147 GST_ERROR_OBJECT (sink, "Failed to create fakesink");
150 /* Add a reference, as it would if the element came from sink->new_kid */
151 gst_object_ref (new_kid);
152 g_object_set (new_kid, "sync", TRUE, NULL);
155 GST_DEBUG_OBJECT (sink, "Setting new kid");
158 /* set temporary bus of our own to catch error messages from the child
159 * (could we just set our own bus on it, or would the state change messages
160 * from the not-yet-added element confuse the state change algorithm? Let's
161 * play it safe for now) */
162 bus = gst_bus_new ();
163 gst_element_set_bus (new_kid, bus);
164 gst_object_unref (bus);
166 if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
169 /* check if child posted an error message and if so re-post it on our bus
170 * so that the application gets to see a decent error and not our generic
171 * fallback error message which is completely indecipherable to the user */
172 msg = gst_bus_pop_filtered (GST_ELEMENT_BUS (new_kid), GST_MESSAGE_ERROR);
174 GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
175 gst_element_post_message (GST_ELEMENT (sink), msg);
177 GST_ELEMENT_ERROR (sink, CORE, STATE_CHANGE, (NULL),
178 ("Failed to set state on new child."));
179 gst_element_set_bus (new_kid, NULL);
180 gst_object_unref (new_kid);
183 gst_element_set_bus (new_kid, NULL);
184 gst_bin_add (GST_BIN (sink), new_kid);
186 /* Now, replace the existing child */
187 GST_OBJECT_LOCK (sink);
190 /* Mark whether a custom kid or fakesink has been installed */
191 sink->have_kid = !is_fakesink;
192 GST_OBJECT_UNLOCK (sink);
194 /* kill old element */
196 GST_DEBUG_OBJECT (sink, "Removing old kid %" GST_PTR_FORMAT, old_kid);
197 gst_element_set_state (old_kid, GST_STATE_NULL);
198 gst_bin_remove (GST_BIN (sink), old_kid);
199 gst_object_unref (old_kid);
200 /* Don't lose the SINK flag */
201 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
204 /* re-attach ghostpad */
205 GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
206 targetpad = gst_element_get_static_pad (sink->kid, "sink");
207 gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
208 gst_object_unref (targetpad);
209 GST_DEBUG_OBJECT (sink, "done changing child of switchsink");
211 /* FIXME: Push new-segment info and pre-roll buffer(s) into the kid */
217 gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
222 /* Nothing to do if clearing the child and we've already installed fakesink */
223 if (new_kid == NULL && sink->kid != NULL && sink->have_kid == FALSE)
226 /* Store the new kid to be committed later */
227 GST_OBJECT_LOCK (sink);
228 cur = GST_STATE (sink);
229 next = GST_STATE_NEXT (sink);
230 p_kid = &sink->new_kid;
231 gst_object_replace ((GstObject **) p_kid, (GstObject *) new_kid);
232 GST_OBJECT_UNLOCK (sink);
234 gst_object_unref (new_kid);
236 /* Sometime, it would be lovely to allow sink changes even when
237 * already running, but this involves sending an appropriate new-segment
238 * and possibly prerolling etc */
239 /* FIXME: Block the pad and replace the kid when it completes */
240 if (cur > GST_STATE_READY || next == GST_STATE_PAUSED) {
241 GST_DEBUG_OBJECT (sink,
242 "Switch-sink is already running. Ignoring change of child.");
243 gst_object_unref (new_kid);
247 return gst_switch_sink_commit_new_kid (sink);
250 static GstStateChangeReturn
251 gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
253 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
254 GstSwitchSink *sink = GST_SWITCH_SINK (element);
256 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
257 (element, transition), GST_STATE_CHANGE_SUCCESS);
259 switch (transition) {
260 case GST_STATE_CHANGE_READY_TO_NULL:
261 if (!gst_switch_sink_reset (sink))
262 ret = GST_STATE_CHANGE_FAILURE;