2 * Copyright (C) 2003-2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * Copyright (C) 2005-2006 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
22 * SECTION:gstaudiomixerutils
23 * @short_description: utility functions to find available audio mixers
24 * from the plugin registry
28 * Provides some utility functions to detect available audio mixers
34 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
35 * with newer GLib versions (>= 2.31.0) */
36 #define GLIB_DISABLE_DEPRECATION_WARNINGS
42 #include "mixerutils.h"
47 gst_audio_mixer_filter_do_filter (GstAudioMixerFilterFunc filter_func,
48 GstElementFactory * factory,
49 GstElement ** p_element, GList ** p_collection, gpointer user_data)
51 /* so, the element is a mixer, let's see if the caller wants it */
52 if (filter_func != NULL) {
53 if (filter_func (GST_MIXER (*p_element), user_data) == TRUE) {
54 *p_collection = g_list_prepend (*p_collection, *p_element);
55 /* do not set state back to NULL here on purpose, caller
56 * might want to keep the mixer open */
60 gst_element_set_state (*p_element, GST_STATE_NULL);
61 *p_collection = g_list_prepend (*p_collection, *p_element);
65 /* create new element for further probing if the old one was cleared */
66 if (*p_element == NULL) {
67 *p_element = gst_element_factory_create (factory, NULL);
72 gst_audio_mixer_filter_check_element (GstElement * element)
74 GstStateChangeReturn ret;
76 /* open device (only then we can know for sure whether it is a mixer) */
77 gst_element_set_state (element, GST_STATE_READY);
78 ret = gst_element_get_state (element, NULL, NULL, 1 * GST_SECOND);
79 if (ret != GST_STATE_CHANGE_SUCCESS) {
80 GST_DEBUG ("could not open device / set element to READY");
81 gst_element_set_state (element, GST_STATE_NULL);
85 /* is this device a mixer? */
86 if (!GST_IS_MIXER (element)) {
87 GST_DEBUG ("element is not a mixer");
88 gst_element_set_state (element, GST_STATE_NULL);
93 if (!gst_mixer_list_tracks (GST_MIXER (element))) {
94 GST_DEBUG ("element is a mixer, but has no tracks");
95 gst_element_set_state (element, GST_STATE_NULL);
99 GST_DEBUG ("element is a mixer with mixer tracks");
104 gst_audio_mixer_filter_probe_feature (GstAudioMixerFilterFunc filter_func,
105 GstElementFactory * factory,
106 GList ** p_collection, gboolean first, gpointer user_data)
110 GST_DEBUG ("probing %s ...", gst_element_factory_get_longname (factory));
113 element = gst_element_factory_create (factory, NULL);
115 if (element == NULL) {
116 GST_DEBUG ("could not create element from factory");
120 GST_DEBUG ("created element %s (%p)", GST_ELEMENT_NAME (element), element);
123 if (GST_IS_PROPERTY_PROBE (element)) {
124 GstPropertyProbe *probe;
125 const GParamSpec *devspec;
127 probe = GST_PROPERTY_PROBE (element);
129 GST_DEBUG ("probing available devices ...");
130 if ((devspec = gst_property_probe_get_property (probe, "device"))) {
133 if ((array = gst_property_probe_probe_and_get_values (probe, devspec))) {
136 GST_DEBUG ("there are %d available devices", array->n_values);
138 /* set all devices and test for mixer */
139 for (n = 0; n < array->n_values; n++) {
142 /* set this device */
143 device = g_value_array_get_nth (array, n);
144 g_object_set_property (G_OBJECT (element), "device", device);
146 GST_DEBUG ("trying device %s ..", g_value_get_string (device));
148 if (gst_audio_mixer_filter_check_element (element)) {
149 gst_audio_mixer_filter_do_filter (filter_func, factory, &element,
150 p_collection, user_data);
152 if (first && *p_collection != NULL) {
153 GST_DEBUG ("Stopping after first found mixer, as requested");
158 g_value_array_free (array);
164 GST_DEBUG ("element does not support the property probe interface");
166 if (gst_audio_mixer_filter_check_element (element)) {
167 gst_audio_mixer_filter_do_filter (filter_func, factory, &element,
168 p_collection, user_data);
173 gst_element_set_state (element, GST_STATE_NULL);
174 gst_object_unref (element);
179 element_factory_rank_compare_func (gconstpointer a, gconstpointer b)
181 gint rank_a = gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (a));
182 gint rank_b = gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (b));
184 /* make order chosen in the end more determinable */
185 if (rank_a == rank_b) {
186 const gchar *name_a = GST_OBJECT_NAME (GST_PLUGIN_FEATURE (a));
187 const gchar *name_b = GST_OBJECT_NAME (GST_PLUGIN_FEATURE (b));
189 return g_ascii_strcasecmp (name_a, name_b);
192 return rank_b - rank_a;
196 * gst_audio_default_registry_mixer_filter:
197 * @filter_func: filter function, or #NULL
198 * @first: set to #TRUE if you only want the first suitable mixer element
199 * @user_data: user data to pass to the filter function
201 * Utility function to find audio mixer elements.
203 * Will traverse the default plugin registry in order of plugin rank and
204 * find usable audio mixer elements. The caller may optionally fine-tune
205 * the selection by specifying a filter function.
207 * Returns: a #GList of audio mixer #GstElement<!-- -->s. You must free each
208 * element in the list by setting it to NULL state and calling
209 * gst_object_unref(). After that the list itself should be freed
210 * using g_list_free().
215 gst_audio_default_registry_mixer_filter (GstAudioMixerFilterFunc filter_func,
216 gboolean first, gpointer data)
218 GList *mixer_list = NULL;
222 /* go through all elements of a certain class and check whether
223 * they implement a mixer. If so, add it to the list. */
224 feature_list = gst_registry_get_feature_list (gst_registry_get (),
225 GST_TYPE_ELEMENT_FACTORY);
227 feature_list = g_list_sort (feature_list, element_factory_rank_compare_func);
229 for (walk = feature_list; walk != NULL; walk = walk->next) {
230 GstElementFactory *factory;
233 factory = GST_ELEMENT_FACTORY (walk->data);
236 klass = gst_element_factory_get_klass (factory);
237 if (strcmp (klass, "Generic/Audio") == 0) {
238 gst_audio_mixer_filter_probe_feature (filter_func, factory,
239 &mixer_list, first, data);
242 if (first && mixer_list != NULL) {
243 GST_DEBUG ("Stopping after first found mixer, as requested");
248 gst_plugin_feature_list_free (feature_list);
250 return g_list_reverse (mixer_list);