]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
OSX: fix crash when no SD are found
[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 <sys/types.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #ifdef HAVE_SOUNDCARD_H
36 # include <soundcard.h>
37 #else
38 # include <sys/soundcard.h>
39 #endif
40
41 #ifndef SNDCTL_DSP_HALT
42 # define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
43 #endif
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_fs.h>
48 #include <vlc_cpu.h>
49 #include <vlc_aout.h>
50
51 #define A52_FRAME_NB 1536
52
53 struct aout_sys_t
54 {
55     int fd;
56     audio_sample_format_t format;
57     bool starting;
58
59     bool mute;
60     uint8_t level;
61     char *device;
62 };
63
64 static int Open (vlc_object_t *);
65 static void Close (vlc_object_t *);
66
67 #define AUDIO_DEV_TEXT N_("Audio output device")
68 #define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
69
70 vlc_module_begin ()
71     set_shortname( "OSS" )
72     set_description (N_("Open Sound System audio output"))
73     set_category( CAT_AUDIO )
74     set_subcategory( SUBCAT_AUDIO_AOUT )
75     add_string ("oss-audio-device", "",
76                 AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
77     set_capability( "audio output", 100 )
78     set_callbacks (Open, Close)
79 vlc_module_end ()
80
81 static int TimeGet (audio_output_t *, mtime_t *);
82 static void Play (audio_output_t *, block_t *);
83 static void Pause (audio_output_t *, bool, mtime_t);
84 static void Flush (audio_output_t *, bool);
85 static int VolumeSync (audio_output_t *);
86
87 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
88 {
89     aout_sys_t* sys = aout->sys;
90
91     /* Open the device */
92     const char *device = sys->device;
93     if (device == NULL)
94         device = getenv ("OSS_AUDIODEV");
95     if (device == NULL)
96         device = "/dev/dsp";
97
98     int fd = vlc_open (device, O_WRONLY);
99     if (fd == -1)
100     {
101         msg_Err (aout, "cannot open OSS device %s: %m", device);
102         return VLC_EGENERIC;
103     }
104     sys->fd = fd;
105     msg_Dbg (aout, "using OSS device: %s", device);
106
107     /* Select audio format */
108     int format;
109     bool spdif = false;
110
111     switch (fmt->i_format)
112     {
113 #ifdef AFMT_FLOAT
114         case VLC_CODEC_FL64:
115         case VLC_CODEC_FL32:
116             format = AFMT_FLOAT;
117             break;
118 #endif
119         case VLC_CODEC_S32N:
120             format = AFMT_S32_NE;
121             break;
122         case VLC_CODEC_S16N:
123             format = AFMT_S16_NE;
124             break;
125         case VLC_CODEC_U8:
126             format = AFMT_U8;
127             break;
128         default:
129             if (AOUT_FMT_SPDIF(fmt))
130                 spdif = var_InheritBool (aout, "spdif");
131             if (spdif)
132                 format = AFMT_AC3;
133 #ifdef AFMT_FLOAT
134             else if (HAVE_FPU)
135                 format = AFMT_FLOAT;
136 #endif
137             else
138                 format = AFMT_S16_NE;
139     }
140
141     if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
142     {
143         msg_Err (aout, "cannot set audio format 0x%X: %m", format);
144         goto error;
145     }
146
147     switch (format)
148     {
149         case AFMT_U8:     fmt->i_format = VLC_CODEC_U8;   break;
150         case AFMT_S16_NE: fmt->i_format = VLC_CODEC_S16N; break;
151         case AFMT_S32_NE: fmt->i_format = VLC_CODEC_S32N; break;
152 #ifdef AFMT_FLOAT
153         case AFMT_FLOAT:  fmt->i_format = VLC_CODEC_FL32; break;
154 #endif
155         case AFMT_AC3:
156             if (spdif)
157             {
158                 fmt->i_format = VLC_CODEC_SPDIFL;
159                 break;
160             }
161         default:
162             msg_Err (aout, "unsupported audio format 0x%X", format);
163             goto error;
164     }
165
166     /* Select channels count */
167     int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
168     if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
169     {
170         msg_Err (aout, "cannot set %d channels: %m", channels);
171         goto error;
172     }
173
174     switch (channels)
175     {
176         case 1: channels = AOUT_CHAN_CENTER;  break;
177         case 2: channels = AOUT_CHANS_STEREO; break;
178         case 4: channels = AOUT_CHANS_4_0;    break;
179         case 6: channels = AOUT_CHANS_5_1;    break;
180         case 8: channels = AOUT_CHANS_7_1;    break;
181         default:
182             msg_Err (aout, "unsupported channels count %d", channels);
183             goto error;
184     }
185
186     /* Select sample rate */
187     int rate = spdif ? 48000 : fmt->i_rate;
188     if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
189     {
190         msg_Err (aout, "cannot set %d Hz sample rate: %m", rate);
191         goto error;
192     }
193
194     /* Setup audio_output_t */
195     aout->time_get = TimeGet;
196     aout->play = Play;
197     aout->pause = Pause;
198     aout->flush = Flush;
199
200     if (spdif)
201     {
202         fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
203         fmt->i_frame_length = A52_FRAME_NB;
204     }
205     else
206     {
207         fmt->i_rate = rate;
208         fmt->i_original_channels =
209         fmt->i_physical_channels = channels;
210     }
211
212     VolumeSync (aout);
213     sys->starting = true;
214     sys->format = *fmt;
215     return VLC_SUCCESS;
216 error:
217     close (fd);
218     return VLC_EGENERIC;
219 }
220
221 static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
222 {
223     aout_sys_t *sys = aout->sys;
224     int delay;
225
226     if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
227     {
228         msg_Warn (aout, "cannot get delay: %m");
229         return -1;
230     }
231
232     *pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
233                         / (sys->format.i_rate * sys->format.i_bytes_per_frame);
234     return 0;
235 }
236
237 /**
238  * Queues one audio buffer to the hardware.
239  */
240 static void Play (audio_output_t *aout, block_t *block)
241 {
242     aout_sys_t *sys = aout->sys;
243     int fd = sys->fd;
244
245     while (block->i_buffer > 0)
246     {
247         ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
248         if (bytes >= 0)
249         {
250             block->p_buffer += bytes;
251             block->i_buffer -= bytes;
252         }
253         else
254             msg_Err (aout, "cannot write samples: %m");
255     }
256     block_Release (block);
257
258     /* Dumb OSS cannot send any kind of events for this... */
259     VolumeSync (aout);
260 }
261
262 /**
263  * Pauses/resumes the audio playback.
264  */
265 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
266 {
267     aout_sys_t *sys = aout->sys;
268     int fd = sys->fd;
269
270     (void) date;
271     ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
272 }
273
274 /**
275  * Flushes/drains the audio playback buffer.
276  */
277 static void Flush (audio_output_t *aout, bool wait)
278 {
279     aout_sys_t *sys = aout->sys;
280     int fd = sys->fd;
281
282     if (wait)
283         return; /* drain is implicit with OSS */
284     ioctl (fd, SNDCTL_DSP_HALT_OUTPUT, NULL);
285 }
286
287 static int VolumeSync (audio_output_t *aout)
288 {
289     aout_sys_t *sys = aout->sys;
290     int fd = sys->fd;
291
292     int level;
293     if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
294         return -1;
295
296     sys->mute = !level;
297     if (level) /* try to keep last volume before mute */
298         sys->level = level;
299     aout_MuteReport (aout, !level);
300     aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
301     return 0;
302 }
303
304 /**
305  * Releases the audio output device.
306  */
307 static void Stop (audio_output_t *aout)
308 {
309     aout_sys_t *sys = aout->sys;
310     int fd = sys->fd;
311
312     ioctl (fd, SNDCTL_DSP_HALT, NULL);
313     close (fd);
314     sys->fd = -1;
315 }
316
317 static int VolumeSet (audio_output_t *aout, float vol)
318 {
319     aout_sys_t *sys = aout->sys;
320     int fd = sys->fd;
321     if (fd == -1)
322         return -1;
323
324     int level = lroundf (vol * 100.f);
325     if (level > 0xFF)
326         level = 0xFFFF;
327     else
328         level |= level << 8;
329     if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
330     {
331         msg_Err (aout, "cannot set volume: %m");
332         return -1;
333     }
334
335     sys->level = level;
336     aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
337     return 0;
338 }
339
340 static int MuteSet (audio_output_t *aout, bool mute)
341 {
342     aout_sys_t *sys = aout->sys;
343     int fd = sys->fd;
344     if (fd == -1)
345         return -1;
346
347     int level = mute ? 0 : (sys->level | (sys->level << 8));
348     if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
349     {
350         msg_Err (aout, "cannot mute: %m");
351         return -1;
352     }
353
354     sys->mute = mute;
355     aout_MuteReport (aout, mute);
356     return 0;
357 }
358
359 static int DevicesEnum (audio_output_t *aout)
360 {
361     int fd = vlc_open ("/dev/dsp", O_WRONLY);
362     if (fd == -1)
363         return -1;
364
365     oss_sysinfo si;
366     int n = -1;
367
368     if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
369     {
370         msg_Err (aout, "cannot get system infos: %m");
371         goto out;
372     }
373
374     msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
375              si.version, si.versionnum, si.license);
376
377     for (int i = 0; i < si.numaudios; i++)
378     {
379         oss_audioinfo ai = { .dev = i };
380
381         if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
382         {
383             msg_Warn (aout, "cannot get device %d infos: %m", i);
384             continue;
385         }
386         if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
387             continue;
388         if (!(ai.caps & PCM_CAP_OUTPUT))
389             continue;
390         if (!ai.enabled)
391             continue;
392
393         aout_HotplugReport (aout, ai.devnode, ai.name);
394         n++;
395     }
396 out:
397     close (fd);
398     return n;
399 }
400
401 static int DeviceSelect (audio_output_t *aout, const char *id)
402 {
403     aout_sys_t *sys = aout->sys;
404     char *path = NULL;
405
406     if (id != NULL)
407     {
408         path = strdup (id);
409         if (unlikely(path == NULL))
410             return -1;
411     }
412
413     free (sys->device);
414     sys->device = path;
415     aout_DeviceReport (aout, path);
416     aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
417     return 0;
418 }
419
420 static int Open (vlc_object_t *obj)
421 {
422     audio_output_t *aout = (audio_output_t *)obj;
423
424     aout_sys_t *sys = malloc (sizeof (*sys));
425     if(unlikely( sys == NULL ))
426         return VLC_ENOMEM;
427
428     sys->fd = -1;
429
430     sys->level = 100;
431     sys->mute = false;
432     sys->device = var_InheritString (aout, "oss-audio-device");
433
434     aout->sys = sys;
435     aout->start = Start;
436     aout->stop = Stop;
437     aout->volume_set = VolumeSet;
438     aout->mute_set = MuteSet;
439     aout->device_select = DeviceSelect;
440
441     DevicesEnum (aout);
442     return VLC_SUCCESS;
443 }
444
445 static void Close (vlc_object_t *obj)
446 {
447     audio_output_t *aout = (audio_output_t *)obj;
448     aout_sys_t *sys = aout->sys;
449
450     free (sys->device);
451     free (sys);
452 }