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