1 /*****************************************************************************
2  * sndio.c : sndio plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2012 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20 
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include <math.h>
26 #include <assert.h>
27 
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_aout.h>
31 
32 #include <sndio.h>
33 
34 static int Open (vlc_object_t *);
35 static void Close (vlc_object_t *);
36 
37 vlc_module_begin ()
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)
44 vlc_module_end ()
45 
46 static int TimeGet (audio_output_t *, 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);
53 
54 struct aout_sys_t
55 {
56     struct sio_hdl *hdl;
57     int started;
58     int delay;
59     unsigned rate;
60     unsigned volume;
61     bool mute;
62 };
63 
64 /** Initializes an sndio playback stream */
Start(audio_output_t * aout,audio_sample_format_t * restrict fmt)65 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
66 {
67     aout_sys_t *sys = aout->sys;
68 
69     if (aout_FormatNbChannels(fmt) == 0)
70         return VLC_EGENERIC;
71 
72     sys->hdl = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
73     if (sys->hdl == NULL)
74     {
75         msg_Err (aout, "cannot create audio playback stream");
76         return VLC_EGENERIC;
77     }
78 
79     struct sio_par par;
80     sio_initpar (&par);
81     switch (fmt->i_format) {
82     case VLC_CODEC_U8:
83         par.bits = 8;
84         par.sig = 0;
85         break;
86     case VLC_CODEC_S16N:
87         par.bits = 16;
88         par.sig = 1;
89         par.le = SIO_LE_NATIVE;
90         break;
91     case VLC_CODEC_S32N:
92     case VLC_CODEC_FL32:
93     case VLC_CODEC_FL64:
94         par.bits = 32;
95         par.sig = 1;
96         par.le = SIO_LE_NATIVE;
97         break;
98     default:
99         /* use a common audio format */
100         par.bits = 16;
101         par.sig = 1;
102         par.le = SIO_LE_NATIVE;
103     }
104     par.pchan = aout_FormatNbChannels (fmt);
105     par.rate = fmt->i_rate;
106     par.round = par.rate / 50;
107     par.appbufsz = par.rate / 4;
108 
109     if (!sio_setpar (sys->hdl, &par) || !sio_getpar (sys->hdl, &par))
110     {
111         msg_Err (aout, "cannot negotiate audio playback parameters");
112         goto error;
113     }
114 
115     if (par.bps != par.bits >> 3 && !par.msb)
116     {
117         msg_Err (aout, "unsupported audio sample format (%u bits in %u bytes)",
118                  par.bits, par.bps);
119         goto error;
120     }
121     if (par.sig != (par.bits != 8))
122     {
123         msg_Err (aout, "unsupported audio sample format (%ssigned)",
124                  par.sig ? "" : "un");
125         goto error;
126     }
127     if (par.bps > 1 && par.le != SIO_LE_NATIVE)
128     {
129         msg_Err (aout, "unsupported audio sample format (%s endian)",
130                  par.le ? "little" : "big");
131         goto error;
132     }
133     switch (par.bits)
134     {
135         case 8:
136             fmt->i_format = VLC_CODEC_U8;
137             break;
138         case 16:
139             fmt->i_format = VLC_CODEC_S16N;
140             break;
141         case 32:
142             fmt->i_format = VLC_CODEC_S32N;
143             break;
144         default:
145             msg_Err (aout, "unsupported audio sample format (%u bits)",
146                      par.bits);
147             goto error;
148     }
149 
150     fmt->i_rate = par.rate;
151     sys->rate = par.rate;
152 
153     /* Channel map */
154     unsigned chans;
155     switch (par.pchan)
156     {
157         case 1:
158             chans = AOUT_CHAN_CENTER;
159             break;
160         case 2:
161             chans = AOUT_CHANS_STEREO;
162             break;
163         case 4:
164             chans = AOUT_CHANS_4_0;
165             break;
166         case 6:
167             chans = AOUT_CHANS_5_1;
168             break;
169         case 8:
170             chans = AOUT_CHANS_7_1;
171             break;
172         default:
173             msg_Err (aout, "unknown %u channels map", par.pchan);
174             goto error;
175     }
176 
177     fmt->channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
178     fmt->i_physical_channels = chans;
179     aout_FormatPrepare (fmt);
180 
181     aout->time_get = TimeGet;
182     aout->play = Play;
183     aout->pause = NULL;
184     aout->flush = Flush;
185     if (sio_onvol(sys->hdl, VolumeChanged, aout))
186     {
187         aout->volume_set = VolumeSet;
188         aout->mute_set = MuteSet;
189     }
190     else
191     {
192         aout->volume_set = NULL;
193         aout->mute_set = NULL;
194     }
195 
196     sys->started = 0;
197     sys->delay = 0;
198     sio_onmove (sys->hdl, PositionChanged, aout);
199     sio_start (sys->hdl);
200     return VLC_SUCCESS;
201 
202 error:
203     sio_close (sys->hdl);
204     return VLC_EGENERIC;
205 }
206 
Stop(audio_output_t * aout)207 static void Stop (audio_output_t *aout)
208 {
209     aout_sys_t *sys = aout->sys;
210 
211     aout->volume_set = NULL;
212     aout->mute_set = NULL;
213     sio_close (sys->hdl);
214 }
215 
PositionChanged(void * arg,int delta)216 static void PositionChanged (void *arg, int delta)
217 {
218     audio_output_t *aout = arg;
219     aout_sys_t *sys = aout->sys;
220 
221     sys->delay -= delta;
222     sys->started = 1;
223 }
224 
TimeGet(audio_output_t * aout,mtime_t * restrict delay)225 static int TimeGet (audio_output_t *aout, mtime_t *restrict delay)
226 {
227     aout_sys_t *sys = aout->sys;
228 
229     if (!sys->started)
230         return -1;
231     *delay = (mtime_t)sys->delay * CLOCK_FREQ / sys->rate;
232     return 0;
233 }
234 
Play(audio_output_t * aout,block_t * block)235 static void Play (audio_output_t *aout, block_t *block)
236 {
237     aout_sys_t *sys = aout->sys;
238 
239     sio_write (sys->hdl, block->p_buffer, block->i_buffer);
240     sys->delay += block->i_nb_samples;
241     block_Release (block);
242 }
243 
Flush(audio_output_t * aout,bool wait)244 static void Flush (audio_output_t *aout, bool wait)
245 {
246     aout_sys_t *sys = aout->sys;
247 
248     sio_stop (sys->hdl);
249     sys->started = 0;
250     sys->delay = 0;
251     sio_start (sys->hdl);
252     (void)wait;
253 }
254 
VolumeChanged(void * arg,unsigned volume)255 static void VolumeChanged (void *arg, unsigned volume)
256 {
257     audio_output_t *aout = arg;
258     float fvol = (float)volume / (float)SIO_MAXVOL;
259 
260     aout_VolumeReport (aout, fvol);
261     aout_MuteReport (aout, volume == 0);
262     if (volume) /* remember last non-zero volume to unmute later */
263         aout->sys->volume = volume;
264 }
265 
VolumeSet(audio_output_t * aout,float fvol)266 static int VolumeSet (audio_output_t *aout, float fvol)
267 {
268     aout_sys_t *sys = aout->sys;
269     unsigned volume;
270 
271     if (fvol < 0)
272         fvol = 0;
273     if (fvol > 1)
274         fvol = 1;
275     volume = lroundf (fvol * SIO_MAXVOL);
276     if (!sys->mute && !sio_setvol (sys->hdl, volume))
277         return -1;
278     sys->volume = volume;
279     return 0;
280 }
281 
MuteSet(audio_output_t * aout,bool mute)282 static int MuteSet (audio_output_t *aout, bool mute)
283 {
284     aout_sys_t *sys = aout->sys;
285 
286     if (!sio_setvol (sys->hdl, mute ? 0 : sys->volume))
287         return -1;
288 
289     sys->mute = mute;
290     return 0;
291 }
292 
Open(vlc_object_t * obj)293 static int Open (vlc_object_t *obj)
294 {
295     audio_output_t *aout = (audio_output_t *)obj;
296     aout_sys_t *sys = malloc (sizeof (*sys));
297     if (unlikely(sys == NULL))
298         return VLC_ENOMEM;
299 
300     aout->sys = sys;
301     aout->start = Start;
302     aout->stop = Stop;
303     /* FIXME: set volume/mute here */
304     return VLC_SUCCESS;
305 }
306 
Close(vlc_object_t * obj)307 static void Close (vlc_object_t *obj)
308 {
309     audio_output_t *aout = (audio_output_t *)obj;
310     aout_sys_t *sys = aout->sys;
311 
312     free (sys);
313 }
314 
315