summaryrefslogtreecommitdiff
path: root/src/module-pipe-sink.c
blob: 9dcf5d2316ebe716ddcf7ced8de072df1252848a (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
#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 "iochannel.h"
#include "sink.h"
#include "module.h"

struct userdata {
    char *filename;
    
    struct sink *sink;
    struct iochannel *io;
    struct core *core;
    void *mainloop_source;
    struct pa_mainloop_api *mainloop;

    struct memchunk memchunk;
};

static void do_write(struct userdata *u) {
    ssize_t r;
    assert(u);

    u->mainloop->enable_fixed(u->mainloop, u->mainloop_source, 0);
        
    if (!iochannel_is_writable(u->io))
        return;

    if (!u->memchunk.length)
        if (sink_render(u->sink, PIPE_BUF, &u->memchunk) < 0)
            return;

    assert(u->memchunk.memblock && u->memchunk.length);
    
    if ((r = iochannel_write(u->io, u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
        fprintf(stderr, "write() failed: %s\n", strerror(errno));
        return;
    }

    u->memchunk.index += r;
    u->memchunk.length -= r;
        
    if (u->memchunk.length <= 0) {
        memblock_unref(u->memchunk.memblock);
        u->memchunk.memblock = NULL;
    }
}

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

    if (iochannel_is_writable(u->io))
        u->mainloop->enable_fixed(u->mainloop, u->mainloop_source, 1);
}

static void fixed_callback(struct pa_mainloop_api *m, void *id, void *userdata) {
    struct userdata *u = userdata;
    assert(u);
    do_write(u);
}

static void io_callback(struct iochannel *io, void*userdata) {
    struct userdata *u = userdata;
    assert(u);
    do_write(u);
}

int module_init(struct core *c, struct module*m) {
    struct userdata *u = NULL;
    struct stat st;
    char *p;
    int fd = -1;
    static const struct pa_sample_spec ss = {
        .format = SAMPLE_S16NE,
        .rate = 44100,
        .channels = 2,
    };
    assert(c && m);

    mkfifo((p = m->argument ? m->argument : "/tmp/musicfifo"), 0777);

    if ((fd = open(p, O_RDWR)) < 0) {
        fprintf(stderr, "open('%s'): %s\n", p, strerror(errno));
        goto fail;
    }

    if (fstat(fd, &st) < 0) {
        fprintf(stderr, "fstat('%s'): %s\n", p, strerror(errno));
        goto fail;
    }

    if (!S_ISFIFO(st.st_mode)) {
        fprintf(stderr, "'%s' is not a FIFO\n", p);
        goto fail;
    }

    
    u = malloc(sizeof(struct userdata));
    assert(u);

    u->filename = strdup(p);
    assert(u->filename);
    u->core = c;
    u->sink = sink_new(c, "fifo", &ss);
    assert(u->sink);
    u->sink->notify = notify_cb;
    u->sink->userdata = u;

    u->io = iochannel_new(c->mainloop, -1, fd);
    assert(u->io);
    iochannel_set_callback(u->io, io_callback, u);

    u->memchunk.memblock = NULL;
    u->memchunk.length = 0;

    u->mainloop = c->mainloop;
    u->mainloop_source = u->mainloop->source_fixed(u->mainloop, fixed_callback, u);
    assert(u->mainloop_source);
    u->mainloop->enable_fixed(u->mainloop, u->mainloop_source, 0);
        
    m->userdata = u;

    return 0;

fail:
    if (fd >= 0)
        close(fd);

    return -1;
}

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

    u = m->userdata;
    assert(u);
    
    if (u->memchunk.memblock)
        memblock_unref(u->memchunk.memblock);
        
    sink_free(u->sink);
    iochannel_free(u->io);
    u->mainloop->cancel_fixed(u->mainloop, u->mainloop_source);

    assert(u->filename);
    unlink(u->filename);
    free(u->filename);
    
    free(u);
}