Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / gconf / gstgconfvideosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /**
20  * SECTION:element-gconfvideosink
21  *
22  * This element outputs video to the videosink that has been configured in
23  * GConf by the user.
24  * 
25  * <refsect2>
26  * <title>Example launch line</title>
27  * |[
28  * gst-launch filesrc location=foo.ogg ! decodebin ! ffmpegcolorspace ! gconfvideosink
29  * ]| Play on configured videosink
30  * </refsect2>
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <string.h>
38
39 #include "gstgconfelements.h"
40 #include "gstgconfvideosink.h"
41
42 static void gst_gconf_video_sink_dispose (GObject * object);
43 static void gst_gconf_video_sink_finalize (GstGConfVideoSink * sink);
44 static void cb_toggle_element (GConfClient * client,
45     guint connection_id, GConfEntry * entry, gpointer data);
46 static GstStateChangeReturn
47 gst_gconf_video_sink_change_state (GstElement * element,
48     GstStateChange transition);
49
50 GST_BOILERPLATE (GstGConfVideoSink, gst_gconf_video_sink, GstSwitchSink,
51     GST_TYPE_SWITCH_SINK);
52
53 static void
54 gst_gconf_video_sink_base_init (gpointer klass)
55 {
56   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
57
58   gst_element_class_set_details_simple (eklass, "GConf video sink",
59       "Sink/Video",
60       "Video sink embedding the GConf-settings for video output",
61       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
62 }
63
64 static void
65 gst_gconf_video_sink_class_init (GstGConfVideoSinkClass * klass)
66 {
67   GObjectClass *oklass = G_OBJECT_CLASS (klass);
68   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
69
70   oklass->dispose = gst_gconf_video_sink_dispose;
71   oklass->finalize = (GObjectFinalizeFunc) gst_gconf_video_sink_finalize;
72   eklass->change_state = gst_gconf_video_sink_change_state;
73 }
74
75 /*
76  * Hack to make negotiation work.
77  */
78
79 static void
80 gst_gconf_video_sink_reset (GstGConfVideoSink * sink)
81 {
82   gst_switch_sink_set_child (GST_SWITCH_SINK (sink), NULL);
83
84   g_free (sink->gconf_str);
85   sink->gconf_str = NULL;
86 }
87
88 static void
89 gst_gconf_video_sink_init (GstGConfVideoSink * sink,
90     GstGConfVideoSinkClass * g_class)
91 {
92   gst_gconf_video_sink_reset (sink);
93
94   sink->client = gconf_client_get_default ();
95   gconf_client_add_dir (sink->client, GST_GCONF_DIR,
96       GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
97   sink->notify_id = gconf_client_notify_add (sink->client,
98       GST_GCONF_DIR "/" GST_GCONF_VIDEOSINK_KEY,
99       cb_toggle_element, sink, NULL, NULL);
100 }
101
102 static void
103 gst_gconf_video_sink_dispose (GObject * object)
104 {
105   GstGConfVideoSink *sink = GST_GCONF_VIDEO_SINK (object);
106
107   if (sink->client) {
108     if (sink->notify_id != 0)
109       gconf_client_notify_remove (sink->client, sink->notify_id);
110
111     g_object_unref (G_OBJECT (sink->client));
112     sink->client = NULL;
113   }
114
115   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
116 }
117
118 static void
119 gst_gconf_video_sink_finalize (GstGConfVideoSink * sink)
120 {
121   g_free (sink->gconf_str);
122
123   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, ((GObject *) (sink)));
124 }
125
126 static gboolean
127 do_change_child (GstGConfVideoSink * sink)
128 {
129   gchar *new_gconf_str;
130   GstElement *new_kid;
131
132   new_gconf_str = gst_gconf_get_string (GST_GCONF_VIDEOSINK_KEY);
133
134   GST_LOG_OBJECT (sink, "old gconf string: %s", GST_STR_NULL (sink->gconf_str));
135   GST_LOG_OBJECT (sink, "new gconf string: %s", GST_STR_NULL (new_gconf_str));
136
137   if (new_gconf_str != NULL && sink->gconf_str != NULL &&
138       (strlen (new_gconf_str) == 0 ||
139           strcmp (sink->gconf_str, new_gconf_str) == 0)) {
140     g_free (new_gconf_str);
141     GST_DEBUG_OBJECT (sink,
142         "GConf key was updated, but it didn't change. Ignoring");
143     return TRUE;
144   }
145
146   GST_DEBUG_OBJECT (sink, "GConf key changed: '%s' to '%s'",
147       GST_STR_NULL (sink->gconf_str), GST_STR_NULL (new_gconf_str));
148
149   GST_DEBUG_OBJECT (sink, "Creating new kid");
150   if (!(new_kid = gst_gconf_get_default_video_sink ())) {
151     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
152         ("Failed to render video sink from GConf"));
153     return FALSE;
154   }
155
156   if (!gst_switch_sink_set_child (GST_SWITCH_SINK (sink), new_kid)) {
157     GST_WARNING_OBJECT (sink, "Failed to update child element");
158     goto fail;
159   }
160
161   g_free (sink->gconf_str);
162   sink->gconf_str = new_gconf_str;
163
164   GST_DEBUG_OBJECT (sink, "done changing gconf video sink");
165
166   return TRUE;
167
168 fail:
169   g_free (new_gconf_str);
170   return FALSE;
171 }
172
173 static void
174 cb_toggle_element (GConfClient * client,
175     guint connection_id, GConfEntry * entry, gpointer data)
176 {
177   do_change_child (GST_GCONF_VIDEO_SINK (data));
178 }
179
180 static GstStateChangeReturn
181 gst_gconf_video_sink_change_state (GstElement * element,
182     GstStateChange transition)
183 {
184   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
185   GstGConfVideoSink *sink = GST_GCONF_VIDEO_SINK (element);
186
187   switch (transition) {
188     case GST_STATE_CHANGE_NULL_TO_READY:
189       if (!do_change_child (sink)) {
190         gst_gconf_video_sink_reset (sink);
191         return GST_STATE_CHANGE_FAILURE;
192       }
193       break;
194     default:
195       break;
196   }
197
198   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
199       (element, transition), GST_STATE_CHANGE_SUCCESS);
200
201   switch (transition) {
202     case GST_STATE_CHANGE_READY_TO_NULL:
203       gst_gconf_video_sink_reset (sink);
204       break;
205     default:
206       break;
207   }
208
209   return ret;
210 }