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