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 #include <vlc_common.h>
42 #include <vlc_plugin.h>
47 #define A52_FRAME_NB 1536
56 static int Open (vlc_object_t *);
57 static void Close (vlc_object_t *);
59 #define AUDIO_DEV_TEXT N_("Audio output device")
60 #define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
63 set_shortname( "OSS" )
64 set_description (N_("Open Sound System audio output"))
65 set_category( CAT_AUDIO )
66 set_subcategory( SUBCAT_AUDIO_AOUT )
67 add_string ("oss-audio-device", "",
68 AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
69 set_capability( "audio output", 100 )
70 set_callbacks( Open, Close )
73 static void Play (audio_output_t *, block_t *);
74 static void Pause (audio_output_t *, bool, mtime_t);
75 static void Flush (audio_output_t *, bool);
76 static int VolumeSync (audio_output_t *);
77 static int VolumeSet (audio_output_t *, float);
78 static int MuteSet (audio_output_t *, bool);
80 static int Open (vlc_object_t *obj)
82 audio_output_t *aout = (audio_output_t *)obj;
86 char *devicebuf = var_InheritString (aout, "oss-audio-device");
89 device = getenv ("OSS_AUDIODEV");
93 msg_Dbg (aout, "using OSS device: %s", device);
95 int fd = vlc_open (device, O_WRONLY);
99 msg_Err (aout, "cannot open OSS device: %m");
103 aout_sys_t *sys = malloc (sizeof (*sys));
104 if (unlikely(sys == NULL))
109 /* Select audio format */
111 vlc_fourcc_t fourcc = aout->format.i_format;
123 format = AFMT_S32_BE;
126 format = AFMT_S32_LE;
129 format = AFMT_S16_BE;
132 format = AFMT_S16_LE;
139 if (AOUT_FMT_SPDIF(&aout->format))
140 spdif = var_InheritBool (aout, "spdif");
146 format = AFMT_S16_NE;
149 if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
151 msg_Err (aout, "cannot set audio format 0x%X: %m", format);
157 case AFMT_S8: fourcc = VLC_CODEC_S8; break;
158 case AFMT_U8: fourcc = VLC_CODEC_U8; break;
159 case AFMT_S16_BE: fourcc = VLC_CODEC_S16B; break;
160 case AFMT_S16_LE: fourcc = VLC_CODEC_S16L; break;
163 case AFMT_S32_BE: fourcc = VLC_CODEC_S32B; break;
164 case AFMT_S32_LE: fourcc = VLC_CODEC_S32L; break;
165 case AFMT_FLOAT: fourcc = VLC_CODEC_FL32; break;
169 fourcc = VLC_CODEC_SPDIFL;
173 msg_Err (aout, "unsupported audio format 0x%X", format);
177 /* Select channels count */
178 int channels = spdif ? 2 : aout_FormatNbChannels (&aout->format);
179 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
181 msg_Err (aout, "cannot set %d channels: %m", channels);
187 case 1: channels = AOUT_CHAN_CENTER; break;
188 case 2: channels = AOUT_CHANS_STEREO; break;
189 case 4: channels = AOUT_CHANS_4_0; break;
190 case 6: channels = AOUT_CHANS_5_1; break;
191 case 8: channels = AOUT_CHANS_7_1; break;
193 msg_Err (aout, "unsupported channels count %d", channels);
197 /* Select sample rate */
198 int rate = spdif ? 48000 : aout->format.i_rate;
199 if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
201 msg_Err (aout, "cannot set %d Hz sample rate: %m", rate);
205 /* Setup audio_output_t */
206 aout->format.i_format = fourcc;
207 aout->format.i_rate = rate;
208 aout->pf_play = Play;
209 aout->pf_pause = Pause;
210 aout->pf_flush = Flush;
211 aout->volume_set = NULL;
212 aout->mute_set = NULL;
216 aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
217 aout->format.i_frame_length = A52_FRAME_NB;
221 aout->format.i_original_channels =
222 aout->format.i_physical_channels = channels;
226 if (VolumeSync (aout) == 0)
228 aout->volume_set = VolumeSet;
229 aout->mute_set = MuteSet;
241 * Releases the audio output.
243 static void Close (vlc_object_t *obj)
245 audio_output_t *aout = (audio_output_t *)obj;
246 aout_sys_t *sys = aout->sys;
250 /* FIXME: ugly hack so selected OSS device survives restart */
251 char *device = var_InheritString (obj, "audio-device");
254 if (!var_Type (obj, "oss-audio-device"))
255 var_Create (obj, "oss-audio-device", VLC_VAR_STRING);
256 var_SetString (obj, "oss-audio-device", device);
260 var_DelCallback (obj, "audio-device", aout_ChannelsRestart, NULL);
261 var_Destroy (obj, "audio-device");
264 ioctl (fd, SNDCTL_DSP_HALT, NULL);
270 * Queues one audio buffer to the hardware.
272 static void Play (audio_output_t *aout, block_t *block)
274 aout_sys_t *sys = aout->sys;
278 if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) >= 0)
280 mtime_t latency = (delay * CLOCK_FREQ * aout->format.i_frame_length)
281 / (aout->format.i_rate * aout->format.i_bytes_per_frame);
282 /* TODO: insert zeroes when starting playback */
283 aout_TimeReport (aout, block->i_pts - latency);
286 msg_Warn (aout, "cannot get delay: %m");
288 while (block->i_buffer > 0)
290 ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
293 block->p_buffer += bytes;
294 block->i_buffer -= bytes;
297 msg_Err (aout, "cannot write samples: %m");
299 block_Release (block);
301 /* Dumb OSS cannot send any kind of events for this... */
306 * Pauses/resumes the audio playback.
308 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
310 aout_sys_t *sys = aout->sys;
314 ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
318 * Flushes/drains the audio playback buffer.
320 static void Flush (audio_output_t *aout, bool wait)
322 aout_sys_t *sys = aout->sys;
326 return; /* drain is implicit with OSS */
327 ioctl (fd, SNDCTL_DSP_HALT_OUTPUT, NULL);
330 static int VolumeSync (audio_output_t *aout)
332 aout_sys_t *sys = aout->sys;
336 if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
340 if (level) /* try to keep last volume before mute */
342 aout_MuteReport (aout, !level);
343 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
347 static int VolumeSet (audio_output_t *aout, float vol)
349 aout_sys_t *sys = aout->sys;
352 int level = lroundf (vol * 100.f);
357 if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
359 msg_Err (aout, "cannot set volume: %m");
364 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
368 static int MuteSet (audio_output_t *aout, bool mute)
370 aout_sys_t *sys = aout->sys;
373 int level = mute ? 0 : (sys->level | (sys->level << 8));
374 if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
376 msg_Err (aout, "cannot mute: %m");
381 aout_MuteReport (aout, mute);