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