various: fix pad template leaks
[platform/upstream/gstreamer.git] / gst / autodetect / gstautoaudiosrc.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
4  * (c) 2008 Stefan Kost <ensonic@users.sf.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-autoaudiosrc
24  * @see_also: autovideosrc, alsasrc, osssrc
25  *
26  * autoaudiosrc is an audio source that automatically detects an appropriate
27  * audio source to use.  It does so by scanning the registry for all elements
28  * that have <quote>Source</quote> and <quote>Audio</quote> in the class field
29  * of their element information, and also have a non-zero autoplugging rank.
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
33  * |[
34  * gst-launch -v -m autoaudiosrc ! audioconvert ! audioresample ! autoaudiosink
35  * ]|
36  * </refsect2>
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <string.h>
44
45 #include "gstautoaudiosrc.h"
46 #include "gstautodetect.h"
47
48 /* Properties */
49 enum
50 {
51   PROP_0,
52   PROP_CAPS,
53 };
54
55 static GstStateChangeReturn
56 gst_auto_audio_src_change_state (GstElement * element,
57     GstStateChange transition);
58 static void gst_auto_audio_src_dispose (GstAutoAudioSrc * src);
59 static void gst_auto_audio_src_clear_kid (GstAutoAudioSrc * src);
60 static void gst_auto_audio_src_set_property (GObject * object, guint prop_id,
61     const GValue * value, GParamSpec * pspec);
62 static void gst_auto_audio_src_get_property (GObject * object, guint prop_id,
63     GValue * value, GParamSpec * pspec);
64
65 GST_BOILERPLATE (GstAutoAudioSrc, gst_auto_audio_src, GstBin, GST_TYPE_BIN);
66
67 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS_ANY);
71
72 static void
73 gst_auto_audio_src_base_init (gpointer klass)
74 {
75   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
76
77   gst_element_class_add_static_pad_template (eklass, &src_template);
78
79   gst_element_class_set_details_simple (eklass, "Auto audio source",
80       "Source/Audio",
81       "Wrapper audio source for automatically detected audio source",
82       "Jan Schmidt <thaytan@noraisin.net>, "
83       "Stefan Kost <ensonic@users.sf.net>");
84 }
85
86 static void
87 gst_auto_audio_src_class_init (GstAutoAudioSrcClass * klass)
88 {
89   GObjectClass *gobject_class;
90   GstElementClass *eklass;
91
92   gobject_class = G_OBJECT_CLASS (klass);
93   eklass = GST_ELEMENT_CLASS (klass);
94
95   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_audio_src_dispose;
96   gobject_class->set_property = gst_auto_audio_src_set_property;
97   gobject_class->get_property = gst_auto_audio_src_get_property;
98
99   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_src_change_state);
100
101   /**
102    * GstAutoAudioSrc:filter-caps
103    *
104    * This property will filter out candidate sinks that can handle the specified
105    * caps. By default only audio sinks that support raw floating point and
106    * integer audio are selected.
107    *
108    * This property can only be set before the element goes to the READY state.
109    *
110    * Since: 0.10.14
111    **/
112   g_object_class_install_property (gobject_class, PROP_CAPS,
113       g_param_spec_boxed ("filter-caps", "Filter caps",
114           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
115           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 }
117
118 static void
119 gst_auto_audio_src_dispose (GstAutoAudioSrc * src)
120 {
121   gst_auto_audio_src_clear_kid (src);
122
123   if (src->filter_caps)
124     gst_caps_unref (src->filter_caps);
125   src->filter_caps = NULL;
126
127   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) src);
128 }
129
130 static void
131 gst_auto_audio_src_clear_kid (GstAutoAudioSrc * src)
132 {
133   if (src->kid) {
134     gst_element_set_state (src->kid, GST_STATE_NULL);
135     gst_bin_remove (GST_BIN (src), src->kid);
136     src->kid = NULL;
137
138     /* Don't lose SOURCE flag */
139     GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
140   }
141 }
142
143 /*
144  * Hack to make initial linking work; ideally, this'd work even when
145  * no target has been assigned to the ghostpad yet.
146  */
147 static void
148 gst_auto_audio_src_reset (GstAutoAudioSrc * src)
149 {
150   GstPad *targetpad;
151
152   gst_auto_audio_src_clear_kid (src);
153
154   /* fakesink placeholder */
155   src->kid = gst_element_factory_make ("fakesrc", "tempsrc");
156   gst_bin_add (GST_BIN (src), src->kid);
157
158   /* pad */
159   targetpad = gst_element_get_static_pad (src->kid, "src");
160   gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad);
161   gst_object_unref (targetpad);
162 }
163
164 static GstStaticCaps raw_caps =
165     GST_STATIC_CAPS ("audio/x-raw-int; audio/x-raw-float");
166
167 static void
168 gst_auto_audio_src_init (GstAutoAudioSrc * src, GstAutoAudioSrcClass * g_class)
169 {
170   src->pad = gst_ghost_pad_new_no_target ("src", GST_PAD_SRC);
171   gst_element_add_pad (GST_ELEMENT (src), src->pad);
172
173   gst_auto_audio_src_reset (src);
174
175   /* set the default raw audio caps */
176   src->filter_caps = gst_static_caps_get (&raw_caps);
177
178   /* mark as source */
179   GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
180 }
181
182 static gboolean
183 gst_auto_audio_src_factory_filter (GstPluginFeature * feature, gpointer data)
184 {
185   guint rank;
186   const gchar *klass;
187
188   /* we only care about element factories */
189   if (!GST_IS_ELEMENT_FACTORY (feature))
190     return FALSE;
191
192   /* audio sinks */
193   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
194   if (!(strstr (klass, "Source") && strstr (klass, "Audio")))
195     return FALSE;
196
197   /* only select elements with autoplugging rank */
198   rank = gst_plugin_feature_get_rank (feature);
199   if (rank < GST_RANK_MARGINAL)
200     return FALSE;
201
202   return TRUE;
203 }
204
205 static gint
206 gst_auto_audio_src_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
207 {
208   gint diff;
209
210   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
211   if (diff != 0)
212     return diff;
213   return strcmp (gst_plugin_feature_get_name (f2),
214       gst_plugin_feature_get_name (f1));
215 }
216
217 static GstElement *
218 gst_auto_audio_src_create_element_with_pretty_name (GstAutoAudioSrc * src,
219     GstElementFactory * factory)
220 {
221   GstElement *element;
222   gchar *name, *marker;
223
224   marker = g_strdup (GST_PLUGIN_FEATURE (factory)->name);
225   if (g_str_has_suffix (marker, "src"))
226     marker[strlen (marker) - 4] = '\0';
227   if (g_str_has_prefix (marker, "gst"))
228     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
229   name = g_strdup_printf ("%s-actual-src-%s", GST_OBJECT_NAME (src), marker);
230   g_free (marker);
231
232   element = gst_element_factory_create (factory, name);
233   g_free (name);
234
235   return element;
236 }
237
238 static GstElement *
239 gst_auto_audio_src_find_best (GstAutoAudioSrc * src)
240 {
241   GList *list, *item;
242   GstElement *choice = NULL;
243   GstMessage *message = NULL;
244   GSList *errors = NULL;
245   GstBus *bus = gst_bus_new ();
246   GstPad *el_pad = NULL;
247   GstCaps *el_caps = NULL;
248   gboolean no_match = TRUE;
249
250   list = gst_registry_feature_filter (gst_registry_get_default (),
251       (GstPluginFeatureFilter) gst_auto_audio_src_factory_filter, FALSE, src);
252   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_src_compare_ranks);
253
254   /* We don't treat sound server sources special. Our policy is that sound
255    * server sources that have a rank must not auto-spawn a daemon under any
256    * circumstances, so there's nothing for us to worry about here */
257   GST_LOG_OBJECT (src, "Trying to find usable audio devices ...");
258
259   for (item = list; item != NULL; item = item->next) {
260     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
261     GstElement *el;
262
263     if ((el = gst_auto_audio_src_create_element_with_pretty_name (src, f))) {
264       GstStateChangeReturn ret;
265
266       GST_DEBUG_OBJECT (src, "Testing %s", GST_PLUGIN_FEATURE (f)->name);
267
268       /* If autoAudioSrc has been provided with filter caps,
269        * accept only sources that match with the filter caps */
270       if (src->filter_caps) {
271         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "src");
272         el_caps = gst_pad_get_caps (el_pad);
273         gst_object_unref (el_pad);
274         GST_DEBUG_OBJECT (src,
275             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
276             src->filter_caps, el_caps);
277         no_match = !gst_caps_can_intersect (src->filter_caps, el_caps);
278         gst_caps_unref (el_caps);
279
280         if (no_match) {
281           GST_DEBUG_OBJECT (src, "Incompatible caps");
282           gst_object_unref (el);
283           continue;
284         } else {
285           GST_DEBUG_OBJECT (src, "Found compatible caps");
286         }
287       }
288
289       gst_element_set_bus (el, bus);
290       ret = gst_element_set_state (el, GST_STATE_READY);
291       if (ret == GST_STATE_CHANGE_SUCCESS) {
292         GST_DEBUG_OBJECT (src, "This worked!");
293         choice = el;
294         break;
295       }
296
297       /* collect all error messages */
298       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
299         GST_DEBUG_OBJECT (src, "error message %" GST_PTR_FORMAT, message);
300         errors = g_slist_append (errors, message);
301       }
302
303       gst_element_set_state (el, GST_STATE_NULL);
304       gst_object_unref (el);
305     }
306   }
307
308   GST_DEBUG_OBJECT (src, "done trying");
309   if (!choice) {
310     if (errors) {
311       /* FIXME: we forward the first error for now; but later on it might make
312        * sense to actually analyse them */
313       gst_message_ref (GST_MESSAGE (errors->data));
314       GST_DEBUG_OBJECT (src, "reposting message %p", errors->data);
315       gst_element_post_message (GST_ELEMENT (src), GST_MESSAGE (errors->data));
316     } else {
317       /* send warning message to application and use a fakesrc */
318       GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
319           ("Failed to find a usable audio source"));
320       choice = gst_element_factory_make ("fakesrc", "fake-audio-src");
321       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
322         g_object_set (choice, "sync", TRUE, NULL);
323       gst_element_set_state (choice, GST_STATE_READY);
324     }
325   }
326   gst_object_unref (bus);
327   gst_plugin_feature_list_free (list);
328   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
329   g_slist_free (errors);
330
331   return choice;
332 }
333
334 static gboolean
335 gst_auto_audio_src_detect (GstAutoAudioSrc * src)
336 {
337   GstElement *esrc;
338   GstPad *targetpad;
339
340   gst_auto_audio_src_clear_kid (src);
341
342   /* find element */
343   GST_DEBUG_OBJECT (src, "Creating new kid");
344   if (!(esrc = gst_auto_audio_src_find_best (src)))
345     goto no_src;
346
347   src->kid = esrc;
348   /* Ensure the child is brought up to the right state to match the parent
349    * although it's currently always in READY and 
350    * we're always doing NULL->READY. */
351   if (GST_STATE (src->kid) < GST_STATE (src))
352     gst_element_set_state (src->kid, GST_STATE (src));
353
354   gst_bin_add (GST_BIN (src), esrc);
355
356   /* attach ghost pad */
357   GST_DEBUG_OBJECT (src, "Re-assigning ghostpad");
358   targetpad = gst_element_get_static_pad (src->kid, "src");
359   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad))
360     goto target_failed;
361
362   gst_object_unref (targetpad);
363   GST_DEBUG_OBJECT (src, "done changing auto audio source");
364
365   return TRUE;
366
367   /* ERRORS */
368 no_src:
369   {
370     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
371         ("Failed to find a supported audio source"));
372     return FALSE;
373   }
374 target_failed:
375   {
376     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
377         ("Failed to set target pad"));
378     gst_object_unref (targetpad);
379     return FALSE;
380   }
381 }
382
383 static GstStateChangeReturn
384 gst_auto_audio_src_change_state (GstElement * element,
385     GstStateChange transition)
386 {
387   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
388   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (element);
389
390   switch (transition) {
391     case GST_STATE_CHANGE_NULL_TO_READY:
392       if (!gst_auto_audio_src_detect (src))
393         return GST_STATE_CHANGE_FAILURE;
394       break;
395     default:
396       break;
397   }
398
399   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
400   if (ret == GST_STATE_CHANGE_FAILURE)
401     return ret;
402
403   switch (transition) {
404     case GST_STATE_CHANGE_READY_TO_NULL:
405       gst_auto_audio_src_reset (src);
406       break;
407     default:
408       break;
409   }
410
411   return ret;
412 }
413
414 static void
415 gst_auto_audio_src_set_property (GObject * object, guint prop_id,
416     const GValue * value, GParamSpec * pspec)
417 {
418   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (object);
419
420   switch (prop_id) {
421     case PROP_CAPS:
422       if (src->filter_caps)
423         gst_caps_unref (src->filter_caps);
424       src->filter_caps = gst_caps_copy (gst_value_get_caps (value));
425       break;
426     default:
427       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428       break;
429   }
430 }
431
432 static void
433 gst_auto_audio_src_get_property (GObject * object, guint prop_id,
434     GValue * value, GParamSpec * pspec)
435 {
436   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (object);
437
438   switch (prop_id) {
439     case PROP_CAPS:{
440       gst_value_set_caps (value, src->filter_caps);
441       break;
442     }
443     default:
444       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
445       break;
446   }
447 }