]> code.delx.au - pulseaudio/blob - src/modules/module-oss-mmap.c
Wrap strerror() in a function that makes it thread safe and converts the
[pulseaudio] / src / modules / 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 <polyp/error.h>
40 #include <polyp/xmalloc.h>
41
42 #include <polypcore/iochannel.h>
43 #include <polypcore/sink.h>
44 #include <polypcore/source.h>
45 #include <polypcore/module.h>
46 #include <polypcore/sample-util.h>
47 #include <polypcore/core-util.h>
48 #include <polypcore/modargs.h>
49 #include <polypcore/log.h>
50
51 #include "oss-util.h"
52 #include "module-oss-mmap-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE(
58 "sink_name=<name for the sink> "
59 "source_name=<name for the source> "
60 "device=<OSS device> "
61 "record=<enable source?> "
62 "playback=<enable sink?> "
63 "format=<sample format> "
64 "channels=<number of channels> "
65 "rate=<sample rate> "
66 "fragments=<number of fragments> "
67 "fragment_size=<fragment size> "
68 "channel_map=<channel map>")
69
70 struct userdata {
71 pa_sink *sink;
72 pa_source *source;
73 pa_core *core;
74 pa_sample_spec sample_spec;
75
76 size_t in_fragment_size, out_fragment_size;
77 unsigned in_fragments, out_fragments;
78 unsigned out_blocks_saved, in_blocks_saved;
79
80 int fd;
81
82 void *in_mmap, *out_mmap;
83 size_t in_mmap_length, out_mmap_length;
84
85 pa_io_event *io_event;
86
87 pa_memblock **in_memblocks, **out_memblocks;
88 unsigned out_current, in_current;
89 pa_module *module;
90 };
91
92 static const char* const valid_modargs[] = {
93 "sink_name",
94 "source_name",
95 "device",
96 "record",
97 "playback",
98 "fragments",
99 "fragment_size",
100 "format",
101 "rate",
102 "channels",
103 "channel_map",
104 NULL
105 };
106
107 #define DEFAULT_SINK_NAME "oss_output"
108 #define DEFAULT_SOURCE_NAME "oss_input"
109 #define DEFAULT_DEVICE "/dev/dsp"
110 #define DEFAULT_NFRAGS 12
111 #define DEFAULT_FRAGSIZE 1024
112
113 static void update_usage(struct userdata *u) {
114 pa_module_set_used(u->module,
115 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
116 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
117 (u->source ? pa_idxset_size(u->source->outputs) : 0));
118 }
119
120 static void out_fill_memblocks(struct userdata *u, unsigned n) {
121 assert(u && u->out_memblocks);
122
123 while (n > 0) {
124 pa_memchunk chunk;
125
126 if (u->out_memblocks[u->out_current])
127 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
128
129 chunk.memblock = u->out_memblocks[u->out_current] =
130 pa_memblock_new_fixed(
131 (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
132 u->out_fragment_size,
133 1,
134 u->core->memblock_stat);
135 assert(chunk.memblock);
136 chunk.length = chunk.memblock->length;
137 chunk.index = 0;
138
139 pa_sink_render_into_full(u->sink, &chunk);
140
141 u->out_current++;
142 while (u->out_current >= u->out_fragments)
143 u->out_current -= u->out_fragments;
144
145 n--;
146 }
147 }
148
149 static void do_write(struct userdata *u) {
150 struct count_info info;
151 assert(u && u->sink);
152
153 update_usage(u);
154
155 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
156 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
157 return;
158 }
159
160 info.blocks += u->out_blocks_saved;
161 u->out_blocks_saved = 0;
162
163 if (!info.blocks)
164 return;
165
166 out_fill_memblocks(u, info.blocks);
167 }
168
169 static void in_post_memblocks(struct userdata *u, unsigned n) {
170 assert(u && u->in_memblocks);
171
172 while (n > 0) {
173 pa_memchunk chunk;
174
175 if (!u->in_memblocks[u->in_current]) {
176 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);
177 chunk.length = chunk.memblock->length;
178 chunk.index = 0;
179
180 pa_source_post(u->source, &chunk);
181 }
182
183 u->in_current++;
184 while (u->in_current >= u->in_fragments)
185 u->in_current -= u->in_fragments;
186
187 n--;
188 }
189 }
190
191 static void in_clear_memblocks(struct userdata*u, unsigned n) {
192 unsigned i = u->in_current;
193 assert(u && u->in_memblocks);
194
195 if (n > u->in_fragments)
196 n = u->in_fragments;
197
198 while (n > 0) {
199 if (u->in_memblocks[i]) {
200 pa_memblock_unref_fixed(u->in_memblocks[i]);
201 u->in_memblocks[i] = NULL;
202 }
203
204 i++;
205 while (i >= u->in_fragments)
206 i -= u->in_fragments;
207
208 n--;
209 }
210 }
211
212 static void do_read(struct userdata *u) {
213 struct count_info info;
214 assert(u && u->source);
215
216 update_usage(u);
217
218 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
219 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
220 return;
221 }
222
223 info.blocks += u->in_blocks_saved;
224 u->in_blocks_saved = 0;
225
226 if (!info.blocks)
227 return;
228
229 in_post_memblocks(u, info.blocks);
230 in_clear_memblocks(u, u->in_fragments/2);
231 }
232
233 static void io_callback(pa_mainloop_api *m, pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t f, void *userdata) {
234 struct userdata *u = userdata;
235 assert (u && u->core->mainloop == m && u->io_event == e);
236
237 if (f & PA_IO_EVENT_INPUT)
238 do_read(u);
239 if (f & PA_IO_EVENT_OUTPUT)
240 do_write(u);
241 }
242
243 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
244 struct userdata *u = s->userdata;
245 struct count_info info;
246 size_t bpos, n, total;
247 assert(s && u);
248
249 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
250 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
251 return 0;
252 }
253
254 u->out_blocks_saved += info.blocks;
255
256 total = u->out_fragments * u->out_fragment_size;
257 bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
258
259 if (bpos <= (size_t) info.ptr)
260 n = total - (info.ptr - bpos);
261 else
262 n = bpos - info.ptr;
263
264 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
265
266 return pa_bytes_to_usec(n, &s->sample_spec);
267 }
268
269 static pa_usec_t source_get_latency_cb(pa_source *s) {
270 struct userdata *u = s->userdata;
271 struct count_info info;
272 size_t bpos, n, total;
273 assert(s && u);
274
275 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
276 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
277 return 0;
278 }
279
280 u->in_blocks_saved += info.blocks;
281
282 total = u->in_fragments * u->in_fragment_size;
283 bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
284
285 if (bpos <= (size_t) info.ptr)
286 n = info.ptr - bpos;
287 else
288 n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
289
290 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
291
292 return pa_bytes_to_usec(n, &s->sample_spec);
293 }
294
295 static int sink_get_hw_volume(pa_sink *s) {
296 struct userdata *u = s->userdata;
297
298 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
299 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
300 s->get_hw_volume = NULL;
301 return -1;
302 }
303
304 return 0;
305 }
306
307 static int sink_set_hw_volume(pa_sink *s) {
308 struct userdata *u = s->userdata;
309
310 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
311 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
312 s->set_hw_volume = NULL;
313 return -1;
314 }
315
316 return 0;
317 }
318
319 static int source_get_hw_volume(pa_source *s) {
320 struct userdata *u = s->userdata;
321
322 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
323 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
324 s->get_hw_volume = NULL;
325 return -1;
326 }
327
328 return 0;
329 }
330
331 static int source_set_hw_volume(pa_source *s) {
332 struct userdata *u = s->userdata;
333
334 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
335 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
336 s->set_hw_volume = NULL;
337 return -1;
338 }
339
340 return 0;
341 }
342
343 int pa__init(pa_core *c, pa_module*m) {
344 struct audio_buf_info info;
345 struct userdata *u = NULL;
346 const char *p;
347 int nfrags, frag_size;
348 int mode, caps;
349 int enable_bits = 0, zero = 0;
350 int playback = 1, record = 1;
351 pa_modargs *ma = NULL;
352 char hwdesc[64];
353 pa_channel_map map;
354
355 assert(c);
356 assert(m);
357
358 m->userdata = u = pa_xnew0(struct userdata, 1);
359 u->module = m;
360 u->fd = -1;
361 u->core = c;
362
363 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
364 pa_log(__FILE__": failed to parse module arguments.");
365 goto fail;
366 }
367
368 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
369 pa_log(__FILE__": record= and playback= expect numeric arguments.");
370 goto fail;
371 }
372
373 if (!playback && !record) {
374 pa_log(__FILE__": neither playback nor record enabled for device.");
375 goto fail;
376 }
377
378 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
379
380 nfrags = DEFAULT_NFRAGS;
381 frag_size = DEFAULT_FRAGSIZE;
382 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
383 pa_log(__FILE__": failed to parse fragments arguments");
384 goto fail;
385 }
386
387 u->sample_spec = c->default_sample_spec;
388 if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
389 pa_log(__FILE__": failed to parse sample specification or channel map");
390 goto fail;
391 }
392
393 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
394 goto fail;
395
396 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
397 pa_log(__FILE__": OSS device not mmap capable.");
398 goto fail;
399 }
400
401 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
402
403 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
404 pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
405 else
406 hwdesc[0] = 0;
407
408 if (nfrags >= 2 && frag_size >= 1)
409 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
410 goto fail;
411
412 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
413 goto fail;
414
415 if (mode != O_WRONLY) {
416 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
417 pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
418 goto fail;
419 }
420
421 pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
422 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
423
424 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
425 if (mode == O_RDWR) {
426 pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
427 mode = O_WRONLY;
428 } else {
429 pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
430 goto fail;
431 }
432 } else {
433
434 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, &map)))
435 goto fail;
436
437 u->source->userdata = u;
438 u->source->get_latency = source_get_latency_cb;
439 u->source->get_hw_volume = source_get_hw_volume;
440 u->source->set_hw_volume = source_set_hw_volume;
441 pa_source_set_owner(u->source, m);
442 u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
443 p,
444 hwdesc[0] ? " (" : "",
445 hwdesc[0] ? hwdesc : "",
446 hwdesc[0] ? ")" : "");
447
448 u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
449
450 enable_bits |= PCM_ENABLE_INPUT;
451 }
452 }
453
454 if (mode != O_RDONLY) {
455 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
456 pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
457 goto fail;
458 }
459
460 pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
461 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
462
463 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
464 if (mode == O_RDWR) {
465 pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
466 mode = O_RDONLY;
467 } else {
468 pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
469 goto fail;
470 }
471 } else {
472 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
473
474 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, &map)))
475 goto fail;
476
477 u->sink->get_latency = sink_get_latency_cb;
478 u->sink->get_hw_volume = sink_get_hw_volume;
479 u->sink->set_hw_volume = sink_set_hw_volume;
480 u->sink->userdata = u;
481 pa_sink_set_owner(u->sink, m);
482 u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
483 p,
484 hwdesc[0] ? " (" : "",
485 hwdesc[0] ? hwdesc : "",
486 hwdesc[0] ? ")" : "");
487
488 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
489
490 enable_bits |= PCM_ENABLE_OUTPUT;
491 }
492 }
493
494 zero = 0;
495 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
496 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
497 goto fail;
498 }
499
500 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
501 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
502 goto fail;
503 }
504
505 assert(u->source || u->sink);
506
507 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);
508 assert(u->io_event);
509
510 pa_modargs_free(ma);
511
512 /* Read mixer settings */
513 if (u->source)
514 source_get_hw_volume(u->source);
515 if (u->sink)
516 sink_get_hw_volume(u->sink);
517
518 return 0;
519
520 fail:
521 pa__done(c, m);
522
523 if (ma)
524 pa_modargs_free(ma);
525
526 return -1;
527 }
528
529 void pa__done(pa_core *c, pa_module*m) {
530 struct userdata *u;
531
532 assert(c);
533 assert(m);
534
535 if (!(u = m->userdata))
536 return;
537
538 if (u->out_memblocks) {
539 unsigned i;
540 for (i = 0; i < u->out_fragments; i++)
541 if (u->out_memblocks[i])
542 pa_memblock_unref_fixed(u->out_memblocks[i]);
543 pa_xfree(u->out_memblocks);
544 }
545
546 if (u->in_memblocks) {
547 unsigned i;
548 for (i = 0; i < u->in_fragments; i++)
549 if (u->in_memblocks[i])
550 pa_memblock_unref_fixed(u->in_memblocks[i]);
551 pa_xfree(u->in_memblocks);
552 }
553
554 if (u->in_mmap && u->in_mmap != MAP_FAILED)
555 munmap(u->in_mmap, u->in_mmap_length);
556
557 if (u->out_mmap && u->out_mmap != MAP_FAILED)
558 munmap(u->out_mmap, u->out_mmap_length);
559
560 if (u->sink) {
561 pa_sink_disconnect(u->sink);
562 pa_sink_unref(u->sink);
563 }
564
565 if (u->source) {
566 pa_source_disconnect(u->source);
567 pa_source_unref(u->source);
568 }
569
570 if (u->io_event)
571 u->core->mainloop->io_free(u->io_event);
572
573 if (u->fd >= 0)
574 close(u->fd);
575
576 pa_xfree(u);
577 }