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