various: fix pad template leaks
[platform/upstream/gstreamer.git] / ext / hal / gsthalaudiosink.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 /**
22  * SECTION:element-halaudiosink
23  *
24  * HalAudioSink allows access to output of sound devices by specifying the
25  * corresponding persistent Unique Device Id (UDI) from the Hardware Abstraction
26  * Layer (HAL) in the #GstHalAudioSink:udi property.
27  * It currently always embeds alsasink or osssink as HAL doesn't support other
28  * sound systems yet. You can also specify the UDI of a device that has ALSA or
29  * OSS subdevices. If both are present ALSA is preferred.
30  *
31  * <refsect2>
32  * <title>Examples</title>
33  * |[
34  * hal-find-by-property --key alsa.type --string playback
35  * ]| list the UDIs of all your ALSA output devices
36  * |[
37  * gst-launch -v audiotestsrc ! halaudiosink udi=/org/freedesktop/Hal/devices/pci_8086_27d8_alsa_playback_0
38  * ]| test your soundcard by playing a test signal on the specified sound device.
39  * </refsect2>
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gsthalelements.h"
47 #include "gsthalaudiosink.h"
48
49 static void gst_hal_audio_sink_dispose (GObject * object);
50 static GstStateChangeReturn
51 gst_hal_audio_sink_change_state (GstElement * element,
52     GstStateChange transition);
53
54 enum
55 {
56   PROP_0,
57   PROP_UDI
58 };
59
60 GST_BOILERPLATE (GstHalAudioSink, gst_hal_audio_sink, GstBin, GST_TYPE_BIN);
61
62 static void gst_hal_audio_sink_set_property (GObject * object, guint prop_id,
63     const GValue * value, GParamSpec * pspec);
64 static void gst_hal_audio_sink_get_property (GObject * object, guint prop_id,
65     GValue * value, GParamSpec * pspec);
66
67 static void
68 gst_hal_audio_sink_base_init (gpointer klass)
69 {
70   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
71
72   static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
73       GST_PAD_SINK,
74       GST_PAD_ALWAYS,
75       GST_STATIC_CAPS_ANY);
76
77   gst_element_class_add_static_pad_template (eklass, &sink_template);
78   gst_element_class_set_details_simple (eklass, "HAL audio sink",
79       "Sink/Audio",
80       "Audio sink for sound device access via HAL",
81       "Jürg Billeter <j@bitron.ch>");
82 }
83
84 static void
85 gst_hal_audio_sink_class_init (GstHalAudioSinkClass * klass)
86 {
87   GObjectClass *oklass = G_OBJECT_CLASS (klass);
88   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
89
90   oklass->set_property = gst_hal_audio_sink_set_property;
91   oklass->get_property = gst_hal_audio_sink_get_property;
92   oklass->dispose = gst_hal_audio_sink_dispose;
93   eklass->change_state = gst_hal_audio_sink_change_state;
94
95   g_object_class_install_property (oklass, PROP_UDI,
96       g_param_spec_string ("udi",
97           "UDI", "Unique Device Id", NULL,
98           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 }
100
101 /*
102  * Hack to make negotiation work.
103  */
104
105 static void
106 gst_hal_audio_sink_reset (GstHalAudioSink * sink)
107 {
108   GstPad *targetpad;
109
110   /* fakesink */
111   if (sink->kid) {
112     gst_element_set_state (sink->kid, GST_STATE_NULL);
113     gst_bin_remove (GST_BIN (sink), sink->kid);
114   }
115   sink->kid = gst_element_factory_make ("fakesink", "testsink");
116   gst_bin_add (GST_BIN (sink), sink->kid);
117
118   targetpad = gst_element_get_static_pad (sink->kid, "sink");
119   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
120   gst_object_unref (targetpad);
121 }
122
123 static void
124 gst_hal_audio_sink_init (GstHalAudioSink * sink, GstHalAudioSinkClass * g_class)
125 {
126   sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
127   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
128
129   gst_hal_audio_sink_reset (sink);
130 }
131
132 static void
133 gst_hal_audio_sink_dispose (GObject * object)
134 {
135   GstHalAudioSink *sink = GST_HAL_AUDIO_SINK (object);
136
137   if (sink->udi) {
138     g_free (sink->udi);
139     sink->udi = NULL;
140   }
141
142   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
143 }
144
145 static gboolean
146 do_toggle_element (GstHalAudioSink * sink)
147 {
148   GstPad *targetpad;
149
150   /* kill old element */
151   if (sink->kid) {
152     GST_DEBUG_OBJECT (sink, "Removing old kid");
153     gst_element_set_state (sink->kid, GST_STATE_NULL);
154     gst_bin_remove (GST_BIN (sink), sink->kid);
155     sink->kid = NULL;
156   }
157
158   GST_DEBUG_OBJECT (sink, "Creating new kid");
159   if (!sink->udi)
160     GST_INFO_OBJECT (sink, "No UDI set for device, using default one");
161
162   if (!(sink->kid = gst_hal_get_audio_sink (sink->udi))) {
163     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
164         ("Failed to render audio sink from Hal"));
165     return FALSE;
166   }
167   gst_element_set_state (sink->kid, GST_STATE (sink));
168   gst_bin_add (GST_BIN (sink), sink->kid);
169
170   /* re-attach ghostpad */
171   GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
172   targetpad = gst_element_get_static_pad (sink->kid, "sink");
173   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
174   gst_object_unref (targetpad);
175   GST_DEBUG_OBJECT (sink, "done changing hal audio sink");
176
177   return TRUE;
178 }
179
180 static void
181 gst_hal_audio_sink_set_property (GObject * object, guint prop_id,
182     const GValue * value, GParamSpec * pspec)
183 {
184   GstHalAudioSink *this = GST_HAL_AUDIO_SINK (object);
185
186   GST_OBJECT_LOCK (this);
187
188   switch (prop_id) {
189     case PROP_UDI:
190       if (this->udi)
191         g_free (this->udi);
192       this->udi = g_value_dup_string (value);
193       break;
194     default:
195       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196       break;
197   }
198
199   GST_OBJECT_UNLOCK (this);
200 }
201
202 static void
203 gst_hal_audio_sink_get_property (GObject * object, guint prop_id,
204     GValue * value, GParamSpec * pspec)
205 {
206   GstHalAudioSink *this = GST_HAL_AUDIO_SINK (object);
207
208   GST_OBJECT_LOCK (this);
209
210   switch (prop_id) {
211     case PROP_UDI:
212       g_value_set_string (value, this->udi);
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218
219   GST_OBJECT_UNLOCK (this);
220 }
221
222 static GstStateChangeReturn
223 gst_hal_audio_sink_change_state (GstElement * element,
224     GstStateChange transition)
225 {
226   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
227   GstHalAudioSink *sink = GST_HAL_AUDIO_SINK (element);
228
229   switch (transition) {
230     case GST_STATE_CHANGE_NULL_TO_READY:
231       if (!do_toggle_element (sink))
232         return GST_STATE_CHANGE_FAILURE;
233       break;
234     default:
235       break;
236   }
237
238   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
239       (element, transition), GST_STATE_CHANGE_SUCCESS);
240
241   switch (transition) {
242     case GST_STATE_CHANGE_READY_TO_NULL:
243       gst_hal_audio_sink_reset (sink);
244       break;
245     default:
246       break;
247   }
248
249   return ret;
250 }