summaryrefslogtreecommitdiffstats
path: root/polyp/polyplib.c
blob: ea5003b4008bdf876798f978c1a7f0b0e629a2a7 (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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include "polyplib.h"
#include "native-common.h"
#include "pdispatch.h"
#include "pstream.h"
#include "dynarray.h"
#include "socket-client.h"
#include "pstream-util.h"
#include "authkey.h"
#include "util.h"

#define DEFAULT_MAXLENGTH 204800
#define DEFAULT_TLENGTH 10240
#define DEFAULT_PREBUF 4096
#define DEFAULT_MINREQ 1024
#define DEFAULT_FRAGSIZE 1024

#define DEFAULT_TIMEOUT (5*60)
#define DEFAULT_SERVER "/tmp/polypaudio/native"
#define DEFAULT_PORT "4713"

struct pa_context {
    char *name;
    struct pa_mainloop_api* mainloop;
    struct pa_socket_client *client;
    struct pa_pstream *pstream;
    struct pa_pdispatch *pdispatch;
    struct pa_dynarray *record_streams, *playback_streams;
    struct pa_stream *first_stream;
    uint32_t ctag;
    uint32_t error;
    enum {
        CONTEXT_UNCONNECTED,
        CONTEXT_CONNECTING,
        CONTEXT_AUTHORIZING,
        CONTEXT_SETTING_NAME,
        CONTEXT_READY,
        CONTEXT_DEAD
    } state;

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

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

    void (*stat_callback)(struct pa_context*c, uint32_t count, uint32_t total, void *userdata);
    void *stat_userdata;
    
    uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
};

struct pa_stream {
    struct pa_context *context;
    struct pa_stream *next, *previous;

    char *name;
    struct pa_buffer_attr buffer_attr;
    struct pa_sample_spec sample_spec;
    uint32_t device_index;
    uint32_t channel;
    int channel_valid;
    enum pa_stream_direction direction;
    
    enum { STREAM_LOOKING_UP, 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_stream *s, int success, void *userdata);
    void *create_complete_userdata;

    void (*drain_complete_callback)(struct pa_stream *s, void *userdata);
    void *drain_complete_userdata;
    
    void (*die_callback)(struct pa_stream*c, void *userdata);
    void *die_userdata;

    void (*get_latency_callback)(struct pa_stream*c, uint32_t latency, void *userdata);
    void *get_latency_userdata;
};

static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
static void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);

static const struct pa_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 },
    [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_playback_stream_killed },
    [PA_COMMAND_RECORD_STREAM_KILLED] = { command_playback_stream_killed },
};

struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) {
    struct pa_context *c;
    assert(mainloop && name);
    
    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->playback_streams = pa_dynarray_new();
    assert(c->playback_streams);
    c->record_streams = pa_dynarray_new();
    assert(c->record_streams);
    c->first_stream = NULL;
    c->error = PA_ERROR_OK;
    c->state = CONTEXT_UNCONNECTED;
    c->ctag = 0;

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

    c->drain_complete_callback = NULL;
    c->drain_complete_userdata = NULL;

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

    c->stat_callback = NULL;
    c->stat_userdata = NULL;

    pa_check_for_sigpipe();
    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)
        pa_socket_client_free(c->client);
    if (c->pdispatch)
        pa_pdispatch_free(c->pdispatch);
    if (c->pstream)
        pa_pstream_free(c->pstream);
    if (c->record_streams)
        pa_dynarray_free(c->record_streams, NULL, NULL);
    if (c->playback_streams)
        pa_dynarray_free(c->playback_streams, NULL, NULL);
        
    free(c->name);
    free(c);
}

static void stream_dead(struct pa_stream *s) {
    assert(s);
    
    if (s->state == STREAM_DEAD)
        return;
    
    if (s->state == STREAM_READY) {
        s->state = STREAM_DEAD;
        if (s->die_callback)
            s->die_callback(s, s->die_userdata);
    } else
        s->state = STREAM_DEAD;
}

static void context_dead(struct pa_context *c) {
    struct pa_stream *s;
    assert(c);
    
    if (c->state == CONTEXT_DEAD)
        return;

    if (c->pdispatch)
        pa_pdispatch_free(c->pdispatch);
    c->pdispatch = NULL;
    
    if (c->pstream)
        pa_pstream_free(c->pstream);
    c->pstream = NULL;
    
    if (c->client)
        pa_socket_client_free(c->client);
    c->client = NULL;
    
    for (s = c->first_stream; s; s = s->next)
        stream_dead(s);

    if (c->state == CONTEXT_READY) {
        c->state = CONTEXT_DEAD;
        if (c->die_callback)
            c->die_callback(c, c->die_userdata);
    } else
        c->state = CONTEXT_DEAD;
}

static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
    struct pa_context *c = userdata;
    assert(p && c);
    c->error = PA_ERROR_CONNECTIONTERMINATED;
    context_dead(c);
}

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

    if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
        fprintf(stderr, "polyp.c: invalid packet.\n");
        c->error = PA_ERROR_PROTOCOL;
        context_dead(c);
    }
}

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

    if (!(s = pa_dynarray_get(c->record_streams, channel)))
        return;

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

static int handle_error(struct pa_context *c, uint32_t command, struct pa_tagstruct *t) {
    assert(c && t);
    
    if (command == PA_COMMAND_ERROR) {
        if (pa_tagstruct_getu32(t, &c->error) < 0) {
            c->error = PA_ERROR_PROTOCOL;
            return -1;
        }

        return 0;
    }

    c->error = (command == PA_COMMAND_TIMEOUT) ? PA_ERROR_TIMEOUT : PA_ERROR_INTERNAL;
    return -1;
}

static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_context *c = userdata;
    assert(pd && c && (c->state == CONTEXT_AUTHORIZING || c->state == CONTEXT_SETTING_NAME));

    if (command != PA_COMMAND_REPLY) {
        handle_error(c, command, t);
        context_dead(c);

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

    if (c->state == CONTEXT_AUTHORIZING) {
        struct pa_tagstruct *t;
        c->state = CONTEXT_SETTING_NAME;
        t = pa_tagstruct_new(NULL, 0);
        assert(t);
        pa_tagstruct_putu32(t, PA_COMMAND_SET_NAME);
        pa_tagstruct_putu32(t, tag = c->ctag++);
        pa_tagstruct_puts(t, c->name);
        pa_pstream_send_tagstruct(c->pstream, t);
        pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c);
    } else {
        assert(c->state == CONTEXT_SETTING_NAME);
        
        c->state = CONTEXT_READY;

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

    return;
}

static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) {
    struct pa_context *c = userdata;
    struct pa_tagstruct *t;
    uint32_t tag;
    assert(client && c && c->state == CONTEXT_CONNECTING);

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

    if (!io) {
        c->error = PA_ERROR_CONNECTIONREFUSED;
        context_dead(c);

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

        return;
    }
    
    c->pstream = pa_pstream_new(c->mainloop, io);
    assert(c->pstream);
    pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
    pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
    pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
    
    c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
    assert(c->pdispatch);

    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_tagstruct_put_arbitrary(t, c->auth_cookie, sizeof(c->auth_cookie));
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c);
    c->state = CONTEXT_AUTHORIZING;
}

static struct sockaddr *resolve_server(const char *server, size_t *len) {
    struct sockaddr *sa;
    struct addrinfo hints, *result = NULL;
    char *port;
    assert(server && len);

    if ((port = strrchr(server, ':')))
        port++;
    if (!port)
        port = DEFAULT_PORT;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    if (getaddrinfo(server, port, &hints, &result) != 0)
        return NULL;
    assert(result);
    
    sa = malloc(*len = result->ai_addrlen);
    assert(sa);
    memcpy(sa, result->ai_addr, *len);

    freeaddrinfo(result);
    
    return sa;
    
}

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);

    if (pa_authkey_load_from_home(PA_NATIVE_COOKIE_FILE, c->auth_cookie, sizeof(c->auth_cookie)) < 0) {
        c->error = PA_ERROR_AUTHKEY;
        return -1;
    }

    if (!server)
        if (!(server = getenv("POLYP_SERVER")))
            server = DEFAULT_SERVER;

    assert(!c->client);
    
    if (*server == '/') {
        if (!(c->client = pa_socket_client_new_unix(c->mainloop, server))) {
            c->error = PA_ERROR_CONNECTIONREFUSED;
            return -1;
        }
    } else {
        struct sockaddr* sa;
        size_t sa_len;

        if (!(sa = resolve_server(server, &sa_len))) {
            c->error = PA_ERROR_INVALIDSERVER;
            return -1;
        }

        c->client = pa_socket_client_new_sockaddr(c->mainloop, sa, sa_len);
        free(sa);

        if (!c->client) {
            c->error = PA_ERROR_CONNECTIONREFUSED;
            return -1;
        }
    }

    c->connect_complete_callback = complete;
    c->connect_complete_userdata = userdata;
    
    pa_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->error;
}

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 void command_playback_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_context *c = userdata;
    struct pa_stream *s;
    uint32_t channel;
    assert(pd && (command == PA_COMMAND_PLAYBACK_STREAM_KILLED || command == PA_COMMAND_RECORD_STREAM_KILLED) && t && c);

    if (pa_tagstruct_getu32(t, &channel) < 0 ||
        !pa_tagstruct_eof(t)) {
        c->error = PA_ERROR_PROTOCOL;
        context_dead(c);
        return;
    }
    
    if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel)))
        return;

    c->error = PA_ERROR_KILLED;
    stream_dead(s);
}

static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_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 (pa_tagstruct_getu32(t, &channel) < 0 ||
        pa_tagstruct_getu32(t, &bytes) < 0 ||
        !pa_tagstruct_eof(t)) {
        c->error = PA_ERROR_PROTOCOL;
        context_dead(c);
        return;
    }
    
    if (!(s = pa_dynarray_get(c->playback_streams, channel)))
        return;

    if (s->state != STREAM_READY)
        return;
    
    s->requested_bytes += bytes;
    
    if (s->requested_bytes && s->write_callback)
        s->write_callback(s, s->requested_bytes, s->write_userdata);
}

static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_stream *s = userdata;
    assert(pd && s && s->state == STREAM_CREATING);

    if (command != PA_COMMAND_REPLY) {
        if (handle_error(s->context, command, t) < 0) {
            context_dead(s->context);
            return;
        }

        stream_dead(s);
        if (s->create_complete_callback)
            s->create_complete_callback(s, 0, s->create_complete_userdata);

        return;
    }

    if (pa_tagstruct_getu32(t, &s->channel) < 0 ||
        pa_tagstruct_getu32(t, &s->device_index) < 0 ||
        !pa_tagstruct_eof(t)) {
        s->context->error = PA_ERROR_PROTOCOL;
        context_dead(s->context);
        return;
    }

    s->channel_valid = 1;
    pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams :  s->context->record_streams, s->channel, s);
    
    s->state = STREAM_READY;
    if (s->create_complete_callback)
        s->create_complete_callback(s, 1, s->create_complete_userdata);
}

static void create_stream(struct pa_stream *s, uint32_t tdev_index) {
    struct pa_tagstruct *t;
    uint32_t tag;
    assert(s);

    s->state = STREAM_CREATING;
    
    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    
    pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_CREATE_PLAYBACK_STREAM : PA_COMMAND_CREATE_RECORD_STREAM);
    pa_tagstruct_putu32(t, tag = s->context->ctag++);
    pa_tagstruct_puts(t, s->name);
    pa_tagstruct_put_sample_spec(t, &s->sample_spec);
    pa_tagstruct_putu32(t, tdev_index);
    pa_tagstruct_putu32(t, s->buffer_attr.maxlength);
    if (s->direction == PA_STREAM_PLAYBACK) {
        pa_tagstruct_putu32(t, s->buffer_attr.tlength);
        pa_tagstruct_putu32(t, s->buffer_attr.prebuf);
        pa_tagstruct_putu32(t, s->buffer_attr.minreq);
    } else
        pa_tagstruct_putu32(t, s->buffer_attr.fragsize);

    pa_pstream_send_tagstruct(s->context->pstream, t);
    pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, s);
}

static void lookup_device_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_stream *s = userdata;
    uint32_t tdev;
    assert(pd && s && s->state == STREAM_LOOKING_UP);

    if (command != PA_COMMAND_REPLY) {
        if (handle_error(s->context, command, t) < 0) {
            context_dead(s->context);
            return;
        }

        stream_dead(s);
        if (s->create_complete_callback)
            s->create_complete_callback(s, 0, s->create_complete_userdata);
        return;
    }

    if (pa_tagstruct_getu32(t, &tdev) < 0 ||
        !pa_tagstruct_eof(t)) {
        s->context->error = PA_ERROR_PROTOCOL;
        context_dead(s->context);
        return;
    }
    
    create_stream(s, tdev);
}

static void lookup_device(struct pa_stream *s, const char *tdev) {
    struct pa_tagstruct *t;
    uint32_t tag;
    assert(s);
    
    s->state = STREAM_LOOKING_UP;

    t = pa_tagstruct_new(NULL, 0);
    assert(t);

    pa_tagstruct_putu32(t, s->direction == PA_STREAM_PLAYBACK ? PA_COMMAND_LOOKUP_SINK : PA_COMMAND_LOOKUP_SOURCE);
    pa_tagstruct_putu32(t, tag = s->context->ctag++);
    pa_tagstruct_puts(t, tdev);

    pa_pstream_send_tagstruct(s->context->pstream, t);
    pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, lookup_device_callback, s);
}

struct pa_stream* 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_stream*s, int success, void *userdata),
    void *userdata) {
    
    struct pa_stream *s;

    assert(c && name && ss && c->state == CONTEXT_READY);
    
    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->get_latency_callback = NULL;
    s->get_latency_userdata = NULL;

    s->name = strdup(name);
    s->state = STREAM_CREATING;
    s->requested_bytes = 0;
    s->channel = 0;
    s->channel_valid = 0;
    s->device_index = (uint32_t) -1;
    s->direction = dir;
    s->sample_spec = *ss;
    if (attr)
        s->buffer_attr = *attr;
    else {
        s->buffer_attr.maxlength = DEFAULT_MAXLENGTH;
        s->buffer_attr.tlength = DEFAULT_TLENGTH;
        s->buffer_attr.prebuf = DEFAULT_PREBUF;
        s->buffer_attr.minreq = DEFAULT_MINREQ;
        s->buffer_attr.fragsize = DEFAULT_FRAGSIZE;
    }

    s->next = c->first_stream;
    if (s->next)
        s->next->previous = s;
    s->previous = NULL;
    c->first_stream = s;

    if (dev)
        lookup_device(s, dev);
    else
        create_stream(s, (uint32_t) -1);
    
    return s;
}

void pa_stream_free(struct pa_stream *s) {
    assert(s && s->context);

    if (s->context->pdispatch) 
        pa_pdispatch_unregister_reply(s->context->pdispatch, s);
    
    free(s->name);

    if (s->channel_valid && s->context->state == CONTEXT_READY) {
        struct pa_tagstruct *t = pa_tagstruct_new(NULL, 0);
        assert(t);
    
        pa_tagstruct_putu32(t, PA_COMMAND_DELETE_PLAYBACK_STREAM);
        pa_tagstruct_putu32(t, s->context->ctag++);
        pa_tagstruct_putu32(t, s->channel);
        pa_pstream_send_tagstruct(s->context->pstream, t);
    }
    
    if (s->channel_valid)
        pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_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 pa_memchunk chunk;
    assert(s && s->context && data && length && s->state == STREAM_READY);

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

    pa_pstream_send_memblock(s->context->pstream, s->channel, 0, &chunk);
    pa_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 && s->state == STREAM_READY);
    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;
}

int pa_context_is_pending(struct pa_context *c) {
    assert(c);

    if (c->state != CONTEXT_READY)
        return 0;

    return pa_pstream_is_pending(c->pstream) || pa_pdispatch_is_pending(c->pdispatch);
}

struct pa_context* pa_stream_get_context(struct pa_stream *p) {
    assert(p);
    return p->context;
}

static void set_dispatch_callbacks(struct pa_context *c);

static void pdispatch_drain_callback(struct pa_pdispatch*pd, void *userdata) {
    set_dispatch_callbacks(userdata);
}

static void pstream_drain_callback(struct pa_pstream *s, void *userdata) {
    set_dispatch_callbacks(userdata);
}

static void set_dispatch_callbacks(struct pa_context *c) {
    assert(c && c->state == CONTEXT_READY);

    pa_pstream_set_drain_callback(c->pstream, NULL, NULL);
    pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL);
    
    if (pa_pdispatch_is_pending(c->pdispatch)) {
        pa_pdispatch_set_drain_callback(c->pdispatch, pdispatch_drain_callback, c);
        return;
    }

    if (pa_pstream_is_pending(c->pstream)) {
        pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
        return;
    }

    assert(c->drain_complete_callback);
    c->drain_complete_callback(c, c->drain_complete_userdata);
}

int pa_context_drain(
    struct pa_context *c, 
    void (*complete) (struct pa_context*c, void *userdata),
    void *userdata) {

    assert(c && c->state == CONTEXT_READY);

    if (complete == NULL) {
        c->drain_complete_callback = NULL;
        pa_pstream_set_drain_callback(c->pstream, NULL, NULL);
        pa_pdispatch_set_drain_callback(c->pdispatch, NULL, NULL);
        return 0;
    }
    
    if (!pa_context_is_pending(c))
        return -1;
    
    c->drain_complete_callback = complete;
    c->drain_complete_userdata = userdata;

    set_dispatch_callbacks(c);

    return 0;
}

static void stream_drain_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_stream *s = userdata;
    assert(pd && s);
    
    if (command != PA_COMMAND_REPLY) {
        if (handle_error(s->context, command, t) < 0) {
            context_dead(s->context);
            return;
        }

        stream_dead(s);
        return;
    }

    if (s->state != STREAM_READY)
        return;

    if (!pa_tagstruct_eof(t)) {
        s->context->error = PA_ERROR_PROTOCOL;
        context_dead(s->context);
        return;
    }

    if (s->drain_complete_callback) {
        void (*temp) (struct pa_stream*s, void *userdata) = s->drain_complete_callback;
        s->drain_complete_callback = NULL;
        temp(s, s->drain_complete_userdata);
    }
}


void pa_stream_drain(struct pa_stream *s, void (*complete) (struct pa_stream*s, void *userdata), void *userdata) {
    struct pa_tagstruct *t;
    uint32_t tag;
    assert(s && s->state == STREAM_READY);

    if (!complete) {
        s->drain_complete_callback = NULL;
        return;
    }

    s->drain_complete_callback = complete;
    s->drain_complete_userdata = userdata;

    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    
    pa_tagstruct_putu32(t, PA_COMMAND_DRAIN_PLAYBACK_STREAM);
    pa_tagstruct_putu32(t, tag = s->context->ctag++);
    pa_tagstruct_putu32(t, s->channel);
    pa_pstream_send_tagstruct(s->context->pstream, t);
    pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_drain_callback, s);
}

void pa_context_exit(struct pa_context *c) {
    struct pa_tagstruct *t;
    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    pa_tagstruct_putu32(t, PA_COMMAND_EXIT);
    pa_tagstruct_putu32(t, c->ctag++);
    pa_pstream_send_tagstruct(c->pstream, t);
}

static void context_stat_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_context *c = userdata;
    uint32_t total, count;
    assert(pd && c);

    if (command != PA_COMMAND_REPLY) {
        if (handle_error(c, command, t) < 0) {
            context_dead(c);
            return;
        }

        if (c->stat_callback)
            c->stat_callback(c, (uint32_t) -1, (uint32_t) -1, c->stat_userdata);
        return;
    }

    if (pa_tagstruct_getu32(t, &count) < 0 ||
        pa_tagstruct_getu32(t, &total) < 0 ||
        !pa_tagstruct_eof(t)) {
        c->error = PA_ERROR_PROTOCOL;
        context_dead(c);
        return;
    }

    if (c->stat_callback)
        c->stat_callback(c, count, total, c->stat_userdata);
}

void pa_context_stat(struct pa_context *c, void (*cb)(struct pa_context *c, uint32_t count, uint32_t total, void *userdata), void *userdata) {
    uint32_t tag;
    struct pa_tagstruct *t;

    c->stat_callback = cb;
    c->stat_userdata = userdata;

    if (cb == NULL)
        return;
    
    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    pa_tagstruct_putu32(t, PA_COMMAND_STAT);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, context_stat_callback, c);
}

static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_stream *s = userdata;
    uint32_t latency;
    assert(pd && s);

    if (command != PA_COMMAND_REPLY) {
        if (handle_error(s->context, command, t) < 0) {
            context_dead(s->context);
            return;
        }

        if (s->get_latency_callback)
            s->get_latency_callback(s, (uint32_t) -1, s->get_latency_userdata);
        return;
    }

    if (pa_tagstruct_getu32(t, &latency) < 0 ||
        !pa_tagstruct_eof(t)) {
        s->context->error = PA_ERROR_PROTOCOL;
        context_dead(s->context);
        return;
    }

    if (s->get_latency_callback)
        s->get_latency_callback(s, latency, s->get_latency_userdata);
}

void pa_stream_get_latency(struct pa_stream *p, void (*cb)(struct pa_stream *p, uint32_t latency, void *userdata), void *userdata) {
    uint32_t tag;
    struct pa_tagstruct *t;

    p->get_latency_callback = cb;
    p->get_latency_userdata = userdata;

    if (cb == NULL)
        return;
    
    t = pa_tagstruct_new(NULL, 0);
    assert(t);
    pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
    pa_tagstruct_putu32(t, tag = p->context->ctag++);
    pa_tagstruct_putu32(t, p->channel);
    pa_pstream_send_tagstruct(p->context->pstream, t);
    pa_pdispatch_register_reply(p->context->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, p);
}