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