add description field for sinks/sources
[profile/ivi/pulseaudio.git] / src / module-oss-mmap.c
1 #include <sys/soundcard.h>
2 #include <sys/ioctl.h>
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <stdio.h>
6 #include <assert.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #include <sys/mman.h>
13
14 #include "iochannel.h"
15 #include "sink.h"
16 #include "source.h"
17 #include "module.h"
18 #include "oss-util.h"
19 #include "sample-util.h"
20 #include "util.h"
21
22 struct userdata {
23     struct pa_sink *sink;
24     struct pa_source *source;
25     struct pa_core *core;
26     struct pa_sample_spec sample_spec;
27
28     size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;
29
30     int fd;
31
32     void *in_mmap, *out_mmap;
33     size_t in_mmap_length, out_mmap_length;
34
35     void *mainloop_source;
36
37     struct pa_memblock **in_memblocks, **out_memblocks;
38     unsigned out_current, in_current;
39 };
40
41 void module_done(struct pa_core *c, struct pa_module*m);
42
43
44 static void out_fill_memblocks(struct userdata *u, unsigned n) {
45     assert(u && u->out_memblocks);
46     
47     while (n > 0) {
48         struct pa_memchunk chunk;
49         
50         if (u->out_memblocks[u->out_current])
51             pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
52             
53         chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size);
54         assert(chunk.memblock);
55         chunk.length = chunk.memblock->length;
56         chunk.index = 0;
57         
58         pa_sink_render_into_full(u->sink, &chunk);
59             
60         u->out_current++;
61         while (u->out_current >= u->out_fragments)
62             u->out_current -= u->out_fragments;
63         
64         n--;
65     }
66 }
67
68 static void do_write(struct userdata *u) {
69     struct count_info info;
70     assert(u && u->sink);
71
72     if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
73         fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno));
74         return;
75     }
76
77     u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);
78
79     if (!info.blocks)
80         return;
81     
82     out_fill_memblocks(u, info.blocks);
83 }
84
85 static void in_post_memblocks(struct userdata *u, unsigned n) {
86     assert(u && u->in_memblocks);
87     
88     while (n > 0) {
89         struct pa_memchunk chunk;
90         
91         if (!u->in_memblocks[u->in_current]) {
92             chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
93             chunk.length = chunk.memblock->length;
94             chunk.index = 0;
95             
96             pa_source_post(u->source, &chunk);
97         }
98
99         u->in_current++;
100         while (u->in_current >= u->in_fragments)
101             u->in_current -= u->in_fragments;
102         
103         n--;
104     }
105 }
106
107 static void in_clear_memblocks(struct userdata*u, unsigned n) {
108     unsigned i = u->in_current;
109     assert(u && u->in_memblocks);
110
111     if (n > u->in_fragments)
112         n = u->in_fragments;
113     
114     while (n > 0) {
115         if (u->in_memblocks[i]) {
116             pa_memblock_unref_fixed(u->in_memblocks[i]);
117             u->in_memblocks[i] = NULL;
118         }
119
120         i++;
121         while (i >= u->in_fragments)
122             i -= u->in_fragments;
123
124         n--;
125     }
126 }
127
128 static void do_read(struct userdata *u) {
129     struct count_info info;
130     assert(u && u->source);
131
132     if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
133         fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno));
134         return;
135     }
136
137     if (!info.blocks)
138         return;
139     
140     in_post_memblocks(u, info.blocks);
141     in_clear_memblocks(u, u->in_fragments/2);
142 };
143
144 static void io_callback(struct pa_mainloop_api *m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
145     struct userdata *u = userdata;
146
147     assert (u && u->core->mainloop == m && u->mainloop_source == id);
148
149     if (events & PA_MAINLOOP_API_IO_EVENT_INPUT)
150         do_read(u);
151     if (events & PA_MAINLOOP_API_IO_EVENT_OUTPUT)
152         do_write(u);
153 }
154
155 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
156     struct userdata *u = s->userdata;
157     assert(s && u);
158
159     do_write(u);
160     return pa_samples_usec(u->out_fill, &s->sample_spec);
161 }
162
163 int pa_module_init(struct pa_core *c, struct pa_module*m) {
164     struct audio_buf_info info;
165     struct userdata *u = NULL;
166     char *p;
167     int frag_size;
168     int mode, caps, caps_read = 0;
169     int enable_bits = 0, zero = 0;
170     assert(c && m);
171
172     m->userdata = u = malloc(sizeof(struct userdata));
173     assert(u);
174     memset(u, 0, sizeof(struct userdata));
175     u->fd = -1;
176     u->core = c;
177     
178     p = m->argument ? m->argument : "/dev/dsp";
179     if ((u->fd = open(p, (mode = O_RDWR)|O_NDELAY)) >= 0) {
180         ioctl(u->fd, SNDCTL_DSP_SETDUPLEX, 0);
181         
182         if (ioctl(u->fd, SNDCTL_DSP_GETCAPS, &caps) < 0) {
183             fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
184             goto fail;
185         }
186
187         if (!(caps & DSP_CAP_DUPLEX)) {
188             close(u->fd);
189             u->fd = -1;
190         } else
191             caps_read = 1;
192     }
193
194     if (u->fd < 0) {
195         if ((u->fd = open(p, (mode = O_WRONLY)|O_NDELAY)) < 0) {
196             if ((u->fd = open(p, (mode = O_RDONLY)|O_NDELAY)) < 0) {
197                 fprintf(stderr, "open('%s'): %s\n", p, strerror(errno));
198                 goto fail;
199             }
200         }
201     }
202
203     if (!caps_read) {
204         if (ioctl(u->fd, SNDCTL_DSP_GETCAPS, &caps) < 0) {
205             fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
206             goto fail;
207         }
208     }
209         
210     if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
211         fprintf(stderr, "OSS device not mmap capable.\n");
212         goto fail;
213     }
214
215     fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
216     
217     frag_size = ((int) 12 << 16) | 10; /* nfrags = 12; frag_size = 2^10 */
218     if (ioctl(u->fd, SNDCTL_DSP_SETFRAGMENT, &frag_size) < 0) {
219         fprintf(stderr, "SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));
220         goto fail;
221     }
222
223     if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
224         goto fail;
225
226     if (mode != O_WRONLY) {
227         if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
228             fprintf(stderr, "SNDCTL_DSP_GETISPACE: %s\n", strerror(errno));
229             goto fail;
230         }
231
232         fprintf(stderr, "module-oss-mmap: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
233         u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
234
235         if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
236             if (mode == O_RDWR) {
237                 fprintf(stderr, "module-oss-mmap: mmap failed for input. Changing to O_WRONLY mode.\n");
238                 mode = O_WRONLY;
239             } else {
240                 fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
241                 goto fail;
242             }
243         } else {
244         
245             u->source = pa_source_new(c, "oss_input", 0, &u->sample_spec);
246             assert(u->source);
247             u->source->userdata = u;
248             pa_source_set_owner(u->source, m);
249             u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
250             
251             
252             u->in_memblocks = malloc(sizeof(struct pa_memblock *)*u->in_fragments);
253             memset(u->in_memblocks, 0, sizeof(struct pa_memblock *)*u->in_fragments);
254             
255             enable_bits |= PCM_ENABLE_INPUT;
256         }
257     }
258
259     if (mode != O_RDONLY) {
260         if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
261             fprintf(stderr, "SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno));
262             goto fail;
263         }
264         
265         fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
266         u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
267
268         if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0))  == MAP_FAILED) {
269             if (mode == O_RDWR) {
270                 fprintf(stderr, "module-oss-mmap: mmap filed for output. Changing to O_RDONLY mode.\n");
271                 mode = O_RDONLY;
272             } else {
273                 fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
274                 goto fail;
275             }
276         } else {
277             pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
278             
279             u->sink = pa_sink_new(c, "oss_output", 0, &u->sample_spec);
280             assert(u->sink);
281             u->sink->get_latency = sink_get_latency_cb;
282             u->sink->userdata = u;
283             pa_sink_set_owner(u->sink, m);
284             u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
285             
286             u->out_memblocks = malloc(sizeof(struct memblock *)*u->out_fragments);
287             memset(u->out_memblocks, 0, sizeof(struct pa_memblock *)*u->out_fragments);
288             
289             enable_bits |= PCM_ENABLE_OUTPUT;
290         }
291     }
292
293     zero = 0;
294     if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
295         fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
296         goto fail;
297     }
298     
299     if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
300         fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
301         goto fail;
302     }
303         
304     assert(u->source || u->sink);
305
306     u->mainloop_source = c->mainloop->source_io(c->mainloop, u->fd, (u->source ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) | (u->sink ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), io_callback, u);
307     assert(u->mainloop_source);
308
309     return 0;
310
311 fail:
312     module_done(c, m);
313
314     return -1;
315 }
316
317 void pa_module_done(struct pa_core *c, struct pa_module*m) {
318     struct userdata *u;
319     assert(c && m);
320
321     u = m->userdata;
322     assert(u);
323
324     if (u->out_memblocks) {
325         unsigned i;
326         for (i = 0; i < u->out_fragments; i++)
327             if (u->out_memblocks[i])
328                 pa_memblock_unref_fixed(u->out_memblocks[i]);
329         free(u->out_memblocks);
330     }
331
332     if (u->in_memblocks) {
333         unsigned i;
334         for (i = 0; i < u->in_fragments; i++)
335             if (u->in_memblocks[i])
336                 pa_memblock_unref_fixed(u->in_memblocks[i]);
337         free(u->in_memblocks);
338     }
339     
340     if (u->in_mmap && u->in_mmap != MAP_FAILED)
341         munmap(u->in_mmap, u->in_mmap_length);
342     
343     if (u->out_mmap && u->out_mmap != MAP_FAILED)
344         munmap(u->out_mmap, u->out_mmap_length);
345     
346     if (u->sink)
347         pa_sink_free(u->sink);
348
349     if (u->source)
350         pa_source_free(u->source);
351
352     if (u->mainloop_source)
353         u->core->mainloop->cancel_io(u->core->mainloop, u->mainloop_source);
354
355     if (u->fd >= 0)
356         close(u->fd);
357
358     free(u);
359 }