Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / gconf / gstgconfaudiosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Jürg Billeter <j@bitron.ch>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION:element-gconfaudiosink
22  *
23  * This element outputs sound to the audiosink that has been configured in
24  * GConf by the user.
25  * 
26  * <refsect2>
27  * <title>Example launch line</title>
28  * |[
29  * gst-launch filesrc location=foo.ogg ! decodebin ! audioconvert ! audioresample ! gconfaudiosink
30  * ]| Play on configured audiosink
31  * </refsect2>
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <string.h>
39
40 #include "gstgconfelements.h"
41 #include "gstgconfaudiosink.h"
42
43 static void gst_gconf_audio_sink_dispose (GObject * object);
44 static void gst_gconf_audio_sink_finalize (GstGConfAudioSink * sink);
45 static void cb_change_child (GConfClient * client,
46     guint connection_id, GConfEntry * entry, gpointer data);
47 static GstStateChangeReturn
48 gst_gconf_audio_sink_change_state (GstElement * element,
49     GstStateChange transition);
50 static void gst_gconf_switch_profile (GstGConfAudioSink * sink,
51     GstGConfProfile profile);
52
53 enum
54 {
55   PROP_0,
56   PROP_PROFILE
57 };
58
59 GST_BOILERPLATE (GstGConfAudioSink, gst_gconf_audio_sink, GstSwitchSink,
60     GST_TYPE_SWITCH_SINK);
61
62 static void gst_gconf_audio_sink_set_property (GObject * object, guint prop_id,
63     const GValue * value, GParamSpec * pspec);
64 static void gst_gconf_audio_sink_get_property (GObject * object, guint prop_id,
65     GValue * value, GParamSpec * pspec);
66
67 static void
68 gst_gconf_audio_sink_base_init (gpointer klass)
69 {
70   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
71
72   gst_element_class_set_details_simple (eklass, "GConf audio sink",
73       "Sink/Audio",
74       "Audio sink embedding the GConf-settings for audio output",
75       "Jan Schmidt <thaytan@mad.scientist.com>");
76 }
77
78 #define GST_TYPE_GCONF_PROFILE (gst_gconf_profile_get_type())
79 static GType
80 gst_gconf_profile_get_type (void)
81 {
82   static GType gconf_profile_type = 0;
83   static const GEnumValue gconf_profiles[] = {
84     {GCONF_PROFILE_SOUNDS, "Sound Events", "sounds"},
85     {GCONF_PROFILE_MUSIC, "Music and Movies", "music"},
86     {GCONF_PROFILE_CHAT, "Audio/Video Conferencing", "chat"},
87     {0, NULL, NULL}
88   };
89
90   if (!gconf_profile_type) {
91     gconf_profile_type =
92         g_enum_register_static ("GstGConfProfile", gconf_profiles);
93   }
94   return gconf_profile_type;
95 }
96
97 static void
98 gst_gconf_audio_sink_class_init (GstGConfAudioSinkClass * klass)
99 {
100   GObjectClass *oklass = G_OBJECT_CLASS (klass);
101   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
102
103   oklass->set_property = gst_gconf_audio_sink_set_property;
104   oklass->get_property = gst_gconf_audio_sink_get_property;
105   oklass->dispose = gst_gconf_audio_sink_dispose;
106   oklass->finalize = (GObjectFinalizeFunc) gst_gconf_audio_sink_finalize;
107   eklass->change_state = gst_gconf_audio_sink_change_state;
108
109   g_object_class_install_property (oklass, PROP_PROFILE,
110       g_param_spec_enum ("profile", "Profile", "Profile",
111           GST_TYPE_GCONF_PROFILE, GCONF_PROFILE_SOUNDS,
112           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113 }
114
115 static void
116 gst_gconf_audio_sink_reset (GstGConfAudioSink * sink)
117 {
118   gst_switch_sink_set_child (GST_SWITCH_SINK (sink), NULL);
119
120   g_free (sink->gconf_str);
121   sink->gconf_str = NULL;
122 }
123
124 static void
125 gst_gconf_audio_sink_init (GstGConfAudioSink * sink,
126     GstGConfAudioSinkClass * g_class)
127 {
128   gst_gconf_audio_sink_reset (sink);
129
130   sink->client = gconf_client_get_default ();
131   gconf_client_add_dir (sink->client, GST_GCONF_DIR "/default",
132       GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
133
134   gst_gconf_switch_profile (sink, GCONF_PROFILE_SOUNDS);
135 }
136
137 static void
138 gst_gconf_audio_sink_dispose (GObject * object)
139 {
140   GstGConfAudioSink *sink = GST_GCONF_AUDIO_SINK (object);
141
142   if (sink->client) {
143     gst_gconf_switch_profile (sink, GCONF_PROFILE_NONE);
144     g_object_unref (G_OBJECT (sink->client));
145     sink->client = NULL;
146   }
147
148   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
149 }
150
151 static void
152 gst_gconf_audio_sink_finalize (GstGConfAudioSink * sink)
153 {
154   g_free (sink->gconf_str);
155
156   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, ((GObject *) (sink)));
157 }
158
159 static gboolean
160 do_change_child (GstGConfAudioSink * sink)
161 {
162   const gchar *key;
163   gchar *new_gconf_str;
164   GstElement *new_kid;
165
166   if (sink->profile == GCONF_PROFILE_NONE)
167     return FALSE;               /* Can't switch to a 'NONE' sink */
168
169   key = gst_gconf_get_key_for_sink_profile (sink->profile);
170   new_gconf_str = gst_gconf_get_string (key);
171
172   GST_LOG_OBJECT (sink, "old gconf string: %s", GST_STR_NULL (sink->gconf_str));
173   GST_LOG_OBJECT (sink, "new gconf string: %s", GST_STR_NULL (new_gconf_str));
174
175   if (new_gconf_str != NULL && sink->gconf_str != NULL &&
176       (strlen (new_gconf_str) == 0 ||
177           strcmp (sink->gconf_str, new_gconf_str) == 0)) {
178     g_free (new_gconf_str);
179     GST_DEBUG_OBJECT (sink,
180         "GConf key was updated, but it didn't change. Ignoring");
181     return TRUE;
182   }
183
184   GST_DEBUG_OBJECT (sink, "GConf key changed: '%s' to '%s'",
185       GST_STR_NULL (sink->gconf_str), GST_STR_NULL (new_gconf_str));
186
187   GST_DEBUG_OBJECT (sink, "Creating new child for profile %d", sink->profile);
188   new_kid =
189       gst_gconf_render_bin_with_default (new_gconf_str, DEFAULT_AUDIOSINK);
190
191   if (new_kid == NULL) {
192     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
193         ("Failed to render audio sink from GConf"));
194     goto fail;
195   }
196
197   if (!gst_switch_sink_set_child (GST_SWITCH_SINK (sink), new_kid)) {
198     GST_WARNING_OBJECT (sink, "Failed to update child element");
199     goto fail;
200   }
201
202   g_free (sink->gconf_str);
203   sink->gconf_str = new_gconf_str;
204
205   GST_DEBUG_OBJECT (sink, "done changing gconf audio sink");
206
207   return TRUE;
208
209 fail:
210   g_free (new_gconf_str);
211   return FALSE;
212 }
213
214 static void
215 gst_gconf_switch_profile (GstGConfAudioSink * sink, GstGConfProfile profile)
216 {
217   if (sink->client == NULL)
218     return;
219
220   if (sink->notify_id) {
221     GST_DEBUG_OBJECT (sink, "Unsubscribing old key %s for profile %d",
222         gst_gconf_get_key_for_sink_profile (sink->profile), sink->profile);
223     gconf_client_notify_remove (sink->client, sink->notify_id);
224     sink->notify_id = 0;
225   }
226
227   sink->profile = profile;
228   if (profile != GCONF_PROFILE_NONE) {
229     const gchar *key = gst_gconf_get_key_for_sink_profile (sink->profile);
230
231     GST_DEBUG_OBJECT (sink, "Subscribing to key %s for profile %d",
232         key, profile);
233     sink->notify_id = gconf_client_notify_add (sink->client, key,
234         cb_change_child, sink, NULL, NULL);
235   }
236 }
237
238 static void
239 gst_gconf_audio_sink_set_property (GObject * object, guint prop_id,
240     const GValue * value, GParamSpec * pspec)
241 {
242   GstGConfAudioSink *sink;
243
244   sink = GST_GCONF_AUDIO_SINK (object);
245
246   switch (prop_id) {
247     case PROP_PROFILE:
248       gst_gconf_switch_profile (sink, g_value_get_enum (value));
249       break;
250     default:
251       break;
252   }
253 }
254
255 static void
256 gst_gconf_audio_sink_get_property (GObject * object, guint prop_id,
257     GValue * value, GParamSpec * pspec)
258 {
259   GstGConfAudioSink *sink;
260
261   sink = GST_GCONF_AUDIO_SINK (object);
262
263   switch (prop_id) {
264     case PROP_PROFILE:
265       g_value_set_enum (value, sink->profile);
266       break;
267     default:
268       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269       break;
270   }
271 }
272
273 static void
274 cb_change_child (GConfClient * client,
275     guint connection_id, GConfEntry * entry, gpointer data)
276 {
277   do_change_child (GST_GCONF_AUDIO_SINK (data));
278 }
279
280 static GstStateChangeReturn
281 gst_gconf_audio_sink_change_state (GstElement * element,
282     GstStateChange transition)
283 {
284   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
285   GstGConfAudioSink *sink = GST_GCONF_AUDIO_SINK (element);
286
287   switch (transition) {
288     case GST_STATE_CHANGE_NULL_TO_READY:
289       if (!do_change_child (sink)) {
290         gst_gconf_audio_sink_reset (sink);
291         return GST_STATE_CHANGE_FAILURE;
292       }
293       break;
294     default:
295       break;
296   }
297
298   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
299       (element, transition), GST_STATE_CHANGE_SUCCESS);
300
301   switch (transition) {
302     case GST_STATE_CHANGE_READY_TO_NULL:
303       gst_gconf_audio_sink_reset (sink);
304       break;
305     default:
306       break;
307   }
308
309   return ret;
310 }