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
Start(audio_output_t * aout,audio_sample_format_t * restrict fmt)89 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
90 {
91 aout_sys_t* sys = aout->sys;
92
93 if (aout_FormatNbChannels(fmt) == 0)
94 return VLC_EGENERIC;
95
96 /* Open the device */
97 const char *device = sys->device;
98 if (device == NULL)
99 device = getenv ("OSS_AUDIODEV");
100 if (device == NULL)
101 device = "/dev/dsp";
102
103 int fd = vlc_open (device, O_WRONLY);
104 if (fd == -1)
105 {
106 msg_Err (aout, "cannot open OSS device %s: %s", device,
107 vlc_strerror_c(errno));
108 return VLC_EGENERIC;
109 }
110 msg_Dbg (aout, "using OSS device: %s", device);
111
112 /* Select audio format */
113 int format;
114 bool spdif = false;
115
116 switch (fmt->i_format)
117 {
118 #ifdef AFMT_FLOAT
119 case VLC_CODEC_FL64:
120 case VLC_CODEC_FL32:
121 format = AFMT_FLOAT;
122 break;
123 #endif
124 case VLC_CODEC_S32N:
125 format = AFMT_S32_NE;
126 break;
127 case VLC_CODEC_S16N:
128 format = AFMT_S16_NE;
129 break;
130 case VLC_CODEC_U8:
131 format = AFMT_U8;
132 break;
133 default:
134 if (AOUT_FMT_SPDIF(fmt))
135 spdif = var_InheritBool (aout, "spdif");
136 if (spdif)
137 format = AFMT_AC3;
138 #ifdef AFMT_FLOAT
139 else if (HAVE_FPU)
140 format = AFMT_FLOAT;
141 #endif
142 else
143 format = AFMT_S16_NE;
144 }
145
146 if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
147 {
148 msg_Err (aout, "cannot set audio format 0x%X: %s", format,
149 vlc_strerror_c(errno));
150 goto error;
151 }
152
153 switch (format)
154 {
155 case AFMT_U8: fmt->i_format = VLC_CODEC_U8; break;
156 case AFMT_S16_NE: fmt->i_format = VLC_CODEC_S16N; break;
157 case AFMT_S32_NE: fmt->i_format = VLC_CODEC_S32N; break;
158 #ifdef AFMT_FLOAT
159 case AFMT_FLOAT: fmt->i_format = VLC_CODEC_FL32; break;
160 #endif
161 case AFMT_AC3:
162 if (spdif)
163 {
164 fmt->i_format = VLC_CODEC_SPDIFL;
165 break;
166 }
167 default:
168 msg_Err (aout, "unsupported audio format 0x%X", format);
169 goto error;
170 }
171
172 /* Select channels count */
173 int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
174 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
175 {
176 msg_Err (aout, "cannot set %d channels: %s", channels,
177 vlc_strerror_c(errno));
178 goto error;
179 }
180
181 switch (channels)
182 {
183 case 1: channels = AOUT_CHAN_CENTER; break;
184 case 2: channels = AOUT_CHANS_STEREO; break;
185 case 4: channels = AOUT_CHANS_4_0; break;
186 case 6: channels = AOUT_CHANS_5_1; break;
187 case 8: channels = AOUT_CHANS_7_1; break;
188 default:
189 msg_Err (aout, "unsupported channels count %d", channels);
190 goto error;
191 }
192
193 /* Select sample rate */
194 int rate = spdif ? 48000 : fmt->i_rate;
195 if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
196 {
197 msg_Err (aout, "cannot set %d Hz sample rate: %s", rate,
198 vlc_strerror_c(errno));
199 goto error;
200 }
201
202 /* Setup audio_output_t */
203 aout->time_get = TimeGet;
204 aout->play = Play;
205 aout->pause = Pause;
206 aout->flush = Flush;
207
208 if (spdif)
209 {
210 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
211 fmt->i_frame_length = A52_FRAME_NB;
212 }
213 else
214 {
215 fmt->i_rate = rate;
216 fmt->i_physical_channels = channels;
217 }
218 fmt->channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
219 aout_FormatPrepare (fmt);
220
221 /* Select timing */
222 unsigned bytes;
223 if (spdif)
224 bytes = AOUT_SPDIF_SIZE;
225 else
226 bytes = fmt->i_rate / (CLOCK_FREQ / AOUT_MIN_PREPARE_TIME)
227 * fmt->i_bytes_per_frame;
228 if (unlikely(bytes < 16))
229 bytes = 16;
230
231 int frag = (AOUT_MAX_ADVANCE_TIME / AOUT_MIN_PREPARE_TIME) << 16
232 | (32 - clz32(bytes - 1));
233 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
234 msg_Err (aout, "cannot set 0x%08x fragment: %s", frag,
235 vlc_strerror_c(errno));
236
237 sys->fd = fd;
238 aout_SoftVolumeStart (aout);
239 sys->starting = true;
240 sys->format = *fmt;
241 return VLC_SUCCESS;
242 error:
243 vlc_close (fd);
244 return VLC_EGENERIC;
245 }
246
TimeGet(audio_output_t * aout,mtime_t * restrict pts)247 static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
248 {
249 aout_sys_t *sys = aout->sys;
250 int delay;
251
252 if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
253 {
254 msg_Warn (aout, "cannot get delay: %s", vlc_strerror_c(errno));
255 return -1;
256 }
257
258 *pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
259 / (sys->format.i_rate * sys->format.i_bytes_per_frame);
260 return 0;
261 }
262
263 /**
264 * Queues one audio buffer to the hardware.
265 */
Play(audio_output_t * aout,block_t * block)266 static void Play (audio_output_t *aout, block_t *block)
267 {
268 aout_sys_t *sys = aout->sys;
269 int fd = sys->fd;
270
271 while (block->i_buffer > 0)
272 {
273 ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
274 if (bytes >= 0)
275 {
276 block->p_buffer += bytes;
277 block->i_buffer -= bytes;
278 }
279 else
280 msg_Err (aout, "cannot write samples: %s", vlc_strerror_c(errno));
281 }
282 block_Release (block);
283 }
284
285 /**
286 * Pauses/resumes the audio playback.
287 */
Pause(audio_output_t * aout,bool pause,mtime_t date)288 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
289 {
290 aout_sys_t *sys = aout->sys;
291 int fd = sys->fd;
292
293 (void) date;
294 ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
295 }
296
297 /**
298 * Flushes/drains the audio playback buffer.
299 */
Flush(audio_output_t * aout,bool wait)300 static void Flush (audio_output_t *aout, bool wait)
301 {
302 aout_sys_t *sys = aout->sys;
303 int fd = sys->fd;
304
305 if (wait)
306 return; /* drain is implicit with OSS */
307 ioctl (fd, SNDCTL_DSP_HALT, NULL);
308 }
309
310 /**
311 * Releases the audio output device.
312 */
Stop(audio_output_t * aout)313 static void Stop (audio_output_t *aout)
314 {
315 aout_sys_t *sys = aout->sys;
316 int fd = sys->fd;
317
318 ioctl (fd, SNDCTL_DSP_HALT, NULL);
319 vlc_close (fd);
320 sys->fd = -1;
321 }
322
DevicesEnum(audio_output_t * aout)323 static int DevicesEnum (audio_output_t *aout)
324 {
325 int fd = vlc_open ("/dev/dsp", O_WRONLY);
326 if (fd == -1)
327 return -1;
328
329 oss_sysinfo si;
330 int n = -1;
331
332 if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
333 {
334 msg_Err (aout, "cannot get system infos: %s", vlc_strerror(errno));
335 goto out;
336 }
337
338 msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
339 si.version, si.versionnum, si.license);
340
341 for (int i = 0; i < si.numaudios; i++)
342 {
343 oss_audioinfo ai = { .dev = i };
344
345 if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
346 {
347 msg_Warn (aout, "cannot get device %d infos: %s", i,
348 vlc_strerror_c(errno));
349 continue;
350 }
351 if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
352 continue;
353 if (!(ai.caps & PCM_CAP_OUTPUT))
354 continue;
355 if (!ai.enabled)
356 continue;
357
358 aout_HotplugReport (aout, ai.devnode, ai.name);
359 n++;
360 }
361 out:
362 vlc_close (fd);
363 return n;
364 }
365
DeviceSelect(audio_output_t * aout,const char * id)366 static int DeviceSelect (audio_output_t *aout, const char *id)
367 {
368 aout_sys_t *sys = aout->sys;
369 char *path = NULL;
370
371 if (id != NULL)
372 {
373 path = strdup (id);
374 if (unlikely(path == NULL))
375 return -1;
376 }
377
378 free (sys->device);
379 sys->device = path;
380 aout_DeviceReport (aout, path);
381 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
382 return 0;
383 }
384
Open(vlc_object_t * obj)385 static int Open (vlc_object_t *obj)
386 {
387 audio_output_t *aout = (audio_output_t *)obj;
388
389 aout_sys_t *sys = malloc (sizeof (*sys));
390 if(unlikely( sys == NULL ))
391 return VLC_ENOMEM;
392
393 sys->fd = -1;
394 sys->device = var_InheritString (aout, "oss-audio-device");
395
396 aout->sys = sys;
397 aout->start = Start;
398 aout->stop = Stop;
399 aout->device_select = DeviceSelect;
400 aout_DeviceReport (aout, sys->device);
401 aout_SoftVolumeInit (aout);
402
403 DevicesEnum (aout);
404 return VLC_SUCCESS;
405 }
406
Close(vlc_object_t * obj)407 static void Close (vlc_object_t *obj)
408 {
409 audio_output_t *aout = (audio_output_t *)obj;
410 aout_sys_t *sys = aout->sys;
411
412 free (sys->device);
413 free (sys);
414 }
415