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