summaryrefslogtreecommitdiffstats
path: root/src/protocol-esound.c
blob: e97ab34d0e09efc6acd6edf59b82b8aa6b7d185d (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <limits.h>

#include "protocol-esound.h"
#include "esound-spec.h"
#include "memblock.h"
#include "client.h"
#include "sinkinput.h"
#include "sink.h"
#include "sourceoutput.h"
#include "source.h"
#include "sample.h"

#include "authkey.h"

#define COOKIE_FILE ".esd_auth"

#define PLAYBACK_BUFFER_SECONDS (.5)
#define PLAYBACK_BUFFER_FRAGMENTS (10)
#define RECORD_BUFFER_SECONDS (5)
#define RECORD_BUFFER_FRAGMENTS (100)

/* This is heavily based on esound's code */

struct connection {
    uint32_t index;
    struct pa_protocol_esound *protocol;
    struct pa_iochannel *io;
    struct pa_client *client;
    int authorized, swap_byte_order;
    void *write_data;
    size_t write_data_alloc, write_data_index, write_data_length;
    void *read_data;
    size_t read_data_alloc, read_data_length;
    esd_proto_t request;
    esd_client_state_t state;
    struct pa_sink_input *sink_input;
    struct pa_source_output *source_output;
    struct pa_memblockq *input_memblockq, *output_memblockq;
    void *fixed_source;
    struct {
        struct pa_memblock *current_memblock;
        size_t memblock_index, fragment_size;
    } playback;
};

struct pa_protocol_esound {
    int public;
    struct pa_core *core;
    struct pa_socket_server *server;
    struct pa_idxset *connections;
    uint32_t sink_index, source_index;
    unsigned n_player;
    uint8_t esd_key[ESD_KEY_LEN];
};

typedef struct proto_handler {
    size_t data_length;
    int (*proc)(struct connection *c, esd_proto_t request, const void *data, size_t length);
    const char *description;
} esd_proto_handler_info_t;

static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
static void sink_input_kill_cb(struct pa_sink_input *i);
static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);

static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
static void source_output_kill_cb(struct pa_source_output *o);

static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length);
static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length);

/* the big map of protocol handler info */
static struct proto_handler proto_map[ESD_PROTO_MAX] = {
    { ESD_KEY_LEN + sizeof(int),      esd_proto_connect, "connect" },
    { ESD_KEY_LEN + sizeof(int),      NULL, "lock" },
    { ESD_KEY_LEN + sizeof(int),      NULL, "unlock" },

    { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
    { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
    { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },

    { ESD_NAME_MAX + 3 * sizeof(int), NULL, "sample cache" },
    { sizeof(int),                    NULL, "sample free" },
    { sizeof(int),                    NULL, "sample play" },
    { sizeof(int),                    NULL, "sample loop" },
    { sizeof(int),                    NULL, "sample stop" },
    { -1,                             NULL, "TODO: sample kill" },

    { ESD_KEY_LEN + sizeof(int),      NULL, "standby" },
    { ESD_KEY_LEN + sizeof(int),      NULL, "resume" },

    { ESD_NAME_MAX,                   NULL, "sample getid" },
    { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },

    { sizeof(int),                    esd_proto_server_info, "server info" },
    { sizeof(int),                    esd_proto_all_info, "all info" },
    { -1,                             NULL, "TODO: subscribe" },
    { -1,                             NULL, "TODO: unsubscribe" },

    { 3 * sizeof(int),                esd_proto_stream_pan, "stream pan"},
    { 3 * sizeof(int),                NULL, "sample pan" },
     
    { sizeof(int),                    NULL, "standby mode" },
    { 0,                              esd_proto_get_latency, "get latency" }
};


static void connection_free(struct connection *c) {
    assert(c);
    pa_idxset_remove_by_data(c->protocol->connections, c, NULL);

    if (c->state == ESD_STREAMING_DATA)
        c->protocol->n_player--;
    
    pa_client_free(c->client);

    if (c->sink_input)
        pa_sink_input_free(c->sink_input);
    if (c->source_output)
        pa_source_output_free(c->source_output);
    if (c->input_memblockq)
        pa_memblockq_free(c->input_memblockq);
    if (c->output_memblockq)
        pa_memblockq_free(c->output_memblockq);

    if (c->playback.current_memblock)
        pa_memblock_unref(c->playback.current_memblock);
    
    free(c->read_data);
    free(c->write_data);
    
    pa_iochannel_free(c->io);
    
    if (c->fixed_source)
        c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
    
    free(c);
}

static struct pa_sink* get_output_sink(struct pa_protocol_esound *p) {
    struct pa_sink *s;
    assert(p);

    if (!(s = pa_idxset_get_by_index(p->core->sinks, p->sink_index)))
        s = pa_sink_get_default(p->core);

    p->sink_index = s ? s->index : PA_IDXSET_INVALID;
    return s;
}

static struct pa_source* get_input_source(struct pa_protocol_esound *p) {
    struct pa_source *s;
    assert(p);

    if (!(s = pa_idxset_get_by_index(p->core->sources, p->sink_index)))
        s = pa_source_get_default(p->core);

    p->source_index = s ? s->index : PA_IDXSET_INVALID;
    return s;
}

static void* connection_write(struct connection *c, size_t length) {
    size_t t, i;
    assert(c);

    assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
    c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);

    t = c->write_data_length+length;
    
    if (c->write_data_alloc < t)
        c->write_data = realloc(c->write_data, c->write_data_alloc = t);

    assert(c->write_data);

    i = c->write_data_length;
    c->write_data_length += length;
    
    return c->write_data+i;
}

/*** esound commands ***/

static int esd_proto_connect(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    uint32_t ekey;
    int *ok;
    assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));

    if (!c->authorized) {
        if (memcmp(data, c->protocol->esd_key, ESD_KEY_LEN) != 0) {
            fprintf(stderr, __FILE__": kicked client with invalid authorization key.\n");
            return -1;
        }

        c->authorized = 1;
    }
    
    ekey = *(uint32_t*)(data+ESD_KEY_LEN);
    if (ekey == ESD_ENDIAN_KEY)
        c->swap_byte_order = 0;
    else if (ekey == ESD_SWAP_ENDIAN_KEY)
        c->swap_byte_order = 1;
    else {
        fprintf(stderr, __FILE__": client sent invalid endian key\n");
        return -1;
    }

    ok = connection_write(c, sizeof(int));
    assert(ok);
    *ok = 1;
    return 0;
}

static int esd_proto_stream_play(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    char name[ESD_NAME_MAX];
    int format, rate;
    struct pa_sink *sink;
    struct pa_sample_spec ss;
    size_t l;
    assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
    
    format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
    rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));

    ss.rate = rate;
    ss.channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
    ss.format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;

    if (!pa_sample_spec_valid(&ss))
        return -1;

    if (!(sink = get_output_sink(c->protocol)))
        return -1;
    
    strncpy(name, data + sizeof(int)*2, sizeof(name));
    name[sizeof(name)-1] = 0;

    pa_client_rename(c->client, name);

    assert(!c->input_memblockq);

    l = (size_t) (pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS); 
    c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
    assert(c->input_memblockq);
    pa_iochannel_socket_set_rcvbuf(c->io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
    c->playback.fragment_size = l/10;
    
    assert(!c->sink_input);
    c->sink_input = pa_sink_input_new(sink, name, &ss);
    assert(c->sink_input);

    c->sink_input->peek = sink_input_peek_cb;
    c->sink_input->drop = sink_input_drop_cb;
    c->sink_input->kill = sink_input_kill_cb;
    c->sink_input->get_latency = sink_input_get_latency_cb;
    c->sink_input->userdata = c;

    c->state = ESD_STREAMING_DATA;

    c->protocol->n_player++;
    
    return 0;
}

static int esd_proto_stream_record(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    char name[ESD_NAME_MAX];
    int format, rate;
    struct pa_source *source;
    struct pa_sample_spec ss;
    size_t l;
    assert(c && length == (sizeof(int)*2+ESD_NAME_MAX));
    
    format = maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
    rate = maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));

    ss.rate = rate;
    ss.channels = ((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1;
    ss.format = ((format & ESD_MASK_BITS) == ESD_BITS16) ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;

    if (!pa_sample_spec_valid(&ss))
        return -1;

    if (request == ESD_PROTO_STREAM_MON) {
        struct pa_sink* sink;

        if (!(sink = get_output_sink(c->protocol)))
            return -1;

        if (!(source = sink->monitor_source))
            return -1;
    } else {
        assert(request == ESD_PROTO_STREAM_REC);
        
        if (!(source = get_input_source(c->protocol)))
            return -1;
    }
    
    strncpy(name, data + sizeof(int)*2, sizeof(name));
    name[sizeof(name)-1] = 0;

    pa_client_rename(c->client, name);

    assert(!c->output_memblockq);

    l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS); 
    c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&ss), 0, 0);
    assert(c->output_memblockq);
    pa_iochannel_socket_set_sndbuf(c->io, l/RECORD_BUFFER_FRAGMENTS*2);
    
    assert(!c->source_output);
    c->source_output = pa_source_output_new(source, name, &ss);
    assert(c->source_output);

    c->source_output->push = source_output_push_cb;
    c->source_output->kill = source_output_kill_cb;
    c->source_output->userdata = c;

    c->state = ESD_STREAMING_DATA;

    c->protocol->n_player++;
    
    return 0;
}

static int esd_proto_get_latency(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    struct pa_sink *sink;
    int latency, *lag;
    assert(c && !data && length == 0);

    if (!(sink = get_output_sink(c->protocol)))
        latency = 0;
    else {
        float usec = pa_sink_get_latency(sink);
        usec += PLAYBACK_BUFFER_SECONDS*1000000*.9;          /* A better estimation would be a good idea! */
        latency = (int) ((usec*44100)/1000000);
    }
    
    lag = connection_write(c, sizeof(int));
    assert(lag);
    *lag = c->swap_byte_order ? swap_endian_32(latency) : latency;
    return 0;
}

static int esd_proto_server_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    int rate = 44100, format = ESD_STEREO|ESD_BITS16;
    int *response;
    struct pa_sink *sink;
    assert(c && data && length == sizeof(int));

    if ((sink = get_output_sink(c->protocol))) {
        rate = sink->sample_spec.rate;
        format = (sink->sample_spec.format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
        format |= (sink->sample_spec.channels >= 2) ? ESD_STEREO : ESD_MONO;
    }
    
    response = connection_write(c, sizeof(int)*3);
    assert(response);
    *(response++) = 0;
    *(response++) = maybe_swap_endian_32(c->swap_byte_order, rate);
    *(response++) = maybe_swap_endian_32(c->swap_byte_order, format);
    return 0;
}

static int esd_proto_all_info(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    void *response;
    size_t t, k, s;
    struct connection *conn;
    size_t index = PA_IDXSET_INVALID;
    assert(c && data && length == sizeof(int));
    
    if (esd_proto_server_info(c, request, data, length) < 0)
        return -1;

    k = sizeof(int)*5+ESD_NAME_MAX;
    s = sizeof(int)*6+ESD_NAME_MAX;
    response = connection_write(c, (t = s+k*(c->protocol->n_player+1)));
    assert(k);

    for (conn = pa_idxset_first(c->protocol->connections, &index); conn; conn = pa_idxset_next(c->protocol->connections, &index)) {
        int format = ESD_BITS16 | ESD_STEREO, rate = 44100, volume = 0xFF;

        if (conn->state != ESD_STREAMING_DATA)
            continue;

        assert(t >= s+k+k);
        
        if (conn->sink_input) {
            rate = conn->sink_input->sample_spec.rate;
            volume = (conn->sink_input->volume*0xFF)/0x100;
            format = (conn->sink_input->sample_spec.format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
            format |= (conn->sink_input->sample_spec.channels >= 2) ? ESD_STEREO : ESD_MONO;
        }
        
        /* id */
        *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, (int) conn->index);
        response += sizeof(int);

        /* name */
        assert(conn->client);
        strncpy(response, conn->client->name, ESD_NAME_MAX);
        response += ESD_NAME_MAX;

        /* rate */
        *((int*) response) = maybe_swap_endian_32(c->swap_byte_order,  rate);
        response += sizeof(int);

        /* left */
        *((int*) response) = maybe_swap_endian_32(c->swap_byte_order,  volume);
        response += sizeof(int);

        /*right*/
        *((int*) response) = maybe_swap_endian_32(c->swap_byte_order,  volume);
        response += sizeof(int);

        /*format*/
        *((int*) response) = maybe_swap_endian_32(c->swap_byte_order, format);
        response += sizeof(int);

        t-= k;
    }

    assert(t == s+k);
    memset(response, 0, t);
    return 0;
}

static int esd_proto_stream_pan(struct connection *c, esd_proto_t request, const void *data, size_t length) {
    int *ok;
    uint32_t index, volume;
    struct connection *conn;
    assert(c && data && length == sizeof(int)*3);
    
    index = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *(int*)data);
    volume = (uint32_t) maybe_swap_endian_32(c->swap_byte_order, *((int*)data + 1));
    volume = (volume*0x100)/0xFF;

    ok = connection_write(c, sizeof(int));
    assert(ok);

    if ((conn = pa_idxset_get_by_index(c->protocol->connections, index))) {
        assert(conn->sink_input);
        conn->sink_input->volume = volume;
        *ok = 1;
    } else
        *ok = 0;
    
    return 0;
}

/*** client callbacks ***/

static void client_kill_cb(struct pa_client *c) {
    assert(c && c->userdata);
    connection_free(c->userdata);
}

/*** pa_iochannel callbacks ***/

static int do_read(struct connection *c) {
    assert(c && c->io);

    if (c->state == ESD_NEXT_REQUEST) {
        ssize_t r;
        assert(c->read_data_length < sizeof(c->request));

        if ((r = pa_iochannel_read(c->io, ((void*) &c->request) + c->read_data_length, sizeof(c->request) - c->read_data_length)) <= 0) {
            fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
            return -1;
        }

        if ((c->read_data_length+= r) >= sizeof(c->request)) {
            struct proto_handler *handler;
            
            if (c->swap_byte_order)
                c->request = swap_endian_32(c->request);

            if (c->request < ESD_PROTO_CONNECT || c->request > ESD_PROTO_MAX) {
                fprintf(stderr, "protocol-esound.c: recieved invalid request.\n");
                return -1;
            }

            handler = proto_map+c->request;

            if (!handler->proc) {
                fprintf(stderr, "protocol-sound.c: recieved unimplemented request.\n");
                return -1;
            }
            
            if (handler->data_length == 0) {
                c->read_data_length = 0;

                if (handler->proc(c, c->request, NULL, 0) < 0)
                    return -1;
                
            } else {
                if (c->read_data_alloc < handler->data_length)
                    c->read_data = realloc(c->read_data, c->read_data_alloc = handler->data_length);
                assert(c->read_data);
                
                c->state = ESD_NEEDS_REQDATA;
                c->read_data_length = 0;
            }
        }

    } else if (c->state == ESD_NEEDS_REQDATA) {
        ssize_t r;
        struct proto_handler *handler = proto_map+c->request;

        assert(handler->proc);
        
        assert(c->read_data && c->read_data_length < handler->data_length);

        if ((r = pa_iochannel_read(c->io, c->read_data + c->read_data_length, handler->data_length - c->read_data_length)) <= 0) {
            fprintf(stderr, "protocol-esound.c: read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
            return -1;
        }

        if ((c->read_data_length+= r) >= handler->data_length) {
            size_t l = c->read_data_length;
            assert(handler->proc);

            c->state = ESD_NEXT_REQUEST;
            c->read_data_length = 0;
            
            if (handler->proc(c, c->request, c->read_data, l) < 0)
                return -1;
        }
    } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
        struct pa_memchunk chunk;
        ssize_t r;
        size_t l;

        assert(c->input_memblockq);

        if (!(l = pa_memblockq_missing(c->input_memblockq)))
            return 0;

        if (l > c->playback.fragment_size)
            l = c->playback.fragment_size;

        if (c->playback.current_memblock) 
            if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
                pa_memblock_unref(c->playback.current_memblock);
                c->playback.current_memblock = NULL;
                c->playback.memblock_index = 0;
            }

        
        if (!c->playback.current_memblock) {
            c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
            assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
            c->playback.memblock_index = 0;
        }

        if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
            fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
            return -1;
        }
        
        chunk.memblock = c->playback.current_memblock;
        chunk.index = c->playback.memblock_index;
        chunk.length = r;
        assert(chunk.memblock);

        c->playback.memblock_index += r;
        
        assert(c->input_memblockq);
        pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
        assert(c->sink_input);
        pa_sink_notify(c->sink_input->sink);

    }
    
    return 0;
}

static int do_write(struct connection *c) {
    assert(c && c->io);

    if (c->write_data_length) {
        ssize_t r;
        
        assert(c->write_data_index < c->write_data_length);
        if ((r = pa_iochannel_write(c->io, c->write_data+c->write_data_index, c->write_data_length-c->write_data_index)) < 0) {
            fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
            return -1;
        }
        
        if ((c->write_data_index +=r) >= c->write_data_length)
            c->write_data_length = c->write_data_index = 0;
        
    } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
        struct pa_memchunk chunk;
        ssize_t r;

        assert(c->output_memblockq);
        if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
            return 0;
        
        assert(chunk.memblock && chunk.length);
        
        if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
            pa_memblock_unref(chunk.memblock);
            fprintf(stderr, __FILE__": write(): %s\n", strerror(errno));
            return -1;
        }
    
        pa_memblockq_drop(c->output_memblockq, r);
        pa_memblock_unref(chunk.memblock);
    }
    
    return 0;
}

static void do_work(struct connection *c) {
    assert(c);

    assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
    c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);

    if (pa_iochannel_is_hungup(c->io))
        goto fail;

    if (pa_iochannel_is_writable(c->io))
        if (do_write(c) < 0)
            goto fail;

    if (pa_iochannel_is_readable(c->io))
        if (do_read(c) < 0)
            goto fail;

    return;

fail:
    connection_free(c);
}

static void io_callback(struct pa_iochannel*io, void *userdata) {
    struct connection *c = userdata;
    assert(io && c && c->io == io);

    do_work(c);
}

/*** fixed callback ***/

void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
    struct connection *c = userdata;
    assert(a && c && c->fixed_source == id);

    do_work(c);
}

/*** sink_input callbacks ***/

static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
    struct connection*c;
    assert(i && i->userdata && chunk);
    c = i->userdata;
    
    if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
        return -1;

    return 0;
}

static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
    struct connection*c = i->userdata;
    assert(i && c && length);

    pa_memblockq_drop(c->input_memblockq, length);

    /* do something */
    assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
    c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
}

static void sink_input_kill_cb(struct pa_sink_input *i) {
    assert(i && i->userdata);
    connection_free((struct connection *) i->userdata);
}


static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
    struct connection*c = i->userdata;
    assert(i && c);
    return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
}

/*** source_output callbacks ***/

static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
    struct connection *c = o->userdata;
    assert(o && c && chunk);

    pa_memblockq_push(c->output_memblockq, chunk, 0);

    /* do something */
    assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
    c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
}

static void source_output_kill_cb(struct pa_source_output *o) {
    assert(o && o->userdata);
    connection_free((struct connection *) o->userdata);
}

/*** socket server callback ***/

static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
    struct connection *c;
    char cname[256];
    assert(s && io && userdata);

    c = malloc(sizeof(struct connection));
    assert(c);
    c->protocol = userdata;
    c->io = io;
    pa_iochannel_set_callback(c->io, io_callback, c);

    pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
    assert(c->protocol->core);
    c->client = pa_client_new(c->protocol->core, "ESOUND", cname);
    assert(c->client);
    c->client->kill = client_kill_cb;
    c->client->userdata = c;
    
    c->authorized = c->protocol->public;
    c->swap_byte_order = 0;

    c->read_data_length = 0;
    c->read_data = malloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
    assert(c->read_data);

    c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
    c->write_data = NULL;

    c->state = ESD_NEEDS_REQDATA;
    c->request = ESD_PROTO_CONNECT;

    c->sink_input = NULL;
    c->input_memblockq = NULL;

    c->source_output = NULL;
    c->output_memblockq = NULL;

    c->playback.current_memblock = NULL;
    c->playback.memblock_index = 0;
    c->playback.fragment_size = 0;

    c->fixed_source = c->protocol->core->mainloop->source_fixed(c->protocol->core->mainloop, fixed_callback, c);
    assert(c->fixed_source);
    c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
    
    pa_idxset_put(c->protocol->connections, c, &c->index);
}

/*** entry points ***/

struct pa_protocol_esound* pa_protocol_esound_new(struct pa_core*core, struct pa_socket_server *server) {
    struct pa_protocol_esound *p;
    assert(core && server);

    p = malloc(sizeof(struct pa_protocol_esound));
    assert(p);

    if (pa_authkey_load_from_home(COOKIE_FILE, p->esd_key, sizeof(p->esd_key)) < 0) {
        free(p);
        return NULL;
    }
    
    p->public = 0;
    p->server = server;
    pa_socket_server_set_callback(p->server, on_connection, p);
    p->core = core;
    p->connections = pa_idxset_new(NULL, NULL);
    assert(p->connections);
    p->sink_index = p->source_index = PA_IDXSET_INVALID;
    p->n_player = 0;

    return p;
}

void pa_protocol_esound_free(struct pa_protocol_esound *p) {
    struct connection *c;
    assert(p);

    while ((c = pa_idxset_first(p->connections, NULL)))
        connection_free(c);

    pa_idxset_free(p->connections, NULL, NULL);
    pa_socket_server_free(p->server);
    free(p);
}