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