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
|
36465
|
45 static struct sio_hdl *hdl;
|
|
46 static struct pollfd *pfds;
|
36463
|
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");
|
36466
|
115 goto err_out;
|
36463
|
116 }
|
|
117 if (!sio_getpar(hdl, &par)) {
|
|
118 mp_msg(MSGT_AO, MSGL_ERR, "ao2: couldn't get params\n");
|
36466
|
119 goto err_out;
|
36463
|
120 }
|
|
121 if (par.bps != SIO_BPS(par.bits)) {
|
|
122 mp_msg(MSGT_AO, MSGL_ERR, "ao2: unsupported format\n");
|
36466
|
123 goto err_out;
|
36463
|
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");
|
36466
|
128 goto err_out;
|
36463
|
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");
|
36466
|
150 goto err_out;
|
36463
|
151 }
|
|
152 return 1;
|
36466
|
153 err_out:
|
36463
|
154 free(pfds);
|
|
155 pfds = NULL;
|
|
156 sio_close(hdl);
|
|
157 hdl = NULL;
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161 /*
|
|
162 * close device
|
|
163 */
|
|
164 static void uninit(int immed)
|
|
165 {
|
|
166 if (hdl)
|
|
167 sio_close(hdl);
|
36466
|
168 hdl = NULL;
|
|
169 free(pfds);
|
|
170 pfds = NULL;
|
36463
|
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 {
|
36467
|
191 int n = sio_pollfd(hdl, pfds, POLLOUT);
|
36463
|
192 while (poll(pfds, n, 0) < 0 && errno == EINTR)
|
|
193 ; /* nothing */
|
|
194 sio_revents(hdl, pfds);
|
|
195 }
|
|
196
|
|
197 /*
|
|
198 * return the number of bytes available in the device buffer
|
|
199 */
|
|
200 static int get_space(void)
|
|
201 {
|
|
202 refresh();
|
|
203 return par.bufsz * par.pchan * par.bps - delay;
|
|
204 }
|
|
205
|
|
206 /*
|
|
207 * delay in seconds between first and last sample in the buffer
|
|
208 */
|
|
209 static float get_delay(void)
|
|
210 {
|
|
211 refresh();
|
|
212 return (float)delay / (par.bps * par.pchan * par.rate);
|
|
213 }
|
|
214
|
|
215 /*
|
|
216 * submit the given number of bytes to the device
|
|
217 */
|
|
218 static int play(void *data, int len, int flags)
|
|
219 {
|
36467
|
220 int n = sio_write(hdl, data, len);
|
36463
|
221 delay += n;
|
|
222 return n;
|
|
223 }
|
|
224
|
|
225 /*
|
|
226 * pause playing
|
|
227 */
|
|
228 static void audio_pause(void)
|
|
229 {
|
|
230 /*
|
|
231 * sndio can't pause, so just stop
|
|
232 */
|
|
233 prepause_delay = delay;
|
|
234 if (!sio_stop(hdl))
|
|
235 mp_msg(MSGT_AO, MSGL_ERR, "ao2: pause: couldn't stop\n");
|
|
236 delay = 0;
|
|
237 }
|
|
238
|
|
239 /*
|
|
240 * resume playing after audio_pause()
|
|
241 */
|
|
242 static void audio_resume(void)
|
|
243 {
|
|
244 /*
|
|
245 * prepare to start; then refill the buffer with the number of bytes
|
|
246 * audio_pause() consumed (this will trigger start)
|
|
247 */
|
|
248 if (!sio_start(hdl))
|
|
249 mp_msg(MSGT_AO, MSGL_ERR, "ao2: resume: couldn't start\n");
|
|
250 mp_ao_resume_refill(&audio_out_sndio,
|
|
251 par.bufsz * par.pchan * par.bps - prepause_delay);
|
|
252 }
|