rename src to polyp
[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
44 struct connection;
45 struct pa_protocol_native;
46
47 struct record_stream {
48     struct connection *connection;
49     uint32_t index;
50     struct pa_source_output *source_output;
51     struct pa_memblockq *memblockq;
52     size_t fragment_size;
53 };
54
55 struct playback_stream {
56     struct connection *connection;
57     uint32_t index;
58     struct pa_sink_input *sink_input;
59     struct pa_memblockq *memblockq;
60     size_t requested_bytes;
61     int drain_request;
62     uint32_t drain_tag;
63 };
64
65 struct connection {
66     int authorized;
67     struct pa_protocol_native *protocol;
68     struct pa_client *client;
69     struct pa_pstream *pstream;
70     struct pa_pdispatch *pdispatch;
71     struct pa_idxset *record_streams, *playback_streams;
72     uint32_t rrobin_index;
73 };
74
75 struct pa_protocol_native {
76     struct pa_module *module;
77     int public;
78     struct pa_core *core;
79     struct pa_socket_server *server;
80     struct pa_idxset *connections;
81     uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
82 };
83
84 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
85 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
86 static void sink_input_kill_cb(struct pa_sink_input *i);
87 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
88
89 static void request_bytes(struct playback_stream*s);
90
91 static void source_output_kill_cb(struct pa_source_output *o);
92 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
93
94 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
95 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
96 static void command_delete_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
97 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
98 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
99 static void command_delete_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
100 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
101 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
102 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
103 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
104 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
105
106 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
107     [PA_COMMAND_ERROR] = { NULL },
108     [PA_COMMAND_TIMEOUT] = { NULL },
109     [PA_COMMAND_REPLY] = { NULL },
110     [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
111     [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_playback_stream },
112     [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
113     [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
114     [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_record_stream },
115     [PA_COMMAND_AUTH] = { command_auth },
116     [PA_COMMAND_REQUEST] = { NULL },
117     [PA_COMMAND_EXIT] = { command_exit },
118     [PA_COMMAND_SET_NAME] = { command_set_name },
119     [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
120     [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
121     [PA_COMMAND_STAT] = { command_stat },
122     [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
123 };
124
125 /* structure management */
126
127 static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) {
128     struct record_stream *s;
129     struct pa_source_output *source_output;
130     size_t base;
131     assert(c && source && ss && name && maxlength);
132
133     if (!(source_output = pa_source_output_new(source, name, ss)))
134         return NULL;
135
136     s = malloc(sizeof(struct record_stream));
137     assert(s);
138     s->connection = c;
139     s->source_output = source_output;
140     s->source_output->push = source_output_push_cb;
141     s->source_output->kill = source_output_kill_cb;
142     s->source_output->userdata = s;
143     s->source_output->owner = c->protocol->module;
144     s->source_output->client = c->client;
145
146     s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_sample_size(ss), 0, 0);
147     assert(s->memblockq);
148
149     s->fragment_size = (fragment_size/base)*base;
150     if (!s->fragment_size)
151         s->fragment_size = base;
152
153     pa_idxset_put(c->record_streams, s, &s->index);
154     return s;
155 }
156
157 static void record_stream_free(struct record_stream* r) {
158     assert(r && r->connection);
159
160     pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
161     pa_source_output_free(r->source_output);
162     pa_memblockq_free(r->memblockq);
163     free(r);
164 }
165
166 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, struct pa_sample_spec *ss, const char *name,
167                                                    size_t maxlength,
168                                                    size_t tlength,
169                                                    size_t prebuf,
170                                                    size_t minreq) {
171     struct playback_stream *s;
172     struct pa_sink_input *sink_input;
173     assert(c && sink && ss && name && maxlength);
174
175     if (!(sink_input = pa_sink_input_new(sink, name, ss)))
176         return NULL;
177     
178     s = malloc(sizeof(struct playback_stream));
179     assert (s);
180     s->connection = c;
181     s->sink_input = sink_input;
182     
183     s->sink_input->peek = sink_input_peek_cb;
184     s->sink_input->drop = sink_input_drop_cb;
185     s->sink_input->kill = sink_input_kill_cb;
186     s->sink_input->get_latency = sink_input_get_latency_cb;
187     s->sink_input->userdata = s;
188     s->sink_input->owner = c->protocol->module;
189     s->sink_input->client = c->client;
190     
191     s->memblockq = pa_memblockq_new(maxlength, tlength, pa_sample_size(ss), prebuf, minreq);
192     assert(s->memblockq);
193
194     s->requested_bytes = 0;
195     s->drain_request = 0;
196     
197     pa_idxset_put(c->playback_streams, s, &s->index);
198     return s;
199 }
200
201 static void playback_stream_free(struct playback_stream* p) {
202     assert(p && p->connection);
203
204     if (p->drain_request)
205         pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
206
207     pa_idxset_remove_by_data(p->connection->playback_streams, p, NULL);
208     pa_sink_input_free(p->sink_input);
209     pa_memblockq_free(p->memblockq);
210     free(p);
211 }
212
213 static void connection_free(struct connection *c) {
214     struct record_stream *r;
215     struct playback_stream *p;
216     assert(c && c->protocol);
217
218     pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
219     while ((r = pa_idxset_first(c->record_streams, NULL)))
220         record_stream_free(r);
221     pa_idxset_free(c->record_streams, NULL, NULL);
222
223     while ((p = pa_idxset_first(c->playback_streams, NULL)))
224         playback_stream_free(p);
225     pa_idxset_free(c->playback_streams, NULL, NULL);
226
227     pa_pdispatch_free(c->pdispatch);
228     pa_pstream_free(c->pstream);
229     pa_client_free(c->client);
230     free(c);
231 }
232
233 static void request_bytes(struct playback_stream *s) {
234     struct pa_tagstruct *t;
235     size_t l;
236     assert(s);
237
238     if (!(l = pa_memblockq_missing(s->memblockq)))
239         return;
240
241     if (l <= s->requested_bytes)
242         return;
243
244     l -= s->requested_bytes;
245
246     if (l < pa_memblockq_get_minreq(s->memblockq))
247         return;
248     
249     s->requested_bytes += l;
250
251     t = pa_tagstruct_new(NULL, 0);
252     assert(t);
253     pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
254     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
255     pa_tagstruct_putu32(t, s->index);
256     pa_tagstruct_putu32(t, l);
257     pa_pstream_send_tagstruct(s->connection->pstream, t);
258
259     /*fprintf(stderr, "Requesting %u bytes\n", l);*/
260 }
261
262 static void send_memblock(struct connection *c) {
263     uint32_t start;
264     struct record_stream *r;
265
266     start = PA_IDXSET_INVALID;
267     for (;;) {
268         struct pa_memchunk chunk;
269         
270         if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
271             return;
272
273         if (start == PA_IDXSET_INVALID)
274             start = c->rrobin_index;
275         else if (start == c->rrobin_index)
276             return;
277
278         if (pa_memblockq_peek(r->memblockq,  &chunk) >= 0) {
279             if (chunk.length > r->fragment_size)
280                 chunk.length = r->fragment_size;
281
282             pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
283             pa_memblockq_drop(r->memblockq, chunk.length);
284             pa_memblock_unref(chunk.memblock);
285             
286             return;
287         }
288     }
289 }
290
291 static void send_playback_stream_killed(struct playback_stream *p) {
292     struct pa_tagstruct *t;
293     assert(p);
294
295     t = pa_tagstruct_new(NULL, 0);
296     assert(t);
297     pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
298     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
299     pa_tagstruct_putu32(t, p->index);
300     pa_pstream_send_tagstruct(p->connection->pstream, t);
301 }
302
303 static void send_record_stream_killed(struct record_stream *r) {
304     struct pa_tagstruct *t;
305     assert(r);
306
307     t = pa_tagstruct_new(NULL, 0);
308     assert(t);
309     pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
310     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
311     pa_tagstruct_putu32(t, r->index);
312     pa_pstream_send_tagstruct(r->connection->pstream, t);
313 }
314
315
316 /*** sinkinput callbacks ***/
317
318 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
319     struct playback_stream *s;
320     assert(i && i->userdata && chunk);
321     s = i->userdata;
322
323     if (pa_memblockq_peek(s->memblockq, chunk) < 0)
324         return -1;
325
326     return 0;
327 }
328
329 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
330     struct playback_stream *s;
331     assert(i && i->userdata && length);
332     s = i->userdata;
333
334     pa_memblockq_drop(s->memblockq, length);
335     request_bytes(s);
336
337     if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
338         pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
339         s->drain_request = 0;
340     }
341 }
342
343 static void sink_input_kill_cb(struct pa_sink_input *i) {
344     assert(i && i->userdata);
345     send_playback_stream_killed((struct playback_stream *) i->userdata);
346     playback_stream_free((struct playback_stream *) i->userdata);
347 }
348
349 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
350     struct playback_stream *s;
351     assert(i && i->userdata);
352     s = i->userdata;
353
354     return pa_samples_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
355 }
356
357 /*** source_output callbacks ***/
358
359 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
360     struct record_stream *s;
361     assert(o && o->userdata && chunk);
362     s = o->userdata;
363     
364     pa_memblockq_push(s->memblockq, chunk, 0);
365     if (!pa_pstream_is_pending(s->connection->pstream))
366         send_memblock(s->connection);
367 }
368
369 static void source_output_kill_cb(struct pa_source_output *o) {
370     assert(o && o->userdata);
371     send_record_stream_killed((struct record_stream *) o->userdata);
372     record_stream_free((struct record_stream *) o->userdata);
373 }
374
375 /*** pdispatch callbacks ***/
376
377 static void protocol_error(struct connection *c) {
378     fprintf(stderr, __FILE__": protocol error, kicking client\n");
379     connection_free(c);
380 }
381
382 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
383     struct connection *c = userdata;
384     struct playback_stream *s;
385     size_t maxlength, tlength, prebuf, minreq;
386     uint32_t sink_index;
387     const char *name;
388     struct pa_sample_spec ss;
389     struct pa_tagstruct *reply;
390     struct pa_sink *sink;
391     assert(c && t && c->protocol && c->protocol->core);
392     
393     if (pa_tagstruct_gets(t, &name) < 0 ||
394         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
395         pa_tagstruct_getu32(t, &sink_index) < 0 ||
396         pa_tagstruct_getu32(t, &maxlength) < 0 ||
397         pa_tagstruct_getu32(t, &tlength) < 0 ||
398         pa_tagstruct_getu32(t, &prebuf) < 0 ||
399         pa_tagstruct_getu32(t, &minreq) < 0 ||
400         !pa_tagstruct_eof(t)) {
401         protocol_error(c);
402         return;
403     }
404
405     if (!c->authorized) {
406         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
407         return;
408     }
409
410     if (sink_index == (uint32_t) -1)
411         sink = pa_sink_get_default(c->protocol->core);
412     else
413         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
414
415     if (!sink) {
416         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
417         return;
418     }
419     
420     if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
421         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
422         return;
423     }
424     
425     reply = pa_tagstruct_new(NULL, 0);
426     assert(reply);
427     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
428     pa_tagstruct_putu32(reply, tag);
429     pa_tagstruct_putu32(reply, s->index);
430     assert(s->sink_input);
431     pa_tagstruct_putu32(reply, s->sink_input->index);
432     pa_pstream_send_tagstruct(c->pstream, reply);
433     request_bytes(s);
434 }
435
436 static void command_delete_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
437     struct connection *c = userdata;
438     uint32_t channel;
439     struct playback_stream *s;
440     assert(c && t);
441     
442     if (pa_tagstruct_getu32(t, &channel) < 0 ||
443         !pa_tagstruct_eof(t)) {
444         protocol_error(c);
445         return;
446     }
447
448     if (!c->authorized) {
449         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
450         return;
451     }
452     
453     if (!(s = pa_idxset_get_by_index(c->playback_streams, channel))) {
454         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
455         return;
456     }
457
458     playback_stream_free(s);
459     pa_pstream_send_simple_ack(c->pstream, tag);
460 }
461
462 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
463     struct connection *c = userdata;
464     struct record_stream *s;
465     size_t maxlength, fragment_size;
466     uint32_t source_index;
467     const char *name;
468     struct pa_sample_spec ss;
469     struct pa_tagstruct *reply;
470     struct pa_source *source;
471     assert(c && t && c->protocol && c->protocol->core);
472     
473     if (pa_tagstruct_gets(t, &name) < 0 ||
474         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
475         pa_tagstruct_getu32(t, &source_index) < 0 ||
476         pa_tagstruct_getu32(t, &maxlength) < 0 ||
477         pa_tagstruct_getu32(t, &fragment_size) < 0 ||
478         !pa_tagstruct_eof(t)) {
479         protocol_error(c);
480         return;
481     }
482
483     if (!c->authorized) {
484         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
485         return;
486     }
487
488     if (source_index == (uint32_t) -1)
489         source = pa_source_get_default(c->protocol->core);
490     else
491         source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
492
493     if (!source) {
494         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
495         return;
496     }
497     
498     if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
499         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
500         return;
501     }
502     
503     reply = pa_tagstruct_new(NULL, 0);
504     assert(reply);
505     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
506     pa_tagstruct_putu32(reply, tag);
507     pa_tagstruct_putu32(reply, s->index);
508     assert(s->source_output);
509     pa_tagstruct_putu32(reply, s->source_output->index);
510     pa_pstream_send_tagstruct(c->pstream, reply);
511 }
512
513 static void command_delete_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
514     struct connection *c = userdata;
515     uint32_t channel;
516     struct record_stream *s;
517     assert(c && t);
518     
519     if (pa_tagstruct_getu32(t, &channel) < 0 ||
520         !pa_tagstruct_eof(t)) {
521         protocol_error(c);
522         return;
523     }
524
525     if (!c->authorized) {
526         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
527         return;
528     }
529     
530     if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
531         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
532         return;
533     }
534
535     record_stream_free(s);
536     pa_pstream_send_simple_ack(c->pstream, tag);
537 }
538
539 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
540     struct connection *c = userdata;
541     assert(c && t);
542     
543     if (!pa_tagstruct_eof(t)) {
544         protocol_error(c);
545         return;
546     }
547
548     if (!c->authorized) {
549         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
550         return;
551     }
552     
553     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
554     c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
555     pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
556     return;
557 }
558
559 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
560     struct connection *c = userdata;
561     const void*cookie;
562     assert(c && t);
563
564     if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
565         !pa_tagstruct_eof(t)) {
566         protocol_error(c);
567         return;
568     }
569         
570     if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
571         fprintf(stderr, "protocol-native.c: Denied access to client with invalid authorization key.\n");
572         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
573         return;
574     }
575
576     c->authorized = 1;
577     pa_pstream_send_simple_ack(c->pstream, tag);
578     return;
579 }
580
581 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
582     struct connection *c = userdata;
583     const char *name;
584     assert(c && t);
585
586     if (pa_tagstruct_gets(t, &name) < 0 ||
587         !pa_tagstruct_eof(t)) {
588         protocol_error(c);
589         return;
590     }
591
592     pa_client_rename(c->client, name);
593     pa_pstream_send_simple_ack(c->pstream, tag);
594     return;
595 }
596
597 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
598     struct connection *c = userdata;
599     const char *name;
600     uint32_t index = PA_IDXSET_INVALID;
601     assert(c && t);
602
603     if (pa_tagstruct_gets(t, &name) < 0 ||
604         !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     if (command == PA_COMMAND_LOOKUP_SINK) {
615         struct pa_sink *sink;
616         if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK)))
617             index = sink->index;
618     } else {
619         struct pa_source *source;
620         assert(command == PA_COMMAND_LOOKUP_SOURCE);
621         if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE)))
622             index = source->index;
623     }
624
625     if (index == PA_IDXSET_INVALID)
626         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
627     else {
628         struct pa_tagstruct *reply;
629         reply = pa_tagstruct_new(NULL, 0);
630         assert(reply);
631         pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
632         pa_tagstruct_putu32(reply, tag);
633         pa_tagstruct_putu32(reply, index);
634         pa_pstream_send_tagstruct(c->pstream, reply);
635     }
636 }
637
638 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
639     struct connection *c = userdata;
640     uint32_t index;
641     struct playback_stream *s;
642     assert(c && t);
643
644     if (pa_tagstruct_getu32(t, &index) < 0 ||
645         !pa_tagstruct_eof(t)) {
646         protocol_error(c);
647         return;
648     }
649
650     if (!c->authorized) {
651         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
652         return;
653     }
654
655     if (!(s = pa_idxset_get_by_index(c->playback_streams, index))) {
656         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
657         return;
658     }
659
660     s->drain_request = 0;
661     
662     if (!pa_memblockq_is_readable(s->memblockq))
663         pa_pstream_send_simple_ack(c->pstream, tag);
664     else {
665         s->drain_request = 1;
666         s->drain_tag = tag;
667     }
668
669
670 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
671     struct connection *c = userdata;
672     assert(c && t);
673     struct pa_tagstruct *reply;
674
675     if (!pa_tagstruct_eof(t)) {
676         protocol_error(c);
677         return;
678     }
679
680     if (!c->authorized) {
681         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
682         return;
683     }
684
685     reply = pa_tagstruct_new(NULL, 0);
686     assert(reply);
687     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
688     pa_tagstruct_putu32(reply, tag);
689     pa_tagstruct_putu32(reply, pa_memblock_get_count());
690     pa_tagstruct_putu32(reply, pa_memblock_get_total());
691     pa_pstream_send_tagstruct(c->pstream, reply);
692 }
693
694 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
695     struct connection *c = userdata;
696     assert(c && t);
697     struct pa_tagstruct *reply;
698     struct playback_stream *s;
699     uint32_t index, latency;
700
701     if (pa_tagstruct_getu32(t, &index) < 0 ||
702         !pa_tagstruct_eof(t)) {
703         protocol_error(c);
704         return;
705     }
706
707     if (!c->authorized) {
708         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
709         return;
710     }
711
712     if (!(s = pa_idxset_get_by_index(c->playback_streams, index))) {
713         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
714         return;
715     }
716
717     latency = pa_sink_input_get_latency(s->sink_input);
718     reply = pa_tagstruct_new(NULL, 0);
719     assert(reply);
720     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
721     pa_tagstruct_putu32(reply, tag);
722     pa_tagstruct_putu32(reply, latency);
723     pa_pstream_send_tagstruct(c->pstream, reply);
724 }
725
726 /*** pstream callbacks ***/
727
728 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
729     struct connection *c = userdata;
730     assert(p && packet && packet->data && c);
731
732     if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
733         fprintf(stderr, "protocol-native: invalid packet.\n");
734         connection_free(c);
735     }
736 }
737
738 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
739     struct connection *c = userdata;
740     struct playback_stream *stream;
741     assert(p && chunk && userdata);
742
743     if (!(stream = pa_idxset_get_by_index(c->playback_streams, channel))) {
744         fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
745         connection_free(c);
746         return;
747     }
748
749     if (chunk->length >= stream->requested_bytes)
750         stream->requested_bytes = 0;
751     else
752         stream->requested_bytes -= chunk->length;
753     
754     pa_memblockq_push_align(stream->memblockq, chunk, delta);
755     assert(stream->sink_input);
756     pa_sink_notify(stream->sink_input->sink);
757
758     /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
759 }
760
761 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
762     struct connection *c = userdata;
763     assert(p && c);
764     connection_free(c);
765
766     fprintf(stderr, "protocol-native: connection died.\n");
767 }
768
769
770 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
771     struct connection *c = userdata;
772     assert(p && c);
773
774     send_memblock(c);
775 }
776
777 /*** client callbacks ***/
778
779 static void client_kill_cb(struct pa_client *c) {
780     assert(c && c->userdata);
781     connection_free(c->userdata);
782 }
783
784 /*** socket server callbacks ***/
785
786 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
787     struct pa_protocol_native *p = userdata;
788     struct connection *c;
789     assert(s && io && p);
790
791     c = malloc(sizeof(struct connection));
792     assert(c);
793     c->authorized = p->public;
794     c->protocol = p;
795     assert(p->core);
796     c->client = pa_client_new(p->core, "NATIVE", "Client");
797     assert(c->client);
798     c->client->kill = client_kill_cb;
799     c->client->userdata = c;
800     c->client->owner = p->module;
801     
802     c->pstream = pa_pstream_new(p->core->mainloop, io);
803     assert(c->pstream);
804
805     pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
806     pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
807     pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
808     pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
809
810     c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
811     assert(c->pdispatch);
812
813     c->record_streams = pa_idxset_new(NULL, NULL);
814     c->playback_streams = pa_idxset_new(NULL, NULL);
815     assert(c->record_streams && c->playback_streams);
816
817     c->rrobin_index = PA_IDXSET_INVALID;
818
819     pa_idxset_put(p->connections, c, NULL);
820 }
821
822 /*** module entry points ***/
823
824 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) {
825     struct pa_protocol_native *p;
826     uint32_t public;
827     assert(core && server && ma);
828
829     if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
830         fprintf(stderr, __FILE__": public= expects numeric argument.\n");
831         return NULL;
832     }
833     
834     p = malloc(sizeof(struct pa_protocol_native));
835     assert(p);
836
837     if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
838         free(p);
839         return NULL;
840     }
841
842     p->module = m;
843     p->public = public;
844     p->server = server;
845     p->core = core;
846     p->connections = pa_idxset_new(NULL, NULL);
847     assert(p->connections);
848
849     pa_socket_server_set_callback(p->server, on_connection, p);
850     
851     return p;
852 }
853
854 void pa_protocol_native_free(struct pa_protocol_native *p) {
855     struct connection *c;
856     assert(p);
857
858     while ((c = pa_idxset_first(p->connections, NULL)))
859         connection_free(c);
860     pa_idxset_free(p->connections, NULL, NULL);
861     pa_socket_server_free(p->server);
862     free(p);
863 }