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