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