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