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