36463
|
1 /*
|
|
2 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
|
|
3 *
|
|
4 * This file is part of MPlayer.
|
|
5 *
|
|
6 * MPlayer is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * MPlayer 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 General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License along
|
|
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 */
|
|
20 #include <sys/types.h>
|
|
21 #include <poll.h>
|
|
22 #include <errno.h>
|
|
23 #include <sndio.h>
|
|
24 #include <stdlib.h>
|
|
25
|
|
26 #include "config.h"
|
|
27 #include "mp_msg.h"
|
|
28 #include "mixer.h"
|
|
29 #include "help_mp.h"
|
|
30
|
|
31 #include "libaf/af_format.h"
|
|
32
|
|
33 #include "audio_out.h"
|
|
34 #include "audio_out_internal.h"
|
|
35
|
|
36 static ao_info_t info = {
|
|
37 "sndio audio output",
|
|
38 "sndio",
|
|
39 "Alexandre Ratchov <alex@caoua.org>",
|
|
40 ""
|
|
41 };
|
|
42
|
|
43 LIBAO_EXTERN(sndio)
|
|
44
|
|
45 static struct sio_hdl *hdl = NULL;
|
|
46 struct pollfd *pfds = NULL;
|
|
47 static struct sio_par par;
|
|
48 static int delay, vol, havevol;
|
|
49 static int prepause_delay;
|
|
50
|
|
51 /*
|
|
52 * control misc parameters (only the volume for now)
|
|
53 */
|
|
54 static int control(int cmd, void *arg)
|
|
55 {
|
|
56 ao_control_vol_t *ctl = arg;
|
|
57
|
|
58 switch (cmd) {
|
|
59 case AOCONTROL_GET_VOLUME:
|
|
60 if (!havevol)
|
|
61 return CONTROL_FALSE;
|
|
62 ctl->left = ctl->right = vol * 100 / SIO_MAXVOL;
|
|
63 break;
|
|
64 case AOCONTROL_SET_VOLUME:
|
|
65 if (!havevol)
|
|
66 return CONTROL_FALSE;
|
|
67 sio_setvol(hdl, ctl->left * SIO_MAXVOL / 100);
|
|
68 break;
|
|
69 default:
|
|
70 return CONTROL_UNKNOWN;
|
|
71 }
|
|
72 return CONTROL_OK;
|
|
73 }
|
|
74
|
|
75 /*
|
|
76 * call-back invoked whenever the the hardware position changes
|
|
77 */
|
|
78 static void movecb(void *addr, int delta)
|
|
79 {
|
|
80 delay -= delta * (int)(par.bps * par.pchan);
|
|
81 }
|
|
82
|
|
83 /*
|
|
84 * call-back invoked whenever the volume changes
|
|
85 */
|
|
86 static void volcb(void *addr, unsigned newvol)
|
|
87 {
|
|
88 vol = newvol;
|
|
89 }
|
|
90
|
|
91 /*
|
|
92 * open device and setup parameters
|
|
93 * return: 1 = success, 0 = failure
|
|
94 */
|
|
95 static int init(int rate, int channels, int format, int flags)
|
|
96 {
|
|
97 int bpf;
|
|
98
|
|
99 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
|
|
100 if (hdl == NULL) {
|
|
101 mp_msg(MSGT_AO, MSGL_ERR, "ao2: can't open sndio\n");
|
|
102 return 0;
|
|
103 }
|
|
104 sio_initpar(&par);
|
|
105 par.bits = af_fmt2bits(format);
|
|
106 par.sig = (format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI;
|
|
107 if (par.bits > 8)
|
|
108 par.le = (format & AF_FORMAT_END_MASK) == AF_FORMAT_LE;
|
|
109 par.rate = rate;
|
|
110 par.pchan = channels;
|
|
111 par.appbufsz = par.rate * 250 / 1000; /* 250ms buffer */
|
|
112 par.round = par.rate * 10 / 1000; /* 10ms block size */
|
|
113 if (!sio_setpar(hdl, &par)) {
|
|
114 mp_msg(MSGT_AO, MSGL_ERR, "ao2: couldn't set params\n");
|
|
115 goto bad_close;
|
|
116 }
|
|
117 if (!sio_getpar(hdl, &par)) {
|
|
118 mp_msg(MSGT_AO, MSGL_ERR, "ao2: couldn't get params\n");
|
|
119 goto bad_close;
|
|
120 }
|
|
121 if (par.bps != SIO_BPS(par.bits)) {
|
|
122 mp_msg(MSGT_AO, MSGL_ERR, "ao2: unsupported format\n");
|
|
123 goto bad_close;
|
|
124 }
|
|
125 pfds = calloc(sio_nfds(hdl), sizeof(*pfds));
|
|
126 if (pfds == NULL) {
|
|
127 mp_msg(MSGT_AO, MSGL_ERR, "ao2: couldn't allocate poll fds\n");
|
|
128 goto bad_close;
|
|
129 }
|
|
130 bpf = par.bps * par.pchan;
|
|
131 ao_data.format = af_bits2fmt(8 * par.bps);
|
|
132 ao_data.format |= par.sig ? AF_FORMAT_SI : AF_FORMAT_US;
|
|
133 if (par.bits > 8)
|
|
134 ao_data.format |= par.le ? AF_FORMAT_LE : AF_FORMAT_BE;
|
|
135 ao_data.channels = par.pchan;
|
|
136 ao_data.bps = bpf * par.rate;
|
|
137 ao_data.buffersize = par.bufsz * bpf;
|
|
138 ao_data.outburst = par.round * bpf;
|
|
139 ao_data.samplerate = rate;
|
|
140 havevol = sio_onvol(hdl, volcb, NULL);
|
|
141 sio_onmove(hdl, movecb, NULL);
|
|
142
|
|
143 /*
|
|
144 * prepare the device to start. It will start as soon there's enough
|
|
145 * data in the buffer to not underrun
|
|
146 */
|
|
147 delay = 0;
|
|
148 if (!sio_start(hdl)) {
|
|
149 mp_msg(MSGT_AO, MSGL_ERR, "ao2: init: couldn't start\n");
|
|
150 goto bad_free;
|
|
151 }
|
|
152 return 1;
|
|
153 bad_free:
|
|
154 free(pfds);
|
|
155 pfds = NULL;
|
|
156 bad_close:
|
|
157 sio_close(hdl);
|
|
158 hdl = NULL;
|
|
159 return 0;
|
|
160 }
|
|
161
|
|
162 /*
|
|
163 * close device
|
|
164 */
|
|
165 static void uninit(int immed)
|
|
166 {
|
|
167 if (hdl)
|
|
168 sio_close(hdl);
|
|
169 if (pfds)
|
|
170 free(pfds);
|
|
171 }
|
|
172
|
|
173 /*
|
|
174 * stop playing and prepare to restart
|
|
175 */
|
|
176 static void reset(void)
|
|
177 {
|
|
178 if (!sio_stop(hdl))
|
|
179 mp_msg(MSGT_AO, MSGL_ERR, "ao2: reset: couldn't stop\n");
|
|
180 delay = 0;
|
|
181 if (!sio_start(hdl))
|
|
182 mp_msg(MSGT_AO, MSGL_ERR, "ao2: reset: couldn't start\n");
|
|
183 }
|
|
184
|
|
185 /*
|
|
186 * refresh the "delay" counter: call sio_revents() which keeps track of
|
|
187 * the hardware position
|
|
188 */
|
|
189 static void refresh(void)
|
|
190 {
|
|
191 int n;
|
|
192
|
|
193 n = sio_pollfd(hdl, pfds, POLLOUT);
|
|
194 while (poll(pfds, n, 0) < 0 && errno == EINTR)
|
|
195 ; /* nothing */
|
|
196 sio_revents(hdl, pfds);
|
|
197 }
|
|
198
|
|
199 /*
|
|
200 * return the number of bytes available in the device buffer
|
|
201 */
|
|
202 static int get_space(void)
|
|
203 {
|
|
204 refresh();
|
|
205 return par.bufsz * par.pchan * par.bps - delay;
|
|
206 }
|
|
207
|
|
208 /*
|
|
209 * delay in seconds between first and last sample in the buffer
|
|
210 */
|
|
211 static float get_delay(void)
|
|
212 {
|
|
213 refresh();
|
|
214 return (float)delay / (par.bps * par.pchan * par.rate);
|
|
215 }
|
|
216
|
|
217 /*
|
|
218 * submit the given number of bytes to the device
|
|
219 */
|
|
220 static int play(void *data, int len, int flags)
|
|
221 {
|
|
222 int n;
|
|
223
|
|
224 n = sio_write(hdl, data, len);
|
|
225 delay += n;
|
|
226 return n;
|
|
227 }
|
|
228
|
|
229 /*
|
|
230 * pause playing
|
|
231 */
|
|
232 static void audio_pause(void)
|
|
233 {
|
|
234 /*
|
|
235 * sndio can't pause, so just stop
|
|
236 */
|
|
237 prepause_delay = delay;
|
|
238 if (!sio_stop(hdl))
|
|
239 mp_msg(MSGT_AO, MSGL_ERR, "ao2: pause: couldn't stop\n");
|
|
240 delay = 0;
|
|
241 }
|
|
242
|
|
243 /*
|
|
244 * resume playing after audio_pause()
|
|
245 */
|
|
246 static void audio_resume(void)
|
|
247 {
|
|
248 /*
|
|
249 * prepare to start; then refill the buffer with the number of bytes
|
|
250 * audio_pause() consumed (this will trigger start)
|
|
251 */
|
|
252 if (!sio_start(hdl))
|
|
253 mp_msg(MSGT_AO, MSGL_ERR, "ao2: resume: couldn't start\n");
|
|
254 mp_ao_resume_refill(&audio_out_sndio,
|
|
255 par.bufsz * par.pchan * par.bps - prepause_delay);
|
|
256 }
|