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
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 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.
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.
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 *****************************************************************************/
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
56 audio_sample_format_t format;
64 static int Open (vlc_object_t *);
65 static void Close (vlc_object_t *);
67 #define AUDIO_DEV_TEXT N_("Audio output device")
68 #define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
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)
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 *);
87 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
89 aout_sys_t* sys = aout->sys;
92 const char *device = sys->device;
94 device = getenv ("OSS_AUDIODEV");
98 int fd = vlc_open (device, O_WRONLY);
101 msg_Err (aout, "cannot open OSS device %s: %m", device);
105 msg_Dbg (aout, "using OSS device: %s", device);
107 /* Select audio format */
111 switch (fmt->i_format)
122 format = AFMT_S32_BE;
125 format = AFMT_S32_LE;
128 format = AFMT_S16_BE;
131 format = AFMT_S16_LE;
138 if (AOUT_FMT_SPDIF(fmt))
139 spdif = var_InheritBool (aout, "spdif");
147 format = AFMT_S16_NE;
150 if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
152 msg_Err (aout, "cannot set audio format 0x%X: %m", format);
158 case AFMT_S8: fmt->i_format = VLC_CODEC_S8; break;
159 case AFMT_U8: fmt->i_format = VLC_CODEC_U8; break;
160 case AFMT_S16_BE: fmt->i_format = VLC_CODEC_S16B; break;
161 case AFMT_S16_LE: fmt->i_format = VLC_CODEC_S16L; break;
164 case AFMT_S32_BE: fmt->i_format = VLC_CODEC_S32B; break;
165 case AFMT_S32_LE: fmt->i_format = VLC_CODEC_S32L; break;
167 case AFMT_FLOAT: fmt->i_format = VLC_CODEC_FL32; break;
172 fmt->i_format = VLC_CODEC_SPDIFL;
176 msg_Err (aout, "unsupported audio format 0x%X", format);
180 /* Select channels count */
181 int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
182 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
184 msg_Err (aout, "cannot set %d channels: %m", channels);
190 case 1: channels = AOUT_CHAN_CENTER; break;
191 case 2: channels = AOUT_CHANS_STEREO; break;
192 case 4: channels = AOUT_CHANS_4_0; break;
193 case 6: channels = AOUT_CHANS_5_1; break;
194 case 8: channels = AOUT_CHANS_7_1; break;
196 msg_Err (aout, "unsupported channels count %d", channels);
200 /* Select sample rate */
201 int rate = spdif ? 48000 : fmt->i_rate;
202 if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
204 msg_Err (aout, "cannot set %d Hz sample rate: %m", rate);
208 /* Setup audio_output_t */
209 aout->time_get = TimeGet;
216 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
217 fmt->i_frame_length = A52_FRAME_NB;
222 fmt->i_original_channels =
223 fmt->i_physical_channels = channels;
227 sys->starting = true;
235 static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
237 aout_sys_t *sys = aout->sys;
240 if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
242 msg_Warn (aout, "cannot get delay: %m");
246 *pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
247 / (sys->format.i_rate * sys->format.i_bytes_per_frame);
252 * Queues one audio buffer to the hardware.
254 static void Play (audio_output_t *aout, block_t *block)
256 aout_sys_t *sys = aout->sys;
259 while (block->i_buffer > 0)
261 ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
264 block->p_buffer += bytes;
265 block->i_buffer -= bytes;
268 msg_Err (aout, "cannot write samples: %m");
270 block_Release (block);
272 /* Dumb OSS cannot send any kind of events for this... */
277 * Pauses/resumes the audio playback.
279 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
281 aout_sys_t *sys = aout->sys;
285 ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
289 * Flushes/drains the audio playback buffer.
291 static void Flush (audio_output_t *aout, bool wait)
293 aout_sys_t *sys = aout->sys;
297 return; /* drain is implicit with OSS */
298 ioctl (fd, SNDCTL_DSP_HALT_OUTPUT, NULL);
301 static int VolumeSync (audio_output_t *aout)
303 aout_sys_t *sys = aout->sys;
307 if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
311 if (level) /* try to keep last volume before mute */
313 aout_MuteReport (aout, !level);
314 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
319 * Releases the audio output device.
321 static void Stop (audio_output_t *aout)
323 aout_sys_t *sys = aout->sys;
326 ioctl (fd, SNDCTL_DSP_HALT, NULL);
331 static int VolumeSet (audio_output_t *aout, float vol)
333 aout_sys_t *sys = aout->sys;
336 int level = lroundf (vol * 100.f);
341 if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
343 msg_Err (aout, "cannot set volume: %m");
348 aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
352 static int MuteSet (audio_output_t *aout, bool mute)
354 aout_sys_t *sys = aout->sys;
357 int level = mute ? 0 : (sys->level | (sys->level << 8));
358 if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
360 msg_Err (aout, "cannot mute: %m");
365 aout_MuteReport (aout, mute);
369 static int DevicesEnum (audio_output_t *aout, char ***idp, char ***namep)
371 aout_sys_t *sys = sys;
377 if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
379 msg_Err (aout, "cannot get system infos: %m");
383 msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
384 si.version, si.versionnum, si.license);
386 char **ids = xmalloc (sizeof (*ids) * si.numaudios);
387 char **names = xmalloc (sizeof (*names) * si.numaudios);
390 for (int i = 0; i < si.numaudios; i++)
392 oss_audioinfo ai = { .dev = i };
394 if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
396 msg_Warn (aout, "cannot get device %d infos: %m", i);
399 if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
401 if (!(ai.caps & PCM_CAP_OUTPUT))
406 ids[n] = xstrdup (ai.devnode);
407 names[n] = xstrdup (ai.name);
415 static int DeviceSelect (audio_output_t *aout, const char *id)
417 aout_sys_t *sys = aout->sys;
423 if (unlikely(path == NULL))
429 aout_DeviceReport (aout, path);
430 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
434 static int Open (vlc_object_t *obj)
436 audio_output_t *aout = (audio_output_t *)obj;
438 aout_sys_t *sys = malloc (sizeof (*sys));
439 if(unlikely( sys == NULL ))
446 sys->device = var_InheritString (aout, "oss-audio-device");
451 aout->volume_set = VolumeSet;
452 aout->mute_set = MuteSet;
453 aout->device_enum = DevicesEnum;
454 aout->device_select = DeviceSelect;
458 static void Close (vlc_object_t *obj)
460 audio_output_t *aout = (audio_output_t *)obj;
461 aout_sys_t *sys = aout->sys;