Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / gconf / gstswitchsink.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  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <string.h>
27
28 #include "gstswitchsink.h"
29
30 GST_DEBUG_CATEGORY_STATIC (switch_debug);
31 #define GST_CAT_DEFAULT switch_debug
32
33 static void gst_switch_sink_dispose (GObject * object);
34 static GstStateChangeReturn
35 gst_switch_sink_change_state (GstElement * element, GstStateChange transition);
36
37 enum
38 {
39   PROP_0
40 };
41
42 GST_BOILERPLATE (GstSwitchSink, gst_switch_sink, GstBin, GST_TYPE_BIN);
43
44 static void
45 gst_switch_sink_base_init (gpointer klass)
46 {
47   GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsink", 0, "switchsink element");
48 }
49
50 static void
51 gst_switch_sink_class_init (GstSwitchSinkClass * klass)
52 {
53   GObjectClass *oklass = G_OBJECT_CLASS (klass);
54   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
55   static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
56       GST_PAD_SINK,
57       GST_PAD_ALWAYS,
58       GST_STATIC_CAPS_ANY);
59   GstPadTemplate *child_pad_templ;
60
61   oklass->dispose = gst_switch_sink_dispose;
62   eklass->change_state = gst_switch_sink_change_state;
63
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);
68   }
69 }
70
71 static gboolean
72 gst_switch_sink_reset (GstSwitchSink * sink)
73 {
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
76    * custom kid */
77   if (sink->kid == NULL) {
78     return gst_switch_sink_set_child (sink, NULL);
79   }
80
81   return TRUE;
82 }
83
84 static void
85 gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
86 {
87   GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
88   GstPadTemplate *templ;
89
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);
93
94   gst_switch_sink_reset (sink);
95
96   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
97 }
98
99 static void
100 gst_switch_sink_dispose (GObject * object)
101 {
102   GstSwitchSink *sink = GST_SWITCH_SINK (object);
103   GstObject *new_kid, *kid;
104
105   GST_OBJECT_LOCK (sink);
106   new_kid = GST_OBJECT_CAST (sink->new_kid);
107   sink->new_kid = NULL;
108
109   kid = GST_OBJECT_CAST (sink->kid);
110   sink->kid = NULL;
111   GST_OBJECT_UNLOCK (sink);
112
113   gst_object_replace (&new_kid, NULL);
114   gst_object_replace (&kid, NULL);
115
116   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
117 }
118
119 static gboolean
120 gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
121 {
122   GstPad *targetpad;
123   GstState kid_state;
124   GstElement *new_kid, *old_kid;
125   gboolean is_fakesink = FALSE;
126   GstBus *bus;
127
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 
132    * about to change */
133   if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
134     kid_state = GST_STATE_NEXT (sink);
135   else
136     kid_state = GST_STATE (sink);
137
138   new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
139   sink->new_kid = NULL;
140   GST_OBJECT_UNLOCK (sink);
141
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");
148       return FALSE;
149     }
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);
153     is_fakesink = TRUE;
154   } else {
155     GST_DEBUG_OBJECT (sink, "Setting new kid");
156   }
157
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);
165
166   if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
167     GstMessage *msg;
168
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);
173     if (msg) {
174       GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
175       gst_element_post_message (GST_ELEMENT (sink), msg);
176     }
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);
181     return FALSE;
182   }
183   gst_element_set_bus (new_kid, NULL);
184   gst_bin_add (GST_BIN (sink), new_kid);
185
186   /* Now, replace the existing child */
187   GST_OBJECT_LOCK (sink);
188   old_kid = sink->kid;
189   sink->kid = new_kid;
190   /* Mark whether a custom kid or fakesink has been installed */
191   sink->have_kid = !is_fakesink;
192   GST_OBJECT_UNLOCK (sink);
193
194   /* kill old element */
195   if (old_kid) {
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);
202   }
203
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");
210
211   /* FIXME: Push new-segment info and pre-roll buffer(s) into the kid */
212
213   return TRUE;
214 }
215
216 gboolean
217 gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
218 {
219   GstState cur, next;
220   GstElement **p_kid;
221
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)
224     return TRUE;
225
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);
233   if (new_kid)
234     gst_object_unref (new_kid);
235
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);
244     return TRUE;
245   }
246
247   return gst_switch_sink_commit_new_kid (sink);
248 }
249
250 static GstStateChangeReturn
251 gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
252 {
253   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
254   GstSwitchSink *sink = GST_SWITCH_SINK (element);
255
256   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
257       (element, transition), GST_STATE_CHANGE_SUCCESS);
258
259   switch (transition) {
260     case GST_STATE_CHANGE_READY_TO_NULL:
261       if (!gst_switch_sink_reset (sink))
262         ret = GST_STATE_CHANGE_FAILURE;
263       break;
264     default:
265       break;
266   }
267
268   return ret;
269 }