]> code.delx.au - pulseaudio/blob - polyp/module-oss.c
add support for SCHED_FIFO
[pulseaudio] / polyp / module-oss.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include "iochannel.h"
39 #include "sink.h"
40 #include "source.h"
41 #include "module.h"
42 #include "oss-util.h"
43 #include "sample-util.h"
44 #include "util.h"
45 #include "modargs.h"
46 #include "xmalloc.h"
47
48 struct userdata {
49 struct pa_sink *sink;
50 struct pa_source *source;
51 struct pa_iochannel *io;
52 struct pa_core *core;
53
54 struct pa_memchunk memchunk, silence;
55
56 uint32_t in_fragment_size, out_fragment_size, sample_size;
57
58 int fd;
59 struct pa_module *module;
60 };
61
62 static const char* const valid_modargs[] = {
63 "sink_name",
64 "source_name",
65 "device",
66 "record",
67 "playback",
68 "fragments",
69 "fragment_size",
70 "format",
71 "rate",
72 "channels",
73 NULL
74 };
75
76 #define DEFAULT_SINK_NAME "oss_output"
77 #define DEFAULT_SOURCE_NAME "oss_input"
78 #define DEFAULT_DEVICE "/dev/dsp"
79
80 static void update_usage(struct userdata *u) {
81 pa_module_set_used(u->module,
82 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
83 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
84 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
85 }
86
87 static void do_write(struct userdata *u) {
88 struct pa_memchunk *memchunk;
89 ssize_t r;
90 assert(u);
91
92 if (!u->sink || !pa_iochannel_is_writable(u->io))
93 return;
94
95 update_usage(u);
96
97 memchunk = &u->memchunk;
98
99 if (!memchunk->length)
100 if (pa_sink_render(u->sink, u->out_fragment_size, memchunk) < 0)
101 memchunk = &u->silence;
102
103 assert(memchunk->memblock);
104 assert(memchunk->memblock->data);
105 assert(memchunk->length);
106
107 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
108 fprintf(stderr, "write() failed: %s\n", strerror(errno));
109 return;
110 }
111
112 if (memchunk == &u->silence)
113 assert(r % u->sample_size == 0);
114 else {
115 u->memchunk.index += r;
116 u->memchunk.length -= r;
117
118 if (u->memchunk.length <= 0) {
119 pa_memblock_unref(u->memchunk.memblock);
120 u->memchunk.memblock = NULL;
121 }
122 }
123 }
124
125 static void do_read(struct userdata *u) {
126 struct pa_memchunk memchunk;
127 ssize_t r;
128 assert(u);
129
130 if (!u->source || !pa_iochannel_is_readable(u->io))
131 return;
132
133 update_usage(u);
134
135 memchunk.memblock = pa_memblock_new(u->in_fragment_size, u->core->memblock_stat);
136 assert(memchunk.memblock);
137 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
138 pa_memblock_unref(memchunk.memblock);
139 if (errno != EAGAIN)
140 fprintf(stderr, "read() failed: %s\n", strerror(errno));
141 return;
142 }
143
144 assert(r <= (ssize_t) memchunk.memblock->length);
145 memchunk.length = memchunk.memblock->length = r;
146 memchunk.index = 0;
147
148 pa_source_post(u->source, &memchunk);
149 pa_memblock_unref(memchunk.memblock);
150 }
151
152 static void io_callback(struct pa_iochannel *io, void*userdata) {
153 struct userdata *u = userdata;
154 assert(u);
155 do_write(u);
156 do_read(u);
157 }
158
159 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
160 int arg;
161 struct userdata *u = s->userdata;
162 assert(s && u && u->sink);
163
164 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
165 fprintf(stderr, "module-oss: device doesn't support SNDCTL_DSP_GETODELAY.\n");
166 s->get_latency = NULL;
167 return 0;
168 }
169
170 return pa_bytes_to_usec(arg, &s->sample_spec);
171 }
172
173 int pa_module_init(struct pa_core *c, struct pa_module*m) {
174 struct audio_buf_info info;
175 struct userdata *u = NULL;
176 const char *p;
177 int fd = -1;
178 int nfrags, frag_size, in_frag_size, out_frag_size;
179 int mode;
180 uint32_t record = 1, playback = 1;
181 struct pa_sample_spec ss;
182 struct pa_modargs *ma = NULL;
183 assert(c && m);
184
185 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
186 fprintf(stderr, __FILE__": failed to parse module arguments.\n");
187 goto fail;
188 }
189
190 if (pa_modargs_get_value_u32(ma, "record", &record) < 0 || pa_modargs_get_value_u32(ma, "playback", &playback) < 0) {
191 fprintf(stderr, __FILE__": record= and playback= expect numeric argument.\n");
192 goto fail;
193 }
194
195 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
196 if (mode == 0) {
197 fprintf(stderr, __FILE__": neither playback nor record enabled for device.\n");
198 goto fail;
199 }
200
201 nfrags = 12;
202 frag_size = 1024;
203 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || nfrags < 2 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 || frag_size < 1) {
204 fprintf(stderr, __FILE__": failed to parse fragments arguments\n");
205 goto fail;
206 }
207
208 ss = c->default_sample_spec;
209 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
210 fprintf(stderr, __FILE__": failed to parse sample specification\n");
211 goto fail;
212 }
213
214 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
215 goto fail;
216
217 fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
218
219 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
220 goto fail;
221
222 if (pa_oss_auto_format(fd, &ss) < 0)
223 goto fail;
224
225 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
226 fprintf(stderr, "SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
227 goto fail;
228 }
229 assert(frag_size);
230 in_frag_size = out_frag_size = frag_size;
231
232 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
233 fprintf(stderr, "module-oss: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
234 in_frag_size = info.fragsize;
235 }
236
237 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
238 fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
239 out_frag_size = info.fragsize;
240 }
241
242 u = pa_xmalloc(sizeof(struct userdata));
243 u->core = c;
244
245 if (mode != O_WRONLY) {
246 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
247 assert(u->source);
248 u->source->userdata = u;
249 pa_source_set_owner(u->source, m);
250 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
251 } else
252 u->source = NULL;
253
254 if (mode != O_RDONLY) {
255 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
256 assert(u->sink);
257 u->sink->get_latency = sink_get_latency_cb;
258 u->sink->userdata = u;
259 pa_sink_set_owner(u->sink, m);
260 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
261 } else
262 u->sink = NULL;
263
264 assert(u->source || u->sink);
265
266 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
267 assert(u->io);
268 pa_iochannel_set_callback(u->io, io_callback, u);
269 u->fd = fd;
270
271 u->memchunk.memblock = NULL;
272 u->memchunk.length = 0;
273 u->sample_size = pa_frame_size(&ss);
274
275 u->out_fragment_size = out_frag_size;
276 u->in_fragment_size = in_frag_size;
277 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
278 assert(u->silence.memblock);
279 pa_silence_memblock(u->silence.memblock, &ss);
280 u->silence.index = 0;
281
282 u->module = m;
283 m->userdata = u;
284
285 pa_modargs_free(ma);
286
287 return 0;
288
289 fail:
290 if (fd >= 0)
291 close(fd);
292
293 if (ma)
294 pa_modargs_free(ma);
295
296 return -1;
297 }
298
299 void pa_module_done(struct pa_core *c, struct pa_module*m) {
300 struct userdata *u;
301 assert(c && m);
302
303 if (!(u = m->userdata))
304 return;
305
306 if (u->memchunk.memblock)
307 pa_memblock_unref(u->memchunk.memblock);
308 if (u->silence.memblock)
309 pa_memblock_unref(u->silence.memblock);
310
311 if (u->sink)
312 pa_sink_free(u->sink);
313 if (u->source)
314 pa_source_free(u->source);
315
316 pa_iochannel_free(u->io);
317 pa_xfree(u);
318 }