Suppress deprecation warnings in selected files, for g_value_array_* mostly
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / mixerutils.c
1 /* GStreamer
2  * Copyright (C) 2003-2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * Copyright (C) 2005-2006 Tim-Philipp Müller <tim centricular net>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:gstaudiomixerutils
23  * @short_description:  utility functions to find available audio mixers
24  *                      from the plugin registry
25  *
26  * <refsect2>
27  * <para>
28  * Provides some utility functions to detect available audio mixers
29  * on the system.
30  * </para>
31  * </refsect2>
32  */
33
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
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include "mixerutils.h"
43
44 #include <gst/interfaces/propertyprobe.h>
45
46 #include <string.h>
47
48 static void
49 gst_audio_mixer_filter_do_filter (GstAudioMixerFilterFunc filter_func,
50     GstElementFactory * factory,
51     GstElement ** p_element, GList ** p_collection, gpointer user_data)
52 {
53   /* so, the element is a mixer, let's see if the caller wants it */
54   if (filter_func != NULL) {
55     if (filter_func (GST_MIXER (*p_element), user_data) == TRUE) {
56       *p_collection = g_list_prepend (*p_collection, *p_element);
57       /* do not set state back to NULL here on purpose, caller
58        * might want to keep the mixer open */
59       *p_element = NULL;
60     }
61   } else {
62     gst_element_set_state (*p_element, GST_STATE_NULL);
63     *p_collection = g_list_prepend (*p_collection, *p_element);
64     *p_element = NULL;
65   }
66
67   /* create new element for further probing if the old one was cleared */
68   if (*p_element == NULL) {
69     *p_element = gst_element_factory_create (factory, NULL);
70   }
71 }
72
73 static gboolean
74 gst_audio_mixer_filter_check_element (GstElement * element)
75 {
76   GstStateChangeReturn ret;
77
78   /* open device (only then we can know for sure whether it is a mixer) */
79   gst_element_set_state (element, GST_STATE_READY);
80   ret = gst_element_get_state (element, NULL, NULL, 1 * GST_SECOND);
81   if (ret != GST_STATE_CHANGE_SUCCESS) {
82     GST_DEBUG ("could not open device / set element to READY");
83     gst_element_set_state (element, GST_STATE_NULL);
84     return FALSE;
85   }
86
87   /* is this device a mixer? */
88   if (!GST_IS_MIXER (element)) {
89     GST_DEBUG ("element is not a mixer");
90     gst_element_set_state (element, GST_STATE_NULL);
91     return FALSE;
92   }
93
94   /* any tracks? */
95   if (!gst_mixer_list_tracks (GST_MIXER (element))) {
96     GST_DEBUG ("element is a mixer, but has no tracks");
97     gst_element_set_state (element, GST_STATE_NULL);
98     return FALSE;
99   }
100
101   GST_DEBUG ("element is a mixer with mixer tracks");
102   return TRUE;
103 }
104
105 static void
106 gst_audio_mixer_filter_probe_feature (GstAudioMixerFilterFunc filter_func,
107     GstElementFactory * factory,
108     GList ** p_collection, gboolean first, gpointer user_data)
109 {
110   GstElement *element;
111
112   GST_DEBUG ("probing %s ...", gst_element_factory_get_longname (factory));
113
114   /* create element */
115   element = gst_element_factory_create (factory, NULL);
116
117   if (element == NULL) {
118     GST_DEBUG ("could not create element from factory");
119     return;
120   }
121
122   GST_DEBUG ("created element %s (%p)", GST_ELEMENT_NAME (element), element);
123
124   if (GST_IS_PROPERTY_PROBE (element)) {
125     GstPropertyProbe *probe;
126     const GParamSpec *devspec;
127
128     probe = GST_PROPERTY_PROBE (element);
129
130     GST_DEBUG ("probing available devices ...");
131     if ((devspec = gst_property_probe_get_property (probe, "device"))) {
132       GValueArray *array;
133
134       if ((array = gst_property_probe_probe_and_get_values (probe, devspec))) {
135         guint n;
136
137         GST_DEBUG ("there are %d available devices", array->n_values);
138
139         /* set all devices and test for mixer */
140         for (n = 0; n < array->n_values; n++) {
141           GValue *device;
142
143           /* set this device */
144           device = g_value_array_get_nth (array, n);
145           g_object_set_property (G_OBJECT (element), "device", device);
146
147           GST_DEBUG ("trying device %s ..", g_value_get_string (device));
148
149           if (gst_audio_mixer_filter_check_element (element)) {
150             gst_audio_mixer_filter_do_filter (filter_func, factory, &element,
151                 p_collection, user_data);
152
153             if (first && *p_collection != NULL) {
154               GST_DEBUG ("Stopping after first found mixer, as requested");
155               break;
156             }
157           }
158         }
159         g_value_array_free (array);
160       }
161     }
162   } else {
163     GST_DEBUG ("element does not support the property probe interface");
164
165     if (gst_audio_mixer_filter_check_element (element)) {
166       gst_audio_mixer_filter_do_filter (filter_func, factory, &element,
167           p_collection, user_data);
168     }
169   }
170
171   if (element) {
172     gst_element_set_state (element, GST_STATE_NULL);
173     gst_object_unref (element);
174   }
175 }
176
177 static gint
178 element_factory_rank_compare_func (gconstpointer a, gconstpointer b)
179 {
180   gint rank_a = gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (a));
181   gint rank_b = gst_plugin_feature_get_rank (GST_PLUGIN_FEATURE (b));
182
183   /* make order chosen in the end more determinable */
184   if (rank_a == rank_b) {
185     const gchar *name_a = GST_PLUGIN_FEATURE_NAME (GST_PLUGIN_FEATURE (a));
186     const gchar *name_b = GST_PLUGIN_FEATURE_NAME (GST_PLUGIN_FEATURE (b));
187
188     return g_ascii_strcasecmp (name_a, name_b);
189   }
190
191   return rank_b - rank_a;
192 }
193
194 /**
195  * gst_audio_default_registry_mixer_filter:
196  * @filter_func: filter function, or #NULL
197  * @first: set to #TRUE if you only want the first suitable mixer element
198  * @user_data: user data to pass to the filter function
199  *
200  * Utility function to find audio mixer elements.
201  *
202  * Will traverse the default plugin registry in order of plugin rank and
203  * find usable audio mixer elements. The caller may optionally fine-tune
204  * the selection by specifying a filter function.
205  *
206  * Returns: a #GList of audio mixer #GstElement<!-- -->s. You must free each
207  *          element in the list by setting it to NULL state and calling
208  *          gst_object_unref(). After that the list itself should be freed
209  *          using g_list_free().
210  *
211  * Since: 0.10.2
212  */
213 GList *
214 gst_audio_default_registry_mixer_filter (GstAudioMixerFilterFunc filter_func,
215     gboolean first, gpointer data)
216 {
217   GList *mixer_list = NULL;
218   GList *feature_list;
219   GList *walk;
220
221   /* go through all elements of a certain class and check whether
222    * they implement a mixer. If so, add it to the list. */
223   feature_list = gst_registry_get_feature_list (gst_registry_get_default (),
224       GST_TYPE_ELEMENT_FACTORY);
225
226   feature_list = g_list_sort (feature_list, element_factory_rank_compare_func);
227
228   for (walk = feature_list; walk != NULL; walk = walk->next) {
229     GstElementFactory *factory;
230     const gchar *klass;
231
232     factory = GST_ELEMENT_FACTORY (walk->data);
233
234     /* check category */
235     klass = gst_element_factory_get_klass (factory);
236     if (strcmp (klass, "Generic/Audio") == 0) {
237       gst_audio_mixer_filter_probe_feature (filter_func, factory,
238           &mixer_list, first, data);
239     }
240
241     if (first && mixer_list != NULL) {
242       GST_DEBUG ("Stopping after first found mixer, as requested");
243       break;
244     }
245   }
246
247   gst_plugin_feature_list_free (feature_list);
248
249   return g_list_reverse (mixer_list);
250 }