remove obnoxious assert from module-combine
[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 #include "autoload.h"
49
50 struct connection;
51 struct pa_protocol_native;
52
53 struct record_stream {
54     struct connection *connection;
55     uint32_t index;
56     struct pa_source_output *source_output;
57     struct pa_memblockq *memblockq;
58     size_t fragment_size;
59 };
60
61 struct playback_stream {
62     int type;
63     struct connection *connection;
64     uint32_t index;
65     struct pa_sink_input *sink_input;
66     struct pa_memblockq *memblockq;
67     size_t requested_bytes;
68     int drain_request;
69     uint32_t drain_tag;
70 };
71
72 struct upload_stream {
73     int type;
74     struct connection *connection;
75     uint32_t index;
76     struct pa_memchunk memchunk;
77     size_t length;
78     char *name;
79     struct pa_sample_spec sample_spec;
80 };
81
82 struct output_stream {
83     int type;
84 };
85
86 enum {
87     UPLOAD_STREAM,
88     PLAYBACK_STREAM
89 };
90
91 struct connection {
92     int authorized;
93     struct pa_protocol_native *protocol;
94     struct pa_client *client;
95     struct pa_pstream *pstream;
96     struct pa_pdispatch *pdispatch;
97     struct pa_idxset *record_streams, *output_streams;
98     uint32_t rrobin_index;
99     struct pa_subscription *subscription;
100 };
101
102 struct pa_protocol_native {
103     struct pa_module *module;
104     int public;
105     struct pa_core *core;
106     struct pa_socket_server *server;
107     struct pa_idxset *connections;
108     uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
109 };
110
111 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
112 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length);
113 static void sink_input_kill_cb(struct pa_sink_input *i);
114 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i);
115
116 static void request_bytes(struct playback_stream*s);
117
118 static void source_output_kill_cb(struct pa_source_output *o);
119 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
120 static pa_usec_t source_output_get_latency_cb(struct pa_source_output *o);
121
122 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_set_client_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_get_record_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
135 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
136 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
137 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
138 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
139 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
140 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
141 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
142 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
143 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
144 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
145 static void command_set_stream_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
146 static void command_kill(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
147 static void command_load_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
148 static void command_unload_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
149 static void command_add_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
150 static void command_remove_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
151 static void command_get_autoload_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
152 static void command_get_autoload_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
153
154 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
155     [PA_COMMAND_ERROR] = { NULL },
156     [PA_COMMAND_TIMEOUT] = { NULL },
157     [PA_COMMAND_REPLY] = { NULL },
158     [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
159     [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
160     [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
161     [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
162     [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
163     [PA_COMMAND_AUTH] = { command_auth },
164     [PA_COMMAND_REQUEST] = { NULL },
165     [PA_COMMAND_EXIT] = { command_exit },
166     [PA_COMMAND_SET_CLIENT_NAME] = { command_set_client_name },
167     [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
168     [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
169     [PA_COMMAND_STAT] = { command_stat },
170     [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
171     [PA_COMMAND_GET_RECORD_LATENCY] = { command_get_record_latency },
172     [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
173     [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
174     [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
175     [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
176     [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
177     [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
178     [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
179     [PA_COMMAND_GET_CLIENT_INFO] = { command_get_info },
180     [PA_COMMAND_GET_MODULE_INFO] = { command_get_info },
181     [PA_COMMAND_GET_SINK_INPUT_INFO] = { command_get_info },
182     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = { command_get_info },
183     [PA_COMMAND_GET_SAMPLE_INFO] = { command_get_info },
184     [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
185     [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
186     [PA_COMMAND_GET_MODULE_INFO_LIST] = { command_get_info_list },
187     [PA_COMMAND_GET_CLIENT_INFO_LIST] = { command_get_info_list },
188     [PA_COMMAND_GET_SINK_INPUT_INFO_LIST] = { command_get_info_list },
189     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = { command_get_info_list },
190     [PA_COMMAND_GET_SAMPLE_INFO_LIST] = { command_get_info_list },
191     [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
192     [PA_COMMAND_SUBSCRIBE] = { command_subscribe },
193     [PA_COMMAND_SET_SINK_VOLUME] = { command_set_volume },
194     [PA_COMMAND_SET_SINK_INPUT_VOLUME] = { command_set_volume },
195     [PA_COMMAND_CORK_PLAYBACK_STREAM] = { command_cork_playback_stream },
196     [PA_COMMAND_FLUSH_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
197     [PA_COMMAND_TRIGGER_PLAYBACK_STREAM] = { command_flush_or_trigger_playback_stream },
198     [PA_COMMAND_SET_DEFAULT_SINK] = { command_set_default_sink_or_source },
199     [PA_COMMAND_SET_DEFAULT_SOURCE] = { command_set_default_sink_or_source },
200     [PA_COMMAND_SET_PLAYBACK_STREAM_NAME] = { command_set_stream_name }, 
201     [PA_COMMAND_SET_RECORD_STREAM_NAME] = { command_set_stream_name },
202     [PA_COMMAND_KILL_CLIENT] = { command_kill },
203     [PA_COMMAND_KILL_SINK_INPUT] = { command_kill },
204     [PA_COMMAND_KILL_SOURCE_OUTPUT] = { command_kill },
205     [PA_COMMAND_LOAD_MODULE] = { command_load_module },
206     [PA_COMMAND_UNLOAD_MODULE] = { command_unload_module },
207     [PA_COMMAND_GET_AUTOLOAD_INFO] = { command_get_autoload_info },
208     [PA_COMMAND_GET_AUTOLOAD_INFO_LIST] = { command_get_autoload_info_list },
209     [PA_COMMAND_ADD_AUTOLOAD] = { command_add_autoload },
210     [PA_COMMAND_REMOVE_AUTOLOAD] = { command_remove_autoload },
211 };
212
213 /* structure management */
214
215 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
216     struct upload_stream *s;
217     assert(c && ss && name && length);
218     
219     s = pa_xmalloc(sizeof(struct upload_stream));
220     s->type = UPLOAD_STREAM;
221     s->connection = c;
222     s->sample_spec = *ss;
223     s->name = pa_xstrdup(name);
224
225     s->memchunk.memblock = NULL;
226     s->memchunk.index = 0;
227     s->memchunk.length = 0;
228
229     s->length = length;
230     
231     pa_idxset_put(c->output_streams, s, &s->index);
232     return s;
233 }
234
235 static void upload_stream_free(struct upload_stream *o) {
236     assert(o && o->connection);
237
238     pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
239
240     pa_xfree(o->name);
241     
242     if (o->memchunk.memblock)
243         pa_memblock_unref(o->memchunk.memblock);
244     
245     pa_xfree(o);
246 }
247
248 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) {
249     struct record_stream *s;
250     struct pa_source_output *source_output;
251     size_t base;
252     assert(c && source && ss && name && maxlength);
253
254     if (!(source_output = pa_source_output_new(source, name, ss, -1)))
255         return NULL;
256
257     s = pa_xmalloc(sizeof(struct record_stream));
258     s->connection = c;
259     s->source_output = source_output;
260     s->source_output->push = source_output_push_cb;
261     s->source_output->kill = source_output_kill_cb;
262     s->source_output->get_latency = source_output_get_latency_cb;
263     s->source_output->userdata = s;
264     s->source_output->owner = c->protocol->module;
265     s->source_output->client = c->client;
266
267     s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0, c->protocol->core->memblock_stat);
268     assert(s->memblockq);
269
270     s->fragment_size = (fragment_size/base)*base;
271     if (!s->fragment_size)
272         s->fragment_size = base;
273
274     pa_idxset_put(c->record_streams, s, &s->index);
275     return s;
276 }
277
278 static void record_stream_free(struct record_stream* r) {
279     assert(r && r->connection);
280
281     pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
282     pa_source_output_disconnect(r->source_output);
283     pa_source_output_unref(r->source_output);
284     pa_memblockq_free(r->memblockq);
285     pa_xfree(r);
286 }
287
288 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
289                                                    size_t maxlength,
290                                                    size_t tlength,
291                                                    size_t prebuf,
292                                                    size_t minreq,
293                                                    pa_volume_t volume) {
294     struct playback_stream *s;
295     struct pa_sink_input *sink_input;
296     assert(c && sink && ss && name && maxlength);
297
298     if (!(sink_input = pa_sink_input_new(sink, name, ss, 0, -1)))
299         return NULL;
300     
301     s = pa_xmalloc(sizeof(struct playback_stream));
302     s->type = PLAYBACK_STREAM;
303     s->connection = c;
304     s->sink_input = sink_input;
305     
306     s->sink_input->peek = sink_input_peek_cb;
307     s->sink_input->drop = sink_input_drop_cb;
308     s->sink_input->kill = sink_input_kill_cb;
309     s->sink_input->get_latency = sink_input_get_latency_cb;
310     s->sink_input->userdata = s;
311     s->sink_input->owner = c->protocol->module;
312     s->sink_input->client = c->client;
313     
314     s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq, c->protocol->core->memblock_stat);
315     assert(s->memblockq);
316
317     s->requested_bytes = 0;
318     s->drain_request = 0;
319
320     s->sink_input->volume = volume;
321     
322     pa_idxset_put(c->output_streams, s, &s->index);
323     return s;
324 }
325
326 static void playback_stream_free(struct playback_stream* p) {
327     assert(p && p->connection);
328
329     if (p->drain_request)
330         pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
331
332     pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
333     pa_sink_input_disconnect(p->sink_input);
334     pa_sink_input_unref(p->sink_input);
335     pa_memblockq_free(p->memblockq);
336     pa_xfree(p);
337 }
338
339 static void connection_free(struct connection *c) {
340     struct record_stream *r;
341     struct output_stream *o;
342     assert(c && c->protocol);
343
344     pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
345     while ((r = pa_idxset_first(c->record_streams, NULL)))
346         record_stream_free(r);
347     pa_idxset_free(c->record_streams, NULL, NULL);
348
349     while ((o = pa_idxset_first(c->output_streams, NULL)))
350         if (o->type == PLAYBACK_STREAM)
351             playback_stream_free((struct playback_stream*) o);
352         else
353             upload_stream_free((struct upload_stream*) o);
354     pa_idxset_free(c->output_streams, NULL, NULL);
355
356     pa_pdispatch_unref(c->pdispatch);
357     pa_pstream_close(c->pstream);
358     pa_pstream_unref(c->pstream);
359     pa_client_free(c->client);
360
361     if (c->subscription)
362         pa_subscription_free(c->subscription);
363     
364     pa_xfree(c);
365 }
366
367 static void request_bytes(struct playback_stream *s) {
368     struct pa_tagstruct *t;
369     size_t l;
370     assert(s);
371
372     if (!(l = pa_memblockq_missing(s->memblockq)))
373         return;
374     
375     if (l <= s->requested_bytes)
376         return;
377
378     l -= s->requested_bytes;
379
380     if (l < pa_memblockq_get_minreq(s->memblockq))
381         return;
382     
383     s->requested_bytes += l;
384
385     t = pa_tagstruct_new(NULL, 0);
386     assert(t);
387     pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
388     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
389     pa_tagstruct_putu32(t, s->index);
390     pa_tagstruct_putu32(t, l);
391     pa_pstream_send_tagstruct(s->connection->pstream, t);
392
393 /*     pa_log(__FILE__": Requesting %u bytes\n", l); */
394 }
395
396 static void send_memblock(struct connection *c) {
397     uint32_t start;
398     struct record_stream *r;
399
400     start = PA_IDXSET_INVALID;
401     for (;;) {
402         struct pa_memchunk chunk;
403         
404         if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
405             return;
406
407         if (start == PA_IDXSET_INVALID)
408             start = c->rrobin_index;
409         else if (start == c->rrobin_index)
410             return;
411
412         if (pa_memblockq_peek(r->memblockq,  &chunk) >= 0) {
413             struct pa_memchunk schunk = chunk;
414             
415             if (schunk.length > r->fragment_size)
416                 schunk.length = r->fragment_size;
417
418             pa_pstream_send_memblock(c->pstream, r->index, 0, &schunk);
419             pa_memblockq_drop(r->memblockq, &chunk, schunk.length);
420             pa_memblock_unref(schunk.memblock);
421             
422             return;
423         }
424     }
425 }
426
427 static void send_playback_stream_killed(struct playback_stream *p) {
428     struct pa_tagstruct *t;
429     assert(p);
430
431     t = pa_tagstruct_new(NULL, 0);
432     assert(t);
433     pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
434     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
435     pa_tagstruct_putu32(t, p->index);
436     pa_pstream_send_tagstruct(p->connection->pstream, t);
437 }
438
439 static void send_record_stream_killed(struct record_stream *r) {
440     struct pa_tagstruct *t;
441     assert(r);
442
443     t = pa_tagstruct_new(NULL, 0);
444     assert(t);
445     pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
446     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
447     pa_tagstruct_putu32(t, r->index);
448     pa_pstream_send_tagstruct(r->connection->pstream, t);
449 }
450
451 /*** sinkinput callbacks ***/
452
453 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
454     struct playback_stream *s;
455     assert(i && i->userdata && chunk);
456     s = i->userdata;
457
458     if (pa_memblockq_peek(s->memblockq, chunk) < 0)
459         return -1;
460
461     return 0;
462 }
463
464 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
465     struct playback_stream *s;
466     assert(i && i->userdata && length);
467     s = i->userdata;
468
469     pa_memblockq_drop(s->memblockq, chunk, length);
470     request_bytes(s);
471
472     if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
473         pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
474         s->drain_request = 0;
475     }
476
477     /*pa_log(__FILE__": after_drop: %u\n", pa_memblockq_get_length(s->memblockq));*/
478 }
479
480 static void sink_input_kill_cb(struct pa_sink_input *i) {
481     assert(i && i->userdata);
482     send_playback_stream_killed((struct playback_stream *) i->userdata);
483     playback_stream_free((struct playback_stream *) i->userdata);
484 }
485
486 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i) {
487     struct playback_stream *s;
488     assert(i && i->userdata);
489     s = i->userdata;
490
491     /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
492     
493     return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
494 }
495
496 /*** source_output callbacks ***/
497
498 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
499     struct record_stream *s;
500     assert(o && o->userdata && chunk);
501     s = o->userdata;
502     
503     pa_memblockq_push_align(s->memblockq, chunk, 0);
504     if (!pa_pstream_is_pending(s->connection->pstream))
505         send_memblock(s->connection);
506 }
507
508 static void source_output_kill_cb(struct pa_source_output *o) {
509     assert(o && o->userdata);
510     send_record_stream_killed((struct record_stream *) o->userdata);
511     record_stream_free((struct record_stream *) o->userdata);
512 }
513
514 static pa_usec_t source_output_get_latency_cb(struct pa_source_output *o) {
515     struct record_stream *s;
516     assert(o && o->userdata);
517     s = o->userdata;
518
519     /*pa_log(__FILE__": get_latency: %u\n", pa_memblockq_get_length(s->memblockq));*/
520     
521     return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &o->sample_spec);
522 }
523
524 /*** pdispatch callbacks ***/
525
526 static void protocol_error(struct connection *c) {
527     pa_log(__FILE__": protocol error, kicking client\n");
528     connection_free(c);
529 }
530
531 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
532     struct connection *c = userdata;
533     struct playback_stream *s;
534     size_t maxlength, tlength, prebuf, minreq;
535     uint32_t sink_index;
536     const char *name, *sink_name;
537     struct pa_sample_spec ss;
538     struct pa_tagstruct *reply;
539     struct pa_sink *sink;
540     pa_volume_t volume;
541     assert(c && t && c->protocol && c->protocol->core);
542     
543     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
544         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
545         pa_tagstruct_getu32(t, &sink_index) < 0 ||
546         pa_tagstruct_gets(t, &sink_name) < 0 ||
547         pa_tagstruct_getu32(t, &maxlength) < 0 ||
548         pa_tagstruct_getu32(t, &tlength) < 0 ||
549         pa_tagstruct_getu32(t, &prebuf) < 0 ||
550         pa_tagstruct_getu32(t, &minreq) < 0 ||
551         pa_tagstruct_getu32(t, &volume) < 0 ||
552         !pa_tagstruct_eof(t)) {
553         protocol_error(c);
554         return;
555     }
556
557
558     if (!c->authorized) {
559         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
560         return;
561     }
562
563     if (sink_index != (uint32_t) -1)
564         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
565     else
566         sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
567
568     if (!sink) {
569         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
570         return;
571     }
572     
573     if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq, volume))) {
574         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
575         return;
576     }
577     
578     reply = pa_tagstruct_new(NULL, 0);
579     assert(reply);
580     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
581     pa_tagstruct_putu32(reply, tag);
582     pa_tagstruct_putu32(reply, s->index);
583     assert(s->sink_input);
584     pa_tagstruct_putu32(reply, s->sink_input->index);
585     pa_tagstruct_putu32(reply, s->requested_bytes = pa_memblockq_missing(s->memblockq));
586     pa_pstream_send_tagstruct(c->pstream, reply);
587     request_bytes(s);
588 }
589
590 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
591     struct connection *c = userdata;
592     uint32_t channel;
593     assert(c && t);
594     
595     if (pa_tagstruct_getu32(t, &channel) < 0 ||
596         !pa_tagstruct_eof(t)) {
597         protocol_error(c);
598         return;
599     }
600
601     if (!c->authorized) {
602         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
603         return;
604     }
605
606     if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
607         struct playback_stream *s;
608         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
609             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
610             return;
611         }
612
613         playback_stream_free(s);
614     } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
615         struct record_stream *s;
616         if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
617             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
618             return;
619         }
620
621         record_stream_free(s);
622     } else {
623         struct upload_stream *s;
624         assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
625         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
626             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
627             return;
628         }
629
630         upload_stream_free(s);
631     }
632             
633     pa_pstream_send_simple_ack(c->pstream, tag);
634 }
635
636 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
637     struct connection *c = userdata;
638     struct record_stream *s;
639     size_t maxlength, fragment_size;
640     uint32_t source_index;
641     const char *name, *source_name;
642     struct pa_sample_spec ss;
643     struct pa_tagstruct *reply;
644     struct pa_source *source;
645     assert(c && t && c->protocol && c->protocol->core);
646     
647     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
648         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
649         pa_tagstruct_getu32(t, &source_index) < 0 ||
650         pa_tagstruct_gets(t, &source_name) < 0 ||
651         pa_tagstruct_getu32(t, &maxlength) < 0 ||
652         pa_tagstruct_getu32(t, &fragment_size) < 0 ||
653         !pa_tagstruct_eof(t)) {
654         protocol_error(c);
655         return;
656     }
657
658     if (!c->authorized) {
659         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
660         return;
661     }
662
663     if (source_index != (uint32_t) -1)
664         source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
665     else
666         source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE, 1);
667
668     if (!source) {
669         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
670         return;
671     }
672     
673     if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
674         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
675         return;
676     }
677     
678     reply = pa_tagstruct_new(NULL, 0);
679     assert(reply);
680     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
681     pa_tagstruct_putu32(reply, tag);
682     pa_tagstruct_putu32(reply, s->index);
683     assert(s->source_output);
684     pa_tagstruct_putu32(reply, s->source_output->index);
685     pa_pstream_send_tagstruct(c->pstream, reply);
686 }
687
688 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
689     struct connection *c = userdata;
690     assert(c && t);
691     
692     if (!pa_tagstruct_eof(t)) {
693         protocol_error(c);
694         return;
695     }
696
697     if (!c->authorized) {
698         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
699         return;
700     }
701     
702     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
703     c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
704     pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
705     return;
706 }
707
708 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
709     struct connection *c = userdata;
710     const void*cookie;
711     assert(c && t);
712
713     if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
714         !pa_tagstruct_eof(t)) {
715         protocol_error(c);
716         return;
717     }
718
719     if (!c->authorized) {
720         if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
721             pa_log(__FILE__": Denied access to client with invalid authorization key.\n");
722             pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
723             return;
724         }
725         
726         c->authorized = 1;
727     }
728     
729     pa_pstream_send_simple_ack(c->pstream, tag);
730     return;
731 }
732
733 static void command_set_client_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
734     struct connection *c = userdata;
735     const char *name;
736     assert(c && t);
737
738     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
739         !pa_tagstruct_eof(t)) {
740         protocol_error(c);
741         return;
742     }
743
744     pa_client_set_name(c->client, name);
745     pa_pstream_send_simple_ack(c->pstream, tag);
746     return;
747 }
748
749 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
750     struct connection *c = userdata;
751     const char *name;
752     uint32_t index = PA_IDXSET_INVALID;
753     assert(c && t);
754
755     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
756         !pa_tagstruct_eof(t)) {
757         protocol_error(c);
758         return;
759     }
760
761     if (!c->authorized) {
762         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
763         return;
764     }
765
766     if (command == PA_COMMAND_LOOKUP_SINK) {
767         struct pa_sink *sink;
768         if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
769             index = sink->index;
770     } else {
771         struct pa_source *source;
772         assert(command == PA_COMMAND_LOOKUP_SOURCE);
773         if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
774             index = source->index;
775     }
776
777     if (index == PA_IDXSET_INVALID)
778         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
779     else {
780         struct pa_tagstruct *reply;
781         reply = pa_tagstruct_new(NULL, 0);
782         assert(reply);
783         pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
784         pa_tagstruct_putu32(reply, tag);
785         pa_tagstruct_putu32(reply, index);
786         pa_pstream_send_tagstruct(c->pstream, reply);
787     }
788 }
789
790 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
791     struct connection *c = userdata;
792     uint32_t index;
793     struct playback_stream *s;
794     assert(c && t);
795
796     if (pa_tagstruct_getu32(t, &index) < 0 ||
797         !pa_tagstruct_eof(t)) {
798         protocol_error(c);
799         return;
800     }
801
802     if (!c->authorized) {
803         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
804         return;
805     }
806
807     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
808         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
809         return;
810     }
811
812     s->drain_request = 0;
813
814     pa_memblockq_prebuf_disable(s->memblockq);
815     
816     if (!pa_memblockq_is_readable(s->memblockq)) {
817         pa_pstream_send_simple_ack(c->pstream, tag);
818     } else {
819         s->drain_request = 1;
820         s->drain_tag = tag;
821
822         pa_sink_notify(s->sink_input->sink);
823     }
824
825
826 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
827     struct connection *c = userdata;
828     struct pa_tagstruct *reply;
829     assert(c && t);
830
831     if (!pa_tagstruct_eof(t)) {
832         protocol_error(c);
833         return;
834     }
835
836     if (!c->authorized) {
837         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
838         return;
839     }
840
841     reply = pa_tagstruct_new(NULL, 0);
842     assert(reply);
843     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
844     pa_tagstruct_putu32(reply, tag);
845     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total);
846     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->total_size);
847     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated);
848     pa_tagstruct_putu32(reply, c->protocol->core->memblock_stat->allocated_size);
849     pa_tagstruct_putu32(reply, pa_scache_total_size(c->protocol->core));
850     pa_pstream_send_tagstruct(c->pstream, reply);
851 }
852
853 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
854     struct connection *c = userdata;
855     struct pa_tagstruct *reply;
856     struct playback_stream *s;
857     struct timeval tv, now;
858     uint32_t index;
859     assert(c && t);
860     
861     if (pa_tagstruct_getu32(t, &index) < 0 ||
862         pa_tagstruct_get_timeval(t, &tv) < 0 ||
863         !pa_tagstruct_eof(t)) {
864         protocol_error(c);
865         return;
866     }
867
868     if (!c->authorized) {
869         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
870         return;
871     }
872
873     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
874         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
875         return;
876     }
877
878     reply = pa_tagstruct_new(NULL, 0);
879     assert(reply);
880     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
881     pa_tagstruct_putu32(reply, tag);
882     pa_tagstruct_put_usec(reply, pa_sink_input_get_latency(s->sink_input));
883     pa_tagstruct_put_usec(reply, pa_sink_get_latency(s->sink_input->sink));
884     pa_tagstruct_put_usec(reply, 0);
885     pa_tagstruct_put_boolean(reply, pa_memblockq_is_readable(s->memblockq));
886     pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
887     pa_tagstruct_put_timeval(reply, &tv);
888     gettimeofday(&now, NULL);
889     pa_tagstruct_put_timeval(reply, &now);
890     pa_pstream_send_tagstruct(c->pstream, reply);
891 }
892
893 static void command_get_record_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
894     struct connection *c = userdata;
895     struct pa_tagstruct *reply;
896     struct record_stream *s;
897     struct timeval tv, now;
898     uint32_t index;
899     assert(c && t);
900
901     if (pa_tagstruct_getu32(t, &index) < 0 ||
902         pa_tagstruct_get_timeval(t, &tv) < 0 ||
903         !pa_tagstruct_eof(t)) {
904         protocol_error(c);
905         return;
906     }
907
908     if (!c->authorized) {
909         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
910         return;
911     }
912
913     if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
914         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
915         return;
916     }
917
918     reply = pa_tagstruct_new(NULL, 0);
919     assert(reply);
920     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
921     pa_tagstruct_putu32(reply, tag);
922     pa_tagstruct_put_usec(reply, pa_source_output_get_latency(s->source_output));
923     pa_tagstruct_put_usec(reply, s->source_output->source->monitor_of ? pa_sink_get_latency(s->source_output->source->monitor_of) : 0);
924     pa_tagstruct_put_usec(reply, pa_source_get_latency(s->source_output->source));
925     pa_tagstruct_put_boolean(reply, 0);
926     pa_tagstruct_putu32(reply, pa_memblockq_get_length(s->memblockq));
927     pa_tagstruct_put_timeval(reply, &tv);
928     gettimeofday(&now, NULL);
929     pa_tagstruct_put_timeval(reply, &now);
930     pa_pstream_send_tagstruct(c->pstream, reply);
931 }
932
933
934 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
935     struct connection *c = userdata;
936     struct upload_stream *s;
937     size_t length;
938     const char *name;
939     struct pa_sample_spec ss;
940     struct pa_tagstruct *reply;
941     assert(c && t && c->protocol && c->protocol->core);
942     
943     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
944         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
945         pa_tagstruct_getu32(t, &length) < 0 ||
946         !pa_tagstruct_eof(t)) {
947         protocol_error(c);
948         return;
949     }
950
951     if (!c->authorized) {
952         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
953         return;
954     }
955
956     if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
957         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
958         return;
959     }
960     
961     if (!(s = upload_stream_new(c, &ss, name, length))) {
962         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
963         return;
964     }
965     
966     reply = pa_tagstruct_new(NULL, 0);
967     assert(reply);
968     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
969     pa_tagstruct_putu32(reply, tag);
970     pa_tagstruct_putu32(reply, s->index);
971     pa_tagstruct_putu32(reply, length);
972     pa_pstream_send_tagstruct(c->pstream, reply);
973 }
974
975 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
976     struct connection *c = userdata;
977     uint32_t channel;
978     struct upload_stream *s;
979     uint32_t index;
980     assert(c && t);
981     
982     if (pa_tagstruct_getu32(t, &channel) < 0 ||
983         !pa_tagstruct_eof(t)) {
984         protocol_error(c);
985         return;
986     }
987
988     if (!c->authorized) {
989         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
990         return;
991     }
992
993     if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
994         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
995         return;
996     }
997
998     pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
999     pa_pstream_send_simple_ack(c->pstream, tag);
1000     upload_stream_free(s);
1001 }
1002
1003 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1004     struct connection *c = userdata;
1005     uint32_t sink_index, volume;
1006     struct pa_sink *sink;
1007     const char *name, *sink_name;
1008     assert(c && t);
1009
1010     if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
1011         pa_tagstruct_gets(t, &sink_name) < 0 ||
1012         pa_tagstruct_getu32(t, &volume) < 0 ||
1013         pa_tagstruct_gets(t, &name) < 0 || !name || 
1014         !pa_tagstruct_eof(t)) {
1015         protocol_error(c);
1016         return;
1017     }
1018     
1019     if (!c->authorized) {
1020         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1021         return;
1022     }
1023
1024     if (sink_index != (uint32_t) -1)
1025         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
1026     else
1027         sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK, 1);
1028
1029     if (!sink) {
1030         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1031         return;
1032     }
1033
1034     if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
1035         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1036         return;
1037     }
1038
1039     pa_pstream_send_simple_ack(c->pstream, tag);
1040 }
1041
1042 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1043     struct connection *c = userdata;
1044     const char *name;
1045     assert(c && t);
1046
1047     if (pa_tagstruct_gets(t, &name) < 0 || !name || 
1048         !pa_tagstruct_eof(t)) {
1049         protocol_error(c);
1050         return;
1051     }
1052
1053     if (!c->authorized) {
1054         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1055         return;
1056     }
1057
1058     if (pa_scache_remove_item(c->protocol->core, name) < 0) {
1059         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1060         return;
1061     }
1062
1063     pa_pstream_send_simple_ack(c->pstream, tag);
1064 }
1065
1066 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
1067     assert(t && sink);
1068     pa_tagstruct_putu32(t, sink->index);
1069     pa_tagstruct_puts(t, sink->name);
1070     pa_tagstruct_puts(t, sink->description);
1071     pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
1072     pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
1073     pa_tagstruct_putu32(t, sink->volume);
1074     pa_tagstruct_putu32(t, sink->monitor_source->index);
1075     pa_tagstruct_puts(t, sink->monitor_source->name);
1076     pa_tagstruct_put_usec(t, pa_sink_get_latency(sink));
1077 }
1078
1079 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
1080     assert(t && source);
1081     pa_tagstruct_putu32(t, source->index);
1082     pa_tagstruct_puts(t, source->name);
1083     pa_tagstruct_puts(t, source->description);
1084     pa_tagstruct_put_sample_spec(t, &source->sample_spec);
1085     pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
1086     pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
1087     pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : NULL);
1088     pa_tagstruct_put_usec(t, pa_source_get_latency(source));
1089 }
1090
1091 static void client_fill_tagstruct(struct pa_tagstruct *t, struct pa_client *client) {
1092     assert(t && client);
1093     pa_tagstruct_putu32(t, client->index);
1094     pa_tagstruct_puts(t, client->name);
1095     pa_tagstruct_puts(t, client->protocol_name);
1096     pa_tagstruct_putu32(t, client->owner ? client->owner->index : (uint32_t) -1);
1097 }
1098
1099 static void module_fill_tagstruct(struct pa_tagstruct *t, struct pa_module *module) {
1100     assert(t && module);
1101     pa_tagstruct_putu32(t, module->index);
1102     pa_tagstruct_puts(t, module->name);
1103     pa_tagstruct_puts(t, module->argument);
1104     pa_tagstruct_putu32(t, module->n_used);
1105     pa_tagstruct_put_boolean(t, module->auto_unload);
1106 }
1107
1108 static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_input *s) {
1109     assert(t && s);
1110     pa_tagstruct_putu32(t, s->index);
1111     pa_tagstruct_puts(t, s->name);
1112     pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1113     pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1114     pa_tagstruct_putu32(t, s->sink->index);
1115     pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1116     pa_tagstruct_putu32(t, s->volume);
1117     pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
1118     pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
1119 }
1120
1121 static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
1122     assert(t && s);
1123     pa_tagstruct_putu32(t, s->index);
1124     pa_tagstruct_puts(t, s->name);
1125     pa_tagstruct_putu32(t, s->owner ? s->owner->index : (uint32_t) -1);
1126     pa_tagstruct_putu32(t, s->client ? s->client->index : (uint32_t) -1);
1127     pa_tagstruct_putu32(t, s->source->index);
1128     pa_tagstruct_put_sample_spec(t, &s->sample_spec);
1129     pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
1130     pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
1131 }
1132
1133 static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {
1134     assert(t && e);
1135     pa_tagstruct_putu32(t, e->index);
1136     pa_tagstruct_puts(t, e->name);
1137     pa_tagstruct_putu32(t, e->volume);
1138     pa_tagstruct_put_usec(t, pa_bytes_to_usec(e->memchunk.length, &e->sample_spec));
1139     pa_tagstruct_put_sample_spec(t, &e->sample_spec);
1140     pa_tagstruct_putu32(t, e->memchunk.length);
1141     pa_tagstruct_put_boolean(t, e->lazy);
1142     pa_tagstruct_puts(t, e->filename);
1143 }
1144
1145 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1146     struct connection *c = userdata;
1147     uint32_t index;
1148     struct pa_sink *sink = NULL;
1149     struct pa_source *source = NULL;
1150     struct pa_client *client = NULL;
1151     struct pa_module *module = NULL;
1152     struct pa_sink_input *si = NULL;
1153     struct pa_source_output *so = NULL;
1154     struct pa_scache_entry *sce = NULL;
1155     const char *name;
1156     struct pa_tagstruct *reply;
1157     assert(c && t);
1158
1159     
1160     if (pa_tagstruct_getu32(t, &index) < 0 ||
1161         (command != PA_COMMAND_GET_CLIENT_INFO &&
1162          command != PA_COMMAND_GET_MODULE_INFO &&
1163          command != PA_COMMAND_GET_SINK_INPUT_INFO &&
1164          command != PA_COMMAND_GET_SOURCE_OUTPUT_INFO &&
1165          pa_tagstruct_gets(t, &name) < 0) ||
1166         !pa_tagstruct_eof(t)) {
1167         protocol_error(c);
1168         return;
1169     }
1170     
1171     if (!c->authorized) {
1172         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1173         return;
1174     }
1175
1176     if (command == PA_COMMAND_GET_SINK_INFO) {
1177         if (index != (uint32_t) -1)
1178             sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1179         else
1180             sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1181     } else if (command == PA_COMMAND_GET_SOURCE_INFO) {
1182         if (index != (uint32_t) -1)
1183             source = pa_idxset_get_by_index(c->protocol->core->sources, index);
1184         else
1185             source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1);
1186     } else if (command == PA_COMMAND_GET_CLIENT_INFO)
1187         client = pa_idxset_get_by_index(c->protocol->core->clients, index);
1188     else if (command == PA_COMMAND_GET_MODULE_INFO) 
1189         module = pa_idxset_get_by_index(c->protocol->core->modules, index);
1190     else if (command == PA_COMMAND_GET_SINK_INPUT_INFO)
1191         si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1192     else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO)
1193         so = pa_idxset_get_by_index(c->protocol->core->source_outputs, index);
1194     else {
1195         assert(command == PA_COMMAND_GET_SAMPLE_INFO);
1196         if (index != (uint32_t) -1)
1197             sce = pa_idxset_get_by_index(c->protocol->core->scache, index);
1198         else
1199             sce = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SAMPLE, 0);
1200     }
1201             
1202     if (!sink && !source && !client && !module && !si && !so && !sce) {
1203         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1204         return;
1205     }
1206
1207     reply = pa_tagstruct_new(NULL, 0);
1208     assert(reply);
1209     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1210     pa_tagstruct_putu32(reply, tag); 
1211     if (sink)
1212         sink_fill_tagstruct(reply, sink);
1213     else if (source)
1214         source_fill_tagstruct(reply, source);
1215     else if (client)
1216         client_fill_tagstruct(reply, client);
1217     else if (module)
1218         module_fill_tagstruct(reply, module);
1219     else if (si)
1220         sink_input_fill_tagstruct(reply, si);
1221     else if (so)
1222         source_output_fill_tagstruct(reply, so);
1223     else
1224         scache_fill_tagstruct(reply, sce);
1225     pa_pstream_send_tagstruct(c->pstream, reply);
1226 }
1227
1228 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1229     struct connection *c = userdata;
1230     struct pa_idxset *i;
1231     uint32_t index;
1232     void *p;
1233     struct pa_tagstruct *reply;
1234     assert(c && t);
1235
1236     if (!pa_tagstruct_eof(t)) {
1237         protocol_error(c);
1238         return;
1239     }
1240     
1241     if (!c->authorized) {
1242         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1243         return;
1244     }
1245
1246     reply = pa_tagstruct_new(NULL, 0);
1247     assert(reply);
1248     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1249     pa_tagstruct_putu32(reply, tag);
1250
1251     if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1252         i = c->protocol->core->sinks;
1253     else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1254         i = c->protocol->core->sources;
1255     else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1256         i = c->protocol->core->clients;
1257     else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1258         i = c->protocol->core->modules;
1259     else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1260         i = c->protocol->core->sink_inputs;
1261     else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST)
1262         i = c->protocol->core->source_outputs;
1263     else {
1264         assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1265         i = c->protocol->core->scache;
1266     }
1267
1268     if (i) {
1269         for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1270             if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1271                 sink_fill_tagstruct(reply, p);
1272             else if (command == PA_COMMAND_GET_SOURCE_INFO_LIST)
1273                 source_fill_tagstruct(reply, p);
1274             else if (command == PA_COMMAND_GET_CLIENT_INFO_LIST)
1275                 client_fill_tagstruct(reply, p);
1276             else if (command == PA_COMMAND_GET_MODULE_INFO_LIST)
1277                 module_fill_tagstruct(reply, p);
1278             else if (command == PA_COMMAND_GET_SINK_INPUT_INFO_LIST)
1279                 sink_input_fill_tagstruct(reply, p);
1280             else if (command == PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST) 
1281                 source_output_fill_tagstruct(reply, p);
1282             else {
1283                 assert(command == PA_COMMAND_GET_SAMPLE_INFO_LIST);
1284                 scache_fill_tagstruct(reply, p);
1285             }
1286         }
1287     }
1288     
1289     pa_pstream_send_tagstruct(c->pstream, reply);
1290 }
1291
1292 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1293     struct connection *c = userdata;
1294     struct pa_tagstruct *reply;
1295     char txt[256];
1296     const char *n;
1297     assert(c && t);
1298
1299     if (!pa_tagstruct_eof(t)) {
1300         protocol_error(c);
1301         return;
1302     }
1303     
1304     if (!c->authorized) {
1305         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1306         return;
1307     }
1308
1309     reply = pa_tagstruct_new(NULL, 0);
1310     assert(reply);
1311     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1312     pa_tagstruct_putu32(reply, tag);
1313     pa_tagstruct_puts(reply, PACKAGE_NAME);
1314     pa_tagstruct_puts(reply, PACKAGE_VERSION);
1315     pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1316     pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1317     pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1318
1319     n = pa_namereg_get_default_sink_name(c->protocol->core);
1320     pa_tagstruct_puts(reply, n);
1321     n = pa_namereg_get_default_source_name(c->protocol->core);
1322     pa_tagstruct_puts(reply, n);
1323     pa_pstream_send_tagstruct(c->pstream, reply);
1324 }
1325
1326 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1327     struct pa_tagstruct *t;
1328     struct connection *c = userdata;
1329     assert(c && core);
1330
1331     t = pa_tagstruct_new(NULL, 0);
1332     assert(t);
1333     pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1334     pa_tagstruct_putu32(t, (uint32_t) -1);
1335     pa_tagstruct_putu32(t, e);
1336     pa_tagstruct_putu32(t, index);
1337     pa_pstream_send_tagstruct(c->pstream, t);
1338 }
1339
1340 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1341     struct connection *c = userdata;
1342     enum pa_subscription_mask m;
1343     assert(c && t);
1344
1345     if (pa_tagstruct_getu32(t, &m) < 0 ||
1346         !pa_tagstruct_eof(t)) {
1347         protocol_error(c);
1348         return;
1349     }
1350     
1351     if (!c->authorized) {
1352         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1353         return;
1354     }
1355
1356     if (c->subscription)
1357         pa_subscription_free(c->subscription);
1358
1359     if (m != 0) {
1360         c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1361         assert(c->subscription);
1362     } else
1363         c->subscription = NULL;
1364
1365     pa_pstream_send_simple_ack(c->pstream, tag);
1366 }
1367
1368 static void command_set_volume(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1369     struct connection *c = userdata;
1370     uint32_t index, volume;
1371     struct pa_sink *sink = NULL;
1372     struct pa_sink_input *si = NULL;
1373     const char *name = NULL;
1374     assert(c && t);
1375
1376     if (pa_tagstruct_getu32(t, &index) < 0 ||
1377         (command == PA_COMMAND_SET_SINK_VOLUME && pa_tagstruct_gets(t, &name) < 0) ||
1378         pa_tagstruct_getu32(t, &volume) ||
1379         !pa_tagstruct_eof(t)) {
1380         protocol_error(c);
1381         return;
1382     }
1383     
1384     if (!c->authorized) {
1385         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1386         return;
1387     }
1388
1389     if (command == PA_COMMAND_SET_SINK_VOLUME) {
1390         if (index != (uint32_t) -1)
1391             sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
1392         else
1393             sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1);
1394     }  else {
1395         assert(command == PA_COMMAND_SET_SINK_INPUT_VOLUME);
1396         si = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index);
1397     }
1398
1399     if (!si && !sink) {
1400         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1401         return;
1402     }
1403
1404     if (sink)
1405         pa_sink_set_volume(sink, volume);
1406     else if (si)
1407         pa_sink_input_set_volume(si, volume);
1408
1409     pa_pstream_send_simple_ack(c->pstream, tag);
1410 }
1411
1412 static void command_cork_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1413     struct connection *c = userdata;
1414     uint32_t index;
1415     uint32_t b;
1416     struct playback_stream *s;
1417     assert(c && t);
1418
1419     if (pa_tagstruct_getu32(t, &index) < 0 ||
1420         pa_tagstruct_getu32(t, &b) < 0 ||
1421         !pa_tagstruct_eof(t)) {
1422         protocol_error(c);
1423         return;
1424     }
1425
1426     if (!c->authorized) {
1427         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1428         return;
1429     }
1430
1431     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1432         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1433         return;
1434     }
1435
1436     pa_sink_input_cork(s->sink_input, b);
1437     pa_pstream_send_simple_ack(c->pstream, tag);
1438 }
1439
1440 static void command_flush_or_trigger_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1441     struct connection *c = userdata;
1442     uint32_t index;
1443     struct playback_stream *s;
1444     assert(c && t);
1445
1446     if (pa_tagstruct_getu32(t, &index) < 0 ||
1447         !pa_tagstruct_eof(t)) {
1448         protocol_error(c);
1449         return;
1450     }
1451
1452     if (!c->authorized) {
1453         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1454         return;
1455     }
1456
1457     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1458         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1459         return;
1460     }
1461
1462     if (command == PA_COMMAND_TRIGGER_PLAYBACK_STREAM)
1463         pa_memblockq_prebuf_disable(s->memblockq);
1464     else {
1465         assert(command == PA_COMMAND_FLUSH_PLAYBACK_STREAM);
1466         pa_memblockq_flush(s->memblockq);
1467         /*pa_log(__FILE__": flush: %u\n", pa_memblockq_get_length(s->memblockq));*/
1468     }
1469
1470     pa_sink_notify(s->sink_input->sink);
1471     pa_pstream_send_simple_ack(c->pstream, tag);
1472     request_bytes(s);
1473 }
1474
1475 static void command_set_default_sink_or_source(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1476     struct connection *c = userdata;
1477     uint32_t index;
1478     const char *s;
1479     assert(c && t);
1480
1481     if (pa_tagstruct_getu32(t, &index) < 0 ||
1482         pa_tagstruct_gets(t, &s) < 0 || !s ||
1483         !pa_tagstruct_eof(t)) {
1484         protocol_error(c);
1485         return;
1486     }
1487
1488     if (!c->authorized) {
1489         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1490         return;
1491     }
1492
1493     pa_namereg_set_default(c->protocol->core, s, command == PA_COMMAND_SET_DEFAULT_SOURCE ? PA_NAMEREG_SOURCE : PA_NAMEREG_SINK);
1494     pa_pstream_send_simple_ack(c->pstream, tag);
1495 }
1496
1497 static void command_set_stream_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1498     struct connection *c = userdata;
1499     uint32_t index;
1500     const char *name;
1501     assert(c && t);
1502
1503     if (pa_tagstruct_getu32(t, &index) < 0 ||
1504         pa_tagstruct_gets(t, &name) < 0 || !name || 
1505         !pa_tagstruct_eof(t)) {
1506         protocol_error(c);
1507         return;
1508     }
1509     
1510     if (!c->authorized) {
1511         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1512         return;
1513     }
1514
1515     if (command == PA_COMMAND_SET_PLAYBACK_STREAM_NAME) {
1516         struct playback_stream *s;
1517         
1518         if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
1519             pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1520             return;
1521         }
1522
1523         pa_sink_input_set_name(s->sink_input, name);
1524         
1525     } else {
1526         struct record_stream *s;
1527         
1528         if (!(s = pa_idxset_get_by_index(c->record_streams, index))) {
1529             pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1530             return;
1531         }
1532
1533         pa_source_output_set_name(s->source_output, name);
1534     }
1535
1536     pa_pstream_send_simple_ack(c->pstream, tag);
1537 }
1538
1539 static void command_kill(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1540     struct connection *c = userdata;
1541     uint32_t index;
1542     assert(c && t);
1543
1544     if (pa_tagstruct_getu32(t, &index) < 0 ||
1545         !pa_tagstruct_eof(t)) {
1546         protocol_error(c);
1547         return;
1548     }
1549     
1550     if (!c->authorized) {
1551         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1552         return;
1553     }
1554
1555     if (command == PA_COMMAND_KILL_CLIENT) {
1556         struct pa_client *client;
1557         
1558         if (!(client = pa_idxset_get_by_index(c->protocol->core->clients, index))) {
1559             pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1560             return;
1561         }
1562
1563         pa_client_kill(client);
1564     } else if (command == PA_COMMAND_KILL_SINK_INPUT) {
1565         struct pa_sink_input *s;
1566         
1567         if (!(s = pa_idxset_get_by_index(c->protocol->core->sink_inputs, index))) {
1568             pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1569             return;
1570         }
1571
1572         pa_sink_input_kill(s);
1573     } else {
1574         struct pa_source_output *s;
1575
1576         assert(command == PA_COMMAND_KILL_SOURCE_OUTPUT);
1577         
1578         if (!(s = pa_idxset_get_by_index(c->protocol->core->source_outputs, index))) {
1579             pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1580             return;
1581         }
1582
1583         pa_source_output_kill(s);
1584     }
1585
1586     pa_pstream_send_simple_ack(c->pstream, tag);
1587 }
1588
1589 static void command_load_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1590     struct connection *c = userdata;
1591     struct pa_module *m;
1592     const char *name, *argument;
1593     struct pa_tagstruct *reply;
1594     assert(c && t);
1595
1596     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1597         pa_tagstruct_gets(t, &argument) < 0 ||
1598         !pa_tagstruct_eof(t)) {
1599         protocol_error(c);
1600         return;
1601     }
1602     
1603     if (!c->authorized) {
1604         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1605         return;
1606     }
1607
1608     if (!(m = pa_module_load(c->protocol->core, name, argument))) {
1609         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INITFAILED);
1610         return;
1611     }
1612
1613     reply = pa_tagstruct_new(NULL, 0);
1614     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1615     pa_tagstruct_putu32(reply, tag);
1616     pa_tagstruct_putu32(reply, m->index);
1617     pa_pstream_send_tagstruct(c->pstream, reply);
1618 }
1619
1620 static void command_unload_module(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1621     struct connection *c = userdata;
1622     uint32_t index;
1623     struct pa_module *m;
1624     assert(c && t);
1625
1626     if (pa_tagstruct_getu32(t, &index) < 0 ||
1627         !pa_tagstruct_eof(t)) {
1628         protocol_error(c);
1629         return;
1630     }
1631     
1632     if (!c->authorized) {
1633         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1634         return;
1635     }
1636
1637     if (!(m = pa_idxset_get_by_index(c->protocol->core->modules, index))) {
1638         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1639         return;
1640     }
1641
1642     pa_module_unload_request(m);
1643     pa_pstream_send_simple_ack(c->pstream, tag);
1644 }
1645
1646 static void command_add_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1647     struct connection *c = userdata;
1648     const char *name, *module, *argument;
1649     uint32_t type;
1650     assert(c && t);
1651
1652     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1653         pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1654         pa_tagstruct_gets(t, &module) < 0 || !module ||
1655         pa_tagstruct_gets(t, &argument) < 0 ||
1656         !pa_tagstruct_eof(t)) {
1657         protocol_error(c);
1658         return;
1659     }
1660     
1661     if (!c->authorized) {
1662         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1663         return;
1664     }
1665
1666     if (pa_autoload_add(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, argument) < 0) {
1667         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
1668         return;
1669     }
1670
1671     pa_pstream_send_simple_ack(c->pstream, tag);
1672 }
1673
1674 static void command_remove_autoload(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1675     struct connection *c = userdata;
1676     const char *name;
1677     uint32_t type;
1678     assert(c && t);
1679
1680     if (pa_tagstruct_gets(t, &name) < 0 || !name ||
1681         pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1682         !pa_tagstruct_eof(t)) {
1683         protocol_error(c);
1684         return;
1685     }
1686     
1687     if (!c->authorized) {
1688         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1689         return;
1690     }
1691
1692     if (pa_autoload_remove(c->protocol->core, name, type == 0 ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
1693         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1694         return;
1695     }
1696
1697     pa_pstream_send_simple_ack(c->pstream, tag);
1698 }
1699
1700 static void autoload_fill_tagstruct(struct pa_tagstruct *t, struct pa_autoload_entry *e) {
1701     assert(t && e);
1702     pa_tagstruct_puts(t, e->name);
1703     pa_tagstruct_putu32(t, e->type == PA_NAMEREG_SINK ? 0 : 1);
1704     pa_tagstruct_puts(t, e->module);
1705     pa_tagstruct_puts(t, e->argument);
1706 }
1707
1708 static void command_get_autoload_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1709     struct connection *c = userdata;
1710     struct pa_autoload_entry *a = NULL;
1711     uint32_t type;
1712     const char *name;
1713     struct pa_tagstruct *reply;
1714     assert(c && t);
1715     
1716     if (pa_tagstruct_gets(t, &name) < 0 || name ||
1717         pa_tagstruct_getu32(t, &type) < 0 || type > 1 ||
1718         !pa_tagstruct_eof(t)) {
1719         protocol_error(c);
1720         return;
1721     }
1722
1723     if (!c->authorized) {
1724         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1725         return;
1726     }
1727
1728     if (!c->protocol->core->autoload_hashmap || !(a = pa_hashmap_get(c->protocol->core->autoload_hashmap, name)) || (a->type == PA_NAMEREG_SINK && type != 0) || (a->type == PA_NAMEREG_SOURCE && type != 1)) {
1729         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1730         return;
1731     }
1732
1733     reply = pa_tagstruct_new(NULL, 0);
1734     assert(reply);
1735     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1736     pa_tagstruct_putu32(reply, tag);
1737     autoload_fill_tagstruct(reply, a);
1738     pa_pstream_send_tagstruct(c->pstream, reply);
1739 }
1740
1741 static void command_get_autoload_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1742     struct connection *c = userdata;
1743     struct pa_tagstruct *reply;
1744     assert(c && t);
1745
1746     if (!pa_tagstruct_eof(t)) {
1747         protocol_error(c);
1748         return;
1749     }
1750     
1751     if (!c->authorized) {
1752         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1753         return;
1754     }
1755
1756     reply = pa_tagstruct_new(NULL, 0);
1757     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1758     pa_tagstruct_putu32(reply, tag);
1759
1760     if (c->protocol->core->autoload_hashmap) {
1761         struct pa_autoload_entry *a;
1762         void *state = NULL;
1763
1764         while ((a = pa_hashmap_iterate(c->protocol->core->autoload_hashmap, &state)))
1765             autoload_fill_tagstruct(reply, a);
1766     }
1767     
1768     pa_pstream_send_tagstruct(c->pstream, reply);
1769 }
1770
1771 /*** pstream callbacks ***/
1772
1773 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1774     struct connection *c = userdata;
1775     assert(p && packet && packet->data && c);
1776
1777     if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1778         pa_log(__FILE__": invalid packet.\n");
1779         connection_free(c);
1780     }
1781 }
1782
1783 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1784     struct connection *c = userdata;
1785     struct output_stream *stream;
1786     assert(p && chunk && userdata);
1787
1788     if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1789         pa_log(__FILE__": client sent block for invalid stream.\n");
1790         connection_free(c);
1791         return;
1792     }
1793
1794     if (stream->type == PLAYBACK_STREAM) {
1795         struct playback_stream *p = (struct playback_stream*) stream;
1796         if (chunk->length >= p->requested_bytes)
1797             p->requested_bytes = 0;
1798         else
1799             p->requested_bytes -= chunk->length;
1800         
1801         pa_memblockq_push_align(p->memblockq, chunk, delta);
1802         assert(p->sink_input);
1803         /*pa_log(__FILE__": after_recv: %u\n", pa_memblockq_get_length(p->memblockq));*/
1804
1805         pa_sink_notify(p->sink_input->sink);
1806 /*         pa_log(__FILE__": Recieved %u bytes.\n", chunk->length); */
1807
1808     } else {
1809         struct upload_stream *u = (struct upload_stream*) stream;
1810         size_t l;
1811         assert(u->type == UPLOAD_STREAM);
1812
1813         if (!u->memchunk.memblock) {
1814             if (u->length == chunk->length) {
1815                 u->memchunk = *chunk;
1816                 pa_memblock_ref(u->memchunk.memblock);
1817                 u->length = 0;
1818             } else {
1819                 u->memchunk.memblock = pa_memblock_new(u->length, c->protocol->core->memblock_stat);
1820                 u->memchunk.index = u->memchunk.length = 0;
1821             }
1822         }
1823         
1824         assert(u->memchunk.memblock);
1825         
1826         l = u->length; 
1827         if (l > chunk->length)
1828             l = chunk->length;
1829
1830         if (l > 0) {
1831             memcpy((uint8_t*) u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length,
1832                    (uint8_t*) chunk->memblock->data+chunk->index, l);
1833             u->memchunk.length += l;
1834             u->length -= l;
1835         }
1836     }
1837 }
1838
1839 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1840     struct connection *c = userdata;
1841     assert(p && c);
1842     connection_free(c);
1843
1844 /*    pa_log(__FILE__": connection died.\n");*/
1845 }
1846
1847
1848 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1849     struct connection *c = userdata;
1850     assert(p && c);
1851
1852     send_memblock(c);
1853 }
1854
1855 /*** client callbacks ***/
1856
1857 static void client_kill_cb(struct pa_client *c) {
1858     assert(c && c->userdata);
1859     connection_free(c->userdata);
1860 }
1861
1862 /*** socket server callbacks ***/
1863
1864 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1865     struct pa_protocol_native *p = userdata;
1866     struct connection *c;
1867     assert(io && p);
1868
1869     c = pa_xmalloc(sizeof(struct connection));
1870
1871     c->authorized =!! p->public;
1872     c->protocol = p;
1873     assert(p->core);
1874     c->client = pa_client_new(p->core, "NATIVE", "Client");
1875     assert(c->client);
1876     c->client->kill = client_kill_cb;
1877     c->client->userdata = c;
1878     c->client->owner = p->module;
1879     
1880     c->pstream = pa_pstream_new(p->core->mainloop, io, p->core->memblock_stat);
1881     assert(c->pstream);
1882
1883     pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1884     pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1885     pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1886     pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1887
1888     c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1889     assert(c->pdispatch);
1890
1891     c->record_streams = pa_idxset_new(NULL, NULL);
1892     c->output_streams = pa_idxset_new(NULL, NULL);
1893     assert(c->record_streams && c->output_streams);
1894
1895     c->rrobin_index = PA_IDXSET_INVALID;
1896     c->subscription = NULL;
1897
1898     pa_idxset_put(p->connections, c, NULL);
1899 }
1900
1901 /*** module entry points ***/
1902
1903 static struct pa_protocol_native* protocol_new_internal(struct pa_core *c, struct pa_module *m, struct pa_modargs *ma) {
1904     struct pa_protocol_native *p;
1905     int public = 0;
1906     assert(c && ma);
1907
1908     if (pa_modargs_get_value_boolean(ma, "public", &public) < 0) {
1909         pa_log(__FILE__": public= expects a boolean argument.\n");
1910         return NULL;
1911     }
1912     
1913     p = pa_xmalloc(sizeof(struct pa_protocol_native));
1914
1915     if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1916         pa_xfree(p);
1917         return NULL;
1918     }
1919
1920     p->module = m;
1921     p->public = public;
1922     p->server = NULL;
1923     p->core = c;
1924     p->connections = pa_idxset_new(NULL, NULL);
1925     assert(p->connections);
1926
1927     return p;
1928 }
1929
1930 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) {
1931     struct pa_protocol_native *p;
1932
1933     if (!(p = protocol_new_internal(core, m, ma)))
1934         return NULL;
1935     
1936     p->server = server;
1937     pa_socket_server_set_callback(p->server, on_connection, p);
1938     
1939     return p;
1940 }
1941
1942 void pa_protocol_native_free(struct pa_protocol_native *p) {
1943     struct connection *c;
1944     assert(p);
1945
1946     while ((c = pa_idxset_first(p->connections, NULL)))
1947         connection_free(c);
1948     pa_idxset_free(p->connections, NULL, NULL);
1949
1950     if (p->server)
1951         pa_socket_server_unref(p->server);
1952     
1953     pa_xfree(p);
1954 }
1955
1956 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) {
1957     struct pa_protocol_native *p;
1958
1959     if (!(p = protocol_new_internal(core, m, ma)))
1960         return NULL;
1961
1962     on_connection(NULL, io, p);
1963     
1964     return p;
1965 }