]> git.sesse.net Git - vlc/blob - modules/audio_output/sndio.c
3ac3a414f831678922595d52f0dfdc9d75c04e40
[vlc] / modules / audio_output / sndio.c
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_objec_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, 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     unsigned long long read_offset;
58     unsigned long long write_offset;
59     unsigned rate;
60     unsigned volume;
61     bool mute;
62 };
63
64 /** Initializes an sndio playback stream */
65 static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
66 {
67     aout_sys_t *sys = aout->sys;
68
69     sys->hdl = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
70     if (sys->hdl == NULL)
71     {
72         msg_Err (obj, "cannot create audio playback stream");
73         free (sys);
74         return VLC_EGENERIC;
75     }
76     aout->sys = sys;
77
78     struct sio_par par;
79     sio_initpar (&par);
80     par.bits = 16;
81     par.bps = par.bits >> 3;
82     par.sig = 1;
83     par.le = SIO_LE_NATIVE;
84     par.pchan = aout_FormatNbChannels (fmt);
85     par.rate = fmt->i_rate;
86     par.xrun = SIO_SYNC;
87
88     if (!sio_setpar (sys->hdl, &par) || !sio_getpar (sys->hdl, &par))
89     {
90         msg_Err (obj, "cannot negotiate audio playback parameters");
91         goto error;
92     }
93
94     if (par.bps != par.bits >> 3)
95     {
96         msg_Err (obj, "unsupported audio sample format (%u bits in %u bytes)",
97                  par.bits, par.bps);
98         goto error;
99     }
100
101     switch (par.bits)
102     {
103         case 8:
104             fmt->i_format = par.sig ? VLC_CODEC_S8 : VLC_CODEC_U8;
105             break;
106         case 16:
107             fmt->i_format = par.sig
108                                  ? (par.le ? VLC_CODEC_S16L : VLC_CODEC_S16B)
109                                  : (par.le ? VLC_CODEC_U16L : VLC_CODEC_U16B);
110             break;
111         case 24:
112             fmt->i_format = par.sig
113                                  ? (par.le ? VLC_CODEC_S24L : VLC_CODEC_S24B)
114                                  : (par.le ? VLC_CODEC_U24L : VLC_CODEC_U24B);
115             break;
116         case 32:
117             fmt->i_format = par.sig
118                                  ? (par.le ? VLC_CODEC_S32L : VLC_CODEC_S32B)
119                                  : (par.le ? VLC_CODEC_U32L : VLC_CODEC_U32B);
120             break;
121         default:
122             msg_Err (obj, "unsupported audio sample format (%u bits)",
123                      par.bits);
124             goto error;
125     }
126
127     fmt->i_rate = par.rate;
128     sys->rate = par.rate;
129
130     /* Channel map */
131     unsigned chans;
132     switch (par.pchan)
133     {
134         case 1:
135             chans = AOUT_CHAN_CENTER;
136             break;
137         case 2:
138             chans = AOUT_CHANS_STEREO;
139             break;
140         case 4:
141             chans = AOUT_CHANS_4_0;
142             break;
143         case 6:
144             chans = AOUT_CHANS_5_1;
145             break;
146         case 8:
147             chans = AOUT_CHANS_7_1;
148             break;
149         default:
150             msg_Err (aout, "unknown %u channels map", par.pchan);
151             goto error;
152     }
153
154     fmt->i_original_channels = fmt->i_physical_channels = chans;
155     aout_FormatPrepare (fmt);
156
157     aout->sys = sys;
158     aout->time_get = TimeGet;
159     aout->play = Play;
160     aout->pause = NULL;
161     aout->flush = Flush;
162     if (sio_onvol(sys->hdl, VolumeChanged, aout))
163     {
164         aout->volume_set = VolumeSet;
165         aout->mute_set = MuteSet;
166     }
167     else
168     {
169         aout->volume_set = NULL;
170         aout->mute_set = NULL;
171     }
172
173     sys->read_offset = 0;
174     sys->write_offset = 0;
175     sio_onmove (sys->hdl, PositionChanged, aout);
176     sio_start (sys->hdl);
177     return VLC_SUCCESS;
178
179 error:
180     sio_close (sys->hdl);
181     return VLC_EGENERIC;
182 }
183
184 static void Close (vlc_object_t *obj)
185 {
186     audio_output_t *aout = (audio_output_t *)obj;
187     aout_sys_t *sys = aout->sys;
188
189     sio_close (sys->hdl);
190 }
191
192 static void PositionChanged (void *arg, int delta)
193 {
194     audio_output_t *aout = arg;
195     aout_sys_t *sys = aout->sys;
196
197     sys->read_offset += delta;
198 }
199
200 static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
201 {
202     aout_sys_t *sys = aout->sys;
203     long long frames = sys->write_offset - sys->read_offset;
204
205     if (frames == 0)
206         return -1;
207
208     *pts = mdate () + (frames * CLOCK_FREQ / sys->rate);
209     return 0;
210 }
211
212 static void Play (audio_output_t *aout, block_t *block)
213 {
214     aout_sys_t *sys = aout->sys;
215
216     sys->write_offset += block->i_nb_samples;
217
218     while (block->i_buffer > 0 && !sio_eof (sys->hdl))
219     {
220         size_t bytes = sio_write (sys->hdl, block->p_buffer, block->i_buffer);
221
222         block->p_buffer += bytes;
223         block->i_buffer -= bytes;
224         /* Note that i_nb_samples and i_pts are not updated here. */
225     }
226     block_Release (block);
227 }
228
229 static void Flush (audio_output_t *aout, bool wait)
230 {
231     if (wait)
232     {
233         long long frames = sys->write_offset - sys->read_offset;
234
235         if (frames > 0)
236             msleep (frames * CLOCK_FREQ / sys->rate);
237     }
238     else
239     {
240         sio_stop (sys->hdl);
241         sys->read_offset = 0;
242         sys->write_offset = 0;
243         sio_start (sys->hdl);
244     }
245 }
246
247 static void VolumeChanged (void *arg, unsigned volume)
248 {
249     audio_output_t *aout = arg;
250     float fvol = (float)volume / (float)SIO_MAXVOL;
251
252     aout_VolumeReport (aout, fvol);
253     aout_MuteReport (aout, volume == 0);
254     if (volume) /* remember last non-zero volume to unmute later */
255         aout->sys->volume = volume;
256 }
257
258 static int VolumeSet (audio_output_t *aout, float fvol)
259 {
260     aout_sys_t *sys = aout->sys;
261     unsigned volume = lroundf (fvol * SIO_MAXVOL);
262
263     if (!sys->mute && !sio_setvol (sys->hdl, volume))
264         return -1;
265     sys->volume = volume;
266     return 0;
267 }
268
269 static int MuteSet (audio_output_t *aout, bool mute)
270 {
271     aout_sys_t *sys = aout->sys;
272
273     if (!sio_setvol (sys->hdl, mute ? 0 : sys->volume))
274         return -1;
275
276     sys->mute = mute;
277     return 0;
278 }
279
280 static int Open (vlc_object_t *obj)
281 {
282     audio_output_t *aout = (audio_output_t *)obj;
283     aout_sys_t *sys = malloc (sizeof (*sys));
284     if (unlikely(sys == NULL))
285         return VLC_ENOMEM;
286
287     aout->sys = sys;
288     aout->start = Start;
289     aout->stop = Stop;
290     /* FIXME: set volume/mute here */
291     return VLC_SUCCESS;
292 }
293
294 static int Close (vlc_object_t *obj)
295 {
296     audio_output_t *aout = (audio_output_t *)obj;
297     aout_sys_t *sys = aout->sys;
298
299     free (sys);
300 }
301