1 /* Copyright (C) 2001 CodeFactory AB
2 * Copyright (C) 2001 Thomas Nyberg <thomas@codefactory.se>
3 * Copyright (C) 2001-2002 Andy Wingo <apwingo@eos.ncsu.edu>
4 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5 * Copyright (C) 2005 Tim-Philipp Müller <tim centricular net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "gstalsadeviceprobe.h"
27 #include "gst/interfaces/propertyprobe.h"
29 G_LOCK_DEFINE_STATIC (probe_lock);
32 gst_alsa_device_property_probe_get_properties (GstPropertyProbe * probe)
34 GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
35 static GList *list = NULL;
42 pspec = g_object_class_find_property (klass, "device");
43 list = g_list_append (NULL, pspec);
46 G_UNLOCK (probe_lock);
52 gst_alsa_get_device_list (snd_pcm_stream_t stream)
56 snd_ctl_card_info_t *info;
57 snd_pcm_info_t *pcminfo;
58 gboolean mixer = (stream == -1);
64 snd_ctl_card_info_malloc (&info);
65 snd_pcm_info_malloc (&pcminfo);
68 if (snd_card_next (&card) < 0 || card < 0) {
69 /* no soundcard found */
70 GST_WARNING ("No soundcard found");
77 g_snprintf (name, sizeof (name), "hw:%d", card);
78 if (snd_ctl_open (&handle, name, 0) < 0) {
81 if (snd_ctl_card_info (handle, info) < 0) {
82 snd_ctl_close (handle);
87 list = g_list_append (list, g_strdup (name));
93 snd_ctl_pcm_next_device (handle, &dev);
97 snd_pcm_info_set_device (pcminfo, dev);
98 snd_pcm_info_set_subdevice (pcminfo, 0);
99 snd_pcm_info_set_stream (pcminfo, stream);
100 if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
104 gst_device = g_strdup_printf ("hw:%d,%d", card, dev);
105 list = g_list_append (list, gst_device);
108 snd_ctl_close (handle);
110 if (snd_card_next (&card) < 0) {
116 snd_ctl_card_info_free (info);
117 snd_pcm_info_free (pcminfo);
123 gst_alsa_device_property_probe_probe_property (GstPropertyProbe * probe,
124 guint prop_id, const GParamSpec * pspec)
126 if (!g_str_equal (pspec->name, "device")) {
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
132 gst_alsa_device_property_probe_needs_probe (GstPropertyProbe * probe,
133 guint prop_id, const GParamSpec * pspec)
135 /* don't cache probed data */
140 gst_alsa_device_property_probe_get_values (GstPropertyProbe * probe,
141 guint prop_id, const GParamSpec * pspec)
143 GstElementClass *klass;
144 const GList *templates;
145 snd_pcm_stream_t mode = -1;
147 GValue value = { 0, };
150 if (!g_str_equal (pspec->name, "device")) {
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
155 klass = GST_ELEMENT_GET_CLASS (GST_ELEMENT (probe));
157 /* I'm pretty sure ALSA has a good way to do this. However, their cool
158 * auto-generated documentation is pretty much useless if you try to
159 * do function-wise look-ups. */
160 /* we assume one pad template at max [zero=mixer] */
161 templates = gst_element_class_get_pad_template_list (klass);
163 if (GST_PAD_TEMPLATE_DIRECTION (templates->data) == GST_PAD_SRC)
164 mode = SND_PCM_STREAM_CAPTURE;
166 mode = SND_PCM_STREAM_PLAYBACK;
169 list = gst_alsa_get_device_list (mode);
172 GST_LOG_OBJECT (probe, "No devices found");
176 array = g_value_array_new (g_list_length (list));
177 g_value_init (&value, G_TYPE_STRING);
178 for (l = list; l != NULL; l = l->next) {
179 GST_LOG_OBJECT (probe, "Found device: %s", (gchar *) l->data);
180 g_value_take_string (&value, (gchar *) l->data);
182 g_value_array_append (array, &value);
184 g_value_unset (&value);
191 gst_alsa_property_probe_interface_init (GstPropertyProbeInterface * iface)
193 iface->get_properties = gst_alsa_device_property_probe_get_properties;
194 iface->probe_property = gst_alsa_device_property_probe_probe_property;
195 iface->needs_probe = gst_alsa_device_property_probe_needs_probe;
196 iface->get_values = gst_alsa_device_property_probe_get_values;
200 gst_alsa_type_add_device_property_probe_interface (GType type)
202 static const GInterfaceInfo probe_iface_info = {
203 (GInterfaceInitFunc) gst_alsa_property_probe_interface_init,
208 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,