]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
direct3d11: catch texture mapping errors
[vlc] / modules / audio_output / oss.c
1 /*****************************************************************************
2  * oss.c: Open Sound System audio output plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VLC authors and VideoLAN
5  * Copyright (C) 2007-2012 RĂ©mi Denis-Courmont
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <math.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #ifdef HAVE_SOUNDCARD_H
37 # include <soundcard.h>
38 #else
39 # include <sys/soundcard.h>
40 #endif
41
42 #ifndef SNDCTL_DSP_HALT
43 # define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
44 #endif
45
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_fs.h>
49 #include <vlc_cpu.h>
50 #include <vlc_aout.h>
51
52 #define A52_FRAME_NB 1536
53
54 struct aout_sys_t
55 {
56     int fd;
57     audio_sample_format_t format;
58     bool starting;
59     bool soft_mute;
60     float soft_gain;
61     char *device;
62 };
63
64 #include "volume.h"
65
66 static int Open (vlc_object_t *);
67 static void Close (vlc_object_t *);
68
69 #define AUDIO_DEV_TEXT N_("Audio output device")
70 #define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
71
72 vlc_module_begin ()
73     set_shortname( "OSS" )
74     set_description (N_("Open Sound System audio output"))
75     set_category( CAT_AUDIO )
76     set_subcategory( SUBCAT_AUDIO_AOUT )
77     add_string ("oss-audio-device", "",
78                 AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
79     add_sw_gain ()
80     set_capability( "audio output", 100 )
81     set_callbacks (Open, Close)
82 vlc_module_end ()
83
84 static int TimeGet (audio_output_t *, mtime_t *);
85 static void Play (audio_output_t *, block_t *);
86 static void Pause (audio_output_t *, bool, mtime_t);
87 static void Flush (audio_output_t *, bool);
88
89 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
90 {
91     aout_sys_t* sys = aout->sys;
92
93     /* Open the device */
94     const char *device = sys->device;
95     if (device == NULL)
96         device = getenv ("OSS_AUDIODEV");
97     if (device == NULL)
98         device = "/dev/dsp";
99
100     int fd = vlc_open (device, O_WRONLY);
101     if (fd == -1)
102     {
103         msg_Err (aout, "cannot open OSS device %s: %s", device,
104                  vlc_strerror_c(errno));
105         return VLC_EGENERIC;
106     }
107     msg_Dbg (aout, "using OSS device: %s", device);
108
109     /* Select audio format */
110     int format;
111     bool spdif = false;
112
113     switch (fmt->i_format)
114     {
115 #ifdef AFMT_FLOAT
116         case VLC_CODEC_FL64:
117         case VLC_CODEC_FL32:
118             format = AFMT_FLOAT;
119             break;
120 #endif
121         case VLC_CODEC_S32N:
122             format = AFMT_S32_NE;
123             break;
124         case VLC_CODEC_S16N:
125             format = AFMT_S16_NE;
126             break;
127         case VLC_CODEC_U8:
128             format = AFMT_U8;
129             break;
130         default:
131             if (AOUT_FMT_SPDIF(fmt))
132                 spdif = var_InheritBool (aout, "spdif");
133             if (spdif)
134                 format = AFMT_AC3;
135 #ifdef AFMT_FLOAT
136             else if (HAVE_FPU)
137                 format = AFMT_FLOAT;
138 #endif
139             else
140                 format = AFMT_S16_NE;
141     }
142
143     if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
144     {
145         msg_Err (aout, "cannot set audio format 0x%X: %s", format,
146                  vlc_strerror_c(errno));
147         goto error;
148     }
149
150     switch (format)
151     {
152         case AFMT_U8:     fmt->i_format = VLC_CODEC_U8;   break;
153         case AFMT_S16_NE: fmt->i_format = VLC_CODEC_S16N; break;
154         case AFMT_S32_NE: fmt->i_format = VLC_CODEC_S32N; break;
155 #ifdef AFMT_FLOAT
156         case AFMT_FLOAT:  fmt->i_format = VLC_CODEC_FL32; break;
157 #endif
158         case AFMT_AC3:
159             if (spdif)
160             {
161                 fmt->i_format = VLC_CODEC_SPDIFL;
162                 break;
163             }
164         default:
165             msg_Err (aout, "unsupported audio format 0x%X", format);
166             goto error;
167     }
168
169     /* Select channels count */
170     int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
171     if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
172     {
173         msg_Err (aout, "cannot set %d channels: %s", channels,
174                  vlc_strerror_c(errno));
175         goto error;
176     }
177
178     switch (channels)
179     {
180         case 1: channels = AOUT_CHAN_CENTER;  break;
181         case 2: channels = AOUT_CHANS_STEREO; break;
182         case 4: channels = AOUT_CHANS_4_0;    break;
183         case 6: channels = AOUT_CHANS_5_1;    break;
184         case 8: channels = AOUT_CHANS_7_1;    break;
185         default:
186             msg_Err (aout, "unsupported channels count %d", channels);
187             goto error;
188     }
189
190     /* Select sample rate */
191     int rate = spdif ? 48000 : fmt->i_rate;
192     if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
193     {
194         msg_Err (aout, "cannot set %d Hz sample rate: %s", rate,
195                  vlc_strerror_c(errno));
196         goto error;
197     }
198
199     /* Setup audio_output_t */
200     aout->time_get = TimeGet;
201     aout->play = Play;
202     aout->pause = Pause;
203     aout->flush = Flush;
204
205     if (spdif)
206     {
207         fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
208         fmt->i_frame_length = A52_FRAME_NB;
209     }
210     else
211     {
212         fmt->i_rate = rate;
213         fmt->i_original_channels =
214         fmt->i_physical_channels = channels;
215     }
216     aout_FormatPrepare (fmt);
217
218     /* Select timing */
219     unsigned bytes;
220     if (spdif)
221         bytes = AOUT_SPDIF_SIZE;
222     else
223         bytes = fmt->i_rate / (CLOCK_FREQ / AOUT_MIN_PREPARE_TIME)
224                 * fmt->i_bytes_per_frame;
225     if (unlikely(bytes < 16))
226         bytes = 16;
227
228     int frag = (AOUT_MAX_ADVANCE_TIME / AOUT_MIN_PREPARE_TIME) << 16
229              | (32 - clz32(bytes - 1));
230     if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
231         msg_Err (aout, "cannot set 0x%08x fragment: %s", frag,
232                  vlc_strerror_c(errno));
233
234     sys->fd = fd;
235     aout_SoftVolumeStart (aout);
236     sys->starting = true;
237     sys->format = *fmt;
238     return VLC_SUCCESS;
239 error:
240     close (fd);
241     return VLC_EGENERIC;
242 }
243
244 static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
245 {
246     aout_sys_t *sys = aout->sys;
247     int delay;
248
249     if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
250     {
251         msg_Warn (aout, "cannot get delay: %s", vlc_strerror_c(errno));
252         return -1;
253     }
254
255     *pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
256                         / (sys->format.i_rate * sys->format.i_bytes_per_frame);
257     return 0;
258 }
259
260 /**
261  * Queues one audio buffer to the hardware.
262  */
263 static void Play (audio_output_t *aout, block_t *block)
264 {
265     aout_sys_t *sys = aout->sys;
266     int fd = sys->fd;
267
268     while (block->i_buffer > 0)
269     {
270         ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
271         if (bytes >= 0)
272         {
273             block->p_buffer += bytes;
274             block->i_buffer -= bytes;
275         }
276         else
277             msg_Err (aout, "cannot write samples: %s", vlc_strerror_c(errno));
278     }
279     block_Release (block);
280 }
281
282 /**
283  * Pauses/resumes the audio playback.
284  */
285 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
286 {
287     aout_sys_t *sys = aout->sys;
288     int fd = sys->fd;
289
290     (void) date;
291     ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
292 }
293
294 /**
295  * Flushes/drains the audio playback buffer.
296  */
297 static void Flush (audio_output_t *aout, bool wait)
298 {
299     aout_sys_t *sys = aout->sys;
300     int fd = sys->fd;
301
302     if (wait)
303         return; /* drain is implicit with OSS */
304     ioctl (fd, SNDCTL_DSP_HALT, NULL);
305 }
306
307 /**
308  * Releases the audio output device.
309  */
310 static void Stop (audio_output_t *aout)
311 {
312     aout_sys_t *sys = aout->sys;
313     int fd = sys->fd;
314
315     ioctl (fd, SNDCTL_DSP_HALT, NULL);
316     close (fd);
317     sys->fd = -1;
318 }
319
320 static int DevicesEnum (audio_output_t *aout)
321 {
322     int fd = vlc_open ("/dev/dsp", O_WRONLY);
323     if (fd == -1)
324         return -1;
325
326     oss_sysinfo si;
327     int n = -1;
328
329     if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
330     {
331         msg_Err (aout, "cannot get system infos: %s", vlc_strerror(errno));
332         goto out;
333     }
334
335     msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
336              si.version, si.versionnum, si.license);
337
338     for (int i = 0; i < si.numaudios; i++)
339     {
340         oss_audioinfo ai = { .dev = i };
341
342         if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
343         {
344             msg_Warn (aout, "cannot get device %d infos: %s", i,
345                       vlc_strerror_c(errno));
346             continue;
347         }
348         if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
349             continue;
350         if (!(ai.caps & PCM_CAP_OUTPUT))
351             continue;
352         if (!ai.enabled)
353             continue;
354
355         aout_HotplugReport (aout, ai.devnode, ai.name);
356         n++;
357     }
358 out:
359     close (fd);
360     return n;
361 }
362
363 static int DeviceSelect (audio_output_t *aout, const char *id)
364 {
365     aout_sys_t *sys = aout->sys;
366     char *path = NULL;
367
368     if (id != NULL)
369     {
370         path = strdup (id);
371         if (unlikely(path == NULL))
372             return -1;
373     }
374
375     free (sys->device);
376     sys->device = path;
377     aout_DeviceReport (aout, path);
378     aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
379     return 0;
380 }
381
382 static int Open (vlc_object_t *obj)
383 {
384     audio_output_t *aout = (audio_output_t *)obj;
385
386     aout_sys_t *sys = malloc (sizeof (*sys));
387     if(unlikely( sys == NULL ))
388         return VLC_ENOMEM;
389
390     sys->fd = -1;
391     sys->device = var_InheritString (aout, "oss-audio-device");
392
393     aout->sys = sys;
394     aout->start = Start;
395     aout->stop = Stop;
396     aout->device_select = DeviceSelect;
397     aout_DeviceReport (aout, sys->device);
398     aout_SoftVolumeInit (aout);
399
400     DevicesEnum (aout);
401     return VLC_SUCCESS;
402 }
403
404 static void Close (vlc_object_t *obj)
405 {
406     audio_output_t *aout = (audio_output_t *)obj;
407     aout_sys_t *sys = aout->sys;
408
409     free (sys->device);
410     free (sys);
411 }