summaryrefslogtreecommitdiffstats
path: root/polyp/polyplib-context.c
blob: 8a7c719c2425430ec49187941495efe3798de322 (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
/* $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 <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/wait.h>

#include "polyplib-internal.h"
#include "polyplib-context.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"
#include "xmalloc.h"
#include "log.h"
#include "client-conf.h"

#define DEFAULT_SERVER "/tmp/polypaudio/native"

static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
    [PA_COMMAND_REQUEST] = { pa_command_request },
    [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { pa_command_stream_killed },
    [PA_COMMAND_RECORD_STREAM_KILLED] = { pa_command_stream_killed },
    [PA_COMMAND_SUBSCRIBE_EVENT] = { pa_command_subscribe_event },
};

struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) {
    struct pa_context *c;
    assert(mainloop && name);
    
    c = pa_xmalloc(sizeof(struct pa_context));
    c->ref = 1;
    c->name = pa_xstrdup(name);
    c->mainloop = mainloop;
    c->client = NULL;
    c->pstream = NULL;
    c->pdispatch = NULL;
    c->playback_streams = pa_dynarray_new();
    c->record_streams = pa_dynarray_new();
    assert(c->playback_streams && c->record_streams);

    PA_LLIST_HEAD_INIT(struct pa_stream, c->streams);
    PA_LLIST_HEAD_INIT(struct pa_operation, c->operations);
    
    c->error = PA_ERROR_OK;
    c->state = PA_CONTEXT_UNCONNECTED;
    c->ctag = 0;

    c->state_callback = NULL;
    c->state_userdata = NULL;

    c->subscribe_callback = NULL;
    c->subscribe_userdata = NULL;

    c->memblock_stat = pa_memblock_stat_new();
    c->local = -1;
    
    pa_check_signal_is_blocked(SIGPIPE);

    c->conf = pa_client_conf_new();
    pa_client_conf_load(c->conf, NULL);
    pa_client_conf_env(c->conf);
    
    return c;
}

static void context_free(struct pa_context *c) {
    assert(c);

    while (c->operations)
        pa_operation_cancel(c->operations);

    while (c->streams)
        pa_stream_set_state(c->streams, PA_STREAM_TERMINATED);
    
    if (c->client)
        pa_socket_client_unref(c->client);
    if (c->pdispatch)
        pa_pdispatch_unref(c->pdispatch);
    if (c->pstream) {
        pa_pstream_close(c->pstream);
        pa_pstream_unref(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);

    pa_memblock_stat_unref(c->memblock_stat);

    if (c->conf)
        pa_client_conf_free(c->conf);
    
    pa_xfree(c->name);
    pa_xfree(c);
}

struct pa_context* pa_context_ref(struct pa_context *c) {
    assert(c && c->ref >= 1);
    c->ref++;
    return c;
}

void pa_context_unref(struct pa_context *c) {
    assert(c && c->ref >= 1);

    if ((--(c->ref)) == 0)
        context_free(c);
}

void pa_context_set_state(struct pa_context *c, enum pa_context_state st) {
    assert(c);
    
    if (c->state == st)
        return;

    pa_context_ref(c);

    if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
        struct pa_stream *s;
        
        s = c->streams ? pa_stream_ref(c->streams) : NULL;
        while (s) {
            struct pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
            pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
            pa_stream_unref(s);
            s = n;
        }

        if (c->pdispatch)
            pa_pdispatch_unref(c->pdispatch);
        c->pdispatch = NULL;
    
        if (c->pstream) {
            pa_pstream_close(c->pstream);
            pa_pstream_unref(c->pstream);
        }
        c->pstream = NULL;
    
        if (c->client)
            pa_socket_client_unref(c->client);
        c->client = NULL;
    }

    c->state = st;
    if (c->state_callback)
        c->state_callback(c, c->state_userdata);

    pa_context_unref(c);
}

void pa_context_fail(struct pa_context *c, int error) {
    assert(c);
    c->error = error;
    pa_context_set_state(c, PA_CONTEXT_FAILED);
}

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

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

    pa_context_ref(c);
    
    if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
        pa_log(__FILE__": invalid packet.\n");
        pa_context_fail(c, PA_ERROR_PROTOCOL);
    }

    pa_context_unref(c);
}

static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_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);

    pa_context_ref(c);
    
    if ((s = pa_dynarray_get(c->record_streams, channel))) {
        if (s->read_callback)
            s->read_callback(s, (uint8_t*) chunk->memblock->data + chunk->index, chunk->length, s->read_userdata);
    }

    pa_context_unref(c);
}

int pa_context_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) {
            pa_context_fail(c, PA_ERROR_PROTOCOL);
            return -1;
                
        }
    } else if (command == PA_COMMAND_TIMEOUT)
        c->error = PA_ERROR_TIMEOUT;
    else {
        pa_context_fail(c, PA_ERROR_PROTOCOL);
        return -1;
    }

    return 0;
}

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 == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME));

    pa_context_ref(c);
    
    if (command != PA_COMMAND_REPLY) {
        
        if (pa_context_handle_error(c, command, t) < 0)
            pa_context_fail(c, PA_ERROR_PROTOCOL);

        pa_context_fail(c, c->error);
        goto finish;
    }

    switch(c->state) {
        case PA_CONTEXT_AUTHORIZING: {
            struct pa_tagstruct *t;
            t = pa_tagstruct_new(NULL, 0);
            assert(t);
            pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_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);

            pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
            break;
        }

        case PA_CONTEXT_SETTING_NAME :
            pa_context_set_state(c, PA_CONTEXT_READY);
            break;
            
        default:
            assert(0);
    }

finish:
    pa_context_unref(c);
}

static void setup_context(struct pa_context *c, struct pa_iochannel *io) {
    struct pa_tagstruct *t;
    uint32_t tag;
    assert(c && io);

    pa_context_ref(c);
    
    assert(!c->pstream);
    c->pstream = pa_pstream_new(c->mainloop, io, c->memblock_stat);
    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);

    assert(!c->pdispatch);
    c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
    assert(c->pdispatch);

    if (pa_authkey_load_from_home(PA_NATIVE_COOKIE_FILE, c->auth_cookie, sizeof(c->auth_cookie)) < 0) {
        pa_context_fail(c, PA_ERROR_AUTHKEY);
        goto finish;
    }

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

    pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);

finish:
    
    pa_context_unref(c);
}

static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) {
    struct pa_context *c = userdata;
    assert(client && c && c->state == PA_CONTEXT_CONNECTING);

    pa_context_ref(c);
    
    pa_socket_client_unref(client);
    c->client = NULL;

    if (!io) {
        pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
        goto finish;
    }

    setup_context(c, io);

finish:
    pa_context_unref(c);
}

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

    snprintf(host, sizeof(host), "%s", server);
    host[strcspn(host, ":")] = 0;
    
    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(host, port, &hints, &result) != 0)
        return NULL;
    assert(result);
    
    sa = pa_xmalloc(*len = result->ai_addrlen);
    memcpy(sa, result->ai_addr, *len);

    freeaddrinfo(result);
    
    return sa;
}

static int default_server_is_running(void) {
    struct stat st;
    
    if (DEFAULT_SERVER[0] != '/')
        return 1;

    if (stat(DEFAULT_SERVER, &st) < 0)
        return 0;

    return 1;
}

static int context_connect_spawn(struct pa_context *c, const struct pa_spawn_api *api) {
    pid_t pid;
    int status, r;
    int fds[2] = { -1, -1} ;
    struct pa_iochannel *io;

    pa_context_ref(c);
    
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
        pa_log(__FILE__": socketpair() failed: %s\n", strerror(errno));
        pa_context_fail(c, PA_ERROR_INTERNAL);
        goto fail;
    }

    if (api && api->prefork)
        api->prefork();

    if ((pid = fork()) < 0) {
        pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
        pa_context_fail(c, PA_ERROR_INTERNAL);

        if (api && api->postfork)
            api->postfork();
        
        goto fail;
    } else if (!pid) {
        /* Child */
        
        char t[128];
        const char *state = NULL;
#define MAX_ARGS 64
        char *argv[MAX_ARGS+1];
        int n = 0;

        close(fds[0]);
        
        if (api && api->atfork)
            api->atfork();

        snprintf(t, sizeof(t), "%s=1", ENV_AUTOSPAWNED);
        putenv(t);

        argv[n++] = c->conf->daemon_binary;
        argv[n++] = "--daemonize=yes";
        
        snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
        argv[n++] = t;

        while (n < MAX_ARGS) {
            char *a;

            if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
                break;
            
            argv[n++] = a;
        }

        argv[n++] = NULL;

        execv(argv[0], argv);
        exit(1);
    } 

    /* Parent */

    r = waitpid(pid, &status, 0);

    if (api && api->postfork)
        api->postfork();
        
    if (r < 0) {
        pa_log(__FILE__": waitpid() failed: %s\n", strerror(errno));
        pa_context_fail(c, PA_ERROR_INTERNAL);
        goto fail;
    } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
        goto fail;
    }

    close(fds[1]);

    c->local = 1;
    
    io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
    setup_context(c, io);

    pa_context_unref(c);

    return 0;

fail:
    if (fds[0] != -1)
        close(fds[0]);
    if (fds[1] != -1)
        close(fds[1]);

    pa_context_unref(c);

    return -1;
}

int pa_context_connect(struct pa_context *c, const char *server, int spawn, const struct pa_spawn_api *api) {
    int r = -1;
    assert(c && c->ref >= 1 && c->state == PA_CONTEXT_UNCONNECTED);

    if (!server)
        server = c->conf->default_server;

    if (!server && spawn && c->conf->autospawn && !default_server_is_running())
        return context_connect_spawn(c, api);

    if (!server)
        server = DEFAULT_SERVER;

    pa_context_ref(c);

    assert(!c->client);
    
    if (*server == '/') {
        if (!(c->client = pa_socket_client_new_unix(c->mainloop, server))) {
            pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
            goto finish;
        }

        c->local = 1;
    } else {
        struct sockaddr* sa;
        size_t sa_len;

        if (!(sa = resolve_server(server, &sa_len))) {
            pa_context_fail(c, PA_ERROR_INVALIDSERVER);
            goto finish;
        }

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

        if (!c->client) {
            pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
            goto finish;
        }

        c->local = 0;
    }

    pa_socket_client_set_callback(c->client, on_connection, c);
    pa_context_set_state(c, PA_CONTEXT_CONNECTING);

    r = 0;
    
finish:
    pa_context_unref(c);
    
    return r;
}

void pa_context_disconnect(struct pa_context *c) {
    assert(c);
    pa_context_set_state(c, PA_CONTEXT_TERMINATED);
}

enum pa_context_state pa_context_get_state(struct pa_context *c) {
    assert(c && c->ref >= 1);
    return c->state;
}

int pa_context_errno(struct pa_context *c) {
    assert(c && c->ref >= 1);
    return c->error;
}

void pa_context_set_state_callback(struct pa_context *c, void (*cb)(struct pa_context *c, void *userdata), void *userdata) {
    assert(c && c->ref >= 1);
    c->state_callback = cb;
    c->state_userdata = userdata;
}

int pa_context_is_pending(struct pa_context *c) {
    assert(c && c->ref >= 1);

/*     pa_log("pstream: %i\n", pa_pstream_is_pending(c->pstream)); */
/*     pa_log("pdispatch: %i\n", pa_pdispatch_is_pending(c->pdispatch)); */
    
    return (c->pstream && pa_pstream_is_pending(c->pstream)) || (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) || c->client;
}

static void set_dispatch_callbacks(struct pa_operation *o);

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_operation *o) {
    int done = 1;
    assert(o && o->context && o->context->ref >= 1 && o->ref >= 1 && o->context->state == PA_CONTEXT_READY);

    pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
    pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
    
    if (pa_pdispatch_is_pending(o->context->pdispatch)) {
        pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
        done = 0;
    }

    if (pa_pstream_is_pending(o->context->pstream)) {
        pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
        done = 0;
    }

    if (!done)
        pa_operation_ref(o);
    else {
        if (o->callback) {
            void (*cb)(struct pa_context *c, void *userdata);
            cb = (void (*)(struct pa_context*, void*)) o->callback;
            cb(o->context, o->userdata);
        }
        
        pa_operation_done(o);
    }   

    pa_operation_unref(o);
}

struct pa_operation* pa_context_drain(struct pa_context *c, void (*cb) (struct pa_context*c, void *userdata), void *userdata) {
    struct pa_operation *o;
    assert(c && c->ref >= 1);

    if (c->state != PA_CONTEXT_READY)
        return NULL;

    if (!pa_context_is_pending(c))
        return NULL;

    o = pa_operation_new(c, NULL);
    assert(o);
    o->callback = cb;
    o->userdata = userdata;

    set_dispatch_callbacks(pa_operation_ref(o));

    return o;
}

void pa_context_exit_daemon(struct pa_context *c) {
    struct pa_tagstruct *t;
    assert(c && c->ref >= 1);
    
    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);
}

void pa_context_simple_ack_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
    struct pa_operation *o = userdata;
    int success = 1;
    assert(pd && o && o->context && o->ref >= 1);

    if (command != PA_COMMAND_REPLY) {
        if (pa_context_handle_error(o->context, command, t) < 0)
            goto finish;

        success = 0;
    } else if (!pa_tagstruct_eof(t)) {
        pa_context_fail(o->context, PA_ERROR_PROTOCOL);
        goto finish;
    }

    if (o->callback) {
        void (*cb)(struct pa_context *c, int success, void *userdata) = o->callback;
        cb(o->context, success, o->userdata);
    }

finish:
    pa_operation_done(o);
    pa_operation_unref(o);
}

struct pa_operation* pa_context_send_simple_command(struct pa_context *c, uint32_t command, void (*internal_callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata), void (*cb)(), void *userdata) {
    struct pa_tagstruct *t;
    struct pa_operation *o;
    uint32_t tag;
    assert(c && cb);

    o = pa_operation_new(c, NULL);
    o->callback = cb;
    o->userdata = userdata;

    t = pa_tagstruct_new(NULL, 0);
    pa_tagstruct_putu32(t, command);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_callback, o);

    return pa_operation_ref(o);
}

const char* pa_get_library_version(void) {
    return PACKAGE_VERSION;
}

struct pa_operation* pa_context_set_default_sink(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success, void *userdata), void *userdata) {
    struct pa_tagstruct *t;
    struct pa_operation *o;
    uint32_t tag;
    assert(c && cb);

    o = pa_operation_new(c, NULL);
    o->callback = cb;
    o->userdata = userdata;

    t = pa_tagstruct_new(NULL, 0);
    pa_tagstruct_putu32(t, PA_COMMAND_SET_DEFAULT_SINK);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_tagstruct_puts(t, name);
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT,  pa_context_simple_ack_callback, o);

    return pa_operation_ref(o);
}

struct pa_operation* pa_context_set_default_source(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success,  void *userdata), void *userdata) {
    struct pa_tagstruct *t;
    struct pa_operation *o;
    uint32_t tag;
    assert(c && cb);

    o = pa_operation_new(c, NULL);
    o->callback = cb;
    o->userdata = userdata;

    t = pa_tagstruct_new(NULL, 0);
    pa_tagstruct_putu32(t, PA_COMMAND_SET_DEFAULT_SOURCE);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_tagstruct_puts(t, name);
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT,  pa_context_simple_ack_callback, o);

    return pa_operation_ref(o);
}

int pa_context_is_local(struct pa_context *c) {
    assert(c);
    return c->local;
}

struct pa_operation* pa_context_set_name(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success,  void *userdata), void *userdata) {
    struct pa_tagstruct *t;
    struct pa_operation *o;
    uint32_t tag;
    assert(c && name && cb);

    o = pa_operation_new(c, NULL);
    o->callback = cb;
    o->userdata = userdata;

    t = pa_tagstruct_new(NULL, 0);
    pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_NAME);
    pa_tagstruct_putu32(t, tag = c->ctag++);
    pa_tagstruct_puts(t, name);
    pa_pstream_send_tagstruct(c->pstream, t);
    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT,  pa_context_simple_ack_callback, o);

    return pa_operation_ref(o);
}