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