]> code.delx.au - pulseaudio/blob - polyp/module-oss-mmap.c
introduce pa_xmalloc() and friends
[pulseaudio] / polyp / module-oss-mmap.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 #include <sys/mman.h>
38
39 #include "iochannel.h"
40 #include "sink.h"
41 #include "source.h"
42 #include "module.h"
43 #include "oss-util.h"
44 #include "sample-util.h"
45 #include "util.h"
46 #include "modargs.h"
47 #include "xmalloc.h"
48
49 struct userdata {
50 struct pa_sink *sink;
51 struct pa_source *source;
52 struct pa_core *core;
53 struct pa_sample_spec sample_spec;
54
55 size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;
56
57 int fd;
58
59 void *in_mmap, *out_mmap;
60 size_t in_mmap_length, out_mmap_length;
61
62 void *mainloop_source;
63
64 struct pa_memblock **in_memblocks, **out_memblocks;
65 unsigned out_current, in_current;
66 struct pa_module *module;
67 };
68
69 static const char* const valid_modargs[] = {
70 "sink_name",
71 "source_name",
72 "device",
73 "record",
74 "playback",
75 "fragments",
76 "fragment_size",
77 "format",
78 "rate",
79 "channels",
80 NULL
81 };
82
83 #define DEFAULT_SINK_NAME "oss_output"
84 #define DEFAULT_SOURCE_NAME "oss_input"
85 #define DEFAULT_DEVICE "/dev/dsp"
86
87 static void update_usage(struct userdata *u) {
88 pa_module_set_used(u->module,
89 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
90 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
91 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
92 }
93
94 static void out_fill_memblocks(struct userdata *u, unsigned n) {
95 assert(u && u->out_memblocks);
96
97 while (n > 0) {
98 struct pa_memchunk chunk;
99
100 if (u->out_memblocks[u->out_current])
101 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
102
103 chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size);
104 assert(chunk.memblock);
105 chunk.length = chunk.memblock->length;
106 chunk.index = 0;
107
108 pa_sink_render_into_full(u->sink, &chunk);
109
110 u->out_current++;
111 while (u->out_current >= u->out_fragments)
112 u->out_current -= u->out_fragments;
113
114 n--;
115 }
116 }
117
118 static void do_write(struct userdata *u) {
119 struct count_info info;
120 assert(u && u->sink);
121
122 update_usage(u);
123
124 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
125 fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno));
126 return;
127 }
128
129 u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);
130
131 if (!info.blocks)
132 return;
133
134 out_fill_memblocks(u, info.blocks);
135 }
136
137 static void in_post_memblocks(struct userdata *u, unsigned n) {
138 assert(u && u->in_memblocks);
139
140 while (n > 0) {
141 struct pa_memchunk chunk;
142
143 if (!u->in_memblocks[u->in_current]) {
144 chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
145 chunk.length = chunk.memblock->length;
146 chunk.index = 0;
147
148 pa_source_post(u->source, &chunk);
149 }
150
151 u->in_current++;
152 while (u->in_current >= u->in_fragments)
153 u->in_current -= u->in_fragments;
154
155 n--;
156 }
157 }
158
159 static void in_clear_memblocks(struct userdata*u, unsigned n) {
160 unsigned i = u->in_current;
161 assert(u && u->in_memblocks);
162
163 if (n > u->in_fragments)
164 n = u->in_fragments;
165
166 while (n > 0) {
167 if (u->in_memblocks[i]) {
168 pa_memblock_unref_fixed(u->in_memblocks[i]);
169 u->in_memblocks[i] = NULL;
170 }
171
172 i++;
173 while (i >= u->in_fragments)
174 i -= u->in_fragments;
175
176 n--;
177 }
178 }
179
180 static void do_read(struct userdata *u) {
181 struct count_info info;
182 assert(u && u->source);
183
184 update_usage(u);
185
186 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
187 fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno));
188 return;
189 }
190
191 if (!info.blocks)
192 return;
193
194 in_post_memblocks(u, info.blocks);
195 in_clear_memblocks(u, u->in_fragments/2);
196 };
197
198 static void io_callback(struct pa_mainloop_api *m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
199 struct userdata *u = userdata;
200
201 assert (u && u->core->mainloop == m && u->mainloop_source == id);
202
203 if (events & PA_MAINLOOP_API_IO_EVENT_INPUT)
204 do_read(u);
205 if (events & PA_MAINLOOP_API_IO_EVENT_OUTPUT)
206 do_write(u);
207 }
208
209 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
210 struct userdata *u = s->userdata;
211 assert(s && u);
212
213 do_write(u);
214 return pa_bytes_to_usec(u->out_fill, &s->sample_spec);
215 }
216
217 int pa_module_init(struct pa_core *c, struct pa_module*m) {
218 struct audio_buf_info info;
219 struct userdata *u = NULL;
220 const char *p;
221 int nfrags, frag_size;
222 int mode, caps;
223 int enable_bits = 0, zero = 0;
224 int playback = 1, record = 1;
225 struct pa_modargs *ma = NULL;
226 assert(c && m);
227
228 m->userdata = u = pa_xmalloc0(sizeof(struct userdata));
229 u->module = m;
230 u->fd = -1;
231 u->core = c;
232
233 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
234 fprintf(stderr, __FILE__": failed to parse module arguments.\n");
235 goto fail;
236 }
237
238 if (pa_modargs_get_value_u32(ma, "record", &record) < 0 || pa_modargs_get_value_u32(ma, "playback", &playback) < 0) {
239 fprintf(stderr, __FILE__": record= and playback= expect numeric arguments.\n");
240 goto fail;
241 }
242
243 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
244 if (mode == 0) {
245 fprintf(stderr, __FILE__": neither playback nor record enabled for device.\n");
246 goto fail;
247 }
248
249 nfrags = 12;
250 frag_size = 1024;
251 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) {
252 fprintf(stderr, __FILE__": failed to parse fragments arguments\n");
253 goto fail;
254 }
255
256 u->sample_spec = c->default_sample_spec;
257 if (pa_modargs_get_sample_spec(ma, &u->sample_spec) < 0) {
258 fprintf(stderr, __FILE__": failed to parse sample specification\n");
259 goto fail;
260 }
261
262 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
263 goto fail;
264
265 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
266 fprintf(stderr, "OSS device not mmap capable.\n");
267 goto fail;
268 }
269
270 fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
271
272 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
273 goto fail;
274
275 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
276 goto fail;
277
278 if (mode != O_WRONLY) {
279 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
280 fprintf(stderr, "SNDCTL_DSP_GETISPACE: %s\n", strerror(errno));
281 goto fail;
282 }
283
284 fprintf(stderr, "module-oss-mmap: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
285 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
286
287 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
288 if (mode == O_RDWR) {
289 fprintf(stderr, "module-oss-mmap: mmap failed for input. Changing to O_WRONLY mode.\n");
290 mode = O_WRONLY;
291 } else {
292 fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
293 goto fail;
294 }
295 } else {
296
297 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec);
298 assert(u->source);
299 u->source->userdata = u;
300 pa_source_set_owner(u->source, m);
301 u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
302
303 u->in_memblocks = pa_xmalloc0(sizeof(struct pa_memblock *)*u->in_fragments);
304
305 enable_bits |= PCM_ENABLE_INPUT;
306 }
307 }
308
309 if (mode != O_RDONLY) {
310 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
311 fprintf(stderr, "SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno));
312 goto fail;
313 }
314
315 fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
316 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
317
318 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
319 if (mode == O_RDWR) {
320 fprintf(stderr, "module-oss-mmap: mmap filed for output. Changing to O_RDONLY mode.\n");
321 mode = O_RDONLY;
322 } else {
323 fprintf(stderr, "module-oss-mmap: mmap(): %s\n", strerror(errno));
324 goto fail;
325 }
326 } else {
327 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
328
329 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec);
330 assert(u->sink);
331 u->sink->get_latency = sink_get_latency_cb;
332 u->sink->userdata = u;
333 pa_sink_set_owner(u->sink, m);
334 u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
335
336 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
337
338 enable_bits |= PCM_ENABLE_OUTPUT;
339 }
340 }
341
342 zero = 0;
343 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
344 fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
345 goto fail;
346 }
347
348 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
349 fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
350 goto fail;
351 }
352
353 assert(u->source || u->sink);
354
355 u->mainloop_source = c->mainloop->source_io(c->mainloop, u->fd, (u->source ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) | (u->sink ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), io_callback, u);
356 assert(u->mainloop_source);
357
358 pa_modargs_free(ma);
359
360 return 0;
361
362 fail:
363 pa_module_done(c, m);
364
365 if (ma)
366 pa_modargs_free(ma);
367
368 return -1;
369 }
370
371 void pa_module_done(struct pa_core *c, struct pa_module*m) {
372 struct userdata *u;
373 assert(c && m);
374
375 if (!(u = m->userdata))
376 return;
377
378 if (u->out_memblocks) {
379 unsigned i;
380 for (i = 0; i < u->out_fragments; i++)
381 if (u->out_memblocks[i])
382 pa_memblock_unref_fixed(u->out_memblocks[i]);
383 pa_xfree(u->out_memblocks);
384 }
385
386 if (u->in_memblocks) {
387 unsigned i;
388 for (i = 0; i < u->in_fragments; i++)
389 if (u->in_memblocks[i])
390 pa_memblock_unref_fixed(u->in_memblocks[i]);
391 pa_xfree(u->in_memblocks);
392 }
393
394 if (u->in_mmap && u->in_mmap != MAP_FAILED)
395 munmap(u->in_mmap, u->in_mmap_length);
396
397 if (u->out_mmap && u->out_mmap != MAP_FAILED)
398 munmap(u->out_mmap, u->out_mmap_length);
399
400 if (u->sink)
401 pa_sink_free(u->sink);
402
403 if (u->source)
404 pa_source_free(u->source);
405
406 if (u->mainloop_source)
407 u->core->mainloop->cancel_io(u->core->mainloop, u->mainloop_source);
408
409 if (u->fd >= 0)
410 close(u->fd);
411
412 pa_xfree(u);
413 }