• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2018 Thibault Saunier <tsaunier@igalia.com>
3  *
4  * alsadeviceprovider.c: alsa device probing and monitoring
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "gstalsadeviceprovider.h"
27 #include <string.h>
28 #include <gst/gst.h>
29 
30 
31 static GstDevice *gst_alsa_device_new (const gchar * device_name,
32     GstCaps * caps, const gchar * internal_name, snd_pcm_stream_t stream,
33     GstStructure * properties);
34 
35 G_DEFINE_TYPE (GstAlsaDeviceProvider, gst_alsa_device_provider,
36     GST_TYPE_DEVICE_PROVIDER);
37 
38 static GstStaticCaps alsa_caps = GST_STATIC_CAPS ("audio/x-raw, "
39     "format = (string) " GST_AUDIO_FORMATS_ALL ", "
40     "layout = (string) interleaved, "
41     "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
42     PASSTHROUGH_CAPS);
43 
44 static GstDevice *
add_device(GstDeviceProvider * provider,snd_ctl_t * info,snd_pcm_stream_t stream,gint card,gint dev)45 add_device (GstDeviceProvider * provider, snd_ctl_t * info,
46     snd_pcm_stream_t stream, gint card, gint dev)
47 {
48   GstCaps *caps, *template;
49   GstDevice *device;
50   snd_pcm_t *handle;
51   snd_ctl_card_info_t *card_info;
52   GstStructure *props;
53   gchar *card_name, *longname = NULL;
54   gchar *device_name = g_strdup_printf ("hw:%d,%d", card, dev);
55 
56   if (snd_pcm_open (&handle, device_name, stream, SND_PCM_NONBLOCK) < 0) {
57     GST_ERROR_OBJECT (provider, "Could not open device %s for inspection!",
58         device_name);
59     g_free (device_name);
60 
61     return NULL;
62   }
63 
64   template = gst_static_caps_get (&alsa_caps);
65   caps = gst_alsa_probe_supported_formats (GST_OBJECT (provider),
66       device_name, handle, template);
67   gst_caps_unref (template);
68 
69   snd_card_get_name (card, &card_name);
70   props = gst_structure_new ("alsa-proplist",
71       "device.api", G_TYPE_STRING, "alsa",
72       "device.class", G_TYPE_STRING, "sound",
73       "alsa.card", G_TYPE_INT, card,
74       "alsa.card_name", G_TYPE_STRING, card_name, NULL);
75   g_free (card_name);
76 
77   snd_ctl_card_info_alloca (&card_info);
78   if (snd_ctl_card_info (info, card_info) == 0) {
79     gst_structure_set (props,
80         "alsa.driver_name", G_TYPE_STRING,
81         snd_ctl_card_info_get_driver (card_info), "alsa.name", G_TYPE_STRING,
82         snd_ctl_card_info_get_name (card_info), "alsa.id", G_TYPE_STRING,
83         snd_ctl_card_info_get_id (card_info), "alsa.mixername", G_TYPE_STRING,
84         snd_ctl_card_info_get_mixername (card_info), "alsa.components",
85         G_TYPE_STRING, snd_ctl_card_info_get_components (card_info), NULL);
86 
87     snd_ctl_card_info_clear (card_info);
88   }
89 
90   snd_card_get_longname (card, &longname);
91   device = gst_alsa_device_new (longname, caps, device_name, stream, props);
92 
93   snd_pcm_close (handle);
94 
95   return device;
96 }
97 
98 static GList *
gst_alsa_device_provider_probe(GstDeviceProvider * provider)99 gst_alsa_device_provider_probe (GstDeviceProvider * provider)
100 {
101   snd_ctl_t *handle;
102   int card, dev;
103   snd_ctl_card_info_t *info;
104   snd_pcm_info_t *pcminfo;
105   GList *list = NULL;
106   gint i;
107   gint streams[] = { SND_PCM_STREAM_CAPTURE, SND_PCM_STREAM_PLAYBACK };
108   snd_pcm_stream_t stream;
109 
110   GST_INFO_OBJECT (provider, "Probing alsa devices");
111   snd_ctl_card_info_malloc (&info);
112   snd_pcm_info_malloc (&pcminfo);
113 
114   for (i = 0; i < G_N_ELEMENTS (streams); i++) {
115     card = -1;
116     stream = streams[i];
117 
118     if (snd_card_next (&card) < 0 || card < 0) {
119       /* no soundcard found */
120       GST_WARNING_OBJECT (provider, "No soundcard found");
121       goto beach;
122     }
123 
124     while (card >= 0) {
125       gchar name[32];
126 
127       g_snprintf (name, sizeof (name), "hw:%d", card);
128       if (snd_ctl_open (&handle, name, 0) < 0)
129         goto next_card;
130 
131       if (snd_ctl_card_info (handle, info) < 0) {
132         snd_ctl_close (handle);
133         goto next_card;
134       }
135 
136       dev = -1;
137       while (1) {
138         GstDevice *device;
139         snd_ctl_pcm_next_device (handle, &dev);
140 
141         if (dev < 0)
142           break;
143         snd_pcm_info_set_device (pcminfo, dev);
144         snd_pcm_info_set_subdevice (pcminfo, 0);
145         snd_pcm_info_set_stream (pcminfo, stream);
146         if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
147           continue;
148         }
149 
150         device = add_device (provider, handle, stream, card, dev);
151         if (device)
152           list = g_list_prepend (list, device);
153       }
154       snd_ctl_close (handle);
155     next_card:
156       if (snd_card_next (&card) < 0) {
157         break;
158       }
159     }
160   }
161 
162 beach:
163   snd_ctl_card_info_free (info);
164   snd_pcm_info_free (pcminfo);
165 
166   return list;
167 }
168 
169 
170 enum
171 {
172   PROP_0,
173   PROP_LAST
174 };
175 
176 
177 static void
gst_alsa_device_provider_class_init(GstAlsaDeviceProviderClass * klass)178 gst_alsa_device_provider_class_init (GstAlsaDeviceProviderClass * klass)
179 {
180   GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
181 
182   dm_class->probe = gst_alsa_device_provider_probe;
183 
184   gst_device_provider_class_set_static_metadata (dm_class,
185       "ALSA Device Provider", "Sink/Source/Audio",
186       "List and provides Alsa source and sink devices",
187       "Thibault Saunier <tsaunier@igalia.com>");
188 }
189 
190 static void
gst_alsa_device_provider_init(GstAlsaDeviceProvider * self)191 gst_alsa_device_provider_init (GstAlsaDeviceProvider * self)
192 {
193 }
194 
195 /*** GstAlsaDevice implementation ******/
196 enum
197 {
198   PROP_INTERNAL_NAME = 1,
199 };
200 
201 
202 G_DEFINE_TYPE (GstAlsaDevice, gst_alsa_device, GST_TYPE_DEVICE);
203 
204 static GstElement *
gst_alsa_device_create_element(GstDevice * device,const gchar * name)205 gst_alsa_device_create_element (GstDevice * device, const gchar * name)
206 {
207   GstAlsaDevice *alsa_dev = GST_ALSA_DEVICE (device);
208   GstElement *elem;
209 
210   elem = gst_element_factory_make (alsa_dev->element, name);
211   g_object_set (elem, "device", alsa_dev->internal_name, NULL);
212 
213   return elem;
214 }
215 
216 static gboolean
gst_alsa_device_reconfigure_element(GstDevice * device,GstElement * element)217 gst_alsa_device_reconfigure_element (GstDevice * device, GstElement * element)
218 {
219   GstAlsaDevice *alsa_dev = GST_ALSA_DEVICE (device);
220 
221   g_object_set (element, "device", alsa_dev->internal_name, NULL);
222 
223   return TRUE;
224 }
225 
226 
227 static void
gst_alsa_device_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)228 gst_alsa_device_get_property (GObject * object, guint prop_id,
229     GValue * value, GParamSpec * pspec)
230 {
231   GstAlsaDevice *device;
232 
233   device = GST_ALSA_DEVICE_CAST (object);
234 
235   switch (prop_id) {
236     case PROP_INTERNAL_NAME:
237       g_value_set_string (value, device->internal_name);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244 
245 static void
gst_alsa_device_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)246 gst_alsa_device_set_property (GObject * object, guint prop_id,
247     const GValue * value, GParamSpec * pspec)
248 {
249   GstAlsaDevice *device;
250 
251   device = GST_ALSA_DEVICE_CAST (object);
252 
253   switch (prop_id) {
254     case PROP_INTERNAL_NAME:
255       device->internal_name = g_value_dup_string (value);
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262 
263 static void
gst_alsa_device_finalize(GObject * object)264 gst_alsa_device_finalize (GObject * object)
265 {
266   GstAlsaDevice *device = GST_ALSA_DEVICE (object);
267 
268   g_free (device->internal_name);
269 
270   G_OBJECT_CLASS (gst_alsa_device_parent_class)->finalize (object);
271 }
272 
273 static void
gst_alsa_device_class_init(GstAlsaDeviceClass * klass)274 gst_alsa_device_class_init (GstAlsaDeviceClass * klass)
275 {
276   GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
277   GObjectClass *object_class = G_OBJECT_CLASS (klass);
278 
279   dev_class->create_element = gst_alsa_device_create_element;
280   dev_class->reconfigure_element = gst_alsa_device_reconfigure_element;
281 
282   object_class->get_property = gst_alsa_device_get_property;
283   object_class->set_property = gst_alsa_device_set_property;
284   object_class->finalize = gst_alsa_device_finalize;
285 
286   g_object_class_install_property (object_class, PROP_INTERNAL_NAME,
287       g_param_spec_string ("internal-name", "Internal AlsaAudio device name",
288           "The internal name of the AlsaAudio device", "",
289           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
290 }
291 
292 static void
gst_alsa_device_init(GstAlsaDevice * device)293 gst_alsa_device_init (GstAlsaDevice * device)
294 {
295 }
296 
297 /* Takes ownership of @caps and @props */
298 static GstDevice *
gst_alsa_device_new(const gchar * device_name,GstCaps * caps,const gchar * internal_name,snd_pcm_stream_t stream,GstStructure * props)299 gst_alsa_device_new (const gchar * device_name,
300     GstCaps * caps, const gchar * internal_name,
301     snd_pcm_stream_t stream, GstStructure * props)
302 {
303   GstAlsaDevice *gstdev;
304   const gchar *element = NULL;
305   const gchar *klass = NULL;
306 
307   g_return_val_if_fail (device_name, NULL);
308   g_return_val_if_fail (internal_name, NULL);
309   g_return_val_if_fail (caps, NULL);
310 
311   switch (stream) {
312     case SND_PCM_STREAM_CAPTURE:
313       element = "alsasrc";
314       klass = "Audio/Source";
315       break;
316     case SND_PCM_STREAM_PLAYBACK:
317       element = "alsasink";
318       klass = "Audio/Sink";
319       break;
320     default:
321       g_assert_not_reached ();
322       break;
323   }
324 
325   gstdev = g_object_new (GST_TYPE_ALSA_DEVICE,
326       "display-name", device_name, "caps", caps, "device-class", klass,
327       "internal-name", internal_name, "properties", props, NULL);
328 
329   gstdev->stream = stream;
330   gstdev->element = element;
331 
332   gst_structure_free (props);
333   gst_caps_unref (caps);
334 
335   return GST_DEVICE (gstdev);
336 }
337