Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / gconf / gstgconfaudiosrc.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2005 Tim-Philipp Müller <tim centricular net>
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-gconfaudiosrc
22  * @see_also: #GstAlsaSrc, #GstAutoAudioSrc
23  *
24  * This element records sound from the audiosink that has been configured in
25  * GConf by the user.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch gconfaudiosrc ! audioconvert ! wavenc ! filesink location=record.wav
31  * ]| Record from configured audioinput
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <string.h>
40
41 #include "gstgconfelements.h"
42 #include "gstgconfaudiosrc.h"
43
44 static void gst_gconf_audio_src_dispose (GObject * object);
45 static void gst_gconf_audio_src_finalize (GstGConfAudioSrc * src);
46 static void cb_toggle_element (GConfClient * client,
47     guint connection_id, GConfEntry * entry, gpointer data);
48 static GstStateChangeReturn
49 gst_gconf_audio_src_change_state (GstElement * element,
50     GstStateChange transition);
51
52 GST_BOILERPLATE (GstGConfAudioSrc, gst_gconf_audio_src, GstSwitchSrc,
53     GST_TYPE_SWITCH_SRC);
54
55 static void
56 gst_gconf_audio_src_base_init (gpointer klass)
57 {
58   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
59
60   gst_element_class_set_details_simple (eklass, "GConf audio source",
61       "Source/Audio",
62       "Audio source embedding the GConf-settings for audio input",
63       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
64 }
65
66 static void
67 gst_gconf_audio_src_class_init (GstGConfAudioSrcClass * klass)
68 {
69   GObjectClass *oklass = G_OBJECT_CLASS (klass);
70   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
71
72   oklass->dispose = gst_gconf_audio_src_dispose;
73   oklass->finalize = (GObjectFinalizeFunc) gst_gconf_audio_src_finalize;
74   eklass->change_state = gst_gconf_audio_src_change_state;
75 }
76
77 /*
78  * Hack to make negotiation work.
79  */
80
81 static gboolean
82 gst_gconf_audio_src_reset (GstGConfAudioSrc * src)
83 {
84   gst_switch_src_set_child (GST_SWITCH_SRC (src), NULL);
85
86   g_free (src->gconf_str);
87   src->gconf_str = NULL;
88   return TRUE;
89 }
90
91 static void
92 gst_gconf_audio_src_init (GstGConfAudioSrc * src,
93     GstGConfAudioSrcClass * g_class)
94 {
95   gst_gconf_audio_src_reset (src);
96
97   src->client = gconf_client_get_default ();
98   gconf_client_add_dir (src->client, GST_GCONF_DIR,
99       GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
100   src->gconf_notify_id = gconf_client_notify_add (src->client,
101       GST_GCONF_DIR "/" GST_GCONF_AUDIOSRC_KEY,
102       cb_toggle_element, src, NULL, NULL);
103 }
104
105 static void
106 gst_gconf_audio_src_dispose (GObject * object)
107 {
108   GstGConfAudioSrc *src = GST_GCONF_AUDIO_SRC (object);
109
110   if (src->client) {
111     if (src->gconf_notify_id) {
112       gconf_client_notify_remove (src->client, src->gconf_notify_id);
113       src->gconf_notify_id = 0;
114     }
115
116     g_object_unref (G_OBJECT (src->client));
117     src->client = NULL;
118   }
119
120   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
121 }
122
123 static void
124 gst_gconf_audio_src_finalize (GstGConfAudioSrc * src)
125 {
126   g_free (src->gconf_str);
127
128   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, ((GObject *) (src)));
129 }
130
131 static gboolean
132 do_toggle_element (GstGConfAudioSrc * src)
133 {
134   GstElement *new_kid;
135   gchar *new_gconf_str;
136
137   new_gconf_str = gst_gconf_get_string (GST_GCONF_AUDIOSRC_KEY);
138   if (new_gconf_str != NULL && src->gconf_str != NULL &&
139       (strlen (new_gconf_str) == 0 ||
140           strcmp (src->gconf_str, new_gconf_str) == 0)) {
141     g_free (new_gconf_str);
142     GST_DEBUG_OBJECT (src, "GConf key was updated, but it didn't change");
143     return TRUE;
144   }
145
146   GST_DEBUG_OBJECT (src, "GConf key changed: '%s' to '%s'",
147       GST_STR_NULL (src->gconf_str), GST_STR_NULL (new_gconf_str));
148
149   GST_DEBUG_OBJECT (src, "Creating new kid");
150   if (!(new_kid = gst_gconf_get_default_audio_src ())) {
151     GST_ELEMENT_ERROR (src, LIBRARY, SETTINGS, (NULL),
152         ("Failed to render audio src from GConf"));
153     return FALSE;
154   }
155
156   if (!gst_switch_src_set_child (GST_SWITCH_SRC (src), new_kid)) {
157     GST_WARNING_OBJECT (src, "Failed to update child element");
158     goto fail;
159   }
160
161   g_free (src->gconf_str);
162   src->gconf_str = new_gconf_str;
163
164   GST_DEBUG_OBJECT (src, "done changing gconf audio src");
165
166   return TRUE;
167 fail:
168   g_free (new_gconf_str);
169   return FALSE;
170 }
171
172 static void
173 cb_toggle_element (GConfClient * client,
174     guint connection_id, GConfEntry * entry, gpointer data)
175 {
176   do_toggle_element (GST_GCONF_AUDIO_SRC (data));
177 }
178
179 static GstStateChangeReturn
180 gst_gconf_audio_src_change_state (GstElement * element,
181     GstStateChange transition)
182 {
183   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
184   GstGConfAudioSrc *src = GST_GCONF_AUDIO_SRC (element);
185
186   switch (transition) {
187     case GST_STATE_CHANGE_NULL_TO_READY:
188       if (!do_toggle_element (src)) {
189         gst_gconf_audio_src_reset (src);
190         return GST_STATE_CHANGE_FAILURE;
191       }
192       break;
193     default:
194       break;
195   }
196
197   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
198       (element, transition), GST_STATE_CHANGE_SUCCESS);
199
200   switch (transition) {
201     case GST_STATE_CHANGE_READY_TO_NULL:
202       if (!gst_gconf_audio_src_reset (src))
203         ret = GST_STATE_CHANGE_FAILURE;
204       break;
205     default:
206       break;
207   }
208
209   return ret;
210 }