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