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"
46 #include "subscribe.h"
49 struct pa_protocol_native;
51 struct record_stream {
52 struct connection *connection;
54 struct pa_source_output *source_output;
55 struct pa_memblockq *memblockq;
59 struct playback_stream {
61 struct connection *connection;
63 struct pa_sink_input *sink_input;
64 struct pa_memblockq *memblockq;
65 size_t requested_bytes;
70 struct upload_stream {
72 struct connection *connection;
74 struct pa_memchunk memchunk;
77 struct pa_sample_spec sample_spec;
80 struct output_stream {
91 struct pa_protocol_native *protocol;
92 struct pa_client *client;
93 struct pa_pstream *pstream;
94 struct pa_pdispatch *pdispatch;
95 struct pa_idxset *record_streams, *output_streams;
96 uint32_t rrobin_index;
97 struct pa_subscription *subscription;
100 struct pa_protocol_native {
101 struct pa_module *module;
103 struct pa_core *core;
104 struct pa_socket_server *server;
105 struct pa_idxset *connections;
106 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
109 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
110 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
111 static void sink_input_kill_cb(struct pa_sink_input *i);
112 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
114 static void request_bytes(struct playback_stream*s);
116 static void source_output_kill_cb(struct pa_source_output *o);
117 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
119 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
120 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
121 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
122 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
135 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
136 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
138 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
139 [PA_COMMAND_ERROR] = { NULL },
140 [PA_COMMAND_TIMEOUT] = { NULL },
141 [PA_COMMAND_REPLY] = { NULL },
142 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
143 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
144 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
145 [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
146 [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
147 [PA_COMMAND_AUTH] = { command_auth },
148 [PA_COMMAND_REQUEST] = { NULL },
149 [PA_COMMAND_EXIT] = { command_exit },
150 [PA_COMMAND_SET_NAME] = { command_set_name },
151 [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
152 [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
153 [PA_COMMAND_STAT] = { command_stat },
154 [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
155 [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
156 [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
157 [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
158 [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
159 [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
160 [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
161 [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
162 [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
163 [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
164 [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
165 [PA_COMMAND_SUBSCRIBE] = { command_subscribe },
168 /* structure management */
170 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
171 struct upload_stream *s;
172 assert(c && ss && name && length);
174 s = pa_xmalloc(sizeof(struct upload_stream));
175 s->type = UPLOAD_STREAM;
177 s->sample_spec = *ss;
178 s->name = pa_xstrdup(name);
180 s->memchunk.memblock = NULL;
181 s->memchunk.index = 0;
182 s->memchunk.length = 0;
186 pa_idxset_put(c->output_streams, s, &s->index);
190 static void upload_stream_free(struct upload_stream *o) {
191 assert(o && o->connection);
193 pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
197 if (o->memchunk.memblock)
198 pa_memblock_unref(o->memchunk.memblock);
203 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) {
204 struct record_stream *s;
205 struct pa_source_output *source_output;
207 assert(c && source && ss && name && maxlength);
209 if (!(source_output = pa_source_output_new(source, name, ss)))
212 s = pa_xmalloc(sizeof(struct record_stream));
214 s->source_output = source_output;
215 s->source_output->push = source_output_push_cb;
216 s->source_output->kill = source_output_kill_cb;
217 s->source_output->userdata = s;
218 s->source_output->owner = c->protocol->module;
219 s->source_output->client = c->client;
221 s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0);
222 assert(s->memblockq);
224 s->fragment_size = (fragment_size/base)*base;
225 if (!s->fragment_size)
226 s->fragment_size = base;
228 pa_idxset_put(c->record_streams, s, &s->index);
232 static void record_stream_free(struct record_stream* r) {
233 assert(r && r->connection);
235 pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
236 pa_source_output_free(r->source_output);
237 pa_memblockq_free(r->memblockq);
241 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
246 struct playback_stream *s;
247 struct pa_sink_input *sink_input;
248 assert(c && sink && ss && name && maxlength);
250 if (!(sink_input = pa_sink_input_new(sink, name, ss)))
253 s = pa_xmalloc(sizeof(struct playback_stream));
254 s->type = PLAYBACK_STREAM;
256 s->sink_input = sink_input;
258 s->sink_input->peek = sink_input_peek_cb;
259 s->sink_input->drop = sink_input_drop_cb;
260 s->sink_input->kill = sink_input_kill_cb;
261 s->sink_input->get_latency = sink_input_get_latency_cb;
262 s->sink_input->userdata = s;
263 s->sink_input->owner = c->protocol->module;
264 s->sink_input->client = c->client;
266 s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq);
267 assert(s->memblockq);
269 s->requested_bytes = 0;
270 s->drain_request = 0;
272 pa_idxset_put(c->output_streams, s, &s->index);
276 static void playback_stream_free(struct playback_stream* p) {
277 assert(p && p->connection);
279 if (p->drain_request)
280 pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
282 pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
283 pa_sink_input_free(p->sink_input);
284 pa_memblockq_free(p->memblockq);
288 static void connection_free(struct connection *c) {
289 struct record_stream *r;
290 struct output_stream *o;
291 assert(c && c->protocol);
293 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
294 while ((r = pa_idxset_first(c->record_streams, NULL)))
295 record_stream_free(r);
296 pa_idxset_free(c->record_streams, NULL, NULL);
298 while ((o = pa_idxset_first(c->output_streams, NULL)))
299 if (o->type == PLAYBACK_STREAM)
300 playback_stream_free((struct playback_stream*) o);
302 upload_stream_free((struct upload_stream*) o);
303 pa_idxset_free(c->output_streams, NULL, NULL);
305 pa_pdispatch_free(c->pdispatch);
306 pa_pstream_free(c->pstream);
307 pa_client_free(c->client);
310 pa_subscription_free(c->subscription);
315 static void request_bytes(struct playback_stream *s) {
316 struct pa_tagstruct *t;
320 if (!(l = pa_memblockq_missing(s->memblockq)))
323 if (l <= s->requested_bytes)
326 l -= s->requested_bytes;
328 if (l < pa_memblockq_get_minreq(s->memblockq))
331 s->requested_bytes += l;
333 t = pa_tagstruct_new(NULL, 0);
335 pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
336 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
337 pa_tagstruct_putu32(t, s->index);
338 pa_tagstruct_putu32(t, l);
339 pa_pstream_send_tagstruct(s->connection->pstream, t);
341 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
344 static void send_memblock(struct connection *c) {
346 struct record_stream *r;
348 start = PA_IDXSET_INVALID;
350 struct pa_memchunk chunk;
352 if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
355 if (start == PA_IDXSET_INVALID)
356 start = c->rrobin_index;
357 else if (start == c->rrobin_index)
360 if (pa_memblockq_peek(r->memblockq, &chunk) >= 0) {
361 if (chunk.length > r->fragment_size)
362 chunk.length = r->fragment_size;
364 pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
365 pa_memblockq_drop(r->memblockq, chunk.length);
366 pa_memblock_unref(chunk.memblock);
373 static void send_playback_stream_killed(struct playback_stream *p) {
374 struct pa_tagstruct *t;
377 t = pa_tagstruct_new(NULL, 0);
379 pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
380 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
381 pa_tagstruct_putu32(t, p->index);
382 pa_pstream_send_tagstruct(p->connection->pstream, t);
385 static void send_record_stream_killed(struct record_stream *r) {
386 struct pa_tagstruct *t;
389 t = pa_tagstruct_new(NULL, 0);
391 pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
392 pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
393 pa_tagstruct_putu32(t, r->index);
394 pa_pstream_send_tagstruct(r->connection->pstream, t);
398 /*** sinkinput callbacks ***/
400 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
401 struct playback_stream *s;
402 assert(i && i->userdata && chunk);
405 if (pa_memblockq_peek(s->memblockq, chunk) < 0)
411 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
412 struct playback_stream *s;
413 assert(i && i->userdata && length);
416 pa_memblockq_drop(s->memblockq, length);
419 if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
420 pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
421 s->drain_request = 0;
425 static void sink_input_kill_cb(struct pa_sink_input *i) {
426 assert(i && i->userdata);
427 send_playback_stream_killed((struct playback_stream *) i->userdata);
428 playback_stream_free((struct playback_stream *) i->userdata);
431 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
432 struct playback_stream *s;
433 assert(i && i->userdata);
436 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
439 /*** source_output callbacks ***/
441 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
442 struct record_stream *s;
443 assert(o && o->userdata && chunk);
446 pa_memblockq_push(s->memblockq, chunk, 0);
447 if (!pa_pstream_is_pending(s->connection->pstream))
448 send_memblock(s->connection);
451 static void source_output_kill_cb(struct pa_source_output *o) {
452 assert(o && o->userdata);
453 send_record_stream_killed((struct record_stream *) o->userdata);
454 record_stream_free((struct record_stream *) o->userdata);
457 /*** pdispatch callbacks ***/
459 static void protocol_error(struct connection *c) {
460 fprintf(stderr, __FILE__": protocol error, kicking client\n");
464 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
465 struct connection *c = userdata;
466 struct playback_stream *s;
467 size_t maxlength, tlength, prebuf, minreq;
469 const char *name, *sink_name;
470 struct pa_sample_spec ss;
471 struct pa_tagstruct *reply;
472 struct pa_sink *sink;
473 assert(c && t && c->protocol && c->protocol->core);
475 if (pa_tagstruct_gets(t, &name) < 0 ||
476 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
477 pa_tagstruct_getu32(t, &sink_index) < 0 ||
478 pa_tagstruct_gets(t, &sink_name) < 0 ||
479 pa_tagstruct_getu32(t, &maxlength) < 0 ||
480 pa_tagstruct_getu32(t, &tlength) < 0 ||
481 pa_tagstruct_getu32(t, &prebuf) < 0 ||
482 pa_tagstruct_getu32(t, &minreq) < 0 ||
483 !pa_tagstruct_eof(t)) {
488 if (!c->authorized) {
489 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
493 if (sink_index != (uint32_t) -1)
494 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
496 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
499 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
503 if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
504 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
508 reply = pa_tagstruct_new(NULL, 0);
510 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
511 pa_tagstruct_putu32(reply, tag);
512 pa_tagstruct_putu32(reply, s->index);
513 assert(s->sink_input);
514 pa_tagstruct_putu32(reply, s->sink_input->index);
515 pa_pstream_send_tagstruct(c->pstream, reply);
519 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
520 struct connection *c = userdata;
524 if (pa_tagstruct_getu32(t, &channel) < 0 ||
525 !pa_tagstruct_eof(t)) {
530 if (!c->authorized) {
531 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
535 if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
536 struct playback_stream *s;
537 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
538 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
542 playback_stream_free(s);
543 } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
544 struct record_stream *s;
545 if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
546 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
550 record_stream_free(s);
552 struct upload_stream *s;
553 assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
554 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
555 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
559 upload_stream_free(s);
562 pa_pstream_send_simple_ack(c->pstream, tag);
565 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
566 struct connection *c = userdata;
567 struct record_stream *s;
568 size_t maxlength, fragment_size;
569 uint32_t source_index;
570 const char *name, *source_name;
571 struct pa_sample_spec ss;
572 struct pa_tagstruct *reply;
573 struct pa_source *source;
574 assert(c && t && c->protocol && c->protocol->core);
576 if (pa_tagstruct_gets(t, &name) < 0 ||
577 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
578 pa_tagstruct_getu32(t, &source_index) < 0 ||
579 pa_tagstruct_gets(t, &source_name) < 0 ||
580 pa_tagstruct_getu32(t, &maxlength) < 0 ||
581 pa_tagstruct_getu32(t, &fragment_size) < 0 ||
582 !pa_tagstruct_eof(t)) {
587 if (!c->authorized) {
588 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
592 if (source_index != (uint32_t) -1)
593 source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
595 source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1);
598 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
602 if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
603 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
607 reply = pa_tagstruct_new(NULL, 0);
609 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
610 pa_tagstruct_putu32(reply, tag);
611 pa_tagstruct_putu32(reply, s->index);
612 assert(s->source_output);
613 pa_tagstruct_putu32(reply, s->source_output->index);
614 pa_pstream_send_tagstruct(c->pstream, reply);
617 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
618 struct connection *c = userdata;
621 if (!pa_tagstruct_eof(t)) {
626 if (!c->authorized) {
627 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
631 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
632 c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
633 pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
637 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
638 struct connection *c = userdata;
642 if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
643 !pa_tagstruct_eof(t)) {
648 if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
649 fprintf(stderr, "protocol-native.c: Denied access to client with invalid authorization key.\n");
650 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
655 pa_pstream_send_simple_ack(c->pstream, tag);
659 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
660 struct connection *c = userdata;
664 if (pa_tagstruct_gets(t, &name) < 0 ||
665 !pa_tagstruct_eof(t)) {
670 pa_client_rename(c->client, name);
671 pa_pstream_send_simple_ack(c->pstream, tag);
675 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
676 struct connection *c = userdata;
678 uint32_t index = PA_IDXSET_INVALID;
681 if (pa_tagstruct_gets(t, &name) < 0 ||
682 !pa_tagstruct_eof(t)) {
687 if (!c->authorized) {
688 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
692 if (command == PA_COMMAND_LOOKUP_SINK) {
693 struct pa_sink *sink;
694 if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
697 struct pa_source *source;
698 assert(command == PA_COMMAND_LOOKUP_SOURCE);
699 if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
700 index = source->index;
703 if (index == PA_IDXSET_INVALID)
704 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
706 struct pa_tagstruct *reply;
707 reply = pa_tagstruct_new(NULL, 0);
709 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
710 pa_tagstruct_putu32(reply, tag);
711 pa_tagstruct_putu32(reply, index);
712 pa_pstream_send_tagstruct(c->pstream, reply);
716 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
717 struct connection *c = userdata;
719 struct playback_stream *s;
722 if (pa_tagstruct_getu32(t, &index) < 0 ||
723 !pa_tagstruct_eof(t)) {
728 if (!c->authorized) {
729 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
733 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
734 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
738 s->drain_request = 0;
740 if (!pa_memblockq_is_readable(s->memblockq))
741 pa_pstream_send_simple_ack(c->pstream, tag);
743 s->drain_request = 1;
748 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
749 struct connection *c = userdata;
751 struct pa_tagstruct *reply;
753 if (!pa_tagstruct_eof(t)) {
758 if (!c->authorized) {
759 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
763 reply = pa_tagstruct_new(NULL, 0);
765 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
766 pa_tagstruct_putu32(reply, tag);
767 pa_tagstruct_putu32(reply, pa_memblock_get_count());
768 pa_tagstruct_putu32(reply, pa_memblock_get_total());
769 pa_pstream_send_tagstruct(c->pstream, reply);
772 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
773 struct connection *c = userdata;
775 struct pa_tagstruct *reply;
776 struct playback_stream *s;
777 uint32_t index, latency;
779 if (pa_tagstruct_getu32(t, &index) < 0 ||
780 !pa_tagstruct_eof(t)) {
785 if (!c->authorized) {
786 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
790 if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
791 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
795 latency = pa_sink_input_get_latency(s->sink_input);
796 reply = pa_tagstruct_new(NULL, 0);
798 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
799 pa_tagstruct_putu32(reply, tag);
800 pa_tagstruct_putu32(reply, latency);
801 pa_pstream_send_tagstruct(c->pstream, reply);
804 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
805 struct connection *c = userdata;
806 struct upload_stream *s;
809 struct pa_sample_spec ss;
810 struct pa_tagstruct *reply;
811 assert(c && t && c->protocol && c->protocol->core);
813 if (pa_tagstruct_gets(t, &name) < 0 ||
814 pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
815 pa_tagstruct_getu32(t, &length) < 0 ||
816 !pa_tagstruct_eof(t)) {
821 if (!c->authorized) {
822 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
826 if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
827 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
831 if (!(s = upload_stream_new(c, &ss, name, length))) {
832 pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
836 reply = pa_tagstruct_new(NULL, 0);
838 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
839 pa_tagstruct_putu32(reply, tag);
840 pa_tagstruct_putu32(reply, s->index);
841 pa_pstream_send_tagstruct(c->pstream, reply);
843 reply = pa_tagstruct_new(NULL, 0);
845 pa_tagstruct_putu32(reply, PA_COMMAND_REQUEST);
846 pa_tagstruct_putu32(reply, (uint32_t) -1); /* tag */
847 pa_tagstruct_putu32(reply, s->index);
848 pa_tagstruct_putu32(reply, length);
849 pa_pstream_send_tagstruct(c->pstream, reply);
852 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
853 struct connection *c = userdata;
855 struct upload_stream *s;
859 if (pa_tagstruct_getu32(t, &channel) < 0 ||
860 !pa_tagstruct_eof(t)) {
865 if (!c->authorized) {
866 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
870 if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
871 pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
875 pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
876 pa_pstream_send_simple_ack(c->pstream, tag);
877 upload_stream_free(s);
880 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
881 struct connection *c = userdata;
882 uint32_t sink_index, volume;
883 struct pa_sink *sink;
884 const char *name, *sink_name;
887 if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
888 pa_tagstruct_gets(t, &sink_name) < 0 ||
889 pa_tagstruct_getu32(t, &volume) < 0 ||
890 pa_tagstruct_gets(t, &name) < 0 ||
891 !pa_tagstruct_eof(t)) {
896 if (!c->authorized) {
897 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
901 if (sink_index != (uint32_t) -1)
902 sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
904 sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
907 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
911 if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
912 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
916 pa_pstream_send_simple_ack(c->pstream, tag);
919 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
920 struct connection *c = userdata;
924 if (pa_tagstruct_gets(t, &name) < 0 ||
925 !pa_tagstruct_eof(t)) {
930 if (!c->authorized) {
931 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
935 if (pa_scache_remove_item(c->protocol->core, name) < 0) {
936 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
940 pa_pstream_send_simple_ack(c->pstream, tag);
943 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
945 pa_tagstruct_putu32(t, sink->index);
946 pa_tagstruct_puts(t, sink->name);
947 pa_tagstruct_puts(t, sink->description ? sink->description : "");
948 pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
949 pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
950 pa_tagstruct_putu32(t, sink->volume);
951 pa_tagstruct_putu32(t, sink->monitor_source->index);
952 pa_tagstruct_puts(t, sink->monitor_source->name);
953 pa_tagstruct_putu32(t, pa_sink_get_latency(sink));
956 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
958 pa_tagstruct_putu32(t, source->index);
959 pa_tagstruct_puts(t, source->name);
960 pa_tagstruct_puts(t, source->description ? source->description : "");
961 pa_tagstruct_put_sample_spec(t, &source->sample_spec);
962 pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
963 pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
964 pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : "");
967 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
968 struct connection *c = userdata;
970 struct pa_sink *sink = NULL;
971 struct pa_source *source = NULL;
973 struct pa_tagstruct *reply;
976 if (pa_tagstruct_getu32(t, &index) < 0 ||
977 pa_tagstruct_gets(t, &name) < 0 ||
978 !pa_tagstruct_eof(t)) {
983 if (!c->authorized) {
984 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
988 if (command == PA_COMMAND_GET_SINK_INFO) {
989 if (index != (uint32_t) -1)
990 sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
992 sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
994 assert(command == PA_COMMAND_GET_SOURCE_INFO);
995 if (index != (uint32_t) -1)
996 source = pa_idxset_get_by_index(c->protocol->core->sources, index);
998 source = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SOURCE, 1);
1001 if (!sink && !source) {
1002 pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
1006 reply = pa_tagstruct_new(NULL, 0);
1008 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1009 pa_tagstruct_putu32(reply, tag);
1011 sink_fill_tagstruct(reply, sink);
1013 source_fill_tagstruct(reply, source);
1014 pa_pstream_send_tagstruct(c->pstream, reply);
1017 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1018 struct connection *c = userdata;
1019 struct pa_idxset *i;
1022 struct pa_tagstruct *reply;
1025 if (!pa_tagstruct_eof(t)) {
1030 if (!c->authorized) {
1031 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1035 reply = pa_tagstruct_new(NULL, 0);
1037 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1038 pa_tagstruct_putu32(reply, tag);
1040 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1041 i = c->protocol->core->sinks;
1043 assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1044 i = c->protocol->core->sources;
1047 for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1048 if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1049 sink_fill_tagstruct(reply, p);
1051 assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1052 source_fill_tagstruct(reply, p);
1056 pa_pstream_send_tagstruct(c->pstream, reply);
1059 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1060 struct connection *c = userdata;
1061 struct pa_tagstruct *reply;
1065 if (!pa_tagstruct_eof(t)) {
1070 if (!c->authorized) {
1071 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1075 reply = pa_tagstruct_new(NULL, 0);
1077 pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1078 pa_tagstruct_putu32(reply, tag);
1079 pa_tagstruct_puts(reply, PACKAGE_NAME);
1080 pa_tagstruct_puts(reply, PACKAGE_VERSION);
1081 pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1082 pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1083 pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1084 pa_pstream_send_tagstruct(c->pstream, reply);
1087 static void subscription_cb(struct pa_core *core, enum pa_subscription_event_type e, uint32_t index, void *userdata) {
1088 struct pa_tagstruct *t;
1089 struct connection *c = userdata;
1092 t = pa_tagstruct_new(NULL, 0);
1094 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
1095 pa_tagstruct_putu32(t, (uint32_t) -1);
1096 pa_tagstruct_putu32(t, e);
1097 pa_tagstruct_putu32(t, index);
1098 pa_pstream_send_tagstruct(c->pstream, t);
1101 static void command_subscribe(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1102 struct connection *c = userdata;
1103 enum pa_subscription_mask m;
1106 if (pa_tagstruct_getu32(t, &m) < 0 ||
1107 !pa_tagstruct_eof(t)) {
1112 if (!c->authorized) {
1113 pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1117 if (c->subscription)
1118 pa_subscription_free(c->subscription);
1121 c->subscription = pa_subscription_new(c->protocol->core, m, subscription_cb, c);
1122 assert(c->subscription);
1124 c->subscription = NULL;
1126 pa_pstream_send_simple_ack(c->pstream, tag);
1130 /*** pstream callbacks ***/
1132 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1133 struct connection *c = userdata;
1134 assert(p && packet && packet->data && c);
1136 if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1137 fprintf(stderr, "protocol-native: invalid packet.\n");
1142 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1143 struct connection *c = userdata;
1144 struct output_stream *stream;
1145 assert(p && chunk && userdata);
1147 if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1148 fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
1153 if (stream->type == PLAYBACK_STREAM) {
1154 struct playback_stream *p = (struct playback_stream*) stream;
1155 if (chunk->length >= p->requested_bytes)
1156 p->requested_bytes = 0;
1158 p->requested_bytes -= chunk->length;
1160 pa_memblockq_push_align(p->memblockq, chunk, delta);
1161 assert(p->sink_input);
1162 pa_sink_notify(p->sink_input->sink);
1163 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1165 struct upload_stream *u = (struct upload_stream*) stream;
1167 assert(u->type == UPLOAD_STREAM);
1169 if (!u->memchunk.memblock) {
1170 if (u->length == chunk->length) {
1171 u->memchunk = *chunk;
1172 pa_memblock_ref(u->memchunk.memblock);
1174 fprintf(stderr, "COPY\n");
1176 u->memchunk.memblock = pa_memblock_new(u->length);
1177 u->memchunk.index = u->memchunk.length = 0;
1181 assert(u->memchunk.memblock);
1184 if (l > chunk->length)
1188 memcpy(u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length, chunk->memblock->data+chunk->index, l);
1189 u->memchunk.length += l;
1195 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1196 struct connection *c = userdata;
1200 fprintf(stderr, "protocol-native: connection died.\n");
1204 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1205 struct connection *c = userdata;
1211 /*** client callbacks ***/
1213 static void client_kill_cb(struct pa_client *c) {
1214 assert(c && c->userdata);
1215 connection_free(c->userdata);
1218 /*** socket server callbacks ***/
1220 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1221 struct pa_protocol_native *p = userdata;
1222 struct connection *c;
1223 assert(s && io && p);
1225 c = pa_xmalloc(sizeof(struct connection));
1226 c->authorized = p->public;
1229 c->client = pa_client_new(p->core, "NATIVE", "Client");
1231 c->client->kill = client_kill_cb;
1232 c->client->userdata = c;
1233 c->client->owner = p->module;
1235 c->pstream = pa_pstream_new(p->core->mainloop, io);
1238 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1239 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1240 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1241 pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1243 c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1244 assert(c->pdispatch);
1246 c->record_streams = pa_idxset_new(NULL, NULL);
1247 c->output_streams = pa_idxset_new(NULL, NULL);
1248 assert(c->record_streams && c->output_streams);
1250 c->rrobin_index = PA_IDXSET_INVALID;
1251 c->subscription = NULL;
1253 pa_idxset_put(p->connections, c, NULL);
1256 /*** module entry points ***/
1258 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) {
1259 struct pa_protocol_native *p;
1261 assert(core && server && ma);
1263 if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
1264 fprintf(stderr, __FILE__": public= expects numeric argument.\n");
1268 p = pa_xmalloc(sizeof(struct pa_protocol_native));
1270 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1279 p->connections = pa_idxset_new(NULL, NULL);
1280 assert(p->connections);
1282 pa_socket_server_set_callback(p->server, on_connection, p);
1287 void pa_protocol_native_free(struct pa_protocol_native *p) {
1288 struct connection *c;
1291 while ((c = pa_idxset_first(p->connections, NULL)))
1293 pa_idxset_free(p->connections, NULL, NULL);
1294 pa_socket_server_free(p->server);