Merging gst-python
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / sys / oss / gstosshelper.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstosshelper.c: OSS helper routines
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
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "gst/gst-i18n-plugin.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #ifdef HAVE_OSS_INCLUDE_IN_SYS
37 # include <sys/soundcard.h>
38 #else
39 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
40 #  include <soundcard.h>
41 # else
42 #  ifdef HAVE_OSS_INCLUDE_IN_MACHINE
43 #   include <machine/soundcard.h>
44 #  else
45 #   error "What to include?"
46 #  endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
47 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
48 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
49
50 #include "gstosshelper.h"
51
52 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
53 #define GST_CAT_DEFAULT oss_debug
54
55 typedef struct _GstOssProbe GstOssProbe;
56 struct _GstOssProbe
57 {
58   int fd;
59   int format;
60   int n_channels;
61   GArray *rates;
62   int min;
63   int max;
64 };
65
66 typedef struct _GstOssRange GstOssRange;
67 struct _GstOssRange
68 {
69   int min;
70   int max;
71 };
72
73 static GstStructure *gst_oss_helper_get_format_structure (unsigned int
74     format_bit);
75 static gboolean gst_oss_helper_rate_probe_check (GstOssProbe * probe);
76 static int gst_oss_helper_rate_check_rate (GstOssProbe * probe, int irate);
77 static void gst_oss_helper_rate_add_range (GQueue * queue, int min, int max);
78 static void gst_oss_helper_rate_add_rate (GArray * array, int rate);
79 static int gst_oss_helper_rate_int_compare (gconstpointer a, gconstpointer b);
80
81 GstCaps *
82 gst_oss_helper_probe_caps (gint fd)
83 {
84 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
85   const guint probe_formats[] = { AFMT_S16_LE, AFMT_U16_LE, AFMT_U8, AFMT_S8 };
86 #else
87   const guint probe_formats[] = { AFMT_S16_BE, AFMT_U16_BE, AFMT_U8, AFMT_S8 };
88 #endif
89   GstOssProbe *probe;
90   int i, f;
91   gboolean ret;
92   GstStructure *structure;
93   GstCaps *caps;
94
95   /* FIXME test make sure we're not currently playing */
96   /* FIXME test both mono and stereo */
97
98   caps = gst_caps_new_empty ();
99
100   /* assume that the most significant bit of format_mask is 0 */
101   for (f = 0; f < G_N_ELEMENTS (probe_formats); ++f) {
102     GValue rate_value = { 0 };
103
104     probe = g_new0 (GstOssProbe, 1);
105     probe->fd = fd;
106     probe->format = probe_formats[f];
107     /* FIXME: this is not working for all cards, see bug #518474 */
108     probe->n_channels = 2;
109
110     ret = gst_oss_helper_rate_probe_check (probe);
111     if (probe->min == -1 || probe->max == -1) {
112       g_array_free (probe->rates, TRUE);
113       g_free (probe);
114       continue;
115     }
116
117     if (ret) {
118       GValue value = { 0 };
119
120       g_array_sort (probe->rates, gst_oss_helper_rate_int_compare);
121
122       g_value_init (&rate_value, GST_TYPE_LIST);
123       g_value_init (&value, G_TYPE_INT);
124
125       for (i = 0; i < probe->rates->len; i++) {
126         g_value_set_int (&value, g_array_index (probe->rates, int, i));
127
128         gst_value_list_append_value (&rate_value, &value);
129       }
130
131       g_value_unset (&value);
132     } else {
133       /* one big range */
134       g_value_init (&rate_value, GST_TYPE_INT_RANGE);
135       gst_value_set_int_range (&rate_value, probe->min, probe->max);
136     }
137
138     g_array_free (probe->rates, TRUE);
139     g_free (probe);
140
141     structure = gst_oss_helper_get_format_structure (probe_formats[f]);
142     gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
143     gst_structure_set_value (structure, "rate", &rate_value);
144     g_value_unset (&rate_value);
145
146     gst_caps_append_structure (caps, structure);
147   }
148
149   if (gst_caps_is_empty (caps)) {
150     /* fixme: make user-visible */
151     GST_WARNING ("Your OSS device could not be probed correctly");
152   } else {
153     caps = gst_caps_simplify (caps);
154   }
155
156   GST_DEBUG ("probed caps: %" GST_PTR_FORMAT, caps);
157
158   return caps;
159 }
160
161 static GstStructure *
162 gst_oss_helper_get_format_structure (unsigned int format_bit)
163 {
164   GstStructure *structure;
165   const gchar *format;
166
167   switch (format_bit) {
168     case AFMT_U8:
169       format = "U8";
170       break;
171     case AFMT_S16_LE:
172       format = "S16LE";
173       break;
174     case AFMT_S16_BE:
175       format = "S16BE";
176       break;
177     case AFMT_S8:
178       format = "S8";
179       break;
180     case AFMT_U16_LE:
181       format = "U16LE";
182       break;
183     case AFMT_U16_BE:
184       format = "U16BE";
185       break;
186     default:
187       g_assert_not_reached ();
188       return NULL;
189   }
190
191   structure = gst_structure_new ("audio/x-raw",
192       "format", G_TYPE_STRING, format,
193       "layout", G_TYPE_STRING, "interleaved", NULL);
194
195   return structure;
196 }
197
198 static gboolean
199 gst_oss_helper_rate_probe_check (GstOssProbe * probe)
200 {
201   GstOssRange *range;
202   GQueue *ranges;
203   int exact_rates = 0;
204   gboolean checking_exact_rates = TRUE;
205   int n_checks = 0;
206   gboolean result = TRUE;
207
208   ranges = g_queue_new ();
209
210   probe->rates = g_array_new (FALSE, FALSE, sizeof (int));
211
212   probe->min = gst_oss_helper_rate_check_rate (probe, 1000);
213   n_checks++;
214   probe->max = gst_oss_helper_rate_check_rate (probe, 100000);
215   /* a little bug workaround */
216   {
217     int max;
218
219     max = gst_oss_helper_rate_check_rate (probe, 48000);
220     if (max > probe->max) {
221       GST_ERROR
222           ("Driver bug recognized (driver does not round rates correctly).  Please file a bug report.");
223       probe->max = max;
224     }
225   }
226   n_checks++;
227   if (probe->min == -1 || probe->max == -1) {
228     /* This is a workaround for drivers that return -EINVAL (or another
229      * error) for rates outside of [8000,48000].  If this fails, the
230      * driver is seriously buggy, and probably doesn't work with other
231      * media libraries/apps.  */
232     probe->min = gst_oss_helper_rate_check_rate (probe, 8000);
233     probe->max = gst_oss_helper_rate_check_rate (probe, 48000);
234   }
235   if (probe->min == -1 || probe->max == -1) {
236     GST_DEBUG ("unexpected check_rate error");
237     return FALSE;
238   }
239   gst_oss_helper_rate_add_range (ranges, probe->min + 1, probe->max - 1);
240
241   while ((range = g_queue_pop_head (ranges))) {
242     int min1;
243     int max1;
244     int mid;
245     int mid_ret;
246
247     GST_DEBUG ("checking [%d,%d]", range->min, range->max);
248
249     mid = (range->min + range->max) / 2;
250     mid_ret = gst_oss_helper_rate_check_rate (probe, mid);
251     if (mid_ret == -1) {
252       /* FIXME ioctl returned an error.  do something */
253       GST_DEBUG ("unexpected check_rate error");
254     }
255     n_checks++;
256
257     if (mid == mid_ret && checking_exact_rates) {
258       int max_exact_matches = 20;
259
260       exact_rates++;
261       if (exact_rates > max_exact_matches) {
262         GST_DEBUG ("got %d exact rates, assuming all are exact",
263             max_exact_matches);
264         result = FALSE;
265         g_free (range);
266         break;
267       }
268     } else {
269       checking_exact_rates = FALSE;
270     }
271
272     /* Assume that the rate is arithmetically rounded to the nearest
273      * supported rate. */
274     if (mid == mid_ret) {
275       min1 = mid - 1;
276       max1 = mid + 1;
277     } else {
278       if (mid < mid_ret) {
279         min1 = mid - (mid_ret - mid);
280         max1 = mid_ret + 1;
281       } else {
282         min1 = mid_ret - 1;
283         max1 = mid + (mid - mid_ret);
284       }
285     }
286
287     gst_oss_helper_rate_add_range (ranges, range->min, min1);
288     gst_oss_helper_rate_add_range (ranges, max1, range->max);
289
290     g_free (range);
291   }
292
293   while ((range = g_queue_pop_head (ranges))) {
294     g_free (range);
295   }
296   g_queue_free (ranges);
297
298   return result;
299 }
300
301 static void
302 gst_oss_helper_rate_add_range (GQueue * queue, int min, int max)
303 {
304   if (min <= max) {
305     GstOssRange *range = g_new0 (GstOssRange, 1);
306
307     range->min = min;
308     range->max = max;
309
310     g_queue_push_tail (queue, range);
311     /* push_head also works, but has different probing behavior */
312     /*g_queue_push_head (queue, range); */
313   }
314 }
315
316 static int
317 gst_oss_helper_rate_check_rate (GstOssProbe * probe, int irate)
318 {
319   int rate;
320   int format;
321   int n_channels;
322   int ret;
323
324   rate = irate;
325   format = probe->format;
326   n_channels = probe->n_channels;
327
328   GST_LOG ("checking format %d, channels %d, rate %d",
329       format, n_channels, rate);
330   ret = ioctl (probe->fd, SNDCTL_DSP_SETFMT, &format);
331   if (ret < 0 || format != probe->format) {
332     GST_DEBUG ("unsupported format: %d (%d)", probe->format, format);
333     return -1;
334   }
335   ret = ioctl (probe->fd, SNDCTL_DSP_CHANNELS, &n_channels);
336   if (ret < 0 || n_channels != probe->n_channels) {
337     GST_DEBUG ("unsupported channels: %d (%d)", probe->n_channels, n_channels);
338     return -1;
339   }
340   ret = ioctl (probe->fd, SNDCTL_DSP_SPEED, &rate);
341   if (ret < 0) {
342     GST_DEBUG ("unsupported rate: %d (%d)", irate, rate);
343     return -1;
344   }
345
346   GST_DEBUG ("rate %d -> %d", irate, rate);
347
348   if (rate == irate - 1 || rate == irate + 1) {
349     rate = irate;
350   }
351   gst_oss_helper_rate_add_rate (probe->rates, rate);
352   return rate;
353 }
354
355 static void
356 gst_oss_helper_rate_add_rate (GArray * array, int rate)
357 {
358   int i;
359   int val;
360
361   for (i = 0; i < array->len; i++) {
362     val = g_array_index (array, int, i);
363
364     if (val == rate)
365       return;
366   }
367   GST_DEBUG ("supported rate: %d", rate);
368   g_array_append_val (array, rate);
369 }
370
371 static int
372 gst_oss_helper_rate_int_compare (gconstpointer a, gconstpointer b)
373 {
374   const int *va = (const int *) a;
375   const int *vb = (const int *) b;
376
377   if (*va < *vb)
378     return -1;
379   if (*va > *vb)
380     return 1;
381   return 0;
382 }
383
384 gchar *
385 gst_oss_helper_get_card_name (const gchar * mixer_name)
386 {
387 #ifdef SOUND_MIXER_INFO
388   struct mixer_info minfo;
389 #endif
390   gint fd;
391   gchar *name = NULL;
392
393   GST_INFO ("Opening mixer for device %s", mixer_name);
394   fd = open (mixer_name, O_RDWR);
395   if (fd == -1)
396     goto open_failed;
397
398   /* get name, not fatal */
399 #ifdef SOUND_MIXER_INFO
400   if (ioctl (fd, SOUND_MIXER_INFO, &minfo) == 0) {
401     name = g_strdup (minfo.name);
402     GST_INFO ("Card name = %s", GST_STR_NULL (name));
403   } else
404 #endif
405   {
406     name = g_strdup ("Unknown");
407     GST_INFO ("Unknown card name");
408   }
409
410   close (fd);
411   return name;
412
413   /* ERRORS */
414 open_failed:
415   {
416     /* this is valid. OSS devices don't need to expose a mixer */
417     GST_DEBUG ("Failed to open mixer device %s, mixing disabled: %s",
418         mixer_name, strerror (errno));
419     return NULL;
420   }
421 }