add support for setting/getting default sink/source via native protocol
[profile/ivi/pulseaudio-panda.git] / polyp / protocol-native.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include "protocol-native.h"
32 #include "native-common.h"
33 #include "packet.h"
34 #include "client.h"
35 #include "source-output.h"
36 #include "sink-input.h"
37 #include "pstream.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
41 #include "authkey.h"
42 #include "namereg.h"
43 #include "scache.h"
44 #include "xmalloc.h"
45 #include "util.h"
46 #include "subscribe.h"
47 #include "log.h"
48
49 struct connection;
50 struct pa_protocol_native;
51
52 struct record_stream {
53     struct connection *connection;
54     uint32_t index;
55     struct pa_source_output *source_output;
56     struct pa_memblockq *memblockq;
57     size_t fragment_size;
58 };
59
60 struct playback_stream {
61     int type;
62     struct connection *connection;
63     uint32_t index;
64     struct pa_sink_input *sink_input;
65     struct pa_memblockq *memblockq;
66     size_t requested_bytes;
67     int drain_request;
68     uint32_t drain_tag;
69 };
70
71 struct upload_stream {
72     int type;
73     struct connection *connection;
74     uint32_t index;
75     struct pa_memchunk memchunk;
76     size_t length;
77     char *name;
78     struct pa_sample_spec sample_spec;
79 };
80
81 struct output_stream {
82     int type;
83 };
84
85 enum {
86     UPLOAD_STREAM,
87     PLAYBACK_STREAM
88 };
89
90 struct connection {
91     int authorized;
92     struct pa_protocol_native *protocol;
93     struct pa_client *client;
94     struct pa_pstream *pstream;
95     struct pa_pdispatch *pdispatch;
96     struct pa_idxset *record_streams, *output_streams;
97     uint32_t rrobin_index;
98     struct pa_subscription *subscription;
99 };
100
101 struct pa_protocol_native {
102     struct pa_module *module;
103     int public;
104     struct pa_core *core;
105     struct pa_socket_server *server;
106     struct pa_idxset *connections;
107     uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
108 };
109
110 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
111 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length);
112 static void sink_input_kill_cb(struct pa_sink_input *i);
113 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
114
115 static void request_bytes(struct playback_stream*s);
116
117 static void source_output_kill_cb(struct pa_source_output *o);
118 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
119
120 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
121 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
122 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
135 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
136 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
137 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
138 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
139 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
140 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
141 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
142
143 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
144     [PA_COMMAND_ERROR] = { NULL },
145     [PA_COMMAND_TIMEOUT] = { NULL },
146     [PA_COMMAND_REPLY] = { NULL },
147     [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
148     [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
149     [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
150     [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
151     [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
152     [PA_COMMAND_AUTH] = { command_auth },
153     [PA_COMMAND_REQUEST] = { NULL },
154     [PA_COMMAND_EXIT] = { command_exit },
155     [PA_COMMAND_SET_NAME] = { command_set_name },
156     [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
157     [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
158     [PA_COMMAND_STAT] = { command_stat },
159     [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
160     [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
161     [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
162     [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
163     [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
164     [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
165     [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
166     [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
167     [PA_COMMAND_GET_CLIENT_INFO] = { command_get_info },
168     [PA_COMMAND_GET_MODULE_INFO] = { command_get_info },
169     [PA_COMMAND_GET_SINK_INPUT_INFO] = { command_get_info },
170     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = { command_get_info },
171     [PA_COMMAND_GET_SAMPLE_INFO] = { command_get_info },
172     [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
173     [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
174     [PA_COMMAND_GET_MODULE_INFO_LIST] = { command_get_info_list },
175     [PA_COMMAND_GET_CLIENT_INFO_LIST] = { command_get_info_list },
176     [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = { command_get_info_list },
177     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = { command_get_info_list },
178     [PA_COMMAND_GET_SAMPLE_INFO_LIST] = { command_get_info_list },
179     [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
180     [PA_COMMAND_SUBSCRIBE] = { command_subscribe },
181     [PA_COMMAND_SET_SINK_VOLUME] = { command_set_volume },
182     [PA_COMMAND_SET_SINK_INPUT_VOLUME] = { command_set_volume },
183     [PA_COMMAND_CORK_PLAYBACK_STREAM] = { command_cork_playback_stream },
184     [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
185     [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
186     [PA_COMMAND_SET_DEFAULT_SINK] = { command_set_default_sink_or_source },
187     [PA_COMMAND_SET_DEFAULT_SOURCE] = { command_set_default_sink_or_source },
188 };
189
190 /* structure management */
191
192 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
193     struct upload_stream *s;
194     assert(c && ss && name && length);
195     
196     s = pa_xmalloc(sizeof(struct upload_stream));
197     s->type = UPLOAD_STREAM;
198     s->connection = c;
199     s->sample_spec = *ss;
200     s->name = pa_xstrdup(name);
201
202     s->memchunk.memblock = NULL;
203     s->memchunk.index = 0;
204     s->memchunk.length = 0;
205
206     s->length = length;
207     
208     pa_idxset_put(c->output_streams, s, &s->index);
209     return s;
210 }
211
212 static void upload_stream_free(struct upload_stream *o) {
213     assert(o && o->connection);
214
215     pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
216
217     pa_xfree(o->name);
218     
219     if (o->memchunk.memblock)
220         pa_memblock_unref(o->memchunk.memblock);
221     
222     pa_xfree(o);
223 }
224
225 static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, const struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) {
226     struct record_stream *s;
227     struct pa_source_output *source_output;
228     size_t base;
229     assert(c && source && ss && name && maxlength);
230
231     if (!(source_output = pa_source_output_new(source, name, ss)))
232         return NULL;
233
234     s = pa_xmalloc(sizeof(struct record_stream));
235     s->connection = c;
236     s->source_output = source_output;
237     s->source_output->push = source_output_push_cb;
238     s->source_output->kill = source_output_kill_cb;
239     s->source_output->userdata = s;
240     s->source_output->owner = c->protocol->module;
241     s->source_output->client = c->client;
242
243     s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
244     assert(s->memblockq);
245
246     s->fragment_size = (fragment_size/base)*base;
247     if (!s->fragment_size)
248         s->fragment_size = base;
249
250     pa_idxset_put(c->record_streams, s, &s->index);
251     return s;
252 }
253
254 static void record_stream_free(struct record_stream* r) {
255     assert(r && r->connection);
256
257     pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
258     pa_source_output_free(r->source_output);
259     pa_memblockq_free(r->memblockq);
260     pa_xfree(r);
261 }
262
263 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
264                                                    size_t maxlength,
265                                                    size_t tlength,
266                                                    size_t prebuf,
267                                                    size_t minreq) {
268     struct playback_stream *s;
269     struct pa_sink_input *sink_input;
270     assert(c && sink && ss && name && maxlength);
271
272     if (!(sink_input = pa_sink_input_new(sink, name, ss)))
273         return NULL;
274     
275     s = pa_xmalloc(sizeof(struct playback_stream));
276     s->type = PLAYBACK_STREAM;
277     s->connection = c;
278     s->sink_input = sink_input;
279     
280     s->sink_input->peek = sink_input_peek_cb;
281     s->sink_input->drop = sink_input_drop_cb;
282     s->sink_input->kill = sink_input_kill_cb;
283     s->sink_input->get_latency = sink_input_get_latency_cb;
284     s->sink_input->userdata = s;
285     s->sink_input->owner = c->protocol->module;
286     s->sink_input->client = c->client;
287     
288     s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
289     assert(s->memblockq);
290
291     s->requested_bytes = 0;
292     s->drain_request = 0;
293     
294     pa_idxset_put(c->output_streams, s, &s->index);
295     return s;
296 }
297
298 static void playback_stream_free(struct playback_stream* p) {
299     assert(p && p->connection);
300
301     if (p->drain_request)
302         pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
303
304     pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
305     pa_sink_input_free(p->sink_input);
306     pa_memblockq_free(p->memblockq);
307     pa_xfree(p);
308 }
309
310 static void connection_free(struct connection *c) {
311     struct record_stream *r;
312     struct output_stream *o;
313     assert(c && c->protocol);
314
315     pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
316     while ((r = pa_idxset_first(c->record_streams, NULL)))
317         record_stream_free(r);
318     pa_idxset_free(c->record_streams, NULL, NULL);
319
320     while ((o = pa_idxset_first(c->output_streams, NULL)))
321         if (o->type == PLAYBACK_STREAM)
322             playback_stream_free((struct playback_stream*) o);
323         else
324             upload_stream_free((struct upload_stream*) o);
325     pa_idxset_free(c->output_streams, NULL, NULL);
326
327     pa_pdispatch_unref(c->pdispatch);
328     pa_pstream_close(c->pstream);
329     pa_pstream_unref(c->pstream);
330     pa_client_free(c->client);
331
332     if (c->subscription)
333         pa_subscription_free(c->subscription);
334     
335     pa_xfree(c);
336 }
337
338 static void request_bytes(struct playback_stream *s) {
339     struct pa_tagstruct *t;
340     size_t l;
341     assert(s);
342
343     if (!(l = pa_memblockq_missing(s->memblockq)))
344         return;
345
346     if (l <= s->requested_bytes)
347         return;
348
349     l -= s->requested_bytes;
350
351     if (l < pa_memblockq_get_minreq(s->memblockq))
352         return;
353     
354     s->requested_bytes += l;
355
356     t = pa_tagstruct_new(NULL, 0);
357     assert(t);
358     pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
359     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
360     pa_tagstruct_putu32(t, s->index);
361     pa_tagstruct_putu32(t, l);
362     pa_pstream_send_tagstruct(s->connection->pstream, t);
363
364     /*pa_log(__FILE__": Requesting %u bytes\n", l);*/
365 }
366
367 static void send_memblock(struct connection *c) {
368     uint32_t start;
369     struct record_stream *r;
370
371     start = PA_IDXSET_INVALID;
372     for (;;) {
373         struct pa_memchunk chunk;
374         
375         if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
376             return;
377
378         if (start == PA_IDXSET_INVALID)
379             start = c->rrobin_index;
380         else if (start == c->rrobin_index)
381             return;
382
383         if (pa_memblockq_peek(r->memblockq,  &chunk) >= 0) {
384             struct pa_memchunk schunk = chunk;
385             
386             if (schunk.length > r->fragment_size)
387                 schunk.length = r->fragment_size;
388
389             pa_pstream_send_memblock(c->pstream, r->index, 0, &schunk);
390             pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
391             pa_memblock_unref(schunk.memblock);
392             
393             return;
394         }
395     }
396 }
397
398 static void send_playback_stream_killed(struct playback_stream *p) {
399     struct pa_tagstruct *t;
400     assert(p);
401
402     t = pa_tagstruct_new(NULL, 0);
403     assert(t);
404     pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
405     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
406     pa_tagstruct_putu32(t, p->index);
407     pa_pstream_send_tagstruct(p->connection->pstream, t);
408 }
409
410 static void send_record_stream_killed(struct record_stream *r) {
411     struct pa_tagstruct *t;
412     assert(r);
413
414     t = pa_tagstruct_new(NULL, 0);
415     assert(t);
416     pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
417     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
418     pa_tagstruct_putu32(t, r->index);
419     pa_pstream_send_tagstruct(r->connection->pstream, t);
420 }
421
422
423 /*** sinkinput callbacks ***/
424
425 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
426     struct playback_stream *s;
427     assert(i && i->userdata && chunk);
428     s = i->userdata;
429
430     if (pa_memblockq_peek(s->memblockq, chunk) < 0)
431         return -1;
432
433     return 0;
434 }
435
436 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
437     struct playback_stream *s;
438     assert(i && i->userdata && length);
439     s = i->userdata;
440
441     pa_memblockq_drop(s->memblockq, chunk, length);
442     request_bytes(s);
443
444     if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
445         pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
446         s->drain_request = 0;
447     }
448
449     /*pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq));*/
450 }
451
452 static void sink_input_kill_cb(struct pa_sink_input *i) {
453     assert(i && i->userdata);
454     send_playback_stream_killed((struct playback_stream *) i->userdata);
455     playback_stream_free((struct playback_stream *) i->userdata);
456 }
457
458 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
459     struct playback_stream *s;
460     assert(i && i->userdata);
461     s = i->userdata;
462
463     /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
464     
465     return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
466 }
467
468 /*** source_output callbacks ***/
469
470 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
471     struct record_stream *s;
472     assert(o && o->userdata && chunk);
473     s = o->userdata;
474     
475     pa_memblockq_push_align(s->memblockq, chunk, 0);
476     if (!pa_pstream_is_pending(s->connection->pstream))
477         send_memblock(s->connection);
478 }
479
480 static void source_output_kill_cb(struct pa_source_output *o) {
481     assert(o && o->userdata);
482     send_record_stream_killed((struct record_stream *) o->userdata);
483     record_stream_free((struct record_stream *) o->userdata);
484 }
485
486 /*** pdispatch callbacks ***/
487
488 static void protocol_error(struct connection *c) {
489     pa_log(__FILE__": protocol error, kicking client\n");
490     connection_free(c);
491 }
492
493 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
494     struct connection *c = userdata;
495     struct playback_stream *s;
496     size_t maxlength, tlength, prebuf, minreq;
497     uint32_t sink_index;
498     const char *name, *sink_name;
499     struct pa_sample_spec ss;
500     struct pa_tagstruct *reply;
501     struct pa_sink *sink;
502     assert(c && t && c->protocol && c->protocol->core);
503     
504     if (pa_tagstruct_gets(t, &name) < 0 ||
505         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
506         pa_tagstruct_getu32(t, &sink_index) < 0 ||
507         pa_tagstruct_gets(t, &sink_name) < 0 ||
508         pa_tagstruct_getu32(t, &maxlength) < 0 ||
509         pa_tagstruct_getu32(t, &tlength) < 0 ||
510         pa_tagstruct_getu32(t, &prebuf) < 0 ||
511         pa_tagstruct_getu32(t, &minreq) < 0 ||
512         !pa_tagstruct_eof(t)) {
513         protocol_error(c);
514         return;
515     }
516
517     if (!c->authorized) {
518         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
519         return;
520     }
521
522     if (sink_index != (uint32_t) -1)
523         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
524     else
525         sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
526
527     if (!sink) {
528         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
529         return;
530     }
531     
532     if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
533         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
534         return;
535     }
536     
537     reply = pa_tagstruct_new(NULL, 0);
538     assert(reply);
539     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
540     pa_tagstruct_putu32(reply, tag);
541     pa_tagstruct_putu32(reply, s->index);
542     assert(s->sink_input);
543     pa_tagstruct_putu32(reply, s->sink_input->index);
544     pa_pstream_send_tagstruct(c->pstream, reply);
545     request_bytes(s);
546 }
547
548 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
549     struct connection *c = userdata;
550     uint32_t channel;
551     assert(c && t);
552     
553     if (pa_tagstruct_getu32(t, &channel) < 0 ||
554         !pa_tagstruct_eof(t)) {
555         protocol_error(c);
556         return;
557     }
558
559     if (!c->authorized) {
560         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
561         return;
562     }
563
564     if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
565         struct playback_stream *s;
566         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
567             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
568             return;
569         }
570
571         playback_stream_free(s);
572     } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
573         struct record_stream *s;
574         if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
575             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
576             return;
577         }
578
579         record_stream_free(s);
580     } else {
581         struct upload_stream *s;
582         assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
583         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
584             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
585             return;
586         }
587
588         upload_stream_free(s);
589     }
590             
591     pa_pstream_send_simple_ack(c->pstream, tag);
592 }
593
594 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
595     struct connection *c = userdata;
596     struct record_stream *s;
597     size_t maxlength, fragment_size;
598     uint32_t source_index;
599     const char *name, *source_name;
600     struct pa_sample_spec ss;
601     struct pa_tagstruct *reply;
602     struct pa_source *source;
603     assert(c && t && c->protocol && c->protocol->core);
604     
605     if (pa_tagstruct_gets(t, &name) < 0 ||
606         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
607         pa_tagstruct_getu32(t, &source_index) < 0 ||
608         pa_tagstruct_gets(t, &source_name) < 0 ||
609         pa_tagstruct_getu32(t, &maxlength) < 0 ||
610         pa_tagstruct_getu32(t, &fragment_size) < 0 ||
611         !pa_tagstruct_eof(t)) {
612         protocol_error(c);
613         return;
614     }
615
616     if (!c->authorized) {
617         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
618         return;
619     }
620
621     if (source_index != (uint32_t) -1)
622         source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
623     else
624         source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1);
625
626     if (!source) {
627         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
628         return;
629     }
630     
631     if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
632         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
633         return;
634     }
635     
636     reply = pa_tagstruct_new(NULL, 0);
637     assert(reply);
638     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
639     pa_tagstruct_putu32(reply, tag);
640     pa_tagstruct_putu32(reply, s->index);
641     assert(s->source_output);
642     pa_tagstruct_putu32(reply, s->source_output->index);
643     pa_pstream_send_tagstruct(c->pstream, reply);
644 }
645
646 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
647     struct connection *c = userdata;
648     assert(c && t);
649     
650     if (!pa_tagstruct_eof(t)) {
651         protocol_error(c);
652         return;
653     }
654
655     if (!c->authorized) {
656         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
657         return;
658     }
659     
660     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
661     c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
662     pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
663     return;
664 }
665
666 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
667     struct connection *c = userdata;
668     const void*cookie;
669     assert(c && t);
670
671     if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
672         !pa_tagstruct_eof(t)) {
673         protocol_error(c);
674         return;
675     }
676         
677     if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
678         pa_log(__FILE__": Denied access to client with invalid authorization key.\n");
679         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
680         return;
681     }
682
683     c->authorized = 1;
684     pa_pstream_send_simple_ack(c->pstream, tag);
685     return;
686 }
687
688 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
689     struct connection *c = userdata;
690     const char *name;
691     assert(c && t);
692
693     if (pa_tagstruct_gets(t, &name) < 0 ||
694         !pa_tagstruct_eof(t)) {
695         protocol_error(c);
696         return;
697     }
698
699     pa_client_rename(c->client, name);
700     pa_pstream_send_simple_ack(c->pstream, tag);
701     return;
702 }
703
704 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
705     struct connection *c = userdata;
706     const char *name;
707     uint32_t index = PA_IDXSET_INVALID;
708     assert(c && t);
709
710     if (pa_tagstruct_gets(t, &name) < 0 ||
711         !pa_tagstruct_eof(t)) {
712         protocol_error(c);
713         return;
714     }
715
716     if (!c->authorized) {
717         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
718         return;
719     }
720
721     if (command == PA_COMMAND_LOOKUP_SINK) {
722         struct pa_sink *sink;
723         if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
724             index = sink->index;
725     } else {
726         struct pa_source *source;
727         assert(command == PA_COMMAND_LOOKUP_SOURCE);
728         if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
729             index = source->index;
730     }
731
732     if (index == PA_IDXSET_INVALID)
733         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
734     else {
735         struct pa_tagstruct *reply;
736         reply = pa_tagstruct_new(NULL, 0);
737         assert(reply);
738         pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
739         pa_tagstruct_putu32(reply, tag);
740         pa_tagstruct_putu32(reply, index);
741         pa_pstream_send_tagstruct(c->pstream, reply);
742     }
743 }
744
745 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
746     struct connection *c = userdata;
747     uint32_t index;
748     struct playback_stream *s;
749     assert(c && t);
750
751     if (pa_tagstruct_getu32(t, &index) < 0 ||
752         !pa_tagstruct_eof(t)) {
753         protocol_error(c);
754         return;
755     }
756
757     if (!c->authorized) {
758         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
759         return;
760     }
761
762     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
763         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
764         return;
765     }
766
767     s->drain_request = 0;
768
769     pa_memblockq_prebuf_disable(s->memblockq);
770     
771     if (!pa_memblockq_is_readable(s->memblockq)) {
772         pa_pstream_send_simple_ack(c->pstream, tag);
773     } else {
774         s->drain_request = 1;
775         s->drain_tag = tag;
776
777         pa_sink_notify(s->sink_input->sink);
778     }
779
780
781 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
782     struct connection *c = userdata;
783     struct pa_tagstruct *reply;
784     assert(c && t);
785
786     if (!pa_tagstruct_eof(t)) {
787         protocol_error(c);
788         return;
789     }
790
791     if (!c->authorized) {
792         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
793         return;
794     }
795
796     reply = pa_tagstruct_new(NULL, 0);
797     assert(reply);
798     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
799     pa_tagstruct_putu32(reply, tag);
800     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
801     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
802     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
803     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
804     pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
805     pa_pstream_send_tagstruct(c->pstream, reply);
806 }
807
808 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
809     struct connection *c = userdata;
810     struct pa_tagstruct *reply;
811     struct playback_stream *s;
812     uint32_t index;
813     assert(c && t);
814
815     if (pa_tagstruct_getu32(t, &index) < 0 ||
816         !pa_tagstruct_eof(t)) {
817         protocol_error(c);
818         return;
819     }
820
821     if (!c->authorized) {
822         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
823         return;
824     }
825
826     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
827         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
828         return;
829     }
830
831     reply = pa_tagstruct_new(NULL, 0);
832     assert(reply);
833     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
834     pa_tagstruct_putu32(reply, tag);
835     pa_tagstruct_putu32(reply, pa_sink_input_get_latency(s->sink_input));
836     pa_tagstruct_putu32(reply, pa_sink_get_latency(s->sink_input->sink));
837     pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
838     pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
839     pa_pstream_send_tagstruct(c->pstream, reply);
840 }
841
842 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
843     struct connection *c = userdata;
844     struct upload_stream *s;
845     size_t length;
846     const char *name;
847     struct pa_sample_spec ss;
848     struct pa_tagstruct *reply;
849     assert(c && t && c->protocol && c->protocol->core);
850     
851     if (pa_tagstruct_gets(t, &name) < 0 ||
852         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
853         pa_tagstruct_getu32(t, &length) < 0 ||
854         !pa_tagstruct_eof(t)) {
855         protocol_error(c);
856         return;
857     }
858
859     if (!c->authorized) {
860         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
861         return;
862     }
863
864     if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
865         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
866         return;
867     }
868     
869     if (!(s = upload_stream_new(c, &ss, name, length))) {
870         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
871         return;
872     }
873     
874     reply = pa_tagstruct_new(NULL, 0);
875     assert(reply);
876     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
877     pa_tagstruct_putu32(reply, tag);
878     pa_tagstruct_putu32(reply, s->index);
879     pa_pstream_send_tagstruct(c->pstream, reply);
880     
881     reply = pa_tagstruct_new(NULL, 0);
882     assert(reply);
883     pa_tagstruct_putu32(reply, PA_COMMAND_REQUEST);
884     pa_tagstruct_putu32(reply, (uint32_t) -1); /* tag */
885     pa_tagstruct_putu32(reply, s->index);
886     pa_tagstruct_putu32(reply, length);
887     pa_pstream_send_tagstruct(c->pstream, reply);
888 }
889
890 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
891     struct connection *c = userdata;
892     uint32_t channel;
893     struct upload_stream *s;
894     uint32_t index;
895     assert(c && t);
896     
897     if (pa_tagstruct_getu32(t, &channel) < 0 ||
898         !pa_tagstruct_eof(t)) {
899         protocol_error(c);
900         return;
901     }
902
903     if (!c->authorized) {
904         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
905         return;
906     }
907
908     if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
909         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
910         return;
911     }
912
913     pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
914     pa_pstream_send_simple_ack(c->pstream, tag);
915     upload_stream_free(s);
916 }
917
918 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
919     struct connection *c = userdata;
920     uint32_t sink_index, volume;
921     struct pa_sink *sink;
922     const char *name, *sink_name;
923     assert(c && t);
924
925     if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
926         pa_tagstruct_gets(t, &sink_name) < 0 ||
927         pa_tagstruct_getu32(t, &volume) < 0 ||
928         pa_tagstruct_gets(t, &name) < 0 ||
929         !pa_tagstruct_eof(t)) {
930         protocol_error(c);
931         return;
932     }
933     
934     if (!c->authorized) {
935         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
936         return;
937     }
938
939     if (sink_index != (uint32_t) -1)
940         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
941     else
942         sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
943
944     if (!sink) {
945         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
946         return;
947     }
948
949     if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
950         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
951         return;
952     }
953
954     pa_pstream_send_simple_ack(c->pstream, tag);
955 }
956
957 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
958     struct connection *c = userdata;
959     const char *name;
960     assert(c && t);
961
962     if (pa_tagstruct_gets(t, &name) < 0 ||
963         !pa_tagstruct_eof(t)) {
964         protocol_error(c);
965         return;
966     }
967
968     if (!c->authorized) {
969         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
970         return;
971     }
972
973     if (pa_scache_remove_item(c->protocol->core, name) < 0) {
974         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
975         return;
976     }
977
978     pa_pstream_send_simple_ack(c->pstream, tag);
979 }
980
981 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
982     assert(t && sink);
983     pa_tagstruct_putu32(t, sink->index);
984     pa_tagstruct_puts(t, sink->name);
985     pa_tagstruct_puts(t, sink->description ? sink->description : "");
986     pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
987     pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
988     pa_tagstruct_putu32(t, sink->volume);
989     pa_tagstruct_putu32(t, sink->monitor_source->index);
990     pa_tagstruct_puts(t, sink->monitor_source->name);
991     pa_tagstruct_putu32(t, pa_sink_get_latency(sink));
992 }
993
994 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
995     assert(t && source);
996     pa_tagstruct_putu32(t, source->index);
997     pa_tagstruct_puts(t, source->name);
998     pa_tagstruct_puts(t, source->description ? source->description : "");
999     pa_tagstruct_put_sample_spec(t, &source->sample_spec);
1000     pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
1001     pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
1002     pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : "");
1003 }
1004
1005 static void client_fill_tagstruct(struct pa_tagstruct *t, struct pa_client *client) {
1006     assert(t && client);
1007     pa_tagstruct_putu32(t, client->index);
1008     pa_tagstruct_puts(t, client->name);
1009     pa_tagstruct_puts(t, client->protocol_name);
1010     pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
1011 }
1012
1013 static void module_fill_tagstruct(struct pa_tagstruct *t, struct pa_module *module) {
1014     assert(t && module);
1015     pa_tagstruct_putu32(t, module->index);
1016     pa_tagstruct_puts(t, module->name);
1017     pa_tagstruct_puts(t, module->argument ? module->argument : "");
1018     pa_tagstruct_putu32(t, module->n_used);
1019     pa_tagstruct_put_boolean(t, module->auto_unload);
1020 }
1021
1022 static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_input *s) {
1023     assert(t && s);
1024     pa_tagstruct_putu32(t, s->index);
1025     pa_tagstruct_puts(t, s->name ? s->name : "");
1026     pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1027     pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1028     pa_tagstruct_putu32(t, s->sink->index);
1029     pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1030     pa_tagstruct_putu32(t, s->volume);
1031     pa_tagstruct_putu32(t, pa_sink_input_get_latency(s));
1032     pa_tagstruct_putu32(t, pa_sink_get_latency(s->sink));
1033 }
1034
1035 static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
1036     assert(t && s);
1037     pa_tagstruct_putu32(t, s->index);
1038     pa_tagstruct_puts(t, s->name ? s->name : "");
1039     pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1040     pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1041     pa_tagstruct_putu32(t, s->source->index);
1042     pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1043 }
1044
1045 static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {
1046     assert(t && e);
1047     pa_tagstruct_putu32(t, e->index);
1048     pa_tagstruct_puts(t, e->name);
1049     pa_tagstruct_putu32(t, e->volume);
1050     pa_tagstruct_putu32(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1051     pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1052     pa_tagstruct_putu32(t, e->memchunk.length);
1053 }
1054
1055 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1056     struct connection *c = userdata;
1057     uint32_t index;
1058     struct pa_sink *sink = NULL;
1059     struct pa_source *source = NULL;
1060     struct pa_client *client = NULL;
1061     struct pa_module *module = NULL;
1062     struct pa_sink_input *si = NULL;
1063     struct pa_source_output *so = NULL;
1064     struct pa_scache_entry *sce = NULL;
1065     const char *name;
1066     struct pa_tagstruct *reply;
1067     assert(c && t);
1068
1069     
1070     if (pa_tagstruct_getu32(t, &index) < 0 ||
1071         (command != PA_COMMAND_GET_CLIENT_INFO &&
1072          command != PA_COMMAND_GET_MODULE_INFO &&
1073          command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1074          command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1075          pa_tagstruct_gets(t, &name) < 0) ||
1076         !pa_tagstruct_eof(t)) {
1077         protocol_error(c);
1078         return;
1079     }
1080     
1081     if (!c->authorized) {
1082         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1083         return;
1084     }
1085
1086     if (command == PA_COMMAND_GET_SINK_INFO) {
1087         if (index != (uint32_t) -1)
1088             sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1089         else
1090             sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
1091     } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1092         if (index != (uint32_t) -1)
1093             source = pa_idxset_get_by_index(c->protocol->core->sources, index);
1094         else
1095             source = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SOURCE, 1);
1096     } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1097         client = pa_idxset_get_by_index(c->protocol->core->clients, index);
1098     else if (command == PA_COMMAND_GET_MODULE_INFO) 
1099         module = pa_idxset_get_by_index(c->protocol->core->modules, index);
1100     else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1101         si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1102     else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1103         so = pa_idxset_get_by_index(c->protocol->core->source_outputs, index);
1104     else {
1105         assert(command == PA_COMMAND_GET_SAMPLE_INFO && name);
1106         if (index != (uint32_t) -1)
1107             sce = pa_idxset_get_by_index(c->protocol->core->scache, index);
1108         else
1109             sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1110     }
1111             
1112     if (!sink && !source && !client && !module && !si && !so && !sce) {
1113         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1114         return;
1115     }
1116
1117     reply = pa_tagstruct_new(NULL, 0);
1118     assert(reply);
1119     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1120     pa_tagstruct_putu32(reply, tag); 
1121     if (sink)
1122         sink_fill_tagstruct(reply, sink);
1123     else if (source)
1124         source_fill_tagstruct(reply, source);
1125     else if (client)
1126         client_fill_tagstruct(reply, client);
1127     else if (module)
1128         module_fill_tagstruct(reply, module);
1129     else if (si)
1130         sink_input_fill_tagstruct(reply, si);
1131     else if (so)
1132         source_output_fill_tagstruct(reply, so);
1133     else
1134         scache_fill_tagstruct(reply, sce);
1135     pa_pstream_send_tagstruct(c->pstream, reply);
1136 }
1137
1138 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1139     struct connection *c = userdata;
1140     struct pa_idxset *i;
1141     uint32_t index;
1142     void *p;
1143     struct pa_tagstruct *reply;
1144     assert(c && t);
1145
1146     if (!pa_tagstruct_eof(t)) {
1147         protocol_error(c);
1148         return;
1149     }
1150     
1151     if (!c->authorized) {
1152         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1153         return;
1154     }
1155
1156     reply = pa_tagstruct_new(NULL, 0);
1157     assert(reply);
1158     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1159     pa_tagstruct_putu32(reply, tag);
1160
1161     if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1162         i = c->protocol->core->sinks;
1163     else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1164         i = c->protocol->core->sources;
1165     else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1166         i = c->protocol->core->clients;
1167     else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1168         i = c->protocol->core->modules;
1169     else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1170         i = c->protocol->core->sink_inputs;
1171     else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1172         i = c->protocol->core->source_outputs;
1173     else {
1174         assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1175         i = c->protocol->core->scache;
1176     }
1177
1178     if (i) {
1179         for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1180             if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1181                 sink_fill_tagstruct(reply, p);
1182             else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1183                 source_fill_tagstruct(reply, p);
1184             else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1185                 client_fill_tagstruct(reply, p);
1186             else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1187                 module_fill_tagstruct(reply, p);
1188             else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1189                 sink_input_fill_tagstruct(reply, p);
1190             else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST) 
1191                 source_output_fill_tagstruct(reply, p);
1192             else {
1193                 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1194                 scache_fill_tagstruct(reply, p);
1195             }
1196         }
1197     }
1198     
1199     pa_pstream_send_tagstruct(c->pstream, reply);
1200 }
1201
1202 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1203     struct connection *c = userdata;
1204     struct pa_tagstruct *reply;
1205     char txt[256];
1206     assert(c && t);
1207
1208     if (!pa_tagstruct_eof(t)) {
1209         protocol_error(c);
1210         return;
1211     }
1212     
1213     if (!c->authorized) {
1214         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1215         return;
1216     }
1217
1218     reply = pa_tagstruct_new(NULL, 0);
1219     assert(reply);
1220     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1221     pa_tagstruct_putu32(reply, tag);
1222     pa_tagstruct_puts(reply, PACKAGE_NAME);
1223     pa_tagstruct_puts(reply, PACKAGE_VERSION);
1224     pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1225     pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1226     pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1227     pa_tagstruct_puts(reply, c->protocol->core->default_sink_name ?  c->protocol->core->default_sink_name : "");
1228     pa_tagstruct_puts(reply, c->protocol->core->default_source_name ?  c->protocol->core->default_source_name : "");
1229     pa_pstream_send_tagstruct(c->pstream, reply);
1230 }
1231
1232 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1233     struct pa_tagstruct *t;
1234     struct connection *c = userdata;
1235     assert(c && core);
1236
1237     t = pa_tagstruct_new(NULL, 0);
1238     assert(t);
1239     pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1240     pa_tagstruct_putu32(t, (uint32_t) -1);
1241     pa_tagstruct_putu32(t, e);
1242     pa_tagstruct_putu32(t, index);
1243     pa_pstream_send_tagstruct(c->pstream, t);
1244 }
1245
1246 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1247     struct connection *c = userdata;
1248     enum pa_subscription_mask m;
1249     assert(c && t);
1250
1251     if (pa_tagstruct_getu32(t, &m) < 0 ||
1252         !pa_tagstruct_eof(t)) {
1253         protocol_error(c);
1254         return;
1255     }
1256     
1257     if (!c->authorized) {
1258         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1259         return;
1260     }
1261
1262     if (c->subscription)
1263         pa_subscription_free(c->subscription);
1264
1265     if (m != 0) {
1266         c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1267         assert(c->subscription);
1268     } else
1269         c->subscription = NULL;
1270
1271     pa_pstream_send_simple_ack(c->pstream, tag);
1272 }
1273
1274 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1275     struct connection *c = userdata;
1276     uint32_t index, volume;
1277     struct pa_sink *sink = NULL;
1278     struct pa_sink_input *si = NULL;
1279     const char *name = NULL;
1280     assert(c && t);
1281
1282     if (pa_tagstruct_getu32(t, &index) < 0 ||
1283         (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1284         pa_tagstruct_getu32(t, &volume) ||
1285         !pa_tagstruct_eof(t)) {
1286         protocol_error(c);
1287         return;
1288     }
1289     
1290     if (!c->authorized) {
1291         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1292         return;
1293     }
1294
1295     if (command == PA_COMMAND_SET_SINK_VOLUME) {
1296         if (index != (uint32_t) -1)
1297             sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1298         else
1299             sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
1300     }  else {
1301         assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1302         si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1303     }
1304
1305     if (!si && !sink) {
1306         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1307         return;
1308     }
1309
1310     if (sink)
1311         pa_sink_set_volume(sink, volume);
1312     else if (si)
1313         pa_sink_input_set_volume(si, volume);
1314
1315     pa_pstream_send_simple_ack(c->pstream, tag);
1316 }
1317
1318 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1319     struct connection *c = userdata;
1320     uint32_t index;
1321     uint32_t b;
1322     struct playback_stream *s;
1323     assert(c && t);
1324
1325     if (pa_tagstruct_getu32(t, &index) < 0 ||
1326         pa_tagstruct_getu32(t, &b) < 0 ||
1327         !pa_tagstruct_eof(t)) {
1328         protocol_error(c);
1329         return;
1330     }
1331
1332     if (!c->authorized) {
1333         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1334         return;
1335     }
1336
1337     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1338         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1339         return;
1340     }
1341
1342     pa_sink_input_cork(s->sink_input, b);
1343     pa_pstream_send_simple_ack(c->pstream, tag);
1344 }
1345
1346 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1347     struct connection *c = userdata;
1348     uint32_t index;
1349     struct playback_stream *s;
1350     assert(c && t);
1351
1352     if (pa_tagstruct_getu32(t, &index) < 0 ||
1353         !pa_tagstruct_eof(t)) {
1354         protocol_error(c);
1355         return;
1356     }
1357
1358     if (!c->authorized) {
1359         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1360         return;
1361     }
1362
1363     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1364         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1365         return;
1366     }
1367
1368     if (command == PA_COMMAND_TRIGGER_PLAYBACK_STREAM)
1369         pa_memblockq_prebuf_disable(s->memblockq);
1370     else {
1371         assert(command == PA_COMMAND_FLUSH_PLAYBACK_STREAM);
1372         pa_memblockq_flush(s->memblockq);
1373         /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1374     }
1375
1376     pa_sink_notify(s->sink_input->sink);
1377     pa_pstream_send_simple_ack(c->pstream, tag);
1378     request_bytes(s);
1379 }
1380
1381 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1382     struct connection *c = userdata;
1383     uint32_t index;
1384     const char *s;
1385     assert(c && t);
1386
1387     if (pa_tagstruct_getu32(t, &index) < 0 ||
1388         pa_tagstruct_gets(t, &s) < 0 ||
1389         !pa_tagstruct_eof(t)) {
1390         protocol_error(c);
1391         return;
1392     }
1393
1394     if (!c->authorized) {
1395         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1396         return;
1397     }
1398
1399     pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1400     pa_pstream_send_simple_ack(c->pstream, tag);
1401 }
1402
1403 /*** pstream callbacks ***/
1404
1405 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1406     struct connection *c = userdata;
1407     assert(p && packet && packet->data && c);
1408
1409     if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1410         pa_log(__FILE__": invalid packet.\n");
1411         connection_free(c);
1412     }
1413 }
1414
1415 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1416     struct connection *c = userdata;
1417     struct output_stream *stream;
1418     assert(p && chunk && userdata);
1419
1420     if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1421         pa_log(__FILE__": client sent block for invalid stream.\n");
1422         connection_free(c);
1423         return;
1424     }
1425
1426     if (stream->type == PLAYBACK_STREAM) {
1427         struct playback_stream *p = (struct playback_stream*) stream;
1428         if (chunk->length >= p->requested_bytes)
1429             p->requested_bytes = 0;
1430         else
1431             p->requested_bytes -= chunk->length;
1432         
1433         pa_memblockq_push_align(p->memblockq, chunk, delta);
1434         assert(p->sink_input);
1435         /*pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq));*/
1436
1437         pa_sink_notify(p->sink_input->sink);
1438         /*pa_log(__FILE__": Recieved %u bytes.\n", chunk->length);*/
1439
1440     } else {
1441         struct upload_stream *u = (struct upload_stream*) stream;
1442         size_t l;
1443         assert(u->type == UPLOAD_STREAM);
1444
1445         if (!u->memchunk.memblock) {
1446             if (u->length == chunk->length) {
1447                 u->memchunk = *chunk;
1448                 pa_memblock_ref(u->memchunk.memblock);
1449                 u->length = 0;
1450             } else {
1451                 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1452                 u->memchunk.index = u->memchunk.length = 0;
1453             }
1454         }
1455         
1456         assert(u->memchunk.memblock);
1457         
1458         l = u->length; 
1459         if (l > chunk->length)
1460             l = chunk->length;
1461
1462         if (l > 0) {
1463             memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
1464                    (uint8_t*) chunk->memblock->data+chunk->index, l);
1465             u->memchunk.length += l;
1466             u->length -= l;
1467         }
1468     }
1469 }
1470
1471 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1472     struct connection *c = userdata;
1473     assert(p && c);
1474     connection_free(c);
1475
1476 /*    pa_log(__FILE__": connection died.\n");*/
1477 }
1478
1479
1480 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1481     struct connection *c = userdata;
1482     assert(p && c);
1483
1484     send_memblock(c);
1485 }
1486
1487 /*** client callbacks ***/
1488
1489 static void client_kill_cb(struct pa_client *c) {
1490     assert(c && c->userdata);
1491     connection_free(c->userdata);
1492 }
1493
1494 /*** socket server callbacks ***/
1495
1496 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1497     struct pa_protocol_native *p = userdata;
1498     struct connection *c;
1499     assert(io && p);
1500
1501     c = pa_xmalloc(sizeof(struct connection));
1502     c->authorized = p->public;
1503     c->protocol = p;
1504     assert(p->core);
1505     c->client = pa_client_new(p->core, "NATIVE", "Client");
1506     assert(c->client);
1507     c->client->kill = client_kill_cb;
1508     c->client->userdata = c;
1509     c->client->owner = p->module;
1510     
1511     c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
1512     assert(c->pstream);
1513
1514     pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1515     pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1516     pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1517     pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1518
1519     c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1520     assert(c->pdispatch);
1521
1522     c->record_streams = pa_idxset_new(NULL, NULL);
1523     c->output_streams = pa_idxset_new(NULL, NULL);
1524     assert(c->record_streams && c->output_streams);
1525
1526     c->rrobin_index = PA_IDXSET_INVALID;
1527     c->subscription = NULL;
1528
1529     pa_idxset_put(p->connections, c, NULL);
1530 }
1531
1532 /*** module entry points ***/
1533
1534 static struct pa_protocol_native* protocol_new_internal(struct pa_core *c, struct pa_module *m, struct pa_modargs *ma) {
1535     struct pa_protocol_native *p;
1536     int public;
1537     assert(c && ma);
1538
1539     if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
1540         pa_log(__FILE__": public= expects numeric argument.\n");
1541         return NULL;
1542     }
1543     
1544     p = pa_xmalloc(sizeof(struct pa_protocol_native));
1545
1546     if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1547         pa_xfree(p);
1548         return NULL;
1549     }
1550
1551     p->module = m;
1552     p->public = public;
1553     p->server = NULL;
1554     p->core = c;
1555     p->connections = pa_idxset_new(NULL, NULL);
1556     assert(p->connections);
1557
1558     return p;
1559 }
1560
1561 struct pa_protocol_native* pa_protocol_native_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
1562     struct pa_protocol_native *p;
1563
1564     if (!(p = protocol_new_internal(core, m, ma)))
1565         return NULL;
1566     
1567     p->server = server;
1568     pa_socket_server_set_callback(p->server, on_connection, p);
1569     
1570     return p;
1571 }
1572
1573 void pa_protocol_native_free(struct pa_protocol_native *p) {
1574     struct connection *c;
1575     assert(p);
1576
1577     while ((c = pa_idxset_first(p->connections, NULL)))
1578         connection_free(c);
1579     pa_idxset_free(p->connections, NULL, NULL);
1580
1581     if (p->server)
1582         pa_socket_server_unref(p->server);
1583     
1584     pa_xfree(p);
1585 }
1586
1587 struct pa_protocol_native* pa_protocol_native_new_iochannel(struct pa_core*core, struct pa_iochannel *io, struct pa_module *m, struct pa_modargs *ma) {
1588     struct pa_protocol_native *p;
1589
1590     if (!(p = protocol_new_internal(core, m, ma)))
1591         return NULL;
1592
1593     on_connection(NULL, io, p);
1594     
1595     return p;
1596 }