summaryrefslogtreecommitdiffstats
path: root/src/thread.c
blob: 0384f49dd721f3980246eb27e8a8d0960cc54e6c (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
#include <polyp/mainloop.h>

static pthread_cond_t request_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t request_mutex = PTHREAD_MUTEX_INITIALIZER;

struct request {
    enum { MESSAGE_OPEN, MESSAGE_CLOSE, MESSAGE_WRITE, MESSAGE_FLUSH, MESSAGE_PAUSE } type;
    void *data;
    struct pa_sample_spec ss;
    size_t length;
    int success, done;
};

static struct request* current_request = NULL;

static int pipe_fds[2] = { -1, -1};

static pthread_t thread_id;
static int thread_running = 0;
static struct pa_context *context = NULL;
static struct pa_stream *stream = NULL;
static struct pa_mainloop_api *mainloop_api = NULL;
static int failed = 0;

static void finish_request(int success) {
    failed = 1;

    pthread_mutex_lock(&request_mutex);

    if (current_request) {
        current_request->done = 1;
        current_request->success = success;
        pthread_cond_signal(&request_cond);
    }

    pthread_mutex_unlock(&request_mutex);
}

static void stream_state_callback(struct pa_stream *s, void *userdata) {
    assert(stream == s);
    
    switch(pa_stream_get_state(c)) {
        case PA_STREAM_CREATING:
            break;
        case PA_STREAM_READY:
            assert(current_request && current_request->type == MESSAGE_OPEN);
            finish_request(1);
            break;
        default:
            finish_request(0);
    }
}

static void context_state_callback(struct pa_context *c, void *userdata) {
    assert(c == context);

    switch (pa_context_get_state(c)) {
        case PA_CONTEXT_CONNECTING:
        case PA_CONTEXT_AUTHORIZING:
        case PA_CONTEXT_SETTING_NAME:
            break;

        case PA_CONTEXT_READY :
            assert(!stream && current_request);
            stream = pa_stream_new(c, &current_request->ss);
            assert(stream);

            pa_stream_set_state(stream, stream_state_callback);
            pa_stream_connect_playback(stream, NULL, NULL);
            break;
        
        default:
            finish_request(0);
    }
}

static void context_drain_callback(struct pa_context *c, void *userdata) {
    assert(c == context);
    mainloop_api->quit(mainloop_api, 0);
}

static void request_func(struct pa_mainloop*api, struct pa_io_event *io, enum pa_io_event_flags f, void *userdata) {
    char x;
    
    assert(api && io && f == PA_IO_EVENT_INPUT);

    read(pipe_fds[0], &x, 1);
    
    pthread_mutex_lock(&request_mutex);

    if (current_request) {
        if (failed) {
            fail();
        } else {
            switch (current_request->type) {
                case MESSAGE_OPEN:
                    assert(!context && !stream);
                    context = pa_context_new(api, "xmms");
                    assert(context);
                    pa_context_set_state_callback(context, context_state_callback, NULL);
                    pa_context_connect(context, NULL);
                    break;

                case MESSAGE_CLOSE: {
                    struct pa_operation *o;
                    assert(context);
                    if ((o = pa_context_drain(context, context_drain_callback, NULL)))
                        pa_operation_unref(o);
                    else
                        api->quit(mainloop_api, 0);
                    break;
                }
                    
                case MESSAGE_WRITE:
                    assert(context && stream && current_request->data && current_request->length > 0);
                    pa_stream_write(stream, current_request->data, current_request->length, free, 0);
                    break;

                case MESSAGE_FLUSH:
                    assert(context && stream);
                    pa_stream_flush
            }
        }
    }
    
    pthread_mutex_unlock(&request_mutex);
}

static void* thread_func(void *t) {
    struct pa_mainloop *m;
    struct pa_io_event *io;

    assert(pipe_fds[0] >= 0 && !mainloop_api);

    failed = 0;
    
    m = pa_mainloop_new();
    assert(m);
    mainloop_api = pa_mainloop_get_api(m);
    assert(mainloop_api);

    io = mainloop_api->io_new(mainloop_api, pipe_fds[0], PA_IO_EVENT_INPUT, &request_func, NULL);
    assert(io);
    
    pa_mainloop_run(m, NULL);

    api->io_free(io);
    pa_mainloop_free(m);

    mainloop_api = NULL;
    
    return NULL;
}

static void start_thread(void) {
    int r;
    assert(!thread_running);

    r = pipe(pipe_fds);
    assert(r >= 0 && pipe_fds[0] >= 0 && pipe_fds[1] >= 0);

    current_request = NULL;

    r = pthread_create(&thread_id, NULL, thread_func, NULL);
    assert(!r);
}

static void stop_thread(void) {
    struct request req;
    assert(thread_running);

    pthread_join(thread_id, NULL);

    thread_running = 0;
    assert(!current_request);

    close(pipe_fds[0]);
    close(pipe_fds[1]);
    pipe_fds[0] = pipe_fds[1] = -1;
}
    

void request_execute(struct request *r) {
    char x = 'x';
    assert(r);
    
    r->success = r->done = 0;
    
    pthread_mutex_lock(&request_mutex);
    assert(!current_request);
    current_request = r;

    assert(pipe_fds[1] >= 0);
    write(pipe_fds[1], &x, sizeof(x));
    
    while (!r->done)
        pthread_cond_wait(&request_cond, &request_mutex);

    current_request = NULL;
    
    pthread_mutex_unlock(&request_mutex);
}