1 /*****************************************************************************
2 * oss.c: Open Sound System audio output plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2000-2002 the VideoLAN team
5 * Copyright (C) 2007-2012 RĂ©mi Denis-Courmont
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #ifdef HAVE_SOUNDCARD_H
36 # include <soundcard.h>
38 # include <sys/soundcard.h>
41 #ifndef SNDCTL_DSP_HALT
42 # define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
51 #define A52_FRAME_NB 1536
61 static int Open (vlc_object_t *);
62 static void Close (vlc_object_t *);
64 #define AUDIO_DEV_TEXT N_("Audio output device")
65 #define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
68 set_shortname( "OSS" )
69 set_description (N_("Open Sound System audio output"))
70 set_category( CAT_AUDIO )
71 set_subcategory( SUBCAT_AUDIO_AOUT )
72 add_string ("oss-audio-device", "",
73 AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
74 set_capability( "audio output", 100 )
75 set_callbacks( Open, Close )
78 static void Play (audio_output_t *, block_t *, mtime_t *);
79 static void Pause (audio_output_t *, bool, mtime_t);
80 static void Flush (audio_output_t *, bool);
81 static int VolumeSync (audio_output_t *);
82 static int VolumeSet (audio_output_t *, float);
83 static int MuteSet (audio_output_t *, bool);
85 static int DeviceChanged (vlc_object_t *obj, const char *varname,
86 vlc_value_t prev, vlc_value_t cur, void *data)
88 aout_ChannelsRestart (obj, varname, prev, cur, data);
90 if (!var_Type (obj, "oss-audio-device"))
91 var_Create (obj, "oss-audio-device", VLC_VAR_STRING);
92 var_SetString (obj, "oss-audio-device", cur.psz_string);
96 static int Open (vlc_object_t *obj)
98 audio_output_t *aout = (audio_output_t *)obj;
100 /* Open the device */
102 char *devicebuf = var_InheritString (aout, "oss-audio-device");
105 device = getenv ("OSS_AUDIODEV");
109 msg_Dbg (aout, "using OSS device: %s", device);
111 int fd = vlc_open (device, O_WRONLY);
114 msg_Err (aout, "cannot open OSS device %s: %m", device);
119 aout_sys_t *sys = malloc (sizeof (*sys));
120 if (unlikely(sys == NULL))
125 /* Select audio format */
127 vlc_fourcc_t fourcc = aout->format.i_format;
141 format = AFMT_S32_BE;
144 format = AFMT_S32_LE;
147 format = AFMT_S16_BE;
150 format = AFMT_S16_LE;
157 if (AOUT_FMT_SPDIF(&aout->format))
158 spdif = var_InheritBool (aout, "spdif");
166 format = AFMT_S16_NE;
169 if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
171 msg_Err (aout, "cannot set audio format 0x%X: %m", format);
177 case AFMT_S8: fourcc = VLC_CODEC_S8; break;
178 case AFMT_U8: fourcc = VLC_CODEC_U8; break;
179 case AFMT_S16_BE: fourcc = VLC_CODEC_S16B; break;
180 case AFMT_S16_LE: fourcc = VLC_CODEC_S16L; break;
183 case AFMT_S32_BE: fourcc = VLC_CODEC_S32B; break;
184 case AFMT_S32_LE: fourcc = VLC_CODEC_S32L; break;
186 case AFMT_FLOAT: fourcc = VLC_CODEC_FL32; break;
191 fourcc = VLC_CODEC_SPDIFL;
195 msg_Err (aout, "unsupported audio format 0x%X", format);
199 /* Select channels count */
200 int channels = spdif ? 2 : aout_FormatNbChannels (&aout->format);
201 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
203 msg_Err (aout, "cannot set %d channels: %m", channels);
209 case 1: channels = AOUT_CHAN_CENTER; break;
210 case 2: channels = AOUT_CHANS_STEREO; break;
211 case 4: channels = AOUT_CHANS_4_0; break;
212 case 6: channels = AOUT_CHANS_5_1; break;
213 case 8: channels = AOUT_CHANS_7_1; break;
215 msg_Err (aout, "unsupported channels count %d", channels);
219 /* Select sample rate */
220 int rate = spdif ? 48000 : aout->format.i_rate;
221 if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
223 msg_Err (aout, "cannot set %d Hz sample rate: %m", rate);
227 /* Setup audio_output_t */
228 aout->format.i_format = fourcc;
229 aout->pf_play = Play;
230 aout->pf_pause = Pause;
231 aout->pf_flush = Flush;
232 aout->volume_set = NULL;
233 aout->mute_set = NULL;
237 aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
238 aout->format.i_frame_length = A52_FRAME_NB;
242 aout->format.i_rate = rate;
243 aout->format.i_original_channels =
244 aout->format.i_physical_channels = channels;
248 if (VolumeSync (aout) == 0)
250 aout->volume_set = VolumeSet;
251 aout->mute_set = MuteSet;
254 sys->starting = true;
256 /* Build the devices list */
257 var_Create (aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
258 var_SetString (aout, "audio-device", device);
259 var_AddCallback (aout, "audio-device", DeviceChanged, NULL);
262 if (ioctl (fd, SNDCTL_SYSINFO, &si) >= 0)
264 vlc_value_t val, text;
266 text.psz_string = _("Audio Device");
267 var_Change (aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL);
269 msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
270 si.version, si.versionnum, si.license);
272 for (int i = 0; i < si.numaudios; i++)
274 oss_audioinfo ai = { .dev = i };
276 if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
278 msg_Warn (aout, "cannot get device %d infos: %m", i);
281 if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
283 if (!(ai.caps & PCM_CAP_OUTPUT))
288 val.psz_string = ai.devnode;
289 text.psz_string = ai.name;
290 var_Change (aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
304 * Releases the audio output.
306 static void Close (vlc_object_t *obj)
308 audio_output_t *aout = (audio_output_t *)obj;
309 aout_sys_t *sys = aout->sys;
312 var_DelCallback (obj, "audio-device", DeviceChanged, NULL);
313 var_Destroy (obj, "audio-device");
315 ioctl (fd, SNDCTL_DSP_HALT, NULL);
321 * Queues one audio buffer to the hardware.
323 static void Play (audio_output_t *aout, block_t *block,
324 mtime_t *restrict drift)
326 aout_sys_t *sys = aout->sys;
330 if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) >= 0)
332 mtime_t latency = (delay * CLOCK_FREQ * aout->format.i_frame_length)
333 / (aout->format.i_rate * aout->format.i_bytes_per_frame);
334 *drift = mdate () + latency - block->i_pts;
337 msg_Warn (aout, "cannot get delay: %m");
340 { /* Start on time */
341 /* TODO: resync on pause resumption and underflow recovery */
342 mtime_t delta = -*drift;
344 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
348 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
349 sys->starting = false;
352 while (block->i_buffer > 0)
354 ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
357 block->p_buffer += bytes;
358 block->i_buffer -= bytes;
361 msg_Err (aout, "cannot write samples: %m");
363 block_Release (block);
365 /* Dumb OSS cannot send any kind of events for this... */
370 * Pauses/resumes the audio playback.
372 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
374 aout_sys_t *sys = aout->sys;
378 ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
382 * Flushes/drains the audio playback buffer.
384 static void Flush (audio_output_t *aout, bool wait)
386 aout_sys_t *sys = aout->sys;
390 return; /* drain is implicit with OSS */
391 ioctl (fd, SNDCTL_DSP_HALT_OUTPUT, NULL);
394 static int VolumeSync (audio_output_t *aout)
396 aout_sys_t *sys = aout->sys;
400 if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
404 if (level) /* try to keep last volume before mute */
406 aout_MuteReport (aout, !level);
407 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
411 static int VolumeSet (audio_output_t *aout, float vol)
413 aout_sys_t *sys = aout->sys;
416 int level = lroundf (vol * 100.f);
421 if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
423 msg_Err (aout, "cannot set volume: %m");
428 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
432 static int MuteSet (audio_output_t *aout, bool mute)
434 aout_sys_t *sys = aout->sys;
437 int level = mute ? 0 : (sys->level | (sys->level << 8));
438 if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
440 msg_Err (aout, "cannot mute: %m");
445 aout_MuteReport (aout, mute);