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 it
7 * 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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
34 static int Open (vlc_object_t *);
35 static void Close (vlc_objec_t *);
38 set_shortname ("sndio")
39 set_description (N_("OpenBSD sndio audio output"))
40 set_category (CAT_AUDIO)
41 set_subcategory (SUBCAT_AUDIO_AOUT)
42 set_capability ("audio output", 120)
43 set_callbacks (Open, Close)
46 static int TimeGet (audio_output, mtime_t *);
47 static void Play (audio_output_t *, block_t *);
48 static void Flush (audio_output_t *, bool);
49 static int VolumeSet (audio_output_t *, float);
50 static int MuteSet (audio_output_t *, bool);
51 static void VolumeChanged (void *, unsigned);
52 static void PositionChanged (void *, int);
57 unsigned long long read_offset;
58 unsigned long long write_offset;
64 /** Initializes an sndio playback stream */
65 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
67 aout_sys_t *sys = aout->sys;
69 sys->hdl = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
72 msg_Err (obj, "cannot create audio playback stream");
81 par.bps = par.bits >> 3;
83 par.le = SIO_LE_NATIVE;
84 par.pchan = aout_FormatNbChannels (fmt);
85 par.rate = fmt->i_rate;
88 if (!sio_setpar (sys->hdl, &par) || !sio_getpar (sys->hdl, &par))
90 msg_Err (obj, "cannot negotiate audio playback parameters");
94 if (par.bps != par.bits >> 3)
96 msg_Err (obj, "unsupported audio sample format (%u bits in %u bytes)",
100 if (par.sig != (par.bits != 8))
102 msg_Err (obj, "unsupported audio sample format (%ssigned)",
103 par.sig ? "" : "un");
106 #ifdef WORDS_BIGENDIAN
109 msg_Err (obj, "unsupported audio sample format (little endian)");
115 msg_Err (obj, "unsupported audio sample format (big endian)");
122 fmt->i_format = VLC_CODEC_U8;
125 fmt->i_format = VLC_CODEC_S16N;
128 fmt->i_format = VLC_CODEC_S32N;
131 msg_Err (obj, "unsupported audio sample format (%u bits)",
136 fmt->i_rate = par.rate;
137 sys->rate = par.rate;
144 chans = AOUT_CHAN_CENTER;
147 chans = AOUT_CHANS_STEREO;
150 chans = AOUT_CHANS_4_0;
153 chans = AOUT_CHANS_5_1;
156 chans = AOUT_CHANS_7_1;
159 msg_Err (aout, "unknown %u channels map", par.pchan);
163 fmt->i_original_channels = fmt->i_physical_channels = chans;
164 aout_FormatPrepare (fmt);
167 aout->time_get = TimeGet;
171 if (sio_onvol(sys->hdl, VolumeChanged, aout))
173 aout->volume_set = VolumeSet;
174 aout->mute_set = MuteSet;
178 aout->volume_set = NULL;
179 aout->mute_set = NULL;
182 sys->read_offset = 0;
183 sys->write_offset = 0;
184 sio_onmove (sys->hdl, PositionChanged, aout);
185 sio_start (sys->hdl);
189 sio_close (sys->hdl);
193 static void Close (vlc_object_t *obj)
195 audio_output_t *aout = (audio_output_t *)obj;
196 aout_sys_t *sys = aout->sys;
198 sio_close (sys->hdl);
201 static void PositionChanged (void *arg, int delta)
203 audio_output_t *aout = arg;
204 aout_sys_t *sys = aout->sys;
206 sys->read_offset += delta;
209 static int TimeGet (audio_output_t *aout, mtime_t *restrict delay)
211 aout_sys_t *sys = aout->sys;
212 long long frames = sys->write_offset - sys->read_offset;
217 *delay = frames * CLOCK_FREQ / sys->rate;
221 static void Play (audio_output_t *aout, block_t *block)
223 aout_sys_t *sys = aout->sys;
225 sys->write_offset += block->i_nb_samples;
227 while (block->i_buffer > 0 && !sio_eof (sys->hdl))
229 size_t bytes = sio_write (sys->hdl, block->p_buffer, block->i_buffer);
231 block->p_buffer += bytes;
232 block->i_buffer -= bytes;
233 /* Note that i_nb_samples and i_pts are not updated here. */
235 block_Release (block);
238 static void Flush (audio_output_t *aout, bool wait)
242 long long frames = sys->write_offset - sys->read_offset;
245 msleep (frames * CLOCK_FREQ / sys->rate);
250 sys->read_offset = 0;
251 sys->write_offset = 0;
252 sio_start (sys->hdl);
256 static void VolumeChanged (void *arg, unsigned volume)
258 audio_output_t *aout = arg;
259 float fvol = (float)volume / (float)SIO_MAXVOL;
261 aout_VolumeReport (aout, fvol);
262 aout_MuteReport (aout, volume == 0);
263 if (volume) /* remember last non-zero volume to unmute later */
264 aout->sys->volume = volume;
267 static int VolumeSet (audio_output_t *aout, float fvol)
269 aout_sys_t *sys = aout->sys;
270 unsigned volume = lroundf (fvol * SIO_MAXVOL);
272 if (!sys->mute && !sio_setvol (sys->hdl, volume))
274 sys->volume = volume;
278 static int MuteSet (audio_output_t *aout, bool mute)
280 aout_sys_t *sys = aout->sys;
282 if (!sio_setvol (sys->hdl, mute ? 0 : sys->volume))
289 static int Open (vlc_object_t *obj)
291 audio_output_t *aout = (audio_output_t *)obj;
292 aout_sys_t *sys = malloc (sizeof (*sys));
293 if (unlikely(sys == NULL))
299 /* FIXME: set volume/mute here */
303 static int Close (vlc_object_t *obj)
305 audio_output_t *aout = (audio_output_t *)obj;
306 aout_sys_t *sys = aout->sys;