1 /*****************************************************************************
2 * sndio.c : sndio plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2012 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
33 static int Open (vlc_object_t *);
34 static void Close (vlc_object_t *);
37 set_shortname ("sndio")
38 set_description (N_("OpenBSD sndio audio output"))
39 set_category (CAT_AUDIO)
40 set_subcategory (SUBCAT_AUDIO_AOUT)
41 set_capability ("audio output", 120)
42 set_callbacks (Open, Close )
45 static void Play (audio_output_t *, block_t *);
46 static void Pause (audio_output_t *, bool, mtime_t);
48 /** Initializes an sndio playback stream */
49 static int Open (vlc_object_t *obj)
51 audio_output_t *aout = (audio_output_t *)obj;
53 struct sio_hdl *sio = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
56 msg_Err (obj, "cannot create audio playback stream");
63 par.bps = par.bits >> 3;
65 par.le = SIO_LE_NATIVE;
66 par.pchan = aout_FormatNbChannels (&aout->format);
67 par.rate = aout->format.i_rate;
70 if (!sio_setpar (sio, &par) || !sio_getpar (sio, &par))
72 msg_Err (obj, "cannot negotiate audio playback parameters");
76 if (par.bps != par.bits >> 3)
78 msg_Err (obj, "unsupported audio sample format (%u bits in %u bytes)",
88 f.i_format = par.sig ? VLC_CODEC_S8 : VLC_CODEC_U8;
91 f.i_format = par.sig ? (par.le ? VLC_CODEC_S16L : VLC_CODEC_S16B)
92 : (par.le ? VLC_CODEC_U16L : VLC_CODEC_U16B);
95 f.i_format = par.sig ? (par.le ? VLC_CODEC_S24L : VLC_CODEC_S24B)
96 : (par.le ? VLC_CODEC_U24L : VLC_CODEC_U24B);
99 f.i_format = par.sig ? (par.le ? VLC_CODEC_S32L : VLC_CODEC_S32B)
100 : (par.le ? VLC_CODEC_U32L : VLC_CODEC_U32B);
103 msg_Err (obj, "unsupported audio sample format (%u bits)",
115 chans = AOUT_CHAN_CENTER;
118 chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
121 chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
122 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
125 chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
126 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
127 | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
130 msg_Err (aout, "unknown %u channels map", par.pchan);
134 f.i_original_channels = f.i_physical_channels = chans;
135 aout_FormatPrepare (&f);
138 aout->sys = (void *)sio;
139 aout->pf_play = Play;
140 aout->pf_pause = Pause;
141 aout->pf_flush = NULL; /* sndio sucks! */
142 aout_VolumeSoftInit (aout); /* TODO: sio_onvol() */
152 static void Close (vlc_object_t *obj)
154 audio_output_t *aout = (audio_output_t *)obj;
155 struct sio_hdl *sio = (void *)aout->sys;
160 static void Play (audio_output_t *aout, block_t *block)
162 struct sio_hdl *sio = (void *)aout->sys;
165 if (sio_getpar (sio, &par) == 0)
167 mtime_t delay = par.bufsz * CLOCK_FREQ / aout->format.i_rate;
169 delay = block->i_pts - (mdate () - delay);
172 size_t frames = (delay * aout->format.i_rate) / CLOCK_FREQ;
173 msg_Dbg (aout, "prepending %zu zeroes", frames);
175 void *pad = calloc (frames, aout->format.i_bytes_per_frame);
176 if (likely(pad != NULL))
178 sio_write (sio, pad, frames * aout->format.i_bytes_per_frame);
183 aout_TimeReport (aout, block->i_pts - delay);
186 while (block->i_buffer > 0 && !sio_eof (sio))
188 size_t bytes = sio_write (sio, block->p_buffer, block->i_buffer);
190 block->p_buffer += bytes;
191 block->i_buffer -= bytes;
192 /* Note that i_nb_samples and i_pts are corrupted here. */
194 block_Release (block);
197 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
199 struct sio_hdl *sio = (void *)aout->sys;