summaryrefslogtreecommitdiffstats
path: root/src/polyp.c
blob: c2d1d822950513684779577e51e5e700d2f6b689 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "polyp.h"
#include "protocol-native-spec.h"
#include "pdispatch.h"
#include "pstream.h"
#include "dynarray.h"
#include "socket-client.h"
#include "pstream-util.h"

#define DEFAULT_QUEUE_LENGTH 10240
#define DEFAULT_MAX_LENGTH 20480
#define DEFAULT_PREBUF 4096
#define DEFAULT_TIMEOUT (5*60)
#define DEFAULT_SERVER "/tmp/polypaudio_native"

struct pa_context {
    char *name;
    struct pa_mainloop_api* mainloop;
    struct socket_client *client;
    struct pstream *pstream;
    struct pdispatch *pdispatch;
    struct dynarray *streams;
    struct pa_stream *first_stream;
    uint32_t ctag;
    uint32_t errno;
    enum { CONTEXT_UNCONNECTED, CONTEXT_CONNECTING, CONTEXT_READY, CONTEXT_DEAD} state;

    void (*connect_complete_callback)(struct pa_context*c, int success, void *userdata);
    void *connect_complete_userdata;

    void (*die_callback)(struct pa_context*c, void *userdata);
    void *die_userdata;
};

struct pa_stream {
    struct pa_context *context;
    struct pa_stream *next, *previous;
    uint32_t device_index;
    uint32_t channel;
    int channel_valid;
    enum pa_stream_direction direction;
    enum { STREAM_CREATING, STREAM_READY, STREAM_DEAD} state;
    uint32_t requested_bytes;

    void (*read_callback)(struct pa_stream *p, const void*data, size_t length, void *userdata);
    void *read_userdata;

    void (*write_callback)(struct pa_stream *p, size_t length, void *userdata);
    void *write_userdata;
    
    void (*create_complete_callback)(struct pa_context*c, struct pa_stream *s, void *userdata);
    void *create_complete_userdata;
    
    void (*die_callback)(struct pa_stream*c, void *userdata);
    void *die_userdata;
};

static int command_request(struct pdispatch *pd, uint32_t command, uint32_t tag, struct tagstruct *t, void *userdata);

static const struct pdispatch_command command_table[PA_COMMAND_MAX] = {
    [PA_COMMAND_ERROR] = { NULL },
    [PA_COMMAND_REPLY] = { NULL },
    [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { NULL },
    [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { NULL },
    [PA_COMMAND_CREATE_RECORD_STREAM] = { NULL },
    [PA_COMMAND_DELETE_RECORD_STREAM] = { NULL },
    [PA_COMMAND_EXIT] = { NULL },
    [PA_COMMAND_REQUEST] = { command_request },
};

struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) {
    assert(mainloop && name);
    struct pa_context *c;
    c = malloc(sizeof(struct pa_context));
    assert(c);
    c->name = strdup(name);
    c->mainloop = mainloop;
    c->client = NULL;
    c->pstream = NULL;
    c->pdispatch = NULL;
    c->streams = dynarray_new();
    assert(c->streams);
    c->first_stream = NULL;
    c->errno = PA_ERROR_OK;
    c->state = CONTEXT_UNCONNECTED;
    c->ctag = 0;

    c->connect_complete_callback = NULL;
    c->connect_complete_userdata = NULL;

    c->die_callback = NULL;
    c->die_userdata = NULL;
    
    return c;
}

void pa_context_free(struct pa_context *c) {
    assert(c);

    while (c->first_stream)
        pa_stream_free(c->first_stream);
    
    if (c->client)
        socket_client_free(c->client);
    if (c->pdispatch)
        pdispatch_free(c->pdispatch);
    if (c->pstream)
        pstream_free(c->pstream);
    if (c->streams)
        dynarray_free(c->streams, NULL, NULL);
        
    free(c->name);
    free(c);
}

static void stream_dead(struct pa_stream *s) {
    if (s->state == STREAM_DEAD)
        return;

    s->state = STREAM_DEAD;
    if (s->die_callback)
        s->die_callback(s, s->die_userdata);
}

static void context_dead(struct pa_context *c) {
    struct pa_stream *s;
    assert(c);
    
    for (s = c->first_stream; s; s = s->next)
        stream_dead(s);

    if (c->state == CONTEXT_DEAD)
        return;
    
    c->state = CONTEXT_DEAD;
    if (c->die_callback)
        c->die_callback(c, c->die_userdata);
}

static void pstream_die_callback(struct pstream *p, void *userdata) {
    struct pa_context *c = userdata;
    assert(p && c);

    assert(c->state != CONTEXT_DEAD);
    
    c->state = CONTEXT_DEAD;

    context_dead(c);
}

static int pstream_packet_callback(struct pstream *p, struct packet *packet, void *userdata) {
    struct pa_context *c = userdata;
    assert(p && packet && c);

    if (pdispatch_run(c->pdispatch, packet, c) < 0) {
        fprintf(stderr, "polyp.c: invalid packet.\n");
        return -1;
    }

    return 0;
}

static int pstream_memblock_callback(struct pstream *p, uint32_t channel, int32_t delta, struct memchunk *chunk, void *userdata) {
    struct pa_context *c = userdata;
    struct pa_stream *s;
    assert(p && chunk && c && chunk->memblock && chunk->memblock->data);

    if (!(s = dynarray_get(c->streams, channel)))
        return -1;

    if (s->read_callback)
        s->read_callback(s, chunk->memblock->data + chunk->index, chunk->length, s->read_userdata);
    
    return 0;
}

static void on_connection(struct socket_client *client, struct iochannel*io, void *userdata) {
    struct pa_context *c = userdata;
    assert(client && io && c && c->state == CONTEXT_CONNECTING);

    socket_client_free(client);
    c->client = NULL;

    if (!io) {
        c->errno = PA_ERROR_CONNECTIONREFUSED;
        c->state = CONTEXT_UNCONNECTED;

        if (c->connect_complete_callback)
            c->connect_complete_callback(c, 0, c->connect_complete_userdata);

        return;
    }
    
    c->pstream = pstream_new(c->mainloop, io);
    assert(c->pstream);
    pstream_set_die_callback(c->pstream, pstream_die_callback, c);
    pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
    pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
    
    c->pdispatch = pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
    assert(c->pdispatch);

    c->state = CONTEXT_READY;

    if (c->connect_complete_callback)
        c->connect_complete_callback(c, 1, c->connect_complete_userdata);
}

int pa_context_connect(struct pa_context *c, const char *server, void (*complete) (struct pa_context*c, int success, void *userdata), void *userdata) {
    assert(c && c->state == CONTEXT_UNCONNECTED);

    assert(!c->client);
    if (!(c->client = socket_client_new_unix(c->mainloop, server ? server : DEFAULT_SERVER))) {
        c->errno = PA_ERROR_CONNECTIONREFUSED;
        return -1;
    }

    c->connect_complete_callback = complete;
    c->connect_complete_userdata = userdata;
    
    socket_client_set_callback(c->client, on_connection, c);
    c->state = CONTEXT_CONNECTING;

    return 0;
}

int pa_context_is_dead(struct pa_context *c) {
    assert(c);
    return c->state == CONTEXT_DEAD;
}

int pa_context_is_ready(struct pa_context *c) {
    assert(c);
    return c->state == CONTEXT_READY;
}

int pa_context_errno(struct pa_context *c) {
    assert(c);
    return c->errno;
}

void pa_context_set_die_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata) {
    assert(c);
    c->die_callback = cb;
    c->die_userdata = userdata;
}

static int command_request(struct pdispatch *pd, uint32_t command, uint32_t tag, struct tagstruct *t, void *userdata) {
    struct pa_stream *s;
    struct pa_context *c = userdata;
    uint32_t bytes, channel;
    assert(pd && command == PA_COMMAND_REQUEST && t && c);

    if (tagstruct_getu32(t, &channel) < 0 ||
        tagstruct_getu32(t, &bytes) < 0 ||
        !tagstruct_eof(t)) {
        c->errno = PA_ERROR_PROTOCOL;
        return -1;
    }
    
    if (!(s = dynarray_get(c->streams, channel))) {
        c->errno = PA_ERROR_PROTOCOL;
        return -1;
    }

    /*fprintf(stderr, "Requested %u bytes\n", bytes);*/
    
    s->requested_bytes += bytes;
    
    if (s->requested_bytes && s->write_callback)
        s->write_callback(s, s->requested_bytes, s->write_userdata);

    return 0;
}

static int create_playback_callback(struct pdispatch *pd, uint32_t command, uint32_t tag, struct tagstruct *t, void *userdata) {
    int ret = 0;
    struct pa_stream *s = userdata;
    assert(pd && s && s->state == STREAM_CREATING);

    if (command != PA_COMMAND_REPLY) {
        struct pa_context *c = s->context;
        assert(c);

        if (command == PA_COMMAND_ERROR && tagstruct_getu32(t, &s->context->errno) < 0) {
            s->context->errno = PA_ERROR_PROTOCOL;
            ret = -1;
        } else if (command == PA_COMMAND_TIMEOUT) {
            s->context->errno = PA_ERROR_TIMEOUT;
            ret = -1;
        }

        goto fail;
    }

    if (tagstruct_getu32(t, &s->channel) < 0 ||
        tagstruct_getu32(t, &s->device_index) < 0 ||
        !tagstruct_eof(t)) {
        s->context->errno = PA_ERROR_PROTOCOL;
        ret = -1;
        goto fail;
    }

    s->channel_valid = 1;
    dynarray_put(s->context->streams, s->channel, s);
    
    s->state = STREAM_READY;
    assert(s->create_complete_callback);
    s->create_complete_callback(s->context, s, s->create_complete_userdata);
    return 0;

fail:
    assert(s->create_complete_callback);
    s->create_complete_callback(s->context, NULL, s->create_complete_userdata);
    pa_stream_free(s);
    return ret;
}

int pa_stream_new(
    struct pa_context *c,
    enum pa_stream_direction dir,
    const char *dev,
    const char *name,
    const struct pa_sample_spec *ss,
    const struct pa_buffer_attr *attr,
    void (*complete) (struct pa_context*c, struct pa_stream *s, void *userdata),
    void *userdata) {
    
    struct pa_stream *s;
    struct tagstruct *t;
    uint32_t tag;

    assert(c && name && ss && c->state == CONTEXT_READY && complete);
    
    s = malloc(sizeof(struct pa_stream));
    assert(s);
    s->context = c;

    s->read_callback = NULL;
    s->read_userdata = NULL;
    s->write_callback = NULL;
    s->write_userdata = NULL;
    s->die_callback = NULL;
    s->die_userdata = NULL;
    s->create_complete_callback = complete;
    s->create_complete_userdata = NULL;

    s->state = STREAM_CREATING;
    s->requested_bytes = 0;
    s->channel = 0;
    s->channel_valid = 0;
    s->device_index = (uint32_t) -1;
    s->direction = dir;

    t = tagstruct_new(NULL, 0);
    assert(t);
    
    tagstruct_putu32(t, dir == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM);
    tagstruct_putu32(t, tag = c->ctag++);
    tagstruct_puts(t, name);
    tagstruct_put_sample_spec(t, ss);
    tagstruct_putu32(t, (uint32_t) -1);
    tagstruct_putu32(t, attr ? attr->queue_length : DEFAULT_QUEUE_LENGTH);
    tagstruct_putu32(t, attr ? attr->max_length  : DEFAULT_MAX_LENGTH);
    tagstruct_putu32(t, attr ? attr->prebuf : DEFAULT_PREBUF);

    pstream_send_tagstruct(c->pstream, t);

    pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, create_playback_callback, s);
    
    s->next = c->first_stream;
    if (s->next)
        s->next->previous = s;
    s->previous = NULL;
    c->first_stream = s;

    return 0;
}

void pa_stream_free(struct pa_stream *s) {
    assert(s && s->context);
    
    if (s->channel_valid) {
        struct tagstruct *t = tagstruct_new(NULL, 0);
        assert(t);
    
        tagstruct_putu32(t, PA_COMMAND_DELETE_PLAYBACK_STREAM);
        tagstruct_putu32(t, s->context->ctag++);
        tagstruct_putu32(t, s->channel);
        pstream_send_tagstruct(s->context->pstream, t);
    }
    
    if (s->channel_valid)
        dynarray_put(s->context->streams, s->channel, NULL);

    if (s->next)
        s->next->previous = s->previous;
    if (s->previous)
        s->previous->next = s->next;
    else
        s->context->first_stream = s->next;
    
    free(s);
}

void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) {
    assert(s && cb);
    s->write_callback = cb;
    s->write_userdata = userdata;
}

void pa_stream_write(struct pa_stream *s, const void *data, size_t length) {
    struct memchunk chunk;
    assert(s && s->context && data && length);

    chunk.memblock = memblock_new(length);
    assert(chunk.memblock && chunk.memblock->data);
    memcpy(chunk.memblock->data, data, length);
    chunk.index = 0;
    chunk.length = length;

    pstream_send_memblock(s->context->pstream, s->channel, 0, &chunk);
    memblock_unref(chunk.memblock);

    /*fprintf(stderr, "Sent %u bytes\n", length);*/
    
    if (length < s->requested_bytes)
        s->requested_bytes -= length;
    else
        s->requested_bytes = 0;
}

size_t pa_stream_writable_size(struct pa_stream *s) {
    assert(s);
    return s->requested_bytes;
}

void pa_stream_set_read_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, const void*data, size_t length, void *userdata), void *userdata) {
    assert(s && cb);
    s->read_callback = cb;
    s->read_userdata = userdata;
}

int pa_stream_is_dead(struct pa_stream *s) {
    return s->state == STREAM_DEAD;
}

int pa_stream_is_ready(struct pa_stream*s) {
    return s->state == STREAM_READY;
}

void pa_stream_set_die_callback(struct pa_stream *s, void (*cb)(struct pa_stream *s, void *userdata), void *userdata) {
    assert(s);
    s->die_callback = cb;
    s->die_userdata = userdata;
}