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