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