4 This file is part of polypaudio.
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.
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.
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
31 #include "protocol-native.h"
32 #include "native-common.h"
35 #include "source-output.h"
36 #include "sink-input.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
45 struct pa_protocol_native;
47 struct record_stream {
48 struct connection *connection;
50 struct pa_source_output *source_output;
51 struct pa_memblockq *memblockq;
55 struct playback_stream {
56 struct connection *connection;
58 struct pa_sink_input *sink_input;
59 struct pa_memblockq *memblockq;
60 size_t requested_bytes;
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;
75 struct pa_protocol_native {
76 struct pa_module *module;
79 struct pa_socket_server *server;
80 struct pa_idxset *connections;
81 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
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);
89 static void request_bytes(struct playback_stream*s);
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);
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);
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 },
125 /* structure management */
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;
131 assert(c && source && ss && name && maxlength);
133 if (!(source_output = pa_source_output_new(source, name, ss)))
136 s = malloc(sizeof(struct record_stream));
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;
146 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_sample_size(ss), 0, 0);
147 assert(s->memblockq);
149 s->fragment_size = (fragment_size/base)*base;
150 if (!s->fragment_size)
151 s->fragment_size = base;
153 pa_idxset_put(c->record_streams, s, &s->index);
157 static void record_stream_free(struct record_stream* r) {
158 assert(r && r->connection);
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);
166 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, struct pa_sample_spec *ss, const char *name,
171 struct playback_stream *s;
172 struct pa_sink_input *sink_input;
173 assert(c && sink && ss && name && maxlength);
175 if (!(sink_input = pa_sink_input_new(sink, name, ss)))
178 s = malloc(sizeof(struct playback_stream));
181 s->sink_input = sink_input;
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;
191 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_sample_size(ss), prebuf, minreq);
192 assert(s->memblockq);
194 s->requested_bytes = 0;
195 s->drain_request = 0;
197 pa_idxset_put(c->playback_streams, s, &s->index);
201 static void playback_stream_free(struct playback_stream* p) {
202 assert(p && p->connection);
204 if (p->drain_request)
205 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
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);
213 static void connection_free(struct connection *c) {
214 struct record_stream *r;
215 struct playback_stream *p;
216 assert(c && c->protocol);
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);
223 while ((p = pa_idxset_first(c->playback_streams, NULL)))
224 playback_stream_free(p);
225 pa_idxset_free(c->playback_streams, NULL, NULL);
227 pa_pdispatch_free(c->pdispatch);
228 pa_pstream_free(c->pstream);
229 pa_client_free(c->client);
233 static void request_bytes(struct playback_stream *s) {
234 struct pa_tagstruct *t;
238 if (!(l = pa_memblockq_missing(s->memblockq)))
241 if (l <= s->requested_bytes)
244 l -= s->requested_bytes;
246 if (l < pa_memblockq_get_minreq(s->memblockq))
249 s->requested_bytes += l;
251 t = pa_tagstruct_new(NULL, 0);
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);
259 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
262 static void send_memblock(struct connection *c) {
264 struct record_stream *r;
266 start = PA_IDXSET_INVALID;
268 struct pa_memchunk chunk;
270 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
273 if (start == PA_IDXSET_INVALID)
274 start = c->rrobin_index;
275 else if (start == c->rrobin_index)
278 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
279 if (chunk.length > r->fragment_size)
280 chunk.length = r->fragment_size;
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);
291 static void send_playback_stream_killed(struct playback_stream *p) {
292 struct pa_tagstruct *t;
295 t = pa_tagstruct_new(NULL, 0);
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);
303 static void send_record_stream_killed(struct record_stream *r) {
304 struct pa_tagstruct *t;
307 t = pa_tagstruct_new(NULL, 0);
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);
316 /*** sinkinput callbacks ***/
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);
323 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
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);
334 pa_memblockq_drop(s->memblockq, length);
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;
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);
349 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
350 struct playback_stream *s;
351 assert(i && i->userdata);
354 return pa_samples_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
357 /*** source_output callbacks ***/
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);
364 pa_memblockq_push(s->memblockq, chunk, 0);
365 if (!pa_pstream_is_pending(s->connection->pstream))
366 send_memblock(s->connection);
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);
375 /*** pdispatch callbacks ***/
377 static void protocol_error(struct connection *c) {
378 fprintf(stderr, __FILE__": protocol error, kicking client\n");
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;
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);
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)) {
405 if (!c->authorized) {
406 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
410 if (sink_index == (uint32_t) -1)
411 sink = pa_sink_get_default(c->protocol->core);
413 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
416 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
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);
425 reply = pa_tagstruct_new(NULL, 0);
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);
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;
439 struct playback_stream *s;
442 if (pa_tagstruct_getu32(t, &channel) < 0 ||
443 !pa_tagstruct_eof(t)) {
448 if (!c->authorized) {
449 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
453 if (!(s = pa_idxset_get_by_index(c->playback_streams, channel))) {
454 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
458 playback_stream_free(s);
459 pa_pstream_send_simple_ack(c->pstream, tag);
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;
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);
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)) {
483 if (!c->authorized) {
484 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
488 if (source_index == (uint32_t) -1)
489 source = pa_source_get_default(c->protocol->core);
491 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
494 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
498 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
499 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
503 reply = pa_tagstruct_new(NULL, 0);
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);
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;
516 struct record_stream *s;
519 if (pa_tagstruct_getu32(t, &channel) < 0 ||
520 !pa_tagstruct_eof(t)) {
525 if (!c->authorized) {
526 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
530 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
531 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
535 record_stream_free(s);
536 pa_pstream_send_simple_ack(c->pstream, tag);
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;
543 if (!pa_tagstruct_eof(t)) {
548 if (!c->authorized) {
549 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
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 */
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;
564 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
565 !pa_tagstruct_eof(t)) {
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);
577 pa_pstream_send_simple_ack(c->pstream, tag);
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;
586 if (pa_tagstruct_gets(t, &name) < 0 ||
587 !pa_tagstruct_eof(t)) {
592 pa_client_rename(c->client, name);
593 pa_pstream_send_simple_ack(c->pstream, tag);
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;
600 uint32_t index = PA_IDXSET_INVALID;
603 if (pa_tagstruct_gets(t, &name) < 0 ||
604 !pa_tagstruct_eof(t)) {
609 if (!c->authorized) {
610 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
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)))
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;
625 if (index == PA_IDXSET_INVALID)
626 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
628 struct pa_tagstruct *reply;
629 reply = pa_tagstruct_new(NULL, 0);
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);
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;
641 struct playback_stream *s;
644 if (pa_tagstruct_getu32(t, &index) < 0 ||
645 !pa_tagstruct_eof(t)) {
650 if (!c->authorized) {
651 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
655 if (!(s = pa_idxset_get_by_index(c->playback_streams, index))) {
656 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
660 s->drain_request = 0;
662 if (!pa_memblockq_is_readable(s->memblockq))
663 pa_pstream_send_simple_ack(c->pstream, tag);
665 s->drain_request = 1;
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;
673 struct pa_tagstruct *reply;
675 if (!pa_tagstruct_eof(t)) {
680 if (!c->authorized) {
681 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
685 reply = pa_tagstruct_new(NULL, 0);
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);
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;
697 struct pa_tagstruct *reply;
698 struct playback_stream *s;
699 uint32_t index, latency;
701 if (pa_tagstruct_getu32(t, &index) < 0 ||
702 !pa_tagstruct_eof(t)) {
707 if (!c->authorized) {
708 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
712 if (!(s = pa_idxset_get_by_index(c->playback_streams, index))) {
713 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
717 latency = pa_sink_input_get_latency(s->sink_input);
718 reply = pa_tagstruct_new(NULL, 0);
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);
726 /*** pstream callbacks ***/
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);
732 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
733 fprintf(stderr, "protocol-native: invalid packet.\n");
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);
743 if (!(stream = pa_idxset_get_by_index(c->playback_streams, channel))) {
744 fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
749 if (chunk->length >= stream->requested_bytes)
750 stream->requested_bytes = 0;
752 stream->requested_bytes -= chunk->length;
754 pa_memblockq_push_align(stream->memblockq, chunk, delta);
755 assert(stream->sink_input);
756 pa_sink_notify(stream->sink_input->sink);
758 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
761 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
762 struct connection *c = userdata;
766 fprintf(stderr, "protocol-native: connection died.\n");
770 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
771 struct connection *c = userdata;
777 /*** client callbacks ***/
779 static void client_kill_cb(struct pa_client *c) {
780 assert(c && c->userdata);
781 connection_free(c->userdata);
784 /*** socket server callbacks ***/
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);
791 c = malloc(sizeof(struct connection));
793 c->authorized = p->public;
796 c->client = pa_client_new(p->core, "NATIVE", "Client");
798 c->client->kill = client_kill_cb;
799 c->client->userdata = c;
800 c->client->owner = p->module;
802 c->pstream = pa_pstream_new(p->core->mainloop, io);
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);
810 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
811 assert(c->pdispatch);
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);
817 c->rrobin_index = PA_IDXSET_INVALID;
819 pa_idxset_put(p->connections, c, NULL);
822 /*** module entry points ***/
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;
827 assert(core && server && ma);
829 if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
830 fprintf(stderr, __FILE__": public= expects numeric argument.\n");
834 p = malloc(sizeof(struct pa_protocol_native));
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) {
846 p->connections = pa_idxset_new(NULL, NULL);
847 assert(p->connections);
849 pa_socket_server_set_callback(p->server, on_connection, p);
854 void pa_protocol_native_free(struct pa_protocol_native *p) {
855 struct connection *c;
858 while ((c = pa_idxset_first(p->connections, NULL)))
860 pa_idxset_free(p->connections, NULL, NULL);
861 pa_socket_server_free(p->server);