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