Branch data Line data Source code
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 [ + + ]: 29 : GST_BOILERPLATE (GstGConfAudioSink, gst_gconf_audio_sink, GstSwitchSink,
60 : 29 : 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 : 7 : gst_gconf_audio_sink_base_init (gpointer klass)
69 : : {
70 : 7 : GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
71 : :
72 : 7 : 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 : 7 : }
77 : :
78 : : #define GST_TYPE_GCONF_PROFILE (gst_gconf_profile_get_type())
79 : : static GType
80 : 7 : 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 [ + - ]: 7 : if (!gconf_profile_type) {
91 : 7 : gconf_profile_type =
92 : 7 : g_enum_register_static ("GstGConfProfile", gconf_profiles);
93 : : }
94 : 7 : return gconf_profile_type;
95 : : }
96 : :
97 : : static void
98 : 7 : gst_gconf_audio_sink_class_init (GstGConfAudioSinkClass * klass)
99 : : {
100 : 7 : GObjectClass *oklass = G_OBJECT_CLASS (klass);
101 : 7 : GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
102 : :
103 : 7 : oklass->set_property = gst_gconf_audio_sink_set_property;
104 : 7 : oklass->get_property = gst_gconf_audio_sink_get_property;
105 : 7 : oklass->dispose = gst_gconf_audio_sink_dispose;
106 : 7 : oklass->finalize = (GObjectFinalizeFunc) gst_gconf_audio_sink_finalize;
107 : 7 : eklass->change_state = gst_gconf_audio_sink_change_state;
108 : :
109 : 7 : 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 : 7 : }
114 : :
115 : : static void
116 : 3 : gst_gconf_audio_sink_reset (GstGConfAudioSink * sink)
117 : : {
118 : 3 : gst_switch_sink_set_child (GST_SWITCH_SINK (sink), NULL);
119 : :
120 : 3 : g_free (sink->gconf_str);
121 : 3 : sink->gconf_str = NULL;
122 : 3 : }
123 : :
124 : : static void
125 : 3 : gst_gconf_audio_sink_init (GstGConfAudioSink * sink,
126 : : GstGConfAudioSinkClass * g_class)
127 : : {
128 : 3 : gst_gconf_audio_sink_reset (sink);
129 : :
130 : 3 : sink->client = gconf_client_get_default ();
131 : 3 : gconf_client_add_dir (sink->client, GST_GCONF_DIR "/default",
132 : : GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
133 : :
134 : 3 : gst_gconf_switch_profile (sink, GCONF_PROFILE_SOUNDS);
135 : 3 : }
136 : :
137 : : static void
138 : 3 : gst_gconf_audio_sink_dispose (GObject * object)
139 : : {
140 : 3 : GstGConfAudioSink *sink = GST_GCONF_AUDIO_SINK (object);
141 : :
142 [ + - ]: 3 : if (sink->client) {
143 : 3 : gst_gconf_switch_profile (sink, GCONF_PROFILE_NONE);
144 : 3 : g_object_unref (G_OBJECT (sink->client));
145 : 3 : sink->client = NULL;
146 : : }
147 : :
148 [ + - ]: 3 : GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
149 : 3 : }
150 : :
151 : : static void
152 : 3 : gst_gconf_audio_sink_finalize (GstGConfAudioSink * sink)
153 : : {
154 : 3 : g_free (sink->gconf_str);
155 : :
156 [ + - ]: 3 : GST_CALL_PARENT (G_OBJECT_CLASS, finalize, ((GObject *) (sink)));
157 : 3 : }
158 : :
159 : : static gboolean
160 : 0 : do_change_child (GstGConfAudioSink * sink)
161 : : {
162 : : const gchar *key;
163 : : gchar *new_gconf_str;
164 : : GstElement *new_kid;
165 : :
166 [ # # ]: 0 : if (sink->profile == GCONF_PROFILE_NONE)
167 : 0 : return FALSE; /* Can't switch to a 'NONE' sink */
168 : :
169 : 0 : key = gst_gconf_get_key_for_sink_profile (sink->profile);
170 : 0 : new_gconf_str = gst_gconf_get_string (key);
171 : :
172 [ # # ][ # # ]: 0 : GST_LOG_OBJECT (sink, "old gconf string: %s", GST_STR_NULL (sink->gconf_str));
173 [ # # ][ # # ]: 0 : GST_LOG_OBJECT (sink, "new gconf string: %s", GST_STR_NULL (new_gconf_str));
174 : :
175 [ # # ][ # # ]: 0 : if (new_gconf_str != NULL && sink->gconf_str != NULL &&
[ # # ]
176 [ # # ]: 0 : (strlen (new_gconf_str) == 0 ||
177 : 0 : strcmp (sink->gconf_str, new_gconf_str) == 0)) {
178 : 0 : g_free (new_gconf_str);
179 [ # # ]: 0 : GST_DEBUG_OBJECT (sink,
180 : : "GConf key was updated, but it didn't change. Ignoring");
181 : 0 : return TRUE;
182 : : }
183 : :
184 [ # # ][ # # ]: 0 : 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 [ # # ]: 0 : GST_DEBUG_OBJECT (sink, "Creating new child for profile %d", sink->profile);
188 : 0 : new_kid =
189 : : gst_gconf_render_bin_with_default (new_gconf_str, DEFAULT_AUDIOSINK);
190 : :
191 [ # # ]: 0 : if (new_kid == NULL) {
192 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
[ # # ][ # # ]
193 : : ("Failed to render audio sink from GConf"));
194 : 0 : goto fail;
195 : : }
196 : :
197 [ # # ]: 0 : if (!gst_switch_sink_set_child (GST_SWITCH_SINK (sink), new_kid)) {
198 [ # # ]: 0 : GST_WARNING_OBJECT (sink, "Failed to update child element");
199 : 0 : goto fail;
200 : : }
201 : :
202 : 0 : g_free (sink->gconf_str);
203 : 0 : sink->gconf_str = new_gconf_str;
204 : :
205 [ # # ]: 0 : GST_DEBUG_OBJECT (sink, "done changing gconf audio sink");
206 : :
207 : 0 : return TRUE;
208 : :
209 : : fail:
210 : 0 : g_free (new_gconf_str);
211 : 0 : return FALSE;
212 : : }
213 : :
214 : : static void
215 : 6 : gst_gconf_switch_profile (GstGConfAudioSink * sink, GstGConfProfile profile)
216 : : {
217 [ - + ]: 6 : if (sink->client == NULL)
218 : 6 : return;
219 : :
220 [ + + ]: 6 : if (sink->notify_id) {
221 [ - + ]: 3 : GST_DEBUG_OBJECT (sink, "Unsubscribing old key %s for profile %d",
222 : : gst_gconf_get_key_for_sink_profile (sink->profile), sink->profile);
223 : 3 : gconf_client_notify_remove (sink->client, sink->notify_id);
224 : 3 : sink->notify_id = 0;
225 : : }
226 : :
227 : 6 : sink->profile = profile;
228 [ + + ]: 6 : if (profile != GCONF_PROFILE_NONE) {
229 : 3 : const gchar *key = gst_gconf_get_key_for_sink_profile (sink->profile);
230 : :
231 [ - + ]: 3 : GST_DEBUG_OBJECT (sink, "Subscribing to key %s for profile %d",
232 : : key, profile);
233 : 3 : sink->notify_id = gconf_client_notify_add (sink->client, key,
234 : : cb_change_child, sink, NULL, NULL);
235 : : }
236 : : }
237 : :
238 : : static void
239 : 0 : gst_gconf_audio_sink_set_property (GObject * object, guint prop_id,
240 : : const GValue * value, GParamSpec * pspec)
241 : : {
242 : : GstGConfAudioSink *sink;
243 : :
244 : 0 : sink = GST_GCONF_AUDIO_SINK (object);
245 : :
246 [ # # ]: 0 : switch (prop_id) {
247 : : case PROP_PROFILE:
248 : 0 : gst_gconf_switch_profile (sink, g_value_get_enum (value));
249 : 0 : break;
250 : : default:
251 : 0 : break;
252 : : }
253 : 0 : }
254 : :
255 : : static void
256 : 3 : gst_gconf_audio_sink_get_property (GObject * object, guint prop_id,
257 : : GValue * value, GParamSpec * pspec)
258 : : {
259 : : GstGConfAudioSink *sink;
260 : :
261 : 3 : sink = GST_GCONF_AUDIO_SINK (object);
262 : :
263 [ + - ]: 3 : switch (prop_id) {
264 : : case PROP_PROFILE:
265 : 3 : g_value_set_enum (value, sink->profile);
266 : 3 : break;
267 : : default:
268 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 : 0 : break;
270 : : }
271 : 3 : }
272 : :
273 : : static void
274 : 0 : cb_change_child (GConfClient * client,
275 : : guint connection_id, GConfEntry * entry, gpointer data)
276 : : {
277 : 0 : do_change_child (GST_GCONF_AUDIO_SINK (data));
278 : 0 : }
279 : :
280 : : static GstStateChangeReturn
281 : 0 : gst_gconf_audio_sink_change_state (GstElement * element,
282 : : GstStateChange transition)
283 : : {
284 : 0 : GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
285 : 0 : GstGConfAudioSink *sink = GST_GCONF_AUDIO_SINK (element);
286 : :
287 [ # # ]: 0 : switch (transition) {
288 : : case GST_STATE_CHANGE_NULL_TO_READY:
289 [ # # ]: 0 : if (!do_change_child (sink)) {
290 : 0 : gst_gconf_audio_sink_reset (sink);
291 : 0 : return GST_STATE_CHANGE_FAILURE;
292 : : }
293 : 0 : break;
294 : : default:
295 : 0 : break;
296 : : }
297 : :
298 [ # # ]: 0 : ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
299 : : (element, transition), GST_STATE_CHANGE_SUCCESS);
300 : :
301 [ # # ]: 0 : switch (transition) {
302 : : case GST_STATE_CHANGE_READY_TO_NULL:
303 : 0 : gst_gconf_audio_sink_reset (sink);
304 : 0 : break;
305 : : default:
306 : 0 : break;
307 : : }
308 : :
309 : 0 : return ret;
310 : : }
|