summaryrefslogtreecommitdiffstats
path: root/src/module-oss-mmap.c
blob: b70ea6bdac5bd05713cf780b934655142bd771ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>

#include "iochannel.h"
#include "sink.h"
#include "source.h"
#include "module.h"
#include "oss.h"
#include "sample-util.h"

struct userdata {
    struct sink *sink;
    struct source *source;
    struct core *core;
    struct pa_sample_spec sample_spec;

    size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;

    int fd;

    void *in_mmap, *out_mmap;
    size_t in_mmap_length, out_mmap_length;

    struct mainloop_source *mainloop_source;

    struct memblock **in_memblocks, **out_memblocks;
    unsigned out_current, in_current;
};

void module_done(struct core *c, struct module*m);

static void out_clear_memblocks(struct userdata*u, unsigned n) {
    unsigned i = u->out_current;
    assert(u && u->out_memblocks);

    if (n > u->out_fragments)
        n = u->out_fragments;
    
    while (n > 0) {
        if (u->out_memblocks[i]) {
            memblock_unref_fixed(u->out_memblocks[i]);
            u->out_memblocks[i] = NULL;
        }

        i++;
        while (i >= u->out_fragments)
            i -= u->out_fragments;

        n--;
    }
}

static void out_fill_memblocks(struct userdata *u, unsigned n) {
    assert(u && u->out_memblocks);
    
    while (n > 0) {
        struct memchunk chunk;
        
        if (!u->out_memblocks[u->out_current]) {

            u->out_memblocks[u->out_current] = chunk.memblock = memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current,  u->out_fragment_size);
            chunk.length = chunk.memblock->length;
            chunk.index = 0;
            
            sink_render_into_full(u->sink, &chunk);
        }
            
        u->out_current++;
        while (u->out_current >= u->out_fragments)
            u->out_current -= u->out_fragments;
        
        n--;
    }
}

static void do_write(struct userdata *u) {
    struct count_info info;
    assert(u && u->sink);

    if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
        fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno));
        return;
    }

    u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);

    if (!info.blocks)
        return;
    
    out_clear_memblocks(u, info.blocks);
    out_fill_memblocks(u, info.blocks);

}

static void in_post_memblocks(struct userdata *u, unsigned n) {
    assert(u && u->in_memblocks);
    
    while (n > 0) {
        struct memchunk chunk;
        
        if (!u->in_memblocks[u->in_current]) {
            u->in_memblocks[u->in_current] = chunk.memblock = memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
            chunk.length = chunk.memblock->length;
            chunk.index = 0;
            
            source_post(u->source, &chunk);
        }

        u->in_current++;
        while (u->in_current >= u->in_fragments)
            u->in_current -= u->in_fragments;
        
        n--;
    }
}

static void in_clear_memblocks(struct userdata*u, unsigned n) {
    unsigned i = u->in_current;
    assert(u && u->in_memblocks);

    if (n > u->in_fragments)
        n = u->in_fragments;
    
    while (n > 0) {
        if (u->in_memblocks[i]) {
            memblock_unref_fixed(u->in_memblocks[i]);
            u->in_memblocks[i] = NULL;
        }

        i++;
        while (i >= u->in_fragments)
            i -= u->in_fragments;

        n--;
    }
}

static void do_read(struct userdata *u) {
    struct count_info info;
    assert(u && u->source);

    if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
        fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno));
        return;
    }

    if (!info.blocks)
        return;
    
    in_post_memblocks(u, info.blocks);
    in_clear_memblocks(u, u->in_fragments/2);
};

static void io_callback(struct pa_mainloop_api *m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
    struct userdata *u = userdata;

    assert (u && u->core->mainloop == m && u->mainloop_source == id);

    if (events & PA_MAINLOOP_API_IO_EVENT_INPUT)
        do_read(u);
    if (events & PA_MAINLOOP_API_IO_EVENT_OUTPUT)
        do_write(u);
}

static uint32_t sink_get_latency_cb(struct sink *s) {
    struct userdata *u = s->userdata;
    assert(s && u);

    do_write(u);
    return pa_samples_usec(u->out_fill, &s->sample_spec);
}

int module_init(struct core *c, struct module*m) {
    struct audio_buf_info info;
    struct userdata *u = NULL;
    char *p;
    int frag_size;
    int mode, caps, caps_read = 0;
    int enable_bits = 0, zero = 0;
    assert(c && m);

    m->userdata = u = malloc(sizeof(struct userdata));
    assert(u);
    memset(u, 0, sizeof(struct userdata));
    u->fd = -1;
    u->core = c;
    
    p = m->argument ? m->argument : "/dev/dsp";
    if ((u->fd = open(p, (mode = O_RDWR)|O_NDELAY)) >= 0) {
        ioctl(u->fd, SNDCTL_DSP_SETDUPLEX, 0);
        
        if (ioctl(u->fd, SNDCTL_DSP_GETCAPS, &caps) < 0) {
            fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
            goto fail;
        }

        if (!(caps & DSP_CAP_DUPLEX)) {
            close(u->fd);
            u->fd = -1;
        } else
            caps_read = 1;
    }

    if (u->fd < 0) {
        if ((u->fd = open(p, (mode = O_WRONLY)|O_NDELAY)) < 0) {
            if ((u->fd = open(p, (mode = O_RDONLY)|O_NDELAY)) < 0) {
                fprintf(stderr, "open('%s'): %s\n", p, strerror(errno));
                goto fail;
            }
        }
    }

    if (!caps_read) {
        if (ioctl(u->fd, SNDCTL_DSP_GETCAPS, &caps) < 0) {
            fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
            goto fail;
        }
    }
        
    if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
        fprintf(stderr, "OSS device not mmap capable.\n");
        goto fail;
    }

    fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
    
    frag_size = ((int) 12 << 16) | 10; /* nfrags = 12; frag_size = 2^10 */
    if (ioctl(u->fd, SNDCTL_DSP_SETFRAGMENT, &frag_size) < 0) {
        fprintf(stderr, "SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));
        goto fail;
    }

    if (oss_auto_format(u->fd, &u->sample_spec) < 0)
        goto fail;

    if (mode != O_WRONLY) {
        if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
            fprintf(stderr, "SNDCTL_DSP_GETISPACE: %s\n", strerror(errno));
            goto fail;
        }

        fprintf(stderr, "module-oss-mmap: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
        u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);

        if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
            if (mode == O_RDWR) {
                fprintf(stderr, "module-oss-mmap: mmap failed for input. Changing to O_WRONLY mode.\n");
                mode = O_WRONLY;
            } else {
                fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
                goto fail;
            }
        } else {
        
            u->source = source_new(c, "dsp", &u->sample_spec);
            assert(u->source);
            u->source->userdata = u;
            
            u->in_memblocks = malloc(sizeof(struct memblock *)*u->in_fragments);
            memset(u->in_memblocks, 0, sizeof(struct memblock *)*u->in_fragments);
            
            enable_bits |= PCM_ENABLE_INPUT;
        }
    }

    if (m != O_RDONLY) {
        if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
            fprintf(stderr, "SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno));
            goto fail;
        }
        
        fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
        u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);

        if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0))  == MAP_FAILED) {
            if (mode == O_RDWR) {
                fprintf(stderr, "module-oss-mmap: mmap filed for output. Changing to O_RDONLY mode.\n");
                mode = O_RDONLY;
            } else {
                fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
                goto fail;
            }
        } else {
            silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
            
            u->sink = sink_new(c, "dsp", &u->sample_spec);
            assert(u->sink);
            u->sink->get_latency = sink_get_latency_cb;
            u->sink->userdata = u;
            
            u->out_memblocks = malloc(sizeof(struct memblock *)*u->out_fragments);
            memset(u->out_memblocks, 0, sizeof(struct memblock *)*u->out_fragments);
            
            enable_bits |= PCM_ENABLE_OUTPUT;
        }
    }

    zero = 0;
    if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
        fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
        goto fail;
    }
    
    if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
        fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
        goto fail;
    }
        
    assert(u->source || u->sink);

    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);
    assert(u->mainloop_source);

    return 0;

fail:
    module_done(c, m);

    return -1;
}

void module_done(struct core *c, struct module*m) {
    struct userdata *u;
    assert(c && m);

    u = m->userdata;
    assert(u);

    if (u->out_memblocks) {
        out_clear_memblocks(u, u->out_fragments);
        free(u->out_memblocks);
    }

    if (u->in_memblocks) {
        in_clear_memblocks(u, u->in_fragments);
        free(u->in_memblocks);
    }
    
    if (u->in_mmap && u->in_mmap != MAP_FAILED)
        munmap(u->in_mmap, u->in_mmap_length);
    
    if (u->out_mmap && u->out_mmap != MAP_FAILED)
        munmap(u->out_mmap, u->out_mmap_length);
    
    if (u->sink)
        sink_free(u->sink);

    if (u->source)
        source_free(u->source);

    if (u->mainloop_source)
        u->core->mainloop->cancel_io(u->core->mainloop, u->mainloop_source);

    if (u->fd >= 0)
        close(u->fd);

    free(u);
}