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