tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / ext / alsa / gstalsadeviceprobe.c
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>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstalsadeviceprobe.h"
27 #include "gst/interfaces/propertyprobe.h"
28
29 G_LOCK_DEFINE_STATIC (probe_lock);
30
31 static const GList *
32 gst_alsa_device_property_probe_get_properties (GstPropertyProbe * probe)
33 {
34   GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
35   static GList *list = NULL;
36
37   /* well, not perfect, but better than no locking at all.
38    * In the worst case we leak a list node, so who cares? */
39   G_LOCK (probe_lock);
40
41   if (!list) {
42     GParamSpec *pspec;
43
44     pspec = g_object_class_find_property (klass, "device");
45     list = g_list_append (NULL, pspec);
46   }
47
48   G_UNLOCK (probe_lock);
49
50   return list;
51 }
52
53 static GList *
54 gst_alsa_get_device_list (snd_pcm_stream_t stream)
55 {
56   snd_ctl_t *handle;
57   int card, dev;
58   snd_ctl_card_info_t *info;
59   snd_pcm_info_t *pcminfo;
60   gboolean mixer = (stream == -1);
61   GList *list = NULL;
62
63   if (stream == -1)
64     stream = 0;
65
66   snd_ctl_card_info_malloc (&info);
67   snd_pcm_info_malloc (&pcminfo);
68   card = -1;
69
70   if (snd_card_next (&card) < 0 || card < 0) {
71     /* no soundcard found */
72     GST_WARNING ("No soundcard found");
73     goto beach;
74   }
75
76   while (card >= 0) {
77     gchar name[32];
78
79     g_snprintf (name, sizeof (name), "hw:%d", card);
80     if (snd_ctl_open (&handle, name, 0) < 0) {
81       goto next_card;
82     }
83     if (snd_ctl_card_info (handle, info) < 0) {
84       snd_ctl_close (handle);
85       goto next_card;
86     }
87
88     if (mixer) {
89       list = g_list_append (list, g_strdup (name));
90     } else {
91       dev = -1;
92       while (1) {
93         gchar *gst_device;
94
95         snd_ctl_pcm_next_device (handle, &dev);
96
97         if (dev < 0)
98           break;
99         snd_pcm_info_set_device (pcminfo, dev);
100         snd_pcm_info_set_subdevice (pcminfo, 0);
101         snd_pcm_info_set_stream (pcminfo, stream);
102         if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
103           continue;
104         }
105
106         gst_device = g_strdup_printf ("hw:%d,%d", card, dev);
107         list = g_list_append (list, gst_device);
108       }
109     }
110     snd_ctl_close (handle);
111   next_card:
112     if (snd_card_next (&card) < 0) {
113       break;
114     }
115   }
116
117 beach:
118   snd_ctl_card_info_free (info);
119   snd_pcm_info_free (pcminfo);
120
121   return list;
122 }
123
124 static void
125 gst_alsa_device_property_probe_probe_property (GstPropertyProbe * probe,
126     guint prop_id, const GParamSpec * pspec)
127 {
128   if (!g_str_equal (pspec->name, "device")) {
129     G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
130   }
131 }
132
133 static gboolean
134 gst_alsa_device_property_probe_needs_probe (GstPropertyProbe * probe,
135     guint prop_id, const GParamSpec * pspec)
136 {
137   /* don't cache probed data */
138   return TRUE;
139 }
140
141 static GValueArray *
142 gst_alsa_device_property_probe_get_values (GstPropertyProbe * probe,
143     guint prop_id, const GParamSpec * pspec)
144 {
145   GstElementClass *klass;
146   const GList *templates;
147   snd_pcm_stream_t mode = -1;
148   GValueArray *array;
149   GValue value = { 0, };
150   GList *l, *list;
151
152   if (!g_str_equal (pspec->name, "device")) {
153     G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
154     return NULL;
155   }
156
157   klass = GST_ELEMENT_GET_CLASS (GST_ELEMENT (probe));
158
159   /* I'm pretty sure ALSA has a good way to do this. However, their cool
160    * auto-generated documentation is pretty much useless if you try to
161    * do function-wise look-ups. */
162   /* we assume one pad template at max [zero=mixer] */
163   templates = gst_element_class_get_pad_template_list (klass);
164   if (templates) {
165     if (GST_PAD_TEMPLATE_DIRECTION (templates->data) == GST_PAD_SRC)
166       mode = SND_PCM_STREAM_CAPTURE;
167     else
168       mode = SND_PCM_STREAM_PLAYBACK;
169   }
170
171   list = gst_alsa_get_device_list (mode);
172
173   if (list == NULL) {
174     GST_LOG_OBJECT (probe, "No devices found");
175     return NULL;
176   }
177
178   array = g_value_array_new (g_list_length (list));
179   g_value_init (&value, G_TYPE_STRING);
180   for (l = list; l != NULL; l = l->next) {
181     GST_LOG_OBJECT (probe, "Found device: %s", (gchar *) l->data);
182     g_value_take_string (&value, (gchar *) l->data);
183     l->data = NULL;
184     g_value_array_append (array, &value);
185   }
186   g_value_unset (&value);
187   g_list_free (list);
188
189   return array;
190 }
191
192 static void
193 gst_alsa_property_probe_interface_init (GstPropertyProbeInterface * iface)
194 {
195   iface->get_properties = gst_alsa_device_property_probe_get_properties;
196   iface->probe_property = gst_alsa_device_property_probe_probe_property;
197   iface->needs_probe = gst_alsa_device_property_probe_needs_probe;
198   iface->get_values = gst_alsa_device_property_probe_get_values;
199 }
200
201 void
202 gst_alsa_type_add_device_property_probe_interface (GType type)
203 {
204   static const GInterfaceInfo probe_iface_info = {
205     (GInterfaceInitFunc) gst_alsa_property_probe_interface_init,
206     NULL,
207     NULL,
208   };
209
210   g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
211       &probe_iface_info);
212 }