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