Debian Codegraph @

src/libout123/modules/sndio.c

raw
1 /*
2 * sndio: sndio audio output
3 *
4 * Copyright (c) 2008 Christian Weisgerber <[email protected]>,
5 * Alexandre Ratchov <[email protected]>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include "../out123_int.h"
21
22 #include <sndio.h>
23
24 #include "../../common/debug.h"
25
26 static int sndio_to_mpg123_enc(int sign, int bits)
27 {
28 switch(bits)
29 {
30 case 8:
31 return sign ? MPG123_ENC_SIGNED_8 : MPG123_ENC_UNSIGNED_8;
32 case 16:
33 return sign ? MPG123_ENC_SIGNED_16 : MPG123_ENC_UNSIGNED_16;
34 case 24:
35 return sign ? MPG123_ENC_SIGNED_24 : MPG123_ENC_UNSIGNED_24;
36 case 32:
37 return sign ? MPG123_ENC_SIGNED_32 : MPG123_ENC_UNSIGNED_32;
38 }
39 return -1;
40 }
41
42 static double rate_diff(long a, unsigned int b)
43 {
44 double ar = (double)a;
45 double br = (double)b;
46 return ( a>b ? (ar-br)/ar : ( a <= 0 ? 1. : (br-ar)/ar ) );
47 }
48
49 static int mpg123_to_sndio_enc(int enc, unsigned int *sig, unsigned int *bits)
50 {
51 switch(enc) {
52 case -1:
53 // Query default format only.
54 break;
55 case MPG123_ENC_SIGNED_32:
56 *sig = 1;
57 *bits = 32;
58 break;
59 case MPG123_ENC_UNSIGNED_32:
60 *sig = 0;
61 *bits = 32;
62 break;
63 case MPG123_ENC_SIGNED_24:
64 *sig = 1;
65 *bits = 24;
66 break;
67 case MPG123_ENC_UNSIGNED_24:
68 *sig = 0;
69 *bits = 24;
70 break;
71 case MPG123_ENC_SIGNED_16:
72 *sig = 1;
73 *bits = 16;
74 break;
75 case MPG123_ENC_UNSIGNED_16:
76 *sig = 0;
77 *bits = 16;
78 break;
79 case MPG123_ENC_UNSIGNED_8:
80 *sig = 0;
81 *bits = 8;
82 break;
83 case MPG123_ENC_SIGNED_8:
84 *sig = 1;
85 *bits = 8;
86 break;
87 default:
88 return -1;
89 }
90 return 0;
91 }
92
93
94 // Just check if there seems some support for stereo audio,
95 // take maximum channel count otherwise.
96 static unsigned int guess_channels(struct sio_hdl *hdl)
97 {
98 struct sio_cap cap;
99 unsigned int maxchan = 0;
100 unsigned int stereo_mask = 0;
101 unsigned int all_conf = 0;
102 if(!sio_getcap(hdl, &cap))
103 return 0; // Zero is as good as nothing.
104 // There is no specification of order in pchan[], So no guessing
105 // about which index for stereo.
106 for(int ci=0; ci<SIO_NCHAN; ++ci)
107 {
108 if(2 == cap.pchan[ci])
109 stereo_mask |= 1 << ci; // Maybe even multiple entries, eh?
110 }
111 for(unsigned int i=0; i<cap.nconf; ++i)
112 {
113 all_conf |= cap.confs[i].pchan;
114 }
115 for(int ci=0; ci<SIO_NCHAN; ++ci)
116 {
117 if(all_conf & (1 << ci) && cap.pchan[ci] > maxchan)
118 maxchan = cap.pchan[ci];
119 }
120 mdebug("maximum device channels: %u\n", maxchan);
121 return all_conf & stereo_mask ? 2 : maxchan;
122 }
123
124 static int open_sndio(out123_handle *ao)
125 {
126 struct sio_hdl *hdl;
127 struct sio_par par;
128
129 hdl = sio_open(ao->device /* NULL is fine */, SIO_PLAY, 0);
130 if (hdl == NULL)
131 {
132 if(!AOQUIET)
133 error("Got nothing from sio_open(). ");
134 return -1;
135 }
136
137 sio_initpar(&par);
138
139 par.le = SIO_LE_NATIVE;
140 if(ao->format != -1)
141 {
142 mdebug("Actually opening with %d channels, rate %ld.", ao->channels, ao->rate);
143 par.rate = ao->rate;
144 par.pchan = ao->channels;
145 } else
146 {
147 // Hack around buggy sndio versions up to 1.8.0 that fail to
148 // neuter the default value before handing to OSS driver.
149 // Also need to re-open, as sndio likes errors to be fatal.
150 if(!sio_setpar(hdl, &par))
151 {
152 sio_close(hdl);
153 hdl = sio_open(ao->device, SIO_PLAY, 0);
154 if(hdl == NULL)
155 {
156 if(!AOQUIET)
157 error("Re-opening of device for channel guessing failed.");
158 return -1;
159 }
160 par.pchan = guess_channels(hdl);
161 }
162 }
163
164 if(mpg123_to_sndio_enc(ao->format, &par.sig, &par.bits))
165 {
166 if (!AOQUIET)
167 error1("invalid sample format %d",
168 ao->format);
169 sio_close(hdl);
170 return -1;
171 }
172
173 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)
174 || par.le != SIO_LE_NATIVE )
175 {
176 if(!AOQUIET)
177 error("parameter setup failure");
178 sio_close(hdl);
179 return -1;
180 }
181 if(ao->format == -1) // Store default format.
182 {
183 ao->format = sndio_to_mpg123_enc(par.sig, par.bits);
184 ao->rate = par.rate;
185 ao->channels = par.pchan;
186 }
187 else
188 {
189 if( ao->format != sndio_to_mpg123_enc(par.sig, par.bits)
190 || ao->channels != par.pchan
191 || rate_diff(ao->rate, par.rate) > 0.005
192 )
193 {
194 if(!AOQUIET)
195 error("format not accepted as given");
196 sio_close(hdl);
197 return -1;
198 }
199 if(!sio_start(hdl))
200 {
201 if(!AOQUIET)
202 error("cannot start");
203 sio_close(hdl);
204 return -1;
205 }
206 }
207 ao->userptr = hdl;
208 return 0;
209 }
210
211 static int get_formats_sndio(out123_handle *ao)
212 {
213 struct sio_hdl *hdl = (struct sio_hdl *)ao->userptr;
214 struct sio_cap cap;
215 int fmt = 0;
216
217 // Direct querying with sio_setpar()/sio_getpar is too slow, so let's
218 // learn about the funky bitmask indexing of the capability stuff.
219 if(!sio_getcap(hdl, &cap))
220 {
221 if(!AOQUIET)
222 error("failure getting caps");
223 return 0;
224 }
225
226 unsigned int rmask = 0;
227 for(int ri=0; ri<SIO_NRATE; ++ri)
228 {
229 if(ao->rate == cap.rate[ri])
230 {
231 rmask |= 1 << ri;
232 break;
233 }
234 }
235 if(!rmask)
236 {
237 for(int ri=0; ri<SIO_NRATE; ++ri)
238 {
239 if(rate_diff(ao->rate, cap.rate[ri]) <= 0.005)
240 {
241 rmask |= 1 << ri;
242 break;
243 }
244 }
245 }
246
247 unsigned int cmask = 0;
248 for(int ci=0; ci<SIO_NCHAN; ++ci)
249 {
250 if(ao->channels == cap.pchan[ci])
251 {
252 cmask |= 1 << ci;
253 break;
254 }
255 }
256
257 if(!rmask || !cmask)
258 {
259 // no rate match, do the elaborate check.
260 debug("cap table does not help, doing elaborate format check");
261 static int fmts[] =
262 {
263 MPG123_ENC_SIGNED_8, MPG123_ENC_UNSIGNED_8
264 , MPG123_ENC_SIGNED_16, MPG123_ENC_UNSIGNED_16
265 , MPG123_ENC_SIGNED_24, MPG123_ENC_UNSIGNED_24
266 , MPG123_ENC_SIGNED_32, MPG123_ENC_UNSIGNED_32
267 };
268
269 for(int i=0;i<sizeof(fmts)/sizeof(int);i++)
270 {
271 struct sio_par par;
272 sio_initpar(&par);
273 par.le = SIO_LE_NATIVE;
274 mpg123_to_sndio_enc(fmts[i], &par.sig, &par.bits);
275 par.rate = ao->rate;
276 par.pchan = ao->channels;
277 if(sio_setpar(hdl, &par) && sio_getpar(hdl, &par))
278 {
279 if( par.le == SIO_LE_NATIVE
280 && fmts[i] == sndio_to_mpg123_enc(par.sig, par.bits)
281 && ao->channels == par.pchan
282 && rate_diff(ao->rate, par.rate) <= 0.005
283 )
284 fmt |= fmts[i];
285 }
286 }
287 } else
288 {
289 int menc[SIO_NENC];
290 for(int ei=0; ei<SIO_NENC; ++ei)
291 {
292 if(cap.enc[ei].le != SIO_LE_NATIVE)
293 menc[ei] = 0;
294 else
295 menc[ei] = sndio_to_mpg123_enc(cap.enc[ei].sig, cap.enc[ei].bits);
296 if(menc[ei] < 0)
297 menc[ei] = 0;
298 }
299 for(unsigned int i=0; i<cap.nconf; ++i)
300 {
301 if(cap.confs[i].pchan & cmask && cap.confs[i].rate & rmask)
302 for(int ei=0; ei<SIO_NENC; ++ei)
303 if(cap.confs[i].enc & (1<<ei))
304 fmt |= menc[ei];
305 }
306 }
307 return fmt;
308 }
309
310 static int write_sndio(out123_handle *ao, unsigned char *buf, int len)
311 {
312 struct sio_hdl *hdl = (struct sio_hdl *)ao->userptr;
313 int count;
314
315 count = (int)sio_write(hdl, buf, len);
316 if (count == 0 && sio_eof(hdl))
317 return -1;
318 return count;
319 }
320
321 static void flush_sndio(out123_handle *ao)
322 {
323 return;
324 }
325
326 static int close_sndio(out123_handle *ao)
327 {
328 struct sio_hdl *hdl = (struct sio_hdl *)ao->userptr;
329
330 if(hdl)
331 sio_close(hdl);
332 return 0;
333 }
334
335 static int init_sndio(out123_handle* ao)
336 {
337 if (ao == NULL)
338 return -1;
339
340 /* Set callbacks */
341 ao->open = open_sndio;
342 ao->flush = flush_sndio; /* required */
343 ao->write = write_sndio;
344 ao->get_formats = get_formats_sndio;
345 ao->close = close_sndio;
346
347 /* Success */
348 return 0;
349 }
350
351 /*
352 Module information data structure
353 */
354 mpg123_module_t mpg123_output_module_info = {
355 /* api_version */ MPG123_MODULE_API_VERSION,
356 /* name */ "sndio",
357 /* description */ "Output audio using sndio library",
358 /* revision */ "$Rev:$",
359 /* handle */ NULL,
360
361 /* init_output */ init_sndio,
362 };