upload tizen1.0 source
[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_pad_template (eklass,
68         gst_static_pad_template_get (&sink_template));
69   }
70 }
71
72 static gboolean
73 gst_switch_sink_reset (GstSwitchSink * sink)
74 {
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
77    * custom kid */
78   if (sink->kid == NULL) {
79     return gst_switch_sink_set_child (sink, NULL);
80   }
81
82   return TRUE;
83 }
84
85 static void
86 gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
87 {
88   GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
89   GstPadTemplate *templ;
90
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);
94
95   gst_switch_sink_reset (sink);
96
97   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
98 }
99
100 static void
101 gst_switch_sink_dispose (GObject * object)
102 {
103   GstSwitchSink *sink = GST_SWITCH_SINK (object);
104   GstObject *new_kid, *kid;
105
106   GST_OBJECT_LOCK (sink);
107   new_kid = GST_OBJECT_CAST (sink->new_kid);
108   sink->new_kid = NULL;
109
110   kid = GST_OBJECT_CAST (sink->kid);
111   sink->kid = NULL;
112   GST_OBJECT_UNLOCK (sink);
113
114   gst_object_replace (&new_kid, NULL);
115   gst_object_replace (&kid, NULL);
116
117   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
118 }
119
120 static gboolean
121 gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
122 {
123   GstPad *targetpad;
124   GstState kid_state;
125   GstElement *new_kid, *old_kid;
126   gboolean is_fakesink = FALSE;
127   GstBus *bus;
128
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 
133    * about to change */
134   if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
135     kid_state = GST_STATE_NEXT (sink);
136   else
137     kid_state = GST_STATE (sink);
138
139   new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
140   sink->new_kid = NULL;
141   GST_OBJECT_UNLOCK (sink);
142
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");
149       return FALSE;
150     }
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);
154     is_fakesink = TRUE;
155   } else {
156     GST_DEBUG_OBJECT (sink, "Setting new kid");
157   }
158
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);
166
167   if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
168     GstMessage *msg;
169
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);
174     if (msg) {
175       GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
176       gst_element_post_message (GST_ELEMENT (sink), msg);
177     }
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);
182     return FALSE;
183   }
184   gst_element_set_bus (new_kid, NULL);
185   gst_bin_add (GST_BIN (sink), new_kid);
186
187   /* Now, replace the existing child */
188   GST_OBJECT_LOCK (sink);
189   old_kid = sink->kid;
190   sink->kid = new_kid;
191   /* Mark whether a custom kid or fakesink has been installed */
192   sink->have_kid = !is_fakesink;
193   GST_OBJECT_UNLOCK (sink);
194
195   /* kill old element */
196   if (old_kid) {
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);
203   }
204
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");
211
212   /* FIXME: Push new-segment info and pre-roll buffer(s) into the kid */
213
214   return TRUE;
215 }
216
217 gboolean
218 gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
219 {
220   GstState cur, next;
221   GstElement **p_kid;
222
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)
225     return TRUE;
226
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);
234   if (new_kid)
235     gst_object_unref (new_kid);
236
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);
245     return TRUE;
246   }
247
248   return gst_switch_sink_commit_new_kid (sink);
249 }
250
251 static GstStateChangeReturn
252 gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
253 {
254   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
255   GstSwitchSink *sink = GST_SWITCH_SINK (element);
256
257   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
258       (element, transition), GST_STATE_CHANGE_SUCCESS);
259
260   switch (transition) {
261     case GST_STATE_CHANGE_READY_TO_NULL:
262       if (!gst_switch_sink_reset (sink))
263         ret = GST_STATE_CHANGE_FAILURE;
264       break;
265     default:
266       break;
267   }
268
269   return ret;
270 }