]> code.delx.au - pulseaudio/blob - polyp/module-oss.c
introduce pa_xmalloc() and friends
[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 if (!u->memchunk.length) {
98 if (pa_sink_render(u->sink, u->out_fragment_size, &u->memchunk) < 0)
99 memchunk = &u->silence;
100 else
101 memchunk = &u->memchunk;
102 }
103
104 assert(memchunk->memblock && memchunk->length);
105
106 if ((r = pa_iochannel_write(u->io, memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
107 fprintf(stderr, "write() failed: %s\n", strerror(errno));
108 return;
109 }
110
111 if (memchunk == &u->silence)
112 assert(r % u->sample_size == 0);
113 else {
114 u->memchunk.index += r;
115 u->memchunk.length -= r;
116
117 if (u->memchunk.length <= 0) {
118 pa_memblock_unref(u->memchunk.memblock);
119 u->memchunk.memblock = NULL;
120 }
121 }
122 }
123
124 static void do_read(struct userdata *u) {
125 struct pa_memchunk memchunk;
126 ssize_t r;
127 assert(u);
128
129 if (!u->source || !pa_iochannel_is_readable(u->io))
130 return;
131
132 update_usage(u);
133
134 memchunk.memblock = pa_memblock_new(u->in_fragment_size);
135 assert(memchunk.memblock);
136 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
137 pa_memblock_unref(memchunk.memblock);
138 if (errno != EAGAIN)
139 fprintf(stderr, "read() failed: %s\n", strerror(errno));
140 return;
141 }
142
143 assert(r <= (ssize_t) memchunk.memblock->length);
144 memchunk.length = memchunk.memblock->length = r;
145 memchunk.index = 0;
146
147 pa_source_post(u->source, &memchunk);
148 pa_memblock_unref(memchunk.memblock);
149 };
150
151 static void io_callback(struct pa_iochannel *io, void*userdata) {
152 struct userdata *u = userdata;
153 assert(u);
154 do_write(u);
155 do_read(u);
156 }
157
158 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
159 int arg;
160 struct userdata *u = s->userdata;
161 assert(s && u && u->sink);
162
163 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
164 fprintf(stderr, "module-oss: device doesn't support SNDCTL_DSP_GETODELAY.\n");
165 s->get_latency = NULL;
166 return 0;
167 }
168
169 return pa_bytes_to_usec(arg, &s->sample_spec);
170 }
171
172 int pa_module_init(struct pa_core *c, struct pa_module*m) {
173 struct audio_buf_info info;
174 struct userdata *u = NULL;
175 const char *p;
176 int fd = -1;
177 int nfrags, frag_size, in_frag_size, out_frag_size;
178 int mode;
179 uint32_t record = 1, playback = 1;
180 struct pa_sample_spec ss;
181 struct pa_modargs *ma = NULL;
182 assert(c && m);
183
184 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
185 fprintf(stderr, __FILE__": failed to parse module arguments.\n");
186 goto fail;
187 }
188
189 if (pa_modargs_get_value_u32(ma, "record", &record) < 0 || pa_modargs_get_value_u32(ma, "playback", &playback) < 0) {
190 fprintf(stderr, __FILE__": record= and playback= expect numeric argument.\n");
191 goto fail;
192 }
193
194 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
195 if (mode == 0) {
196 fprintf(stderr, __FILE__": neither playback nor record enabled for device.\n");
197 goto fail;
198 }
199
200 nfrags = 12;
201 frag_size = 1024;
202 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) {
203 fprintf(stderr, __FILE__": failed to parse fragments arguments\n");
204 goto fail;
205 }
206
207 ss = c->default_sample_spec;
208 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
209 fprintf(stderr, __FILE__": failed to parse sample specification\n");
210 goto fail;
211 }
212
213 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
214 goto fail;
215
216 fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
217
218 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
219 goto fail;
220
221 if (pa_oss_auto_format(fd, &ss) < 0)
222 goto fail;
223
224 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
225 fprintf(stderr, "SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
226 goto fail;
227 }
228 assert(frag_size);
229 in_frag_size = out_frag_size = frag_size;
230
231 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
232 fprintf(stderr, "module-oss: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
233 in_frag_size = info.fragsize;
234 }
235
236 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
237 fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
238 out_frag_size = info.fragsize;
239 }
240
241 u = pa_xmalloc(sizeof(struct userdata));
242 u->core = c;
243
244 if (mode != O_WRONLY) {
245 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
246 assert(u->source);
247 u->source->userdata = u;
248 pa_source_set_owner(u->source, m);
249 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
250 } else
251 u->source = NULL;
252
253 if (mode != O_RDONLY) {
254 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
255 assert(u->sink);
256 u->sink->get_latency = sink_get_latency_cb;
257 u->sink->userdata = u;
258 pa_sink_set_owner(u->sink, m);
259 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
260 } else
261 u->sink = NULL;
262
263 assert(u->source || u->sink);
264
265 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
266 assert(u->io);
267 pa_iochannel_set_callback(u->io, io_callback, u);
268 u->fd = fd;
269
270 u->memchunk.memblock = NULL;
271 u->memchunk.length = 0;
272 u->sample_size = pa_frame_size(&ss);
273
274 u->out_fragment_size = out_frag_size;
275 u->in_fragment_size = in_frag_size;
276 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size);
277 assert(u->silence.memblock);
278 pa_silence_memblock(u->silence.memblock, &ss);
279 u->silence.index = 0;
280
281 u->module = m;
282 m->userdata = u;
283
284 pa_modargs_free(ma);
285
286 return 0;
287
288 fail:
289 if (fd >= 0)
290 close(fd);
291
292 if (ma)
293 pa_modargs_free(ma);
294
295 return -1;
296 }
297
298 void pa_module_done(struct pa_core *c, struct pa_module*m) {
299 struct userdata *u;
300 assert(c && m);
301
302 if (!(u = m->userdata))
303 return;
304
305 if (u->memchunk.memblock)
306 pa_memblock_unref(u->memchunk.memblock);
307 if (u->silence.memblock)
308 pa_memblock_unref(u->silence.memblock);
309
310 if (u->sink)
311 pa_sink_free(u->sink);
312 if (u->source)
313 pa_source_free(u->source);
314
315 pa_iochannel_free(u->io);
316 pa_xfree(u);
317 }