a9e130866b96fef136688241d2d96ecde17dcaec
[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 #include "modargs.h"
22
23 struct userdata {
24     struct pa_sink *sink;
25     struct pa_source *source;
26     struct pa_core *core;
27     struct pa_sample_spec sample_spec;
28
29     size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;
30
31     int fd;
32
33     void *in_mmap, *out_mmap;
34     size_t in_mmap_length, out_mmap_length;
35
36     void *mainloop_source;
37
38     struct pa_memblock **in_memblocks, **out_memblocks;
39     unsigned out_current, in_current;
40 };
41
42 static const char* const valid_modargs[] = {
43     "sink_name",
44     "source_name",
45     "device",
46     "record",
47     "playback",
48     NULL
49 };
50
51 #define DEFAULT_SINK_NAME "oss_output"
52 #define DEFAULT_SOURCE_NAME "oss_input"
53 #define DEFAULT_DEVICE "/dev/dsp"
54
55 static void out_fill_memblocks(struct userdata *u, unsigned n) {
56     assert(u && u->out_memblocks);
57     
58     while (n > 0) {
59         struct pa_memchunk chunk;
60         
61         if (u->out_memblocks[u->out_current])
62             pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
63             
64         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);
65         assert(chunk.memblock);
66         chunk.length = chunk.memblock->length;
67         chunk.index = 0;
68         
69         pa_sink_render_into_full(u->sink, &chunk);
70             
71         u->out_current++;
72         while (u->out_current >= u->out_fragments)
73             u->out_current -= u->out_fragments;
74         
75         n--;
76     }
77 }
78
79 static void do_write(struct userdata *u) {
80     struct count_info info;
81     assert(u && u->sink);
82
83     if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
84         fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno));
85         return;
86     }
87
88     u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);
89
90     if (!info.blocks)
91         return;
92     
93     out_fill_memblocks(u, info.blocks);
94 }
95
96 static void in_post_memblocks(struct userdata *u, unsigned n) {
97     assert(u && u->in_memblocks);
98     
99     while (n > 0) {
100         struct pa_memchunk chunk;
101         
102         if (!u->in_memblocks[u->in_current]) {
103             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);
104             chunk.length = chunk.memblock->length;
105             chunk.index = 0;
106             
107             pa_source_post(u->source, &chunk);
108         }
109
110         u->in_current++;
111         while (u->in_current >= u->in_fragments)
112             u->in_current -= u->in_fragments;
113         
114         n--;
115     }
116 }
117
118 static void in_clear_memblocks(struct userdata*u, unsigned n) {
119     unsigned i = u->in_current;
120     assert(u && u->in_memblocks);
121
122     if (n > u->in_fragments)
123         n = u->in_fragments;
124     
125     while (n > 0) {
126         if (u->in_memblocks[i]) {
127             pa_memblock_unref_fixed(u->in_memblocks[i]);
128             u->in_memblocks[i] = NULL;
129         }
130
131         i++;
132         while (i >= u->in_fragments)
133             i -= u->in_fragments;
134
135         n--;
136     }
137 }
138
139 static void do_read(struct userdata *u) {
140     struct count_info info;
141     assert(u && u->source);
142
143     if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
144         fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno));
145         return;
146     }
147
148     if (!info.blocks)
149         return;
150     
151     in_post_memblocks(u, info.blocks);
152     in_clear_memblocks(u, u->in_fragments/2);
153 };
154
155 static void io_callback(struct pa_mainloop_api *m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
156     struct userdata *u = userdata;
157
158     assert (u && u->core->mainloop == m && u->mainloop_source == id);
159
160     if (events & PA_MAINLOOP_API_IO_EVENT_INPUT)
161         do_read(u);
162     if (events & PA_MAINLOOP_API_IO_EVENT_OUTPUT)
163         do_write(u);
164 }
165
166 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
167     struct userdata *u = s->userdata;
168     assert(s && u);
169
170     do_write(u);
171     return pa_samples_usec(u->out_fill, &s->sample_spec);
172 }
173
174 int pa_module_init(struct pa_core *c, struct pa_module*m) {
175     struct audio_buf_info info;
176     struct userdata *u = NULL;
177     const char *p;
178     int frag_size;
179     int mode, caps;
180     int enable_bits = 0, zero = 0;
181     int playback = 1, record = 1;
182     struct pa_modargs *ma = NULL;
183     assert(c && m);
184
185     m->userdata = u = malloc(sizeof(struct userdata));
186     assert(u);
187     memset(u, 0, sizeof(struct userdata));
188     u->fd = -1;
189     u->core = c;
190
191     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
192         fprintf(stderr, __FILE__": failed to parse module arguments.\n");
193         goto fail;
194     }
195     
196     if (pa_modargs_get_value_u32(ma, "record", &record) < 0 || pa_modargs_get_value_u32(ma, "playback", &playback) < 0) {
197         fprintf(stderr, __FILE__": record= and playback= expect numeric arguments.\n");
198         goto fail;
199     }
200
201     mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
202     if (mode == 0) {
203         fprintf(stderr, __FILE__": neither playback nor record enabled for device.\n");
204         goto fail;
205     }
206
207     if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
208         goto fail;
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, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 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, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 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     pa_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 }