Merge branch 'master' into 0.11
[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 #define gst_auto_audio_src_parent_class parent_class
66 G_DEFINE_TYPE (GstAutoAudioSrc, gst_auto_audio_src, GST_TYPE_BIN);
67
68 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
69     GST_PAD_SRC,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS_ANY);
72
73 static void
74 gst_auto_audio_src_class_init (GstAutoAudioSrcClass * klass)
75 {
76   GObjectClass *gobject_class;
77   GstElementClass *eklass;
78
79   gobject_class = G_OBJECT_CLASS (klass);
80   eklass = GST_ELEMENT_CLASS (klass);
81
82   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_audio_src_dispose;
83   gobject_class->set_property = gst_auto_audio_src_set_property;
84   gobject_class->get_property = gst_auto_audio_src_get_property;
85
86   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_audio_src_change_state);
87
88   /**
89    * GstAutoAudioSrc:filter-caps
90    *
91    * This property will filter out candidate sinks that can handle the specified
92    * caps. By default only audio sinks that support raw floating point and
93    * integer audio are selected.
94    *
95    * This property can only be set before the element goes to the READY state.
96    *
97    * Since: 0.10.14
98    **/
99   g_object_class_install_property (gobject_class, PROP_CAPS,
100       g_param_spec_boxed ("filter-caps", "Filter caps",
101           "Filter sink candidates using these caps.", GST_TYPE_CAPS,
102           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
103
104   gst_element_class_add_pad_template (eklass,
105       gst_static_pad_template_get (&src_template));
106
107   gst_element_class_set_details_simple (eklass, "Auto audio source",
108       "Source/Audio",
109       "Wrapper audio source for automatically detected audio source",
110       "Jan Schmidt <thaytan@noraisin.net>, "
111       "Stefan Kost <ensonic@users.sf.net>");
112 }
113
114 static void
115 gst_auto_audio_src_dispose (GstAutoAudioSrc * src)
116 {
117   gst_auto_audio_src_clear_kid (src);
118
119   if (src->filter_caps)
120     gst_caps_unref (src->filter_caps);
121   src->filter_caps = NULL;
122
123   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) src);
124 }
125
126 static void
127 gst_auto_audio_src_clear_kid (GstAutoAudioSrc * src)
128 {
129   if (src->kid) {
130     gst_element_set_state (src->kid, GST_STATE_NULL);
131     gst_bin_remove (GST_BIN (src), src->kid);
132     src->kid = NULL;
133
134     /* Don't lose SOURCE flag */
135     GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
136   }
137 }
138
139 /*
140  * Hack to make initial linking work; ideally, this'd work even when
141  * no target has been assigned to the ghostpad yet.
142  */
143 static void
144 gst_auto_audio_src_reset (GstAutoAudioSrc * src)
145 {
146   GstPad *targetpad;
147
148   gst_auto_audio_src_clear_kid (src);
149
150   /* fakesink placeholder */
151   src->kid = gst_element_factory_make ("fakesrc", "tempsrc");
152   gst_bin_add (GST_BIN (src), src->kid);
153
154   /* pad */
155   targetpad = gst_element_get_static_pad (src->kid, "src");
156   gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad);
157   gst_object_unref (targetpad);
158 }
159
160 static GstStaticCaps raw_caps = GST_STATIC_CAPS ("audio/x-raw");
161
162 static void
163 gst_auto_audio_src_init (GstAutoAudioSrc * src)
164 {
165   src->pad = gst_ghost_pad_new_no_target ("src", GST_PAD_SRC);
166   gst_element_add_pad (GST_ELEMENT (src), src->pad);
167
168   gst_auto_audio_src_reset (src);
169
170   /* set the default raw audio caps */
171   src->filter_caps = gst_static_caps_get (&raw_caps);
172
173   /* mark as source */
174   GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
175 }
176
177 static gboolean
178 gst_auto_audio_src_factory_filter (GstPluginFeature * feature, gpointer data)
179 {
180   guint rank;
181   const gchar *klass;
182
183   /* we only care about element factories */
184   if (!GST_IS_ELEMENT_FACTORY (feature))
185     return FALSE;
186
187   /* audio sinks */
188   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
189   if (!(strstr (klass, "Source") && strstr (klass, "Audio")))
190     return FALSE;
191
192   /* only select elements with autoplugging rank */
193   rank = gst_plugin_feature_get_rank (feature);
194   if (rank < GST_RANK_MARGINAL)
195     return FALSE;
196
197   return TRUE;
198 }
199
200 static gint
201 gst_auto_audio_src_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
202 {
203   gint diff;
204
205   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
206   if (diff != 0)
207     return diff;
208   return strcmp (gst_plugin_feature_get_name (f2),
209       gst_plugin_feature_get_name (f1));
210 }
211
212 static GstElement *
213 gst_auto_audio_src_create_element_with_pretty_name (GstAutoAudioSrc * src,
214     GstElementFactory * factory)
215 {
216   GstElement *element;
217   gchar *name, *marker;
218
219   marker = g_strdup (GST_OBJECT_NAME (factory));
220   if (g_str_has_suffix (marker, "src"))
221     marker[strlen (marker) - 4] = '\0';
222   if (g_str_has_prefix (marker, "gst"))
223     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
224   name = g_strdup_printf ("%s-actual-src-%s", GST_OBJECT_NAME (src), marker);
225   g_free (marker);
226
227   element = gst_element_factory_create (factory, name);
228   g_free (name);
229
230   return element;
231 }
232
233 static GstElement *
234 gst_auto_audio_src_find_best (GstAutoAudioSrc * src)
235 {
236   GList *list, *item;
237   GstElement *choice = NULL;
238   GstMessage *message = NULL;
239   GSList *errors = NULL;
240   GstBus *bus = gst_bus_new ();
241   GstPad *el_pad = NULL;
242   GstCaps *el_caps = NULL;
243   gboolean no_match = TRUE;
244
245   list = gst_registry_feature_filter (gst_registry_get_default (),
246       (GstPluginFeatureFilter) gst_auto_audio_src_factory_filter, FALSE, src);
247   list = g_list_sort (list, (GCompareFunc) gst_auto_audio_src_compare_ranks);
248
249   /* We don't treat sound server sources special. Our policy is that sound
250    * server sources that have a rank must not auto-spawn a daemon under any
251    * circumstances, so there's nothing for us to worry about here */
252   GST_LOG_OBJECT (src, "Trying to find usable audio devices ...");
253
254   for (item = list; item != NULL; item = item->next) {
255     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
256     GstElement *el;
257
258     if ((el = gst_auto_audio_src_create_element_with_pretty_name (src, f))) {
259       GstStateChangeReturn ret;
260
261       GST_DEBUG_OBJECT (src, "Testing %s", GST_OBJECT_NAME (f));
262
263       /* If autoAudioSrc has been provided with filter caps,
264        * accept only sources that match with the filter caps */
265       if (src->filter_caps) {
266         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "src");
267         el_caps = gst_pad_get_caps (el_pad, NULL);
268         gst_object_unref (el_pad);
269         GST_DEBUG_OBJECT (src,
270             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
271             src->filter_caps, el_caps);
272         no_match = !gst_caps_can_intersect (src->filter_caps, el_caps);
273         gst_caps_unref (el_caps);
274
275         if (no_match) {
276           GST_DEBUG_OBJECT (src, "Incompatible caps");
277           gst_object_unref (el);
278           continue;
279         } else {
280           GST_DEBUG_OBJECT (src, "Found compatible caps");
281         }
282       }
283
284       gst_element_set_bus (el, bus);
285       ret = gst_element_set_state (el, GST_STATE_READY);
286       if (ret == GST_STATE_CHANGE_SUCCESS) {
287         GST_DEBUG_OBJECT (src, "This worked!");
288         choice = el;
289         break;
290       }
291
292       /* collect all error messages */
293       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
294         GST_DEBUG_OBJECT (src, "error message %" GST_PTR_FORMAT, message);
295         errors = g_slist_append (errors, message);
296       }
297
298       gst_element_set_state (el, GST_STATE_NULL);
299       gst_object_unref (el);
300     }
301   }
302
303   GST_DEBUG_OBJECT (src, "done trying");
304   if (!choice) {
305     if (errors) {
306       /* FIXME: we forward the first error for now; but later on it might make
307        * sense to actually analyse them */
308       gst_message_ref (GST_MESSAGE (errors->data));
309       GST_DEBUG_OBJECT (src, "reposting message %p", errors->data);
310       gst_element_post_message (GST_ELEMENT (src), GST_MESSAGE (errors->data));
311     } else {
312       /* send warning message to application and use a fakesrc */
313       GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
314           ("Failed to find a usable audio source"));
315       choice = gst_element_factory_make ("fakesrc", "fake-audio-src");
316       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
317         g_object_set (choice, "sync", TRUE, NULL);
318       gst_element_set_state (choice, GST_STATE_READY);
319     }
320   }
321   gst_object_unref (bus);
322   gst_plugin_feature_list_free (list);
323   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
324   g_slist_free (errors);
325
326   return choice;
327 }
328
329 static gboolean
330 gst_auto_audio_src_detect (GstAutoAudioSrc * src)
331 {
332   GstElement *esrc;
333   GstPad *targetpad;
334
335   gst_auto_audio_src_clear_kid (src);
336
337   /* find element */
338   GST_DEBUG_OBJECT (src, "Creating new kid");
339   if (!(esrc = gst_auto_audio_src_find_best (src)))
340     goto no_src;
341
342   src->kid = esrc;
343   /* Ensure the child is brought up to the right state to match the parent
344    * although it's currently always in READY and 
345    * we're always doing NULL->READY. */
346   if (GST_STATE (src->kid) < GST_STATE (src))
347     gst_element_set_state (src->kid, GST_STATE (src));
348
349   gst_bin_add (GST_BIN (src), esrc);
350
351   /* attach ghost pad */
352   GST_DEBUG_OBJECT (src, "Re-assigning ghostpad");
353   targetpad = gst_element_get_static_pad (src->kid, "src");
354   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad))
355     goto target_failed;
356
357   gst_object_unref (targetpad);
358   GST_DEBUG_OBJECT (src, "done changing auto audio source");
359
360   return TRUE;
361
362   /* ERRORS */
363 no_src:
364   {
365     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
366         ("Failed to find a supported audio source"));
367     return FALSE;
368   }
369 target_failed:
370   {
371     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
372         ("Failed to set target pad"));
373     gst_object_unref (targetpad);
374     return FALSE;
375   }
376 }
377
378 static GstStateChangeReturn
379 gst_auto_audio_src_change_state (GstElement * element,
380     GstStateChange transition)
381 {
382   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
383   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (element);
384
385   switch (transition) {
386     case GST_STATE_CHANGE_NULL_TO_READY:
387       if (!gst_auto_audio_src_detect (src))
388         return GST_STATE_CHANGE_FAILURE;
389       break;
390     default:
391       break;
392   }
393
394   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
395   if (ret == GST_STATE_CHANGE_FAILURE)
396     return ret;
397
398   switch (transition) {
399     case GST_STATE_CHANGE_READY_TO_NULL:
400       gst_auto_audio_src_reset (src);
401       break;
402     default:
403       break;
404   }
405
406   return ret;
407 }
408
409 static void
410 gst_auto_audio_src_set_property (GObject * object, guint prop_id,
411     const GValue * value, GParamSpec * pspec)
412 {
413   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (object);
414
415   switch (prop_id) {
416     case PROP_CAPS:
417       if (src->filter_caps)
418         gst_caps_unref (src->filter_caps);
419       src->filter_caps = gst_caps_copy (gst_value_get_caps (value));
420       break;
421     default:
422       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
423       break;
424   }
425 }
426
427 static void
428 gst_auto_audio_src_get_property (GObject * object, guint prop_id,
429     GValue * value, GParamSpec * pspec)
430 {
431   GstAutoAudioSrc *src = GST_AUTO_AUDIO_SRC (object);
432
433   switch (prop_id) {
434     case PROP_CAPS:{
435       gst_value_set_caps (value, src->filter_caps);
436       break;
437     }
438     default:
439       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440       break;
441   }
442 }